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