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<Dim> fShapeX;
19 std::vector<size_t> fShape;
20 std::vector<Dim> fShapeY;
21 std::vector<Dim> fShapeDim;
22
23 std::string fNX;
24 std::string fNShape;
25 std::string fNY;
26 std::string fType;
27
28 bool fInitialized = false;
29 bool fInitializedShape = false;
30 bool fDimShapeValues = false;
31 bool fInitBroadcast = false;
32
33public:
35 ROperator_Expand(std::string nameX, std::string nameShape, std::string nameY):
36 fNX(UTILITY::Clean_name(nameX)), fNShape(UTILITY::Clean_name(nameShape)), fNY(UTILITY::Clean_name(nameY)){
39 }
40
41
42 void Initialize(RModel& model) override {
43 // the generated code may use the UnidirectionalBroadcast inference helper
44 model.AddNeededHelperFunction("UnidirectionalBroadcast");
45 // input must be a graph input, or already initialized intermediate tensor
46 if (!model.CheckIfTensorAlreadyExist(fNX)) {
47 throw std::runtime_error("TMVA SOFIE Expand Op Input Tensor " + fNX + " is not found in model");
48 }
50 if (model.IsInitializedTensor(fNShape)) {
51 fInitializedShape = true;
52 int64_t *shapeData =
53 static_cast<int64_t *>(model.GetInitializedTensorData(fNShape).get());
55 if (fShape.size() != 1) {
56 throw std::runtime_error("TMVA::SOFIE - Expand operator shape must be a 1d tensor.");
57 }
58 size_t N = fShape[0];
59 // what do we do if shapeData contains negative values?
60 for (size_t i = 0; i < N; i++) {
61 if ( shapeData[i] < 0)
62 throw std::runtime_error("TMVA::SOFIE - Expand: invalid shape value " + std::to_string(shapeData[i]));
63 }
64 std::vector<size_t> shape(shapeData, shapeData + N);
66 } else if (model.IsShapeTensor(fNShape)) {
67 // case input shape is a shape tensor
69 fDimShapeValues = true;
70 } else {
71 // assume shape of input shape is known (size is 1)
74 for (size_t i = 0; i < fShapeDim.size(); i++) {
75 fShapeDim[i] = Dim{std::string("v_") + fNShape + "_" + std::to_string(i)};
76 model.AddShapeParam(fShapeDim[i].param);
77 }
78 }
79 // Y is the common shape of fShapeX and shape
81 fShapeY = ret.second;
83 std::vector<size_t> shapeX;
84 std::vector<size_t> shapeY;
85 // case shape tensor and input shape are known
86 if (!model.IsDynamicTensor(fNX) && !model.IsDimInputTensor(fNX) && fInitializedShape) {
90 fInitBroadcast = true;
91 }
92 if (fInitialized) {
93 // cannot have Dim initialized tensors
94 assert(!shapeX.empty() && !shapeY.empty());
95 // Broadcast X to the common shape shapeY
96 // If X is an initialized tensor (constant)
97 auto data = model.GetInitializedTensorData(fNX);
98 if (fInitBroadcast) {
99 std::shared_ptr<void> broadcastedData(
100 UTILITY::UnidirectionalBroadcast(static_cast<T *>(data.get()), shapeX, shapeY),
101 std::default_delete<T[]>());
102 // Update the data and the shape of X
105 // need to set as a not writable tensor
108 }
109 if (fInitBroadcast || model.IsConstantTensor(fNX)) {
110 fIsOutputConstant = true; // constant output in this case
112 fOutputTensorNames.pop_back();
113 } else {
115 }
116 } else {
117 // // case input is not initialized
119 }
121 if (model.Verbose()) {
122 std::cout << "Expand - input " << fNX << " shape " << ConvertDimShapeToString(fShapeX) << " --> " << fNY << " shape "
123 << ConvertDimShapeToString(fShapeY) << (fIsOutputConstant ? ConvertValuesToString(model.GetTensorData<T>(fNY)) + " (constant)" : "") << std::endl;
124 }
125 }
126
127 std::string GenerateInitCode() override {
128 std::stringstream out;
130 // shapeX and shapeY are the same in this case
132 out << "// Copying initialized tensor " << fNX << " to " << fNY << "\n";
133 out << SP << "std::copy(tensor_" << fNX << ", " << "tensor_" << fNX << " + " << length << ", tensor_" << fNY << ");\n";
134 }
135 return out.str();
136 }
137
138 std::string Generate(std::string opName) override {
139 if (fIsOutputConstant) return "";
140 opName = "op_" + opName;
141 if (fShapeY.empty()) {
142 throw std::runtime_error("TMVA SOFIE Expand Op called to Generate without being initialized first");
143 }
144 std::stringstream out;
145 out << SP << "\n//------ Expand " << opName << " --> " << ConvertDimShapeToString(fShapeY) << "\n";
146 // need to declare shape parameters for non initialized shapes
148 for (size_t i = 0; i < fShapeDim.size(); i++) {
149 out << SP << "size_t " << fShapeDim[i] << " = " << "tensor_" << fNShape << "[" << i << "];\n";
150 }
151 }
152 // No need to broadcast A if it's an initialized tensor or shapes are the same
155 if (lengthX != lengthY) {
156 out << SP << "if ( (" << lengthX << ") < (" << lengthY << ") ) {\n";
157 out << SP << SP << "// Broadcasting uninitialized tensor " << fNX << "\n";
158 out << SP << SP << "UTILITY::UnidirectionalBroadcast(tensor_" << fNX << ", " << ConvertDimShapeToString(fShapeX) << ", " << ConvertDimShapeToString(fShapeY)
159 << ", tensor_"<<fNY<<");\n";
160 out << SP << "} else {\n";
161 out << SP << SP << "std::copy(tensor_" << fNX << ", " << "tensor_" << fNX << " + (" << lengthX << "), tensor_" << fNY << ");\n";
162 out << SP << "}\n";
163 } else {
164 // case of equal length even if shapes are dims
165 out << SP << "std::copy(tensor_" << fNX << ", " << "tensor_" << fNX << " + (" << lengthX << "), tensor_" << fNY << ");\n";
166 }
167
168 return out.str();
169 }
170
171};
172
173}//SOFIE
174}//Experimental
175}//TMVA
176
177
178#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 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
void AddNeededHelperFunction(std::string name)
void AddShapeParam(const std::string &name, size_t def_value=0)
Definition RModel.cxx:345
std::vector< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:64
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:100
bool IsDynamicTensor(const std::string &name) const
Definition RModel.cxx:296
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:311
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:157
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:242
bool IsDimInputTensor(const std::string &name) const
Definition RModel.cxx:301
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
Definition RModel.cxx:270
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:283
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:287
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:376
std::vector< T > GetTensorData(const std::string &name)
Definition RModel.hxx:245
void SetNotWritableInitializedTensor(const std::string &tensor_name)
Definition RModel.cxx:385
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:125
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:367
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
Definition RModel.cxx:278
ROperator_Expand(std::string nameX, std::string nameShape, std::string nameY)
std::string Generate(std::string opName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:50
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:47
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:45
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:51
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
T * UnidirectionalBroadcast(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations