Logo ROOT  
Reference Guide
ROperator_BasicBinary.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROperator_BasicBinary
2#define TMVA_SOFIE_ROperator_BasicBinary
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
15
16template <typename T, EBasicBinaryOperator Op1>
18 const char *Name() { return ""; }
19 const char *Op() { return ""; }
20};
21template <typename T>
23 static const char *Name() { return "Add"; }
24 static const char *Op() { return "+"; }
25};
26
27template <typename T>
29 static const char *Name() { return "Sub"; }
30 static const char *Op() { return "-"; }
31};
32
33template <typename T>
35 static const char *Name() { return "Mul"; }
36 static const char *Op() { return "*"; }
37};
38
39template <typename T>
41 static const char *Name() { return "Div"; }
42 static const char *Op() { return "/"; }
43};
44
45template<typename T, EBasicBinaryOperator Op>
46class ROperator_BasicBinary final : public ROperator{
47private:
48
49 std::string fNX1;
50 std::string fNX2;
51 std::string fNY;
52 std::vector<size_t> fShape;
53
54
55public:
57 ROperator_BasicBinary(std::string nameX1, std::string nameX2, std::string nameY):
58 fNX1(UTILITY::Clean_name(nameX1)), fNX2(UTILITY::Clean_name(nameX2)), fNY(UTILITY::Clean_name(nameY)){}
59
60 // type of output given input
61 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
62 return input;
63 }
64
65 // shape of output tensors given input tensors
66 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
67 // assume now inputs have same shape (no broadcasting)
68 auto ret = std::vector<std::vector<size_t>>(1, input[0]); // return vector size 1 with first input
69 return ret;
70 }
71
72 void Initialize(RModel& model){
73 // input must be a graph input, or already initialized intermediate tensor
74 if (model.CheckIfTensorAlreadyExist(fNX1) == false){
75 throw std::runtime_error(std::string("TMVA SOFIE Binary Op Input Tensor ") + fNX1 + "is not found in model");
76 }
77 if (model.CheckIfTensorAlreadyExist(fNX2) == false) {
78 throw std::runtime_error(std::string("TMVA SOFIE Binary Op Input Tensor ") + fNX2 + "is not found in model");
79 }
80 auto shapeX1 = model.GetTensorShape(fNX1);
81 auto shapeX2 = model.GetTensorShape(fNX2);
82 // If the shape of 2 tensors are not same we perform multi-directional Broadcasting.
83 // We only support tensors with same length and the resultant output length should also be same.
84 if (shapeX1 != shapeX2) {
86 size_t length1 = ConvertShapeToLength(shapeX1);
87 size_t length2 = ConvertShapeToLength(shapeX2);
88 size_t output_length = ConvertShapeToLength(fShape);
89 if(length1 != length2 || length1 != output_length){
90 throw std::runtime_error(std::string("TMVA SOFIE Binary Op does not support input tensors with different lengths. The output tensor should also have the same length as the input tensors."));
91 }
92 }
93 // If both the tensors have same shape then assign the same shape to resultant output.
94 else if(shapeX1 == shapeX2){
95 fShape = shapeX1;
96 }
98 }
99
100
101 std::string Generate(std::string OpName){
102 OpName = "op_" + OpName;
103
104 if (fShape.empty()) {
105 throw std::runtime_error("TMVA SOFIE Binary Op called to Generate without being initialized first");
106 }
107 std::stringstream out;
109 out << "\n//------ " + std::string(BinaryOperatorTrait<T,Op>::Name())+"\n";
110 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
111 out << SP << SP << "tensor_" << fNY << "[id] = tensor_" << fNX1 << "[id]" +
112 std::string(BinaryOperatorTrait<T,Op>::Op()) + "tensor_" << fNX2 << "[id];\n";
113 out << SP << "}\n";
114 return out.str();
115 }
116
117};
118
119}//SOFIE
120}//Experimental
121}//TMVA
122
123
124#endif //TMVA_SOFIE_ROperator_BasicBinary
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:79
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition: RModel.cxx:149
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition: RModel.cxx:100
const std::vector< size_t > & GetTensorShape(std::string name)
Definition: RModel.cxx:58
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
ROperator_BasicBinary(std::string nameX1, std::string nameX2, std::string nameY)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
const std::string SP
space used to correctly indent the generated C++ code
Definition: ROperator.hxx:39
double T(double x)
Definition: ChebyshevPol.h:34
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > Multidirectional_broadcast(std::vector< size_t > input1_shape, std::vector< size_t > input2_shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations