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<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 {
39 }
40
41 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
42 return input;
43 }
44
45 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
46 auto ret = input; //suggest copy to compiler
47 return ret;
48 }
49
50 void Initialize(RModel& model) override {
51 //input must be a graph input, or already initialized intermediate tensor
52 size_t length = 1;
53 if (!fNX.empty()) {
54 // case of ConstantOfShape (since no inputs in case of Constant operator)
55 fIsConstantOfShape = true;
56 if (model.CheckIfTensorAlreadyExist(fNX) == false){
57 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor is not found in model");
58 }
59 // get output shape from input values:
60 // can work only if input is a constant or initialized tensor (or dynamic one)
61 auto dptr = model.GetInitializedTensorData(fNX);
62 auto input_tensor = static_cast<int64_t *>(dptr.get());
63 auto input_shape = model.GetTensorShape(fNX);
64 if (input_shape.size() > 1 )
65 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op Input Tensor has invalid shape");
66 if (input_tensor != nullptr && !input_shape.empty()) {
67 fShape = std::vector<size_t> (input_shape[0]);
68 for (size_t i = 0; i < fShape.size(); i++)
69 fShape[i] = input_tensor[i];
70 } else
71 fShape = {1}; // scalar case
72
74 if (fValues.size() != 1)
75 throw std::runtime_error("TMVA SOFIE ConstantOfShape Op value Tensor has invalid size " + std::to_string(fValues.size()));
76
77 T value = fValues[0];
78 fValues = std::vector<T>(length, value);
79
80 } else {
81 // case of constant operator
82 // in case of standard constant the shape is provided as input
84 if (length != fValues.size())
85 throw std::runtime_error("TMVA SOFIE Constant Op has invalid shape : " + ConvertShapeToString(fShape) +
86 " with " + std::to_string(fValues.size()) + " values");
87 }
88
89 // we need to create an initialized tensor of type constant to flag to not save it in a weight file
90 // but keep its initialization in the generated code. The values might also be needed in initializing the
91 // following operators using as input Constant or ConstantOfShape
92 // resize fValues to shape length
94 if (model.Verbose()) {
95 std::cout << "adding constant tensor " << fNY << " with shape " << ConvertShapeToString(fShape)
96 << " and values [";
97 for (auto v : fValues) std::cout << " " << v;
98 std::cout << "]" << std::endl;
99 }
100 }
101
102 std::string Generate(std::string /* OpName */) override {
103 // no code to generate here. Tensor are defined in Session constructor
104 return "//---------------------------------------\n";
105 }
106};
107
108}//SOFIE
109}//Experimental
110}//TMVA
111
112
113#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
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:95
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:165
const std::vector< size_t > & GetTensorShape(std::string name) const
Definition RModel.cxx:29
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:261
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::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:47
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations