Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Pad.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Pad
2#define TMVA_SOFIE_ROPERATOR_Pad
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{
17public:
19private:
20
21 std::string fNX;
22 std::string fNP;
23 std::string fNCV;
24 std::string fNAX;
25 std::string fNY;
28 std::vector<size_t> fInputShape;
29 std::vector<size_t> fOutputShape;
30 std::vector<std::pair<int64_t, int64_t>> fPads;
31
32public:
33
35 ROperator_Pad(const std::string & nameX, const std::string & nameP, const std::string & nameCV,
36 const std::string & nameAX, const std::string & nameY, const std::string & mode) :
37 fNX(UTILITY::Clean_name(nameX)), fNP(UTILITY::Clean_name(nameP)),
38 fNCV(UTILITY::Clean_name(nameCV)), fNAX(UTILITY::Clean_name(nameAX)),
39 fNY(UTILITY::Clean_name(nameY))
40 {
42 if (mode == "constant")
44 else if (mode == "reflect")
46 else if (mode == "edge")
47 fMode = kEdge;
48 else if (mode == "wrap")
49 fMode = kWrap;
50
53 }
54
55 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
56 return input;
57 }
58
59 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
60 auto ret = input; //suggest copy to compiler
61 return ret;
62 }
63
64 void Initialize(RModel& model) override {
65 if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
66 throw std::runtime_error("TMVA SOFIE Pad Op Input Tensor is not found in model");
67 }
68
70
71 if (fMode != EMode::kConstant) {
72 throw std::runtime_error("TMVA SOFIE Pad Op supports now only Constant mode");
73 }
74
75 // get pads data
76 int64_t * padsData = nullptr;
77 if (model.IsInitializedTensor(fNP)) {
78 padsData = static_cast<int64_t*>(model.GetInitializedTensorData(fNP).get());
79 } else {
80 throw std::runtime_error("TMVA SOFIE Pad Op supports now only initialized Pads data");
81 }
82 // get constant value
84 if (!fNCV.empty()) {
85 if (model.IsInitializedTensor(fNCV)) {
86 T * cData = static_cast<T*>(model.GetInitializedTensorData(fNCV).get());
88 } else {
89 throw std::runtime_error("TMVA SOFIE Pad Op supports now only initialized Constant Value data");
90 }
91 }
92 std::vector<int64_t> axes;
93 if (!fNAX.empty()) {
94 if (model.IsInitializedTensor(fNAX)) {
95 auto shape = model.GetTensorShape(fNAX);
96 // it should be a 1D tensor
97 size_t nax = shape[0];
98 // switch types
100 auto data = static_cast<int64_t*>(model.GetInitializedTensorData(fNAX).get());
101 axes = std::vector<int64_t>(data, data + nax);
102 } else if (model.GetTensorType(fNAX) == ETensorType::INT32) {
103 auto data = static_cast<int32_t*>(model.GetInitializedTensorData(fNAX).get());
104 axes.resize(nax);
105 for (size_t i = 0; i < nax; i++)
106 axes[i] = data[i];
107 } else {
108 throw std::runtime_error("TMVA SOFIE Pad Op invalid input Axes type");
109 }
110 } else {
111 throw std::runtime_error("TMVA SOFIE Pad Op supports now only initialized Axes data");
112 }
113 }
114
115
117 size_t axesSize = axes.size();
118 if (axesSize == 0) {
119 for (size_t i = 0; i < fInputShape.size(); i++) {
120 axes.push_back(i);
121 }
122 axesSize = fInputShape.size();
123 }
124 fPads.resize(fInputShape.size());
125 for (size_t i = 0; i < fInputShape.size(); i++) {
126 if (axes[i] < 0) axes[i] += fInputShape.size();
127 if (axes[i] == int64_t(i)) {
128 fPads[i].first = padsData[i];
129 fPads[i].second = padsData[axesSize + i];
130 int64_t outDim = static_cast<int64_t>(fOutputShape[i]) + fPads[i].first + fPads[i].second;
131 if (outDim < 0)
132 throw std::runtime_error("TMVA SOFIE Pad Op : invalid Pads values");
133 fOutputShape[i] = outDim;
134 }
135 }
136
138
139 if (model.Verbose()) {
140 std::cout << "initializing Pad operator with pads .. : ";
141 for (auto & p : fPads)
142 std::cout << "{ " << p.first << " , " << p.second << "} ";
143 std::cout << std::endl;
144 std::cout << "Pad: " << fNX << " " << ConvertShapeToString(fInputShape) << " -> " << fNY << " with shape " << ConvertShapeToString(fOutputShape)
145 << std::endl;
146 }
147
148 }
149
150
151 std::string Generate(std::string OpName) override {
152 OpName = "op_" + OpName;
153 if (fOutputShape.empty()){
154 throw std::runtime_error("TMVA SOFIE Operator Pad called to Generate without being initialized first");
155 }
156 std::stringstream out;
159 out << "\n//------ Pad\n";
160 // fill first output tensor with the constant values
162 int dims = fOutputShape.size();
163 out << "std::fill(tensor_" << fNY << ", tensor_" << fNY << " + " << length << ","
164 << fConstantValue << ");\n";
165
166 // copy now data from input tensor in output ones
167 for (int i = 0; i < dims; i++) {
168 for (int j = 1; j < i; j++) out << SP;
169 out << "for (int id" << i << " = 0; id" << i << " < " << fInputShape[i] << "; id"
170 << i << "++) {\n";
171 }
172 // compute index from strides
173 //linear_index = i_1 * stride[0] + i_2 * stride[1] + ... + i_N * stride[N-1]
174 for (int j = 0; j < dims; j++) out << SP;
175 out << "tensor_" << fNY << "[";
176 for (int i = 0; i < dims; i++) {
177 out << "(id" << i;
178 if (fPads[i].first != 0) out << " + " << fPads[i].first;
179 out << ")";
180 if (i < dims-1) out << " * " << outStride[i] << " + ";
181 }
182 out << "] =\n tensor_" << fNX << "[";
183 for (int i = 0; i < dims; i++) {
184 out << "id" << i;
185 if (i < dims-1) out << " * " << inputStride[i] << " + ";
186 }
187 out << "];\n";
188 for (int i = dims-1; i >= 0; i--) {
189 for (int j = 1; j < i; j++) out << SP;
190 out << "}\n";
191 }
192
193 return out.str();
194 }
195
196};
197
198}//SOFIE
199}//Experimental
200}//TMVA
201
202
203#endif //TMVA_SOFIE_ROPERATOR_Swish
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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
Option_t Option_t TPoint TPoint const char mode
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:200
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:95
const ETensorType & GetTensorType(std::string name) const
Definition RModel.cxx:67
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:175
const std::vector< size_t > & GetTensorShape(std::string name) const
Definition RModel.cxx:29
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:261
void Initialize(RModel &model) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string OpName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< std::pair< int64_t, int64_t > > fPads
ROperator_Pad(const std::string &nameX, const std::string &nameP, const std::string &nameCV, const std::string &nameAX, const std::string &nameY, const std::string &mode)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
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
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations