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{
193 AddBlasRoutines(op->GetBlasRoutines());
194 auto libs = op->GetStdLibs();
195 auto op_input_tensors = op->GetOpInputTensors();
196 for (auto &stdlib : libs) {
198 }
199 if (order_execution >= 0) {
200 fOperators.insert(fOperators.begin() + order_execution, std::move(op));
201 } else {
202 fOperators.push_back(std::move(op));
203 order_execution = fOperators.size() - 1;
204 }
205
206 // storing the last usage of tensors which are input to the operator
207 // (excluding tensors which are inputs to the model or the initialized (weights) tensors)
208 // We call this function during parsing so we don't have yet initialized the operators
209 for (size_t index = 0; index < op_input_tensors.size(); index++) {
211 std::find(fInputTensorNames.begin(), fInputTensorNames.end(),
213
215 if (Verbose())
216 std::cout << "adding order execution for " << op_input_tensors[index] << " order " << order_execution
217 << std::endl;
218 }
219 }
220}
221
222void RModel::AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
223 tensor_name = UTILITY::Clean_name(tensor_name);
224 //NB: own data
225 if (CheckIfTensorAlreadyExist(tensor_name)) {
226 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
227 }
229 fInitializedTensors[tensor_name] = new_tensor;
230}
231
232void RModel::AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
233 tensor_name = UTILITY::Clean_name(tensor_name);
234 //NB: own data
235 if (CheckIfTensorAlreadyExist(tensor_name)) {
236 throw std::runtime_error("TMVA-SOFIE: constant tensor with name " + tensor_name + " already exists \n");
237 }
238 InitializedTensor new_tensor {type, shape, data, true}; // add here flag to specify is a constant tensor
239 fInitializedTensors[tensor_name] = new_tensor;
240}
241
242void RModel::AddShapeTensor(const std::string & name, const std::vector<Dim> & shape_values, bool scalar){
243 auto tensor_name = UTILITY::Clean_name(name);
244 if (fShapeTensors.count(tensor_name) != 0) {
245 throw std::runtime_error("TMVA-SOFIE: shape tensor with name " + tensor_name + " already exists \n");
246 }
247 fShapeTensors[tensor_name] = std::make_pair(shape_values, scalar);
248}
249
250void RModel::AddAliasTensor(const std::string & name, const std::string & origin){
251 // add an alias tensor to origin
252 auto tensor_name = UTILITY::Clean_name(name);
254 if (fAliasTensors.count(tensor_name) != 0) {
255 throw std::runtime_error("TMVA-SOFIE: alias tensor with name " + tensor_name + " already exists \n");
256 }
257 fAliasTensors[tensor_name] = origin_name;
258}
259
260bool RModel::IsShapeTensor(const std::string & tensor_name) const {
261 return fShapeTensors.count(tensor_name) != 0;
262}
263
264bool RModel::IsAliasTensor(const std::string & tensor_name) const {
265 return fAliasTensors.count(tensor_name) != 0;
266}
267
268const std::vector<Dim> & RModel::GetShapeTensorValues(const std::string & tensor_name) const {
269 //if (!IsShapeTensor(tensor_name) ) return std::vector<Dim>{};
270 return fShapeTensors.at(tensor_name).first;
271}
272
273bool RModel::IsInitializedTensor(const std::string& tensorName) const {
274 std::string name = UTILITY::Clean_name(tensorName);
275 return fInitializedTensors.find(name) != fInitializedTensors.end();
276}
277bool RModel::IsConstantTensor(const std::string& tensorName) const {
278 // a constant tensor is an initialized tensor but has the constant flag set
279 std::string name = UTILITY::Clean_name(tensorName);
280 auto itr = fInitializedTensors.find(name);
281 if (itr == fInitializedTensors.end()) return false;
282 return itr->second.IsConstantTensor();
283}
284
285// dynamic tensors include also Dim input tensors
286bool RModel::IsDynamicTensor(const std::string& tensorName) const {
287 std::string name = UTILITY::Clean_name(tensorName);
289 return (ret) ? true : IsDimInputTensor(tensorName);
290}
291bool RModel::IsDimInputTensor(const std::string& tensorName) const {
292 std::string name = UTILITY::Clean_name(tensorName);
293 return fInputTensorInfos.find(name) != fInputTensorInfos.end();
294}
295bool RModel::IsReadyInputTensor(const std::string& tensorName) const {
296 std::string name = UTILITY::Clean_name(tensorName);
298}
299
300// generic addition of a tensor
301void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape) {
303 if (!int_shape.empty())
304 AddIntermediateTensor(tensor_name, type, int_shape);
305 else
306 AddDynamicTensor(tensor_name, type, dim_shape);
307}
308
309void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape) {
310 tensor_name = UTILITY::Clean_name(tensor_name);
311 if (CheckIfTensorAlreadyExist(tensor_name)) {
312 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
313 }
314 TensorInfo new_tensor {type, shape};
316}
317
318void RModel::AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape){
319 tensor_name = UTILITY::Clean_name(tensor_name);
320 if (CheckIfTensorAlreadyExist(tensor_name)){
321 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
322 }
324 fDynamicTensorInfos[tensor_name] = new_tensor;
325 // store shape parameter if not existing
326 for (auto &d : shape) {
327 if (d.isParam) {
328 if (d.dim != size_t(-1)) {
329 AddShapeParam(d.param, d.dim);
330 }
331 }
332 }
333}
334
335void RModel::AddShapeParam(const std::string & param, size_t default_value) {
336 if (fShapeParams.count(param) == 0) {
337 fShapeParams[param] = std::to_string(default_value);
338 // add also in the vector list (used to keep the order)
339 fDimShapeNames.push_back(param);
340 }
341}
342
344 fOutputTensorNames.clear();
345 for(auto& it : outputtensornames) {
346 fOutputTensorNames.emplace_back(UTILITY::Clean_name(it));
347 }
348}
349
350void RModel::UpdateOutputTensorList(std::vector<std::string> curr_output_tensors, std::vector<std::string> new_output_tensors) {
351 for(auto& it:curr_output_tensors) {
352 fOutputTensorNames.erase(std::remove(fOutputTensorNames.begin(), fOutputTensorNames.end(), it), fOutputTensorNames.end());
353 }
355}
356
357void RModel::UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
358 tensor_name = UTILITY::Clean_name(tensor_name);
359 if (!CheckIfTensorAlreadyExist(tensor_name)) {
360 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to update it");
361 }
363 fInitializedTensors[tensor_name] = new_tensor;
364}
365
366std::shared_ptr<void> RModel::GetInitializedTensorData(std::string tensor_name) {
367 auto f = fInitializedTensors.find(tensor_name);
368 if (f == fInitializedTensors.end()) {
369 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to get its data");
370 } else {
371 return f->second.sharedptr();
372 }
373}
374
375void RModel::SetNotWritableInitializedTensor(const std::string & tensor_name) {
376 auto t = fInitializedTensors.find(tensor_name);
377 if (t == fInitializedTensors.end()) {
378 throw std::runtime_error("TMVA-SOFIE: initialized tensor " + tensor_name + " not found when trying to get its info");
379 }
380 t->second.SetNotWritable();
381 }
382
383std::string RModel::AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors)
384{
385 std::stringstream code;
386
387 if (fVerbose) {
388 std::cout << "Total chunks allocated\n";
390 std::cout << "..... chunk " << chunk->first << " size " << chunk->second.tensor_size << " " << chunk->second.tensor_name << std::endl;
391 }
392 }
393
394 auto declareIntermediateTensor = [this, &code](std::string const &name, size_t size, size_t location) {
395 std::string typeName = ConvertTypeToString(GetTensorType(name));
396 code << "\n // Allocating memory for intermediate tensor " << name << " with size " << size << " bytes";
397 code << "\n"
398 << typeName << "* " << TensorMember(name) << " = reinterpret_cast<" << typeName
399 << "*>(fIntermediateMemoryPool.data() + " << location << ");\n";
400 };
401
402 if (fVerbose) std::cout << "*** AllocateIntermediateMemory: Loop on op output tensors\n";
403 // order output tensors by size
404 std::vector<TensorMemoryInfo> ordered_output_tensors;
405
406 for (auto &it : op_output_tensors) {
407 auto name = std::string(it);
410 continue;
411
412 // case of alias tensor
413 if (IsAliasTensor(name)) {
414 continue;
415 }
416
418 // important fill the pair in the ordered output tensors with the string view and not the string
419 TensorMemoryInfo tmi = {it, tensor_size};
420 ordered_output_tensors.push_back(tmi);
421 }
423 [](const TensorMemoryInfo &a, const TensorMemoryInfo &b) { return a.tensor_size > b.tensor_size; });
424
425 for (auto &it : ordered_output_tensors) {
426 bool allocated = false;
427 std::string name = std::string{it.tensor_name};
428 size_t tensor_size = it.tensor_size;
429 if (fVerbose)
430 std::cout << "output tensor " << name << " size " << tensor_size << std::endl;
431
434
435 if (fVerbose) std::cout << ".. available chunk " << chunk->first << " with size = " << chunk->second;
436 // check if available memory chunks can accommodate the tensor
437 if (chunk->second >= tensor_size) {
438 // need to use here string_view (i.e it.tensor_name)
439 // split returns the new chunk with size of new tensor. The free chunk is before the used one
440 auto new_chunk = fIntermediateMemoryInfo.total_stack[chunk->first].split(it.tensor_name, tensor_size);
441 auto new_chunk_location = chunk->first + chunk->second - tensor_size;
443
445 chunk->second -= tensor_size;
446
447 allocated = true;
448
449 if (fVerbose) std::cout << " is re-used and split in a new of size " << new_chunk.tensor_size << " at " << new_chunk_location;
450
451 if (chunk->second == 0) {
452 if (fVerbose) std::cout << " and deleted since size matches";
454 }
455 if (fVerbose) std::cout << std::endl;
456 break;
457 } else if (chunk->first == fIntermediateMemoryInfo.available_stack.rbegin()->first &&
458 fIntermediateMemoryInfo.total_stack.rbegin()->first == chunk->first) {
459 // case last available chunk is the last in the memory, we can increase that one
460 fIntermediateMemoryInfo.total_stack[chunk->first] = {it.tensor_name, tensor_size};
461 declareIntermediateTensor(name, tensor_size, chunk->first);
463 allocated = true;
464 if (fVerbose) std::cout << " is extended with a bigger one of size " << tensor_size << std::endl;
465 break;
466 }
467 ++chunk;
468 if (fVerbose) std::cout << std::endl;
469 }
470
471 if (!allocated) {
473 ? 0
474 : fIntermediateMemoryInfo.total_stack.rbegin()->first +
475 fIntermediateMemoryInfo.total_stack.rbegin()->second.tensor_size;
476
478
480
481 if (fVerbose) std::cout << "no chunk available - add in total stack a new chunk with size of tensor and idx : " << chunk_idx
482 << std::endl;
483 }
484 }
485 return code.str();
486}
487
488void RModel::CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_input_tensors, const size_t& op_idx){
489 if (fVerbose) std::cout << "*** CheckAndFlushIntermediateMemory: Loop on input tensors for op " << op_idx << "\n";
490 //print available chunks
491 if (fVerbose) std::cout << "available chunks before freeing them : \n";
494 if (fVerbose) std::cout << "-- free chunk " << chunk->first << " size = " << chunk->second << std::endl;
495 }
496 for (auto &iv : op_input_tensors) {
497 // last occurrence of the tensor is reached => flush it from memory
498 if (fVerbose) std::cout << ".. input tensors : " << iv;
499
500 // for alias tensors replace name with its alias
501 std::string it{iv}; // convert view to string
502 if (IsAliasTensor(it))
503 it = fAliasTensors[it];
505 if (fVerbose) std::cout << " flash condition is met - looping on chunks to find matching one \n";
506 for (auto chunk = fIntermediateMemoryInfo.total_stack.begin();
508 if (fVerbose) std::cout << "--- chunk " << chunk->first << " , " << chunk->second.tensor_name << " size " << chunk->second.tensor_size;
509 if (chunk->second.tensor_name == it) {
510 if (fVerbose) std::cout << " -- Found chunk corresponding to input tensor: " << chunk->first;
511 // check if nearby chunks in available memory can coalesce
513 chunk->first); // smallest element greater than the flushed chunk idx
516 : std::prev(first_greater); // largest element smaller than the flushed chunk idx
517
518 // check if the next stack entry is actually adjacent in memory
519
521 last_smaller->first + last_smaller->second == chunk->first) {
522 // merge chunk with previous one
523 last_smaller->second += chunk->second.tensor_size;
525 if (fVerbose) std::cout << " is adjacent in memory with previous one - merge ";
527 last_smaller->first + last_smaller->second == first_greater->first) {
528 // merge also with following one
529 last_smaller->second += first_greater->second;
532 // delete merged one in available stack and in total stack
535 if (fVerbose) std::cout << " merge also with following that is free ";
536 }
538 if (fVerbose) std::cout << std::endl;
539 break;
541 chunk->first + chunk->second.tensor_size == first_greater->first) {
542 // merge with first greater
543 if (fVerbose) std::cout << " is adjacent in memory with following one - merge \n";
544 // cannot modify idx of first_greter. Insert a new one and delete previous one
545 size_t new_size = chunk->second.tensor_size + first_greater->second;
546 size_t first_greater_idx = first_greater->first;
548 // cannot use anymore first_greater
553 } else {
554 fIntermediateMemoryInfo.available_stack.insert({chunk->first, chunk->second.tensor_size});
555 if (fVerbose) std::cout << " insert in the available stack the chunk with size " << chunk->second.tensor_size << std::endl;
556 }
557 chunk->second.tensor_name = "free";
558 break;
559 }
560 }
561 } else {
562 if (fVerbose) std::cout << std::endl;
563 }
564 }
565}
566
567void RModel::Initialize(int batchSize, bool verbose) {
568 std::map<std::string, size_t> inputParams;
569 if (batchSize > 0) {
570 inputParams["input_size"] = batchSize;
571 inputParams["batch_size"] = batchSize;
572 inputParams["bs"] = batchSize;
573 }
574 Initialize(inputParams, verbose);
576}
577void RModel::Initialize(const std::map<std::string, size_t> & inputParams, bool verbose) {
578
579 fVerbose = int(verbose);
580
581 if (fIsInitialized) {
582 if (verbose)
583 std::cout << "Model is already initialized - skip initialization " << std::endl;
584 return;
585 }
587 fDynamicTensorInfos.clear();
588
589
590 // loop on inputs and see if shape can be full specified
591 // if the batch size is provided it can be used to specify the full shape
592 // Add the full specified tensors in fReadyInputTensors collection
593 auto originalInputTensorInfos = fInputTensorInfos; // need to copy because we may delete elements
594 for (auto &input : originalInputTensorInfos) {
595 if (verbose) std::cout << "looking at the tensor " << input.first << std::endl;
596 // if a parameter (e.g. batch_size) is specified use for converting parametric shape in defined one
597 if (!inputParams.empty()) {
598 for (auto &d : input.second.shape) {
599 if (d.isParam) {
600 std::string pname = d.param;
601 if (pname == input.first + "_size") pname = "input_size";
602 auto itr = inputParams.find(pname);
603 if (itr != inputParams.end() ) {
604 d = Dim{ itr->second };
605 if (verbose)
606 std::cout << "Tensor: " << input.first << " - fix parametric shape " << itr->first << " to " << itr->second << std::endl;
607 }
608 }
609 }
610 }
611 // see if shape now is fully defined
612 auto shape = ConvertShapeToInt(input.second.shape);
613 if (verbose)
614 std::cout << "converting input shape for " << input.first << " " << ConvertShapeToString(shape) << " from "
615 << ConvertDimShapeToString(input.second.shape) << std::endl;
616 if (!shape.empty()) {
617 // case shape is defined (not parametric) we add the tensor in the fReadyInputTensorInfos map and
618 // we remove the tensor from the fInputTensorInfo where th eold parametric shape was stored
619 fInputTensorInfos.erase(input.first);
620 // add to the ready input tensor information the new fixed shape
621 AddInputTensorInfo(input.first, input.second.type, shape);
622 // check consistency
624 }
625 // store the parameters of the input tensors
626 else {
627 // store the found parametric shape parameters
628 for (auto &d : input.second.shape) {
629 if (d.isParam) {
630 if (fShapeParams.count(d.param) == 0) {
631 fDimShapeNames.push_back(d.param);
632 fShapeParams[d.param] = std::to_string(d.dim);
633 }
634 }
635 }
636 }
637 }
638
639 if (verbose) {
642 }
643
644 // Go through model and initialize each operator
645 int i = 0;
646
647 std::vector<size_t> temp_available_stack; // vector stores individual chunks of available memory that maybe reused
648
649 // Build set of initialized tensors consumed by at least one runtime operator (need for later)
650 std::unordered_set<std::string> runtimeInitializedInputs;
651 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
652 if (verbose) {
653 auto& r = *fOperators[op_idx].get();
654 std::cout << "Initializing operator " << i << " " << typeid(r).name() << std::endl;
655 }
656 fOperators[op_idx]->Initialize(*this);
657 for(auto &it:fOperators[op_idx]->GetOpOutputTensors()){
658 std::string name = std::string{it};
659 // check if tensor is not an initialized or output tensor and it is not already in the list
661 std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end() &&
663 {
665 }
666 }
667 // loop for non-constant operators and flag the inputs which are initialized tensors to make sure they are writable
668 if (!fOperators[op_idx]->IsOutputConstant()) {
669 for (auto &it : fOperators[op_idx]->GetOpInputTensors()) {
670 std::string name = std::string{it};
671 if (fInitializedTensors.find(name) != fInitializedTensors.end()) {
673 }
674 }
675 }
676
677 i++;
678 }
679
680 // loop on initialized tensors and make the integers as constant to be
681 // not written in a weight file and check if the tensors flagged as not writable are really not writable,
682 // i.e. are not used by non constant operators
683 for (auto &it : fInitializedTensors) {
684 // check if not-writable tensors are really not writable, i.e. are not used by non constant operators
685 if (it.second.IsNotWritable() && runtimeInitializedInputs.find(it.first) != runtimeInitializedInputs.end()) {
686 it.second.SetWritable();
687 if (verbose) {
688 std::cout << "Initialized tensor " << it.first << " is flagged as not writable but is used by non constant operators, set it as writable \n";
689 }
690 }
691 // 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
692 // only FLOAT tensors are written in a weight file
693 if (it.second.type() != ETensorType::FLOAT) {
694 it.second.SetConstant();
695 }
696 }
697
698 // check if there are initialized tensors to write in a weight file
699 if (fUseWeightFile) {
700 bool modelHasWeights = false;
701 for (auto &it : fInitializedTensors) {
702 if (it.second.IsWeightTensor()) {
703 modelHasWeights = true;
704 break;
705 }
706 }
707 if (!modelHasWeights)
708 fUseWeightFile = false;
709 }
710
711 // update fIntermediateTensorFrequencyLookup for alias tensors
712 for (auto & it : fAliasTensors) {
716 else {
717 // take the largest one
719 }
720 }
721
722 fIsInitialized = true;
723}
724
725void RModel::InitializeSubGraph(std::shared_ptr<RModel> graph) {
726 // add the subgraph to the list
727 fSubGraphs.push_back(graph);
728 //this needs to be done before initializing
729 graph->fParentGraph = this;
730 graph->fIsSubGraph = true;
731
732 graph->Initialize(fBatchSize, fVerbose);
733 // set the same options as parent model
734 graph->fWeightFile = fWeightFile;
735 graph->fUseWeightFile = fUseWeightFile;
736 graph->fUseSession = fUseSession;
737 // add needed blas routines and libs
738 std::vector<std::string> blasRoutines;
739 for (auto & e : graph->fNeededBlasRoutines)
740 blasRoutines.push_back(e);
742 for (auto e : graph->fNeededStdLib)
744
745 // add parent input tensors to current graph
746 for (auto & name : fInputTensorNames)
747 graph->fInputTensorNames.emplace_back(name);
748
749 // clean graph name
750 graph->fName = UTILITY::Clean_name(graph->fName);
751
752}
753
754// Function to generate the code for declaring and initializing constant tensors
755// This is for tensors which are not part of weight files and can be created from the Constant operator
756template <typename T>
757std::string GenerateConstantTensorCode(const std::pair<std::string, InitializedTensor> &t)
758{
759 std::stringstream strs;
760 std::string type = ConvertTypeToString(t.second.type());
761 size_t length = ConvertShapeToLength(t.second.shape());
762 // avoid using stack sizes for constant tensors to reduce compilation time
763 // also for weights which can be broadcasted do not use stack but allocate as a std::vector
764 bool allocateOnStack = (length > 100 || t.second.IsWeightTensor()) ? false : true;
765
766 const T *data = t.second.data<T>();
767
768 // and check if all values are the same
769 bool sameData = false;
770
771 // for non stack allocation check if data are the same
772 if (!allocateOnStack && length > 1) {
773 size_t idx = 1;
774 do {
775 sameData = (data[idx] == data[idx - 1]);
776 idx++;
777 } while (sameData && idx < length);
778 }
779 if (allocateOnStack) {
780 strs << type << " fTensor_" << t.first << "[" << length << "] = " << ConvertValuesToString(length, data) << ";\n";
781 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ";\n";
782 } else {
783 strs << "std::vector<" << type << "> fTensor_" << t.first << " = ";
784 if (sameData)
785 strs << "std::vector<" << type << ">(" << length << ", " << ConvertValToString(data[0]) << ");\n";
786 else {
788 }
789 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ".data();\n";
790 }
791 return strs.str();
792}
793
795{
796 if (!fInitializedTensors.empty())
797 fGC += "// initialized (weights and constant) tensors\n";
798
799 // here are constant tensor or initialized ones which are not weights (e.g. int64_t tensors )
800 for (auto &i : fInitializedTensors) {
801 if (i.second.IsNotWritable()) continue;
802 size_t length = ConvertShapeToLength(i.second.shape());
803 if (!fUseWeightFile || i.second.IsConstantTensor() || !i.second.IsWeightTensor() || i.second.type() != ETensorType::FLOAT ) {
804 if (i.second.type() == ETensorType::FLOAT) {
805 // check if NaN of Inf are inside tensor data
806 bool hasInfOrNaN = false;
807 const float *data = i.second.data<float>();
808 for (size_t idx = 0; idx < length; idx++) {
809 if (std::is_floating_point<float>::value) {
810 if (std::isinf(data[idx]) || std::isnan(data[idx])) {
811 hasInfOrNaN = true;
812 break;
813 }
814 }
815 }
816 if (hasInfOrNaN)
817 AddNeededStdLib("limits");
819 fConstantTensorSize += length * sizeof(float);
820 } else if (i.second.type() == ETensorType::INT64) {
822 fConstantTensorSize += length * sizeof(int64_t);
823 } else if (i.second.type() == ETensorType::INT32) {
825 fConstantTensorSize += length * sizeof(int32_t);
826 } else if (i.second.type() == ETensorType::BOOL || i.second.type() == ETensorType::UINT8 ) {
828 fConstantTensorSize += length * sizeof(uint8_t);
829 }
830
831
832 } else {
833 // case of tensors which are read from a file
834 if (i.second.type() == ETensorType::FLOAT) {
835 fGC += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
836 fGC += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
837 fWeightsTensorSize += length * sizeof(float);
838 }
839 }
840 }
841}
842
844 if (fIntermediateMemoryInfo.total_stack.empty()) return;
845 fGC += "\n//--- Allocating session memory pool to be used for allocating intermediate tensors\n";
846
847 // char memory block is allocated since char takes 1 byte, thus easier to allocate tensors
848 // of other data types
850 const size_t memPoolSize = totalStack.rbegin()->first + totalStack.rbegin()->second.tensor_size;
851 fGC += "std::vector<char> fIntermediateMemoryPool = std::vector<char>(" + std::to_string(memPoolSize) + ");\n\n";
852}
853
855 if (!fIntermediateTensorInfos.empty()) {
856 std::string tensor_declaration_block = "";
857 for (auto &i : fIntermediateTensorInfos) {
858 bool is_alias = (IsAliasTensor(i.first));
859 if (i.second.type == ETensorType::BOOL && !is_alias) {
860 tensor_declaration_block += "std::vector<std::uint8_t> fTensor_" + i.first + " = std::vector<std::uint8_t>(" + std::to_string(ConvertShapeToLength(i.second.shape)) + ");\n";
861 tensor_declaration_block += "std::uint8_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
862 continue;
863 }
865 bool not_in_freq_map =
868 (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) == fOutputTensorNames.end());
869
871 size_t length = ConvertShapeToLength(i.second.shape);
872
873 if (i.second.type == ETensorType::FLOAT) {
874 tensor_declaration_block += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
875 tensor_declaration_block += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
877 }
878 else if (i.second.type == ETensorType::DOUBLE) {
879 tensor_declaration_block += "std::vector<double> fTensor_" + i.first + " = std::vector<double>(" + std::to_string(length) + ");\n";
880 tensor_declaration_block += "double * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
882 }
883 else if (i.second.type == ETensorType::INT64) {
884 tensor_declaration_block += "std::vector<int64_t> fTensor_" + i.first + " = std::vector<int64_t>(" + std::to_string(length) + ");\n";
885 tensor_declaration_block += "int64_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
887 }
888 }
889 if (is_alias) {
890 tensor_declaration_block += ConvertTypeToString(i.second.type) + " * " + TensorMember(i.first) + " = nullptr;\n";
891 }
892
893 }
894
895 if (tensor_declaration_block.length()) {
896 fGC += "\n//--- declare and allocate the intermediate tensors\n" + tensor_declaration_block;
897 }
898 }
899 // add also the dynamic tensors (only declarations, allocation will be done later)
900 if (!fDynamicTensorInfos.empty()) {
901 fGC += "//--- declare the dynamic tensors\n";
902 for (auto &i : fDynamicTensorInfos) {
903 fGC += ConvertTypeToString(i.second.type) + " * " + TensorMember(i.first) + " = nullptr;\n";
904 }
905 fGC += "//--- dynamic tensors pool\n";
906 fGC += "std::vector<char> fDynamicMemoryPool;\n";
907 }
908}
909
910// generate code for specific operator declarations to be defined in the Session class
912 std::string strcode;
913 for (auto & op : fOperators) {
914 strcode += op->GenerateDeclCode();
915 }
916 if (strcode.empty()) return;
917 fGC += "\n//---- operator declarations \n";
918 fGC += strcode;
919 fGC += "\n";
920}
921
923{
924 // generate code for allocating dynamic tensors using the greedy memory allocations
925 if (fDynamicTensorInfos.empty())
926 return;
927
928 if (fVerbose) {
929 std::cout << "generating code for dynamic tensor management" << std::endl;
931 }
932
933 std::stringstream out;
934 out << "// dynamic tensor memory management\n";
935 out << SP << "std::vector<TMVA::Experimental::SOFIE::TensorLifeInfo> dynamicTensorInfos;\n";
936 out << SP << "dynamicTensorInfos.reserve(" << fDynamicTensorInfos.size() << ");\n";
937
938 // loop on all the operators to find begin/end life of the tensors
939 int op_index = 0;
940 std::vector<std::pair<std::string, ETensorType>> tensors;
941 tensors.reserve(fDynamicTensorInfos.size());
942 for (auto & op : fOperators) {
943 // loop on output tensors -
944 for (auto &it : op->GetOpOutputTensors()) {
945 if (fVerbose) {
946 auto op_ptr = op.get();
947 std::cout << "Looping on operator " << op_index << " " << typeid(*op_ptr).name() << std::endl;
948 }
949 // check if is a dynamic tensor and not an alias tensor or output tensor
950 std::string name = std::string(it);
952 && std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end()) {
954 auto type = GetTensorType(name);
955 size_t type_size = GetTypeSize(type);
956 int begin = op_index;
957 int end = fOperators.size();
958 // look for end
961 end = it_lookup->second + 1; // end is last time used + 1
962 // // some tensors (like xcol in convolutions) are just used within the operators
963 // if (end == 0 && begin > 0) end = begin+1;
964
965 if (begin> end) {
966 std::cout << "op " << op_index << "tensor_" << name << " begin " << begin << " " << " end " << end << std::endl;
967 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo: tensor_" + name + " has end before begin");
968 }
969
970 // write in code
971 out << SP << "dynamicTensorInfos.push_back( {" << begin << ", " << end << ", " << type_size << "* (" << tensor_size << ") });"
972 << " // tensor_" << name << std::endl;
973 tensors.push_back({name,type});
974 }
975 }
976 op_index++; // increment operator index
977 }
978 out << "\n" << SP << "auto memory_result = OrganizeMemory(dynamicTensorInfos);\n\n";
979 out << "// allocating now the memory\n";
980 out << SP << "fDynamicMemoryPool = std::vector<char>(memory_result.total_bytes);\n";
981 out << SP << "int idx = 0;\n";
982 for (auto & it : tensors) {
983 out << SP << "tensor_" << it.first << " = reinterpret_cast<" << ConvertTypeToString(it.second) << " *>(fDynamicMemoryPool.data() + memory_result.offsets[idx++]);\n";
984 }
985 // check that all dynamic tensors are covered
986 bool missingTensor = false;
987 for (auto &i : fDynamicTensorInfos) {
988 if (IsAliasTensor(i.first)) continue;
989 if (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) != fOutputTensorNames.end()) continue;
990 if (std::find(tensors.begin(), tensors.end(), std::pair<std::string,ETensorType>{i.first, i.second.type}) == tensors.end()) {
991 std::cout << "Dynamic tensors " << i.first << " is not in list of operator input/output " << std::endl;
992 missingTensor = true;
993 }
994 }
995 if (missingTensor)
996 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo - some tensors are not in input/output list");
997
998 fGC += out.str();
999}
1000
1001/// Check if a given parameter is used for the shape of an input tensor.
1002bool RModel::IsInputTensorShapeParam(std::string const &paramName) const
1003{
1004 for (auto &name : fInputTensorNames) {
1005 if (IsDimInputTensor(name)) {
1006 auto shape = GetDynamicTensorShape(name);
1007 for (auto &d : shape) {
1008 if (d.param == paramName)
1009 return true;
1010 }
1011 }
1012 }
1013 return false;
1014}
1015
1016/// Collects all identifiers starting with "tensor_" in the input code,
1017/// provided that the occurrence is not immediately preceded by a
1018/// character that is valid in a C++ identifier. Excludes input and output tensor names.
1019/// Returns a deduplicated std::vector<std::string>.
1020std::vector<std::string> RModel::CollectTensorMemberNames(const std::string &input)
1021{
1022 const std::string target = "tensor_";
1023
1024 std::vector<std::string> result;
1025
1026 for (size_t i = 0; i < input.size();) {
1027
1028 bool doCollect = false;
1029
1030 if (i + target.size() <= input.size() && input.compare(i, target.size(), target) == 0 &&
1031 (i == 0 || !IsIdentifierChar(input[i - 1]))) {
1032
1033 doCollect = true;
1034
1035 std::size_t j = i + target.size();
1036
1037 // Extend to full identifier
1038 while (j < input.size() && IsIdentifierChar(input[j]))
1039 ++j;
1040
1041 std::string fullName = input.substr(i, j - i);
1042
1043 // Exclude input tensor names
1044 for (std::string const &name : fInputTensorNames) {
1045 if (fullName == target + name) {
1046 doCollect = false;
1047 break;
1048 }
1049 }
1050
1051 // Exclude output tensor names
1052 if (doCollect) {
1053 for (std::string const &name : fOutputTensorNames) {
1054 if (fullName == target + name) {
1055 doCollect = false;
1056 break;
1057 }
1058 }
1059 }
1060
1061 if (doCollect) {
1062 result.push_back(fullName);
1063 }
1064
1065 i = j; // advance past the identifier
1066 } else {
1067 ++i;
1068 }
1069 }
1070
1071 // Deduplicate (order not preserved)
1072 std::sort(result.begin(), result.end());
1073 result.erase(std::unique(result.begin(), result.end()), result.end());
1074
1075 return result;
1076}
1077
1079 // generate the infer signature given the inputs: eg. "float * tensor1, float * tensor2"
1080 // if (decl = false) generate only calling signature (tensor1,tensor2,....)
1081 std::string rGC;
1082 std::unordered_map<std::string, int> inputParams;
1083 int i_input = 0;
1084 for (auto &name : fInputTensorNames) {
1085 // if is a dynamic tensor pass initial parameters
1086 if (IsDimInputTensor(name)) {
1087 auto shape = GetDynamicTensorShape(name);
1088 for (auto &d : shape) {
1089 std::string pName = d.param;
1090 // need to check if the input parameters is already existing in another input tensor
1091 if (d.isParam && inputParams.count(pName) == 0) {
1092 if (isdecl) rGC += "size_t ";
1093 rGC += d.param + ",";
1095 }
1096 }
1097 }
1098 if (isdecl) {
1100 if (type == "other")
1101 throw std::runtime_error("TMVA-SOFIE: input tensor " + name +
1102 " is of a data type which is not yet supported.");
1103 rGC += type + " const* ";
1104 }
1105 rGC += "tensor_" + name + ",";
1106 i_input++;
1107 }
1108
1109 if (fInputTensorNames.size() > 0) rGC.pop_back();// remove last ","
1110 return rGC;
1111}
1112
1113namespace {
1114
1115std::string typeForOutput(ETensorType t) {
1116 // The std::vector<bool> is a special type that is not wrapping continuous memory.
1117 // We don't want to use it as a return type.
1119 return ConvertTypeToString(t);
1120}
1121
1122std::string memberNameForDimShape(std::string name)
1123{
1124 if (!name.empty()) {
1125 name[0] = std::toupper(static_cast<unsigned char>(name[0]));
1126 }
1127 name = "f" + name;
1128 return name;
1129}
1130
1131}
1132
1134{
1135 size_t outputSize = fOutputTensorNames.size();
1136 // assume output types are all the same
1137
1138 bool sameOutputTypes = true;
1139 std::string inferReturnType; // type return by infer function
1141 fGC += "\n\n";
1142 if (outputSize == 1) {
1143 fGC += "std::vector<" + typeForOutput(eFirstOutputType) + ">";
1144 } else {
1145 // if all output types are the same we return an std::vector - otherwise a tuple
1146 for (std::string const &name : fOutputTensorNames) {
1148 sameOutputTypes = false;
1149 }
1150 if (sameOutputTypes)
1151 fGC += "std::vector<std::vector<" + typeForOutput(eFirstOutputType) + ">>";
1152 else {
1153 inferReturnType = "std::tuple<";
1154 for (size_t i = 0; i < outputSize; i++) {
1155 inferReturnType += "std::vector<" + typeForOutput(GetTensorType(fOutputTensorNames[i])) + ">";
1156 if (i < outputSize - 1)
1157 inferReturnType += ",";
1158 }
1159 inferReturnType += ">";
1161 }
1162 }
1163
1164 fGC += " infer(" + GenerateInferSignature() + "){\n";
1165
1166 std::string doInferArgs = GenerateInferSignature(false);
1167 if (!doInferArgs.empty())
1168 doInferArgs += ",";
1169 for (std::string const &name : fOutputTensorNames) {
1170 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1171 std::string n;
1172 if(!isDynamic) {
1173 n = std::to_string(ConvertShapeToLength(GetTensorShape(name)));
1174 } else {
1176 // Use the session member (fXxx) when any dim is a runtime-computed identifier
1177 // (e.g. NonZero count). For expression-type dims derived from input shapes
1178 // (e.g. "((W+-3)/2+1)"), use the expression directly.
1179 // for input shape parameters we don't need to use the session member since it is passed as argument to the infer function and it is not a runtime computed value
1180 bool hasRuntimeParam = false;
1181 for (auto const &dim : GetDynamicTensorShape(name)) {
1182 if (dim.isParam && IsIdentifier(dim.param) && !IsInputTensorShapeParam(dim.param))
1183 hasRuntimeParam = true;
1184 }
1186 }
1187 std::string outputName = "output_tensor_" + name;
1188 fGC += SP + "std::vector<" + typeForOutput(GetTensorType(name)) + " > " + outputName + "(" + n + ");\n";
1189 doInferArgs += " " + outputName + ".data(),";
1190 if(isDynamic) {
1191 for (auto const &dim : GetDynamicTensorShape(name)) {
1192 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param)) {
1193 fGC += SP + "size_t " + dim.param + " = 0;\n";
1194 doInferArgs += " " + dim.param + ",";
1195 }
1196 }
1197 }
1198 }
1199 if (!doInferArgs.empty())
1200 doInferArgs.back() = ' ';
1201
1202 // verifying if the dynamic parameters are within allowed range
1203 std::unordered_set<std::string> input_params_checked;
1204 std::string dynamic_parameters_check = "";
1205 for (auto &name : fInputTensorNames) {
1206 if (IsDimInputTensor(name)) {
1207 auto shape = GetDynamicTensorShape(name);
1208 for (auto &d : shape) {
1209 std::string pName = d.param;
1210 if (d.isParam && input_params_checked.count(pName) == 0) {
1211 std::string memberName = memberNameForDimShape(d.param);
1212 dynamic_parameters_check += d.param + " > " + memberName + " || ";
1214 fGC += SP + "if (" + d.param + " > " + memberName + ") {\n";
1215 fGC += SP + SP + "throw std::runtime_error(\"TMVA-SOFIE: dynamic input tensor shape parameter " +
1216 d.param + " exceeds the initialized maximum allowed shape.\");\n";
1217 fGC += SP + "}\n";
1218 }
1219 }
1220 }
1221 }
1222
1223 if (fUseSession) {
1224 fGC += SP + "doInfer(*this, " + doInferArgs + ");\n";
1225 } else {
1226 fGC += SP + "doInfer(" + doInferArgs + ");\n";
1227 }
1228
1229 // If the output tensors have dynamic sizes, now is the time to set them
1230 for (std::string const &name : fOutputTensorNames) {
1231 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1232 if (isDynamic) {
1233 std::string outputName = "output_tensor_" + name;
1234 auto tensor_size = ConvertDimShapeToLength(GetDimTensorShape(name));
1235 fGC += SP + outputName + ".resize(" + tensor_size + ");\n";
1236 }
1237 }
1238
1239 fGC += SP + "return {";
1240 for (size_t i = 0; i < fOutputTensorNames.size(); i++) {
1241 fGC += "output_tensor_" + fOutputTensorNames[i];
1242 if (i < fOutputTensorNames.size() - 1)
1243 fGC += ",";
1244 }
1245 fGC += "};\n";
1246 fGC += "}\n"; // end of infer function scope
1247}
1248
1250{
1251 std::string sessionName = !fIsSubGraph ? "Session" : "Session_" + fName;
1252
1253 if (fUseSession && !fIsGNNComponent) {
1254 // forward declare session struct
1255 fGC += "struct " + sessionName + ";\n";
1256 }
1257
1258 // Determine the signature of the actual inference function
1260 if (!doInferSignature.empty())
1261 doInferSignature += ", ";
1262 for (auto const &name : fOutputTensorNames) {
1263 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1264 doInferSignature += typeForOutput(GetTensorType(name)) + " *tensor_" + name + ",";
1265 if(isDynamic) {
1266 for (auto const &dim : GetDynamicTensorShape(name)) {
1267 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param))
1268 doInferSignature += " size_t &" + dim.param + "_output,";
1269 }
1270 }
1271 }
1272 doInferSignature.back() = ' ';
1273
1274 if (fUseSession) {
1275 doInferSignature = sessionName + " const &session, " + doInferSignature;
1276 }
1277
1278 doInferSignature = "inline void doInfer(" + doInferSignature + ")";
1279
1280 if (!fIsGNNComponent) {
1281 // forward declare inference implementation
1282 fGC += doInferSignature + ";\n";
1283 }
1284
1285 // define the Session struct (for GNN this is generated in RModel_GNN)
1286 if (fUseSession && !fIsGNNComponent) {
1287 fGC += "struct " + sessionName + " {\n";
1288 }
1289
1290 // generate code for declaring the initialized tensors
1292
1294 // evaluate total intermediate memory and position intermediate tensor addresses
1295 std::string intermediate_memory_alloc_string = "";
1296 intermediate_memory_alloc_string += "\n// --- Positioning intermediate tensor memory --";
1297 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1298 if (fVerbose) {
1299 auto op = fOperators[op_idx].get();
1300 std::cout << "\n******************\n analyzing input/output operator " << op_idx << " "
1301 << typeid(*op).name() << std::endl;
1302 }
1305 }
1306
1307 // to check remaining unused fragments after memory allocation (lesser the better)
1308 // for (const auto &it: fIntermediateMemoryInfo.available_stack){
1309 // std::cout<<"chunk_idx: "<<it.first<<", chunk_size: "<<it.second<<"\n";
1310 // }
1311
1312 // generate the memory pool to be used by intermediate tensors
1314
1315 // position intermediate tensors
1317 }
1318
1319 // generate the declaring the intermediate tensors
1321 // generate code for declarations of some specific operators
1323
1324 // storing the parameters for future checking to avoid mismatches
1325 if (!fDimShapeNames.empty()) {
1326 fGC += "\n// dynamic shape parameters\n";
1328 std::sort(dimShapeNames.begin(), dimShapeNames.end());
1329 for (const auto &p : dimShapeNames) {
1330 fGC += "size_t " + memberNameForDimShape(p) + ";\n";
1331 }
1332 }
1333
1334 // add subgraph session
1335 if (!fSubGraphs.empty()) fGC += "// subgraph sessions\n";
1336 for (auto & graph : fSubGraphs) {
1337 fGC += "Session_" + graph->fName + " fSession_" + graph->fName + ";\n";
1338 }
1339
1340 // Generate code for Session constructor
1341 if (fUseSession) {
1342 // add here specific operator code that needs to define session data members
1343 fGC += "\n";
1344 for (size_t id = 0; id < fOperators.size(); id++) {
1345 std::string opName = std::to_string(id);
1346 fGC += fOperators[id]->GenerateSessionMembersCode(opName);
1347 }
1348 fGC += "\n";
1349 // here add initialization and reading of weight tensors
1350 if (fUseWeightFile) {
1351 std::string fileName = fName;
1353 fileName += ".dat";
1354 }
1356 fileName += ".root";
1357 }
1358 fGC += sessionName + "(std::string filename =\"" + fileName + "\"";
1359 } else {
1360 // no need to pass weight file since it is not used
1361 // keep passing a string for compatibility
1362 fGC += sessionName + "(std::string = \"\"";
1363 }
1364 // add initialization of shape parameters
1365 // assume all parameters are of type size_t
1366 if (!fDimShapeNames.empty()) {
1367 // need to use same order as in infer function not alphabetical one
1368 for (auto &p : fDimShapeNames) {
1369 fGC += ",\n";
1370 fGC += " size_t " + p + " = " + fShapeParams[p];
1371 }
1372 }
1373 fGC += ") {\n";
1374
1375 // initializing dynamic parameters
1376 if (!fDimShapeNames.empty()) {
1377 fGC += "\n\n";
1378 std::sort(fDimShapeNames.begin(), fDimShapeNames.end());
1379 for (const auto &p : fDimShapeNames) {
1380 fGC += " " + memberNameForDimShape(p) + " = " + p + ";\n";
1381 }
1382 }
1383 // add some extra code needed for initialization of dynamic parameters
1385
1386 if (fUseWeightFile) {
1387 fGC += "\n//--- reading weights from file\n";
1389 fGC += "\n";
1390 // fUseWeightFile = fUseWeightFile;
1391 }
1392
1393 // now we have passed the parameters we can allocate the dynamic tensors
1395
1396 // add here initialization code for operator
1397 for (size_t id = 0; id < fOperators.size(); id++) {
1398 fGC += fOperators[id]->GenerateInitCode();
1399 }
1400
1401 fGC += "}\n\n";
1402 }
1403
1404 // generate the inference overload that returns an output struct
1406
1407 // end of session
1408 if (fUseSession && !fIsGNNComponent) {
1409 fGC += "}; // end of Session\n\n";
1410
1412 }
1413
1414 fGC += doInferSignature + " {\n";
1415 fGC += "\n";
1416
1417 // generate the inference code
1418 if (fVerbose)
1419 std::cout << "Generating main inference code for " << fName << std::endl;
1420
1421 if (fOutputTensorNames.size() == 0)
1422 throw std::runtime_error("TMVA-SOFIE: output size=0 are not supported");
1423
1424 std::string allOperatorCode;
1425
1426 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1427 if (fVerbose)
1428 std::cout << "Generating code for operator .... " << op_idx << std::endl;
1429 std::string operatorCode = fOperators[op_idx]->Generate(std::to_string(op_idx));
1431 }
1432
1433 // If the generated code users members of the session struct, use the
1434 // local variable name that we're using for the session:
1435 ReplaceAll(allOperatorCode, "this->", "session.");
1436
1437 if (fUseSession && !fIsGNNComponent) {
1438 // Collect all "tensor_*" data members that are not input or output tensors
1440 for (auto const& name: tensorMemberNames) {
1441 fGC += " auto &" + name + " = session." + name + ";\n";
1442 }
1443 fGC += "\n";
1444 }
1445
1447
1448 for (auto const& name: fOutputTensorNames) {
1449 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1450 if(isDynamic) {
1451 for (auto const &dim : GetDynamicTensorShape(name)) {
1452 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param))
1453 fGC += " " + dim.param + "_output = " + dim.param + ";\n";
1454 }
1455 }
1456 if(IsConstantTensor(name)) {
1457 std::string t = "session.tensor_" + name;
1459 fGC += " std::copy(" + t + ", " + t + " + " + std::to_string(length) + ", tensor_" + name + ");\n";
1460 }
1461 }
1462 fGC += "\n";
1463
1464 fGC += "}\n";
1465}
1466
1467void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, long pos, bool verbose)
1468{
1469 fVerbose = verbose;
1470 fBatchSize = batchSize;
1471 fReadPos = pos;
1472
1473 // session flag is used in operator initialize
1474 if (static_cast<std::underlying_type_t<Options>>(Options::kNoSession) & options) {
1475 fUseSession = false;
1477 }
1478 if (static_cast<std::underlying_type_t<Options>>(Options::kNoWeightFile) & options) {
1479 fUseWeightFile = false;
1481 }
1482 if (static_cast<std::underlying_type_t<Options>>(Options::kRootBinaryWeightFile) & options) {
1483 fUseWeightFile = true;
1485 }
1486 if (fUseWeightFile && !fUseSession) {
1487 throw std::runtime_error(
1488 "TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
1489 }
1490
1491 if (static_cast<std::underlying_type_t<Options>>(Options::kGNN) & options)
1492 fIsGNN = true;
1493 if (static_cast<std::underlying_type_t<Options>>(Options::kGNNComponent) & options)
1494 fIsGNNComponent = true;
1495
1496 // initialize the model including all operators and sub-graphs
1497 Initialize(batchSize, verbose);
1498
1499 // if having dynamic tensor we need to have a Session
1500 if (!fDynamicTensorInfos.empty()) {
1501 fUseSession = true;
1502 if (verbose)
1503 std::cout << "Warning: Force having a Session since model has dynamic tensors " << std::endl;
1504 }
1505
1506 std::string hgname;
1507 if (!fIsGNNComponent && !fIsSubGraph) {
1508 fGC.clear();
1510 }
1511
1512 // generate first code for the subgraphs
1513 for (auto &graph : fSubGraphs) {
1514 if (fVerbose)
1515 std::cout << "generate session code for subgraph " << graph->fName << std::endl;
1516 graph->GenerateSessionCode();
1517 fGC += graph->fGC;
1518 }
1519
1520 if (fVerbose)
1521 std::cout << "generate Main session code - model " << fName << std::endl;
1522
1523 // generate main session code
1525
1526 if (!fIsGNNComponent && !fIsSubGraph) {
1527 fGC += ("} //TMVA_SOFIE_" + fName + "\n");
1528 fGC += "\n#endif // " + hgname + "\n";
1529 }
1530}
1531
1533 // generate the code to read initialized tensors from a text data file
1535 // check if there are tensors to write
1536
1537 if (!fUseWeightFile) return;
1538
1539 fGC += " std::ifstream f;\n";
1540 fGC += " f.open(filename);\n";
1541 fGC += " if (!f.is_open()) {\n";
1542 fGC += " throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input weights\");\n";
1543 fGC += " }\n";
1544
1545 if(fIsGNNComponent) {
1546 fGC += " f.seekg(" + std::to_string(pos) + ");\n";
1547 }
1548
1549 fGC += " using TMVA::Experimental::SOFIE::ReadTensorFromStream;\n";
1550
1551 // loop on tensors and parse the file
1552 for (auto& i: fInitializedTensors) {
1553 // skip Constant and shape tensors (not written in a file)
1554 if (!i.second.IsWeightTensor()) continue;
1555 std::string tensor_name = "tensor_" + i.first;
1556 if (i.second.type() == ETensorType::FLOAT) {
1557 std::string length = std::to_string(ConvertShapeToLength(i.second.shape()));
1558 fGC += " ReadTensorFromStream(f, " + tensor_name + ", \"" + tensor_name + "\", " + length + ");\n";
1559 } else {
1560 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be read from a file");
1561 }
1562 }
1563 fGC += " f.close();\n";
1564 }
1565
1566 // generate the code to read initialized tensors from a ROOT data file
1568#ifdef SOFIE_SUPPORT_ROOT_BINARY
1569 fGC += " {\n";
1570 fGC += " std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
1571 fGC += " if (!rootFile->IsOpen()) {\n";
1572 fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
1573 fGC += " }\n";
1574
1575 std::string dirName = fName + "_weights";
1576 fGC += " if (!rootFile->GetKey(\"" + dirName + "\")) {\n";
1577 fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
1578 fGC += " }\n";
1579
1580 for (auto &i : fInitializedTensors) {
1581 // skip Constant and shape tensors
1582 if (!i.second.IsWeightTensor()) continue;
1583 fGC += " {\n";
1584 std::string tensor_name = "tensor_" + i.first;
1585 if (i.second.type() == ETensorType::FLOAT) {
1586 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
1587 fGC += dirName + "/" + tensor_name + "\"));\n";
1588 } else if (i.second.type() == ETensorType::DOUBLE) {
1589 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
1590 fGC += dirName + + "/" + tensor_name + "\"));\n";
1591 } else if (i.second.type() == ETensorType::INT64) {
1592 fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
1593 fGC += dirName + "/" + tensor_name + "\"));\n";
1594 } else {
1595 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be read from a ROOT file");
1596 }
1597 fGC += " }\n";
1598 }
1599 fGC += " }\n";
1600#else
1601 throw std::runtime_error("SOFIE was not built with ROOT file support.");
1602#endif // SOFIE_SUPPORT_ROOT_BINARY
1603 }
1604}
1605
1607 // Determine the file extension based on the weight file type
1608 std::string fileExtension;
1609 switch (fWeightFile) {
1611 fileExtension = ".dat";
1612 break;
1614 fileExtension = ".root";
1615 break;
1617 fileExtension = ".dat";
1618 break;
1619 }
1620
1621 // If filename is empty, use the model name as the base filename
1622 if (filename.empty()) {
1624 }
1625
1626 // Write the initialized tensors to the file
1628#ifdef SOFIE_SUPPORT_ROOT_BINARY
1629 if(fIsGNNComponent || fIsGNN) {
1630 throw std::runtime_error("SOFIE-GNN yet not supports writing to a ROOT file.");
1631 }
1632 std::unique_ptr<TFile> outputFile(TFile::Open(filename.c_str(), "UPDATE"));
1633
1634 std::string dirName = fName + "_weights";
1635 // check if directory exists, in case delete to replace with new one
1636 if (outputFile->GetKey(dirName.c_str()))
1637 outputFile->rmdir(dirName.c_str());
1638
1639 auto outputDir = outputFile->mkdir(dirName.c_str());
1640
1641 for (const auto& item : fInitializedTensors) {
1642 // skip Constant tensors and tensors which are not writable (e.g. shape tensors)
1643 if (!item.second.IsWeightTensor()) continue;
1644 std::string tensorName = "tensor_" + item.first;
1645 size_t length = 1;
1646 length = ConvertShapeToLength(item.second.shape());
1647 if(item.second.type() == ETensorType::FLOAT) {
1648 const float* data = item.second.data<float>();
1649 std::vector<float> tensorDataVector(data, data + length);
1650 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<float>", tensorName.c_str());
1651 }
1652 else if(item.second.type() == ETensorType::DOUBLE) {
1653 const double* data = item.second.data<double>();
1654 std::vector<double> tensorDataVector(data, data + length);
1655 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<double>", tensorName.c_str());
1656 }
1657 else if(item.second.type() == ETensorType::INT64) {
1658 const int64_t* data = item.second.data<int64_t>();
1659 std::vector<int64_t> tensorDataVector(data, data + length);
1660 outputDir->WriteObjectAny(&tensorDataVector, "std::vector<int64_t>", tensorName.c_str());
1661 }
1662 else {
1663 throw std::runtime_error("tmva-sofie tensor " + tensorName + " with type " + ConvertTypeToString(item.second.type()) +
1664 " cannot be written to a ROOT file");
1665 }
1666 }
1667 outputFile->Write(filename.c_str());
1668
1669 // this needs to be changed, similar to the text file
1670 return -1;
1671
1672#else
1673 throw std::runtime_error("SOFIE was not built with ROOT file support.");
1674#endif // SOFIE_SUPPORT_ROOT_BINARY
1675 } else if (fWeightFile == WeightFileType::Text) {
1676 std::ofstream f;
1677 if(fIsGNNComponent) {
1678 // appending all GNN components into the same file
1679 f.open(filename, std::ios::app);
1680 } else {
1681 f.open(filename);
1682 }
1683 if (!f.is_open())
1684 throw
1685 std::runtime_error("tmva-sofie failed to open file " + filename + " for tensor weight data");
1686 for (auto& i: fInitializedTensors) {
1687 // skip Constant tensors and not writable tensors (e.g. shape tensors)
1688 if (!i.second.IsWeightTensor()) {
1689 continue;
1690 }
1691 size_t length = ConvertShapeToLength(i.second.shape());
1692 std::string tensor_name = "tensor_" + i.first;
1693 f << tensor_name << " " << length << "\n";
1694 if (i.second.type() == ETensorType::FLOAT) {
1695 const float * data = i.second.data<float>();
1696 for (size_t idx = 0; idx < length; idx++) {
1697 // round to zero sub-normal values
1698 float value = data[idx];
1699 if (value != 0. && std::abs(value) < std::numeric_limits<float>::min() ) value = 0;
1700 // handle non-finite values explicitly
1701 if (std::isinf(value))
1702 f << (value > 0 ? "inf" : "-inf");
1703 else if (std::isnan(value))
1704 f << "nan";
1705 else
1706 f << std::setprecision(std::numeric_limits<float>::max_digits10) << value;
1707 f << ( (idx < length-1) ? " " : "\n" );
1708 }
1709 }
1710 else {
1711 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be written to a file");
1712 }
1713 if (f.fail())
1714 throw std::runtime_error("tmva-sofie failed to write tensor data to file for " + tensor_name);
1715 }
1716 long curr_pos = f.tellp();
1717 f.close();
1718 return curr_pos;
1719 } else {
1720 return -1;
1721 }
1722}
1723
1725 std::cout << "Summary of model " << GetName() << std::endl;
1726 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
1727 auto& r = *fOperators[op_idx].get();
1728 std::string raw_name = typeid(r).name();
1729 // look for ROperator_NAME
1730 std::string name = raw_name.substr(raw_name.find("ROperator_")+10, raw_name.size());
1731 std::cout << op_idx << " " << name << " : ";
1732 for (auto & t_in : r.GetOpInputTensors()) std::cout << t_in << " ";
1733 std::cout << " ----> ";
1734 for (auto & t_out : r.GetOpOutputTensors()) std::cout << t_out << " ";
1735 std::cout << std::endl;
1736 }
1737}
1738
1739/// To emit the dimensions of the input tensors as a data member of a session,
1740/// which is helpful when validating the inference inputs.
1742{
1743 fGC += "\n// Input tensor dimensions\n";
1744 fGC += "using TMVA::Experimental::SOFIE::SingleDim;\n";
1745 fGC += "using TMVA::Experimental::SOFIE::TensorDims;\n";
1746 fGC += "using TMVA::Experimental::SOFIE::makeDims;\n\n";
1747 bool hasDynamicInputTensors = false;
1748
1749 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1750 auto const &name = fInputTensorNames[iInput];
1751 if (IsDimInputTensor(name)) {
1753 }
1754 std::vector<Dim> shape = GetDimTensorShape(name);
1755 fGC += "constexpr std::array<SingleDim, " + std::to_string(shape.size()) + "> dim_" + name + "{";
1756 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1757 auto const &dim = shape[iDim];
1758 if (dim.isParam) {
1759 fGC += "SingleDim{\"" + dim.GetVal() + "\"}";
1760 } else {
1761 fGC += "SingleDim{" + dim.GetVal() + "}";
1762 }
1763 if (iDim != shape.size() - 1) {
1764 fGC += ", ";
1765 }
1766 }
1767 fGC += "};\n";
1768 }
1769 fGC += "\nconstexpr std::array<TensorDims, " + std::to_string(fInputTensorNames.size()) + "> inputTensorDims{\n";
1770 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1771 auto const &name = fInputTensorNames[iInput];
1772 fGC += SP + "makeDims(dim_" + name + ")";
1773 if (iInput == fInputTensorNames.size() - 1) {
1774 fGC += "\n";
1775 } else {
1776 fGC += ",\n";
1777 }
1778 }
1779 fGC += "};\n";
1780
1781 fGC +=
1782 "\nconstexpr bool hasDynamicInputTensors{" + std::string{hasDynamicInputTensors ? "true" : "false"} + "};\n\n";
1783
1784 fGC += "\n// Output tensor dimensions\n";
1785 bool hasDynamicOutputTensors = false;
1786 for (std::size_t iOutput = 0; iOutput < fOutputTensorNames.size(); ++iOutput) {
1787 auto const &name = fOutputTensorNames[iOutput];
1788 if (IsDynamicTensor(name)) {
1790 }
1791 std::vector<Dim> shape = GetDimTensorShape(name);
1792 fGC += "constexpr std::array<SingleDim, " + std::to_string(shape.size()) + "> dim_" + name + "{";
1793 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1794 auto const &dim = shape[iDim];
1795 if (dim.isParam) {
1796 fGC += "SingleDim{\"" + dim.GetVal() + "\"}";
1797 } else {
1798 fGC += "SingleDim{" + dim.GetVal() + "}";
1799 }
1800 if (iDim != shape.size() - 1) {
1801 fGC += ", ";
1802 }
1803 }
1804 fGC += "};\n";
1805 }
1806 fGC += "\nconstexpr std::array<TensorDims, " + std::to_string(fOutputTensorNames.size()) + "> outputTensorDims{\n";
1807 for (std::size_t iOutput = 0; iOutput < fOutputTensorNames.size(); ++iOutput) {
1808 auto const &name = fOutputTensorNames[iOutput];
1809 fGC += SP + "makeDims(dim_" + name + ")";
1810 if (iOutput == fOutputTensorNames.size() - 1) {
1811 fGC += "\n";
1812 } else {
1813 fGC += ",\n";
1814 }
1815 }
1816 fGC += "};\n";
1817 fGC +=
1818 "\nconstexpr bool hasDynamicOutputTensors{" + std::string{hasDynamicOutputTensors ? "true" : "false"} + "};\n\n";
1819}
1820
1822 std::cout << "Model requires following inputs:\n";
1823 for (auto& inputInfo: fInputTensorInfos) {
1824 std::cout << "Parametrised Tensor name: " << inputInfo.first << "\t";
1825 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1826 std::cout << "shape: [";
1827 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1828 if (inputInfo.second.shape[i].isParam) {
1829 std::cout << inputInfo.second.shape[i].param;
1830 } else {
1831 std::cout << inputInfo.second.shape[i].dim ;
1832 }
1833 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1834 }
1835 std::cout << "]" << std::endl;
1836 }
1837
1838 for (auto& inputInfo: fReadyInputTensorInfos) {
1839 std::cout << "Fully Specified Tensor name: " << inputInfo.first << "\t";
1840 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1841 std::cout << "shape: [";
1842 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1843 std::cout << inputInfo.second.shape[i];
1844 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1845 }
1846 std::cout << "]" << std::endl;
1847 }
1848 std::cout << "\n";
1849}
1850
1852 std::cout << "Model initialized the following tensors:\n";
1853 for (auto& it: fInitializedTensors) {
1854 std::cout << "Tensor name: \"" << it.first << "\"\t";
1855 std::cout << "type: " << ConvertTypeToString(it.second.type()) << "\t";
1856 std::cout << "shape: [";
1857 for (size_t i = 0; i < it.second.shape().size(); i++) {
1858 std::cout << it.second.shape()[i];
1859 if (i < it.second.shape().size() - 1) std::cout << ",";
1860 }
1861 std::cout << "]";
1862 if (it.second.IsConstantTensor()) std::cout << " (Constant)";
1863 if (it.second.IsNotWritable()) std::cout << " (Not Writable)";
1864 std::cout << std::endl;
1865 }
1866 std::cout << "\n";
1867}
1868
1870 std::cout << "Model specify the following intermediate tensors:\n";
1871 for (auto& it: fIntermediateTensorInfos) {
1872 std::cout << "Tensor name: \"" << it.first << "\"\t";
1873 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
1874 std::cout << "shape: [";
1875 for (size_t i = 0; i < it.second.shape.size(); i++) {
1876 std::cout << it.second.shape[i];
1877 if (i < it.second.shape.size() - 1) std::cout << ",";
1878 }
1879 std::cout << "]" << std::endl;
1880 }
1881 std::cout << "\n";
1882}
1883
1885 std::cout << "Model specify the following dynamic tensors:\n";
1886 for (auto& it: fDynamicTensorInfos) {
1887 std::cout << "Tensor name: \"" << it.first << "\"\t";
1888 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
1889 std::cout << "shape: [";
1890 for (size_t i = 0; i < it.second.shape.size(); i++) {
1891 std::cout << it.second.shape[i].GetVal();
1892 if (i < it.second.shape.size() - 1) std::cout << ",";
1893 }
1894 std::cout << "]" << std::endl;
1895 }
1896 std::cout << "\n";
1897}
1898
1900 std::cout << "Model specify the following output tensors:\n";
1901 for (auto& it: fOutputTensorNames) {
1902 std::cout << "Tensor name: \"" << it << "\"\t";
1903 try {
1904 auto shape = GetDimTensorShape(it);
1905 std::cout << "with shape: " << ConvertDimShapeToString(shape) << std::endl;
1906 } catch (...) {
1907 std::cout << "with shape not yet defined" << std::endl;
1908 }
1909 }
1910 std::cout << "\n";
1911}
1912
1914 auto it = fInitializedTensors.find(name);
1915 if (it == fInitializedTensors.end()) {
1916 std::cout << "Tensor " << name << " not found in model's initialized tensor list" << std::endl;
1917 return;
1918 }
1919
1920 std::cout << "Tensor name: " << it->first << "\t";
1921 std::cout << "type: " << ConvertTypeToString(it->second.type()) << "\t";
1922 int length =1;
1923 std::cout << "shape: [";
1924 for (size_t i = 0; i < it->second.shape().size(); i++) {
1925 std::cout << it->second.shape()[i];
1926 length *= it->second.shape()[i];
1927 if (i < it->second.shape().size() - 1) std::cout << ",";
1928 }
1929 std::cout << "]" << std::endl;
1930 bool ellipsis = true;
1931 if (n_print > length) {
1932 n_print = length;
1933 ellipsis = false;
1934 }
1935
1936 std::cout << "data: [" << std::endl;
1937 if (it->second.type() == ETensorType::FLOAT) {
1938 auto converted_data = it->second.data<float>();
1939 for (int i =0; i < n_print; i++) {
1940 std::cout << converted_data[i];
1941 if (i < n_print - 1) std::cout << " ,";
1942 }
1943 }
1944 if (ellipsis) std::cout << ", ...";
1945 std::cout << "]" << std::endl;
1946
1947}
1948
1949void RModel::OutputGenerated(std::string filename, bool append) {
1950
1952
1953 // write weights in a text file
1954 if (fUseWeightFile) {
1955 if (!filename.empty()) {
1956 size_t pos = filename.find(".hxx");
1958 filename.replace(pos, 4, ".dat");
1960 filename = filename.erase(pos, 4);
1961 filename += ".root";
1962 }
1963 } else {
1964 filename = fName;
1965 filename += fWeightFile == WeightFileType::Text ? ".dat" : ".root";
1966 }
1968 }
1969}
1970
1971void RModel::Streamer(TBuffer &R__b) {
1972 if (R__b.IsReading()) {
1973 RModel::Class()->ReadBuffer(R__b, this);
1974 for (auto & i : fInitializedTensors) {
1975 i.second.CastPersistentToShared();
1976 }
1977 }
1978 else {
1979 for (auto & i : fInitializedTensors) {
1980 i.second.CastSharedToPersistent();
1981 }
1982 RModel::Class()->WriteBuffer(R__b, this);
1983 }
1984}
1985
1986} // 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:335
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:286
void AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name)
Definition RModel.cxx:250
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:301
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:1078
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:1741
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:1949
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:343
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:232
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:318
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:222
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:291
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:260
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:273
bool IsAliasTensor(const std::string &name) const
check if a tensor is a alias tensor
Definition RModel.cxx:264
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
Definition RModel.cxx:488
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:1913
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:277
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:567
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1606
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:1467
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:1020
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:366
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:383
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:725
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:33
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:375
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:357
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:268
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:295
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:350
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:242
bool IsInputTensorShapeParam(std::string const &name) const
Check if a given parameter is used for the shape of an input tensor.
Definition RModel.cxx:1002
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:757
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