Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_GatherND.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_GatherND
2#define TMVA_SOFIE_ROPERATOR_GatherND
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9#include <stdexcept>
10#include <string>
11
12namespace TMVA{
13namespace Experimental{
14namespace SOFIE{
15
17{
18private:
19
20 size_t fBatchDims = 0;
21 std::string fNX;
22 std::string fNIndices;
23 std::string fNY;
24
25 std::vector<Dim> fShapeX;
26 std::vector<Dim> fShapeIndices;
27 std::vector<Dim> fShapeY;
28
29 std::vector<int64_t> fIndices; // indices vector in case they are known at initialization
30
31 std::string fType;
32
33public:
35 ROperator_GatherND(int batch_dims, std::string nameX, std::string nameIndices, std::string nameY):
36 fBatchDims(batch_dims), fNX(UTILITY::Clean_name(nameX)), fNIndices(UTILITY::Clean_name(nameIndices)), fNY(UTILITY::Clean_name(nameY)) {
39 }
40
41 void Initialize(RModel& model) override {
42 if (!model.CheckIfTensorAlreadyExist(fNX)) {
43 throw std::runtime_error("TMVA SOFIE GatherND Op Input Tensor " + fNX + " is not found in model");
44 }
46 if (model.Verbose())
47 std::cout << "GatherND - initial shape " << ConvertShapeToString(fShapeX) << " shape of indices "
48 << ConvertShapeToString(model.GetDimTensorShape(fNIndices)) << std::endl;
49 // fShapeIndices can be dynamic
51 size_t q = fShapeIndices.size();
52 // Axis in range [0, r) where r=rank(X)
53 size_t r = fShapeX.size();
54
55 if (q < 1) {
56 throw std::runtime_error("TMVA SOFIE GatherND : rank of Indices is < 1");
57 }
58 if (r < 1) {
59 throw std::runtime_error("TMVA SOFIE GatherND : rank of input tensor is < 1");
60 }
61 if (fBatchDims >= std::min(q,r)) {
62 throw std::runtime_error("TMVA SOFIE GatherND : invalid batch dim value");
63 }
64 if (fBatchDims > 0) {
65 for (size_t i = 0; i < fBatchDims; i++) {
66 if (fShapeX[i] != fShapeIndices[i]) {
67 std::cout << " input shape " << ConvertShapeToString(fShapeX) << " "
68 << " index shape " << ConvertShapeToString(fShapeIndices) << std::endl;
69 throw std::runtime_error("TMVA SOFIE GatherND : invalid input or index shape for " + std::to_string(i));
70 }
71 }
72 }
73
74 //general case. Assumption is that last dimension of index shape is known (is not dynamic)
75 if (fShapeIndices.back().isParam)
76 throw std::runtime_error("TMVA SOFIE GatherND : Index_shape(-1) is not known");
77
78 // output shape size (output rank)
79 // is (q-1)+r -index_shape[-1]
80 size_t last_index_shape = fShapeIndices.back().dim;
82 throw std::runtime_error("TMVA SOFIE GatherND : Index_shape(-1) has wrong value " +
83 std::to_string(last_index_shape));
84 }
85
86 size_t output_rank = r + q -1 - last_index_shape - fBatchDims;
87 //fShapeY.resize(output_rank);
88 // first index shape dimensions are same in output
89 fShapeY = std::vector<Dim>(fShapeIndices.begin(), fShapeIndices.end() - 1);
90 fShapeY.insert(fShapeY.end(), fShapeX.begin() + fBatchDims + last_index_shape, fShapeX.end());
91 if (fShapeY.size() != output_rank) {
92 std::cout << " input shape " << ConvertShapeToString(fShapeX) << " "
93 << " index shape " << ConvertShapeToString(fShapeIndices)
94 << " output shape " << ConvertShapeToString(fShapeY)
95 << " and output rank should be " << output_rank << std::endl;
96 throw std::runtime_error("TMVA SOFIE GatherND : Something is wrong in initialization ");
97 }
98
99 if (!fIsOutputConstant) {
100 // Add output tensor
103 if (model.Verbose())
104 std::cout << "GatherND: input " << fNX << " " << ConvertShapeToString(fShapeX) << " indices " << fNIndices << ConvertShapeToString(fShapeIndices)
105 << " -> " << fNY << " with shape " << ConvertShapeToString(fShapeY) << std::endl;
106 }
107
108
109
110 // // case indices tensor is initialized
111 // if (model.IsInitializedTensor(fNIndices)) {
112 // // empty shape Indices is a scalar value for the indices
113 // size_t indicesLength = ConvertShapeToLength(model.GetTensorShape(fNIndices));
114 // int64_t* indicesData = static_cast<int64_t*>(model.GetInitializedTensorData(fNIndices).get());
115 // //flag index tensor as not writable (not sure this is needed since index tensor might be used in generated code)
116 // model.SetNotWritableInitializedTensor(fNIndices);
117 // // update indices data in case of negative dim values
118 // for (size_t i = 0; i < indicesLength; i++) {
119 // // move this at generation time?
120 // if (!fShapeX[fAttrAxis].isParam) {
121 // if (indicesData[i] < 0) {
122 // indicesData[i] += fShapeX[fAttrAxis].dim;
123 // }
124 // }
125 // }
126 // // Save in a vector GatherND Indices of size q
127 // fIndices = std::vector<int64_t>(indicesData, indicesData + indicesLength);
128 // }
129
130 // case input is known (type is an integer) and input indices is a scalar (or vector of size 1)
131 // if (model.IsInitializedTensor(fNX) && q <= 1 && r == 1 && fIndices.size() > 0) {
132 // auto shapeX = ConvertShapeToInt(fShapeX); // we assume model is not dynamic
133 // auto shapeY = ConvertShapeToInt(fShapeY);
134 // if (model.GetTensorType(fNX) == ETensorType::INT64) {
135 // auto inputData = static_cast<int64_t*>(model.GetInitializedTensorData(fNX).get());
136 // // if q <=1 and r = 1 output length = 1 (it is a scalar)
137 // std::vector<int64_t> outputData(1); //ConvertShapeToLength(shapeY));
138 // outputData[0] = inputData[fIndices[0]];
139 // model.AddConstantTensor(fNY, shapeY, outputData.data());
140 // if (model.Verbose())
141 // std::cout << "GatherND: " << fNX << " " << ConvertShapeToString(shapeX) << " -> " << fNY << " with shape " << ConvertShapeToString(shapeY)
142 // << " and values " << ConvertValuesToString(outputData) << " (constant) " << std::endl;
143 // fIsOutputConstant = true;
144 // }
145 // }
146 // // case input is a shape tensor (r is == 1 by definition) and indices are known
147 // else if (model.IsShapeTensor(fNX) && q <=1 && fIndices.size() > 0) {
148 // auto inputData = model.GetShapeTensorValues(fNX);
149 // // if r == 1 and q<=1 then output length is 1 (is a scalar or tensor of size1)
150 // std::vector<Dim> outputData(1);
151 // outputData[0] = inputData[fIndices[0]];
152 // if (outputData[0].isParam) {
153 // fIsOutputConstant = true;
154 // // shapeY can be scalar or vector of size1
155 // model.AddShapeTensor(fNY, outputData, fShapeY.size() == 0);
156 // if (model.Verbose())
157 // std::cout << "GatherND: " << fNX << " " << ConvertShapeToString(fShapeX) << " -> " << fNY << " with shape " << ConvertShapeToString(fShapeY)
158 // << " and values " << ConvertShapeToString(outputData) << " (shape) " << std::endl;
159 // } else {
160 // int64_t value = static_cast<int64_t>(outputData[0].dim);
161 // auto shapeY = ConvertShapeToInt(fShapeY);
162 // model.AddConstantTensor(fNY, shapeY, &value);
163 // fIsOutputConstant = true;
164 // if (model.Verbose())
165 // std::cout << "GatherND: " << fNX << " " << ConvertShapeToString(fShapeX) << " -> " << fNY << " with shape " << ConvertShapeToString(fShapeY)
166 // << " and values {" << value << "} (constant) " << std::endl;
167 // }
168 // }
169
170 }
171
172 std::string Generate(std::string opName) override {
173 if (fIsOutputConstant) {
174 // no code to generate here for constant output. Tensor output is defined in Session constructor
175 return "//---------------------------------------\n";
176 }
177 opName = "op_" + opName;
178 std::stringstream out;
179 out << "//--------- GatherND " << opName << " --> " << ConvertShapeToString(fShapeY) << "\n";
180 // The shape of the output is q + r - 1
181 size_t r = fShapeX.size();
182 // Indices of shape q
183 size_t q = fShapeIndices.size();
184 // Strides
188
189 // case input_index_shape == rank of input
190 size_t ss = fShapeIndices.back().dim;
191
192 // check for negative indices
194 out << SP << "for (size_t i = 0; i < " << indicesLength << "; i++) {\n";
195 out << SP << SP << "if (tensor_" << fNIndices << "[i] < 0 ) {\n";
196 // corresponding input shape is i % strides[N-1]
197 out << SP << SP << SP << "size_t s_i = " << fShapeX[fBatchDims] << ";\n";
198 for (size_t j = 1; j < ss; j++) {
199 out << SP << SP << SP << "if (i % " << ss << " == " << j << ") s_i = " << fShapeX[fBatchDims+j] << ";\n";
200 }
201 out << SP << SP << SP << "const_cast<int64_t &>(tensor_" << fNIndices << "[i]) += s_i;\n";
202 out << SP << SP << "}\n";
203 out << SP << "}\n";
204 // loop on batch dims
205 std::string outIndex;
206 std::string inIndex;
207 std::string idIndex;
208 for (size_t j = 0; j < fBatchDims; j++) {
209 std::string index = "i_" + std::to_string(j);
210 for (size_t k = 0; k <= j; k++) out << SP;
211 out << "for (size_t " << index << " = 0; " << index << " < " << fShapeY[j] << "; " << index << "++) {\n";
212 if (j > 0) {
213 outIndex += " + ";
214 inIndex += " + ";
215 idIndex += " + ";
216 }
217 outIndex += index;
218 if (stridesY[j].GetVal() != "1")
219 outIndex += " * " + stridesY[j].GetVal();
220 inIndex += index;
221 if (stridesX[j].GetVal() != "1")
222 inIndex += " * " + stridesX[j].GetVal();
223 idIndex += index;
224 if (stridesIndices[j].GetVal() != "1")
225 idIndex += " * " + stridesIndices[j].GetVal();
226 }
227 // loop between b and q-1
228 for (size_t j = fBatchDims; j < q - 1; j++) {
229 std::string index = "i_" + std::to_string(j);
230 for (size_t k = 0; k <= j; k++) out << SP;
231 out << "for (size_t " << index << " = 0; " << index << " < " << fShapeY[j] << "; " << index << "++) {\n";
232 if (j > 0) {
233 outIndex += " + ";
234 idIndex += " + ";
235 }
236 outIndex += index;
237 if (stridesY[j].GetVal() != "1")
238 outIndex += " * " + stridesY[j].GetVal();
239 idIndex += index;
240 if (stridesIndices[j].GetVal() != "1")
241 idIndex += " * " + stridesIndices[j].GetVal();
242 }
243 for (size_t k = 0; k <= q - 1; k++) out << SP;
244 out << "size_t inputIndex = " << inIndex;
245 std::string indexIndex = idIndex;
246 for (size_t l = 0; l < ss; l++) {
247 if (l > 0)
248 indexIndex = idIndex + " + " + std::to_string(l);
249 // compute input index using index tensors
250 if (!indexIndex.empty() || l>0)
251 out << " + ";
252 out << "tensor_" << fNIndices << "[" << indexIndex << "]";
253 if (stridesX[fBatchDims + l].GetVal() != "1") out
254 << " * " << stridesX[fBatchDims + l];
255 }
256 out << ";\n";
257 for (size_t k = 0; k <= q - 1; k++) out << SP;
258 // case slice is a scalar
259 if (ss == r - fBatchDims) {
260 out << "tensor_" << fNY << "[" << outIndex << "] = "
261 << "tensor_" << fNX << "[inputIndex];\n";
262 } else {
263 // we make a copy of slice
264 out << "std::copy(tensor_" << fNX << " + inputIndex, tensor_" << fNX << " + inputIndex + "
265 << stridesX[fBatchDims + ss - 1] << ","
266 << "tensor_" << fNY << "+" << outIndex << ");\n";
267 }
268 // close the loops
269
270 // end loops j_k, j_{k + 1}, ..., j_{r - 2}
271 for (size_t j = q-1; j > 0; j--) {
272 for (size_t k = 0; k <j; k++) out << SP;
273 out << "}\n";
274 }
275
276 return out.str();
277 }
278
279};
280
281}//SOFIE
282}//Experimental
283}//TMVA
284
285
286#endif //TMVA_SOFIE_ROPERATOR_RELU
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 char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
float * q
const_iterator end() const
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:65
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:262
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:90
ROperator_GatherND(int batch_dims, std::string nameX, std::string nameIndices, std::string nameY)
std::string Generate(std::string opName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
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:48
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 ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations
TLine l
Definition textangle.C:4