Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Comparision.hxx
Go to the documentation of this file.
1
2#ifndef TMVA_SOFIE_ROperator_Comparision
3#define TMVA_SOFIE_ROperator_Comparision
4
6#include "TMVA/ROperator.hxx"
7#include "TMVA/RModel.hxx"
8
9#include <sstream>
10
11namespace TMVA{
12namespace Experimental{
13namespace SOFIE{
14
16
17template <typename T, EComparisionOperator Op1>
19
20template <typename T>
21struct ComparisionTrait<T, Eq> {
22 static const std::string Name() { return "Equal"; }
23 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " == " + t2 + " ? true : false "; }
24 static bool Result(T v1, T v2) { return v1 == v2;}
25};
26
27template <typename T>
29 static const std::string Name() { return "Less"; }
30 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " < " + t2 + " ? true : false "; }
31 static bool Result(T v1, T v2) { return v1 < v2;}
32};
33
34template <typename T>
36 static const std::string Name() { return "LessOrEqual"; }
37 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " <= " + t2 + " ? true : false "; }
38 static bool Result(T v1, T v2) { return v1 <= v2;}
39};
40
41template <typename T>
43 static const std::string Name() { return "Greater"; }
44 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " > " + t2 + " ? true : false "; }
45 static bool Result(T v1, T v2) { return v1 > v2;}
46};
47
48template <typename T>
50 static const std::string Name() { return "GreaterOrEqual"; }
51 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " >= " + t2 + " ? true : false " ; }
52 static bool Result(T v1, T v2) { return v1 >= v2;}
53};
54
55template<typename T, EComparisionOperator Op>
57private:
58
59 bool fIsModelOutput = false;
60 std::string fNX1;
61 std::string fNX2;
62 std::string fNY;
63 std::vector<size_t> fShapeX1;
64 std::vector<size_t> fShapeX2;
65 std::vector<size_t> fShapeY;
66 std::string fNBroadcastedX1;
67 std::string fNBroadcastedX2;
70 bool fBroadcast = false;
71
72
73public:
75 ROperator_Comparision(const std::string & nameX1, const std::string & nameX2, const std::string & nameY):
76 fNX1(UTILITY::Clean_name(nameX1)), fNX2(UTILITY::Clean_name(nameX2)), fNY(UTILITY::Clean_name(nameY)){
78
79 // output will be a boolean vector so should not be considered for memory optimized pool
81 }
82
83 // type of output given input
84 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
85 return input;
86 }
87
88 // shape of output tensors given input tensors
89 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
90 auto ret = input; // return vector size 1 with first input
91 return ret;
92 }
93
94 void Initialize(RModel& model) override {
95 // input must be a graph input, or already initialized intermediate tensor
97 throw std::runtime_error(std::string("TMVA SOFIE Comparision Op Input Tensor ") + fNX1 + "is not found in model");
98 }
99 if (!model.CheckIfTensorAlreadyExist(fNX2)) {
100 throw std::runtime_error(std::string("TMVA SOFIE Comparision Op Input Tensor ") + fNX2 + "is not found in model");
101 }
107 if (broadcast) {
108 // Y is the common shape of A and B
112 // Broadcast A to Y
113 if (broadcastX1) {
114 if (model.IsInitializedTensor(fNX1)) {
115 auto data = model.GetInitializedTensorData(fNX1);
116 std::shared_ptr<void> broadcastedData(
117 UTILITY::UnidirectionalBroadcast<T>(static_cast<T *>(data.get()), fShapeX1, fShapeY),
118 std::default_delete<T[]>());
119 // Update the data and the shape of A
122 } else {
123 // Add an intermediate tensor for broadcasting A
124 fNBroadcastedX1 = "Broadcasted" + fNX1;
126 }
127 }
128 // Broadcast B to Y
129 if (broadcastX2) {
130 if (model.IsInitializedTensor(fNX2)) {
131 auto data = model.GetInitializedTensorData(fNX2);
132 std::shared_ptr<void> broadcastedData(
133 UTILITY::UnidirectionalBroadcast<T>(static_cast<T *>(data.get()), fShapeX2, fShapeY),
134 std::default_delete<T[]>());
135 // Update the data and the shape of B
138 } else {
139 // Add an intermediate tensor for broadcasting B
140 fNBroadcastedX2 = "Broadcasted" + fNX2;
142 }
143 }
144 } else {
146 }
147 // case of constant tensors
148 if (model.IsInitializedTensor(fNX1) && model.IsInitializedTensor(fNX2) ) {
149 fIsOutputConstant = true;
150 auto data1 = static_cast<T *>(model.GetInitializedTensorData(fNX1).get());
151 auto data2 = static_cast<T *>(model.GetInitializedTensorData(fNX2).get());
153 bool * outData = new bool[length];
154 for (size_t i = 0; i < length; i++)
157 if (model.Verbose())
158 std::cout << ComparisionTrait<T,Op>::Name() << " op ---> " << fNY << " " << ConvertShapeToString(fShapeY) << " : "
159 << ConvertValuesToString(length,outData) << std::endl;
160 delete [] outData;
161 } else {
163 }
164 // check if this is not output operators to add a specific line for definining the tensor_xxx variable
165 const auto & outputTensorNames = model.GetOutputTensorNames();
166 fIsModelOutput = false;
168 fIsModelOutput = true;
169 }
170
171 std::string Generate(std::string OpName) override {
172 if (fIsOutputConstant) return "";
173 OpName = "op_" + OpName;
174
175 if (fShapeY.empty()) {
176 throw std::runtime_error("TMVA SOFIE Comparision Op called to Generate without being initialized first");
177 }
178 std::stringstream out;
179 out << SP << "\n//------ " << ComparisionTrait<T,Op>::Name() << "\n";
181 // Broadcast A if it's uninitialized
182 if (!fNBroadcastedX1.empty()) {
184 out << SP << "// Broadcasting uninitialized tensor " << fNX1 << "\n";
185 out << SP << "{\n";
186 out << SP << SP << type1 << "* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<" << type1 << ">(tensor_" << fNX1 << ", " << ConvertShapeToString(fShapeX1) << ", " << ConvertShapeToString(fShapeY) << ");\n";
187 out << SP << SP << "std::copy(data, data + " << length << ", tensor_" << fNBroadcastedX1 << ");\n";
188 out << SP << SP << "delete[] data;\n";
189 out << SP << "}\n";
190 }
191 // Broadcast B if it's uninitialized
192 if (!fNBroadcastedX2.empty()) {
194 out << SP << "// Broadcasting uninitialized tensor " << fNX2 << "\n";
195 out << SP << "{\n";
196 out << SP << SP << type2 << "* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<" << type2 << ">(tensor_" << fNX2 << ", " << ConvertShapeToString(fShapeX2) << ", " << ConvertShapeToString(fShapeY) << ");\n";
197 out << SP << SP << "std::copy(data, data + " << length << ", tensor_" << fNBroadcastedX2 << ");\n";
198 out << SP << SP << "delete[] data;\n";
199 out << SP << "}\n";
200 }
201 const std::string& nameX1 = fNBroadcastedX1.empty()? fNX1 : fNBroadcastedX1;
202 const std::string& nameX2 = fNBroadcastedX2.empty()? fNX2 : fNBroadcastedX2;
203
204 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
205 out << SP << SP << "fTensor_" << fNY << "[id] = " << ComparisionTrait<T,Op>::Op( "tensor_" + nameX1 + "[id]" , "tensor_" + nameX2 + "[id]") << " ;\n";
206 out << SP << "}\n";
207 // since output is a boolean need to add the tensor_xxx variable since it is not defined as a pointer to a boolean std::vector
208 if (!fIsModelOutput)
209 out << SP << "const std::vector<bool> & tensor_" << fNY << " = fTensor_" << fNY << ";\n";
210
211 return out.str();
212 }
213
214};
215
216}//SOFIE
217}//Experimental
218}//TMVA
219
220
221#endif //TMVA_SOFIE_ROperator_Comparision
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 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_iterator begin() const
const_iterator end() const
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:94
const std::vector< std::string > & GetOutputTensorNames() const
Definition RModel.hxx:172
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 UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:279
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string OpName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
ROperator_Comparision(const std::string &nameX1, const std::string &nameX2, const std::string &nameY)
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
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t >, std::vector< size_t >)
std::string ConvertValuesToString(size_t n, const T *data)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations
static std::string Op(const std::string &t1, const std::string t2)
static std::string Op(const std::string &t1, const std::string t2)
static std::string Op(const std::string &t1, const std::string t2)
static std::string Op(const std::string &t1, const std::string t2)
static std::string Op(const std::string &t1, const std::string t2)
auto * t1
Definition textangle.C:20