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>
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 {
43 }
44 }
45
46 void Initialize(RModel& model) override {
47 //input must be a graph input, or already initialized intermediate tensor
49 throw
50 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNStart + "is not found in model");
51 }
53 throw
54 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNLimit + "is not found in model");
55 }
57 throw
58 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNDelta + "is not found in model");
59 }
61
62
63
64 auto analyzeInput = [&](const std::string & tName, T & value, Dim & dim) {
65 int ftype = 0; // type of input (0 intermediate, 1 constant , 2 shape)
66 if (model.IsInitializedTensor(tName)) {
67 T * data = static_cast<T*>(model.GetInitializedTensorData(tName).get());
68 if (!data)
69 std::runtime_error("TMVA SOFIE Range Op Input Tensor has invalid input data");
70 value = *data;
71 ftype = 1;
72 } else if (model.IsShapeTensor(tName)) {
73 auto data = model.GetShapeTensorValues(tName);
74 dim = data[0];
75 if (!dim.isParam) {
76 value = static_cast<T>(dim.dim);
77 ftype = 1;
78 } else
79 ftype = 2;
80 }
81 return ftype;
82 };
83
93 if (res1 == 0 || res2 == 0 || res3 == 0) {
94 // cannot know at compile time- need to do fully at run time
95 //
96 fShape = {Dim{"range_size_" + fNStart + "_" + fNLimit}};
98 } else if (res1 == 1 && res2 == 1 && res3 == 1) {
99 size_t number_of_elements = std::max(static_cast<int>(std::ceil((limit_value - start_value) / delta_value )) , 0 );
100 fIsOutputConstant = true;
101
102 // compute output
103 std::vector<T> output(number_of_elements);
104 for (size_t i=0; i<number_of_elements; ++i) {
105 output[i] = start_value + (i * delta_value);
106 }
107 std::vector<size_t> shape = {number_of_elements};
108 model.AddConstantTensor(fNOutput,shape, output.data());
109 fShape = ConvertShapeToDim(shape);
110
111 } else { // case of a shape tensor
112 std::string start = (res1 == 1) ? std::to_string(start_value) : start_dim.GetVal();
113 std::string limit = (res2 == 1) ? std::to_string(limit_value) : limit_dim.GetVal();
114 std::string delta = (res3 == 1) ? std::to_string(delta_value) : delta_dim.GetVal();
115 std::stringstream s;
116 if (type == ETensorType::FLOAT ) {
117 if (delta_value == 1)
118 s << "std::max(std::ceil("<< limit << " - " << start << "),0.0f)";
119 else
120 s << "std::max(std::ceil(("<< limit << " - " << start << ")/" << delta << "),0.0f)";
121 } else if (type == ETensorType::INT64 ) {
122 if (delta == "1") {
123 if (start == "0")
124 s << limit;
125 else
126 s << "std::max((" << limit << " - " << start << "),0L)";
127 } else {
128 if (start == "0")
129 s << "((" << limit << ")/" << delta << ")";
130 else
131 s << "std::max((" << limit << " - " << start << ")/"<< delta << "),0L)";
132 }
133 } else {
134 throw
135 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + ConvertTypeToString(type) + "is not supported");
136 }
137
138
139 fShape = { Dim {s.str(), static_cast<size_t>(-1)} };
141 }
142
143
144 if (model.Verbose()) {
145 std::cout << "Range -> output is " << fNOutput << " : " << ConvertShapeToString(fShape);
146 if (fIsOutputConstant) std::cout << " : " << ConvertValuesToString(model.GetTensorData<T>(fNOutput));
147 std::cout << std::endl;
148 }
149 }
150
151 std::string Generate(std::string opName) override {
152
153 std::stringstream out;
154 out << "\n//------ Range " << opName << "---> " << ConvertDimShapeToString(fShape) << "\n";
155 if (fIsOutputConstant) return out.str();
156
157 opName = "op_" + opName;
158 if (fShape.empty()) {
159 throw std::runtime_error("TMVA SOFIE Range operator called to Generate without being initialized first");
160 }
161
162 std::string outputSizeVar;
163 std::string outputSize = fShape[0].param;
164 if (outputSize.find("range_size") != std::string::npos) {
165 outputSizeVar = outputSize;
166 outputSize = "static_cast<size_t>(std::max(std::ceil((static_cast<float>(*tensor_" + fNLimit +
167 ") - static_cast<float>(*tensor_" + fNStart + ")) / static_cast<float>(*tensor_" + fNDelta + ")), 0.0f))";
168 } else {
169 outputSizeVar = "range_" + opName;
170 }
171 out << SP << "size_t " << outputSizeVar << " = " << outputSize << ";\n";
172 out << SP << "for (size_t i = 0; i < " << outputSizeVar << "; i++) {\n";
173 out << SP << SP << "tensor_" << fNOutput << "[i] = *tensor_" << fNStart << " + i * (*tensor_" << fNDelta << ");\n";
174 out << SP << "}\n";
175
176 return out.str();
177 }
178};
179
180}//SOFIE
181}//Experimental
182}//TMVA
183
184#endif //TMVA_SOFIE_ROPERATOR_RANGE
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 data
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:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:193
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
Definition RModel.cxx:279
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:221
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:234
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:327
std::vector< T > GetTensorData(const std::string &name)
Definition RModel.hxx:247
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:229
ROperator_Range(std::string start, std::string limit, std::string delta, std::string nameOutput)
std::string Generate(std::string opName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
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:48
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertValuesToString(size_t n, const T *data)
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations
static void output()