Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_GNN_Parser.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_ml
3## \notebook -nodraw
4##
5## Tutorial parsing a Graph Neural Network from ONNX and generating SOFIE
6## inference code.
7##
8## A graph network model following DeepMind's Encode-Process-Decode architecture
9## (see arXiv:1806.01261) is defined in PyTorch and exported to ONNX with
10## dynamic node and edge counts. The SOFIE ONNX parser then generates C++
11## inference code for the four component networks. The tutorial also generates
12## input data, evaluated here with PyTorch as a reference, which serves as
13## input for the tutorial TMVA_SOFIE_GNN_Application.C.
14##
15## \macro_code
16##
17## \author
18
19import time
20
21import numpy as np
22import ROOT
23import torch
24import torch.nn as nn
25
26# defining graph properties. Number of nodes/edges are the maximum
27num_max_nodes = 100
28num_max_edges = 300
29node_size = 4
30edge_size = 4
31global_size = 1
32LATENT_SIZE = 100
33NUM_LAYERS = 4
34processing_steps = 5
35numevts = 100
36
39
40
41# method for returning dictionary of graph data
42def get_dynamic_graph_data_dict(NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
43 num_nodes = np.random.randint(num_max_nodes - 2, size=1)[0] + 2
44 num_edges = np.random.randint(num_max_edges - 2, size=1)[0] + 2
45 return {
46 "globals": 10 * np.random.rand(1, GLOBAL_FEATURE_SIZE).astype(np.float32) - 5.0,
47 "nodes": 10 * np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32) - 5.0,
48 "edges": 10 * np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32) - 5.0,
49 "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int64),
50 "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int64),
51 }
52
53
54# method to instantiate an MLP model to be added in the GNN
55# (a stack of Linear+ReLU layers, with a final LayerNorm for the core network)
56def make_mlp_model(num_inputs, with_layer_norm=False):
57 layers = []
58 for _ in range(NUM_LAYERS):
59 layers += [nn.Linear(num_inputs, LATENT_SIZE), nn.ReLU()]
60 num_inputs = LATENT_SIZE
61 if with_layer_norm:
62 layers.append(nn.LayerNorm(LATENT_SIZE))
63 return nn.Sequential(*layers)
64
65
66# module applying independent MLPs to the node, edge and global features
68 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
69 super().__init__()
70 self.node_fn = make_mlp_model(num_node_inputs)
71 self.edge_fn = make_mlp_model(num_edge_inputs)
72 self.global_fn = make_mlp_model(num_global_inputs)
73
74 def forward(self, node_data, edge_data, global_data):
75 return self.node_fn(node_data), self.edge_fn(edge_data), self.global_fn(global_data)
76
77
78# module implementing a full graph-network block (see arXiv:1806.01261):
79# - edge update from [edge, receiver node, sender node, global]
80# - node update from [sum of received edges, node, global]
81# - global update from [sum of edges, sum of nodes, global]
83 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
84 super().__init__()
85 self.edge_fn = make_mlp_model(num_edge_inputs + 2 * num_node_inputs + num_global_inputs, True)
86 self.node_fn = make_mlp_model(LATENT_SIZE + num_node_inputs + num_global_inputs, True)
87 self.global_fn = make_mlp_model(2 * LATENT_SIZE + num_global_inputs, True)
88
89 def forward(self, node_data, edge_data, global_data, receivers, senders):
90 n_nodes = node_data.shape[0]
91 n_edges = edge_data.shape[0]
92 edge_input = torch.cat(
93 [edge_data, node_data[receivers], node_data[senders], global_data.expand(n_edges, -1)], dim=1
94 )
95 edge_output = self.edge_fn(edge_input)
96 # aggregate the updated edge data per receiving node
97 received_edges = torch.zeros(n_nodes, edge_output.shape[1]).scatter_add(
98 0, receivers.unsqueeze(1).expand(n_edges, edge_output.shape[1]), edge_output
99 )
100 node_input = torch.cat([received_edges, node_data, global_data.expand(n_nodes, -1)], dim=1)
101 node_output = self.node_fn(node_input)
102 global_input = torch.cat(
103 [edge_output.sum(0, keepdim=True), node_output.sum(0, keepdim=True), global_data], dim=1
104 )
105 global_output = self.global_fn(global_input)
106 return node_output, edge_output, global_output
107
108
109# defining a Encode-Process-Decode module for LHCb toy model
111 def __init__(self):
112 super().__init__()
113 self._encoder = MLPGraphIndependent(node_size, edge_size, global_size)
114 self._core = MLPGraphNetwork(2 * LATENT_SIZE, 2 * LATENT_SIZE, 2 * LATENT_SIZE)
115 self._decoder = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE)
116 self._output_transform = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE)
117
118 def forward(self, node_data, edge_data, global_data, receivers, senders, num_processing_steps):
119 latent = self._encoder(node_data, edge_data, global_data)
120 latent0 = latent
121 output_ops = []
122 for _ in range(num_processing_steps):
123 core_input = tuple(torch.cat([a, b], dim=1) for a, b in zip(latent0, latent))
124 latent = self._core(*core_input, receivers, senders)
125 decoded_op = self._decoder(*latent)
126 output_ops.append(self._output_transform(*decoded_op))
127 return output_ops
128
129
130########################################################################################################
131
132# Instantiating EncodeProcessDecode Model
133ep_model = EncodeProcessDecode()
135
136# Export the four component models to ONNX, with dynamic node and edge counts
137num_nodes_dim = torch.export.Dim("num_nodes", min=2, max=num_max_nodes)
138num_edges_dim = torch.export.Dim("num_edges", min=2, max=num_max_edges)
139
140
141def export_component(component, name, num_features):
142 sample_input = (
143 torch.zeros(num_max_nodes, num_features[0]),
144 torch.zeros(num_max_edges, num_features[1]),
145 torch.zeros(1, num_features[2]),
146 )
147 input_names = ["node_data", "edge_data", "global_data"]
148 dynamic_shapes = {
149 "node_data": {0: num_nodes_dim},
150 "edge_data": {0: num_edges_dim},
151 "global_data": None,
152 }
153 if isinstance(component, MLPGraphNetwork):
154 sample_input += (
155 torch.randint(num_max_nodes, (num_max_edges,)),
156 torch.randint(num_max_nodes, (num_max_edges,)),
157 )
158 input_names += ["receivers", "senders"]
159 dynamic_shapes.update({"receivers": {0: num_edges_dim}, "senders": {0: num_edges_dim}})
161 component,
162 sample_input,
163 name + ".onnx",
164 input_names=input_names,
165 output_names=["node_output", "edge_output", "global_output"],
166 dynamic_shapes=dynamic_shapes,
167 dynamo=True,
168 )
169
170
171export_component(ep_model._encoder, "encoder", (node_size, edge_size, global_size))
172export_component(ep_model._core, "core", (2 * LATENT_SIZE,) * 3)
173export_component(ep_model._decoder, "decoder", (LATENT_SIZE,) * 3)
174export_component(ep_model._output_transform, "output_transform", (LATENT_SIZE,) * 3)
175
176# Make the SOFIE models: parse the ONNX files and generate the inference code
178for name in ["encoder", "core", "decoder", "output_transform"]:
179 model = parser.Parse(name + ".onnx")
182 print("generated SOFIE model", name + ".hxx")
183
184####################################################################################################################################
185
186# generate data and save in a ROOT TTree
187fileOut = ROOT.TFile.Open("graph_data.root", "RECREATE")
188tree = ROOT.TTree("gdata", "GNN data")
189
190node_data = ROOT.std.vector["float"]()
191edge_data = ROOT.std.vector["float"]()
192global_data = ROOT.std.vector["float"]()
193receivers = ROOT.std.vector["int"]()
194senders = ROOT.std.vector["int"]()
195
196tree.Branch("node_data", "std::vector<float>", node_data)
197tree.Branch("edge_data", "std::vector<float>", edge_data)
198tree.Branch("global_data", "std::vector<float>", global_data)
199tree.Branch("receivers", "std::vector<int>", receivers)
200tree.Branch("senders", "std::vector<int>", senders)
201
202print("\n\nSaving data in a ROOT File:")
203h1 = ROOT.TH1D("h1", "GNN nodes output", 40, 1, 0)
204h2 = ROOT.TH1D("h2", "GNN edges output", 40, 1, 0)
205h3 = ROOT.TH1D("h3", "GNN global output", 40, 1, 0)
206dataset = []
207for i in range(numevts):
208 graphData = get_dynamic_graph_data_dict(node_size, edge_size, global_size)
209 node_data.assign(graphData["nodes"].flatten())
210 edge_data.assign(graphData["edges"].flatten())
211 global_data.assign(graphData["globals"].flatten())
212 receivers.assign(graphData["receivers"].astype(np.int32))
213 senders.assign(graphData["senders"].astype(np.int32))
214 tree.Fill()
215 dataset.append(graphData)
216
218
219# evaluate the reference PyTorch model on these events
220start = time.time()
221for graphData in dataset:
222 output_gnn = ep_model(
223 torch.from_numpy(graphData["nodes"]),
224 torch.from_numpy(graphData["edges"]),
225 torch.from_numpy(graphData["globals"]),
226 torch.from_numpy(graphData["receivers"]),
227 torch.from_numpy(graphData["senders"]),
228 processing_steps,
229 )
230 h1.Fill(np.mean(output_gnn[-1][0].numpy()))
231 h2.Fill(np.mean(output_gnn[-1][1].numpy()))
232 h3.Fill(np.mean(output_gnn[-1][2].numpy()))
233
234end = time.time()
235print("time to evaluate ", numevts, " events", end - start)
236
237c1 = ROOT.TCanvas()
238c1.Divide(1, 3)
239c1.cd(1)
241c1.cd(2)
243c1.cd(3)
245
247h1.Write()
248h2.Write()
249h3.Write()
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.