Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Constant.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Constant
2#define TMVA_SOFIE_ROPERATOR_Constant
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14template<typename T>
15class ROperator_Constant final : public ROperator
16{
17
18private:
19
20 std::string fNX;
21 std::string fNY;
22 std::vector<size_t> fShape;
23 std::vector<T> fValues;
24 std::string fAttrType;
25 bool fIsConstantOfShape = false;
26
27public:
29
30 ROperator_Constant(const std::string & type, const std::vector<T> & values, const std::vector<size_t> & shape, std::string nameX, std::string nameY):
31 fNX(UTILITY::Clean_name(nameX)),
32 fNY(UTILITY::Clean_name(nameY)),
33 fShape(shape),
34 fValues(values),
36 { }
37
38 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
39 return input;
40 }
41
42 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
43 auto ret = input; //suggest copy to compiler
44 return ret;
45 }
46
47 void Initialize(RModel& model){
48 //input must be a graph input, or already initialized intermediate tensor
49 size_t length = 1;
50 if (!fNX.empty()) {
51 // case of ConstantOfShape (since no inputs in case of Constant operator)
52 fIsConstantOfShape = true;
53 if (model.CheckIfTensorAlreadyExist(fNX) == false){
54 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor is not found in model");
55 }
56 // get output shape from input values:
57 // can work only if input is a constant or initialized tensor (or dynamic one)
58 auto dptr = model.GetInitializedTensorData(fNX);
59 auto input_tensor = static_cast<int64_t *>(dptr.get());
60 auto input_shape = model.GetTensorShape(fNX);
61 if (input_shape.size() > 1 )
62 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor has invalid shape");
63 if (input_tensor != nullptr && !input_shape.empty()) {
64 fShape = std::vector<size_t> (input_shape[0]);
65 for (size_t i = 0; i < fShape.size(); i++)
66 fShape[i] = input_tensor[i];
67 } else
68 fShape = {1}; // scalar case
69
71 if (fValues.size() != 1)
72 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op value Tensor has invalid size " + std::to_string(fValues.size()));
73
74 T value = fValues[0];
75 fValues = std::vector<T>(length, value);
76
77 } else {
78 // case of constant operator
79 // in case of standard constant the shape is provided as input
81 if (length != fValues.size())
82 throw std::runtime_error("TMVA SOFIE Constant Op has invalid shape : " + ConvertShapeToString(fShape) +
83 " with " + std::to_string(fValues.size()) + " values");
84 }
85
86 // we need to create an initialized tensor of type constant to flag to not save it in a weight file
87 // but keep its initialization in the generated code. The values might also be needed in initializing the
88 // following operators using as input Constant or ConstantOfShape
89 // resize fValues to shape length
91 if (model.Verbose()) {
92 std::cout << "adding constant tensor " << fNY << " with shape " << ConvertShapeToString(fShape)
93 << " and values [";
94 for (auto v : fValues) std::cout << " " << v;
95 std::cout << "]" << std::endl;
96 }
97 }
98
99 std::string Generate(std::string /* OpName */){
100 // no code to generate here. Tensor are defined in Session constructor
101 return "//---------------------------------------\n";
102 }
103};
104
105}//SOFIE
106}//Experimental
107}//TMVA
108
109
110#endif //TMVA_SOFIE_ROPERATOR_Constant
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
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 value
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
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:178
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:56
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:264
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
ROperator_Constant(const std::string &type, const std::vector< T > &values, const std::vector< size_t > &shape, std::string nameX, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations