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
4#include <type_traits>
5#include <unordered_set>
6#include <vector>
7#include <unordered_map>
8#include <iostream>
9#include <memory>
10#include <ctime>
11#include <set>
12#include <iomanip>
13#include <fstream>
14#include <sstream>
15#include "TMVA/SOFIE_common.hxx"
16#include "TMVA/ROperator.hxx"
17#include "TBuffer.h"
18
19namespace TMVA{
20namespace Experimental{
21namespace SOFIE{
22
23enum class Options {
24 kDefault = 0x0,
25 kNoSession = 0x1,
26 kNoWeightFile = 0x2,
27};
28
29std::underlying_type_t<Options> operator|(Options opA, Options opB);
30std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB);
31
32class RModel: public TObject{
33
34private:
35 std::unordered_map<std::string, InputTensorInfo> fInputTensorInfos; //graph input only; not including operator input (intermediate tensors)
36 std::unordered_map<std::string, TensorInfo> fReadyInputTensorInfos;
37 std::unordered_map<std::string, InitializedTensor> fInitializedTensors;
38 std::unordered_map<std::string, TensorInfo> fIntermediateTensorInfos;
39 std::vector<std::string> fOutputTensorNames;
40 std::vector<std::string> fInputTensorNames; //input tensor names using ONNX order
41
42 std::vector<std::unique_ptr<ROperator>> fOperators;
43
44 std::string fName="UnnamedModel";
45 std::string fFileName; //file name of original model file for identification
46 std::string fParseTime; //UTC date and time string at parsing
47
48
49 std::string fGC; //generated code
50 std::unordered_set<std::string> fNeededBlasRoutines;
51
52 const std::unordered_set<std::string> fAllowedStdLib = {"vector", "algorithm", "cmath"};
53 std::unordered_set<std::string> fNeededStdLib = {"vector"};
54 std::unordered_set<std::string> fCustomOpHeaders;
55 bool fUseWeightFile = true;
56 bool fUseSession = true;
57
58public:
59
60 //explicit move ctor/assn
61 RModel(RModel&& other);
62
63 RModel& operator=(RModel&& other);
64
65 //disallow copy
66 RModel(const RModel& other) = delete;
67 RModel& operator=(const RModel& other) = delete;
68
70 RModel(std::string name, std::string parsedtime);
71
72 const std::vector<size_t>& GetTensorShape(std::string name);
73 const ETensorType& GetTensorType(std::string name);
74
75 bool CheckIfTensorAlreadyExist(std::string tensor_name);
76 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape);
77 void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape);
78 void AddOperator(std::unique_ptr<ROperator> op, int order_execution = -1);
79 void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data);
80 // Check if a tensor is initialized
81 bool IsInitializedTensor(const std::string& name) const;
82 void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape);
83 void AddBlasRoutines(std::vector<std::string> routines) {
84 for (auto &routine : routines) {
85 fNeededBlasRoutines.insert(routine);
86 }
87 }
88 void AddNeededStdLib(std::string libname) {
89 if (fAllowedStdLib.find(libname) != fAllowedStdLib.end()) {
90 fNeededStdLib.insert(libname);
91 }
92 }
93 void AddNeededCustomHeader(std::string filename) {
95 }
96 void AddInputTensorName(std::string name);
97 void AddOutputTensorNameList(std::vector<std::string> outputtensornames);
98 void UpdateOutputTensorList(std::vector<std::string> curr_output_tensor, std::vector<std::string> modify_output_tensor);
99 void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data);
100 std::shared_ptr<void> GetInitializedTensorData(std::string tensor_name);
101
102
103 void Initialize(int batchSize=1);
104 void Generate(std::underlying_type_t<Options> options, int batchSize = 1);
105 void Generate(Options options = Options::kDefault, int batchSize = 1) {
106 Generate(static_cast<std::underlying_type_t<Options>>(options), batchSize);
107 }
108
110 void WriteInitializedTensorsToFile(std::string filename = "");
111
113 std::cout << fGC;
114 }
116 void OutputGenerated(std::string filename = "");
117 std::vector<std::string> GetOutputTensorNames(){
118 return fOutputTensorNames;
119 }
120
121/*
122 template <typename T>
123 void AddInitializedTensor(std::string tensor_name, RTensor<T> new_tensor){
124 //a view only
125 T obj;
126 if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()){
127 throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
128 }
129 InitializedTensor new_tensor_ {GetTemplatedType(obj), new_tensor.GetShape() , static_cast<void>(new_tensor.GetData())};
130 fInitializedTensors[tensor_name] = new_tensor_;
131 }
132*/
133
136 void HeadInitializedTensors(std::string name, int n_print = 50);
137
138 bool UseSession() const { return fUseSession;}
139
141
143};
144
145}//SOFIE
146}//Experimental
147}//TMVA
148
149#endif //TMVA_SOFIE_RMODEL
#define ClassDef(name, id)
Definition Rtypes.h:337
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 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
void AddOutputTensorNameList(std::vector< std::string > outputtensornames)
Definition RModel.cxx:169
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:80
RModel(const RModel &other)=delete
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition RModel.cxx:160
std::unordered_set< std::string > fNeededBlasRoutines
Definition RModel.hxx:50
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:101
std::vector< std::unique_ptr< ROperator > > fOperators
Definition RModel.hxx:42
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:108
void Initialize(int batchSize=1)
Definition RModel.cxx:200
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
Definition RModel.hxx:38
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
Definition RModel.hxx:36
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:144
RModel & operator=(RModel &&other)
Definition RModel.cxx:38
void AddBlasRoutines(std::vector< std::string > routines)
Definition RModel.hxx:83
void AddInputTensorName(std::string name)
Definition RModel.cxx:127
std::vector< std::string > fOutputTensorNames
Definition RModel.hxx:39
void AddNeededStdLib(std::string libname)
Definition RModel.hxx:88
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:155
std::unordered_set< std::string > fNeededStdLib
Definition RModel.hxx:53
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
Definition RModel.cxx:131
void HeadInitializedTensors(std::string name, int n_print=50)
Definition RModel.cxx:587
void OutputGenerated(std::string filename="")
Definition RModel.cxx:627
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:59
RModel & operator=(const RModel &other)=delete
void AddNeededCustomHeader(std::string filename)
Definition RModel.hxx:93
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
Definition RModel.hxx:35
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:191
void Generate(std::underlying_type_t< Options > options, int batchSize=1)
Definition RModel.cxx:240
const std::unordered_set< std::string > fAllowedStdLib
Definition RModel.hxx:52
void WriteInitializedTensorsToFile(std::string filename="")
Definition RModel.cxx:499
void Generate(Options options=Options::kDefault, int batchSize=1)
Definition RModel.hxx:105
std::vector< std::string > fInputTensorNames
Definition RModel.hxx:40
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
Definition RModel.hxx:37
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:182
std::vector< std::string > GetOutputTensorNames()
Definition RModel.hxx:117
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
Definition RModel.cxx:175
std::unordered_set< std::string > fCustomOpHeaders
Definition RModel.hxx:54
Mother of all ROOT objects.
Definition TObject.h:41
std::underlying_type_t< Options > operator|(Options opA, Options opB)
Definition RModel.cxx:15
create variable transformations