Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModel.cxx
Go to the documentation of this file.
1#include <limits>
2#include <algorithm>
3#include <cctype>
4#include <memory>
5#include <string>
6#include <cstdlib>
7
8#ifdef SOFIE_SUPPORT_ROOT_BINARY
9#include "TFile.h"
10#endif
11
12#include "TMVA/RModel.hxx"
13#include "TMVA/SOFIE_common.hxx"
14
16
17namespace {
18
19const std::string SP = " ";
20
21void ReplaceAll(std::string &str, const std::string &from, const std::string &to)
22{
23 size_t pos = 0;
24 while ((pos = str.find(from, pos)) != std::string::npos) {
25 str.replace(pos, from.length(), to);
26 pos += to.length();
27 }
28}
29
30bool IsIdentifierChar(char c)
31{
32 return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
33}
34
35// Returns true if s is a valid C++ identifier (can be used as a variable name).
36// Dim::param can be either a plain name (e.g. "W") or a computed expression
37// (e.g. "((W+-3)/2+1)"); only the former can be used as a C++ variable name.
38bool IsIdentifier(const std::string &s)
39{
40 if (s.empty() || std::isdigit(static_cast<unsigned char>(s[0])))
41 return false;
42 for (char c : s)
45 return true;
46}
47
48// Get the data member name corresponding to a tensor with a given name.
49std::string TensorMember(std::string const &name)
50{
51 return "tensor_" + name;
52}
53
54} // namespace
55
56std::underlying_type_t<Options> operator|(Options opA, Options opB) {
57 return static_cast<std::underlying_type_t<Options>>(opA) | static_cast<std::underlying_type_t<Options>>(opB);
58}
59std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB) {
60 return opA | static_cast<std::underlying_type_t<Options>>(opB);
61}
62
63
64std::vector<size_t> RModel::GetTensorShape(const std::string & name) const {
65 auto f = fReadyInputTensorInfos.find(name);
66 if (f != fReadyInputTensorInfos.end()) {
67 return f->second.shape;
68 }
69 auto f2 = fInitializedTensors.find(name);
70 if (f2 != fInitializedTensors.end()) {
71 return f2->second.shape();
72 }
73 auto f3 = fInputTensorInfos.find(name);
74 if (f3 != fInputTensorInfos.end()) {
75 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] is an input tensor with unspecified dimension parameter");
76 }
77 auto f4 = fIntermediateTensorInfos.find(name);
78 if (f4 != fIntermediateTensorInfos.end()) {
79 return f4->second.shape;
80 }
81 // case of shape tensors
82 auto f5 = fShapeTensors.find(name);
83 if (f5 != fShapeTensors.end()) {
84 // shape is vector of size 1 with size of shape values or just a scalar
85 if (f5->second.second) // check scalar flag
86 return std::vector<size_t>{};
87 else
88 return std::vector<size_t>{f5->second.first.size()};
89 }
90
92 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] is a dynamic tensor. Use GetDynamicTensorShape instead of GetTensorShape");
93
96
97 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not found");
98}
99
100std::vector<Dim> RModel::GetDimTensorShape(const std::string & name) const {
101 if (auto f = fDynamicTensorInfos.find(name); f != fDynamicTensorInfos.end()) {
102 return f->second.shape;
103 }
104 if (auto f = fInputTensorInfos.find(name); f != fInputTensorInfos.end()) {
105 return f->second.shape;
106 }
107 // in case is not a dynamic tensor convert normal shape to Dim one
108 // for this we need to return the vector by value
110}
111std::vector<Dim> RModel::GetDynamicTensorShape(const std::string & name) const {
112 if (auto f = fDynamicTensorInfos.find(name); f != fDynamicTensorInfos.end()) {
113 return f->second.shape;
114 }
115 if (auto f = fInputTensorInfos.find(name); f != fInputTensorInfos.end()) {
116 return f->second.shape;
117 }
118 // throw error if shape is not dynamic
119 if (!IsDynamicTensor(name))
120 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not dynamic");
121
122 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not found");
123}
124
126 auto f = fReadyInputTensorInfos.find(name);
127 if (f != fReadyInputTensorInfos.end()) {
128 return f->second.type;
129 }
130 auto f2 = fInitializedTensors.find(name);
131 if (f2 != fInitializedTensors.end()) {
132 return f2->second.type();
133 }
134 auto f3 = fInputTensorInfos.find(name);
135 if (f3 != fInputTensorInfos.end()) {
136 return f3->second.type;
137 }
138 auto f4 = fIntermediateTensorInfos.find(name);
139 if (f4 != fIntermediateTensorInfos.end()) {
140 return f4->second.type;
141 }
142 auto f5 = fDynamicTensorInfos.find(name);
143 if (f5 != fDynamicTensorInfos.end()){
144 return f5->second.type;
145 }
146 // case of shape tensor type is INT64
147 if (fShapeTensors.find(name) != fShapeTensors.end()){
148 return ETensorType::INT64;
149 }
150
153
154 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the type is requested is not found, model name: " + fName);
155}
156
157bool RModel::CheckIfTensorAlreadyExist(std::string tensor_name) {
158 if (fReadyInputTensorInfos.find(tensor_name) != fReadyInputTensorInfos.end()) return true;
159 if (fInputTensorInfos.find(tensor_name) != fInputTensorInfos.end()) return true;
160 if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()) return true;
161 if (fIntermediateTensorInfos.find(tensor_name) != fIntermediateTensorInfos.end()) return true;
162 if (fDynamicTensorInfos.find(tensor_name) != fDynamicTensorInfos.end()) return true;
163 if (fShapeTensors.find(tensor_name) != fShapeTensors.end()) return true;
165 return false;
166}
167
168void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape) {
171 throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
172 }
173
174 InputTensorInfo inputInfo { type, shape };
176}
177
178void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape) {
181 throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
182 }
183 TensorInfo inputInfo { type, shape };
185}
186
190
191void RModel::AddOperator(std::unique_ptr<ROperator> op, int order_execution) {
192 AddBlasRoutines(op->GetBlasRoutines());
193 auto libs = op->GetStdLibs();
194 auto op_input_tensors = op->GetOpInputTensors();
195 for (auto& stdlib : libs) {
197 }
198 if (order_execution >= 0) {
199 fOperators.insert(fOperators.begin() + order_execution, std::move(op));
200 } else {
201 fOperators.push_back(std::move(op));
202 order_execution = fOperators.size()-1;
203 }
204
205 // storing the last usage of tensors which are input to the operator
206 // (excluding tensors which are inputs to the model or the initialized (weights) tensors)
207 // We call this function during parsing so we don't have yet initialized the operators
208 for(size_t index = 0; index<op_input_tensors.size() &&
210 std::find(fInputTensorNames.begin(), fInputTensorNames.end(),
212 ++index)
213 {
215 }
216}
217
218void RModel::AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
219 tensor_name = UTILITY::Clean_name(tensor_name);
220 //NB: own data
221 if (CheckIfTensorAlreadyExist(tensor_name)) {
222 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
223 }
225 fInitializedTensors[tensor_name] = new_tensor;
226}
227
228void RModel::AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
229 tensor_name = UTILITY::Clean_name(tensor_name);
230 //NB: own data
231 if (CheckIfTensorAlreadyExist(tensor_name)) {
232 throw std::runtime_error("TMVA-SOFIE: constant tensor with name " + tensor_name + " already exists \n");
233 }
234 InitializedTensor new_tensor {type, shape, data, true}; // add here flag to specify is a constant tensor
235 fInitializedTensors[tensor_name] = new_tensor;
236}
237
238void RModel::AddShapeTensor(const std::string & name, const std::vector<Dim> & shape_values, bool scalar){
239 auto tensor_name = UTILITY::Clean_name(name);
240 if (fShapeTensors.count(tensor_name) != 0) {
241 throw std::runtime_error("TMVA-SOFIE: shape tensor with name " + tensor_name + " already exists \n");
242 }
243 fShapeTensors[tensor_name] = std::make_pair(shape_values, scalar);
244}
245
246void RModel::AddAliasTensor(const std::string & name, const std::string & origin){
247 // add an alias tensor to origin
248 auto tensor_name = UTILITY::Clean_name(name);
250 if (fAliasTensors.count(tensor_name) != 0) {
251 throw std::runtime_error("TMVA-SOFIE: alias tensor with name " + tensor_name + " already exists \n");
252 }
253 fAliasTensors[tensor_name] = origin_name;
254}
255
256bool RModel::IsShapeTensor(const std::string & tensor_name) const {
257 return fShapeTensors.count(tensor_name) != 0;
258}
259
260bool RModel::IsAliasTensor(const std::string & tensor_name) const {
261 return fAliasTensors.count(tensor_name) != 0;
262}
263
264const std::vector<Dim> & RModel::GetShapeTensorValues(const std::string & tensor_name) const {
265 //if (!IsShapeTensor(tensor_name) ) return std::vector<Dim>{};
266 return fShapeTensors.at(tensor_name).first;
267}
268
269bool RModel::IsInitializedTensor(const std::string& tensorName) const {
270 std::string name = UTILITY::Clean_name(tensorName);
271 return fInitializedTensors.find(name) != fInitializedTensors.end();
272}
273bool RModel::IsConstantTensor(const std::string& tensorName) const {
274 // a constant tensor is an initialized tensor but has the constant flag set
275 std::string name = UTILITY::Clean_name(tensorName);
276 auto itr = fInitializedTensors.find(name);
277 if (itr == fInitializedTensors.end()) return false;
278 return itr->second.IsConstantTensor();
279}
280
281// dynamic tensors include also Dim input tensors
282bool RModel::IsDynamicTensor(const std::string& tensorName) const {
283 std::string name = UTILITY::Clean_name(tensorName);
285 return (ret) ? true : IsDimInputTensor(tensorName);
286}
287bool RModel::IsDimInputTensor(const std::string& tensorName) const {
288 std::string name = UTILITY::Clean_name(tensorName);
289 return fInputTensorInfos.find(name) != fInputTensorInfos.end();
290}
291bool RModel::IsReadyInputTensor(const std::string& tensorName) const {
292 std::string name = UTILITY::Clean_name(tensorName);
294}
295
296// generic addition of a tensor
297void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape) {
299 if (!int_shape.empty())
300 AddIntermediateTensor(tensor_name, type, int_shape);
301 else
302 AddDynamicTensor(tensor_name, type, dim_shape);
303}
304
305void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape) {
306 tensor_name = UTILITY::Clean_name(tensor_name);
307 if (CheckIfTensorAlreadyExist(tensor_name)) {
308 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
309 }
310 TensorInfo new_tensor {type, shape};
312}
313
314void RModel::AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape){
315 tensor_name = UTILITY::Clean_name(tensor_name);
316 if (CheckIfTensorAlreadyExist(tensor_name)){
317 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
318 }
320 fDynamicTensorInfos[tensor_name] = new_tensor;
321 // store shape parameter if not existing
322 for (auto &d : shape) {
323 if (d.isParam) {
324 if (d.dim != size_t(-1)) {
325 AddShapeParam(d.param, d.dim);
326 }
327 }
328 }
329}
330
331void RModel::AddShapeParam(const std::string & param, size_t default_value) {
332 if (fShapeParams.count(param) == 0) {
333 fShapeParams[param] = std::to_string(default_value);
334 // add also in the vector list (used to keep the order)
335 fDimShapeNames.push_back(param);
336 }
337}
338
340 fOutputTensorNames.clear();
341 for(auto& it : outputtensornames) {
342 fOutputTensorNames.emplace_back(UTILITY::Clean_name(it));
343 }
344}
345
346void RModel::UpdateOutputTensorList(std::vector<std::string> curr_output_tensors, std::vector<std::string> new_output_tensors) {
347 for(auto& it:curr_output_tensors) {
348 fOutputTensorNames.erase(std::remove(fOutputTensorNames.begin(), fOutputTensorNames.end(), it), fOutputTensorNames.end());
349 }
351}
352
353void RModel::UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
354 tensor_name = UTILITY::Clean_name(tensor_name);
355 if (!CheckIfTensorAlreadyExist(tensor_name)) {
356 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to update it");
357 }
359 fInitializedTensors[tensor_name] = new_tensor;
360}
361
362std::shared_ptr<void> RModel::GetInitializedTensorData(std::string tensor_name) {
363 auto f = fInitializedTensors.find(tensor_name);
364 if (f == fInitializedTensors.end()) {
365 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to get its data");
366 } else {
367 return f->second.sharedptr();
368 }
369}
370
371void RModel::SetNotWritableInitializedTensor(const std::string & tensor_name) {
372 auto t = fInitializedTensors.find(tensor_name);
373 if (t == fInitializedTensors.end()) {
374 throw std::runtime_error("TMVA-SOFIE: initialized tensor " + tensor_name + " not found when trying to get its info");
375 }
376 t->second.SetNotWritable();
377 }
378
379std::string RModel::AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors)
380{
381 std::stringstream code;
382
383 if (fVerbose) {
384 std::cout << "Total chunks allocated\n";
386 std::cout << "..... chunk " << chunk->first << " size " << chunk->second.tensor_size << " " << chunk->second.tensor_name << std::endl;
387 }
388 }
389
390 auto declareIntermediateTensor = [this, &code](std::string const &name, size_t size, size_t location) {
391 std::string typeName = ConvertTypeToString(GetTensorType(name));
392 code << "\n // Allocating memory for intermediate tensor " << name << " with size " << size << " bytes";
393 code << "\n"
394 << typeName << "* " << TensorMember(name) << " = reinterpret_cast<" << typeName
395 << "*>(fIntermediateMemoryPool.data() + " << location << ");\n";
396 };
397
398 if (fVerbose) std::cout << "*** AllocateIntermediateMemory: Loop on op output tensors\n";
399 // order output tensors by size
400 std::vector<TensorMemoryInfo> ordered_output_tensors;
401
402 for (auto &it : op_output_tensors) {
403 auto name = std::string(it);
406 continue;
407
408 // case of alias tensor
409 if (IsAliasTensor(name)) {
410 continue;
411 }
412
414 // important fill the pair in the ordered output tensors with the string view and not the string
415 TensorMemoryInfo tmi = {it, tensor_size};
416 ordered_output_tensors.push_back(tmi);
417 }
419 [](const TensorMemoryInfo &a, const TensorMemoryInfo &b) { return a.tensor_size > b.tensor_size; });
420
421 for (auto &it : ordered_output_tensors) {
422 bool allocated = false;
423 std::string name = std::string{it.tensor_name};
424 size_t tensor_size = it.tensor_size;
425 if (fVerbose)
426 std::cout << "output tensor " << name << " size " << tensor_size << std::endl;
427
430
431 if (fVerbose) std::cout << ".. available chunk " << chunk->first << " with size = " << chunk->second;
432 // check if available memory chunks can accommodate the tensor
433 if (chunk->second >= tensor_size) {
434 // need to use here string_view (i.e it.tensor_name)
435 // split returns the new chunk with size of new tensor. The free chunk is before the used one
436 auto new_chunk = fIntermediateMemoryInfo.total_stack[chunk->first].split(it.tensor_name, tensor_size);
437 auto new_chunk_location = chunk->first + chunk->second - tensor_size;
439
441 chunk->second -= tensor_size;
442
443 allocated = true;
444
445 if (fVerbose) std::cout << " is re-used and split in a new of size " << new_chunk.tensor_size << " at " << new_chunk_location;
446
447 if (chunk->second == 0) {
448 if (fVerbose) std::cout << " and deleted since size matches";
450 }
451 if (fVerbose) std::cout << std::endl;
452 break;
453 } else if (chunk->first == fIntermediateMemoryInfo.available_stack.rbegin()->first &&
454 fIntermediateMemoryInfo.total_stack.rbegin()->first == chunk->first) {
455 // case last available chunk is the last in the memory, we can increase that one
456 fIntermediateMemoryInfo.total_stack[chunk->first] = {it.tensor_name, tensor_size};
457 declareIntermediateTensor(name, tensor_size, chunk->first);
459 allocated = true;
460 if (fVerbose) std::cout << " is extended with a bigger one of size " << tensor_size << std::endl;
461 break;
462 }
463 ++chunk;
464 if (fVerbose) std::cout << std::endl;
465 }
466
467 if (!allocated) {
469 ? 0
470 : fIntermediateMemoryInfo.total_stack.rbegin()->first +
471 fIntermediateMemoryInfo.total_stack.rbegin()->second.tensor_size;
472
474
476
477 if (fVerbose) std::cout << "no chunk available - add in total stack a new chunk with size of tensor and idx : " << chunk_idx
478 << std::endl;
479 }
480 }
481 return code.str();
482}
483
484void RModel::CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_input_tensors, const size_t& op_idx){
485 if (fVerbose) std::cout << "*** CheckAndFlushIntermediateMemory: Loop on input tensors for op " << op_idx << "\n";
486 //print available chunks
487 if (fVerbose) std::cout << "available chunks before freeing them : \n";
490 if (fVerbose) std::cout << "-- free chunk " << chunk->first << " size = " << chunk->second << std::endl;
491 }
492 for (auto &iv : op_input_tensors) {
493 // last occurrence of the tensor is reached => flush it from memory
494 if (fVerbose) std::cout << ".. input tensors : " << iv;
495
496 // for alias tensors replace name with its alias
497 std::string it{iv}; // convert view to string
498 if (IsAliasTensor(it))
499 it = fAliasTensors[it];
501 if (fVerbose) std::cout << " flash condition is met - looping on chunks to find matching one \n";
502 for (auto chunk = fIntermediateMemoryInfo.total_stack.begin();
504 if (fVerbose) std::cout << "--- chunk " << chunk->first << " , " << chunk->second.tensor_name << " size " << chunk->second.tensor_size;
505 if (chunk->second.tensor_name == it) {
506 if (fVerbose) std::cout << " -- Found chunk corresponding to input tensor: " << chunk->first;
507 // check if nearby chunks in available memory can coalesce
509 chunk->first); // smallest element greater than the flushed chunk idx
512 : std::prev(first_greater); // largest element smaller than the flushed chunk idx
513
514 // check if the next stack entry is actually adjacent in memory
515
517 last_smaller->first + last_smaller->second == chunk->first) {
518 // merge chunk with previous one
519 last_smaller->second += chunk->second.tensor_size;
521 if (fVerbose) std::cout << " is adjacent in memory with previous one - merge ";
523 last_smaller->first + last_smaller->second == first_greater->first) {
524 // merge also with following one
525 last_smaller->second += first_greater->second;
528 // delete merged one in available stack and in total stack
531 if (fVerbose) std::cout << " merge also with following that is free ";
532 }
534 if (fVerbose) std::cout << std::endl;
535 break;
537 chunk->first + chunk->second.tensor_size == first_greater->first) {
538 // merge with first greater
539 if (fVerbose) std::cout << " is adjacent in memory with following one - merge \n";
540 // cannot modify idx of first_greter. Insert a new one and delete previous one
541 size_t new_size = chunk->second.tensor_size + first_greater->second;
542 size_t first_greater_idx = first_greater->first;
544 // cannot use anymore first_greater
549 } else {
550 fIntermediateMemoryInfo.available_stack.insert({chunk->first, chunk->second.tensor_size});
551 if (fVerbose) std::cout << " insert in the available stack the chunk with size " << chunk->second.tensor_size << std::endl;
552 }
553 chunk->second.tensor_name = "free";
554 break;
555 }
556 }
557 } else {
558 if (fVerbose) std::cout << std::endl;
559 }
560 }
561}
562
563void RModel::Initialize(int batchSize, bool verbose) {
564 std::map<std::string, size_t> inputParams;
565 if (batchSize > 0) {
566 inputParams["input_size"] = batchSize;
567 inputParams["batch_size"] = batchSize;
568 inputParams["bs"] = batchSize;
569 }
570 Initialize(inputParams, verbose);
572}
573void RModel::Initialize(const std::map<std::string, size_t> & inputParams, bool verbose) {
574
575 fVerbose = int(verbose);
576
577 if (fIsInitialized) {
578 if (verbose)
579 std::cout << "Model is already initialized - skip initialization " << std::endl;
580 return;
581 }
583 fDynamicTensorInfos.clear();
584
585
586 // loop on inputs and see if shape can be full specified
587 // if the batch size is provided it can be used to specify the full shape
588 // Add the full specified tensors in fReadyInputTensors collection
589 auto originalInputTensorInfos = fInputTensorInfos; // need to copy because we may delete elements
590 for (auto &input : originalInputTensorInfos) {
591 if (verbose) std::cout << "looking at the tensor " << input.first << std::endl;
592 // if a parameter (e.g. batch_size) is specified use for converting parametric shape in defined one
593 if (!inputParams.empty()) {
594 for (auto &d : input.second.shape) {
595 if (d.isParam) {
596 std::string pname = d.param;
597 if (pname == input.first + "_size") pname = "input_size";
598 auto itr = inputParams.find(pname);
599 if (itr != inputParams.end() ) {
600 d = Dim{ itr->second };
601 if (verbose)
602 std::cout << "Tensor: " << input.first << " - fix parametric shape " << itr->first << " to " << itr->second << std::endl;
603 }
604 }
605 }
606 }
607 // see if shape now is fully defined
608 auto shape = ConvertShapeToInt(input.second.shape);
609 if (verbose)
610 std::cout << "converting input shape for " << input.first << " " << ConvertShapeToString(shape) << " from "
611 << ConvertDimShapeToString(input.second.shape) << std::endl;
612 if (!shape.empty()) {
613 // case shape is defined (not parametric) we add the tensor in the fReadyInputTensorInfos map and
614 // we remove the tensor from the fInputTensorInfo where th eold parametric shape was stored
615 fInputTensorInfos.erase(input.first);
616 // add to the ready input tensor information the new fixed shape
617 AddInputTensorInfo(input.first, input.second.type, shape);
618 // check consistency
620 }
621 // store the parameters of the input tensors
622 else {
623 // store the found parametric shape parameters
624 for (auto &d : input.second.shape) {
625 if (d.isParam) {
626 if (fShapeParams.count(d.param) == 0) {
627 fDimShapeNames.push_back(d.param);
628 fShapeParams[d.param] = std::to_string(d.dim);
629 }
630 }
631 }
632 }
633 }
634
635 if (verbose) {
638 }
639
640 // Go through model and initialize each operator
641 int i = 0;
642
643 std::vector<size_t> temp_available_stack; // vector stores individual chunks of available memory that maybe reused
644
645 // Build set of initialized tensors consumed by at least one runtime operator (need for later)
646 std::unordered_set<std::string> runtimeInitializedInputs;
647 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
648 if (verbose) {
649 auto& r = *fOperators[op_idx].get();
650 std::cout << "Initializing operator " << i << " " << typeid(r).name() << std::endl;
651 }
652 fOperators[op_idx]->Initialize(*this);
653 for(auto &it:fOperators[op_idx]->GetOpOutputTensors()){
654 std::string name = std::string{it};
655 // check if tensor is not an initialized or output tensor and it is not already in the list
657 std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end() &&
659 {
661 }
662 }
663 // loop for non-constant operators and flag the inputs which are initialized tensors to make sure they are writable
664 if (!fOperators[op_idx]->IsOutputConstant()) {
665 for (auto &it : fOperators[op_idx]->GetOpInputTensors()) {
666 std::string name = std::string{it};
667 if (fInitializedTensors.find(name) != fInitializedTensors.end()) {
669 }
670 }
671 }
672
673 i++;
674 }
675
676 // loop on initialized tensors and make the integers as constant to be
677 // not written in a weight file and check if the tensors flagged as not writable are really not writable,
678 // i.e. are not used by non constant operators
679 for (auto &it : fInitializedTensors) {
680 // check if not-writable tensors are really not writable, i.e. are not used by non constant operators
681 if (it.second.IsNotWritable() && runtimeInitializedInputs.find(it.first) != runtimeInitializedInputs.end()) {
682 it.second.SetWritable();
683 if (verbose) {
684 std::cout << "Initialized tensor " << it.first << " is flagged as not writable but is used by non constant operators, set it as writable \n";
685 }
686 }
687 // if the tensor is an integer we can flag it as constant since it will not be written in a weight file and it is considered equivalent as being created from a Constant operator
688 // only FLOAT tensors are written in a weight file
689 if (it.second.type() != ETensorType::FLOAT) {
690 it.second.SetConstant();
691 }
692 }
693
694 // check if there are initialized tensors to write in a weight file
695 if (fUseWeightFile) {
696 bool modelHasWeights = false;
697 for (auto &it : fInitializedTensors) {
698 if (it.second.IsWeightTensor()) {
699 modelHasWeights = true;
700 break;
701 }
702 }
703 if (!modelHasWeights)
704 fUseWeightFile = false;
705 }
706
707 // update fIntermediateTensorFrequencyLookup for alias tensors
708 for (auto & it : fAliasTensors) {
712 else {
713 // take the largest one
715 }
716 }
717
718 fIsInitialized = true;
719}
720
721void RModel::InitializeSubGraph(std::shared_ptr<RModel> graph) {
722 // add the subgraph to the list
723 fSubGraphs.push_back(graph);
724 //this needs to be done before initializing
725 graph->fParentGraph = this;
726 graph->fIsSubGraph = true;
727
728 graph->Initialize(fBatchSize, fVerbose);
729 // set the same options as parent model
730 graph->fWeightFile = fWeightFile;
731 graph->fUseWeightFile = fUseWeightFile;
732 graph->fUseSession = fUseSession;
733 // add needed blas routines and libs
734 std::vector<std::string> blasRoutines;
735 for (auto & e : graph->fNeededBlasRoutines)
736 blasRoutines.push_back(e);
738 for (auto e : graph->fNeededStdLib)
740
741 // add parent input tensors to current graph
742 for (auto & name : fInputTensorNames)
743 graph->fInputTensorNames.emplace_back(name);
744
745 // clean graph name
746 graph->fName = UTILITY::Clean_name(graph->fName);
747
748}
749
750// Function to generate the code for declaring and initializing constant tensors
751// This is for tensors which are not part of weight files and can be created from the Constant operator
752template <typename T>
753std::string GenerateConstantTensorCode(const std::pair<std::string, InitializedTensor> &t)
754{
755 std::stringstream strs;
756 std::string type = ConvertTypeToString(t.second.type());
757 size_t length = ConvertShapeToLength(t.second.shape());
758 // avoid using stack sizes for constant tensors to reduce compilation time
759 // also for weights which can be broadcasted do not use stack but allocate as a std::vector
760 bool allocateOnStack = (length > 100 || t.second.IsWeightTensor()) ? false : true;
761
762 const T *data = t.second.data<T>();
763
764 // and check if all values are the same
765 bool sameData = false;
766 // for non stack allocation check if data are the same
767 if (!allocateOnStack && length > 1) {
768 size_t idx = 1;
769 do {
770 sameData = (data[idx] == data[idx - 1]);
771 idx++;
772 } while (sameData && idx < length);
773 }
774 if (allocateOnStack) {
775 strs << type << " fTensor_" << t.first << "[" << length << "] = " << ConvertValuesToString(length, data) << ";\n";
776 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ";\n";
777 } else {
778 strs << "std::vector<" << type << "> fTensor_" << t.first << " = ";
779 if (sameData)
780 strs << "std::vector<" << type << ">(" << length << ", " << ConvertValToString(data[0]) << ");\n";
781 else {
783 }
784 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ".data();\n";
785 }
786 return strs.str();
787}
788
790{
791 if (!fInitializedTensors.empty())
792 fGC += "// initialized (weights and constant) tensors\n";
793
794 // here are constant tensor or initialized ones which are not weights (e.g. int64_t tensors )
795 for (auto &i : fInitializedTensors) {
796 if (i.second.IsNotWritable()) continue;
797 size_t length = ConvertShapeToLength(i.second.shape());
798 if (!fUseWeightFile || i.second.IsConstantTensor() || !i.second.IsWeightTensor() || i.second.type() != ETensorType::FLOAT ) {
799 if (i.second.type() == ETensorType::FLOAT) {
801 fConstantTensorSize += length * sizeof(float);
802 } else if (i.second.type() == ETensorType::INT64) {
804 fConstantTensorSize += length * sizeof(int64_t);
805 } else if (i.second.type() == ETensorType::INT32) {
807 fConstantTensorSize += length * sizeof(int32_t);
808 } else if (i.second.type() == ETensorType::BOOL || i.second.type() == ETensorType::UINT8 ) {
810 fConstantTensorSize += length * sizeof(uint8_t);
811 }
812
813
814 } else {
815 // case of tensors which are read from a file
816 if (i.second.type() == ETensorType::FLOAT) {
817 fGC += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
818 fGC += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
819 fWeightsTensorSize += length * sizeof(float);
820 }
821 }
822 }
823}
824
826 if (fIntermediateMemoryInfo.total_stack.empty()) return;
827 fGC += "\n//--- Allocating session memory pool to be used for allocating intermediate tensors\n";
828
829 // char memory block is allocated since char takes 1 byte, thus easier to allocate tensors
830 // of other data types
832 const size_t memPoolSize = totalStack.rbegin()->first + totalStack.rbegin()->second.tensor_size;
833 fGC += "std::vector<char> fIntermediateMemoryPool = std::vector<char>(" + std::to_string(memPoolSize) + ");\n\n";
834}
835
837 if (!fIntermediateTensorInfos.empty()) {
838 std::string tensor_declaration_block = "";
839 for (auto &i : fIntermediateTensorInfos) {
840 bool is_alias = (IsAliasTensor(i.first));
841 if (i.second.type == ETensorType::BOOL && !is_alias) {
842 tensor_declaration_block += "std::vector<std::uint8_t> fTensor_" + i.first + " = std::vector<std::uint8_t>(" + std::to_string(ConvertShapeToLength(i.second.shape)) + ");\n";
843 tensor_declaration_block += "std::uint8_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
844 continue;
845 }
847 bool not_in_freq_map =
850 (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) == fOutputTensorNames.end());
851
853 size_t length = ConvertShapeToLength(i.second.shape);
854
855 if (i.second.type == ETensorType::FLOAT) {
856 tensor_declaration_block += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
857 tensor_declaration_block += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
859 }
860 else if (i.second.type == ETensorType::DOUBLE) {
861 tensor_declaration_block += "std::vector<double> fTensor_" + i.first + " = std::vector<double>(" + std::to_string(length) + ");\n";
862 tensor_declaration_block += "double * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
864 }
865 else if (i.second.type == ETensorType::INT64) {
866 tensor_declaration_block += "std::vector<int64_t> fTensor_" + i.first + " = std::vector<int64_t>(" + std::to_string(length) + ");\n";
867 tensor_declaration_block += "int64_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
869 }
870 }
871 if (is_alias) {
872 tensor_declaration_block += ConvertTypeToString(i.second.type) + " * " + TensorMember(i.first) + " = nullptr;\n";
873 }
874
875 }
876
877 if (tensor_declaration_block.length()) {
878 fGC += "\n//--- declare and allocate the intermediate tensors\n" + tensor_declaration_block;
879 }
880 }
881 // add also the dynamic tensors (only declarations, allocation will be done later)
882 if (!fDynamicTensorInfos.empty()) {
883 fGC += "//--- declare the dynamic tensors\n";
884 for (auto &i : fDynamicTensorInfos) {
885 fGC += ConvertTypeToString(i.second.type) + " * " + TensorMember(i.first) + " = nullptr;\n";
886 }
887 fGC += "//--- dynamic tensors pool\n";
888 fGC += "std::vector<char> fDynamicMemoryPool;\n";
889 }
890}
891
892// generate code for specific operator declarations to be defined in the Session class
894 std::string strcode;
895 for (auto & op : fOperators) {
896 strcode += op->GenerateDeclCode();
897 }
898 if (strcode.empty()) return;
899 fGC += "\n//---- operator declarations \n";
900 fGC += strcode;
901 fGC += "\n";
902}
903
905{
906 // generate code for allocating dynamic tensors using the greedy memory allocations
907 if (fDynamicTensorInfos.empty())
908 return;
909
910 if (fVerbose) {
911 std::cout << "generating code for dynamic tensor management" << std::endl;
913 }
914
915 std::stringstream out;
916 out << "// dynamic tensor memory management\n";
917 out << SP << "std::vector<TMVA::Experimental::SOFIE::TensorLifeInfo> dynamicTensorInfos;\n";
918 out << SP << "dynamicTensorInfos.reserve(" << fDynamicTensorInfos.size() << ");\n";
919
920 // loop on all the operators to find begin/end life of the tensors
921 int op_index = 0;
922 std::vector<std::pair<std::string, ETensorType>> tensors;
923 tensors.reserve(fDynamicTensorInfos.size());
924 for (auto & op : fOperators) {
925 // loop on output tensors -
926 for (auto &it : op->GetOpOutputTensors()) {
927 if (fVerbose) {
928 auto op_ptr = op.get();
929 std::cout << "Looping on operator " << op_index << " " << typeid(*op_ptr).name() << std::endl;
930 }
931 // check if is a dynamic tensor and not an alias tensor or output tensor
932 std::string name = std::string(it);
934 && std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end()) {
936 auto type = GetTensorType(name);
937 size_t type_size = GetTypeSize(type);
938 int begin = op_index;
939 int end = fOperators.size();
940 // look for end
943 end = it_lookup->second + 1; // end is last time used + 1
944 // // some tensors (like xcol in convolutions) are just used within the operators
945 // if (end == 0 && begin > 0) end = begin+1;
946
947 if (begin> end) {
948 std::cout << "op " << op_index << "tensor_" << name << " begin " << begin << " " << " end " << end << std::endl;
949 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo: tensor_" + name + " has end before begin");
950 }
951
952 // write in code
953 out << SP << "dynamicTensorInfos.push_back( {" << begin << ", " << end << ", " << type_size << "* (" << tensor_size << ") });"
954 << " // tensor_" << name << std::endl;
955 tensors.push_back({name,type});
956 }
957 }
958 op_index++; // increment operator index
959 }
960 out << "\n" << SP << "auto memory_result = OrganizeMemory(dynamicTensorInfos);\n\n";
961 out << "// allocating now the memory\n";
962 out << SP << "fDynamicMemoryPool = std::vector<char>(memory_result.total_bytes);\n";
963 out << SP << "int idx = 0;\n";
964 for (auto & it : tensors) {
965 out << SP << "tensor_" << it.first << " = reinterpret_cast<" << ConvertTypeToString(it.second) << " *>(fDynamicMemoryPool.data() + memory_result.offsets[idx++]);\n";
966 }
967 // check that all dynamic tensors are covered
968 bool missingTensor = false;
969 for (auto &i : fDynamicTensorInfos) {
970 if (IsAliasTensor(i.first)) continue;
971 if (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) != fOutputTensorNames.end()) continue;
972 if (std::find(tensors.begin(), tensors.end(), std::pair<std::string,ETensorType>{i.first, i.second.type}) == tensors.end()) {
973 std::cout << "Dynamic tensors " << i.first << " is not in list of operator input/output " << std::endl;
974 missingTensor = true;
975 }
976 }
977 if (missingTensor)
978 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo - some tensors are not in input/output list");
979
980 fGC += out.str();
981}
982
983/// Check if a given parameter is used for the shape of an input tensor.
984bool RModel::IsInputTensorShapeParam(std::string const &paramName) const
985{
986 for (auto &name : fInputTensorNames) {
987 if (IsDimInputTensor(name)) {
988 auto shape = GetDynamicTensorShape(name);
989 for (auto &d : shape) {
990 if (d.param == paramName)
991 return true;
992 }
993 }
994 }
995 return false;
996}
997
998/// Collects all identifiers starting with "tensor_" in the input code,
999/// provided that the occurrence is not immediately preceded by a
1000/// character that is valid in a C++ identifier. Excludes input and output tensor names.
1001/// Returns a deduplicated std::vector<std::string>.
1002std::vector<std::string> RModel::CollectTensorMemberNames(const std::string &input)
1003{
1004 const std::string target = "tensor_";
1005
1006 std::vector<std::string> result;
1007
1008 for (size_t i = 0; i < input.size();) {
1009
1010 bool doCollect = false;
1011
1012 if (i + target.size() <= input.size() && input.compare(i, target.size(), target) == 0 &&
1013 (i == 0 || !IsIdentifierChar(input[i - 1]))) {
1014
1015 doCollect = true;
1016
1017 std::size_t j = i + target.size();
1018
1019 // Extend to full identifier
1020 while (j < input.size() && IsIdentifierChar(input[j]))
1021 ++j;
1022
1023 std::string fullName = input.substr(i, j - i);
1024
1025 // Exclude input tensor names
1026 for (std::string const &name : fInputTensorNames) {
1027 if (fullName == target + name) {
1028 doCollect = false;
1029 break;
1030 }
1031 }
1032
1033 // Exclude output tensor names
1034 if (doCollect) {
1035 for (std::string const &name : fOutputTensorNames) {
1036 if (fullName == target + name) {
1037 doCollect = false;
1038 break;
1039 }
1040 }
1041 }
1042
1043 if (doCollect) {
1044 result.push_back(fullName);
1045 }
1046
1047 i = j; // advance past the identifier
1048 } else {
1049 ++i;
1050 }
1051 }
1052
1053 // Deduplicate (order not preserved)
1054 std::sort(result.begin(), result.end());
1055 result.erase(std::unique(result.begin(), result.end()), result.end());
1056
1057 return result;
1058}
1059
1061 // generate the infer signature given the inputs: eg. "float * tensor1, float * tensor2"
1062 // if (decl = false) generate only calling signature (tensor1,tensor2,....)
1063 std::string rGC;
1064 std::unordered_map<std::string, int> inputParams;
1065 int i_input = 0;
1066 for (auto &name : fInputTensorNames) {
1067 // if is a dynamic tensor pass initial parameters
1068 if (IsDimInputTensor(name)) {
1069 auto shape = GetDynamicTensorShape(name);
1070 for (auto &d : shape) {
1071 std::string pName = d.param;
1072 // need to check if the input parameters is already existing in another input tensor
1073 if (d.isParam && inputParams.count(pName) == 0) {
1074 if (isdecl) rGC += "size_t ";
1075 rGC += d.param + ",";
1077 }
1078 }
1079 }
1080 if (isdecl) {
1082 if (type == "other")
1083 throw std::runtime_error("TMVA-SOFIE: input tensor " + name +
1084 " is of a data type which is not yet supported.");
1085 rGC += type + " const* ";
1086 }
1087 rGC += "tensor_" + name + ",";
1088 i_input++;
1089 }
1090
1091 if (fInputTensorNames.size() > 0) rGC.pop_back();// remove last ","
1092 return rGC;
1093}
1094
1095namespace {
1096
1097std::string typeForOutput(ETensorType t) {
1098 // The std::vector<bool> is a special type that is not wrapping continuous memory.
1099 // We don't want to use it as a return type.
1101 return ConvertTypeToString(t);
1102}
1103
1104std::string memberNameForDimShape(std::string name)
1105{
1106 if (!name.empty()) {
1107 name[0] = std::toupper(static_cast<unsigned char>(name[0]));
1108 }
1109 name = "f" + name;
1110 return name;
1111}
1112
1113}
1114
1116{
1117 size_t outputSize = fOutputTensorNames.size();
1118 // assume output types are all the same
1119
1120 bool sameOutputTypes = true;
1121 std::string inferReturnType; // type return by infer function
1123 fGC += "\n\n";
1124 if (outputSize == 1) {
1125 fGC += "std::vector<" + typeForOutput(eFirstOutputType) + ">";
1126 } else {
1127 // if all output types are the same we return an std::vector - otherwise a tuple
1128 for (std::string const &name : fOutputTensorNames) {
1130 sameOutputTypes = false;
1131 }
1132 if (sameOutputTypes)
1133 fGC += "std::vector<std::vector<" + typeForOutput(eFirstOutputType) + ">>";
1134 else {
1135 inferReturnType = "std::tuple<";
1136 for (size_t i = 0; i < outputSize; i++) {
1137 inferReturnType += "std::vector<" + typeForOutput(GetTensorType(fOutputTensorNames[i])) + ">";
1138 if (i < outputSize - 1)
1139 inferReturnType += ",";
1140 }
1141 inferReturnType += ">";
1143 }
1144 }
1145
1146 fGC += " infer(" + GenerateInferSignature() + "){\n";
1147
1148 std::string doInferArgs = GenerateInferSignature(false);
1149 if (!doInferArgs.empty())
1150 doInferArgs += ",";
1151 for (std::string const &name : fOutputTensorNames) {
1152 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1153 std::string n;
1154 if(!isDynamic) {
1155 n = std::to_string(ConvertShapeToLength(GetTensorShape(name)));
1156 } else {
1158 // Use the session member (fXxx) when any dim is a runtime-computed identifier
1159 // (e.g. NonZero count). For expression-type dims derived from input shapes
1160 // (e.g. "((W+-3)/2+1)"), use the expression directly.
1161 bool hasRuntimeParam = false;
1162 for (auto const &dim : GetDynamicTensorShape(name)) {
1163 if (dim.isParam && IsIdentifier(dim.param) && !IsInputTensorShapeParam(dim.param))
1164 hasRuntimeParam = true;
1165 }
1167 }
1168 std::string outputName = "output_tensor_" + name;
1169 fGC += SP + "std::vector<" + typeForOutput(GetTensorType(name)) + " > " + outputName + "(" + n + ");\n";
1170 doInferArgs += " " + outputName + ".data(),";
1171 if(isDynamic) {
1172 for (auto const &dim : GetDynamicTensorShape(name)) {
1173 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param)) {
1174 fGC += SP + "size_t " + dim.param + " = 0;\n";
1175 doInferArgs += " " + dim.param + ",";
1176 }
1177 }
1178 }
1179 }
1180 if (!doInferArgs.empty())
1181 doInferArgs.back() = ' ';
1182
1183 // verifying if the dynamic parameters are within allowed range
1184 std::unordered_set<std::string> input_params_checked;
1185 std::string dynamic_parameters_check = "";
1186 for (auto &name : fInputTensorNames) {
1187 if (IsDimInputTensor(name)) {
1188 auto shape = GetDynamicTensorShape(name);
1189 for (auto &d : shape) {
1190 std::string pName = d.param;
1191 if (d.isParam && input_params_checked.count(pName) == 0) {
1192 std::string memberName = memberNameForDimShape(d.param);
1193 dynamic_parameters_check += d.param + " > " + memberName + " || ";
1195 fGC += SP + "if (" + d.param + " > " + memberName + ") {\n";
1196 fGC += SP + SP + "throw std::runtime_error(\"TMVA-SOFIE: dynamic input tensor shape parameter " +
1197 d.param + " exceeds the initialized maximum allowed shape.\");\n";
1198 fGC += SP + "}\n";
1199 }
1200 }
1201 }
1202 }
1203
1204 if (fUseSession) {
1205 fGC += SP + "doInfer(*this, " + doInferArgs + ");\n";
1206 } else {
1207 fGC += SP + "doInfer(" + doInferArgs + ");\n";
1208 }
1209
1210 // If the output tensors have dynamic sizes, now is the time to set them
1211 for (std::string const &name : fOutputTensorNames) {
1212 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1213 if (isDynamic) {
1214 std::string outputName = "output_tensor_" + name;
1215 auto tensor_size = ConvertDimShapeToLength(GetDimTensorShape(name));
1216 fGC += SP + outputName + ".resize(" + tensor_size + ");\n";
1217 }
1218 }
1219
1220 fGC += SP + "return {";
1221 for (size_t i = 0; i < fOutputTensorNames.size(); i++) {
1222 fGC += "output_tensor_" + fOutputTensorNames[i];
1223 if (i < fOutputTensorNames.size() - 1)
1224 fGC += ",";
1225 }
1226 fGC += "};\n";
1227 fGC += "}\n"; // end of infer function scope
1228}
1229
1231{
1232 std::string sessionName = !fIsSubGraph ? "Session" : "Session_" + fName;
1233
1234 if (fUseSession && !fIsGNNComponent) {
1235 // forward declare session struct
1236 fGC += "struct " + sessionName + ";\n";
1237 }
1238
1239 // Determine the signature of the actual inference function
1241 if (!doInferSignature.empty())
1242 doInferSignature += ", ";
1243 for (auto const &name : fOutputTensorNames) {
1244 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1245 doInferSignature += typeForOutput(GetTensorType(name)) + " *tensor_" + name + ",";
1246 if(isDynamic) {
1247 for (auto const &dim : GetDynamicTensorShape(name)) {
1248 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param))
1249 doInferSignature += " size_t &" + dim.param + "_output,";
1250 }
1251 }
1252 }
1253 doInferSignature.back() = ' ';
1254
1255 if (fUseSession) {
1256 doInferSignature = sessionName + " const &session, " + doInferSignature;
1257 }
1258
1259 doInferSignature = "inline void doInfer(" + doInferSignature + ")";
1260
1261 if (!fIsGNNComponent) {
1262 // forward declare inference implementation
1263 fGC += doInferSignature + ";\n";
1264 }
1265
1266 // define the Session struct (for GNN this is generated in RModel_GNN)
1267 if (fUseSession && !fIsGNNComponent) {
1268 fGC += "struct " + sessionName + " {\n";
1269 }
1270
1271 // generate code for declaring the initialized tensors
1273
1275 // evaluate total intermediate memory and position intermediate tensor addresses
1276 std::string intermediate_memory_alloc_string = "";
1277 intermediate_memory_alloc_string += "\n// --- Positioning intermediate tensor memory --";
1278 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1279 if (fVerbose) {
1280 auto op = fOperators[op_idx].get();
1281 std::cout << "\n******************\n analyzing input/output operator " << op_idx << " "
1282 << typeid(*op).name() << std::endl;
1283 }
1286 }
1287
1288 // to check remaining unused fragments after memory allocation (lesser the better)
1289 // for (const auto &it: fIntermediateMemoryInfo.available_stack){
1290 // std::cout<<"chunk_idx: "<<it.first<<", chunk_size: "<<it.second<<"\n";
1291 // }
1292
1293 // generate the memory pool to be used by intermediate tensors
1295
1296 // position intermediate tensors
1298 }
1299
1300 // generate the declaring the intermediate tensors
1302 // generate code for declarations of some specific operators
1304
1305 // storing the parameters for future checking to avoid mismatches
1306 if (!fDimShapeNames.empty()) {
1307 fGC += "\n\n";
1308 std::sort(fDimShapeNames.begin(), fDimShapeNames.end());
1309 for (const auto &p : fDimShapeNames) {
1310 fGC += "size_t " + memberNameForDimShape(p) + ";\n";
1311 }
1312 }
1313
1314 // add subgraph session
1315 if (!fSubGraphs.empty()) fGC += "// subgraph sessions\n";
1316 for (auto & graph : fSubGraphs) {
1317 fGC += "Session_" + graph->fName + " fSession_" + graph->fName + ";\n";
1318 }
1319
1320 // Generate code for Session constructor
1321 if (fUseSession) {
1322 // add here specific operator code that needs to define session data members
1323 fGC += "\n";
1324 for (size_t id = 0; id < fOperators.size(); id++) {
1325 std::string opName = std::to_string(id);
1326 fGC += fOperators[id]->GenerateSessionMembersCode(opName);
1327 }
1328 fGC += "\n";
1329 // here add initialization and reading of weight tensors
1330 if (fUseWeightFile) {
1331 std::string fileName = fName;
1333 fileName += ".dat";
1334 }
1336 fileName += ".root";
1337 }
1338 fGC += sessionName + "(std::string filename =\"" + fileName + "\"";
1339 } else {
1340 // no need to pass weight file since it is not used
1341 // keep passing a string for compatibility
1342 fGC += sessionName + "(std::string = \"\"";
1343 }
1344 // add initialization of shape parameters
1345 // assume all parameters are of type size_t
1346 if (!fDimShapeNames.empty()) {
1347 // sort first the shape parameters in alphabetical order to avoid a random order
1348 std::sort(fDimShapeNames.begin(), fDimShapeNames.end() );
1349 for (auto &p : fDimShapeNames) {
1350 fGC += ",\n";
1351 fGC += " size_t " + p + " = " + fShapeParams[p];
1352 }
1353 }
1354 fGC += ") {\n";
1355
1356 // initializing dynamic parameters
1357 if (!fDimShapeNames.empty()) {
1358 fGC += "\n\n";
1359 std::sort(fDimShapeNames.begin(), fDimShapeNames.end());
1360 for (const auto &p : fDimShapeNames) {
1361 fGC += " " + memberNameForDimShape(p) + " = " + p + ";\n";
1362 }
1363 }
1364
1365 if (fUseWeightFile) {
1366 fGC += "\n//--- reading weights from file\n";
1368 fGC += "\n";
1369 // fUseWeightFile = fUseWeightFile;
1370 }
1371
1372 // now we have passed the parameters we can allocate the dynamic tensors
1374
1375 // add here initialization code for operator
1376 for (size_t id = 0; id < fOperators.size(); id++) {
1377 fGC += fOperators[id]->GenerateInitCode();
1378 }
1379
1380 fGC += "}\n\n";
1381 }
1382
1383 // generate the inference overload that returns an output struct
1385
1386 // end of session
1387 if (fUseSession && !fIsGNNComponent) {
1388 fGC += "}; // end of Session\n\n";
1389
1391 }
1392
1393 fGC += doInferSignature + " {\n";
1394 fGC += "\n";
1395
1396 // generate the inference code
1397 if (fVerbose)
1398 std::cout << "Generating main inference code for " << fName << std::endl;
1399
1400 if (fOutputTensorNames.size() == 0)
1401 throw std::runtime_error("TMVA-SOFIE: output size=0 are not supported");
1402
1403 std::string allOperatorCode;
1404
1405 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1406 if (fVerbose)
1407 std::cout << "Generating code for operator .... " << op_idx << std::endl;
1408 std::string operatorCode = fOperators[op_idx]->Generate(std::to_string(op_idx));
1410 }
1411
1412 // If the generated code users members of the session struct, use the
1413 // local variable name that we're using for the session:
1414 ReplaceAll(allOperatorCode, "this->", "session.");
1415
1416 if (fUseSession && !fIsGNNComponent) {
1417 // Collect all "tensor_*" data members that are not input or output tensors
1419 for (auto const& name: tensorMemberNames) {
1420 fGC += " auto &" + name + " = session." + name + ";\n";
1421 }
1422 fGC += "\n";
1423 }
1424
1426
1427 for (auto const& name: fOutputTensorNames) {
1428 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1429 if(isDynamic) {
1430 for (auto const &dim : GetDynamicTensorShape(name)) {
1431 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param))
1432 fGC += " " + dim.param + "_output = " + dim.param + ";\n";
1433 }
1434 }
1435 if(IsConstantTensor(name)) {
1436 std::string t = "session.tensor_" + name;
1438 fGC += " std::copy(" + t + ", " + t + " + " + std::to_string(length) + ", tensor_" + name + ");\n";
1439 }
1440 }
1441 fGC += "\n";
1442
1443 fGC += "}\n";
1444}
1445
1446void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, long pos, bool verbose)
1447{
1448 fVerbose = verbose;
1449 fBatchSize = batchSize;
1450 fReadPos = pos;
1451
1452 // session flag is used in operator initialize
1453 if (static_cast<std::underlying_type_t<Options>>(Options::kNoSession) & options) {
1454 fUseSession = false;
1456 }
1457 if (static_cast<std::underlying_type_t<Options>>(Options::kNoWeightFile) & options) {
1458 fUseWeightFile = false;
1460 }
1461 if (static_cast<std::underlying_type_t<Options>>(Options::kRootBinaryWeightFile) & options) {
1462 fUseWeightFile = true;
1464 }
1465 if (fUseWeightFile && !fUseSession) {
1466 throw std::runtime_error(
1467 "TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
1468 }
1469
1470 if (static_cast<std::underlying_type_t<Options>>(Options::kGNN) & options)
1471 fIsGNN = true;
1472 if (static_cast<std::underlying_type_t<Options>>(Options::kGNNComponent) & options)
1473 fIsGNNComponent = true;
1474
1475 // initialize the model including all operators and sub-graphs
1476 Initialize(batchSize, verbose);
1477
1478 // if having dynamic tensor we need to have a Session
1479 if (!fDynamicTensorInfos.empty()) {
1480 fUseSession = true;
1481 if (verbose)
1482 std::cout << "Warning: Force having a Session since model has dynamic tensors " << std::endl;
1483 }
1484
1485 std::string hgname;
1486 if (!fIsGNNComponent && !fIsSubGraph) {
1487 fGC.clear();
1489 }
1490
1491 // generate first code for the subgraphs
1492 for (auto &graph : fSubGraphs) {
1493 if (fVerbose)
1494 std::cout << "generate session code for subgraph " << graph->fName << std::endl;
1495 graph->GenerateSessionCode();
1496 fGC += graph->fGC;
1497 }
1498
1499 if (fVerbose)
1500 std::cout << "generate Main session code - model " << fName << std::endl;
1501
1502 // generate main session code
1504
1505 if (!fIsGNNComponent && !fIsSubGraph) {
1506 fGC += ("} //TMVA_SOFIE_" + fName + "\n");
1507 fGC += "\n#endif // " + hgname + "\n";
1508 }
1509}
1510
1512 // generate the code to read initialized tensors from a text data file
1514 // check if there are tensors to write
1515
1516 if (!fUseWeightFile) return;
1517
1518 fGC += " std::ifstream f;\n";
1519 fGC += " f.open(filename);\n";
1520 fGC += " if (!f.is_open()) {\n";
1521 fGC += " throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input weights\");\n";
1522 fGC += " }\n";
1523
1524 if(fIsGNNComponent) {
1525 fGC += " f.seekg(" + std::to_string(pos) + ");\n";
1526 }
1527
1528 fGC += " using TMVA::Experimental::SOFIE::ReadTensorFromStream;\n";
1529
1530 // loop on tensors and parse the file
1531 for (auto& i: fInitializedTensors) {
1532 // skip Constant and shape tensors (not written in a file)
1533 if (!i.second.IsWeightTensor()) continue;
1534 std::string tensor_name = "tensor_" + i.first;
1535 if (i.second.type() == ETensorType::FLOAT) {
1536 std::string length = std::to_string(ConvertShapeToLength(i.second.shape()));
1537 fGC += " ReadTensorFromStream(f, " + tensor_name + ", \"" + tensor_name + "\", " + length + ");\n";
1538 } else {
1539 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be read from a file");
1540 }
1541 }
1542 fGC += " f.close();\n";
1543 }
1544
1545 // generate the code to read initialized tensors from a ROOT data file
1547#ifdef SOFIE_SUPPORT_ROOT_BINARY
1548 fGC += " {\n";
1549 fGC += " std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
1550 fGC += " if (!rootFile->IsOpen()) {\n";
1551 fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
1552 fGC += " }\n";
1553
1554 std::string dirName = fName + "_weights";
1555 fGC += " if (!rootFile->GetKey(\"" + dirName + "\")) {\n";
1556 fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
1557 fGC += " }\n";
1558
1559 for (auto &i : fInitializedTensors) {
1560 // skip Constant and shape tensors
1561 if (!i.second.IsWeightTensor()) continue;
1562 fGC += " {\n";
1563 std::string tensor_name = "tensor_" + i.first;
1564 if (i.second.type() == ETensorType::FLOAT) {
1565 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
1566 fGC += dirName + "/" + tensor_name + "\"));\n";
1567 } else if (i.second.type() == ETensorType::DOUBLE) {
1568 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
1569 fGC += dirName + + "/" + tensor_name + "\"));\n";
1570 } else if (i.second.type() == ETensorType::INT64) {
1571 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
1572 fGC += dirName + "/" + tensor_name + "\"));\n";
1573 } else {
1574 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be read from a ROOT file");
1575 }
1576 fGC += " }\n";
1577 }
1578 fGC += " }\n";
1579#else
1580 throw std::runtime_error("SOFIE was not built with ROOT file support.");
1581#endif // SOFIE_SUPPORT_ROOT_BINARY
1582 }
1583}
1584
1586 // Determine the file extension based on the weight file type
1587 std::string fileExtension;
1588 switch (fWeightFile) {
1590 fileExtension = ".dat";
1591 break;
1593 fileExtension = ".root";
1594 break;
1596 fileExtension = ".dat";
1597 break;
1598 }
1599
1600 // If filename is empty, use the model name as the base filename
1601 if (filename.empty()) {
1603 }
1604
1605 // Write the initialized tensors to the file
1607#ifdef SOFIE_SUPPORT_ROOT_BINARY
1608 if(fIsGNNComponent || fIsGNN) {
1609 throw std::runtime_error("SOFIE-GNN yet not supports writing to a ROOT file.");
1610 }
1611 std::unique_ptr<TFile> outputFile(TFile::Open(filename.c_str(), "UPDATE"));
1612
1613 std::string dirName = fName + "_weights";
1614 // check if directory exists, in case delete to replace with new one
1615 if (outputFile->GetKey(dirName.c_str()))
1616 outputFile->rmdir(dirName.c_str());
1617
1618 auto outputDir = outputFile->mkdir(dirName.c_str());
1619
1620 for (const auto& item : fInitializedTensors) {
1621 // skip Constant tensors and tensors which are not writable (e.g. shape tensors)
1622 if (!item.second.IsWeightTensor()) continue;
1623 std::string tensorName = "tensor_" + item.first;
1624 size_t length = 1;
1625 length = ConvertShapeToLength(item.second.shape());
1626 if(item.second.type() == ETensorType::FLOAT) {
1627 const float* data = item.second.data<float>();
1628 std::vector<float> tensorDataVector(data, data + length);
1629 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<float>", tensorName.c_str());
1630 }
1631 else if(item.second.type() == ETensorType::DOUBLE) {
1632 const double* data = item.second.data<double>();
1633 std::vector<double> tensorDataVector(data, data + length);
1634 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<double>", tensorName.c_str());
1635 }
1636 else if(item.second.type() == ETensorType::INT64) {
1637 const int64_t* data = item.second.data<int64_t>();
1638 std::vector<int64_t> tensorDataVector(data, data + length);
1639 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<int64_t>", tensorName.c_str());
1640 }
1641 else {
1642 throw std::runtime_error("tmva-sofie tensor " + tensorName + " with type " + ConvertTypeToString(item.second.type()) +
1643 " cannot be written to a ROOT file");
1644 }
1645 }
1646 outputFile->Write(filename.c_str());
1647
1648 // this needs to be changed, similar to the text file
1649 return -1;
1650
1651#else
1652 throw std::runtime_error("SOFIE was not built with ROOT file support.");
1653#endif // SOFIE_SUPPORT_ROOT_BINARY
1654 } else if (fWeightFile == WeightFileType::Text) {
1655 std::ofstream f;
1656 if(fIsGNNComponent) {
1657 // appending all GNN components into the same file
1658 f.open(filename, std::ios::app);
1659 } else {
1660 f.open(filename);
1661 }
1662 if (!f.is_open())
1663 throw
1664 std::runtime_error("tmva-sofie failed to open file " + filename + " for tensor weight data");
1665 for (auto& i: fInitializedTensors) {
1666 // skip Constant tensors and not writable tensors (e.g. shape tensors)
1667 if (!i.second.IsWeightTensor()) {
1668 continue;
1669 }
1670 size_t length = ConvertShapeToLength(i.second.shape());
1671 std::string tensor_name = "tensor_" + i.first;
1672 f << tensor_name << " " << length << "\n";
1673 if (i.second.type() == ETensorType::FLOAT) {
1674 const float * data = i.second.data<float>();
1675 for (size_t idx = 0; idx < length; idx++) {
1676 // round to zero sub-normal values
1677 float value = data[idx];
1678 if (value != 0. && std::abs(value) < std::numeric_limits<float>::min() ) value = 0;
1679 // handle non-finite values explicitly
1680 if (std::isinf(value))
1681 f << (value > 0 ? "inf" : "-inf");
1682 else if (std::isnan(value))
1683 f << "nan";
1684 else
1685 f << std::setprecision(std::numeric_limits<float>::max_digits10) << value;
1686 f << ( (idx < length-1) ? " " : "\n" );
1687 }
1688 }
1689 else {
1690 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be written to a file");
1691 }
1692 if (f.fail())
1693 throw std::runtime_error("tmva-sofie failed to write tensor data to file for " + tensor_name);
1694 }
1695 long curr_pos = f.tellp();
1696 f.close();
1697 return curr_pos;
1698 } else {
1699 return -1;
1700 }
1701}
1702
1704 std::cout << "Summary of model " << GetName() << std::endl;
1705 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
1706 auto& r = *fOperators[op_idx].get();
1707 std::string raw_name = typeid(r).name();
1708 // look for ROperator_NAME
1709 std::string name = raw_name.substr(raw_name.find("ROperator_")+10, raw_name.size());
1710 std::cout << op_idx << " " << name << " : ";
1711 for (auto & t_in : r.GetOpInputTensors()) std::cout << t_in << " ";
1712 std::cout << " ----> ";
1713 for (auto & t_out : r.GetOpOutputTensors()) std::cout << t_out << " ";
1714 std::cout << std::endl;
1715 }
1716}
1717
1718/// To emit the dimensions of the input tensors as a data member of a session,
1719/// which is helpful when validating the inference inputs.
1721{
1722 fGC += "\n// Input tensor dimensions\n";
1723 fGC += "using TMVA::Experimental::SOFIE::SingleDim;\n";
1724 fGC += "using TMVA::Experimental::SOFIE::TensorDims;\n";
1725 fGC += "using TMVA::Experimental::SOFIE::makeDims;\n\n";
1726 bool hasDynamicInputTensors = false;
1727
1728 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1729 auto const &name = fInputTensorNames[iInput];
1730 if (IsDimInputTensor(name)) {
1732 }
1733 std::vector<Dim> shape = GetDimTensorShape(name);
1734 fGC += "constexpr std::array<SingleDim, " + std::to_string(shape.size()) + "> dim_" + name + "{";
1735 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1736 auto const &dim = shape[iDim];
1737 if (dim.isParam) {
1738 fGC += "SingleDim{\"" + dim.GetVal() + "\"}";
1739 } else {
1740 fGC += "SingleDim{" + dim.GetVal() + "}";
1741 }
1742 if (iDim != shape.size() - 1) {
1743 fGC += ", ";
1744 }
1745 }
1746 fGC += "};\n";
1747 }
1748 fGC += "\nconstexpr std::array<TensorDims, " + std::to_string(fInputTensorNames.size()) + "> inputTensorDims{\n";
1749 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1750 auto const &name = fInputTensorNames[iInput];
1751 fGC += SP + "makeDims(dim_" + name + ")";
1752 if (iInput == fInputTensorNames.size() - 1) {
1753 fGC += "\n";
1754 } else {
1755 fGC += ",\n";
1756 }
1757 }
1758 fGC += "};\n";
1759
1760 fGC +=
1761 "\nconstexpr bool hasDynamicInputTensors{" + std::string{hasDynamicInputTensors ? "true" : "false"} + "};\n\n";
1762}
1763
1765 std::cout << "Model requires following inputs:\n";
1766 for (auto& inputInfo: fInputTensorInfos) {
1767 std::cout << "Parametrised Tensor name: " << inputInfo.first << "\t";
1768 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1769 std::cout << "shape: [";
1770 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1771 if (inputInfo.second.shape[i].isParam) {
1772 std::cout << inputInfo.second.shape[i].param;
1773 } else {
1774 std::cout << inputInfo.second.shape[i].dim ;
1775 }
1776 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1777 }
1778 std::cout << "]" << std::endl;
1779 }
1780
1781 for (auto& inputInfo: fReadyInputTensorInfos) {
1782 std::cout << "Fully Specified Tensor name: " << inputInfo.first << "\t";
1783 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1784 std::cout << "shape: [";
1785 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1786 std::cout << inputInfo.second.shape[i];
1787 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1788 }
1789 std::cout << "]" << std::endl;
1790 }
1791 std::cout << "\n";
1792}
1793
1795 std::cout << "Model initialized the following tensors:\n";
1796 for (auto& it: fInitializedTensors) {
1797 std::cout << "Tensor name: \"" << it.first << "\"\t";
1798 std::cout << "type: " << ConvertTypeToString(it.second.type()) << "\t";
1799 std::cout << "shape: [";
1800 for (size_t i = 0; i < it.second.shape().size(); i++) {
1801 std::cout << it.second.shape()[i];
1802 if (i < it.second.shape().size() - 1) std::cout << ",";
1803 }
1804 std::cout << "]";
1805 if (it.second.IsConstantTensor()) std::cout << " (Constant)";
1806 if (it.second.IsNotWritable()) std::cout << " (Not Writable)";
1807 std::cout << std::endl;
1808 }
1809 std::cout << "\n";
1810}
1811
1813 std::cout << "Model specify the following intermediate tensors:\n";
1814 for (auto& it: fIntermediateTensorInfos) {
1815 std::cout << "Tensor name: \"" << it.first << "\"\t";
1816 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
1817 std::cout << "shape: [";
1818 for (size_t i = 0; i < it.second.shape.size(); i++) {
1819 std::cout << it.second.shape[i];
1820 if (i < it.second.shape.size() - 1) std::cout << ",";
1821 }
1822 std::cout << "]" << std::endl;
1823 }
1824 std::cout << "\n";
1825}
1826
1828 std::cout << "Model specify the following dynamic tensors:\n";
1829 for (auto& it: fDynamicTensorInfos) {
1830 std::cout << "Tensor name: \"" << it.first << "\"\t";
1831 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
1832 std::cout << "shape: [";
1833 for (size_t i = 0; i < it.second.shape.size(); i++) {
1834 std::cout << it.second.shape[i].GetVal();
1835 if (i < it.second.shape.size() - 1) std::cout << ",";
1836 }
1837 std::cout << "]" << std::endl;
1838 }
1839 std::cout << "\n";
1840}
1841
1843 std::cout << "Model specify the following output tensors:\n";
1844 for (auto& it: fOutputTensorNames) {
1845 std::cout << "Tensor name: \"" << it << "\"\t";
1846 try {
1847 auto shape = GetDimTensorShape(it);
1848 std::cout << "with shape: " << ConvertDimShapeToString(shape) << std::endl;
1849 } catch (...) {
1850 std::cout << "with shape not yet defined" << std::endl;
1851 }
1852 }
1853 std::cout << "\n";
1854}
1855
1857 auto it = fInitializedTensors.find(name);
1858 if (it == fInitializedTensors.end()) {
1859 std::cout << "Tensor " << name << " not found in model's initialized tensor list" << std::endl;
1860 return;
1861 }
1862
1863 std::cout << "Tensor name: " << it->first << "\t";
1864 std::cout << "type: " << ConvertTypeToString(it->second.type()) << "\t";
1865 int length =1;
1866 std::cout << "shape: [";
1867 for (size_t i = 0; i < it->second.shape().size(); i++) {
1868 std::cout << it->second.shape()[i];
1869 length *= it->second.shape()[i];
1870 if (i < it->second.shape().size() - 1) std::cout << ",";
1871 }
1872 std::cout << "]" << std::endl;
1873 bool ellipsis = true;
1874 if (n_print > length) {
1875 n_print = length;
1876 ellipsis = false;
1877 }
1878
1879 std::cout << "data: [" << std::endl;
1880 if (it->second.type() == ETensorType::FLOAT) {
1881 auto converted_data = it->second.data<float>();
1882 for (int i =0; i < n_print; i++) {
1883 std::cout << converted_data[i];
1884 if (i < n_print - 1) std::cout << " ,";
1885 }
1886 }
1887 if (ellipsis) std::cout << ", ...";
1888 std::cout << "]" << std::endl;
1889
1890}
1891
1892void RModel::OutputGenerated(std::string filename, bool append) {
1893
1895
1896 // write weights in a text file
1897 if (fUseWeightFile) {
1898 if (!filename.empty()) {
1899 size_t pos = filename.find(".hxx");
1901 filename.replace(pos, 4, ".dat");
1903 filename = filename.erase(pos, 4);
1904 filename += ".root";
1905 }
1906 } else {
1907 filename = fName;
1908 filename += fWeightFile == WeightFileType::Text ? ".dat" : ".root";
1909 }
1911 }
1912}
1913
1914void RModel::Streamer(TBuffer &R__b) {
1915 if (R__b.IsReading()) {
1916 RModel::Class()->ReadBuffer(R__b, this);
1917 for (auto & i : fInitializedTensors) {
1918 i.second.CastPersistentToShared();
1919 }
1920 }
1921 else {
1922 for (auto & i : fInitializedTensors) {
1923 i.second.CastSharedToPersistent();
1924 }
1925 RModel::Class()->WriteBuffer(R__b, this);
1926 }
1927}
1928
1929} // namespace SOFIE::Experimental::TMVA
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
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 filename
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 target
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 r
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 result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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 length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 type
char name[80]
Definition TGX11.cxx:148
const_iterator begin() const
const_iterator end() const
Buffer base class used for serializing objects.
Definition TBuffer.h:43
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3787
void GenerateHeaderInfo(std::string &hgname)
void OutputGenerated(std::string filename="", bool append=false)
const std::string & GetName() const
void AddBlasRoutines(std::vector< std::string > routines)
void AddNeededStdLib(std::string libname)
void AddShapeParam(const std::string &name, size_t def_value=0)
Definition RModel.cxx:331
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:64
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:100
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
Definition RModel.hxx:31
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:282
void AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name)
Definition RModel.cxx:246
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:297
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:1060
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:157
std::vector< std::unique_ptr< ROperator > > fOperators
Definition RModel.hxx:39
void GenerateRequiredInputTensorInfo()
To emit the dimensions of the input tensors as a data member of a session, which is helpful when vali...
Definition RModel.cxx:1720
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:1892
std::unordered_map< std::string, std::string > fAliasTensors
Definition RModel.hxx:34
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:168
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:30
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
Definition RModel.cxx:339
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
Definition RModel.hxx:28
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:228
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:314
std::vector< std::string > fDimShapeNames
Definition RModel.hxx:35
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:218
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
Definition RModel.hxx:46
void AddInputTensorName(std::string name)
Definition RModel.cxx:187
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:36
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:287
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:256
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:269
bool IsAliasTensor(const std::string &name) const
check if a tensor is a alias tensor
Definition RModel.cxx:260
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
Definition RModel.cxx:484
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:191
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:1856
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:273
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:563
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1585
OptimizationLevel fOptimizationLevel
Definition RModel.hxx:25
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0, bool verbose=false)
Definition RModel.cxx:1446
std::vector< std::string > CollectTensorMemberNames(const std::string &input)
Collects all identifiers starting with "tensor_" in the input code, provided that the occurrence is n...
Definition RModel.cxx:1002
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
Definition RModel.cxx:111
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:27
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:362
MemoryPoolInfo fIntermediateMemoryInfo
! intermediate memory info (transient)
Definition RModel.hxx:45
std::string AllocateIntermediateMemory(std::span< const std::string_view > op_output_tensors)
Definition RModel.cxx:379
std::unordered_map< std::string, std::pair< std::vector< Dim >, bool > > fShapeTensors
Definition RModel.hxx:32
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:721
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:33
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:371
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:125
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:37
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
Definition RModel.hxx:29
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:353
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:264
std::vector< std::shared_ptr< RModel > > fSubGraphs
! sub-graph models (transient)
Definition RModel.hxx:41
bool IsReadyInputTensor(const std::string &name) const
Definition RModel.cxx:291
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:346
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:238
bool IsInputTensorShapeParam(std::string const &name) const
Check if a given parameter is used for the shape of an input tensor.
Definition RModel.cxx:984
const Int_t n
Definition legend1.C:16
void ReplaceAll(std::string &str, const std::string &from, const std::string &to, bool recurse=false)
std::string Clean_name(std::string input_tensor_name)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
constexpr size_t GetTypeSize(ETensorType type)
std::string GenerateConstantTensorCode(const std::pair< std::string, InitializedTensor > &t)
Definition RModel.cxx:753
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
std::underlying_type_t< Options > operator|(Options opA, Options opB)
Definition RModel.cxx:56
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
std::string ConvertValToString(T value)
std::map< size_t, TensorMemoryInfo > total_stack
std::map< size_t, size_t > available_stack