Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Identity.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_IDENTITY
2#define TMVA_SOFIE_ROPERATOR_IDENTITY
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14template <typename T>
16{
17
18private:
19
20 bool fIsInputInitialized = false;
21 std::string fNX;
22 std::string fNY;
23 std::vector<size_t> fShape;
24
25public:
27 ROperator_Identity(std::string nameX, std::string nameY):
28 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){
31 }
32
33 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
34 return input;
35 }
36
37 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
38 auto ret = input; //suggest copy to compiler
39 return ret;
40 }
41
42 void Initialize(RModel& model){
43 //input must be a graph input, or already initialized intermediate tensor
44 if (model.CheckIfTensorAlreadyExist(fNX) == false){
45 throw std::runtime_error("TMVA SOFIE Identity Op Input Tensor is not found in model");
46 }
47 fShape = model.GetTensorShape(fNX);
48 if (model.IsInitializedTensor(fNX)) {
49 // we need to check if is a weight (initialized) or a constant tensor
50 // in the first case we need to create a constant tensor with the output, in teh second we
51 // need to generate the identy code in the GenerateInitCode
52 if (model.IsConstantTensor(fNX)) {
53 auto inputData = static_cast<T*>(model.GetInitializedTensorData(fNX).get());
55 fIsOutputConstant = true;
56 } else {
58 // need to create a dummy intermediate tensor for the declaration
59 // this could probably be improved to save memory
61 }
62 } else
64 }
65
66 std::string GenerateInitCode() {
67 // generate init code for identity operator
68 if (!fIsInputInitialized) return "";
69 std::stringstream out;
70 out << "\n//------ IDENTITY\n";
71 // just copy the tensor pointers
72 out << SP << SP << "tensor_" << fNY << " = tensor_" << fNX << ";\n";
73 return out.str();
74 }
75
76
77 std::string Generate(std::string OpName){
78 if (fIsOutputConstant || fIsInputInitialized) return "";
79 OpName = "op_" + OpName;
80 if (fShape.empty()) {
81 throw std::runtime_error("TMVA SOFIE Operator Identity called to Generate without being initialized first");
82 }
83 std::stringstream out;
84 out << "\n//------ IDENTITY\n";
85 // just copy the tensor pointers
86 out << SP << SP << "tensor_" << fNY << " = tensor_" << fNX << ";\n";
87 return out.str();
88 }
89
90};
91
92}//SOFIE
93}//Experimental
94}//TMVA
95
96
97#endif //TMVA_SOFIE_ROPERATOR_IDENTITY
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
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:94
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:227
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:192
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:202
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:206
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:288
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
ROperator_Identity(std::string nameX, std::string nameY)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:44
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
create variable transformations