Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_SubGraph.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_SubGraph
2#define TMVA_SOFIE_ROPERATOR_SubGraph
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14 // operator dealing with subgraphs (such as If , Loop, etc..)
15
16class ROperator_If final : public ROperator
17{
18
19private:
20
21 std::string fNX;
22 ETensorType fType = ETensorType::UNDEFINED; // output type (support only one common type)
23 std::vector<std::string> fNYs;
24 std::shared_ptr<RModel> fModel_then;
25 std::shared_ptr<RModel> fModel_else;
28
29public:
31 ROperator_If(const std::string & nameX, const std::vector<std::string> & nameYs, std::unique_ptr<RModel> model_then, std::unique_ptr<RModel> model_else):
32 fNX(UTILITY::Clean_name(nameX)), fNYs(nameYs), fModel_then(std::move(model_then)), fModel_else(std::move(model_else))
33 {
34 for (auto & n : fNYs)
36 }
37
38 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
39 return input;
40 }
41
42 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
43 auto ret = input; //suggest copy to compiler
44 return ret;
45 }
46
47 void Initialize(RModel& model){
48 //input must be a graph input, or already initialized intermediate tensor
49 if (model.CheckIfTensorAlreadyExist(fNX) == false){
50 throw std::runtime_error("TMVA SOFIE If Op Input Tensor is not found in model");
51 }
52 //add the subgraph model to parent RModel and initialize them
55
56 // generate input string signature for subgraphs
57 fInputSignature_modelThen = fModel_then->GenerateInferSignature(false);
58 fInputSignature_modelElse = fModel_else->GenerateInferSignature(false);
59
60 // add the outputs
61 for (size_t i = 0; i < fNYs.size(); i++) {
62 // assume shape of then tensor is same of else tensor
63 // if not need to make a parametric tensor output (tbd)
64 auto soutput_name = fModel_then->GetOutputTensorNames()[i];
65 auto shape = fModel_then->GetTensorShape(soutput_name);
66 auto type = fModel_then->GetTensorType(soutput_name);
67 if (i == 0)
68 fType = type;
69 else {
70 if (type != fType)
71 throw std::runtime_error("TMVA SOFIE If Op supports only all outputs of the same type");
72 }
73 model.AddIntermediateTensor(fNYs[i], fType, shape );
74 }
75
76 }
77
78
79 std::string Generate(std::string opName){
80 opName = "op_" + opName;
82 throw std::runtime_error("TMVA If operator called to Generate without being initialized first");
83 }
84 std::stringstream out;
85 //size_t length = ConvertShapeToLength(fShape);
86 std::string typeName = ConvertTypeToString(fType);
87 out << "\n//------ If operator\n";
88 out << SP << "std::vector<std::vector<" << typeName << ">> outputs_" << opName << ";\n";
89 // use the std::vector since is a boolean
90 out << SP << "if (fTensor_" << fNX << "[0] ) { \n";
91 // then branch
92 out << SP << SP << "outputs_" << opName << " = "
93 << "fSession_" << fModel_then->GetName() << ".infer(" << fInputSignature_modelThen << ");\n";
94 // else branch
95 out << SP << "} else {\n";
96 out << SP << SP << "outputs_" << opName << " = "
97 << "fSession_" + fModel_else->GetName() + ".infer(" << fInputSignature_modelElse << ");\n";
98 out << SP << "}\n";
99 // copy the outputs
100 out << SP << "if (outputs_" << opName << ".size() != " << fNYs.size() << ")\n";
101 out << SP << SP << "throw std::runtime_error(\" If operator: invalid output size!\");\n\n";
102 for (size_t i = 0; i < fNYs.size(); i++) {
103 out << SP << "std::copy(outputs_" << opName << "[" << i << "].begin(), outputs_" << opName << "[" << i << "].end(), fTensor_" << fNYs[i] << ".begin());\n";
104 }
105 return out.str();
106 }
107
108
109
110};
111
112}//SOFIE
113}//Experimental
114}//TMVA
115
116
117#endif //TMVA_SOFIE_ROPERATOR_Tanh
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
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
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:378
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
ROperator_If(const std::string &nameX, const std::vector< std::string > &nameYs, std::unique_ptr< RModel > model_then, std::unique_ptr< RModel > model_else)
std::string Generate(std::string opName)
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:41
const Int_t n
Definition legend1.C:16
std::string Clean_name(std::string input_tensor_name)
std::string ConvertTypeToString(ETensorType type)
create variable transformations