Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Cast.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Cast
2#define TMVA_SOFIE_ROPERATOR_Cast
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14
16{
17
18private:
19
20 std::string fNX;
21 std::string fNY;
22 std::vector<Dim> fShape;
24
25public:
27 ROperator_Cast(ETensorType type,std::string nameX, std::string nameY):
28 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)),
29 fType(type)
30 {
33 }
34
35 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
36 return input;
37 }
38
39 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
40 auto ret = input; //suggest copy to compiler
41 return ret;
42 }
43
44 void Initialize(RModel& model) override {
45 //input must be a graph input, or already initialized intermediate tensor
46 if (model.CheckIfTensorAlreadyExist(fNX) == false){
47 throw std::runtime_error("TMVA SOFIE Cast Op Input Tensor is not found in model");
48 }
50 // should we add a check if the same type
51 auto inputType = model.GetTensorType(fNX);
52 if (model.IsInitializedTensor(fNX)) {
53 fIsOutputConstant = true;
56 model.AddConstantTensor<int64_t>(fNY, ConvertShapeToInt(fShape), static_cast<int64_t*>(inputData.get()));
58 }
59 else
60 fIsOutputConstant = false;
61 } else if (model.IsShapeTensor(fNX) && fType == ETensorType::INT64) {
63 model.AddShapeTensor(fNY, shapeData, fShape.size() == 0);
64 fIsOutputConstant = true;
65 }
68 if (model.Verbose()) {
69 std::cout << "Cast : " << ConvertTypeToString(inputType) << " " << fNX << " -> " << ConvertTypeToString(fType) << " for " << fNY
70 << " shape " << ConvertDimShapeToString(fShape);
71 if (fIsOutputConstant) std::cout << " (constant) ";
72 std::cout << std::endl;
73 }
74 }
75
76
77 std::string Generate(std::string opName) override {
78
79 // output shape can be empty if is a scalar
80
81 std::stringstream out;
83
84 out << "\n//------ CAST " << opName << " ---> " << fNY << " " << ConvertDimShapeToString(fShape) << "\n";
85 // no generated code for constant outputs
86 if (fIsOutputConstant) return out.str();
87
88 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
89
90 out << SP << SP << "tensor_" << fNY << "[id] = static_cast<"<< ConvertTypeToString(fType) << ">(tensor_" << fNX << "[id]);\n";
91
92 out << SP << "}\n";
93 return out.str();
94 }
95
96};
97
98}//SOFIE
99}//Experimental
100}//TMVA
101
102
103#endif //TMVA_SOFIE_ROPERATOR_Cast
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 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< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:87
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:284
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:144
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:215
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:243
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:256
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:349
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:358
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:112
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:251
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
Definition RModel.cxx:225
std::string Generate(std::string opName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
void Initialize(RModel &model) override
ROperator_Cast(ETensorType type, std::string nameX, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:50
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:47
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:45
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:51
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations