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
15class ROperator_Cast final : public ROperator
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)),
29 fAttrType(attr_type) {}
30
31 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
32 return input;
33 }
34
35 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
36 auto ret = input; //suggest copy to compiler
37 return ret;
38 }
39
40 void Initialize(RModel& model){
41 //input must be a graph input, or already initialized intermediate tensor
42 if (model.CheckIfTensorAlreadyExist(fNX) == false){
43 throw std::runtime_error("TMVA SOFIE Cast Op Input Tensor is not found in model");
44 }
45 fShape = model.GetTensorShape(fNX);
46 // shoud we add a check if the same type
47 auto inputType = model.GetTensorType(fNX);
48 if (model.IsInitializedTensor(fNX)) {
49 fIsOutputConstant = true;
50 auto inputData = model.GetInitializedTensorData(fNX);
52 model.AddConstantTensor<int64_t>(fNY, fShape, static_cast<int64_t*>(inputData.get()));
54 }
55 else
56 fIsOutputConstant = false;
57 }
60 if (model.Verbose()) {
61 std::cout << "Cast : " << ConvertTypeToString(inputType) << " " << fNX << " -> " << fAttrType << " for " << fNY;
62 if (fIsOutputConstant) std::cout << " (constant) ";
63 std::cout << std::endl;
64 }
65 }
66
67
68 std::string Generate(std::string OpName){
69 if (fIsOutputConstant) return "";
70
71 OpName = "op_" + OpName;
72 if (fShape.empty()) {
73 throw std::runtime_error("TMVA SOFIE Cast called to Generate without being initialized first");
74 }
75 std::stringstream out;
77
78 // out << SP << ETensorType << " " << OpName << "_attr = " << fattr << ";\n";
79 out << "\n//------ CAST\n";
80 // no generated code for constant outputs
81 if (fIsOutputConstant) return out.str();
82
83 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
84
85 out << SP << SP << "tensor_" << fNY << "[id] = static_cast<"<< fAttrType << ">(tensor_" << fNX << "[id]);\n";
86
87 out << SP << "}\n";
88 return out.str();
89 }
90
91};
92
93}//SOFIE
94}//Experimental
95}//TMVA
96
97
98#endif //TMVA_SOFIE_ROPERATOR_Cast
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:203
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
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:188
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
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:273
ROperator_Cast(std::string attr_type, std::string nameX, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::string Generate(std::string OpName)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:43
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations