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
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
38 std::transform(fNYs.begin(), fNYs.end(), fOutputTensorNames.begin(),
39 [](const std::string& s) -> std::string_view { return s; });
40 }
41
42 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
43 return input;
44 }
45
46 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
47 auto ret = input; //suggest copy to compiler
48 return ret;
49 }
50
51 void Initialize(RModel& model) override {
52 //input must be a graph input, or already initialized intermediate tensor
53 if (model.CheckIfTensorAlreadyExist(fNX) == false){
54 throw std::runtime_error("TMVA SOFIE If Op Input Tensor is not found in model");
55 }
56 //add the subgraph model to parent RModel and initialize them
59
60 // generate input string signature for subgraphs
61 fInputSignature_modelThen = fModel_then->GenerateInferSignature(false);
62 fInputSignature_modelElse = fModel_else->GenerateInferSignature(false);
63
64 // add the outputs
65 for (size_t i = 0; i < fNYs.size(); i++) {
66 // assume shape of then tensor is same of else tensor
67 // if not need to make a parametric tensor output (tbd)
68 auto soutput_name = fModel_then->GetOutputTensorNames()[i];
69 auto shape = fModel_then->GetTensorShape(soutput_name);
70 auto type = fModel_then->GetTensorType(soutput_name);
71 if (i == 0)
72 fType = type;
73 else {
74 if (type != fType)
75 throw std::runtime_error("TMVA SOFIE If Op supports only all outputs of the same type");
76 }
77 model.AddIntermediateTensor(fNYs[i], fType, shape );
78 }
79
80 }
81
82
83 std::string Generate(std::string opName) override {
84 opName = "op_" + opName;
86 throw std::runtime_error("TMVA If operator called to Generate without being initialized first");
87 }
88 std::stringstream out;
89 //size_t length = ConvertShapeToLength(fShape);
90 std::string typeName = ConvertTypeToString(fType);
91 out << "\n//------ If operator\n";
92 out << SP << "std::vector<std::vector<" << typeName << ">> outputs_" << opName << ";\n";
93 // use the std::vector since is a boolean
94 out << SP << "if (fTensor_" << fNX << "[0] ) { \n";
95 // then branch
96 out << SP << SP << "outputs_" << opName << " = "
97 << "fSession_" << fModel_then->GetName() << ".infer(" << fInputSignature_modelThen << ");\n";
98 // else branch
99 out << SP << "} else {\n";
100 out << SP << SP << "outputs_" << opName << " = "
101 << "fSession_" + fModel_else->GetName() + ".infer(" << fInputSignature_modelElse << ");\n";
102 out << SP << "}\n";
103 // copy the outputs
104 out << SP << "if (outputs_" << opName << ".size() != " << fNYs.size() << ")\n";
105 out << SP << SP << "throw std::runtime_error(\" If operator: invalid output size!\");\n\n";
106 for (size_t i = 0; i < fNYs.size(); i++) {
107 out << SP << "std::copy(outputs_" << opName << "[" << i << "].begin(), outputs_" << opName << "[" << i << "].end(), fTensor_" << fNYs[i] << ".begin());\n";
108 }
109 return out.str();
110 }
111
112
113
114};
115
116}//SOFIE
117}//Experimental
118}//TMVA
119
120
121#endif //TMVA_SOFIE_ROPERATOR_Tanh
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 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:200
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:95
void InitializeSubGraph(std::shared_ptr< RModel > graph)
Definition RModel.cxx:484
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string opName) override
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::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
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
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