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};
25
26template <typename T>
28 static const std::string Name() { return "Less"; }
29 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " < " + t2 + " ? true : false "; }
30};
31
32template <typename T>
34 static const std::string Name() { return "LessOrEqual"; }
35 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " <= " + t2 + " ? true : false "; }
36};
37
38template <typename T>
40 static const std::string Name() { return "Greater"; }
41 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " > " + t2 + " ? true : false "; }
42};
43
44template <typename T>
46 static const std::string Name() { return "GreaterOrEqual"; }
47 static std::string Op(const std::string & t1, const std::string t2) { return t1 + " >= " + t2 + " ? true : false " ; }
48};
49
50template<typename T, EComparisionOperator Op>
51class ROperator_Comparision final : public ROperator{
52private:
53
54 bool fIsModelOutput = false;
55 std::string fNX1;
56 std::string fNX2;
57 std::string fNY;
58 std::vector<size_t> fShapeX1;
59 std::vector<size_t> fShapeX2;
60 std::vector<size_t> fShapeY;
61 std::string fNBroadcastedX1;
62 std::string fNBroadcastedX2;
65 bool fBroadcast = false;
66
67
68public:
70 ROperator_Comparision(const std::string & nameX1, const std::string & nameX2, const std::string & nameY):
71 fNX1(UTILITY::Clean_name(nameX1)), fNX2(UTILITY::Clean_name(nameX2)), fNY(UTILITY::Clean_name(nameY)){}
72
73 // type of output given input
74 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
75 return input;
76 }
77
78 // shape of output tensors given input tensors
79 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
80 auto ret = input; // return vector size 1 with first input
81 return ret;
82 }
83
84 void Initialize(RModel& model) override {
85 // input must be a graph input, or already initialized intermediate tensor
87 throw std::runtime_error(std::string("TMVA SOFIE Comparision Op Input Tensor ") + fNX1 + "is not found in model");
88 }
89 if (!model.CheckIfTensorAlreadyExist(fNX2)) {
90 throw std::runtime_error(std::string("TMVA SOFIE Comparision Op Input Tensor ") + fNX2 + "is not found in model");
91 }
96 bool broadcast = !UTILITY::AreSameShape(fShapeX1, fShapeX2);
97 if (broadcast) {
98 // Y is the common shape of A and B
100 bool broadcastX1 = !UTILITY::AreSameShape(fShapeX1, fShapeY);
101 bool broadcastX2 = !UTILITY::AreSameShape(fShapeX2, fShapeY);
102 // Broadcast A to Y
103 if (broadcastX1) {
104 if (model.IsInitializedTensor(fNX1)) {
105 auto data = model.GetInitializedTensorData(fNX1);
106 std::shared_ptr<void> broadcastedData(
107 UTILITY::UnidirectionalBroadcast<T>(static_cast<T *>(data.get()), fShapeX1, fShapeY),
108 std::default_delete<T[]>());
109 // Update the data and the shape of A
110 model.UpdateInitializedTensor(fNX1, model.GetTensorType(fNX1), fShapeY, broadcastedData);
112 } else {
113 // Add an intermediate tensor for broadcasting A
114 fNBroadcastedX1 = "Broadcasted" + fNX1;
116 }
117 }
118 // Broadcast B to Y
119 if (broadcastX2) {
120 if (model.IsInitializedTensor(fNX2)) {
121 auto data = model.GetInitializedTensorData(fNX2);
122 std::shared_ptr<void> broadcastedData(
123 UTILITY::UnidirectionalBroadcast<T>(static_cast<T *>(data.get()), fShapeX2, fShapeY),
124 std::default_delete<T[]>());
125 // Update the data and the shape of B
126 model.UpdateInitializedTensor(fNX2, model.GetTensorType(fNX2), fShapeY, broadcastedData);
128 } else {
129 // Add an intermediate tensor for broadcasting B
130 fNBroadcastedX2 = "Broadcasted" + fNX2;
132 }
133 }
134 } else {
136 }
138 // check if this is not output operators to add a specific line for definining the tensor_xxx variable
139 const auto & outputTensorNames = model.GetOutputTensorNames();
140 fIsModelOutput = false;
141 if (std::find(outputTensorNames.begin(), outputTensorNames.end(), fNY) != outputTensorNames.end())
142 fIsModelOutput = true;
143 }
144
145 std::string Generate(std::string OpName) override {
146 OpName = "op_" + OpName;
147
148 if (fShapeY.empty()) {
149 throw std::runtime_error("TMVA SOFIE Comparision Op called to Generate without being initialized first");
150 }
151 std::stringstream out;
152 out << SP << "\n//------ " << ComparisionTrait<T,Op>::Name() << "\n";
154 // Broadcast A if it's uninitialized
155 if (!fNBroadcastedX1.empty()) {
156 std::string type1 = ConvertTypeToString(fTensorType1);
157 out << SP << "// Broadcasting uninitialized tensor " << fNX1 << "\n";
158 out << SP << "{\n";
159 out << SP << SP << type1 << "* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<" << type1 << ">(tensor_" << fNX1 << ", " << ConvertShapeToString(fShapeX1) << ", " << ConvertShapeToString(fShapeY) << ");\n";
160 out << SP << SP << "std::copy(data, data + " << length << ", tensor_" << fNBroadcastedX1 << ");\n";
161 out << SP << SP << "delete[] data;\n";
162 out << SP << "}\n";
163 }
164 // Broadcast B if it's uninitialized
165 if (!fNBroadcastedX2.empty()) {
166 std::string type2 = ConvertTypeToString(fTensorType2);
167 out << SP << "// Broadcasting uninitialized tensor " << fNX2 << "\n";
168 out << SP << "{\n";
169 out << SP << SP << type2 << "* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<" << type2 << ">(tensor_" << fNX2 << ", " << ConvertShapeToString(fShapeX2) << ", " << ConvertShapeToString(fShapeY) << ");\n";
170 out << SP << SP << "std::copy(data, data + " << length << ", tensor_" << fNBroadcastedX2 << ");\n";
171 out << SP << SP << "delete[] data;\n";
172 out << SP << "}\n";
173 }
174 const std::string& nameX1 = fNBroadcastedX1.empty()? fNX1 : fNBroadcastedX1;
175 const std::string& nameX2 = fNBroadcastedX2.empty()? fNX2 : fNBroadcastedX2;
176
177 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
178 out << SP << SP << "fTensor_" << fNY << "[id] = " << ComparisionTrait<T,Op>::Op( "tensor_" + nameX1 + "[id]" , "tensor_" + nameX2 + "[id]") << " ;\n";
179 out << SP << "}\n";
180 // 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
181 if (!fIsModelOutput)
182 out << SP << "const std::vector<bool> & tensor_" << fNY << " = fTensor_" << fNY << ";\n";
183
184 return out.str();
185 }
186
187};
188
189}//SOFIE
190}//Experimental
191}//TMVA
192
193
194#endif //TMVA_SOFIE_ROperator_Comparision
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 ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:94
const std::vector< std::string > & GetOutputTensorNames() const
Definition RModel.hxx:155
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
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 UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:255
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)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
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 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