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