1#ifndef TMVA_SOFIE_ROPERATOR_Reduce
2#define TMVA_SOFIE_ROPERATOR_Reduce
16namespace Experimental{
21template <
typename T, EReduceOpMode Op>
48 ROperator_Reduce(
int keepdims, std::vector<int64_t> attrAxes, std::string nameX, std::string nameAxes, std::string nameY):
49 fkeepdims(keepdims),
fAttrAxes(attrAxes),
fNX(UTILITY::Clean_name(nameX)),
fNAxes(UTILITY::Clean_name(nameAxes)),
fNY(UTILITY::Clean_name(nameY)) {
61 auto & outputShape = ret[0];
62 for (
size_t j = 0; j <
fAttrAxes.size(); j++) {
65 throw std::runtime_error(
"TMVA SOFIE Reduce Op - invalid axes values " + std::to_string(
fAttrAxes[j]));
73 std::sort(ax.begin(), ax.end());
74 for (
size_t j = 0; j < ax.size(); j++) {
76 if (outputShape.size() > 1) {
77 outputShape.erase(outputShape.begin() + ax[j]);
78 for (
size_t k = j+1; k < ax.size(); k++)
91 throw std::runtime_error(
"TMVA SOFIE Reduce Op Input Tensor " +
fNX +
" is not found in model");
97 auto ax_ptr =
static_cast<int64_t *
>(ax_shptr.get());
100 fAttrAxes = std::vector<int64_t>(ax_ptr, ax_ptr+ax_length);
104 for (
size_t i = 0; i <
fAttrAxes.size(); i++)
113 OpName =
"op_" + OpName;
115 throw std::runtime_error(
"TMVA SOFIE Reduce Op called to Generate without being initialized first");
133 std::stringstream out;
134 out <<
"\n//---- operator " <<
Name() <<
" " << OpName <<
"\n";
137 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",1);\n";
139 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",0);\n";
141 out <<
SP <<
"for (size_t i = 0; i < " << inputLength <<
"; i++) {\n";
146 out <<
SP <<
SP <<
"size_t outputIndex = 0;\n";
147 for (
size_t k = 0; k < dim; k++) {
150 out <<
SP <<
SP <<
"size_t i_" << k <<
" = i / " << inputStrides[k] <<
" % " <<
fShapeX[k] <<
";\n";
151 out <<
SP <<
SP <<
"outputIndex += i_" << k <<
" * " << outputStrides[k] <<
";\n";
155 out <<
SP <<
SP <<
"// compute reduction....\n";
157 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] *= tensor_" <<
fNX <<
"[i];\n";
159 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i];\n";
161 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i] * tensor_" <<
fNX <<
"[i];\n";
166 size_t reducedLength = inputLength/outputLength;
167 out <<
SP <<
"for (size_t i = 0; i < " << outputLength <<
"; i++) {\n";
168 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] /= static_cast<float>(" << reducedLength <<
");\n";
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
const ETensorType & GetTensorType(std::string name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
std::vector< size_t > fShapeX
void Initialize(RModel &model)
ROperator_Reduce(int keepdims, std::vector< int64_t > attrAxes, std::string nameX, std::string nameAxes, std::string nameY)
EReduceOpMode fReduceOpMode
std::string Generate(std::string OpName)
std::vector< int64_t > fAttrAxes
std::vector< size_t > fShapeY
std::vector< size_t > fShapeYNotPruned
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
const std::string SP
space used to correctly indent the generated C++ code
bool fUseSession
flag to identify if using the session class
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::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations