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>
15class ROperator_Expand final : public ROperator{
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)){}
33
34 // type of output given input
35 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
36 return input;
37 }
38
39 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
40 return input;
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)) {
46 throw std::runtime_error("TMVA SOFIE Expand Op Input Tensor " + fNX + " is not found in model");
47 }
48 fShapeX = model.GetTensorShape(fNX);
49 if (!model.IsInitializedTensor(fNShape)) {
50 throw std::runtime_error("TMVA::SOFIE - Tensor " + fNShape + " is not initialized.");
51 }
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 std::vector<size_t> shape(shapeData, shapeData + N);
60 // Y is the common shape of fShapeX and shape
62 fShapeX, shape);
64 // Broadcast X to the common shape fShapeY
65 bool broadcast = !UTILITY::AreSameShape(fShapeX, fShapeY);
66 if (broadcast && model.IsInitializedTensor(fNX)) {
67 // If X is an initialized tensor (constant)
68 auto data = model.GetInitializedTensorData(fNX);
69 std::shared_ptr<void> broadcastedData(
70 UTILITY::UnidirectionalBroadcast<float>(static_cast<float *>(data.get()), fShapeX, fShapeY),
71 std::default_delete<float[]>());
72 // Update the data and the shape of X
73 model.UpdateInitializedTensor(fNX, model.GetTensorType(fNX), fShapeY, broadcastedData);
75 }
78 if (model.Verbose())
79 std::cout << "Expand - output is with shape " << ConvertShapeToString(fShapeY) << std::endl;
80 }
81
82 std::string GenerateInitCode() override {
83 std::stringstream out;
84 return out.str();
85 }
86
87 std::string Generate(std::string OpName) override {
88 OpName = "op_" + OpName;
89
90 if (fShapeY.empty()) {
91 throw std::runtime_error("TMVA SOFIE Expand Op called to Generate without being initialized first");
92 }
93 std::stringstream out;
94 out << SP << "\n//------ Expand Op" << "\n";
96 // No need to broadcast A if it's an initialized tensor
97 if (fInitialized) {
98 out << "// Copying initialized tensor " << fNX << " to " << fNY << "\n";
99 out << SP << "std::copy(tensor_" << fNX << ", " << "tensor_" << fNX << " + " << length << ", tensor_" << fNY << ");\n";
100 } else {
101 out << SP << "// Broadcasting uninitialized tensor " << fNX << "\n";
102 out << SP << "{\n";
103 out << SP << SP << "float* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_" << fNX << ", " << ConvertShapeToString(fShapeX) << ", " << ConvertShapeToString(fShapeY) << ");\n";
104 out << SP << SP << "std::copy(data, data + " << length << ", tensor_" << fNY << ");\n";
105 out << SP << SP << "delete[] data;\n";
106 out << SP << "}\n";
107 }
108 return out.str();
109 }
110
111};
112
113}//SOFIE
114}//Experimental
115}//TMVA
116
117
118#endif //TMVA_SOFIE_ROperator_Expand
#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:203
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:188
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:264
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:255
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
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
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