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
38 fOutputTensorNames.resize(fNYs.size());
39 std::transform(fNYs.begin(), fNYs.end(), fOutputTensorNames.begin(),
40 [](const std::string& s) -> std::string_view { return s; });
41 }
42
43 void Initialize(RModel& model) override {
44 //input must be a graph input, or already initialized intermediate tensor
45 if (model.CheckIfTensorAlreadyExist(fNX) == false){
46 throw std::runtime_error("TMVA SOFIE If Op Input Tensor is not found in model");
47 }
48 //add the subgraph model to parent RModel and initialize them
49 model.InitializeSubGraph(fModel_then);
50 model.InitializeSubGraph(fModel_else);
51
52 // generate input string signature for subgraphs
53 fInputSignature_modelThen = fModel_then->GenerateInferSignature(false);
54 fInputSignature_modelElse = fModel_else->GenerateInferSignature(false);
55
56 // add the outputs
57 for (size_t i = 0; i < fNYs.size(); i++) {
58 // assume shape of then tensor is same of else tensor
59 // if not need to make a parametric tensor output (tbd)
60 auto soutput_name = fModel_then->GetOutputTensorNames()[i];
61 auto shape = fModel_then->GetTensorShape(soutput_name);
62 auto type = fModel_then->GetTensorType(soutput_name);
63 if (i == 0)
64 fType = type;
65 else {
66 if (type != fType)
67 throw std::runtime_error("TMVA SOFIE If Op supports only all outputs of the same type");
68 }
69 model.AddIntermediateTensor(fNYs[i], fType, shape );
70 }
71
72 }
73
74
75 std::string Generate(std::string opName) override {
76 opName = "op_" + opName;
78 throw std::runtime_error("TMVA If operator called to Generate without being initialized first");
79 }
80 std::stringstream out;
81 out << "\n//------ If operator\n";
82 // the subgraphs write directly into the output tensors of the If operator
83 std::string outputs;
84 for (size_t i = 0; i < fNYs.size(); i++) {
85 if (i > 0)
86 outputs += ", ";
87 outputs += "tensor_" + fNYs[i];
88 }
89 auto callBranch = [&](const std::shared_ptr<RModel> &model, const std::string &signature) {
90 return "doInfer(session.fSession_" + model->GetName() + ", " + signature + ", " + outputs + ");\n";
91 };
92 // the condition is a boolean tensor, only its first element is used
93 out << SP << "if (tensor_" << fNX << "[0]) {\n";
94 out << SP << SP << callBranch(fModel_then, fInputSignature_modelThen);
95 out << SP << "} else {\n";
96 out << SP << SP << callBranch(fModel_else, fInputSignature_modelElse);
97 out << SP << "}\n";
98 return out.str();
99 }
100
101
102
103};
104
105}//SOFIE
106}//Experimental
107}//TMVA
108
109
110#endif //TMVA_SOFIE_ROPERATOR_Tanh
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
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::string_view > fInputTensorNames
Definition ROperator.hxx:44
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:40
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:45
const Int_t n
Definition legend1.C:16
std::string Clean_name(std::string input_tensor_name)
create variable transformations