17import graph_nets
as gn
22from graph_nets
import utils_tf
38def printMemory(s = "") :
41 python_process = psutil.Process(pid)
42 memoryUse = python_process.memory_info()[0]/(1024.*1024.)
43 print(s,
"memory:",memoryUse,
"(MB)")
47def get_dynamic_graph_data_dict(NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
48 num_nodes = np.random.randint(num_max_nodes-2, size=1)[0] + 2
49 num_edges = np.random.randint(num_max_edges-1, size=1)[0] + 1
51 "globals": 10*np.random.rand(GLOBAL_FEATURE_SIZE).astype(np.float32)-5.,
52 "nodes": 10*np.random.rand(num_nodes, NODE_FEATURE_SIZE).astype(np.float32)-5.,
53 "edges": 10*np.random.rand(num_edges, EDGE_FEATURE_SIZE).astype(np.float32)-5.,
54 "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int32),
55 "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int32)
59def get_fix_graph_data_dict(num_nodes, num_edges, NODE_FEATURE_SIZE=2, EDGE_FEATURE_SIZE=2, GLOBAL_FEATURE_SIZE=1):
61 "globals": np.ones((GLOBAL_FEATURE_SIZE),dtype=np.float32),
62 "nodes": np.ones((num_nodes, NODE_FEATURE_SIZE), dtype = np.float32),
63 "edges": np.ones((num_edges, EDGE_FEATURE_SIZE), dtype = np.float32),
64 "senders": np.random.randint(num_nodes, size=num_edges, dtype=np.int32),
65 "receivers": np.random.randint(num_nodes, size=num_edges, dtype=np.int32)
73 return snt.Sequential([
74 snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
75 snt.LayerNorm(axis=-1, create_offset=
True, create_scale=
True)
79class MLPGraphIndependent(snt.Module):
80 def __init__(self, name="MLPGraphIndependent"):
81 super(MLPGraphIndependent, self).__init__(name=name)
82 self._network = gn.modules.GraphIndependent(
83 edge_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
84 node_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True),
85 global_model_fn =
lambda: snt.nets.MLP([LATENT_SIZE]*NUM_LAYERS, activate_final=
True))
87 def __call__(self, inputs):
88 return self._network(inputs)
91class MLPGraphNetwork(snt.Module):
92 def __init__(self, name="MLPGraphNetwork"):
93 super(MLPGraphNetwork, self).__init__(name=name)
94 self._network = gn.modules.GraphNetwork(
95 edge_model_fn=make_mlp_model,
96 node_model_fn=make_mlp_model,
97 global_model_fn=make_mlp_model)
99 def __call__(self, inputs):
100 return self._network(inputs)
103class EncodeProcessDecode(snt.Module):
106 name="EncodeProcessDecode"):
107 super(EncodeProcessDecode, self).__init__(name=name)
108 self._encoder = MLPGraphIndependent()
109 self._core = MLPGraphNetwork()
110 self._decoder = MLPGraphIndependent()
111 self._output_transform = MLPGraphIndependent()
113 def __call__(self, input_op, num_processing_steps):
114 latent = self._encoder(input_op)
117 for _
in range(num_processing_steps):
118 core_input = utils_tf.concat([latent0, latent], axis=1)
119 latent = self._core(core_input)
120 decoded_op = self._decoder(latent)
121 output_ops.append(self._output_transform(decoded_op))
129printMemory(
"before instantiating")
130ep_model = EncodeProcessDecode()
131printMemory(
"after instantiating")
134GraphData = get_fix_graph_data_dict(num_max_nodes, num_max_edges, node_size, edge_size, global_size)
137input_graph_data = utils_tf.data_dicts_to_graphs_tuple([GraphData])
141CoreGraphData = get_fix_graph_data_dict(num_max_nodes, num_max_edges, 2*LATENT_SIZE, 2*LATENT_SIZE, 2*LATENT_SIZE)
142input_core_graph_data = utils_tf.data_dicts_to_graphs_tuple([CoreGraphData])
145DecodeGraphData = get_fix_graph_data_dict(num_max_nodes, num_max_edges, LATENT_SIZE, LATENT_SIZE, LATENT_SIZE)
148printMemory(
"before first eval")
149output_gn = ep_model(input_graph_data, processing_steps)
150printMemory(
"after first eval")
157encoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._encoder._network, GraphData, filename =
"encoder")
159encoder.OutputGenerated()
161core = ROOT.TMVA.Experimental.SOFIE.RModel_GNN.ParseFromMemory(ep_model._core._network, CoreGraphData, filename =
"core")
163core.OutputGenerated()
165decoder = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._decoder._network, DecodeGraphData, filename =
"decoder")
167decoder.OutputGenerated()
169output_transform = ROOT.TMVA.Experimental.SOFIE.RModel_GraphIndependent.ParseFromMemory(ep_model._output_transform._network, DecodeGraphData, filename =
"output_transform")
170output_transform.Generate()
171output_transform.OutputGenerated()
178fileOut = ROOT.TFile.Open(
"graph_data.root",
"RECREATE")
179tree = ROOT.TTree(
"gdata",
"GNN data")
182node_data = ROOT.std.vector[
'float'](num_max_nodes*node_size)
183edge_data = ROOT.std.vector[
'float'](num_max_edges*edge_size)
184global_data = ROOT.std.vector[
'float'](global_size)
185receivers = ROOT.std.vector[
'int'](num_max_edges)
186senders = ROOT.std.vector[
'int'](num_max_edges)
187outgnn = ROOT.std.vector[
'float'](3)
189tree.Branch(
"node_data",
"std::vector<float>" , node_data)
190tree.Branch(
"edge_data",
"std::vector<float>" , edge_data)
191tree.Branch(
"global_data",
"std::vector<float>" , global_data)
192tree.Branch(
"receivers",
"std::vector<int>" , receivers)
193tree.Branch(
"senders",
"std::vector<int>" , senders)
196print(
"\n\nSaving data in a ROOT File:")
197h1 = ROOT.TH1D(
"h1",
"GraphNet nodes output",40,1,0)
198h2 = ROOT.TH1D(
"h2",
"GraphNet edges output",40,1,0)
199h3 = ROOT.TH1D(
"h3",
"GraphNet global output",40,1,0)
201for i
in range(0,numevts):
202 graphData = get_dynamic_graph_data_dict(node_size, edge_size, global_size)
203 s_nodes = graphData[
'nodes'].size
204 s_edges = graphData[
'edges'].size
205 num_edges = graphData[
'edges'].shape[0]
206 tmp = ROOT.std.vector[
'float'](graphData[
'nodes'].reshape((graphData[
'nodes'].size)))
207 node_data.assign(tmp.begin(),tmp.end())
208 tmp = ROOT.std.vector[
'float'](graphData[
'edges'].reshape((graphData[
'edges'].size)))
209 edge_data.assign(tmp.begin(),tmp.end())
210 tmp = ROOT.std.vector[
'float'](graphData[
'globals'].reshape((graphData[
'globals'].size)))
211 global_data.assign(tmp.begin(),tmp.end())
213 tmp = ROOT.std.vector[
'int'](graphData[
'receivers'])
214 receivers.assign(tmp.begin(),tmp.end())
215 tmp = ROOT.std.vector[
'int'](graphData[
'senders'])
216 senders.assign(tmp.begin(),tmp.end())
217 if (i < 1
and verbose) :
218 print(
"Nodes - shape:",
int(node_data.size()/node_size),node_size,
"data: ",node_data)
219 print(
"Edges - shape:",num_edges, edge_size,
"data: ", edge_data)
220 print(
"Globals : ",global_data)
221 print(
"Receivers : ",receivers)
222 print(
"Senders : ",senders)
227 tf_graph_data = utils_tf.data_dicts_to_graphs_tuple([graphData])
228 dataset.append(tf_graph_data)
233printMemory(
"before eval1")
234output_gnn = ep_model(dataset[0], processing_steps)
235printMemory(
"after eval1")
239for tf_graph_data
in dataset:
240 output_gnn = ep_model(tf_graph_data, processing_steps)
241 output_nodes = output_gnn[-1].nodes.numpy()
242 output_edges = output_gnn[-1].edges.numpy()
243 output_globals = output_gnn[-1].globals.numpy()
244 outgnn[0] = np.mean(output_nodes)
245 outgnn[1] = np.mean(output_edges)
246 outgnn[2] = np.mean(output_globals)
250 if (firstEvent
and verbose) :
251 print(
"Output of first event")
252 print(
"nodes data", output_gnn[-1].nodes.numpy())
253 print(
"edge data", output_gnn[-1].edges.numpy())
254 print(
"global data", output_gnn[-1].globals.numpy())
260print(
"time to evaluate events",end-start)
261printMemory(
"after eval Nevts")