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