Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModel.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_RMODEL
2#define TMVA_SOFIE_RMODEL
3
5
6#include "Rtypes.h" // for ClassDefNV
7
8#include <ctime>
9#include <fstream>
10#include <iomanip>
11#include <iostream>
12#include <memory>
13#include <set>
14#include <sstream>
15#include <type_traits>
16#include <unordered_map>
17#include <unordered_set>
18#include <vector>
19
21
22enum class Options {
23 kDefault = 0x0,
24 kNoWeightFile = 0x2,
25 // Write the weight tensors in the binary safetensors format
26 // (https://huggingface.co/docs/safetensors) instead of the text format
28};
29
30// Optimization levels inspired by ONNXRuntime.
31// We only get Operator Fusion with the Basic, and
32// memory reuse with Extended. kExtended is enabled
33// by default
35 kBasic = 0x0,
36 kExtended = 0x1,
37};
38
40
41std::underlying_type_t<Options> operator|(Options opA, Options opB);
42std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB);
43
44// The ROperator interface is an implementation detail of the code generation
45// and deliberately not exposed to the public: only this forward declaration
46// is visible here, the full definition (TMVA/ROperator.hxx) is a private
47// header of the SOFIE libraries.
48class ROperator;
49
50class RModel final {
51
52private:
53 std::string fFileName; // file name of original model file for identification
54 std::string fParseTime; // UTC date and time string at parsing
55
57
58 std::unordered_set<std::string> fNeededBlasRoutines;
59 // Set to true once GenerateHeaderInfo has emitted the extern "C" declaration
60 // of the BLAS sgemm_ routine (from fNeededBlasRoutines). It lets the
61 // standalone Gemm_Call helper skip emitting a second, duplicate declaration.
62 bool fBlasSgemmDeclared = false;
63
64 std::unordered_set<std::string> fNeededStdLib = {"vector"};
65 std::unordered_set<std::string> fCustomOpHeaders;
66
67 // Inference helper functions (from SOFIE_common) that the generated code
68 // needs. Their standalone definitions are emitted into the generated header
69 // so that it does not depend on including TMVA/SOFIE_common.hxx.
70 std::set<std::string> fNeededHelperFunctions;
71
72 std::string fName = "UnnamedModel";
73 std::string fGC; // generated code
74 bool fUseWeightFile = true;
75
76 // Placeholder tokens emitted by GenerateHeaderInfo and later replaced by
77 // EmitHelperFunctionsCode with the actual helper includes / definitions.
78 // This two-step approach is needed because the full set of required helpers
79 // is only known once all operators (and sub-graphs) have been generated.
80 static constexpr const char *kHelperIncludesMarker = "//@SOFIE_HELPER_INCLUDES@\n";
81 static constexpr const char *kHelperFunctionsMarker = "//@SOFIE_HELPER_FUNCTIONS@\n";
82
83 bool fIsInitialized = false;
84 bool fIsSubGraph = false;
85 bool fUseVDT = false;
86 int fVerbose = 0;
87 int fBatchSize = -1;
88 size_t fConstantTensorSize = 0; // size (in Bytes) of the allocated constant tensors
89 size_t fWeightsTensorSize = 0; // size (in Bytes) of the allocated weight tensors
90 size_t fOtherTensorSize = 0; // size (in Bytes) of intermediate tensors which are not managed by the memory pool
91
93
94 std::unordered_map<std::string, InputTensorInfo> fInputTensorInfos; // input tensors where shape may not fully defined or other graph inputs?
95 std::unordered_map<std::string, TensorInfo> fReadyInputTensorInfos; // input tensors where shape is full defined
96 std::unordered_map<std::string, InitializedTensor> fInitializedTensors;
97 std::unordered_map<std::string, TensorInfo> fIntermediateTensorInfos;
98 std::unordered_map<std::string, DynamicTensorInfo> fDynamicTensorInfos;
99 std::unordered_map<std::string, std::pair<std::vector<Dim>, bool>> fShapeTensors; // constant tensors describing a shape
100 std::unordered_map<std::string, std::string> fShapeParams; // parameters defining the dynamic shape (e.g. batch size), store also its default value
101 std::unordered_set<std::string> fComputedShapeParams; ///<! shape parameters computed at run time by an operator
102 std::unordered_map<std::string, std::string> fAliasTensors; // list of alias tensors
103 std::vector<std::string> fDimShapeNames; // parameter names used to define the shapes
104 std::vector<std::string> fOutputTensorNames;
105 std::vector<std::string> fInputTensorNames; // input tensor names using ONNX order
106
107 // A bare std::unique_ptr<ROperator> would require the complete ROperator
108 // type wherever a destroyed RModel is instantiated; with the
109 // out-of-line-deleter declared here and defined in RModel.cxx the
110 // forward declaration above is enough, so the ROperator interface can stay
111 // private.
113 void operator()(ROperator *ptr) const;
114 };
115 std::vector<std::unique_ptr<ROperator, ROperatorDeleter>> fOperators;
116
117 std::vector<std::shared_ptr<RModel>> fSubGraphs; ///<! sub-graph models (transient)
118 RModel * fParentGraph = nullptr;
119
120 // memory pool information for intermediate tensors
121 MemoryPoolInfo fIntermediateMemoryInfo; ///<! intermediate memory info (transient)
122 std::unordered_map<std::string_view, size_t> fIntermediateTensorFrequencyLookup; ///<! lookup table for intermediate tensor frequency (transient)
123
124 std::string fExtraCodeForDimShapes; // extra code needed for initialization of dynamic parameters (e.g. number of non zero elements in NonZero operator)
125
126 /// Shape parameters of a dynamic tensor that an operator computes at run time and
127 /// that are Session constructor arguments
128 std::vector<std::string> RuntimeBoundParams(const std::string &name) const;
129
130public:
131 /**
132 Default constructor. Needed to allow serialization of ROOT objects. See
133 https://root.cern/manual/io_custom_classes/#restrictions-on-types-root-io-can-handle
134 */
135 RModel() = default;
136 RModel(std::string name, std::string parsedtime) : fFileName(std::move(name)), fParseTime(std::move(parsedtime))
137 {
138 fName = fFileName.substr(0, fFileName.rfind("."));
140 }
141
142 // Defined out of line because ROperator is an incomplete type in this
143 // header (the definition is a private implementation header).
147 RModel(RModel const &) = delete;
148 RModel &operator=(RModel const &) = delete;
149
150 int Verbose() const { return fVerbose;}
151
152 std::vector<size_t> GetTensorShape(const std::string & name) const;
153 std::vector<Dim> GetDimTensorShape(const std::string & name) const;
154 std::vector<Dim> GetDynamicTensorShape(const std::string & name) const ;
155
156 // get the values for the tensor representing a shape
157 const std::vector<Dim> & GetShapeTensorValues(const std::string & tensor_name) const;
158
159 ETensorType GetTensorType(std::string name) const;
160
161
162 bool CheckIfTensorAlreadyExist(std::string tensor_name);
163 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape);
164 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape);
165 void AddOperator(std::unique_ptr<ROperator> op, int order_execution = -1);
166 void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
167 std::shared_ptr<void> data);
168 void AddInitializedTensor(const std::string &tensor_name, ETensorType tensor_type,
169 const std::vector<std::size_t> &shape, void *raw_data);
170 void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
171 std::shared_ptr<void> data);
172
173 // Make tensor_name share the memory of orig_tensor_name, for operators such as Reshape
174 // whose output only reinterprets the shape of its input.
175 // Returns false, and registers nothing, when tensor_name needs storage of its own.
176 bool AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name);
177
178 template<class T>
179 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const T * data) {
180 size_t length = ConvertShapeToLength(shape);
181 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
182 std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
183 AddConstantTensor(name, GetTemplatedType<T>(T()), shape, data_ptr);
184 }
185 // for boolean can be more convenient passing an std::vector
186 template<class T>
187 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const std::vector<T> & data) {
188 size_t length = data.size();
189 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
190 std::copy(data.begin(), data.end(), (T*) data_ptr.get());
191 //std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
192 AddConstantTensor(name, GetTemplatedType<T>(T()), shape, data_ptr);
193 }
194
195 void AddShapeTensor(const std::string & name, const std::vector<Dim> & shapeValues, bool scalar = false);
196
197 void AddExtraCodeForDimShapes(const std::string & code) { fExtraCodeForDimShapes += code; }
198
199 // add and initialize subgraph to the model
200 void InitializeSubGraph(std::shared_ptr<RModel> graph);
201
202 // set a flag to indicate tensor does not need to be written in a weight file
203 // (e.g. shape tensors used as input to define a shape (in Reshape))
204 void SetNotWritableInitializedTensor(const std::string & tensor_name);
205
206 // Check if a tensor is initialized
207 bool IsInitializedTensor(const std::string &name) const;
208 // Check if a tensor is Constant (note a Constant tensor is also initialized)
209 bool IsConstantTensor(const std::string &name) const;
210 bool IsDynamicTensor(const std::string &name) const;
211 // Check if tensor is a input dynamic tensor (without a specified shape, based on Sim structure
212 bool IsDimInputTensor(const std::string &name) const;
213 // check if tensor is a fully specified input tensor
214 bool IsReadyInputTensor(const std::string &name) const;
215 /// check if a tensor is a shape tensor
216 bool IsShapeTensor(const std::string & name) const;
217 /// check if a tensor is a alias tensor
218 bool IsAliasTensor(const std::string & name) const;
219
220 // Add intermediate tensor
221 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape);
222 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape);
223 // Add an intermediate dynamic tensor
224 void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape);
225 // void Add a shape parameter
226 void AddShapeParam(const std::string & name, size_t def_value = 0);
227 /// Declare a shape parameter as computed at run time by an operator
228 void AddComputedShapeParam(const std::string &name);
229 bool IsComputedShapeParam(const std::string &name) const { return fComputedShapeParams.count(name) != 0; }
230 /// Line for an operator's Generate(), where its size variables are declared and before it writes the output.
231 /// It is replaced by a check that the shape parameters, computed at run time, do not exceed the maximum
232 /// values given to the Session constructor.
233 static std::string SizeCheckMarker(const std::string &name);
234 void AddInputTensorName(std::string name);
235 void AddOutputTensorNameList(std::vector<std::string> output_tensor_names);
236 void
237 UpdateOutputTensorList(std::vector<std::string> curr_output_tensor, std::vector<std::string> modify_output_tensor);
238 void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
239 std::shared_ptr<void> data);
240 std::shared_ptr<void> GetInitializedTensorData(std::string tensor_name);
241
242 template<class T>
243 std::vector<T> GetTensorData(const std::string & name);
244
245 void Initialize(int batchSize = -1, bool verbose = false);
246 void Initialize(const std::map<std::string,size_t> & inputParams, bool verbose = false);
247
248 void Generate(std::underlying_type_t<Options> options, int batchSize = -1, bool verbose = false);
249 void Generate(Options options = Options::kDefault, int batchSize = -1, bool verbose = false)
250 {
251 Generate(static_cast<std::underlying_type_t<Options>>(options), batchSize, verbose);
252 }
253 // generate the infer function signature. If isdecl= false generate the calling infer function
254 // used to infer the sub-graphs
255 std::string GenerateInferSignature(bool isdecl = true);
256
257 // calculate total intermediate memory and position intermediate tensor addresses
258 std::string AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors);
259 void CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_output_tensors, const size_t& op_idx);
260
261 void SetOptimizationLevel(OptimizationLevel optim_level) { fOptimizationLevel = optim_level; }
262
263 // get the size in bytes of the constant tensors
265 // get the size in bytes of the weight tensors
266 size_t GetWeightsTensorSize() const { return fWeightsTensorSize; }
267 // get the size in bytes of the intermediate tensors which are not part of the memory pool
268 size_t GetOtherTensorSize() const { return fOtherTensorSize; }
269 // get the size in bytes of the intermediate tensors managed by the memory pool
271 return (!fIntermediateMemoryInfo.total_stack.empty())
272 ? fIntermediateMemoryInfo.total_stack.rbegin()->first + fIntermediateMemoryInfo.total_stack.rbegin()->second.tensor_size
273 : 0;
274 }
275
276protected:
277 // internal functions
278 // generate code for the initialized tensors
280 // generate code for the intermediate tensors
282 // generate code for the dynamic tensors
284 // generate code for declarations needed by operators
286 // generate code for inference
287 void GenerateOutput();
288 // generate code for initializing memory pool for intermediate tensors
290 // Generate all session code
291 void GenerateSessionCode();
292 bool IsInputTensorShapeParam(std::string const &name) const;
293 std::vector<std::string> CollectTensorMemberNames(const std::string &input);
295
296public:
297 const std::vector<std::string> & GetInputTensorNames() const { return fInputTensorNames; }
298 const std::vector<std::string> & GetOutputTensorNames() const { return fOutputTensorNames; }
299 const std::vector<std::string> & GetDimShapeNames() const { return fDimShapeNames; }
300
302 long WriteInitializedTensorsToFile(std::string filename = "");
303 // Write the weight tensors as a safetensors payload to a stream or to an
304 // in-memory buffer (e.g. for use with a blob-based Session constructor)
305 void WriteInitializedTensorsToStream(std::ostream &os);
307
308 void PrintSummary() const;
309 void PrintIntermediateTensors() const;
310 void PrintOutputTensors() const;
311 void OutputGenerated(std::string filename = "", bool append = false);
312 void SetFilename(std::string filename) { fName = filename; }
313 std::string GetFilename() { return fName; }
314 const std::string &GetName() const { return fName; }
315
316 void AddBlasRoutines(std::vector<std::string> routines)
317 {
318 for (auto &routine : routines) {
319 fNeededBlasRoutines.insert(routine);
320 }
321 }
322 void AddNeededStdLib(std::string libname)
323 {
324 // if the library is already in the set, insert does nothing, so we don't need to check before inserting
325 fNeededStdLib.insert(std::move(libname));
326 }
328 {
329 fCustomOpHeaders.insert(std::move(filename));
330 }
331 // Register an inference helper function that the generated code needs. See
332 // EmitHelperFunctionsCode for the list of recognised keys.
334 {
335 fNeededHelperFunctions.insert(std::move(name));
336 }
337 const std::set<std::string> &GetNeededHelperFunctions() const { return fNeededHelperFunctions; }
338
339 void GenerateHeaderInfo(std::string &hgname);
340 // Replace the helper markers in the generated code with the standalone
341 // definitions of the helper functions collected in fNeededHelperFunctions.
343 void PrintGenerated(std::ostream &os = std::cout) { os << fGC; }
344 std::string ReturnGenerated() { return fGC; }
345
346 void PrintRequiredInputTensors() const;
347 void PrintInitializedTensors() const;
348 void PrintDynamicTensors() const;
349 void HeadInitializedTensors(std::string name, int n_print = 50);
350
351 // flag to use vdt for fast math functions (e.g. exp in softmax)
352 void SetUseVDT(bool on) {
353 fUseVDT = on;
354 }
355 bool UseVDT() const { return fUseVDT;}
356
357 // RModel is an internal representation that doesn't support ROOT IO (if you need model IO, use ONNX directly).
359};
360
361// need to implement here templated member functions and its specialization
362
363
364template<class T>
365inline std::vector<T> RModel::GetTensorData(const std::string & name) {
366 if (!IsInitializedTensor(name)) return std::vector<T>{};
367 T * data = static_cast<T*>(GetInitializedTensorData(name).get());
369 return std::vector<T>(data, data+size);
370}
371
372template<>
373inline std::vector<Dim> RModel::GetTensorData<Dim>(const std::string & name) {
374 if (!IsShapeTensor(name)) return std::vector<Dim>{};
376}
377
378} // namespace TMVA::Experimental::SOFIE
379
380#endif // TMVA_SOFIE_RMODEL
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define ClassDefNV(name, id)
Definition Rtypes.h:351
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 length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
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
std::string WriteInitializedTensorsToBuffer()
Definition RModel.cxx:1936
void AddShapeParam(const std::string &name, size_t def_value=0)
Definition RModel.cxx:417
std::vector< std::string > RuntimeBoundParams(const std::string &name) const
Shape parameters of a dynamic tensor that an operator computes at run time and that are Session const...
Definition RModel.cxx:1120
void AddNeededHelperFunction(std::string name)
Definition RModel.hxx:333
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:109
RModel(RModel const &)=delete
bool IsComputedShapeParam(const std::string &name) const
Definition RModel.hxx:229
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:145
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
Definition RModel.hxx:98
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:368
const std::vector< std::string > & GetOutputTensorNames() const
Definition RModel.hxx:298
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:383
size_t GetIntermediateTensorSize() const
Definition RModel.hxx:270
bool AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name)
Definition RModel.cxx:307
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:1217
std::unordered_set< std::string > fNeededBlasRoutines
Definition RModel.hxx:58
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:202
void GenerateHeaderInfo(std::string &hgname)
Definition RModel.cxx:2167
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:1960
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:2254
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:213
static std::string SizeCheckMarker(const std::string &name)
Line for an operator's Generate(), where its size variables are declared and before it writes the out...
Definition RModel.cxx:1135
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:97
void SetOptimizationLevel(OptimizationLevel optim_level)
Definition RModel.hxx:261
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
Definition RModel.cxx:437
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:289
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:400
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:269
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
Definition RModel.hxx:122
void AddExtraCodeForDimShapes(const std::string &code)
Definition RModel.hxx:197
void AddBlasRoutines(std::vector< std::string > routines)
Definition RModel.hxx:316
void AddInputTensorName(std::string name)
Definition RModel.cxx:232
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:104
void AddNeededStdLib(std::string libname)
Definition RModel.hxx:322
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:373
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:342
size_t GetConstantTensorSize() const
Definition RModel.hxx:264
static constexpr const char * kHelperIncludesMarker
Definition RModel.hxx:80
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:355
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:346
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:582
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:236
RModel()=default
Default constructor.
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:2131
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:359
void WriteInitializedTensorsToStream(std::ostream &os)
Definition RModel.cxx:1881
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:663
size_t GetWeightsTensorSize() const
Definition RModel.hxx:266
const std::set< std::string > & GetNeededHelperFunctions() const
Definition RModel.hxx:337
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1806
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:1159
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
Definition RModel.cxx:156
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const std::vector< T > &data)
Definition RModel.hxx:187
void AddNeededCustomHeader(std::string filename)
Definition RModel.hxx:327
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:94
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:460
void AddComputedShapeParam(const std::string &name)
Declare a shape parameter as computed at run time by an operator.
Definition RModel.cxx:428
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:477
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
std::vector< T > GetTensorData(const std::string &name)
Definition RModel.hxx:365
void SetFilename(std::string filename)
Definition RModel.hxx:312
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:826
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:100
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:469
const std::string & GetName() const
Definition RModel.hxx:314
RModel & operator=(RModel const &)=delete
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:170
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:105
const std::vector< std::string > & GetInputTensorNames() const
Definition RModel.hxx:297
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:451
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, bool verbose=false)
Definition RModel.cxx:1699
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:350
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:377
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:444
const std::vector< std::string > & GetDimShapeNames() const
Definition RModel.hxx:299
void Generate(Options options=Options::kDefault, int batchSize=-1, bool verbose=false)
Definition RModel.hxx:249
RModel(std::string name, std::string parsedtime)
Definition RModel.hxx:136
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:299
std::unordered_set< std::string > fCustomOpHeaders
Definition RModel.hxx:65
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const T *data)
Definition RModel.hxx:179
void PrintGenerated(std::ostream &os=std::cout)
Definition RModel.hxx:343
bool IsInputTensorShapeParam(std::string const &name) const
Check if a given parameter is used for the shape of an input tensor.
Definition RModel.cxx:1141
std::string Clean_name(std::string input_tensor_name)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::underlying_type_t< Options > operator|(Options opA, Options opB)
Definition RModel.cxx:102
std::map< size_t, TensorMemoryInfo > total_stack