1#ifndef TMVA_SOFIE_ROPERATOR_GATHER
2#define TMVA_SOFIE_ROPERATOR_GATHER
38 ROperator_Gather(int64_t attrAxis, std::string nameX, std::string nameIndices, std::string nameY):
55 throw std::runtime_error(
"TMVA SOFIE Gather Op Input Tensor " +
fNX +
" is not found in model");
75 bool hasNegativeIndex =
false;
81 for (
size_t i = 0; i < indicesLength; i++) {
85 hasNegativeIndex =
true;
91 if (hasNegativeIndex) {
92 std::string nameIndicesUpdated =
fNIndices +
"_updated";
108 for (
size_t i = 0; i <
q; i++) {
121 std::vector<int64_t> outputData(1);
122 outputData[0] = inputData[
fIndices[0]];
150 <<
" and values {" <<
value <<
"} (constant) " << std::endl;
163 std::string
Generate(std::string opName)
override {
164 opName =
"op_" + opName;
165 std::stringstream out;
169 out <<
"//--------------------(constant)----------\n";
174 out <<
"//--------------------(shape)----------\n";
192 out <<
SP <<
"// correct in case of negative gather indices\n";
193 out <<
SP <<
"for (size_t i = 0; i < " << indicesLength <<
"; i++){\n";
194 out <<
SP <<
SP <<
"if (tensor_" <<
fNIndices <<
"[i] < 0)\n";
204 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
205 std::string
index =
"j_" + std::to_string(j);
206 for (
size_t k = 0; k <= j; k++) out <<
SP;
210 for (
size_t i = 0; i <
q; i++) {
211 std::string
index =
"i_" + std::to_string(i);
212 for (
size_t k = 0; k <= i +
fAttrAxis; k++) out <<
SP;
217 std::string
index =
"j_" + std::to_string(
q+j);
218 for (
size_t k = 0; k <=
q + j; k++) out <<
SP;
224 out <<
SP <<
"{ // scalar case \n";
227 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
228 out <<
"size_t y_index = ";
229 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
230 if (j > 0) out <<
" + ";
232 if (stridesY[j].dim != 1) out <<
" * " << stridesY[j];
234 for (
size_t i = 0; i <
q; i++) {
240 if (j +
q > 0) out <<
" + ";
242 if (stridesY[
q+j].dim != 1) out <<
" * " << stridesY[
q+j];
250 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
251 out <<
"size_t i_index = ";
252 for (
size_t i = 0; i <
q; i++) {
253 if (i > 0) out <<
" + ";
255 if (stridesIndices[i].dim != 1) out <<
" * " << stridesIndices[i];
263 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
264 out <<
"size_t k = static_cast<size_t>(" <<
"tensor_" <<
fNIndices <<
"[i_index]" <<
");\n";
266 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
267 out <<
"size_t x_index = k";
269 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
272 if (stridesX[j].dim != 1) out <<
" * " << stridesX[j];
279 if (stridesX[j+1].dim != 1) out <<
" * " << stridesX[j+1];
282 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
283 out <<
"tensor_" <<
fNY <<
"[y_index] = tensor_" <<
fNX <<
"[x_index];\n";
286 for (
size_t j =
q+
r-1; j > 0; j--) {
287 for (
size_t k = 0; k <j; k++) out <<
SP;
292 out <<
SP <<
"} // close Gather scope for scalar case \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 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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
std::vector< size_t > GetTensorShape(const std::string &name) const
std::vector< Dim > GetDimTensorShape(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
bool IsInitializedTensor(const std::string &name) const
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
ETensorType GetTensorType(std::string name) const
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
void AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
void Initialize(RModel &model) override
std::vector< int64_t > fIndices
std::string Generate(std::string opName) override
ROperator_Gather(int64_t attrAxis, std::string nameX, std::string nameIndices, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< Dim > fShapeY
std::vector< Dim > fShapeX
std::vector< Dim > fShapeIndices
std::vector< Dim > fOutputShapeData
std::vector< std::string_view > fInputTensorNames
bool fIsOutputParamShape
flag to identify of the output represents a parametric shape (can be known at compile time)
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::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
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)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations