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 <cstdint>
5#include <cstdio>
6#include <memory>
7#include <string>
8#include <cstdlib>
9
10#include "TMVA/RModel.hxx"
11#include "TMVA/ROperator.hxx"
12
14
15// Out-of-line because ROperator is forward-declared in the public header.
17{
18 std::default_delete<ROperator>()(ptr);
19}
20
21RModel::~RModel() = default;
22RModel::RModel(RModel &&) = default;
23RModel &RModel::operator=(RModel &&) = default;
24
25namespace {
26
27const std::string SP = " ";
28
29void ReplaceAll(std::string &str, const std::string &from, const std::string &to)
30{
31 size_t pos = 0;
32 while ((pos = str.find(from, pos)) != std::string::npos) {
33 str.replace(pos, from.length(), to);
34 pos += to.length();
35 }
36}
37
38bool IsIdentifierChar(char c)
39{
40 return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
41}
42
43// Returns true if s is a valid C++ identifier (can be used as a variable name).
44// Dim::param can be either a plain name (e.g. "W") or a computed expression
45// (e.g. "((W+-3)/2+1)"); only the former can be used as a C++ variable name.
46bool IsIdentifier(const std::string &s)
47{
48 if (s.empty() || std::isdigit(static_cast<unsigned char>(s[0])))
49 return false;
50 for (char c : s)
53 return true;
54}
55
56// Get the data member name corresponding to a tensor with a given name.
57std::string TensorMember(std::string const &name)
58{
59 return "tensor_" + name;
60}
61
62// Safetensors dtype token (https://huggingface.co/docs/safetensors) for a
63// tensor type.
64std::string SafetensorsDType(ETensorType type)
65{
66 switch (type) {
67 case ETensorType::FLOAT: return "F32";
68 case ETensorType::DOUBLE: return "F64";
69 case ETensorType::INT64: return "I64";
70 default:
71 throw std::runtime_error("tmva-sofie tensor with type " + ConvertTypeToString(type) +
72 " cannot be written to a safetensors file");
73 }
74}
75
76// Escape a string for inclusion in the safetensors JSON header. Only what
77// JSON requires: quotation marks, backslashes and control characters.
78std::string JsonEscape(const std::string &s)
79{
80 std::string out;
81 out.reserve(s.size() + 2);
82 for (const char c : s) {
83 switch (c) {
84 case '"': out += "\\\""; break;
85 case '\\': out += "\\\\"; break;
86 default:
87 if (static_cast<unsigned char>(c) < 0x20) {
88 char buf[8];
89 std::snprintf(buf, sizeof(buf), "\\u%04x", c);
90 out += buf;
91 } else {
92 out.push_back(c);
93 }
94 }
95 }
96 return out;
97}
98
99} // namespace
100
101std::underlying_type_t<Options> operator|(Options opA, Options opB) {
102 return static_cast<std::underlying_type_t<Options>>(opA) | static_cast<std::underlying_type_t<Options>>(opB);
103}
104std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB) {
105 return opA | static_cast<std::underlying_type_t<Options>>(opB);
106}
107
108std::vector<size_t> RModel::GetTensorShape(const std::string & name) const {
109 auto f = fReadyInputTensorInfos.find(name);
110 if (f != fReadyInputTensorInfos.end()) {
111 return f->second.shape;
112 }
113 auto f2 = fInitializedTensors.find(name);
114 if (f2 != fInitializedTensors.end()) {
115 return f2->second.shape();
116 }
117 auto f3 = fInputTensorInfos.find(name);
118 if (f3 != fInputTensorInfos.end()) {
119 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] is an input tensor with unspecified dimension parameter");
120 }
121 auto f4 = fIntermediateTensorInfos.find(name);
122 if (f4 != fIntermediateTensorInfos.end()) {
123 return f4->second.shape;
124 }
125 // case of shape tensors
126 auto f5 = fShapeTensors.find(name);
127 if (f5 != fShapeTensors.end()) {
128 // shape is vector of size 1 with size of shape values or just a scalar
129 if (f5->second.second) // check scalar flag
130 return std::vector<size_t>{};
131 else
132 return std::vector<size_t>{f5->second.first.size()};
133 }
134
136 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] is a dynamic tensor. Use GetDynamicTensorShape instead of GetTensorShape");
137
140
141 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not found");
142}
143
144std::vector<Dim> RModel::GetDimTensorShape(const std::string & name) const {
145 if (auto f = fDynamicTensorInfos.find(name); f != fDynamicTensorInfos.end()) {
146 return f->second.shape;
147 }
148 if (auto f = fInputTensorInfos.find(name); f != fInputTensorInfos.end()) {
149 return f->second.shape;
150 }
151 // in case is not a dynamic tensor convert normal shape to Dim one
152 // for this we need to return the vector by value
154}
155std::vector<Dim> RModel::GetDynamicTensorShape(const std::string & name) const {
156 if (auto f = fDynamicTensorInfos.find(name); f != fDynamicTensorInfos.end()) {
157 return f->second.shape;
158 }
159 if (auto f = fInputTensorInfos.find(name); f != fInputTensorInfos.end()) {
160 return f->second.shape;
161 }
162 // throw error if shape is not dynamic
163 if (!IsDynamicTensor(name))
164 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not dynamic");
165
166 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not found");
167}
168
170 auto f = fReadyInputTensorInfos.find(name);
171 if (f != fReadyInputTensorInfos.end()) {
172 return f->second.type;
173 }
174 auto f2 = fInitializedTensors.find(name);
175 if (f2 != fInitializedTensors.end()) {
176 return f2->second.type();
177 }
178 auto f3 = fInputTensorInfos.find(name);
179 if (f3 != fInputTensorInfos.end()) {
180 return f3->second.type;
181 }
182 auto f4 = fIntermediateTensorInfos.find(name);
183 if (f4 != fIntermediateTensorInfos.end()) {
184 return f4->second.type;
185 }
186 auto f5 = fDynamicTensorInfos.find(name);
187 if (f5 != fDynamicTensorInfos.end()){
188 return f5->second.type;
189 }
190 // case of shape tensor type is INT64
191 if (fShapeTensors.find(name) != fShapeTensors.end()){
192 return ETensorType::INT64;
193 }
194
197
198 throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the type is requested is not found, model name: " + fName);
199}
200
201bool RModel::CheckIfTensorAlreadyExist(std::string tensor_name) {
202 if (fReadyInputTensorInfos.find(tensor_name) != fReadyInputTensorInfos.end()) return true;
203 if (fInputTensorInfos.find(tensor_name) != fInputTensorInfos.end()) return true;
204 if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()) return true;
205 if (fIntermediateTensorInfos.find(tensor_name) != fIntermediateTensorInfos.end()) return true;
206 if (fDynamicTensorInfos.find(tensor_name) != fDynamicTensorInfos.end()) return true;
207 if (fShapeTensors.find(tensor_name) != fShapeTensors.end()) return true;
209 return false;
210}
211
212void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape) {
215 throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
216 }
217
218 InputTensorInfo inputInfo { type, shape };
220}
221
222void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape) {
225 throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
226 }
227 TensorInfo inputInfo { type, shape };
229}
230
234
235void RModel::AddOperator(std::unique_ptr<ROperator> op, int order_execution)
236{
237 AddBlasRoutines(op->GetBlasRoutines());
238 auto libs = op->GetStdLibs();
239 auto op_input_tensors = op->GetOpInputTensors();
240 for (auto &stdlib : libs) {
242 }
243 // Convert to the deleter used for storage (see ROperatorDeleter in the header)
244 std::unique_ptr<ROperator, ROperatorDeleter> opStored(op.release());
245 if (order_execution >= 0) {
246 fOperators.insert(fOperators.begin() + order_execution, std::move(opStored));
247 } else {
248 fOperators.push_back(std::move(opStored));
249 order_execution = fOperators.size() - 1;
250 }
251
252 // storing the last usage of tensors which are input to the operator
253 // (excluding tensors which are inputs to the model or the initialized (weights) tensors)
254 // We call this function during parsing so we don't have yet initialized the operators
255 for (size_t index = 0; index < op_input_tensors.size(); index++) {
257 std::find(fInputTensorNames.begin(), fInputTensorNames.end(),
259
261 if (Verbose())
262 std::cout << "adding order execution for " << op_input_tensors[index] << " order " << order_execution
263 << std::endl;
264 }
265 }
266}
267
268void RModel::AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
269 tensor_name = UTILITY::Clean_name(tensor_name);
270 //NB: own data
271 if (CheckIfTensorAlreadyExist(tensor_name)) {
272 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
273 }
275 fInitializedTensors[tensor_name] = new_tensor;
276}
277
278void RModel::AddInitializedTensor(const std::string &tensor_name, ETensorType tensor_type,
279 const std::vector<std::size_t> &shape, void *raw_data)
280{
281 size_t size = ConvertShapeToLength(shape);
282 auto itemsize = GetTypeSize(tensor_type);
283 std::shared_ptr<void> data(malloc(size * itemsize), free);
284 std::memcpy(data.get(), raw_data, size * itemsize);
285 AddInitializedTensor(tensor_name, tensor_type, shape, data);
286}
287
288void RModel::AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
289 tensor_name = UTILITY::Clean_name(tensor_name);
290 //NB: own data
291 if (CheckIfTensorAlreadyExist(tensor_name)) {
292 throw std::runtime_error("TMVA-SOFIE: constant tensor with name " + tensor_name + " already exists \n");
293 }
294 InitializedTensor new_tensor {type, shape, data, true}; // add here flag to specify is a constant tensor
295 fInitializedTensors[tensor_name] = new_tensor;
296}
297
298void RModel::AddShapeTensor(const std::string & name, const std::vector<Dim> & shape_values, bool scalar){
299 auto tensor_name = UTILITY::Clean_name(name);
300 if (fShapeTensors.count(tensor_name) != 0) {
301 throw std::runtime_error("TMVA-SOFIE: shape tensor with name " + tensor_name + " already exists \n");
302 }
303 fShapeTensors[tensor_name] = std::make_pair(shape_values, scalar);
304}
305
306bool RModel::AddAliasTensor(const std::string &name, const std::string &origin)
307{
308 // add an alias tensor to origin: the generated code points it to the memory of origin.
309 // The tensor must already be registered as an intermediate tensor.
310 auto tensor_name = UTILITY::Clean_name(name);
312 // two operators writing the same tensor is a malformed graph rather than a case to refuse,
313 // so this comes before the checks below and the error does not depend on the optimization level
314 if (fAliasTensors.count(tensor_name) != 0) {
315 throw std::runtime_error("TMVA-SOFIE: alias tensor with name " + tensor_name + " already exists \n");
316 }
317 // an alias of an alias refers directly to the tensor owning the memory
318 if (auto it = fAliasTensors.find(origin_name); it != fAliasTensors.end())
319 origin_name = it->second;
320
321 // with kBasic every tensor keeps its own buffer, as the automatic differentiation of the
322 // generated code requires
324 return false;
325 // graph outputs are written into the buffers provided by the caller
326 if (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), tensor_name) != fOutputTensorNames.end())
327 return false;
328 // only intermediate tensors can be shared: graph inputs are read-only and initialized tensors
329 // are managed by the session
331 return false;
332 // boolean tensors are std::vector<uint8_t> members accessed directly: ROperator_SubGraph
333 // reads fTensor_<cond>[0] and writes fTensor_<out>.begin(), which an alias does not have
335 return false;
336
337 fAliasTensors[tensor_name] = origin_name;
338 return true;
339}
340
341bool RModel::IsShapeTensor(const std::string & tensor_name) const {
342 return fShapeTensors.count(tensor_name) != 0;
343}
344
345bool RModel::IsAliasTensor(const std::string & tensor_name) const {
346 return fAliasTensors.count(tensor_name) != 0;
347}
348
349const std::vector<Dim> & RModel::GetShapeTensorValues(const std::string & tensor_name) const {
350 //if (!IsShapeTensor(tensor_name) ) return std::vector<Dim>{};
351 return fShapeTensors.at(tensor_name).first;
352}
353
354bool RModel::IsInitializedTensor(const std::string& tensorName) const {
355 std::string name = UTILITY::Clean_name(tensorName);
356 return fInitializedTensors.find(name) != fInitializedTensors.end();
357}
358bool RModel::IsConstantTensor(const std::string& tensorName) const {
359 // a constant tensor is an initialized tensor but has the constant flag set
360 std::string name = UTILITY::Clean_name(tensorName);
361 auto itr = fInitializedTensors.find(name);
362 if (itr == fInitializedTensors.end()) return false;
363 return itr->second.IsConstantTensor();
364}
365
366// dynamic tensors include also Dim input tensors
367bool RModel::IsDynamicTensor(const std::string& tensorName) const {
368 std::string name = UTILITY::Clean_name(tensorName);
370 return (ret) ? true : IsDimInputTensor(tensorName);
371}
372bool RModel::IsDimInputTensor(const std::string& tensorName) const {
373 std::string name = UTILITY::Clean_name(tensorName);
374 return fInputTensorInfos.find(name) != fInputTensorInfos.end();
375}
376bool RModel::IsReadyInputTensor(const std::string& tensorName) const {
377 std::string name = UTILITY::Clean_name(tensorName);
379}
380
381// generic addition of a tensor
382void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape) {
384 if (!int_shape.empty())
385 AddIntermediateTensor(tensor_name, type, int_shape);
386 else
387 AddDynamicTensor(tensor_name, type, dim_shape);
388}
389
390void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape) {
391 tensor_name = UTILITY::Clean_name(tensor_name);
392 if (CheckIfTensorAlreadyExist(tensor_name)) {
393 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
394 }
395 TensorInfo new_tensor {type, shape};
397}
398
399void RModel::AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape){
400 tensor_name = UTILITY::Clean_name(tensor_name);
401 if (CheckIfTensorAlreadyExist(tensor_name)){
402 throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
403 }
405 fDynamicTensorInfos[tensor_name] = new_tensor;
406 // store shape parameter if not existing
407 for (auto &d : shape) {
408 if (d.isParam) {
409 if (d.dim != size_t(-1)) {
410 AddShapeParam(d.param, d.dim);
411 }
412 }
413 }
414}
415
416void RModel::AddShapeParam(const std::string & param, size_t default_value) {
417 // a parameter computed at run time by an operator is never a Session constructor argument
418 if (fComputedShapeParams.count(param) != 0)
419 return;
420 if (fShapeParams.count(param) == 0) {
421 fShapeParams[param] = std::to_string(default_value);
422 // add also in the vector list (used to keep the order)
423 fDimShapeNames.push_back(param);
424 }
425}
426
427void RModel::AddComputedShapeParam(const std::string &param)
428{
429 fComputedShapeParams.insert(param);
430 // it may already be registered as an argument, reached through a shape that broadcasting
431 // rebuilt from a string; the operator's own declaration is the only one that should remain
432 fShapeParams.erase(param);
433 fDimShapeNames.erase(std::remove(fDimShapeNames.begin(), fDimShapeNames.end(), param), fDimShapeNames.end());
434}
435
437 fOutputTensorNames.clear();
438 for(auto& it : outputtensornames) {
439 fOutputTensorNames.emplace_back(UTILITY::Clean_name(it));
440 }
441}
442
443void RModel::UpdateOutputTensorList(std::vector<std::string> curr_output_tensors, std::vector<std::string> new_output_tensors) {
444 for(auto& it:curr_output_tensors) {
445 fOutputTensorNames.erase(std::remove(fOutputTensorNames.begin(), fOutputTensorNames.end(), it), fOutputTensorNames.end());
446 }
448}
449
450void RModel::UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
451 tensor_name = UTILITY::Clean_name(tensor_name);
452 if (!CheckIfTensorAlreadyExist(tensor_name)) {
453 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to update it");
454 }
456 fInitializedTensors[tensor_name] = new_tensor;
457}
458
459std::shared_ptr<void> RModel::GetInitializedTensorData(std::string tensor_name) {
460 auto f = fInitializedTensors.find(tensor_name);
461 if (f == fInitializedTensors.end()) {
462 throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to get its data");
463 } else {
464 return f->second.sharedptr();
465 }
466}
467
468void RModel::SetNotWritableInitializedTensor(const std::string & tensor_name) {
469 auto t = fInitializedTensors.find(tensor_name);
470 if (t == fInitializedTensors.end()) {
471 throw std::runtime_error("TMVA-SOFIE: initialized tensor " + tensor_name + " not found when trying to get its info");
472 }
473 t->second.SetNotWritable();
474 }
475
476std::string RModel::AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors)
477{
478 std::stringstream code;
479
480 if (fVerbose) {
481 std::cout << "Total chunks allocated\n";
483 std::cout << "..... chunk " << chunk->first << " size " << chunk->second.tensor_size << " " << chunk->second.tensor_name << std::endl;
484 }
485 }
486
487 auto declareIntermediateTensor = [this, &code](std::string const &name, size_t size, size_t location) {
488 std::string typeName = ConvertTypeToString(GetTensorType(name));
489 code << "\n // Allocating memory for intermediate tensor " << name << " with size " << size << " bytes";
490 code << "\n"
491 << typeName << "* " << TensorMember(name) << " = reinterpret_cast<" << typeName
492 << "*>(fIntermediateMemoryPool.data() + " << location << ");\n";
493 };
494
495 if (fVerbose) std::cout << "*** AllocateIntermediateMemory: Loop on op output tensors\n";
496 // order output tensors by size
497 std::vector<TensorMemoryInfo> ordered_output_tensors;
498
499 for (auto &it : op_output_tensors) {
500 auto name = std::string(it);
503 continue;
504
505 // case of alias tensor
506 if (IsAliasTensor(name)) {
507 continue;
508 }
509
511 // important fill the pair in the ordered output tensors with the string view and not the string
512 TensorMemoryInfo tmi = {it, tensor_size};
513 ordered_output_tensors.push_back(tmi);
514 }
516 [](const TensorMemoryInfo &a, const TensorMemoryInfo &b) { return a.tensor_size > b.tensor_size; });
517
518 for (auto &it : ordered_output_tensors) {
519 bool allocated = false;
520 std::string name = std::string{it.tensor_name};
521 size_t tensor_size = it.tensor_size;
522 if (fVerbose)
523 std::cout << "output tensor " << name << " size " << tensor_size << std::endl;
524
527
528 if (fVerbose) std::cout << ".. available chunk " << chunk->first << " with size = " << chunk->second;
529 // check if available memory chunks can accommodate the tensor
530 if (chunk->second >= tensor_size) {
531 // need to use here string_view (i.e it.tensor_name)
532 // split returns the new chunk with size of new tensor. The free chunk is before the used one
533 auto new_chunk = fIntermediateMemoryInfo.total_stack[chunk->first].split(it.tensor_name, tensor_size);
534 auto new_chunk_location = chunk->first + chunk->second - tensor_size;
536
538 chunk->second -= tensor_size;
539
540 allocated = true;
541
542 if (fVerbose) std::cout << " is re-used and split in a new of size " << new_chunk.tensor_size << " at " << new_chunk_location;
543
544 if (chunk->second == 0) {
545 if (fVerbose) std::cout << " and deleted since size matches";
547 }
548 if (fVerbose) std::cout << std::endl;
549 break;
550 } else if (chunk->first == fIntermediateMemoryInfo.available_stack.rbegin()->first &&
551 fIntermediateMemoryInfo.total_stack.rbegin()->first == chunk->first) {
552 // case last available chunk is the last in the memory, we can increase that one
553 fIntermediateMemoryInfo.total_stack[chunk->first] = {it.tensor_name, tensor_size};
554 declareIntermediateTensor(name, tensor_size, chunk->first);
556 allocated = true;
557 if (fVerbose) std::cout << " is extended with a bigger one of size " << tensor_size << std::endl;
558 break;
559 }
560 ++chunk;
561 if (fVerbose) std::cout << std::endl;
562 }
563
564 if (!allocated) {
566 ? 0
567 : fIntermediateMemoryInfo.total_stack.rbegin()->first +
568 fIntermediateMemoryInfo.total_stack.rbegin()->second.tensor_size;
569
571
573
574 if (fVerbose) std::cout << "no chunk available - add in total stack a new chunk with size of tensor and idx : " << chunk_idx
575 << std::endl;
576 }
577 }
578 return code.str();
579}
580
581void RModel::CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_input_tensors, const size_t& op_idx){
582 if (fVerbose) std::cout << "*** CheckAndFlushIntermediateMemory: Loop on input tensors for op " << op_idx << "\n";
583 //print available chunks
584 if (fVerbose) std::cout << "available chunks before freeing them : \n";
587 if (fVerbose) std::cout << "-- free chunk " << chunk->first << " size = " << chunk->second << std::endl;
588 }
589 for (auto &iv : op_input_tensors) {
590 // last occurrence of the tensor is reached => flush it from memory
591 if (fVerbose) std::cout << ".. input tensors : " << iv;
592
593 // for alias tensors the memory to flush is the one of the tensor they refer to
594 std::string it{iv}; // convert view to string
595 if (IsAliasTensor(it))
596 it = fAliasTensors[it];
597 // find(): operator[] would insert a key viewing the local string
599 if (lastUse != fIntermediateTensorFrequencyLookup.end() && lastUse->second == op_idx) {
600 if (fVerbose) std::cout << " flash condition is met - looping on chunks to find matching one \n";
601 for (auto chunk = fIntermediateMemoryInfo.total_stack.begin();
603 if (fVerbose) std::cout << "--- chunk " << chunk->first << " , " << chunk->second.tensor_name << " size " << chunk->second.tensor_size;
604 if (chunk->second.tensor_name == it) {
605 if (fVerbose) std::cout << " -- Found chunk corresponding to input tensor: " << chunk->first;
606 // check if nearby chunks in available memory can coalesce
608 chunk->first); // smallest element greater than the flushed chunk idx
611 : std::prev(first_greater); // largest element smaller than the flushed chunk idx
612
613 // check if the next stack entry is actually adjacent in memory
614
616 last_smaller->first + last_smaller->second == chunk->first) {
617 // merge chunk with previous one
618 last_smaller->second += chunk->second.tensor_size;
620 if (fVerbose) std::cout << " is adjacent in memory with previous one - merge ";
622 last_smaller->first + last_smaller->second == first_greater->first) {
623 // merge also with following one
624 last_smaller->second += first_greater->second;
627 // delete merged one in available stack and in total stack
630 if (fVerbose) std::cout << " merge also with following that is free ";
631 }
633 if (fVerbose) std::cout << std::endl;
634 break;
636 chunk->first + chunk->second.tensor_size == first_greater->first) {
637 // merge with first greater
638 if (fVerbose) std::cout << " is adjacent in memory with following one - merge \n";
639 // cannot modify idx of first_greter. Insert a new one and delete previous one
640 size_t new_size = chunk->second.tensor_size + first_greater->second;
641 size_t first_greater_idx = first_greater->first;
643 // cannot use anymore first_greater
648 } else {
649 fIntermediateMemoryInfo.available_stack.insert({chunk->first, chunk->second.tensor_size});
650 if (fVerbose) std::cout << " insert in the available stack the chunk with size " << chunk->second.tensor_size << std::endl;
651 }
652 chunk->second.tensor_name = "free";
653 break;
654 }
655 }
656 } else {
657 if (fVerbose) std::cout << std::endl;
658 }
659 }
660}
661
662void RModel::Initialize(int batchSize, bool verbose) {
663 std::map<std::string, size_t> inputParams;
664 if (batchSize > 0) {
665 inputParams["input_size"] = batchSize;
666 inputParams["batch_size"] = batchSize;
667 inputParams["bs"] = batchSize;
668 }
669 Initialize(inputParams, verbose);
671}
672void RModel::Initialize(const std::map<std::string, size_t> & inputParams, bool verbose) {
673
674 fVerbose = int(verbose);
675
676 if (fIsInitialized) {
677 if (verbose)
678 std::cout << "Model is already initialized - skip initialization " << std::endl;
679 return;
680 }
682 fDynamicTensorInfos.clear();
683
684
685 // loop on inputs and see if shape can be full specified
686 // if the batch size is provided it can be used to specify the full shape
687 // Add the full specified tensors in fReadyInputTensors collection
688 auto originalInputTensorInfos = fInputTensorInfos; // need to copy because we may delete elements
689 for (auto &input : originalInputTensorInfos) {
690 if (verbose) std::cout << "looking at the tensor " << input.first << std::endl;
691 // if a parameter (e.g. batch_size) is specified use for converting parametric shape in defined one
692 if (!inputParams.empty()) {
693 for (auto &d : input.second.shape) {
694 if (d.isParam) {
695 std::string pname = d.param;
696 if (pname == input.first + "_size") pname = "input_size";
697 auto itr = inputParams.find(pname);
698 if (itr != inputParams.end() ) {
699 d = Dim{ itr->second };
700 if (verbose)
701 std::cout << "Tensor: " << input.first << " - fix parametric shape " << itr->first << " to " << itr->second << std::endl;
702 }
703 }
704 }
705 }
706 // see if shape now is fully defined
707 auto shape = ConvertShapeToInt(input.second.shape);
708 if (verbose)
709 std::cout << "converting input shape for " << input.first << " " << ConvertShapeToString(shape) << " from "
710 << ConvertDimShapeToString(input.second.shape) << std::endl;
711 if (!shape.empty()) {
712 // case shape is defined (not parametric) we add the tensor in the fReadyInputTensorInfos map and
713 // we remove the tensor from the fInputTensorInfo where th eold parametric shape was stored
714 fInputTensorInfos.erase(input.first);
715 // add to the ready input tensor information the new fixed shape
716 AddInputTensorInfo(input.first, input.second.type, shape);
717 // check consistency
719 }
720 // store the parameters of the input tensors
721 else {
722 // store the found parametric shape parameters
723 for (auto &d : input.second.shape) {
724 if (d.isParam) {
725 // through AddShapeParam, which keeps out the parameters computed at run time
726 AddShapeParam(d.param, d.dim);
727 }
728 }
729 }
730 }
731
732 if (verbose) {
735 }
736
737 // Go through model and initialize each operator
738 int i = 0;
739
740 std::vector<size_t> temp_available_stack; // vector stores individual chunks of available memory that maybe reused
741
742 // Build set of initialized tensors consumed by at least one runtime operator (need for later)
743 std::unordered_set<std::string> runtimeInitializedInputs;
744 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
745 if (verbose) {
746 auto& r = *fOperators[op_idx].get();
747 std::cout << "Initializing operator " << i << " " << typeid(r).name() << std::endl;
748 }
749 fOperators[op_idx]->Initialize(*this);
750 for(auto &it:fOperators[op_idx]->GetOpOutputTensors()){
751 std::string name = std::string{it};
752 // check if tensor is not an initialized or output tensor and it is not already in the list
754 std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end() &&
756 {
758 }
759 }
760 // loop for non-constant operators and flag the inputs which are initialized tensors to make sure they are writable
761 if (!fOperators[op_idx]->IsOutputConstant()) {
762 for (auto &it : fOperators[op_idx]->GetOpInputTensors()) {
763 std::string name = std::string{it};
764 if (fInitializedTensors.find(name) != fInitializedTensors.end()) {
766 }
767 }
768 }
769
770 i++;
771 }
772
773 // loop on initialized tensors and make the integers as constant to be
774 // not written in a weight file and check if the tensors flagged as not writable are really not writable,
775 // i.e. are not used by non constant operators
776 for (auto &it : fInitializedTensors) {
777 // check if not-writable tensors are really not writable, i.e. are not used by non constant operators
778 if (it.second.IsNotWritable() && runtimeInitializedInputs.find(it.first) != runtimeInitializedInputs.end()) {
779 it.second.SetWritable();
780 if (verbose) {
781 std::cout << "Initialized tensor " << it.first << " is flagged as not writable but is used by non constant operators, set it as writable \n";
782 }
783 }
784 // 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
785 // only FLOAT tensors are written in a weight file
786 if (it.second.type() != ETensorType::FLOAT) {
787 it.second.SetConstant();
788 }
789 }
790
791 // check if there are initialized tensors to write in a weight file
792 if (fUseWeightFile) {
793 bool modelHasWeights = false;
794 for (auto &it : fInitializedTensors) {
795 if (it.second.IsWeightTensor()) {
796 modelHasWeights = true;
797 break;
798 }
799 }
800 if (!modelHasWeights)
801 fUseWeightFile = false;
802 }
803
804 // update fIntermediateTensorFrequencyLookup for alias tensors
805 for (auto & it : fAliasTensors) {
809 else {
810 // take the largest one
812 }
813 }
814
815 fIsInitialized = true;
816}
817
818void RModel::InitializeSubGraph(std::shared_ptr<RModel> graph) {
819 // add the subgraph to the list
820 fSubGraphs.push_back(graph);
821 //this needs to be done before initializing
822 graph->fParentGraph = this;
823 graph->fIsSubGraph = true;
824
825 graph->Initialize(fBatchSize, fVerbose);
826 // set the same options as parent model
827 graph->fWeightFile = fWeightFile;
828 graph->fUseWeightFile = fUseWeightFile;
829 // add needed blas routines and libs
830 std::vector<std::string> blasRoutines;
831 for (auto & e : graph->fNeededBlasRoutines)
832 blasRoutines.push_back(e);
834 for (auto e : graph->fNeededStdLib)
836 // helper functions used by the subgraph must be emitted in the top-level
837 // header, so propagate them to the parent model
838 for (auto const &h : graph->GetNeededHelperFunctions())
840
841 // add parent input tensors to current graph
842 for (auto & name : fInputTensorNames)
843 graph->fInputTensorNames.emplace_back(name);
844
845 // clean graph name
846 graph->fName = UTILITY::Clean_name(graph->fName);
847
848}
849
850// Function to generate the code for declaring and initializing constant tensors
851// This is for tensors which are not part of weight files and can be created from the Constant operator
852template <typename T>
853std::string GenerateConstantTensorCode(const std::pair<std::string, InitializedTensor> &t)
854{
855 std::stringstream strs;
856 std::string type = ConvertTypeToString(t.second.type());
857 size_t length = ConvertShapeToLength(t.second.shape());
858 // avoid using stack sizes for constant tensors to reduce compilation time
859 // also for weights which can be broadcasted do not use stack but allocate as a std::vector
860 bool allocateOnStack = (length > 100 || t.second.IsWeightTensor()) ? false : true;
861
862 const T *data = t.second.data<T>();
863
864 // and check if all values are the same
865 bool sameData = false;
866
867 // for non stack allocation check if data are the same
868 if (!allocateOnStack && length > 1) {
869 size_t idx = 1;
870 do {
871 sameData = (data[idx] == data[idx - 1]);
872 idx++;
873 } while (sameData && idx < length);
874 }
875 if (allocateOnStack) {
876 strs << type << " fTensor_" << t.first << "[" << length << "] = " << ConvertValuesToString(length, data) << ";\n";
877 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ";\n";
878 } else {
879 strs << "std::vector<" << type << "> fTensor_" << t.first << " = ";
880 if (sameData)
881 strs << "std::vector<" << type << ">(" << length << ", " << ConvertValToString(data[0]) << ");\n";
882 else {
884 }
885 strs << type << " * " << TensorMember(t.first) << " = fTensor_" + t.first + ".data();\n";
886 }
887 return strs.str();
888}
889
891{
892 if (!fInitializedTensors.empty())
893 fGC += "// initialized (weights and constant) tensors\n";
894
895 // here are constant tensor or initialized ones which are not weights (e.g. int64_t tensors )
896 for (auto &i : fInitializedTensors) {
897 if (i.second.IsNotWritable()) continue;
898 size_t length = ConvertShapeToLength(i.second.shape());
899 if (!fUseWeightFile || i.second.IsConstantTensor() || !i.second.IsWeightTensor() || i.second.type() != ETensorType::FLOAT ) {
900 if (i.second.type() == ETensorType::FLOAT) {
901 // check if NaN of Inf are inside tensor data
902 bool hasInfOrNaN = false;
903 const float *data = i.second.data<float>();
904 for (size_t idx = 0; idx < length; idx++) {
905 if (std::is_floating_point<float>::value) {
906 if (std::isinf(data[idx]) || std::isnan(data[idx])) {
907 hasInfOrNaN = true;
908 break;
909 }
910 }
911 }
912 if (hasInfOrNaN)
913 AddNeededStdLib("limits");
915 fConstantTensorSize += length * sizeof(float);
916 } else if (i.second.type() == ETensorType::INT64) {
918 fConstantTensorSize += length * sizeof(int64_t);
919 } else if (i.second.type() == ETensorType::INT32) {
921 fConstantTensorSize += length * sizeof(int32_t);
922 } else if (i.second.type() == ETensorType::BOOL || i.second.type() == ETensorType::UINT8 ) {
924 fConstantTensorSize += length * sizeof(uint8_t);
925 }
926
927
928 } else {
929 // case of tensors which are read from a file
930 if (i.second.type() == ETensorType::FLOAT) {
931 fGC += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
932 fGC += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
933 fWeightsTensorSize += length * sizeof(float);
934 }
935 }
936 }
937}
938
940 if (fIntermediateMemoryInfo.total_stack.empty()) return;
941 fGC += "\n//--- Allocating session memory pool to be used for allocating intermediate tensors\n";
942
943 // char memory block is allocated since char takes 1 byte, thus easier to allocate tensors
944 // of other data types
946 const size_t memPoolSize = totalStack.rbegin()->first + totalStack.rbegin()->second.tensor_size;
947 fGC += "std::vector<char> fIntermediateMemoryPool = std::vector<char>(" + std::to_string(memPoolSize) + ");\n\n";
948}
949
951 if (!fIntermediateTensorInfos.empty()) {
952 std::string tensor_declaration_block = "";
953 for (auto &i : fIntermediateTensorInfos) {
954 // alias tensors have no storage: the operator creating them declares a pointer to the
955 // memory of the tensor they refer to
956 if (IsAliasTensor(i.first))
957 continue;
958 if (i.second.type == ETensorType::BOOL) {
959 tensor_declaration_block += "std::vector<std::uint8_t> fTensor_" + i.first +
960 " = std::vector<std::uint8_t>(" +
961 std::to_string(ConvertShapeToLength(i.second.shape)) + ");\n";
963 "std::uint8_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
964 continue;
965 }
967 bool not_in_freq_map =
970 (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) == fOutputTensorNames.end());
971
973 size_t length = ConvertShapeToLength(i.second.shape);
974
975 if (i.second.type == ETensorType::FLOAT) {
976 tensor_declaration_block += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
977 tensor_declaration_block += "float * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
979 }
980 else if (i.second.type == ETensorType::DOUBLE) {
981 tensor_declaration_block += "std::vector<double> fTensor_" + i.first + " = std::vector<double>(" + std::to_string(length) + ");\n";
982 tensor_declaration_block += "double * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
984 }
985 else if (i.second.type == ETensorType::INT64) {
986 tensor_declaration_block += "std::vector<int64_t> fTensor_" + i.first + " = std::vector<int64_t>(" + std::to_string(length) + ");\n";
987 tensor_declaration_block += "int64_t * " + TensorMember(i.first) + " = fTensor_" + i.first + ".data();\n";
989 }
990 }
991 }
992
993 if (tensor_declaration_block.length()) {
994 fGC += "\n//--- declare and allocate the intermediate tensors\n" + tensor_declaration_block;
995 }
996 }
997 // add also the dynamic tensors (only declarations, allocation will be done later)
998 if (!fDynamicTensorInfos.empty()) {
999 fGC += "//--- declare the dynamic tensors\n";
1000 for (auto &i : fDynamicTensorInfos) {
1001 if (IsAliasTensor(i.first))
1002 continue;
1003 fGC += ConvertTypeToString(i.second.type) + " * " + TensorMember(i.first) + " = nullptr;\n";
1004 }
1005 fGC += "//--- dynamic tensors pool\n";
1006 fGC += "std::vector<char> fDynamicMemoryPool;\n";
1007 }
1008}
1009
1010// generate code for specific operator declarations to be defined in the Session class
1012 std::string strcode;
1013 for (auto & op : fOperators) {
1014 strcode += op->GenerateDeclCode();
1015 }
1016 if (strcode.empty()) return;
1017 fGC += "\n//---- operator declarations \n";
1018 fGC += strcode;
1019 fGC += "\n";
1020}
1021
1023{
1024 // generate code for allocating dynamic tensors using the greedy memory allocations
1025 if (fDynamicTensorInfos.empty())
1026 return;
1027
1028 if (fVerbose) {
1029 std::cout << "generating code for dynamic tensor management" << std::endl;
1031 }
1032
1033 // the generated code uses the TensorLifeInfo / OrganizeMemory inference helpers
1034 AddNeededHelperFunction("DynamicMemory");
1035
1036 std::stringstream out;
1037 out << "// dynamic tensor memory management\n";
1038 out << SP << "std::vector<TensorLifeInfo> dynamicTensorInfos;\n";
1039 out << SP << "dynamicTensorInfos.reserve(" << fDynamicTensorInfos.size() << ");\n";
1040
1041 // loop on all the operators to find begin/end life of the tensors
1042 int op_index = 0;
1043 std::vector<std::pair<std::string, ETensorType>> tensors;
1044 tensors.reserve(fDynamicTensorInfos.size());
1045 for (auto & op : fOperators) {
1046 // loop on output tensors -
1047 for (auto &it : op->GetOpOutputTensors()) {
1048 if (fVerbose) {
1049 auto op_ptr = op.get();
1050 std::cout << "Looping on operator " << op_index << " " << typeid(*op_ptr).name() << std::endl;
1051 }
1052 // check if is a dynamic tensor and not an alias tensor or output tensor
1053 std::string name = std::string(it);
1055 && std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), name) == fOutputTensorNames.end()) {
1056 auto tensor_size = ConvertDimShapeToLength(GetDimTensorShape(name));
1057 auto type = GetTensorType(name);
1058 size_t type_size = GetTypeSize(type);
1059 int begin = op_index;
1060 int end = fOperators.size();
1061 // look for end
1064 end = it_lookup->second + 1; // end is last time used + 1
1065 // // some tensors (like xcol in convolutions) are just used within the operators
1066 // if (end == 0 && begin > 0) end = begin+1;
1067
1068 if (begin> end) {
1069 std::cout << "op " << op_index << "tensor_" << name << " begin " << begin << " " << " end " << end << std::endl;
1070 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo: tensor_" + name + " has end before begin");
1071 }
1072
1073 // write in code
1074 out << SP << "dynamicTensorInfos.push_back( {" << begin << ", " << end << ", " << type_size << "* (" << tensor_size << ") });"
1075 << " // tensor_" << name << std::endl;
1076 tensors.push_back({name,type});
1077 }
1078 }
1079 op_index++; // increment operator index
1080 }
1081 out << "\n" << SP << "auto memory_result = OrganizeMemory(dynamicTensorInfos);\n\n";
1082 out << "// allocating now the memory\n";
1083 out << SP << "fDynamicMemoryPool = std::vector<char>(memory_result.total_bytes);\n";
1084 out << SP << "int idx = 0;\n";
1085 for (auto & it : tensors) {
1086 out << SP << "tensor_" << it.first << " = reinterpret_cast<" << ConvertTypeToString(it.second) << " *>(fDynamicMemoryPool.data() + memory_result.offsets[idx++]);\n";
1087 }
1088 // check that all dynamic tensors are covered
1089 bool missingTensor = false;
1090 for (auto &i : fDynamicTensorInfos) {
1091 if (IsAliasTensor(i.first)) continue;
1092 if (std::find(fOutputTensorNames.begin(), fOutputTensorNames.end(), i.first) != fOutputTensorNames.end()) continue;
1093 if (std::find(tensors.begin(), tensors.end(), std::pair<std::string,ETensorType>{i.first, i.second.type}) == tensors.end()) {
1094 std::cout << "Dynamic tensors " << i.first << " is not in list of operator input/output " << std::endl;
1095 missingTensor = true;
1096 }
1097 }
1098 if (missingTensor)
1099 throw std::runtime_error("TMVA-SOFIE: RModel::GenerateDynamicTensorInfo - some tensors are not in input/output list");
1100
1101 fGC += out.str();
1102}
1103
1104/// Check if a given parameter is used for the shape of an input tensor.
1105bool RModel::IsInputTensorShapeParam(std::string const &paramName) const
1106{
1107 for (auto &name : fInputTensorNames) {
1108 if (IsDimInputTensor(name)) {
1109 auto shape = GetDynamicTensorShape(name);
1110 for (auto &d : shape) {
1111 if (d.param == paramName)
1112 return true;
1113 }
1114 }
1115 }
1116 return false;
1117}
1118
1119/// Collects all identifiers starting with "tensor_" in the input code,
1120/// provided that the occurrence is not immediately preceded by a
1121/// character that is valid in a C++ identifier. Excludes input and output tensor names.
1122/// Returns a deduplicated std::vector<std::string>.
1123std::vector<std::string> RModel::CollectTensorMemberNames(const std::string &input)
1124{
1125 const std::string target = "tensor_";
1126
1127 std::vector<std::string> result;
1128
1129 for (size_t i = 0; i < input.size();) {
1130
1131 bool doCollect = false;
1132
1133 if (i + target.size() <= input.size() && input.compare(i, target.size(), target) == 0 &&
1134 (i == 0 || !IsIdentifierChar(input[i - 1]))) {
1135
1136 doCollect = true;
1137
1138 std::size_t j = i + target.size();
1139
1140 // Extend to full identifier
1141 while (j < input.size() && IsIdentifierChar(input[j]))
1142 ++j;
1143
1144 std::string fullName = input.substr(i, j - i);
1145
1146 // Exclude input tensor names
1147 for (std::string const &name : fInputTensorNames) {
1148 if (fullName == target + name) {
1149 doCollect = false;
1150 break;
1151 }
1152 }
1153
1154 // Exclude output tensor names
1155 if (doCollect) {
1156 for (std::string const &name : fOutputTensorNames) {
1157 if (fullName == target + name) {
1158 doCollect = false;
1159 break;
1160 }
1161 }
1162 }
1163
1164 if (doCollect) {
1165 result.push_back(fullName);
1166 }
1167
1168 i = j; // advance past the identifier
1169 } else {
1170 ++i;
1171 }
1172 }
1173
1174 // Deduplicate (order not preserved)
1175 std::sort(result.begin(), result.end());
1176 result.erase(std::unique(result.begin(), result.end()), result.end());
1177
1178 return result;
1179}
1180
1182 // generate the infer signature given the inputs: eg. "float * tensor1, float * tensor2"
1183 // if (decl = false) generate only calling signature (tensor1,tensor2,....)
1184 std::string rGC;
1185 std::unordered_map<std::string, int> inputParams;
1186 int i_input = 0;
1187 for (auto &name : fInputTensorNames) {
1188 // if is a dynamic tensor pass initial parameters
1189 if (IsDimInputTensor(name)) {
1190 auto shape = GetDynamicTensorShape(name);
1191 for (auto &d : shape) {
1192 std::string pName = d.param;
1193 // need to check if the input parameters is already existing in another input tensor
1194 if (d.isParam && inputParams.count(pName) == 0) {
1195 if (isdecl) rGC += "size_t ";
1196 rGC += d.param + ",";
1198 }
1199 }
1200 }
1201 if (isdecl) {
1203 if (type == "other")
1204 throw std::runtime_error("TMVA-SOFIE: input tensor " + name +
1205 " is of a data type which is not yet supported.");
1206 rGC += type + " const* ";
1207 }
1208 rGC += "tensor_" + name + ",";
1209 i_input++;
1210 }
1211
1212 if (fInputTensorNames.size() > 0) rGC.pop_back();// remove last ","
1213 return rGC;
1214}
1215
1216namespace {
1217
1218std::string typeForOutput(ETensorType t) {
1219 // The std::vector<bool> is a special type that is not wrapping continuous memory.
1220 // We don't want to use it as a return type.
1222 return ConvertTypeToString(t);
1223}
1224
1225std::string memberNameForDimShape(std::string name)
1226{
1227 if (!name.empty()) {
1228 name[0] = std::toupper(static_cast<unsigned char>(name[0]));
1229 }
1230 name = "f" + name;
1231 return name;
1232}
1233
1234}
1235
1237{
1238 size_t outputSize = fOutputTensorNames.size();
1239 // assume output types are all the same
1240
1241 bool sameOutputTypes = true;
1242 std::string inferReturnType; // type return by infer function
1244 fGC += "\n\n";
1245 if (outputSize == 1) {
1246 fGC += "std::vector<" + typeForOutput(eFirstOutputType) + ">";
1247 } else {
1248 // if all output types are the same we return an std::vector - otherwise a tuple
1249 for (std::string const &name : fOutputTensorNames) {
1251 sameOutputTypes = false;
1252 }
1253 if (sameOutputTypes)
1254 fGC += "std::vector<std::vector<" + typeForOutput(eFirstOutputType) + ">>";
1255 else {
1256 inferReturnType = "std::tuple<";
1257 for (size_t i = 0; i < outputSize; i++) {
1258 inferReturnType += "std::vector<" + typeForOutput(GetTensorType(fOutputTensorNames[i])) + ">";
1259 if (i < outputSize - 1)
1260 inferReturnType += ",";
1261 }
1262 inferReturnType += ">";
1264 }
1265 }
1266
1267 fGC += " infer(" + GenerateInferSignature() + "){\n";
1268
1269 std::string doInferArgs = GenerateInferSignature(false);
1270 if (!doInferArgs.empty())
1271 doInferArgs += ",";
1272 // several outputs can share one run-time shape parameter: declare and pass it once
1273 std::unordered_set<std::string> emittedShapeParams;
1274 for (std::string const &name : fOutputTensorNames) {
1275 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1276 std::string n;
1277 if(!isDynamic) {
1278 n = std::to_string(ConvertShapeToLength(GetTensorShape(name)));
1279 } else {
1281 // Use the session member (fXxx) when any dim is a runtime-computed identifier
1282 // (e.g. NonZero count). For expression-type dims derived from input shapes
1283 // (e.g. "((W+-3)/2+1)"), use the expression directly.
1284 // 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
1285 bool hasRuntimeParam = false;
1286 for (auto const &dim : GetDynamicTensorShape(name)) {
1287 if (dim.isParam && IsIdentifier(dim.param) && !IsInputTensorShapeParam(dim.param))
1288 hasRuntimeParam = true;
1289 }
1291 }
1292 std::string outputName = "output_tensor_" + name;
1293 fGC += SP + "std::vector<" + typeForOutput(GetTensorType(name)) + " > " + outputName + "(" + n + ");\n";
1294 doInferArgs += " " + outputName + ".data(),";
1295 if(isDynamic) {
1296 for (auto const &dim : GetDynamicTensorShape(name)) {
1297 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param) &&
1298 emittedShapeParams.insert(dim.param).second) {
1299 fGC += SP + "size_t " + dim.param + " = 0;\n";
1300 doInferArgs += " " + dim.param + ",";
1301 }
1302 }
1303 }
1304 }
1305 if (!doInferArgs.empty())
1306 doInferArgs.back() = ' ';
1307
1308 // verifying if the dynamic parameters are within allowed range
1309 std::unordered_set<std::string> input_params_checked;
1310 std::string dynamic_parameters_check = "";
1311 for (auto &name : fInputTensorNames) {
1312 if (IsDimInputTensor(name)) {
1313 auto shape = GetDynamicTensorShape(name);
1314 for (auto &d : shape) {
1315 std::string pName = d.param;
1316 if (d.isParam && input_params_checked.count(pName) == 0) {
1317 std::string memberName = memberNameForDimShape(d.param);
1318 dynamic_parameters_check += d.param + " > " + memberName + " || ";
1320 fGC += SP + "if (" + d.param + " > " + memberName + ") {\n";
1321 fGC += SP + SP + "throw std::runtime_error(\"TMVA-SOFIE: dynamic input tensor shape parameter " +
1322 d.param + " exceeds the initialized maximum allowed shape.\");\n";
1323 fGC += SP + "}\n";
1324 }
1325 }
1326 }
1327 }
1328
1329 fGC += SP + "doInfer(*this, " + doInferArgs + ");\n";
1330
1331 // If the output tensors have dynamic sizes, now is the time to set them
1332 for (std::string const &name : fOutputTensorNames) {
1333 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1334 if (isDynamic) {
1335 std::string outputName = "output_tensor_" + name;
1336 auto tensor_size = ConvertDimShapeToLength(GetDimTensorShape(name));
1337 fGC += SP + outputName + ".resize(" + tensor_size + ");\n";
1338 }
1339 }
1340
1341 fGC += SP + "return {";
1342 for (size_t i = 0; i < fOutputTensorNames.size(); i++) {
1343 fGC += "output_tensor_" + fOutputTensorNames[i];
1344 if (i < fOutputTensorNames.size() - 1)
1345 fGC += ",";
1346 }
1347 fGC += "};\n";
1348 fGC += "}\n"; // end of infer function scope
1349}
1350
1352{
1353 std::string sessionName = !fIsSubGraph ? "Session" : "Session_" + fName;
1354
1355 // forward declare session struct
1356 fGC += "struct " + sessionName + ";\n";
1357
1358 // Determine the signature of the actual inference function
1360 if (!doInferSignature.empty())
1361 doInferSignature += ", ";
1362 // one argument per shape parameter, even when several outputs share it
1363 std::unordered_set<std::string> signatureShapeParams;
1364 for (auto const &name : fOutputTensorNames) {
1365 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1366 doInferSignature += typeForOutput(GetTensorType(name)) + " *tensor_" + name + ",";
1367 if(isDynamic) {
1368 for (auto const &dim : GetDynamicTensorShape(name)) {
1369 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param) &&
1370 signatureShapeParams.insert(dim.param).second)
1371 doInferSignature += " size_t &" + dim.param + "_output,";
1372 }
1373 }
1374 }
1375 doInferSignature.back() = ' ';
1376
1377 doInferSignature = sessionName + " const &session, " + doInferSignature;
1378
1379 doInferSignature = "inline void doInfer(" + doInferSignature + ")";
1380
1381 // forward declare inference implementation
1382 fGC += doInferSignature + ";\n";
1383
1384 // define the Session struct
1385 fGC += "struct " + sessionName + " {\n";
1386
1387 // generate code for declaring the initialized tensors
1389
1391 // evaluate total intermediate memory and position intermediate tensor addresses
1392 std::string intermediate_memory_alloc_string = "";
1393 intermediate_memory_alloc_string += "\n// --- Positioning intermediate tensor memory --";
1394 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1395 if (fVerbose) {
1396 auto op = fOperators[op_idx].get();
1397 std::cout << "\n******************\n analyzing input/output operator " << op_idx << " "
1398 << typeid(*op).name() << std::endl;
1399 }
1402 }
1403
1404 // to check remaining unused fragments after memory allocation (lesser the better)
1405 // for (const auto &it: fIntermediateMemoryInfo.available_stack){
1406 // std::cout<<"chunk_idx: "<<it.first<<", chunk_size: "<<it.second<<"\n";
1407 // }
1408
1409 // generate the memory pool to be used by intermediate tensors
1411
1412 // position intermediate tensors
1414 }
1415
1416 // generate the declaring the intermediate tensors
1418 // generate code for declarations of some specific operators
1420
1421 // storing the parameters for future checking to avoid mismatches
1422 if (!fDimShapeNames.empty()) {
1423 fGC += "\n// dynamic shape parameters\n";
1425 std::sort(dimShapeNames.begin(), dimShapeNames.end());
1426 for (const auto &p : dimShapeNames) {
1427 fGC += "size_t " + memberNameForDimShape(p) + ";\n";
1428 }
1429 }
1430
1431 // add subgraph session
1432 if (!fSubGraphs.empty()) fGC += "// subgraph sessions\n";
1433 for (auto & graph : fSubGraphs) {
1434 fGC += "Session_" + graph->fName + " fSession_" + graph->fName + ";\n";
1435 }
1436
1437 // Generate code for Session constructor
1438 // add here specific operator code that needs to define session data members
1439 fGC += "\n";
1440 for (size_t id = 0; id < fOperators.size(); id++) {
1441 std::string opName = std::to_string(id);
1442 fGC += fOperators[id]->GenerateSessionMembersCode(opName);
1443 }
1444 fGC += "\n";
1445 // collect declaration of shape parameters (default values) and argument
1446 // forwarding for delegating constructors
1447 std::string dynParamDecls;
1448 std::string dynParamArgs;
1449 if (!fDimShapeNames.empty()) {
1450 // need to use same order as in infer function not alphabetical one
1451 for (auto &p : fDimShapeNames) {
1452 dynParamDecls += ",\n size_t " + p + " = " + fShapeParams[p];
1453 dynParamArgs += ", " + p;
1454 }
1455 }
1456 // here add initialization and reading of weight tensors
1458 // A Session is constructed from an in-memory safetensors blob; the
1459 // file-based constructor loads the payload fully into memory and
1460 // delegates. The temporary buffer is alive for the duration of the
1461 // delegated constructor call, and the blob constructor copies the
1462 // weights out of it.
1463 AddNeededHelperFunction("SafetensorsBlob");
1464 fGC += " static std::string LoadWeightsFromFile(const std::string &filename) {\n";
1465 fGC += " std::ifstream f(filename, std::ios::binary);\n";
1466 fGC += " if (!f.is_open()) {\n";
1467 fGC += " throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input "
1468 "weights\");\n";
1469 fGC += " }\n";
1470 fGC += " return std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());\n";
1471 fGC += " }\n\n";
1472 fGC += sessionName + "(std::string filename =\"" + fName + ".safetensors\"" + dynParamDecls + ")\n";
1473 fGC += " : " + sessionName + "(SafetensorsBlob{LoadWeightsFromFile(filename)}" + dynParamArgs + ") {}\n\n";
1474 fGC += sessionName + "(SafetensorsBlob weights_blob" + dynParamDecls + ") {\n";
1475 } else if (fUseWeightFile) {
1476 std::string fileName = fName + ".dat";
1477 fGC += sessionName + "(std::string filename =\"" + fileName + "\"" + dynParamDecls + ") {\n";
1478 } else {
1479 // no need to pass weight file since it is not used
1480 // keep passing a string for compatibility
1481 fGC += sessionName + "(std::string = \"\"" + dynParamDecls + ") {\n";
1482 }
1483
1484 // initializing dynamic parameters
1485 if (!fDimShapeNames.empty()) {
1486 fGC += "\n\n";
1487 std::sort(fDimShapeNames.begin(), fDimShapeNames.end());
1488 for (const auto &p : fDimShapeNames) {
1489 fGC += " " + memberNameForDimShape(p) + " = " + p + ";\n";
1490 }
1491 }
1492 // add some extra code needed for initialization of dynamic parameters
1494
1495 if (fUseWeightFile) {
1496 fGC += "\n//--- reading weights from file\n";
1498 fGC += "\n";
1499 // fUseWeightFile = fUseWeightFile;
1500 }
1501
1502 // now we have passed the parameters we can allocate the dynamic tensors
1504
1505 // add here initialization code for operator
1506 for (size_t id = 0; id < fOperators.size(); id++) {
1507 fGC += fOperators[id]->GenerateInitCode();
1508 }
1509
1510 fGC += "}\n\n";
1511
1513 // Models requested with a safetensors weight file but without weight
1514 // tensors also expose the blob constructor, which ignores the blob, so
1515 // that both construction modes exist (from a file and from an in-memory
1516 // blob) whatever the model.
1517 AddNeededHelperFunction("SafetensorsBlob");
1518 fGC += sessionName + "(SafetensorsBlob" + dynParamDecls + ")\n";
1519 fGC += " : " + sessionName + "(std::string{}" + dynParamArgs + ") {}\n\n";
1520 }
1521
1522 // Used to build the tangent Session objects needed to differentiate the
1523 // generated code with Clad: the derivatives of the (constant) weights
1524 // are zero, but a default-constructed Session holds the actual values.
1525 fGC += "// Set all weight and constant tensors to zero. This is useful to create\n"
1526 "// the tangent Session objects needed to differentiate the generated code\n"
1527 "// with Clad.\n"
1528 "void SetWeightsToZero() {\n";
1529 for (auto &i : fInitializedTensors) {
1530 // IsNotWritable tensors have no emitted fTensor_ member, and integer
1531 // tensors are structural (indices, shapes) that carry no tangent.
1532 if (i.second.IsNotWritable() ||
1533 (i.second.type() != ETensorType::FLOAT && i.second.type() != ETensorType::DOUBLE))
1534 continue;
1535 fGC += " for (auto &v : fTensor_" + i.first + ") v = 0;\n";
1536 }
1537 for (auto &graph : fSubGraphs) {
1538 fGC += " fSession_" + graph->fName + ".SetWeightsToZero();\n";
1539 }
1540 fGC += "}\n\n";
1541
1542 // generate the inference overload that returns an output struct
1544
1545 // end of session
1546 fGC += "}; // end of Session\n\n";
1547
1549
1550 fGC += doInferSignature + " {\n";
1551 fGC += "\n";
1552
1553 // generate the inference code
1554 if (fVerbose)
1555 std::cout << "Generating main inference code for " << fName << std::endl;
1556
1557 if (fOutputTensorNames.size() == 0)
1558 throw std::runtime_error("TMVA-SOFIE: output size=0 are not supported");
1559
1560 std::string allOperatorCode;
1561
1562 for (size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx) {
1563 if (fVerbose)
1564 std::cout << "Generating code for operator .... " << op_idx << std::endl;
1565 std::string operatorCode = fOperators[op_idx]->Generate(std::to_string(op_idx));
1567 }
1568
1569 // If the generated code users members of the session struct, use the
1570 // local variable name that we're using for the session:
1571 ReplaceAll(allOperatorCode, "this->", "session.");
1572
1573 // Collect all "tensor_*" data members that are not input or output tensors
1575 const std::string prefix = "tensor_";
1576 for (auto const& name: tensorMemberNames) {
1577 // alias tensors are not session members: the operator creating them declares a local pointer
1578 if (IsAliasTensor(name.substr(prefix.size())))
1579 continue;
1580 fGC += " auto &" + name + " = session." + name + ";\n";
1581 }
1582 fGC += "\n";
1583
1584 // an initialized tensor (constant, or a weight reached e.g. through an Identity) that is a
1585 // model output is not written by any operator: copy it into the output buffer before the
1586 // operator code, so that operators reading the weight also see its value (the output
1587 // parameter shadows the session member in doInfer)
1588 for (auto const &name : fOutputTensorNames) {
1590 std::string t = "session.tensor_" + name;
1592 fGC += " std::copy(" + t + ", " + t + " + " + std::to_string(length) + ", tensor_" + name + ");\n";
1593 }
1594 }
1595
1597
1598 std::unordered_set<std::string> assignedShapeParams;
1599 for (auto const& name: fOutputTensorNames) {
1600 bool isDynamic = fDynamicTensorInfos.count(name) > 0;
1601 if(isDynamic) {
1602 for (auto const &dim : GetDynamicTensorShape(name)) {
1603 if (dim.isParam && !IsInputTensorShapeParam(dim.param) && IsIdentifier(dim.param) &&
1604 assignedShapeParams.insert(dim.param).second)
1605 fGC += " " + dim.param + "_output = " + dim.param + ";\n";
1606 }
1607 }
1608 }
1609 fGC += "\n";
1610
1611 fGC += "}\n";
1612}
1613
1614void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, bool verbose)
1615{
1616 fVerbose = verbose;
1617 fBatchSize = batchSize;
1618
1619 // weights are contained in the generated header when kNoWeightFile is used
1620 if (static_cast<std::underlying_type_t<Options>>(Options::kNoWeightFile) & options) {
1621 fUseWeightFile = false;
1623 }
1624 if (static_cast<std::underlying_type_t<Options>>(Options::kSafetensorsWeightFile) & options) {
1625 fUseWeightFile = true;
1627 }
1628
1629 // initialize the model including all operators and sub-graphs
1630 Initialize(batchSize, verbose);
1631
1632 std::string hgname;
1633 if (!fIsSubGraph) {
1634 fGC.clear();
1636 }
1637
1638 // generate first code for the subgraphs
1639 for (auto &graph : fSubGraphs) {
1640 if (fVerbose)
1641 std::cout << "generate session code for subgraph " << graph->fName << std::endl;
1642 graph->GenerateSessionCode();
1643 fGC += graph->fGC;
1644 }
1645
1646 if (fVerbose)
1647 std::cout << "generate Main session code - model " << fName << std::endl;
1648
1649 // generate main session code
1651
1652 if (!fIsSubGraph) {
1653 fGC += ("} //TMVA_SOFIE_" + fName + "\n");
1654 fGC += "\n#endif // " + hgname + "\n";
1655 // dump the standalone definitions of the helper functions this model uses
1656 // so that the generated header does not depend on TMVA/SOFIE_common.hxx
1658 }
1659}
1660
1662 // generate the code to read initialized tensors from a text data file
1664 // check if there are tensors to write
1665
1666 if (!fUseWeightFile) return;
1667
1668 fGC += " std::ifstream f;\n";
1669 fGC += " f.open(filename);\n";
1670 fGC += " if (!f.is_open()) {\n";
1671 fGC += " throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input weights\");\n";
1672 fGC += " }\n";
1673
1674 // ReadTensorFromStream is emitted as a standalone helper in the header
1675 AddNeededHelperFunction("ReadTensorFromStream");
1676
1677 // loop on tensors and parse the file
1678 for (auto& i: fInitializedTensors) {
1679 // skip Constant and shape tensors (not written in a file)
1680 if (!i.second.IsWeightTensor()) continue;
1681 std::string tensor_name = "tensor_" + i.first;
1682 if (i.second.type() == ETensorType::FLOAT) {
1683 std::string length = std::to_string(ConvertShapeToLength(i.second.shape()));
1684 fGC += " ReadTensorFromStream(f, " + tensor_name + ", \"" + tensor_name + "\", " + length + ");\n";
1685 } else {
1686 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be read from a file");
1687 }
1688 }
1689 fGC += " f.close();\n";
1690 }
1691
1692 // generate the code to read initialized tensors from a safetensors blob
1693 // in memory (the blob comes from a file or directly from the caller)
1695 // the SafetensorsBlob/SafetensorsReader helpers are emitted as
1696 // standalone helpers in the generated header
1697 AddNeededHelperFunction("SafetensorsReader");
1698
1699 fGC += " SafetensorsReader sofie_weights_reader(weights_blob);\n";
1700
1701 for (auto &i : fInitializedTensors) {
1702 // skip Constant and shape tensors (not written in a file)
1703 if (!i.second.IsWeightTensor())
1704 continue;
1705 std::string tensor_name = "tensor_" + i.first;
1706 std::string dtype;
1707 try {
1708 dtype = SafetensorsDType(i.second.type());
1709 } catch (const std::runtime_error &) {
1710 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " +
1711 ConvertTypeToString(i.second.type()) +
1712 " cannot be read from a safetensors payload");
1713 }
1714 std::string length = std::to_string(ConvertShapeToLength(i.second.shape()));
1715 fGC += " sofie_weights_reader.Read(\"" + tensor_name + "\", fTensor_" + i.first + ", " + length + ", \"" +
1716 dtype + "\");\n";
1717 }
1718 }
1719}
1720
1722 // Determine the file extension based on the weight file type
1723 std::string fileExtension;
1724 switch (fWeightFile) {
1726 fileExtension = ".dat";
1727 break;
1729 fileExtension = ".safetensors";
1730 break;
1732 fileExtension = ".dat";
1733 break;
1734 }
1735
1736 // If filename is empty, use the model name as the base filename
1737 if (filename.empty()) {
1739 }
1740
1741 // Write the initialized tensors to the file
1743 std::ofstream f(filename, std::ios::binary);
1744 if (!f.is_open())
1745 throw std::runtime_error("tmva-sofie failed to open file " + filename + " for tensor weight data");
1746
1748
1749 long curr_pos = f.tellp();
1750 f.close();
1751 return curr_pos;
1752 } else if (fWeightFile == WeightFileType::Text) {
1753 std::ofstream f;
1754 f.open(filename);
1755 if (!f.is_open())
1756 throw
1757 std::runtime_error("tmva-sofie failed to open file " + filename + " for tensor weight data");
1758 for (auto& i: fInitializedTensors) {
1759 // skip Constant tensors and not writable tensors (e.g. shape tensors)
1760 if (!i.second.IsWeightTensor()) {
1761 continue;
1762 }
1763 size_t length = ConvertShapeToLength(i.second.shape());
1764 std::string tensor_name = "tensor_" + i.first;
1765 f << tensor_name << " " << length << "\n";
1766 if (i.second.type() == ETensorType::FLOAT) {
1767 const float * data = i.second.data<float>();
1768 for (size_t idx = 0; idx < length; idx++) {
1769 // round to zero sub-normal values
1770 float value = data[idx];
1771 if (value != 0. && std::abs(value) < std::numeric_limits<float>::min() ) value = 0;
1772 // handle non-finite values explicitly
1773 if (std::isinf(value))
1774 f << (value > 0 ? "inf" : "-inf");
1775 else if (std::isnan(value))
1776 f << "nan";
1777 else
1778 f << std::setprecision(std::numeric_limits<float>::max_digits10) << value;
1779 f << ( (idx < length-1) ? " " : "\n" );
1780 }
1781 }
1782 else {
1783 throw std::runtime_error("tmva-sofie tensor " + tensor_name + " with type " + ConvertTypeToString(i.second.type()) + " cannot be written to a file");
1784 }
1785 if (f.fail())
1786 throw std::runtime_error("tmva-sofie failed to write tensor data to file for " + tensor_name);
1787 }
1788 long curr_pos = f.tellp();
1789 f.close();
1790 return curr_pos;
1791 } else {
1792 return -1;
1793 }
1794}
1795
1797{
1798 // safetensors payloads are little-endian by specification; refuse to
1799 // write silently corrupted output on a big-endian host
1800 const std::uint16_t one = 1;
1801 if (!*reinterpret_cast<const std::uint8_t *>(&one))
1802 throw std::runtime_error("tmva-sofie: safetensors weights can only be written on a little-endian host");
1803
1804 // The safetensors layout (https://huggingface.co/docs/safetensors):
1805 // 8 bytes: little-endian unsigned size N of the JSON header
1806 // N bytes: JSON header {name: {"dtype", "shape", "data_offsets"}}
1807 // rest: the raw little-endian tensor payloads, concatenated;
1808 // data_offsets are relative to the start of this region
1809 std::uint64_t offset = 0;
1810 // sort the tensor names for a reproducible output
1811 std::vector<std::string> names;
1812 for (const auto &item : fInitializedTensors) {
1813 // skip Constant tensors and not writable tensors (e.g. shape tensors)
1814 if (item.second.IsWeightTensor())
1815 names.push_back(item.first);
1816 }
1817 std::sort(names.begin(), names.end());
1818 std::string headerStr = "{";
1819 for (size_t idx = 0; idx < names.size(); ++idx) {
1820 const auto &tensor = fInitializedTensors.at(names[idx]);
1821 const std::uint64_t nbytes = ConvertShapeToLength(tensor.shape()) * GetTypeSize(tensor.type());
1822 if (idx > 0)
1823 headerStr += ",";
1824 headerStr += "\"" + JsonEscape("tensor_" + names[idx]) + "\":{\"dtype\":\"" + SafetensorsDType(tensor.type()) +
1825 "\",\"shape\":[";
1826 for (size_t i = 0; i < tensor.shape().size(); ++i) {
1827 if (i > 0)
1828 headerStr += ",";
1829 headerStr += std::to_string(tensor.shape()[i]);
1830 }
1831 headerStr += "],\"data_offsets\":[" + std::to_string(offset) + "," + std::to_string(offset + nbytes) + "]}";
1832 offset += nbytes;
1833 }
1834 headerStr += "}";
1835 // 8-byte little-endian header length
1836 const std::uint64_t headerSize = headerStr.size();
1837 char sizestr[8];
1838 for (int i = 0; i < 8; ++i)
1839 sizestr[i] = static_cast<char>((headerSize >> (8 * i)) & 0xff);
1840 f.write(sizestr, 8);
1841 f.write(headerStr.data(), headerStr.size());
1842 for (const auto &name : names) {
1843 const auto &tensor = fInitializedTensors.at(name);
1844 const std::uint64_t nbytes = ConvertShapeToLength(tensor.shape()) * GetTypeSize(tensor.type());
1845 f.write(reinterpret_cast<const char *>(tensor.data<void>()), nbytes);
1846 }
1847 if (f.fail())
1848 throw std::runtime_error("tmva-sofie failed to write safetensors payload");
1849}
1850
1852{
1853 std::ostringstream buffer;
1855 return buffer.str();
1856}
1857
1859 std::cout << "Summary of model " << GetName() << std::endl;
1860 for(size_t op_idx = 0; op_idx < fOperators.size(); ++op_idx){
1861 auto& r = *fOperators[op_idx].get();
1862 std::string raw_name = typeid(r).name();
1863 // look for ROperator_NAME
1864 std::string name = raw_name.substr(raw_name.find("ROperator_")+10, raw_name.size());
1865 std::cout << op_idx << " " << name << " : ";
1866 for (auto & t_in : r.GetOpInputTensors()) std::cout << t_in << " ";
1867 std::cout << " ----> ";
1868 for (auto & t_out : r.GetOpOutputTensors()) std::cout << t_out << " ";
1869 std::cout << std::endl;
1870 }
1871}
1872
1873/// To emit the dimensions of the input tensors as a data member of a session,
1874/// which is helpful when validating the inference inputs.
1876{
1877 fGC += "\n// Input tensor dimensions\n";
1878 // SingleDim / TensorDims / makeDims are emitted as standalone helpers
1879 AddNeededHelperFunction("InputTensorDims");
1880 bool hasDynamicInputTensors = false;
1881
1882 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1883 auto const &name = fInputTensorNames[iInput];
1884 if (IsDimInputTensor(name)) {
1886 }
1887 std::vector<Dim> shape = GetDimTensorShape(name);
1888 fGC += "constexpr std::array<SingleDim, " + std::to_string(shape.size()) + "> dim_" + name + "{";
1889 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1890 auto const &dim = shape[iDim];
1891 if (dim.isParam) {
1892 fGC += "SingleDim{\"" + dim.GetVal() + "\"}";
1893 } else {
1894 fGC += "SingleDim{" + dim.GetVal() + "}";
1895 }
1896 if (iDim != shape.size() - 1) {
1897 fGC += ", ";
1898 }
1899 }
1900 fGC += "};\n";
1901 }
1902 fGC += "\nconstexpr std::array<TensorDims, " + std::to_string(fInputTensorNames.size()) + "> inputTensorDims{\n";
1903 for (std::size_t iInput = 0; iInput < fInputTensorNames.size(); ++iInput) {
1904 auto const &name = fInputTensorNames[iInput];
1905 fGC += SP + "makeDims(dim_" + name + ")";
1906 if (iInput == fInputTensorNames.size() - 1) {
1907 fGC += "\n";
1908 } else {
1909 fGC += ",\n";
1910 }
1911 }
1912 fGC += "};\n";
1913
1914 fGC +=
1915 "\nconstexpr bool hasDynamicInputTensors{" + std::string{hasDynamicInputTensors ? "true" : "false"} + "};\n\n";
1916
1917 fGC += "\n// Output tensor dimensions\n";
1918 bool hasDynamicOutputTensors = false;
1919 for (std::size_t iOutput = 0; iOutput < fOutputTensorNames.size(); ++iOutput) {
1920 auto const &name = fOutputTensorNames[iOutput];
1921 if (IsDynamicTensor(name)) {
1923 }
1924 std::vector<Dim> shape = GetDimTensorShape(name);
1925 fGC += "constexpr std::array<SingleDim, " + std::to_string(shape.size()) + "> dim_" + name + "{";
1926 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1927 auto const &dim = shape[iDim];
1928 if (dim.isParam) {
1929 fGC += "SingleDim{\"" + dim.GetVal() + "\"}";
1930 } else {
1931 fGC += "SingleDim{" + dim.GetVal() + "}";
1932 }
1933 if (iDim != shape.size() - 1) {
1934 fGC += ", ";
1935 }
1936 }
1937 fGC += "};\n";
1938 }
1939 fGC += "\nconstexpr std::array<TensorDims, " + std::to_string(fOutputTensorNames.size()) + "> outputTensorDims{\n";
1940 for (std::size_t iOutput = 0; iOutput < fOutputTensorNames.size(); ++iOutput) {
1941 auto const &name = fOutputTensorNames[iOutput];
1942 fGC += SP + "makeDims(dim_" + name + ")";
1943 if (iOutput == fOutputTensorNames.size() - 1) {
1944 fGC += "\n";
1945 } else {
1946 fGC += ",\n";
1947 }
1948 }
1949 fGC += "};\n";
1950 fGC +=
1951 "\nconstexpr bool hasDynamicOutputTensors{" + std::string{hasDynamicOutputTensors ? "true" : "false"} + "};\n\n";
1952}
1953
1955 std::cout << "Model requires following inputs:\n";
1956 for (auto& inputInfo: fInputTensorInfos) {
1957 std::cout << "Parametrised Tensor name: " << inputInfo.first << "\t";
1958 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1959 std::cout << "shape: [";
1960 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1961 if (inputInfo.second.shape[i].isParam) {
1962 std::cout << inputInfo.second.shape[i].param;
1963 } else {
1964 std::cout << inputInfo.second.shape[i].dim ;
1965 }
1966 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1967 }
1968 std::cout << "]" << std::endl;
1969 }
1970
1971 for (auto& inputInfo: fReadyInputTensorInfos) {
1972 std::cout << "Fully Specified Tensor name: " << inputInfo.first << "\t";
1973 std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
1974 std::cout << "shape: [";
1975 for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1976 std::cout << inputInfo.second.shape[i];
1977 if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
1978 }
1979 std::cout << "]" << std::endl;
1980 }
1981 std::cout << "\n";
1982}
1983
1985 std::cout << "Model initialized the following tensors:\n";
1986 for (auto& it: fInitializedTensors) {
1987 std::cout << "Tensor name: \"" << it.first << "\"\t";
1988 std::cout << "type: " << ConvertTypeToString(it.second.type()) << "\t";
1989 std::cout << "shape: [";
1990 for (size_t i = 0; i < it.second.shape().size(); i++) {
1991 std::cout << it.second.shape()[i];
1992 if (i < it.second.shape().size() - 1) std::cout << ",";
1993 }
1994 std::cout << "]";
1995 if (it.second.IsConstantTensor()) std::cout << " (Constant)";
1996 if (it.second.IsNotWritable()) std::cout << " (Not Writable)";
1997 std::cout << std::endl;
1998 }
1999 std::cout << "\n";
2000}
2001
2003 std::cout << "Model specify the following intermediate tensors:\n";
2004 for (auto& it: fIntermediateTensorInfos) {
2005 std::cout << "Tensor name: \"" << it.first << "\"\t";
2006 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
2007 std::cout << "shape: [";
2008 for (size_t i = 0; i < it.second.shape.size(); i++) {
2009 std::cout << it.second.shape[i];
2010 if (i < it.second.shape.size() - 1) std::cout << ",";
2011 }
2012 std::cout << "]" << std::endl;
2013 }
2014 std::cout << "\n";
2015}
2016
2018 std::cout << "Model specify the following dynamic tensors:\n";
2019 for (auto& it: fDynamicTensorInfos) {
2020 std::cout << "Tensor name: \"" << it.first << "\"\t";
2021 std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
2022 std::cout << "shape: [";
2023 for (size_t i = 0; i < it.second.shape.size(); i++) {
2024 std::cout << it.second.shape[i].GetVal();
2025 if (i < it.second.shape.size() - 1) std::cout << ",";
2026 }
2027 std::cout << "]" << std::endl;
2028 }
2029 std::cout << "\n";
2030}
2031
2033 std::cout << "Model specify the following output tensors:\n";
2034 for (auto& it: fOutputTensorNames) {
2035 std::cout << "Tensor name: \"" << it << "\"\t";
2036 try {
2037 auto shape = GetDimTensorShape(it);
2038 std::cout << "with shape: " << ConvertDimShapeToString(shape) << std::endl;
2039 } catch (...) {
2040 std::cout << "with shape not yet defined" << std::endl;
2041 }
2042 }
2043 std::cout << "\n";
2044}
2045
2047 auto it = fInitializedTensors.find(name);
2048 if (it == fInitializedTensors.end()) {
2049 std::cout << "Tensor " << name << " not found in model's initialized tensor list" << std::endl;
2050 return;
2051 }
2052
2053 std::cout << "Tensor name: " << it->first << "\t";
2054 std::cout << "type: " << ConvertTypeToString(it->second.type()) << "\t";
2055 int length =1;
2056 std::cout << "shape: [";
2057 for (size_t i = 0; i < it->second.shape().size(); i++) {
2058 std::cout << it->second.shape()[i];
2059 length *= it->second.shape()[i];
2060 if (i < it->second.shape().size() - 1) std::cout << ",";
2061 }
2062 std::cout << "]" << std::endl;
2063 bool ellipsis = true;
2064 if (n_print > length) {
2065 n_print = length;
2066 ellipsis = false;
2067 }
2068
2069 std::cout << "data: [" << std::endl;
2070 if (it->second.type() == ETensorType::FLOAT) {
2071 auto converted_data = it->second.data<float>();
2072 for (int i =0; i < n_print; i++) {
2073 std::cout << converted_data[i];
2074 if (i < n_print - 1) std::cout << " ,";
2075 }
2076 }
2077 if (ellipsis) std::cout << ", ...";
2078 std::cout << "]" << std::endl;
2079
2080}
2081
2083 fGC += ("//Code generated automatically by TMVA for Inference of Model file [" + fFileName + "] at [" + fParseTime.substr(0, fParseTime.length()-1) +"] \n");
2084 // add header guards
2085 hgname = fName;
2086 std::transform(hgname.begin(), hgname.end(), hgname.begin(), [](unsigned char c) {
2087 return std::toupper(c);
2088 } );
2089 hgname = "ROOT_TMVA_SOFIE_" + hgname;
2090 fGC += "\n#ifndef " + hgname + "\n";
2091 fGC += "#define " + hgname + "\n\n";
2092 // Standard library headers the generated code relies on. Listed explicitly
2093 // now that they are no longer pulled in transitively via SOFIE_common.hxx.
2094 for (const char *h : {"cstdint", "cstring", "string", "vector", "map", "memory", "sstream", "iostream", "iomanip",
2095 "limits", "stdexcept", "algorithm", "cmath", "cassert"}) {
2096 fNeededStdLib.insert(h);
2097 }
2098 for (auto& i: fNeededStdLib) {
2099 fGC += "#include <" + i + ">\n";
2100 }
2101 for (auto& i: fCustomOpHeaders) {
2102 fGC += "#include \"" + i + "\"\n";
2103 }
2104 // Placeholder for the #include directives needed by the embedded helper
2105 // functions (filled in by EmitHelperFunctionsCode).
2107 if (fUseWeightFile)
2108 fGC += "#include <fstream>\n";
2109 // the safetensors file constructor buffers the payload with
2110 // std::istreambuf_iterator
2112 fGC += "#include <iterator>\n";
2113
2114 fGC += "\nnamespace TMVA_SOFIE_" + fName + "{\n";
2115 if (!fNeededBlasRoutines.empty()) {
2116 fGC += ("namespace BLAS{\n");
2117 for (auto &routine : fNeededBlasRoutines) {
2118 if (routine == "Gemm") {
2119 fGC += ("\textern \"C\" void sgemm_(const char * transa, const char * transb, const int * m, const int * n, const int * k,\n"
2120 "\t const float * alpha, const float * A, const int * lda, const float * B, const int * ldb,\n"
2121 "\t const float * beta, float * C, const int * ldc);\n");
2122 // sgemm_ now declared; the standalone Gemm_Call helper will skip its copy.
2123 fBlasSgemmDeclared = true;
2124 } else if (routine == "Gemv") {
2125 fGC += ("\textern \"C\" void sgemv_(const char * trans, const int * m, const int * n, const float * alpha, const float * A,\n"
2126 "\t const int * lda, const float * X, const int * incx, const float * beta, const float * Y, const int * incy);\n");
2127 } else if (routine == "Axpy") {
2128 fGC += ("\textern \"C\" void saxpy_(const int * n, const float * alpha, const float * x,\n"
2129 "\t const int * incx, float * y, const int * incy);\n");
2130 } else if (routine == "Copy") {
2131 fGC += ("\textern \"C\" void scopy_(const int *n, const float* x, const int *incx, float* y, const int* incy);\n");
2132 }
2133 }
2134 fGC += ("}//BLAS\n");
2135 }
2136 // Placeholder for the standalone definitions of the inference helper
2137 // functions used by this model (filled in by EmitHelperFunctionsCode). It
2138 // sits inside the generated model namespace, right before the session code.
2140}
2141
2143{
2144 HelperFunctionsCode code =
2146
2147 auto replaceMarker = [this](const std::string &marker, const std::string &replacement) {
2148 auto pos = fGC.find(marker);
2149 if (pos != std::string::npos) {
2150 fGC.replace(pos, marker.size(), replacement);
2151 }
2152 };
2153
2156
2157 // Clad derivatives live at file scope and reference the model's helpers, so
2158 // insert them after the whole model namespace, just before the include guard.
2159 if (!code.cladDefinitions.empty()) {
2160 auto pos = fGC.rfind("#endif");
2161 if (pos != std::string::npos) {
2162 fGC.insert(pos, code.cladDefinitions + "\n");
2163 } else {
2164 fGC += code.cladDefinitions;
2165 }
2166 }
2167}
2168
2169void RModel::OutputGenerated(std::string filename, bool append) {
2170
2171 // the model can be appended only if a file name is provided
2172 if (filename.empty()) {
2173 // if a file is pr
2174 filename = fName + ".hxx";
2175 append = false;
2176 }
2177 std::ofstream f;
2178 if (append)
2179 f.open(filename, std::ios_base::app);
2180 else
2181 f.open(filename);
2182 if (!f.is_open()) {
2183 throw std::runtime_error("tmva-sofie failed to open file for output generated inference code");
2184 }
2185 f << fGC;
2186 f.close();
2187
2188 // write weights in a separate weight file
2189 if (fUseWeightFile) {
2190 const std::string extension = fWeightFile == WeightFileType::Safetensors ? ".safetensors" : ".dat";
2191 if (!filename.empty()) {
2192 size_t pos = filename.find(".hxx");
2193 if (pos != std::string::npos)
2194 filename.replace(pos, 4, extension);
2195 else
2197 } else {
2199 }
2201 }
2202}
2203
2204} // 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 h(i)
Definition RSha256.hxx:106
#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 offset
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:142
const_iterator begin() const
const_iterator end() const
std::string WriteInitializedTensorsToBuffer()
Definition RModel.cxx:1851
void AddShapeParam(const std::string &name, size_t def_value=0)
Definition RModel.cxx:416
void AddNeededHelperFunction(std::string name)
Definition RModel.hxx:327
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:108
std::set< std::string > fNeededHelperFunctions
Definition RModel.hxx:70
std::unordered_set< std::string > fComputedShapeParams
! shape parameters computed at run time by an operator
Definition RModel.hxx:101
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:144
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
Definition RModel.hxx:98
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:367
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:382
bool AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name)
Definition RModel.cxx:306
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:1181
std::unordered_set< std::string > fNeededBlasRoutines
Definition RModel.hxx:58
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:201
void GenerateHeaderInfo(std::string &hgname)
Definition RModel.cxx:2082
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:1875
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:2169
std::unordered_map< std::string, std::string > fAliasTensors
Definition RModel.hxx:102
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:212
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:97
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
Definition RModel.cxx:436
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
Definition RModel.hxx:95
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:288
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:399
std::vector< std::string > fDimShapeNames
Definition RModel.hxx:103
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:268
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
Definition RModel.hxx:122
void AddBlasRoutines(std::vector< std::string > routines)
Definition RModel.hxx:310
void AddInputTensorName(std::string name)
Definition RModel.cxx:231
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:104
void AddNeededStdLib(std::string libname)
Definition RModel.hxx:316
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:372
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:341
static constexpr const char * kHelperIncludesMarker
Definition RModel.hxx:80
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:354
std::unordered_set< std::string > fNeededStdLib
Definition RModel.hxx:64
bool IsAliasTensor(const std::string &name) const
check if a tensor is a alias tensor
Definition RModel.cxx:345
static constexpr const char * kHelperFunctionsMarker
Definition RModel.hxx:81
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
Definition RModel.cxx:581
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:235
RModel()=default
Default constructor.
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:2046
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:358
void WriteInitializedTensorsToStream(std::ostream &os)
Definition RModel.cxx:1796
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:662
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1721
OptimizationLevel fOptimizationLevel
Definition RModel.hxx:92
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:1123
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
Definition RModel.cxx:155
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:94
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:459
void AddComputedShapeParam(const std::string &name)
Declare a shape parameter as computed at run time by an operator (e.g.
Definition RModel.cxx:427
MemoryPoolInfo fIntermediateMemoryInfo
! intermediate memory info (transient)
Definition RModel.hxx:121
std::string AllocateIntermediateMemory(std::span< const std::string_view > op_output_tensors)
Definition RModel.cxx:476
std::unordered_map< std::string, std::pair< std::vector< Dim >, bool > > fShapeTensors
Definition RModel.hxx:99
std::vector< std::unique_ptr< ROperator, ROperatorDeleter > > fOperators
Definition RModel.hxx:115
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:818
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:100
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:468
const std::string & GetName() const
Definition RModel.hxx:308
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:169
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:105
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
Definition RModel.hxx:96
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:450
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, bool verbose=false)
Definition RModel.cxx:1614
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:349
std::vector< std::shared_ptr< RModel > > fSubGraphs
! sub-graph models (transient)
Definition RModel.hxx:117
bool IsReadyInputTensor(const std::string &name) const
Definition RModel.cxx:376
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:443
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:298
std::unordered_set< std::string > fCustomOpHeaders
Definition RModel.hxx:65
bool IsInputTensorShapeParam(std::string const &name) const
Check if a given parameter is used for the shape of an input tensor.
Definition RModel.cxx:1105
const Int_t n
Definition legend1.C:16
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:853
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
HelperFunctionsCode GenerateHelperFunctionsCode(const std::set< std::string > &neededHelpers, const std::string &modelNamespace, bool sgemmAlreadyDeclared=false)
Return the standalone C++ source of the inference helper functions requested in neededHelpers (see RM...
std::underlying_type_t< Options > operator|(Options opA, Options opB)
Definition RModel.cxx:101
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
std::string ConvertValToString(T value)
if(fPos !=fText.size()) Fail("unexpected trailing content")
Source code of the inference helper functions to embed in generated code so that it is standalone and...
std::string definitions
function/type definitions to place inside the generated model namespace
std::string cladDefinitions
Clad custom-derivative definitions to place at file scope (outside the model namespace) so that Clad ...
std::string includes
#include directives to place in the header preamble
std::map< size_t, TensorMemoryInfo > total_stack
std::map< size_t, size_t > available_stack