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<size_t> fShape;
23 std::string fAttrType = "float";
24
25public:
27 ROperator_Cast(std::string attr_type,std::string nameX, std::string nameY):
28 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)),
32 }
33
34 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
35 return input;
36 }
37
38 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
39 auto ret = input; //suggest copy to compiler
40 return ret;
41 }
42
43 void Initialize(RModel& model) override {
44 //input must be a graph input, or already initialized intermediate tensor
45 if (model.CheckIfTensorAlreadyExist(fNX) == false){
46 throw std::runtime_error("TMVA SOFIE Cast Op Input Tensor is not found in model");
47 }
48 fShape = model.GetTensorShape(fNX);
49 // shoud we add a check if the same type
50 auto inputType = model.GetTensorType(fNX);
51 if (model.IsInitializedTensor(fNX)) {
52 fIsOutputConstant = true;
55 model.AddConstantTensor<int64_t>(fNY, fShape, static_cast<int64_t*>(inputData.get()));
57 }
58 else
59 fIsOutputConstant = false;
60 }
63 if (model.Verbose()) {
64 std::cout << "Cast : " << ConvertTypeToString(inputType) << " " << fNX << " -> " << fAttrType << " for " << fNY;
65 if (fIsOutputConstant) std::cout << " (constant) ";
66 std::cout << std::endl;
67 }
68 }
69
70
71 std::string Generate(std::string OpName) override {
72 if (fIsOutputConstant) return "";
73
74 OpName = "op_" + OpName;
75 if (fShape.empty()) {
76 throw std::runtime_error("TMVA SOFIE Cast called to Generate without being initialized first");
77 }
78 std::stringstream out;
80
81 // out << SP << ETensorType << " " << OpName << "_attr = " << fattr << ";\n";
82 out << "\n//------ CAST\n";
83 // no generated code for constant outputs
84 if (fIsOutputConstant) return out.str();
85
86 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
87
88 out << SP << SP << "tensor_" << fNY << "[id] = static_cast<"<< fAttrType << ">(tensor_" << fNX << "[id]);\n";
89
90 out << SP << "}\n";
91 return out.str();
92 }
93
94};
95
96}//SOFIE
97}//Experimental
98}//TMVA
99
100
101#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
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:94
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:227
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:192
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:202
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:288
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:297
ROperator_Cast(std::string attr_type, std::string nameX, std::string nameY)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
void Initialize(RModel &model) override
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:46
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:47
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations