Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
22
23 std::unordered_map<std::string, InputTensorInfo> fInputTensorInfos; // input tensors where shape may not fully defined or other graph inputs?
24 std::unordered_map<std::string, TensorInfo> fReadyInputTensorInfos; // input tensors where shape is full defined
25 std::unordered_map<std::string, InitializedTensor> fInitializedTensors;
26 std::unordered_map<std::string, TensorInfo> fIntermediateTensorInfos;
27 std::unordered_map<std::string, DynamicTensorInfo> fDynamicTensorInfos;
28 std::unordered_map<std::string, std::string>
29 fShapeParams; // parameters defining the dynamic shape (e.g. batch size), store also its default value
30 std::vector<std::string> fOutputTensorNames;
31 std::vector<std::string> fInputTensorNames; // input tensor names using ONNX order
32
33 std::vector<std::unique_ptr<ROperator>> fOperators;
34
35 std::vector<std::shared_ptr<RModel>> fSubGraphs; ///<! sub-graph models (transient)
36 RModel * fParentGraph = nullptr;
37
38 // memory pool information for intermediate tensors
39 MemoryPoolInfo fIntermediateMemoryInfo; ///<! intermediate memory info (transient)
40 std::unordered_map<std::string_view, size_t> fIntermediateTensorFrequencyLookup; ///<! lookup table for intermediate tensor frequency (transient)
41
42public:
43 /**
44 Default constructor. Needed to allow serialization of ROOT objects. See
45 https://root.cern/manual/io_custom_classes/#restrictions-on-types-root-io-can-handle
46 */
47 RModel() = default;
48 RModel(std::string name, std::string parsedtime) : RModel_Base(name, parsedtime) {}
49
50 // For GNN Functions usage
52
53 int Verbose() const { return fVerbose;}
54
55 const std::vector<size_t> &GetTensorShape(std::string name) const;
56 std::vector<Dim> GetDynamicTensorShape(std::string name) const;
57 const ETensorType &GetTensorType(std::string name) const;
58
59 bool CheckIfTensorAlreadyExist(std::string tensor_name);
60 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape);
61 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape);
62 void AddOperator(std::unique_ptr<ROperator> op, int order_execution = -1);
64 {
65 std::unique_ptr<ROperator> tmp(op);
66 AddOperator(std::move(tmp), order_execution);
67 }
68 void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
69 std::shared_ptr<void> data);
70 void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
71 std::shared_ptr<void> data);
72
73 template<class T>
74 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const T * data) {
75 size_t length = ConvertShapeToLength(shape);
76 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
77 std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
79 }
80 // for boolean can be more convenient passing an std::vector
81 template<class T>
82 void AddConstantTensor(const std::string & name, const std::vector<size_t> & shape, const std::vector<T> & data) {
83 size_t length = data.size();
84 std::shared_ptr<void> data_ptr(malloc(length * sizeof(T)), free);
85 std::copy(data.begin(), data.end(), (T*) data_ptr.get());
86 //std::memcpy(data_ptr.get(), (void*) data, length * sizeof(T));
88 }
89
90 template <typename T>
91 void AddInitializedTensor(const std::string & tensor_name, const std::vector<std::size_t> & shape, T *raw_data)
92 {
93 size_t size = ConvertShapeToLength(shape);
94 std::shared_ptr<void> data(malloc(size * sizeof(T)), free);
95 std::memcpy(data.get(), raw_data, size * sizeof(T));
96 AddInitializedTensor(tensor_name, GetTemplatedType(T()), shape, data);
97 }
98
99 // add and initialize subgraph to the model
100 void InitializeSubGraph(std::shared_ptr<RModel> graph);
101
102 // set a flag to indicate tensor does not need to be written in a weight file
103 // (e.g. shape tensors used as input to define a shape (in Reshape))
104 void SetNotWritableInitializedTensor(const std::string & tensor_name);
105
106 // Check if a tensor is initialized
107 bool IsInitializedTensor(const std::string &name) const;
108 // Check if a tensor is Constant (note a Constant tensor is also initialized)
109 bool IsConstantTensor(const std::string &name) const;
110 bool IsDynamicTensor(const std::string &name) const;
111 // Check if tensor is a input dynamic tensor (without a specified shape, based on Sim structure
112 bool IsDimInputTensor(const std::string &name) const;
113 // check if tensor is a fully specified input tensor
114 bool IsReadyInputTensor(const std::string &name) const;
115
116 // Add intermediate tensor
117 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<Dim> dim_shape);
118 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape);
119 // Add an intermediate dynamic tensor
120 void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector<Dim> shape);
121
122 void AddInputTensorName(std::string name);
123 void AddOutputTensorNameList(std::vector<std::string> output_tensor_names);
124 void
125 UpdateOutputTensorList(std::vector<std::string> curr_output_tensor, std::vector<std::string> modify_output_tensor);
126 void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
127 std::shared_ptr<void> data);
128 std::shared_ptr<void> GetInitializedTensorData(std::string tensor_name);
129
130 void Initialize(int batchSize = -1, bool verbose = false);
131 void Initialize(const std::map<std::string,size_t> & inputParams, bool verbose = false);
132
133 void Generate(std::underlying_type_t<Options> options, int batchSize = -1, long pos = 0, bool verbose = false);
134 void Generate(Options options = Options::kDefault, int batchSize = -1, int pos = 0, bool verbose = false)
135 {
136 Generate(static_cast<std::underlying_type_t<Options>>(options), batchSize, pos, verbose);
137 }
138 // generate the infer function signature. If isdecl= false generate the calling infer function
139 // used to infer the sub-graphs
140 std::string GenerateInferSignature(bool isdecl = true);
141
142 // calculate total intermediate memory and position intermediate tensor addresses
143 std::string AllocateIntermediateMemory(std::span<const std::string_view> op_output_tensors);
144 void CheckAndFlushIntermediateMemory(std::span<const std::string_view> op_output_tensors, const size_t& op_idx);
145
147
148protected:
149 // internal functions
150 // generate code for the initialized tensors
152 // generate code for the intermediate tensors
154 // generate code for the dynamic tensors
156 // generate code for declarations needed by operators
158 // generate code for inference
159 void GenerateOutput();
160 // generate code for initializing memory pool for intermediate tensors
162 // Generate all session code
163 void GenerateSessionCode();
164
165public:
166 const std::vector<std::string> &GetInputTensorNames() const { return fInputTensorNames; }
167 const std::vector<std::string> &GetOutputTensorNames() const { return fOutputTensorNames; }
168
170 long WriteInitializedTensorsToFile(std::string filename = "");
171
173 void PrintOutputTensors();
174 void OutputGenerated(std::string filename = "", bool append = false);
175 std::vector<std::string> GetOutputTensorNames() { return fOutputTensorNames; }
176 void SetFilename(std::string filename) { fName = filename; }
177
178 /*
179 template <typename T>
180 void AddInitializedTensor(std::string tensor_name, RTensor<T> new_tensor){
181 //a view only
182 T obj;
183 if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()){
184 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
185 }
186 InitializedTensor new_tensor_ {GetTemplatedType(obj), new_tensor.GetShape() ,
187 static_cast<void>(new_tensor.GetData())}; fInitializedTensors[tensor_name] = new_tensor_;
188 }
189 */
190
193 void PrintDynamicTensors();
194 void HeadInitializedTensors(std::string name, int n_print = 50);
195
196 bool UseSession() const { return fUseSession; }
197
198 // Use the ClassDef macro to allow definition of custom streaming
200};
201
202} // namespace SOFIE
203} // namespace Experimental
204} // namespace TMVA
205
206#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:1536
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
Definition RModel.hxx:27
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:186
const std::vector< std::string > & GetOutputTensorNames() const
Definition RModel.hxx:167
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:200
void AddOperatorReference(ROperator *op, int order_execution=-1)
Definition RModel.hxx:63
std::string GenerateInferSignature(bool isdecl=true)
Definition RModel.cxx:665
RModel(std::string function_name)
Definition RModel.hxx:51
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:95
std::vector< std::unique_ptr< ROperator > > fOperators
Definition RModel.hxx:33
void OutputGenerated(std::string filename="", bool append=false)
Definition RModel.cxx:1303
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:105
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:26
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
Definition RModel.cxx:238
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
Definition RModel.hxx:24
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:165
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:217
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:155
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
Definition RModel.hxx:40
void AddInputTensorName(std::string name)
Definition RModel.cxx:124
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:30
const ETensorType & GetTensorType(std::string name) const
Definition RModel.cxx:67
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:190
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:175
const std::vector< size_t > & GetTensorShape(std::string name) const
Definition RModel.cxx:29
void AddInitializedTensor(const std::string &tensor_name, const std::vector< std::size_t > &shape, T *raw_data)
Definition RModel.hxx:91
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
Definition RModel.cxx:334
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:128
RModel()=default
Default constructor.
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:1267
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:179
void Initialize(int batchSize=-1, bool verbose=false)
Definition RModel.cxx:373
long WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:1065
OptimizationLevel fOptimizationLevel
Definition RModel.hxx:21
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0, bool verbose=false)
Definition RModel.cxx:917
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const std::vector< T > &data)
Definition RModel.hxx:82
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:23
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:261
std::vector< Dim > GetDynamicTensorShape(std::string name) const
Definition RModel.cxx:55
MemoryPoolInfo fIntermediateMemoryInfo
! intermediate memory info (transient)
Definition RModel.hxx:39
std::string AllocateIntermediateMemory(std::span< const std::string_view > op_output_tensors)
Definition RModel.cxx:278
void SetFilename(std::string filename)
Definition RModel.hxx:176
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:484
std::unordered_map< std::string, std::string > fShapeParams
Definition RModel.hxx:29
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:270
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:31
const std::vector< std::string > & GetInputTensorNames() const
Definition RModel.hxx:166
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
Definition RModel.hxx:25
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:252
std::vector< std::string > GetOutputTensorNames()
Definition RModel.hxx:175
std::vector< std::shared_ptr< RModel > > fSubGraphs
! sub-graph models (transient)
Definition RModel.hxx:35
bool IsReadyInputTensor(const std::string &name) const
Definition RModel.cxx:194
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:245
RModel(std::string name, std::string parsedtime)
Definition RModel.hxx:48
void AddConstantTensor(const std::string &name, const std::vector< size_t > &shape, const T *data)
Definition RModel.hxx:74
void SetOptimizationLevel(const OptimizationLevel &optim_level)
Definition RModel.hxx:146
void Generate(Options options=Options::kDefault, int batchSize=-1, int pos=0, bool verbose=false)
Definition RModel.hxx:134
ETensorType GetTemplatedType(T)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations