Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Expand.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROperator_Expand
2#define TMVA_SOFIE_ROperator_Expand
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>
16private:
17
18 std::vector<size_t> fShapeX;
19 std::vector<size_t> fShape;
20 std::vector<size_t> fShapeY;
21
22 std::string fNX;
23 std::string fNShape;
24 std::string fNY;
25 std::string fType;
26
27 bool fInitialized = false;
28
29public:
31 ROperator_Expand(std::string nameX, std::string nameShape, std::string nameY):
32 fNX(UTILITY::Clean_name(nameX)), fNShape(UTILITY::Clean_name(nameShape)), fNY(UTILITY::Clean_name(nameY)){
35 }
36
37 // type of output given input
38 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
39 return input;
40 }
41
42 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
43 return input;
44 }
45
46 void Initialize(RModel& model) override {
47 // input must be a graph input, or already initialized intermediate tensor
48 if (!model.CheckIfTensorAlreadyExist(fNX)) {
49 throw std::runtime_error("TMVA SOFIE Expand Op Input Tensor " + fNX + " is not found in model");
50 }
51 fShapeX = model.GetTensorShape(fNX);
52 if (!model.IsInitializedTensor(fNShape)) {
53 throw std::runtime_error("TMVA::SOFIE - Tensor " + fNShape + " is not initialized.");
54 }
55 int64_t *shapeData =
56 static_cast<int64_t *>(model.GetInitializedTensorData(fNShape).get());
58 if (fShape.size() != 1) {
59 throw std::runtime_error("TMVA::SOFIE - Expand operator shape must be a 1d tensor.");
60 }
61 size_t N = fShape[0];
62 std::vector<size_t> shape(shapeData, shapeData + N);
63 // Y is the common shape of fShapeX and shape
65 fShapeX, shape);
67 // Broadcast X to the common shape fShapeY
69 if (model.IsInitializedTensor(fNX)) {
70 // If X is an initialized tensor (constant)
71 auto data = model.GetInitializedTensorData(fNX);
72 if (broadcast) {
73 std::shared_ptr<void> broadcastedData(
74 UTILITY::UnidirectionalBroadcast<T>(static_cast<T *>(data.get()), fShapeX, fShapeY),
75 std::default_delete<T[]>());
76 // Update the data and the shape of X
79 // need to set as a not writable tensor
82 }
83 if (broadcast || model.IsConstantTensor(fNX)) {
84 fIsOutputConstant = true; // constant output in this case
86 fOutputTensorNames.pop_back();
87 } else {
89 }
90 } else {
91 // case input is not initialized
93 }
95 if (model.Verbose())
96 std::cout << "Expand - output is with shape " << ConvertShapeToString(fShapeY) << std::endl;
97 }
98
99 std::string GenerateInitCode() override {
100 std::stringstream out;
101 if (!fIsOutputConstant && (fInitialized || fShapeX == fShapeY ) ) {
103 out << "// Copying initialized tensor " << fNX << " to " << fNY << "\n";
104 out << SP << "std::copy(tensor_" << fNX << ", " << "tensor_" << fNX << " + " << length << ", tensor_" << fNY << ");\n";
105 }
106 return out.str();
107 }
108
109 std::string Generate(std::string OpName) override {
110 if (fIsOutputConstant) return "";
111 OpName = "op_" + OpName;
112 if (fShapeY.empty()) {
113 throw std::runtime_error("TMVA SOFIE Expand Op called to Generate without being initialized first");
114 }
115 std::stringstream out;
116 out << SP << "\n//------ Expand Op" << "\n";
117 // No need to broadcast A if it's an initialized tensor or shapes are the same
118 if (!fInitialized && fShapeX != fShapeY) {
119 out << SP << "// Broadcasting uninitialized tensor " << fNX << "\n";
120 out << SP << "TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<" << fType << ">(tensor_" << fNX << ", " << ConvertShapeToString(fShapeX) << ", " << ConvertShapeToString(fShapeY)
121 << ", std::span<"<<fType<<">(tensor_"<<fNY<<", "<<ConvertShapeToLength(fShapeY)<<"));\n";
122 }
123 return out.str();
124 }
125
126};
127
128}//SOFIE
129}//Experimental
130}//TMVA
131
132
133#endif //TMVA_SOFIE_ROperator_Expand
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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: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
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:297
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:279
ROperator_Expand(std::string nameX, std::string nameShape, std::string nameY)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string OpName) override
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
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t >, std::vector< size_t >)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations