Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModel_GraphIndependent.cxx
Go to the documentation of this file.
1#include <limits>
2#include <algorithm>
3#include <cctype>
4
6
7namespace TMVA {
8namespace Experimental {
9namespace SOFIE {
10
12 edges_update_block = std::move(other.edges_update_block);
13 nodes_update_block = std::move(other.nodes_update_block);
14 globals_update_block = std::move(other.globals_update_block);
15
16 num_nodes = std::move(other.num_nodes);
17 num_edges = std::move(other.num_edges);
18
19 fName = std::move(other.fName);
20 fFileName = std::move(other.fFileName);
21 fParseTime = std::move(other.fParseTime);
22}
23
25 edges_update_block = std::move(other.edges_update_block);
26 nodes_update_block = std::move(other.nodes_update_block);
27 globals_update_block = std::move(other.globals_update_block);
28
29 num_nodes = std::move(other.num_nodes);
30 num_edges = std::move(other.num_edges);
31
32 fName = std::move(other.fName);
33 fFileName = std::move(other.fFileName);
34 fParseTime = std::move(other.fParseTime);
35
36 return *this;
37}
38
40 edges_update_block = std::move(graph_input_struct.edges_update_block);
41 nodes_update_block = std::move(graph_input_struct.nodes_update_block);
42 globals_update_block = std::move(graph_input_struct.globals_update_block);
43
44 num_nodes = graph_input_struct.num_nodes;
45 num_edges = graph_input_struct.edges.size();
46 num_node_features = graph_input_struct.num_node_features;
47 num_edge_features = graph_input_struct.num_edge_features;
48 num_global_features = graph_input_struct.num_global_features;
49
50 fFileName = graph_input_struct.filename;
51 fName = fFileName.substr(0, fFileName.rfind("."));
52
53 std::time_t ttime = std::time(0);
54 std::tm* gmt_time = std::gmtime(&ttime);
55 fParseTime = std::asctime(gmt_time);
56}
57
59 std::string hgname;
60 GenerateHeaderInfo(hgname);
61
62 std::ofstream f;
63 f.open(fName+".dat");
64 f.close();
65
66 //Generating Infer function definition for Edge update function
67 long next_pos;
68 size_t block_size = num_edges;
69 fGC+="\n\nnamespace Edge_Update{\nstruct Session {\n";
70 std::vector<std::vector<std::size_t>> Update_Input = {{block_size, num_edge_features}};
71 edges_update_block->Initialize();
72 edges_update_block->AddInputTensors(Update_Input);
73 fGC+=edges_update_block->GenerateModel(fName);
74 next_pos = edges_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
75 fGC+="};\n}\n";
76
77 // the number of output edges features can be smaller, so we need to correct here
78 auto num_edge_features_input = num_edge_features;
79 if(edges_update_block->GetFunctionBlock()->GetTensorShape(edges_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1] != num_edge_features) {
80 num_edge_features = edges_update_block->GetFunctionBlock()->GetTensorShape(edges_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1];
81 }
82
83 fGC+="\n\nnamespace Node_Update{\nstruct Session {\n";
84 // Generating Infer function definition for Node Update function
85 // num_node_features is the output one
86 block_size = num_nodes;
87 Update_Input = {{block_size, num_node_features}};
88 nodes_update_block->Initialize();
89 nodes_update_block->AddInputTensors(Update_Input);
90 fGC+=nodes_update_block->GenerateModel(fName,next_pos);
91 next_pos = nodes_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
92 fGC+="};\n}\n";
93
94 // we need to correct the output number of node features
95 auto num_node_features_input = num_node_features;
96 if(nodes_update_block->GetFunctionBlock()->GetTensorShape(nodes_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1] != num_node_features) {
97 num_node_features = nodes_update_block->GetFunctionBlock()->GetTensorShape(nodes_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1];
98 }
99
100 fGC+="\n\nnamespace Global_Update{\nstruct Session {\n";
101 // Generating Infer function definition for Global Update function
102 Update_Input = {{1, num_global_features}};
103 globals_update_block->Initialize();
104 globals_update_block->AddInputTensors(Update_Input);
105 fGC+=globals_update_block->GenerateModel(fName,next_pos);
106 next_pos = globals_update_block->GetFunctionBlock()->WriteInitializedTensorsToFile(fName+".dat");
107 fGC+="};\n}\n";
108
109 // we need to correct the output number of global features
110 auto num_global_features_input = num_global_features;
111 // global features are in shape[1]
112 if(globals_update_block->GetFunctionBlock()->GetTensorShape(globals_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1] != num_global_features) {
113 num_global_features = globals_update_block->GetFunctionBlock()->GetTensorShape(globals_update_block->GetFunctionBlock()->GetOutputTensorNames()[0])[1];
114 }
115
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 // create temp vector for edge and node updates
125 fGC += "std::vector<float> fEdgeUpdates = std::vector<float>(" + std::to_string(num_edges) + "*" + std::to_string(num_edge_features) + ");";
126 fGC += "\nstd::vector<float> fNodeUpdates = std::vector<float>(" + std::to_string(num_nodes) + "*" + std::to_string(num_node_features) + ");\n";
127
128 fGC += "\n// input vectors for edge update\n";
129 fGC += "std::vector<float> fEdgeInputs = std::vector<float>(" + std::to_string(num_edges) + "*" + std::to_string(num_edge_features_input) + ");\n";
130
131 fGC += "\n// input vectors for node update\n";
132 fGC += "std::vector<float> fNodeInputs = std::vector<float>(" + std::to_string(num_nodes) + "*" + std::to_string(num_node_features_input) + ");\n";
133
134 fGC += "\nvoid infer(TMVA::Experimental::SOFIE::GNN_Data& input_graph){\n";
135
136 // computing updated edge attributes
137 fGC += "\n// --- Edge Update ---\n";
138
139 std::string e_size_input = std::to_string(num_edge_features_input);
140 fGC += "for (int k = 0; k < " + std::to_string(num_edges) + "; k++) { \n";
141 fGC += " std::copy(input_graph.edge_data.GetData() + k * " + e_size_input +
142 ", input_graph.edge_data.GetData() + (k + 1) * " + e_size_input +
143 ", fEdgeInputs.begin() + k * " + e_size_input + ");\n";
144 fGC += "}\n";
145
146 fGC += "fEdgeUpdates = " + edges_update_block->Generate({"fEdgeInputs.data()"}) + "\n";
147
148 if(num_edge_features != num_edge_features_input) {
149 fGC += "\n// resize edge graph data since output feature size is not equal to input size\n";
150 fGC+="input_graph.edge_data = input_graph.edge_data.Resize({"+std::to_string(num_edges)+", "+std::to_string(num_edge_features)+"});\n";
151 }
152 // copy output
153 fGC += "\nfor (int k = 0; k < " + std::to_string(num_edges) + "; k++) { \n";
154 fGC += " std::copy(fEdgeUpdates.begin()+ k * " + std::to_string(num_edge_features) + ", fEdgeUpdates.begin()+ (k+1) * " + std::to_string(num_edge_features) +
155 ",input_graph.edge_data.GetData() + k * " + std::to_string(num_edge_features)+ ");\n";
156 fGC += "}\n";
157 fGC += "\n";
158
159 // computing updated node attributes
160 std::string n_size_input = std::to_string(num_node_features_input);
161 fGC += "\n// --- Node Update ---";
162 fGC += "\nfor (int k = 0; k < " + std::to_string(num_nodes) + "; k++) { \n";
163 fGC += " std::copy(input_graph.node_data.GetData() + k * " + n_size_input +
164 ", input_graph.node_data.GetData() + (k + 1) * " + n_size_input +
165 ", fNodeInputs.begin() + k * " + n_size_input + ");\n";
166 fGC += "}\n";
167
168 fGC+="\nfNodeUpdates = ";
169 fGC+=nodes_update_block->Generate({"fNodeInputs.data()"}); // computing updated node attributes
170 fGC+="\n";
171
172 if(num_node_features != num_node_features_input) {
173 fGC += "\n// resize node graph data since output feature size is not equal to input size\n";
174 fGC+="input_graph.node_data = input_graph.node_data.Resize({"+std::to_string(num_nodes)+", "+std::to_string(num_node_features)+"});\n";
175 }
176 // copy output
177 fGC += "\nfor (int k = 0; k < " + std::to_string(num_nodes) + "; k++) { \n";
178 fGC += " std::copy(fNodeUpdates.begin()+ k * " + std::to_string(num_node_features) + ", fNodeUpdates.begin() + (k+1) * " + std::to_string(num_node_features) +
179 ",input_graph.node_data.GetData() + k * " + std::to_string(num_node_features)+ ");\n";
180 fGC += "}\n";
181 fGC += "\n";
182
183 // computing updated global attributes
184 fGC += "\n// --- Global Update ---\n";
185 fGC += "std::vector<float> Global_Data = ";
186 fGC += globals_update_block->Generate({"input_graph.global_data.GetData()"});
187 fGC += "\n";
188
189 if(num_global_features != num_global_features_input) {
190 fGC += "\n// resize global graph data since output feature size is not equal to input size\n";
191 fGC+="input_graph.global_data = input_graph.global_data.Resize({"+std::to_string(num_global_features)+"});\n";
192 }
193
194 fGC += "\nstd::copy(Global_Data.begin(), Global_Data.end(), input_graph.global_data.GetData());";
195 fGC += "\n";
196
197 fGC += ("}\n};\n} //TMVA_SOFIE_" + fName + "\n");
198 fGC += "\n#endif // TMVA_SOFIE_" + hgname + "\n";
199
200}
201
202}//SOFIE
203}//Experimental
204}//TMVA
#define f(i)
Definition RSha256.hxx:104
void GenerateHeaderInfo(std::string &hgname)
RModel_GraphIndependent & operator=(RModel_GraphIndependent &&other)
create variable transformations
std::unique_ptr< RFunction_Update > globals_update_block