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