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
6#include "TMVA/ROperator.hxx"
7
8namespace TMVA {
9namespace Experimental {
10namespace SOFIE {
11
12class RModel final : public RModel_Base {
13
14private:
15 bool fIsInitialized = false;
16 bool fIsSubGraph = false;
17 int fVerbose = 0;
18 int fBatchSize = -1;
19 long fReadPos = 0; // reading file position
20 size_t fConstantTensorSize = 0; // size (in Bytes) of the allocated constant tensors
21 size_t fWeightsTensorSize = 0; // size (in Bytes) of the allocated weight tensors
22 size_t fOtherTensorSize = 0; // size (in Bytes) of intermediate tensors which are not managed by the memory pool
23
25
26 std::unordered_map<std::string, InputTensorInfo> fInputTensorInfos; // input tensors where shape may not fully defined or other graph inputs?
27 std::unordered_map<std::string, TensorInfo> fReadyInputTensorInfos; // input tensors where shape is full defined
28 std::unordered_map<std::string, InitializedTensor> fInitializedTensors;
29 std::unordered_map<std::string, TensorInfo> fIntermediateTensorInfos;
30 std::unordered_map<std::string, DynamicTensorInfo> fDynamicTensorInfos;
31 std::unordered_map<std::string, std::pair<std::vector<Dim>, bool>> fShapeTensors; // constant tensors describing a shape
32 std::unordered_map<std::string, std::string> fShapeParams; // parameters defining the dynamic shape (e.g. batch size), store also its default value
33 std::vector<std::string> fDimShapeNames; // parameter names used to define the shapes
34 std::vector<std::string> fOutputTensorNames;
35 std::vector<std::string> fInputTensorNames; // input tensor names using ONNX order
36
37
38
39 std::vector<std::unique_ptr<ROperator>> fOperators;
40
41 std::vector<std::shared_ptr<RModel>> fSubGraphs; ///<! sub-graph models (transient)
42 RModel * fParentGraph = nullptr;
43
44 // memory pool information for intermediate tensors
45 MemoryPoolInfo fIntermediateMemoryInfo; ///<! intermediate memory info (transient)
46 std::unordered_map<std::string_view, size_t> fIntermediateTensorFrequencyLookup; ///<! lookup table for intermediate tensor frequency (transient)
47
48public:
49 /**
50 Default constructor. Needed to allow serialization of ROOT objects. See
51 https://root.cern/manual/io_custom_classes/#restrictions-on-types-root-io-can-handle
52 */
53 RModel() = default;
54 RModel(std::string name, std::string parsedtime) : RModel_Base(name, parsedtime) {}
55
56 // For GNN Functions usage
58
59 int Verbose() const { return fVerbose;}
60
61 std::vector<size_t> GetTensorShape(const std::string & name) const;
62 std::vector<Dim> GetDimTensorShape(const std::string & name) const;
63 std::vector<Dim> GetDynamicTensorShape(const std::string & name) const ;
64
65 // get the values for the tensor representing a shape
66 const std::vector<Dim> & GetShapeTensorValues(const std::string & tensor_name) const;
67
68 ETensorType GetTensorType(std::string name) const;
69
70
71 bool CheckIfTensorAlreadyExist(std::string tensor_name);
72 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape);
73 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape);
74 void AddOperator(std::unique_ptr<ROperator> op, int order_execution = -1);
76 {
77 std::unique_ptr<ROperator> tmp(op);
78 AddOperator(std::move(tmp), order_execution);
79 }
80 void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
81 std::shared_ptr<void> data);
82 void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
83 std::shared_ptr<void> data);
84
85
86 template<class T>
87 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const T * data) {
88 size_t length = ConvertShapeToLength(shape);
89 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
90 std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
92 }
93 // for boolean can be more convenient passing an std::vector
94 template<class T>
95 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const std::vector<T> & data) {
96 size_t length = data.size();
97 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
98 std::copy(data.begin(), data.end(), (T*) data_ptr.get());
99 //std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
101 }
102
103 template <typename T>
104 void AddInitializedTensor(const std::string & tensor_name, const std::vector<std::size_t> & shape, T *raw_data)
105 {
106 size_t size = ConvertShapeToLength(shape);
107 std::shared_ptr<void> data(malloc(size * sizeof(T)), free);
108 std::memcpy(data.get(), raw_data, size * sizeof(T));
109 AddInitializedTensor(tensor_name, GetTemplatedType(T()), shape, data);
110 }
111
112 void AddShapeTensor(const std::string & name, const std::vector<Dim> & shapeValues, bool scalar = false);
113
114
115 // add and initialize subgraph to the model
116 void InitializeSubGraph(std::shared_ptr<RModel> graph);
117
118 // set a flag to indicate tensor does not need to be written in a weight file
119 // (e.g. shape tensors used as input to define a shape (in Reshape))
120 void SetNotWritableInitializedTensor(const std::string & tensor_name);
121
122 // Check if a tensor is initialized
123 bool IsInitializedTensor(const std::string &name) const;
124 // Check if a tensor is Constant (note a Constant tensor is also initialized)
125 bool IsConstantTensor(const std::string &name) const;
126 bool IsDynamicTensor(const std::string &name) const;
127 // Check if tensor is a input dynamic tensor (without a specified shape, based on Sim structure
128 bool IsDimInputTensor(const std::string &name) const;
129 // check if tensor is a fully specified input tensor
130 bool IsReadyInputTensor(const std::string &name) const;
131 /// check if a tensor is a shape tensor
132 bool IsShapeTensor(const std::string & name) const;
133
134 // Add intermediate tensor
135 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape);
136 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape);
137 // Add an intermediate dynamic tensor
138 void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape);
139 // void Add a shape parameter
140 void AddShapeParam(const std::string & name, size_t def_value = 0);
141 void AddInputTensorName(std::string name);
142 void AddOutputTensorNameList(std::vector<std::string> output_tensor_names);
143 void
144 UpdateOutputTensorList(std::vector<std::string> curr_output_tensor, std::vector<std::string> modify_output_tensor);
145 void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
146 std::shared_ptr<void> data);
147 std::shared_ptr<void> GetInitializedTensorData(std::string tensor_name);
148
149 template<class T>
150 std::vector<T> GetTensorData(const std::string & name);
151
152 void Initialize(int batchSize = -1, bool verbose = false);
153 void Initialize(const std::map<std::string,size_t> & inputParams, bool verbose = false);
154
155 void Generate(std::underlying_type_t<Options> options, int batchSize = -1, long pos = 0, bool verbose = false);
156 void Generate(Options options = Options::kDefault, int batchSize = -1, int pos = 0, bool verbose = false)
157 {
158 Generate(static_cast<std::underlying_type_t<Options>>(options), batchSize, pos, verbose);
159 }
160 // generate the infer function signature. If isdecl= false generate the calling infer function
161 // used to infer the sub-graphs
162 std::string GenerateInferSignature(bool isdecl = true);
163
164 // calculate total intermediate memory and position intermediate tensor addresses
165 std::string AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors);
166 void CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_output_tensors, const size_t& op_idx);
167
169
170 // get the size in bytes of the constant tensors
172 // get the size in bytes of the weight tensors
173 size_t GetWeightsTensorSize() const { return fWeightsTensorSize; }
174 // get the size in bytes of the intermediate tensors which are not part of the memory pool
175 size_t GetOtherTensorSize() const { return fOtherTensorSize; }
176 // get the size in bytes of the intermediate tensors managed by the memory pool
178 return (!fIntermediateMemoryInfo.total_stack.empty())
179 ? fIntermediateMemoryInfo.total_stack.rbegin()->first + fIntermediateMemoryInfo.total_stack.rbegin()->second.tensor_size
180 : 0;
181 }
182
183protected:
184 // internal functions
185 // generate code for the initialized tensors
187 // generate code for the intermediate tensors
189 // generate code for the dynamic tensors
191 // generate code for declarations needed by operators
193 // generate code for inference
194 void GenerateOutput();
195 // generate code for initializing memory pool for intermediate tensors
197 // Generate all session code
198 void GenerateSessionCode();
199
200public:
201 const std::vector<std::string> & GetInputTensorNames() const { return fInputTensorNames; }
202 const std::vector<std::string> & GetOutputTensorNames() const { return fOutputTensorNames; }
203 const std::vector<std::string> & GetDimShapeNames() const { return fDimShapeNames; }
204
206 long WriteInitializedTensorsToFile(std::string filename = "");
207
209 void PrintOutputTensors();
210 void OutputGenerated(std::string filename = "", bool append = false);
211 std::vector<std::string> GetOutputTensorNames() { return fOutputTensorNames; }
212 void SetFilename(std::string filename) { fName = filename; }
213
214 /*
215 template <typename T>
216 void AddInitializedTensor(std::string tensor_name, RTensor<T> new_tensor){
217 //a view only
218 T obj;
219 if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()){
220 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
221 }
222 InitializedTensor new_tensor_ {GetTemplatedType(obj), new_tensor.GetShape() ,
223 static_cast<void>(new_tensor.GetData())}; fInitializedTensors[tensor_name] = new_tensor_;
224 }
225 */
226
229 void PrintDynamicTensors();
230 void HeadInitializedTensors(std::string name, int n_print = 50);
231
232 bool UseSession() const { return fUseSession; }
233
234 // Use the ClassDef macro to allow definition of custom streaming
236};
237
238// need to implement here templated member functions and its specialization
239
240
241template<class T>
242inline std::vector<T> RModel::GetTensorData(const std::string & name) {
243 if (!IsInitializedTensor(name)) return std::vector<T>{};
244 T * data = static_cast<T*>(GetInitializedTensorData(name).get());
246 return std::vector<T>(data, data+size);
247}
248
249template<>
250inline std::vector<Dim> RModel::GetTensorData<Dim>(const std::string & name) {
251 if (!IsShapeTensor(name)) return std::vector<Dim>{};
253}
254
255} // namespace SOFIE
256} // namespace Experimental
257} // namespace TMVA
258
259#endif // TMVA_SOFIE_RMODEL
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.
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 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 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:110
#define malloc
Definition civetweb.c:1575
void AddShapeParam(const std::string &name, size_t def_value=0)
Definition RModel.cxx:281
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:29
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:65
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
Definition RModel.hxx:30
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:232
const std::vector< std::string > & GetOutputTensorNames() const
Definition RModel.hxx:202
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:247
size_t GetIntermediateTensorSize() const
Definition RModel.hxx:177
void AddOperatorReference(ROperator *op, int order_execution=-1)
Definition RModel.hxx:75
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:805
RModel(std::string function_name)
Definition RModel.hxx:57
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
std::vector< std::unique_ptr< ROperator > > fOperators
Definition RModel.hxx:39
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:1430
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:133
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:29
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
Definition RModel.cxx:289
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
Definition RModel.hxx:27
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:193
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:264
std::vector< std::string > fDimShapeNames
Definition RModel.hxx:33
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:183
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
Definition RModel.hxx:46
void AddInputTensorName(std::string name)
Definition RModel.cxx:152
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:34
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:237
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:211
size_t GetConstantTensorSize() const
Definition RModel.hxx:171
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:220
void AddInitializedTensor(const std::string &tensor_name, const std::vector< std::size_t > &shape, T *raw_data)
Definition RModel.hxx:104
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
Definition RModel.cxx:429
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:156
RModel()=default
Default constructor.
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:1394
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:224
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:503
size_t GetWeightsTensorSize() const
Definition RModel.hxx:173
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1192
OptimizationLevel fOptimizationLevel
Definition RModel.hxx:24
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0, bool verbose=false)
Definition RModel.cxx:1062
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
Definition RModel.cxx:76
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const std::vector< T > &data)
Definition RModel.hxx:95
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:26
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:312
MemoryPoolInfo fIntermediateMemoryInfo
! intermediate memory info (transient)
Definition RModel.hxx:45
std::string AllocateIntermediateMemory(std::span< const std::string_view > op_output_tensors)
Definition RModel.cxx:329
std::unordered_map< std::string, std::pair< std::vector< Dim >, bool > > fShapeTensors
Definition RModel.hxx:31
std::vector< T > GetTensorData(const std::string &name)
Definition RModel.hxx:242
void SetFilename(std::string filename)
Definition RModel.hxx:212
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:618
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:32
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:321
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:90
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:35
const std::vector< std::string > & GetInputTensorNames() const
Definition RModel.hxx:201
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
Definition RModel.hxx:28
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:303
std::vector< std::string > GetOutputTensorNames()
Definition RModel.hxx:211
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:215
std::vector< std::shared_ptr< RModel > > fSubGraphs
! sub-graph models (transient)
Definition RModel.hxx:41
bool IsReadyInputTensor(const std::string &name) const
Definition RModel.cxx:241
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:296
const std::vector< std::string > & GetDimShapeNames() const
Definition RModel.hxx:203
RModel(std::string name, std::string parsedtime)
Definition RModel.hxx:54
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:203
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const T *data)
Definition RModel.hxx:87
void SetOptimizationLevel(const OptimizationLevel &optim_level)
Definition RModel.hxx:168
void Generate(Options options=Options::kDefault, int batchSize=-1, int pos=0, bool verbose=false)
Definition RModel.hxx:156
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
ETensorType GetTemplatedType(T)
create variable transformations
std::map< size_t, TensorMemoryInfo > total_stack