1#ifndef TMVA_SOFIE_ROPERATOR_GATHER
2#define TMVA_SOFIE_ROPERATOR_GATHER
13namespace Experimental{
38 ROperator_Gather(int64_t attrAxis, std::string nameX, std::string nameIndices, std::string nameY):
39 fAttrAxis(attrAxis),
fNX(UTILITY::Clean_name(nameX)),
fNIndices(UTILITY::Clean_name(nameIndices)),
fNY(UTILITY::Clean_name(nameY)) {
45 if (!model.CheckIfTensorAlreadyExist(
fNX)) {
46 throw std::runtime_error(
"TMVA SOFIE Gather Op Input Tensor " +
fNX +
" is not found in model");
64 if (model.IsInitializedTensor(
fNIndices)) {
66 bool hasNegativeIndex =
false;
68 int64_t*
data =
static_cast<int64_t*
>(model.GetInitializedTensorData(
fNIndices).get());
72 for (
size_t i = 0; i < indicesLength; i++) {
76 hasNegativeIndex =
true;
82 if (hasNegativeIndex) {
83 std::string nameIndicesUpdated =
fNIndices +
"_updated";
84 model.AddConstantTensor(nameIndicesUpdated, model.GetTensorShape(
fNIndices),
fIndices.data());
99 for (
size_t i = 0; i <
q; i++) {
106 if (model.IsInitializedTensor(
fNX) &&
q <= 1 &&
r == 1 &&
fIndices.size() > 0) {
110 auto inputData =
static_cast<int64_t*
>(model.GetInitializedTensorData(
fNX).get());
112 std::vector<int64_t> outputData(1);
113 outputData[0] = inputData[
fIndices[0]];
114 model.AddConstantTensor(
fNY, shapeY, outputData.data());
122 else if (model.IsShapeTensor(
fNX) &&
q <=1 &&
fIndices.size() > 0) {
123 auto inputData = model.GetShapeTensorValues(
fNX);
137 model.AddConstantTensor(
fNY, shapeY, &
value);
141 <<
" and values {" <<
value <<
"} (constant) " << std::endl;
146 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShapeY);
154 std::string
Generate(std::string opName)
override {
155 opName =
"op_" + opName;
156 std::stringstream out;
160 out <<
"//--------------------(constant)----------\n";
165 out <<
"//--------------------(shape)----------\n";
185 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
186 std::string
index =
"j_" + std::to_string(j);
187 for (
size_t k = 0; k <= j; k++) out <<
SP;
191 for (
size_t i = 0; i <
q; i++) {
192 std::string
index =
"i_" + std::to_string(i);
193 for (
size_t k = 0; k <= i +
fAttrAxis; k++) out <<
SP;
198 std::string
index =
"j_" + std::to_string(
q+j);
199 for (
size_t k = 0; k <=
q + j; k++) out <<
SP;
205 out <<
SP <<
"{ // scalar case \n";
208 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
209 out <<
"size_t y_index = ";
210 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
211 if (j > 0) out <<
" + ";
213 if (stridesY[j].dim != 1) out <<
" * " << stridesY[j];
215 for (
size_t i = 0; i <
q; i++) {
221 if (j +
q > 0) out <<
" + ";
223 if (stridesY[
q+j].dim != 1) out <<
" * " << stridesY[
q+j];
231 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
232 out <<
"size_t i_index = ";
233 for (
size_t i = 0; i <
q; i++) {
234 if (i > 0) out <<
" + ";
236 if (stridesIndices[i].dim != 1) out <<
" * " << stridesIndices[i];
246 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
248 out <<
"int64_t k_i = static_cast<int64_t>(tensor_" <<
fNIndices <<
"[i_index]);\n";
249 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
250 out <<
"size_t k = static_cast<size_t>(k_i < 0 ? k_i + " <<
fShapeX[
fAttrAxis] <<
" : k_i);\n";
252 out <<
"size_t k = static_cast<size_t>(" <<
"tensor_" <<
fNIndices <<
"[i_index]" <<
");\n";
255 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
256 out <<
"size_t x_index = k";
258 for (
size_t j = 0; j < size_t(
fAttrAxis); j++) {
261 if (stridesX[j].dim != 1) out <<
" * " << stridesX[j];
268 if (stridesX[j+1].dim != 1) out <<
" * " << stridesX[j+1];
271 for (
size_t k = 0; k <
q +
r; k++) out <<
SP;
272 out <<
"tensor_" <<
fNY <<
"[y_index] = tensor_" <<
fNX <<
"[x_index];\n";
275 for (
size_t j =
q+
r-1; j > 0; j--) {
276 for (
size_t k = 0; k <j; k++) out <<
SP;
281 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 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
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< 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 ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations