Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModel_GNN.cxx
Go to the documentation of this file.
1#include <algorithm>
2#include <cctype>
3#include <fstream>
4#include <limits>
5
6#include "TMVA/RModel_GNN.hxx"
7#include "TMVA/RFunction.hxx"
8
9namespace TMVA {
10namespace Experimental {
11namespace SOFIE {
12
14 edges_update_block = std::move(graph_input_struct.edges_update_block);
15 nodes_update_block = std::move(graph_input_struct.nodes_update_block);
16 globals_update_block = std::move(graph_input_struct.globals_update_block);
17
18 edge_node_agg_block = std::move(graph_input_struct.edge_node_agg_block);
19 edge_global_agg_block = std::move(graph_input_struct.edge_global_agg_block);
20 node_global_agg_block = std::move(graph_input_struct.node_global_agg_block);
21
22 num_nodes = graph_input_struct.num_nodes;
23 num_edges = graph_input_struct.edges.size();
24 num_node_features = graph_input_struct.num_node_features;
25 num_edge_features = graph_input_struct.num_edge_features;
26 num_global_features = graph_input_struct.num_global_features;
27
29 fName = fFileName.substr(0, fFileName.rfind("."));
30
31 std::time_t ttime = std::time(0);
32 std::tm* gmt_time = std::gmtime(&ttime);
33 fParseTime = std::asctime(gmt_time);
34}
35
37 std::string hgname;
38 // the inference interface uses the GNN_Data helper type
39 AddNeededHelperFunction("GNN_Data");
41
42 std::ofstream f;
43 f.open(fName+".dat");
44 f.close();
45
46 // Generating Infer function definition for Edge Update function
47 long next_pos;
48 //size_t block_size = num_edges;
49 fGC+="\n\nnamespace Edge_Update{\nstruct Session {\n";
50 // there are 4 input tensors for edge updates: {edges, receiver nodes, sender nodes, globals }
51 std::vector<std::vector<Dim>> update_input_edges(4);
56 edges_update_block->Initialize();
57 edges_update_block->AddInputTensors(update_input_edges);
58 fGC+=edges_update_block->GenerateModel(fName);
59 next_pos = edges_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
60 fGC+="};\n}\n";
61
62 // the number of output edges features can be smaller, so we need to correct here
64 auto edges_update_output_shape = edges_update_block->GetFunctionBlock()->GetDynamicTensorShape(edges_update_block->GetFunctionBlock()->GetOutputTensorNames()[0]);
67 }
68
69 fGC+="\n\nnamespace Node_Update{\nstruct Session {\n";
70 // Generating Infer function definition for Node Update function
71 // num_node_features is the output one
72
73 //block_size = num_nodes;
74 // there are 3 input tensors for node updates: {received edges, nodes, globals }
75 std::vector<std::vector<Dim>> update_input_nodes(3);
79 nodes_update_block->Initialize();
80 nodes_update_block->AddInputTensors(update_input_nodes);
81 fGC+=nodes_update_block->GenerateModel(fName,next_pos);
82 next_pos = nodes_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
83 fGC+="};\n}\n";
84
85 // we need to correct the output number of node features
87 auto nodes_update_output_shape = nodes_update_block->GetFunctionBlock()->GetDynamicTensorShape(nodes_update_block->GetFunctionBlock()->GetOutputTensorNames()[0]);
90 }
91
92 fGC+="\n\nnamespace Global_Update{\nstruct Session {\n";
93 // Generating Infer function definition for Global Update function
94 std::vector<std::vector<std::size_t>> update_input_globals = {{1, num_edge_features},{1, num_node_features},{1, num_global_features}};
95 globals_update_block->Initialize();
97 fGC+=globals_update_block->GenerateModel(fName,next_pos);
98 next_pos = globals_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
99 fGC+="};\n}\n";
100
101 // correct for difference in global size (check shape[1] of output of the globals update)
103 if(globals_update_block->GetFunctionBlock()->GetTensorShape(globals_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1] != num_global_features) {
104 num_global_features = globals_update_block->GetFunctionBlock()->GetTensorShape(globals_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1];
105 }
106
107 fGC+=edge_node_agg_block->GenerateModel();
108
109 if(edge_node_agg_block->GetFunctionType() != edge_global_agg_block->GetFunctionType()) {
110 fGC+=edge_global_agg_block->GenerateModel();
111 }
112 if((edge_node_agg_block->GetFunctionType() != node_global_agg_block->GetFunctionType()) && (edge_global_agg_block->GetFunctionType() != node_global_agg_block->GetFunctionType())) {
113 fGC+=node_global_agg_block->GenerateModel();
114 }
115 fGC+="\n\n";
116
117 // computing inplace on input graph
118 fGC += "struct Session {\n";
119 fGC += "\n// Instantiating session objects for graph components\n";
120 fGC += "Edge_Update::Session edge_update;\n";
121 fGC += "Node_Update::Session node_update;\n";
122 fGC += "Global_Update::Session global_update;\n\n";
123
124 std::string e_num = std::to_string(num_edges);
125 std::string n_num = std::to_string(num_nodes);
126 std::string e_size_input = std::to_string(num_edge_features_input);
127 std::string n_size_input = std::to_string(num_node_features_input);
128 std::string g_size_input = std::to_string(num_global_features_input);
129 std::string e_size = std::to_string(num_edge_features);
130 std::string n_size = std::to_string(num_node_features);
131 std::string g_size = std::to_string(num_global_features);
132
133 // create temp vector for edge and node updates
134 fGC += "std::vector<float> fEdgeUpdates = std::vector<float>(" + e_num + "*" + e_size + ");\n";
135 fGC += "\n\nstd::vector<float> fNodeUpdates = std::vector<float>(" + n_num + "*" + n_size + ");\n";
136
137 fGC += "\n// input vectors for edge update\n";
138 fGC += "std::vector<float> fEdgeInputs = std::vector<float>(" + e_num + "*" + e_size_input + ");\n";
139 fGC += "std::vector<float> fRecNodeInputs = std::vector<float>(" + e_num + "*" + n_size_input + ");\n";
140 fGC += "std::vector<float> fSndNodeInputs = std::vector<float>(" + e_num + "*" + n_size_input + ");\n";
141 fGC += "std::vector<float> fGlobInputs = std::vector<float>(" + e_num + "*" + g_size_input + ");\n\n";
142
143 fGC += "\n// input vectors for node update\n";
144 fGC += "std::vector<float> fNodeInputs = std::vector<float>(" + n_num + "*" + n_size_input + ");\n";
145 fGC += "std::vector<float> fNodeEdgeAggregate = std::vector<float>(" + n_num + "*" + n_size_input + ", 0);\n";
146 fGC += "std::vector<float> fNodeAggregateTemp;\n";
147
148 fGC += "\nvoid infer(GNN_Data& input_graph){\n";
149
150 // computing updated edge attributes
151 fGC += "\n// --- Edge Update ---\n";
152 fGC += "size_t n_edges = input_graph.edge_data.GetShape()[0];\n";
153 fGC += "if (n_edges > " + e_num + ")\n";
154 fGC += " throw std::runtime_error(\"Number of input edges larger than " + e_num + "\" );\n\n";
155 fGC += "auto receivers = input_graph.edge_index.GetData();\n";
156 fGC += "auto senders = input_graph.edge_index.GetData() + n_edges;\n";
157
158 fGC += "for (size_t k = 0; k < n_edges; k++) { \n";
159 fGC += " std::copy(input_graph.edge_data.GetData() + k * " + e_size_input +
160 ", input_graph.edge_data.GetData() + (k + 1) * " + e_size_input +
161 ", fEdgeInputs.begin() + k * " + e_size_input + ");\n";
162 fGC += " std::copy(input_graph.node_data.GetData() + receivers[k] * " + n_size_input +
163 ", input_graph.node_data.GetData() + (receivers[k] + 1) * " + n_size_input +
164 ", fRecNodeInputs.begin() + k * " + n_size_input + ");\n";
165 fGC += " std::copy(input_graph.node_data.GetData() + senders[k] * " + n_size_input +
166 ", input_graph.node_data.GetData() + (senders[k] + 1) * " + n_size_input +
167 ", fSndNodeInputs.begin() + k * " + n_size_input + ");\n";
168 fGC += " std::copy(input_graph.global_data.GetData()";
169 fGC += ", input_graph.global_data.GetData() + " + g_size_input +
170 ", fGlobInputs.begin() + k * " + g_size_input + ");\n";
171 fGC += "}\n";
172
173 fGC += "fEdgeUpdates = " + edges_update_block->Generate({"n_edges","fEdgeInputs.data(), fRecNodeInputs.data(), fSndNodeInputs.data(), fGlobInputs.data()"}) + "\n";
174
176 fGC += "\n// resize edge graph data since output feature size is not equal to input size\n";
177 fGC+="input_graph.edge_data = input_graph.edge_data.Resize({n_edges, "+e_size+"});\n";
178 }
179 // copy output
180 fGC += "\nfor (size_t k = 0; k < n_edges; k++) { \n";
181 fGC += " std::copy(fEdgeUpdates.begin()+ k * " + e_size + ", fEdgeUpdates.begin()+ (k+1) * " + e_size +
182 ",input_graph.edge_data.GetData() + k * " + e_size + ");\n";
183 fGC += "}\n";
184 fGC += "\n";
185
186 fGC += "\n\n// --- Node Update ---\n";
187 fGC += "size_t n_nodes = input_graph.node_data.GetShape()[0];\n";
188 // computing updated node attributes
189 fGC += "for (size_t k = 0; k < n_nodes; k++) { \n";
190 fGC += " std::copy(input_graph.node_data.GetData() + k * " + n_size_input +
191 ", input_graph.node_data.GetData() + (k + 1) * " + n_size_input +
192 ", fNodeInputs.begin() + k * " + n_size_input + ");\n";
193 fGC += "}\n";
194 // reset initial aggregate edge vector to zero
195 fGC += "\nstd::fill(fNodeEdgeAggregate.begin(), fNodeEdgeAggregate.end(), 0.);\n";
196 // fGlobInputs is size { n_edges, n_globals}. It needs to be here { n_nodes, n_globals}
197 // if number of nodes is larger than edges we need to resize it and copy values
198
199 fGC += "\n// resize global vector feature to number of nodes if needed\n";
200 fGC += "if (n_nodes > n_edges) {\n";
201 fGC += " fGlobInputs.resize( n_nodes * " + std::to_string(num_global_features_input) + ");\n";
202 fGC += " for (size_t k = n_edges; k < n_nodes; k++)\n";
203 fGC += " std::copy(fGlobInputs.begin(), fGlobInputs.begin() + " + g_size_input +
204 " , fGlobInputs.begin() + k * " + g_size_input + ");\n";
205 fGC += "}\n";
206
207 // loop on nodes and aggregate incoming edges
208 fGC += "\n// aggregate edges going to a node\n";
209 fGC += "for (size_t j = 0; j < n_nodes; j++) {\n";
210 // approximate number of receivers/node to allocate vector
211 fGC += " std::vector<float *> edgesData; edgesData.reserve( int(n_edges/n_nodes) +1);\n";
212 // loop on edges
213 fGC += " for (size_t k = 0; k < n_edges; k++) {\n";
214 fGC += " if (receivers[k] == j) \n";
215 fGC += " edgesData.emplace_back(input_graph.edge_data.GetData() + k * " + e_size + ");\n";
216 fGC += " }\n";
217 fGC += " fNodeAggregateTemp = " + edge_node_agg_block->Generate(num_edge_features, "edgesData") + ";\n";
218 fGC += " std::copy(fNodeAggregateTemp.begin(), fNodeAggregateTemp.end(), fNodeEdgeAggregate.begin() + " +
219 e_size + " * j);\n";
220 fGC += "}\n"; // end node loop
221
222
223 fGC+="\n";
224 fGC+="fNodeUpdates = ";
225 fGC+=nodes_update_block->Generate({"n_nodes","fNodeEdgeAggregate.data()","fNodeInputs.data()","fGlobInputs.data()"}); // computing updated node attributes
226 fGC+="\n";
227
229 fGC += "\n// resize node graph data since output feature size is not equal to input size\n";
230 fGC+="input_graph.node_data = input_graph.node_data.Resize({n_nodes, " + n_size + "});\n";
231 }
232 // copy output
233 fGC += "\nfor (size_t k = 0; k < n_nodes; k++) { \n";
234 fGC += " std::copy(fNodeUpdates.begin()+ k * " + n_size + ", fNodeUpdates.begin() + (k+1) * " + n_size +
235 ",input_graph.node_data.GetData() + k * " + n_size+ ");\n";
236 fGC += "}\n";
237 fGC += "\n";
238
239 // aggregating edges & nodes for global update
240 fGC += "std::vector<float *> allEdgesData; allEdgesData.reserve(n_edges);\n";
241 fGC += "for (size_t k = 0; k < n_edges; k++) {\n";
242 fGC += " allEdgesData.emplace_back(input_graph.edge_data.GetData() + k * " + e_size + ");\n";
243 fGC += "}\n";
244 fGC += "std::vector<float *> allNodesData; allNodesData.reserve(n_nodes);\n";
245 fGC += "for (size_t k = 0; k < n_nodes; k++) {\n";
246 fGC += " allNodesData.emplace_back(input_graph.node_data.GetData() + k * " + n_size + ");\n";
247 fGC += "}\n";
248
249
250 fGC += "\n// --- Global Update ---\n";
251 fGC+="std::vector<float> Edge_Global_Aggregate = ";
252 fGC+=edge_global_agg_block->Generate(num_edge_features, "allEdgesData"); // aggregating edge attributes globally
253 fGC+=";\n";
254
255 fGC+="std::vector<float> Node_Global_Aggregate = ";
256 fGC+=node_global_agg_block->Generate(num_node_features, "allNodesData"); // aggregating node attributes globally
257 fGC+=";\n";
258
259 // computing updated global attributes
260 fGC += "std::vector<float> Global_Data = ";
261 fGC += globals_update_block->Generate({"Edge_Global_Aggregate.data()","Node_Global_Aggregate.data()", "input_graph.global_data.GetData()"});
263 fGC += "\n// resize global graph data since output feature size is not equal to input size\n";
264 fGC+="input_graph.global_data = input_graph.global_data.Resize({"+g_size+"});\n";
265 }
266 fGC += "\nstd::copy(Global_Data.begin(), Global_Data.end(), input_graph.global_data.GetData());";
267 fGC+="\n}\n";
268 fGC+="};\n";
269
270 // propagate the helper functions needed by the update-function components
271 // (they are generated as GNN components and share the top-level namespace)
272 for (auto *block : {edges_update_block.get(), nodes_update_block.get(), globals_update_block.get()}) {
273 if (block && block->GetFunctionBlock()) {
274 for (auto const &h : block->GetFunctionBlock()->GetNeededHelperFunctions())
276 }
277 }
278
279 fGC += ("} //TMVA_SOFIE_" + fName + "\n");
280 fGC += "\n#endif // TMVA_SOFIE_" + hgname + "\n";
281
282 // dump the standalone helper-function definitions into the generated header
284}
285
286}//SOFIE
287}//Experimental
288}//TMVA
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void GenerateHeaderInfo(std::string &hgname)
void AddNeededHelperFunction(std::string name)
std::unique_ptr< RFunction_Aggregate > node_global_agg_block
std::unique_ptr< RFunction_Update > globals_update_block
std::unique_ptr< RFunction_Update > edges_update_block
std::unique_ptr< RFunction_Aggregate > edge_global_agg_block
std::unique_ptr< RFunction_Aggregate > edge_node_agg_block
RModel_GNN(GNN_Init &graph_input_struct)
std::unique_ptr< RFunction_Update > nodes_update_block
create variable transformations