Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Range.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_RANGE
2#define TMVA_SOFIE_ROPERATOR_RANGE
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9#include <algorithm>
10
11namespace TMVA{
12namespace Experimental{
13namespace SOFIE{
14
15template <typename T>
16class ROperator_Range final : public ROperator
17{
18private:
19
20 std::string fNStart;
21 std::string fNLimit;
22 std::string fNDelta;
23 std::string fNOutput;
24 std::vector<Dim> fShape;
25 std::string fType;
26
27public:
29
30 ROperator_Range(std::string start, std::string limit, std::string delta, std::string nameOutput):
31 fNStart(start), fNLimit(limit), fNDelta(delta),
32 fNOutput(UTILITY::Clean_name(nameOutput)) {
33 if (std::is_same<T, float>::value) {
34 fType = "float";
35 } else if (std::is_same<T, int64_t>::value) {
36 fType = "int64_t";
37 }
38 static_assert( (std::is_same_v<T, float> || std::is_same_v<T, int64_t>),
39 "TMVA::SOFIE - Unsupported type by Range operator");
40 }
41
42 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
43 return input;
44 }
45
46 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
47 auto ret = input; //suggest copy to compiler
48 return ret;
49 }
50
51 void Initialize(RModel& model) override {
52 //input must be a graph input, or already initialized intermediate tensor
54 throw
55 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNStart + "is not found in model");
56 }
58 throw
59 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNLimit + "is not found in model");
60 }
62 throw
63 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNDelta + "is not found in model");
64 }
67 T * start = static_cast<T*>(model.GetInitializedTensorData(fNStart).get());
68 T * limit = static_cast<T*>(model.GetInitializedTensorData(fNLimit).get());
69 T * delta = static_cast<T*>(model.GetInitializedTensorData(fNDelta).get());
70 if (!start || !delta || !limit)
71 std::runtime_error("TMVA SOFIE Range Op Input Tensor has invalid input data");
72 T a = *start;
73 T b = *limit;
74 T d = *delta;
75 int number_of_elements = std::max( static_cast<double>(std::ceil( (b - a) / d )) , 0. );
76 std::vector<T> output(number_of_elements);
77 for (int i=0; i<number_of_elements; ++i) {
78 output[i] = a + (i * d);
79 }
80 std::vector<size_t> shape = {static_cast<size_t>(number_of_elements)};
81 model.AddConstantTensor(fNOutput,shape, output.data());
82 fIsOutputConstant = true;
83 // set the input tensor not writable
87 }
88 else {
89 fShape = {Dim{"range_size"}};
91 }
92 if (model.Verbose()) {
93 std::cout << "Range -> output is " << fNOutput << " ";
94 if (fIsOutputConstant) std::cout << ConvertDynamicShapeToString(fShape) << std::endl;
95 else std::cout << ConvertShapeToString(model.GetTensorShape(fNOutput)) << std::endl;
96 }
97 }
98
99 std::string Generate(std::string OpName) override {
100
101 std::stringstream out;
102 out << "\n//------ Range\n";
103 if (fIsOutputConstant) return out.str();
104
105 OpName = "op_" + OpName;
106 if (fShape.empty()) {
107 throw std::runtime_error("TMVA SOFIE Range operator called to Generate without being initialized first");
108 }
109
110 std::string sizeName = fShape[0].param;
111 out << SP << "size_t " << sizeName << " = static_cast<size_t>(std::max(std::ceil((static_cast<float>(*tensor_" << fNLimit << ") - static_cast<float>(*tensor_" << fNStart << ")) / static_cast<float>(*tensor_" << fNDelta << ")), 0.0f));\n";
112 out << SP << "if (" << sizeName << " > " << "fTensor_" << fNOutput << ".size() ){\n";
113 out << SP << SP << "fTensor_" << fNOutput << ".resize(" << sizeName << ");\n";
114 // need to re-initialized pointer to tensor data
115 out << SP << SP << "tensor_" << fNOutput << " = fTensor_" << fNOutput << ".data();\n";
116 out << SP << "}\n";
117 out << SP << "for (size_t i = 0; i < " << sizeName << "; i++) {\n";
118 out << SP << SP << "fTensor_" << fNOutput << "[i] = *tensor_" << fNStart << " + i * (*tensor_" << fNDelta << ");\n";
119 out << SP << "}\n";
120 return out.str();
121 }
122};
123
124}//SOFIE
125}//Experimental
126}//TMVA
127
128#endif //TMVA_SOFIE_ROPERATOR_RANGE
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
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 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:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:178
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:220
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_Range(std::string start, std::string limit, std::string delta, std::string nameOutput)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::string Generate(std::string OpName) override
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 ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
ETensorType ConvertStringToType(std::string type)
create variable transformations
static void output()