Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_GNN.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_ml
3## \notebook -nodraw
4##
5## Tutorial showing inference of a Graph Neural Network with SOFIE.
6##
7## A graph network model following DeepMind's Encode-Process-Decode architecture
8## (see arXiv:1806.01261) is defined in PyTorch and exported to ONNX. The ONNX
9## models are then parsed with the SOFIE ONNX parser, C++ inference code is
10## generated and compiled, and its output is validated against PyTorch.
11##
12## \macro_code
13##
14## \author
15
16import cppyy
17import time
18
19import numpy as np
20import ROOT
21import torch
22import torch.nn as nn
23
24# defining graph properties
25num_nodes = 5
26num_edges = 20
27snd = np.array([1, 2, 3, 4, 2, 3, 4, 3, 4, 4, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3], dtype="int64")
28rec = np.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 1, 2, 3, 4, 2, 3, 4, 3, 4, 4], dtype="int64")
29node_size = 4
30edge_size = 4
31global_size = 1
32LATENT_SIZE = 100
33NUM_LAYERS = 4
34processing_steps = 5
35numevts = 40
36
39
40
41# method for returning dictionary of graph data
42def get_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
43 return {
44 "globals": 10 * np.random.rand(1, GLOBAL_FEATURE_SIZE).astype(np.float32) - 5.0,
45 "nodes": 10 * np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32) - 5.0,
46 "edges": 10 * np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32) - 5.0,
47 "senders": snd,
48 "receivers": rec,
49 }
50
51
52# method to instantiate an MLP model to be added in the GNN
53# (a stack of Linear+ReLU layers, with a final LayerNorm for the core network)
54def make_mlp_model(num_inputs, with_layer_norm=False):
55 layers = []
56 for _ in range(NUM_LAYERS):
57 layers += [nn.Linear(num_inputs, LATENT_SIZE), nn.ReLU()]
58 num_inputs = LATENT_SIZE
59 if with_layer_norm:
60 layers.append(nn.LayerNorm(LATENT_SIZE))
61 return nn.Sequential(*layers)
62
63
64# module applying independent MLPs to the node, edge and global features
66 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
67 super().__init__()
68 self.node_fn = make_mlp_model(num_node_inputs)
69 self.edge_fn = make_mlp_model(num_edge_inputs)
70 self.global_fn = make_mlp_model(num_global_inputs)
71
72 def forward(self, node_data, edge_data, global_data):
73 return self.node_fn(node_data), self.edge_fn(edge_data), self.global_fn(global_data)
74
75
76# module implementing a full graph-network block (see arXiv:1806.01261):
77# - edge update from [edge, receiver node, sender node, global]
78# - node update from [sum of received edges, node, global]
79# - global update from [sum of edges, sum of nodes, global]
81 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
82 super().__init__()
83 self.edge_fn = make_mlp_model(num_edge_inputs + 2 * num_node_inputs + num_global_inputs, True)
84 self.node_fn = make_mlp_model(LATENT_SIZE + num_node_inputs + num_global_inputs, True)
85 self.global_fn = make_mlp_model(2 * LATENT_SIZE + num_global_inputs, True)
86
87 def forward(self, node_data, edge_data, global_data, receivers, senders):
88 n_nodes = node_data.shape[0]
89 n_edges = edge_data.shape[0]
90 edge_input = torch.cat(
91 [edge_data, node_data[receivers], node_data[senders], global_data.expand(n_edges, -1)], dim=1
92 )
93 edge_output = self.edge_fn(edge_input)
94 # aggregate the updated edge data per receiving node
95 received_edges = torch.zeros(n_nodes, edge_output.shape[1]).scatter_add(
96 0, receivers.unsqueeze(1).expand(n_edges, edge_output.shape[1]), edge_output
97 )
98 node_input = torch.cat([received_edges, node_data, global_data.expand(n_nodes, -1)], dim=1)
99 node_output = self.node_fn(node_input)
100 global_input = torch.cat(
101 [edge_output.sum(0, keepdim=True), node_output.sum(0, keepdim=True), global_data], dim=1
102 )
103 global_output = self.global_fn(global_input)
104 return node_output, edge_output, global_output
105
106
107# defining a Encode-Process-Decode module for LHCb toy model
109 def __init__(self):
110 super().__init__()
111 self._encoder = MLPGraphIndependent(node_size, edge_size, global_size)
112 self._core = MLPGraphNetwork(2 * LATENT_SIZE, 2 * LATENT_SIZE, 2 * LATENT_SIZE)
113 self._decoder = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE)
114 self._output_transform = MLPGraphIndependent(LATENT_SIZE, LATENT_SIZE, LATENT_SIZE)
115
116 def forward(self, node_data, edge_data, global_data, receivers, senders, num_processing_steps):
117 latent = self._encoder(node_data, edge_data, global_data)
118 latent0 = latent
119 output_ops = []
120 for _ in range(num_processing_steps):
121 core_input = tuple(torch.cat([a, b], dim=1) for a, b in zip(latent0, latent))
122 latent = self._core(*core_input, receivers, senders)
123 decoded_op = self._decoder(*latent)
124 output_ops.append(self._output_transform(*decoded_op))
125 return output_ops
126
127
128# Instantiating EncodeProcessDecode Model
129ep_model = EncodeProcessDecode()
131
132# Export the four component models to ONNX
133sample_indices = (torch.from_numpy(rec), torch.from_numpy(snd))
134
135
136def export_component(component, name, num_features):
137 sample_input = (
138 torch.zeros(num_nodes, num_features[0]),
139 torch.zeros(num_edges, num_features[1]),
140 torch.zeros(1, num_features[2]),
141 )
142 input_names = ["node_data", "edge_data", "global_data"]
143 if isinstance(component, MLPGraphNetwork):
144 sample_input += sample_indices
145 input_names += ["receivers", "senders"]
147 component,
148 sample_input,
149 name + ".onnx",
150 input_names=input_names,
151 output_names=["node_output", "edge_output", "global_output"],
152 dynamo=True,
153 )
154
155
156export_component(ep_model._encoder, "gnn_encoder", (node_size, edge_size, global_size))
157export_component(ep_model._core, "gnn_core", (2 * LATENT_SIZE,) * 3)
158export_component(ep_model._decoder, "gnn_decoder", (LATENT_SIZE,) * 3)
159export_component(ep_model._output_transform, "gnn_output_transform", (LATENT_SIZE,) * 3)
160
161# Parse the ONNX models with SOFIE and generate the C++ inference code
163for name in ["gnn_encoder", "gnn_core", "gnn_decoder", "gnn_output_transform"]:
164 model = parser.Parse(name + ".onnx")
167
168# Compile now the generated C++ code from SOFIE
169gen_code = """#pragma cling optimize(2)
170#include "gnn_encoder.hxx"
171#include "gnn_core.hxx"
172#include "gnn_decoder.hxx"
173#include "gnn_output_transform.hxx"
174"""
176
177
178# Build SOFIE GNN Model and run inference
179class SofieGNN:
180 def __init__(self):
181 self.encoder_session = ROOT.TMVA_SOFIE_gnn_encoder.Session()
182 self.core_session = ROOT.TMVA_SOFIE_gnn_core.Session()
183 self.decoder_session = ROOT.TMVA_SOFIE_gnn_decoder.Session()
184 self.output_transform_session = ROOT.TMVA_SOFIE_gnn_output_transform.Session()
185
186 @staticmethod
187 def _as_arrays(result, num_nodes, num_edges):
188 # a session returns the flat node, edge and global output tensors
189 return (
190 np.asarray(result[0], dtype=np.float32).reshape(num_nodes, -1),
191 np.asarray(result[1], dtype=np.float32).reshape(num_edges, -1),
192 np.asarray(result[2], dtype=np.float32).reshape(1, -1),
193 )
194
195 def infer(self, graphData):
196 n_nodes = len(graphData["nodes"])
197 n_edges = len(graphData["edges"])
198
199 def c(x):
200 return np.ascontiguousarray(x, dtype=np.float32)
201
202 receivers = np.ascontiguousarray(graphData["receivers"], dtype=np.int64)
203 senders = np.ascontiguousarray(graphData["senders"], dtype=np.int64)
204 i64p = cppyy.ll.cast["const int64_t*"]
205 receivers_ptr = i64p(receivers.ctypes.data)
206 senders_ptr = i64p(senders.ctypes.data)
207
208 latent = self._as_arrays(
209 self.encoder_session.infer(c(graphData["nodes"]), c(graphData["edges"]), c(graphData["globals"])),
210 n_nodes, n_edges,
211 )
212 latent0 = latent
213 output_ops = []
214 for _ in range(processing_steps):
215 core_input = tuple(np.concatenate([a, b], axis=1) for a, b in zip(latent0, latent))
216 latent = self._as_arrays(
217 self.core_session.infer(c(core_input[0]), c(core_input[1]), c(core_input[2]), receivers_ptr, senders_ptr),
218 n_nodes, n_edges,
219 )
220 decoded = self._as_arrays(
221 self.decoder_session.infer(c(latent[0]), c(latent[1]), c(latent[2])), n_nodes, n_edges
222 )
224 self._as_arrays(
225 self.output_transform_session.infer(c(decoded[0]), c(decoded[1]), c(decoded[2])),
226 n_nodes, n_edges,
227 )
228 )
229 return output_ops
230
231
232# Test both GNN on some simulated events
233dataSet = [get_graph_data_dict(num_nodes, num_edges, node_size, edge_size, global_size) for i in range(numevts)]
234
235
236# Function to run the PyTorch model
237def RunGNet(graphData):
238 return ep_model(
239 torch.from_numpy(graphData["nodes"]),
240 torch.from_numpy(graphData["edges"]),
241 torch.from_numpy(graphData["globals"]),
242 torch.from_numpy(graphData["receivers"]),
243 torch.from_numpy(graphData["senders"]),
244 processing_steps,
245 )
246
247
248start = time.time()
249hG = ROOT.TH1D("hG", "Result from PyTorch", 20, 1, 0)
250torchOutput = []
251for i in range(numevts):
252 out = RunGNet(dataSet[i])
253 torchOutput.append([[t.numpy() for t in step] for step in out])
254 hG.Fill(np.mean(torchOutput[-1][1][2]))
255
256end = time.time()
257print("elapsed time for ", numevts, "events = ", end - start)
258
259# running SOFIE-GNN
260hS = ROOT.TH1D("hS", "Result from SOFIE", 20, 1, 0)
261start0 = time.time()
262gnn = SofieGNN()
263start = time.time()
264print("time to create SOFIE GNN class", start - start0)
265sofieOutput = []
266for i in range(numevts):
267 out = gnn.infer(dataSet[i])
269 hS.Fill(np.mean(out[1][2]))
270
271end = time.time()
272print("elapsed time for ", numevts, "events = ", end - start)
273
274c0 = ROOT.TCanvas()
275c0.Divide(1, 2)
276c1 = c0.cd(1)
277c1.Divide(2, 1)
278c1.cd(1)
279hG.Draw()
280c1.cd(2)
281hS.Draw()
282
283hDn = ROOT.TH1D("hDn", "Difference for node data", 40, 1, 0)
284hDe = ROOT.TH1D("hDe", "Difference for edge data", 40, 1, 0)
285hDg = ROOT.TH1D("hDg", "Difference for global data", 40, 1, 0)
286# compute differences between SOFIE and PyTorch
287maxDifference = 0.0
288for i in range(numevts):
289 for hist, j in [(hDn, 0), (hDe, 1), (hDg, 2)]:
290 difference = sofieOutput[i][1][j] - torchOutput[i][1][j]
291 for value in difference.flatten():
292 hist.Fill(value)
293 maxDifference = max(maxDifference, np.abs(difference).max())
294
295print("maximum difference between SOFIE and PyTorch = ", maxDifference)
296if maxDifference > 1e-4:
297 raise RuntimeError("SOFIE and PyTorch outputs disagree")
298
299c2 = c0.cd(2)
300c2.Divide(3, 1)
301c2.cd(1)
302hDn.Draw()
303c2.cd(2)
304hDe.Draw()
305c2.cd(3)
306hDg.Draw()
307
308c0.Draw()
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len