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>
16{
17
18private:
19
20 std::string fNX;
21 std::string fNY;
22 std::vector<size_t> fShape;
23 std::vector<Dim> fDimShape;
24 std::vector<Dim> fDimOutputShape;
25 std::vector<T> fValues;
26 std::string fAttrType;
27 bool fIsConstantOfShape = false;
29
30public:
32
33 ROperator_Constant(const std::string & type, const std::vector<T> & values, const std::vector<size_t> & shape, std::string nameX, std::string nameY):
34 fNX(UTILITY::Clean_name(nameX)),
35 fNY(UTILITY::Clean_name(nameY)),
36 fShape(shape),
37 fValues(values),
39 {
42 }
43
44 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
45 return input;
46 }
47
48 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
49 auto ret = input; //suggest copy to compiler
50 return ret;
51 }
52
53 void Initialize(RModel& model) override {
54 //input must be a graph input, or already initialized intermediate tensor
55 size_t length = 1;
56 /// ConstantOfShape-------------
57 if (!fNX.empty()) {
58 // case of ConstantOfShape (since no inputs in case of Constant operator)
59 fIsConstantOfShape = true;
60 if (model.CheckIfTensorAlreadyExist(fNX) == false){
61 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor is not found in model");
62 }
63 // get output shape from input values:
64 // can work only if input is a constant or initialized tensor (or dynamic one)
65 if (model.IsConstantTensor(fNX)) {
66 fIsOutputConstant = true;
67 auto dptr = model.GetInitializedTensorData(fNX);
68 auto input_tensor = static_cast<int64_t *>(dptr.get());
69 auto input_shape = model.GetTensorShape(fNX);
70 if (input_shape.size() > 1 )
71 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor has invalid shape");
72 if (input_tensor != nullptr && !input_shape.empty()) {
73 fShape = std::vector<size_t> (input_shape[0]);
74 for (size_t i = 0; i < fShape.size(); i++)
75 fShape[i] = input_tensor[i];
76 } else
77 fShape = {1}; // scalar case
78
80 if (fValues.size() != 1)
81 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op value Tensor has invalid size " + std::to_string(fValues.size()));
82
83 T value = fValues[0];
84 fValues = std::vector<T>(length, value);
85 }
86 else if (model.IsShapeTensor(fNX)) {
87 // case tensor values representing output shapes are known
89 } else {
90 // case of not known shape tensors- we need to do at run time
91 // not sure if we ever encounter this case
94 if (fDimShape.size() > 1 )
95 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor has invalid shape");
96 if (!fDimShape[0].isParam) {
97 fDimOutputShape.resize(fDimShape[0].dim);
98 for (size_t i = 0; i < fDimShape[0].dim; i++) {
99 fDimOutputShape[i] = Dim{ std::string("s_") + fNY + "_" + std::to_string(i)};
100 }
101 }
102 else {
103 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor has not defied shape");
104 }
105 }
106
107 } else {
108 // case of constant operator
109 // in case of standard constant the shape is provided as input
110 fIsOutputConstant = true;
112 if (length != fValues.size())
113 throw std::runtime_error("TMVA SOFIE Constant Op has invalid shape : " + ConvertShapeToString(fShape) +
114 " with " + std::to_string(fValues.size()) + " values");
115 }
116
117 // we need to create an initialized tensor of type constant to flag to not save it in a weight file
118 // but keep its initialization in the generated code. The values might also be needed in initializing the
119 // following operators using as input Constant or ConstantOfShape
120 // resize fValues to shape length
121 if (fIsOutputConstant) {
123 if (model.Verbose()) {
124 std::cout << "adding constant tensor " << fNY << " with shape " << ConvertShapeToString(fShape)
125 << " and values [";
126 for (auto v : fValues) std::cout << " " << v;
127 std::cout << "]" << std::endl;
128 }
129 } else {
131 }
132 }
133
134 std::string Generate(std::string opName) override {
135 // no code to generate here. Tensor are defined in Session constructor
136 std::stringstream out;
137 if (fIsOutputConstant) {
138 if (fNX.empty())
139 out << "// ---- Constant (no-op) " << opName << " --> " << ConvertShapeToString(fDimOutputShape) << "\n";
140 else
141 out << "// ---- ConstantOfShape (no-op) " << opName << " --> " << ConvertShapeToString(fDimOutputShape) << "\n";
142 return out.str();
143 }
144 // Only ConstantOfShape might require generation code
145 // generate constant tensor according to input
146
147 out << "\n//--------- ConstantOfShape " << opName << " --> " << ConvertShapeToString(fDimOutputShape) << "\n";
148 // set shape values if needed
150 for (size_t i = 0; i < fDimOutputShape.size(); i++) {
151 out << SP << "size_t " << fDimOutputShape[i].param << " = " << "tensor_" << fNX << "[" << i << "];\n";
152 }
153 }
155 // vector is already allocated- fill with values
156 out << SP << "if (" << length << " > fTensor_" << fNY << ".size())\n";
157 out << SP << SP << "fTensor_" << fNY << ".resize(" << length << ");\n";
158 out << SP << "std::fill(fTensor_" << fNY << ".begin(), fTensor_" << fNY << ".end(), " << fValues[0] << ");\n";
159 return out.str();
160 }
161};
162
163}//SOFIE
164}//Experimental
165}//TMVA
166
167
168#endif //TMVA_SOFIE_ROPERATOR_Constant
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 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
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:29
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:65
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:247
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:193
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:211
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:224
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:312
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:215
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
ROperator_Constant(const std::string &type, const std::vector< T > &values, const std::vector< size_t > &shape, std::string nameX, std::string nameY)
std::string Generate(std::string opName) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:44
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:48
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
ETensorType ConvertStringToType(std::string type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations