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")
42def get_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
56 for _
in range(NUM_LAYERS):
58 num_inputs = LATENT_SIZE
66 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
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)
81 def __init__(self, num_node_inputs, num_edge_inputs, num_global_inputs):
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)
87 def forward(self, node_data, edge_data, global_data, receivers, senders):
91 [edge_data, node_data[receivers], node_data[senders],
global_data.expand(n_edges, -1)], dim=1
93 edge_output = self.edge_fn(edge_input)
99 node_output = self.node_fn(node_input)
103 global_output = self.global_fn(global_input)
104 return node_output, edge_output, global_output
112 self._core =
MLPGraphNetwork(2 * LATENT_SIZE, 2 * LATENT_SIZE, 2 * LATENT_SIZE)
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)
120 for _
in range(num_processing_steps):
122 latent = self._core(*core_input, receivers, senders)
123 decoded_op = self._decoder(*latent)
142 input_names = [
"node_data",
"edge_data",
"global_data"]
144 sample_input += sample_indices
145 input_names += [
"receivers",
"senders"]
150 input_names=input_names,
151 output_names=[
"node_output",
"edge_output",
"global_output"],
163for name
in [
"gnn_encoder",
"gnn_core",
"gnn_decoder",
"gnn_output_transform"]:
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"
195 def infer(self, graphData):
196 n_nodes =
len(graphData[
"nodes"])
197 n_edges =
len(graphData[
"edges"])
208 latent = self._as_arrays(
209 self.encoder_session.
infer(
c(graphData[
"nodes"]),
c(graphData[
"edges"]),
c(graphData[
"globals"])),
214 for _
in range(processing_steps):
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),
220 decoded = self._as_arrays(
221 self.decoder_session.
infer(
c(latent[0]),
c(latent[1]),
c(latent[2])), n_nodes, n_edges
225 self.output_transform_session.
infer(
c(decoded[0]),
c(decoded[1]),
c(decoded[2])),
249hG =
ROOT.TH1D(
"hG",
"Result from PyTorch", 20, 1, 0)
251for i
in range(numevts):
257print(
"elapsed time for ", numevts,
"events = ", end - start)
260hS =
ROOT.TH1D(
"hS",
"Result from SOFIE", 20, 1, 0)
264print(
"time to create SOFIE GNN class", start - start0)
266for i
in range(numevts):
272print(
"elapsed time for ", numevts,
"events = ", end - start)
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)
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]
293 maxDifference = max(maxDifference,
np.abs(difference).max())
295print(
"maximum difference between SOFIE and PyTorch = ", maxDifference)
296if maxDifference > 1e-4:
297 raise RuntimeError(
"SOFIE and PyTorch outputs disagree")
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