1#ifndef TMVA_SOFIE_ROPERATOR_GatherND
2#define TMVA_SOFIE_ROPERATOR_GatherND
35 ROperator_GatherND(
int batch_dims, std::string nameX, std::string nameIndices, std::string nameY):
42 if (!model.CheckIfTensorAlreadyExist(
fNX)) {
43 throw std::runtime_error(
"TMVA SOFIE GatherND Op Input Tensor " +
fNX +
" is not found in model");
56 throw std::runtime_error(
"TMVA SOFIE GatherND : rank of Indices is < 1");
59 throw std::runtime_error(
"TMVA SOFIE GatherND : rank of input tensor is < 1");
62 throw std::runtime_error(
"TMVA SOFIE GatherND : invalid batch dim value");
69 throw std::runtime_error(
"TMVA SOFIE GatherND : invalid input or index shape for " + std::to_string(i));
76 throw std::runtime_error(
"TMVA SOFIE GatherND : Index_shape(-1) is not known");
81 if (last_index_shape < 1 || last_index_shape >
r -
fBatchDims) {
82 throw std::runtime_error(
"TMVA SOFIE GatherND : Index_shape(-1) has wrong value " +
83 std::to_string(last_index_shape));
86 size_t output_rank =
r +
q -1 - last_index_shape -
fBatchDims;
91 if (
fShapeY.size() != output_rank) {
95 <<
" and output rank should be " << output_rank << std::endl;
96 throw std::runtime_error(
"TMVA SOFIE GatherND : Something is wrong in initialization ");
101 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShapeY);
172 std::string
Generate(std::string opName)
override {
175 return "//---------------------------------------\n";
177 opName =
"op_" + opName;
178 std::stringstream out;
194 out <<
SP <<
"for (size_t i = 0; i < " << indicesLength <<
"; i++) {\n";
195 out <<
SP <<
SP <<
"if (tensor_" <<
fNIndices <<
"[i] < 0 ) {\n";
198 for (
size_t j = 1; j < ss; j++) {
201 out <<
SP <<
SP <<
SP <<
"const_cast<int64_t &>(tensor_" <<
fNIndices <<
"[i]) += s_i;\n";
202 out <<
SP <<
SP <<
"}\n";
205 std::string outIndex;
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";
218 if (stridesY[j].GetVal() !=
"1")
219 outIndex +=
" * " + stridesY[j].GetVal();
221 if (stridesX[j].GetVal() !=
"1")
222 inIndex +=
" * " + stridesX[j].GetVal();
224 if (stridesIndices[j].GetVal() !=
"1")
225 idIndex +=
" * " + stridesIndices[j].GetVal();
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";
237 if (stridesY[j].GetVal() !=
"1")
238 outIndex +=
" * " + stridesY[j].GetVal();
240 if (stridesIndices[j].GetVal() !=
"1")
241 idIndex +=
" * " + stridesIndices[j].GetVal();
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++) {
248 indexIndex = idIndex +
" + " + std::to_string(
l);
250 if (!indexIndex.empty() ||
l>0)
252 out <<
"tensor_" <<
fNIndices <<
"[" << indexIndex <<
"]";
257 for (
size_t k = 0; k <=
q - 1; k++) out <<
SP;
260 out <<
"tensor_" <<
fNY <<
"[" << outIndex <<
"] = "
261 <<
"tensor_" <<
fNX <<
"[inputIndex];\n";
264 out <<
"std::copy(tensor_" <<
fNX <<
" + inputIndex, tensor_" <<
fNX <<
" + inputIndex + "
266 <<
"tensor_" <<
fNY <<
"+" << outIndex <<
");\n";
271 for (
size_t j =
q-1; j > 0; j--) {
272 for (
size_t k = 0; k <j; k++) out <<
SP;
ROperator_GatherND(int batch_dims, std::string nameX, std::string nameIndices, std::string nameY)
std::vector< int64_t > fIndices
void Initialize(RModel &model) override
std::vector< Dim > fShapeIndices
std::vector< Dim > fShapeY
std::string Generate(std::string opName) override
std::vector< Dim > fShapeX
std::vector< std::string_view > fInputTensorNames
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
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 ConvertDimShapeToString(const std::vector< Dim > &shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations