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++)
116 OpName =
"op_" + OpName;
118 throw std::runtime_error(
"TMVA SOFIE Reduce Op called to Generate without being initialized first");
136 std::stringstream out;
137 out <<
"\n//---- operator " <<
Name() <<
" " << OpName <<
"\n";
139 bool reduceLastDims =
true;
141 for (
int k =
fShapeX.size()-1; k >= kmin; k--) {
144 reduceLastDims =
false;
148 size_t reducedLength = inputLength / outputLength;
149 if (reduceLastDims) {
151 out <<
SP <<
"for (size_t i = 0; i < " << outputLength <<
"; i++) {\n";
153 out << SP << SP << ConvertTypeToString(GetTemplatedType<T>(T())) <<
" reducedValue = " << startingValue <<
";\n";
155 out <<
SP <<
SP <<
"for (size_t j = 0; j < " << reducedLength <<
"; j++) {\n";
157 out <<
SP <<
SP <<
SP <<
"reducedValue *= tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
159 out <<
SP <<
SP <<
SP <<
"reducedValue *= tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
161 out <<
SP <<
SP <<
SP <<
"reducedValue *= tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j] * tensor_"
162 <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
163 out <<
SP <<
SP <<
"}\n";
165 out <<
SP <<
SP <<
"reducedValue /= static_cast<float>(" << reducedLength <<
");\n";
166 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] = reducedValue;\n";
173 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",1);\n";
175 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",0);\n";
177 out <<
SP <<
"for (size_t i = 0; i < " << inputLength <<
"; i++) {\n";
182 out <<
SP <<
SP <<
"size_t outputIndex = 0;\n";
183 for (
size_t k = 0; k < dim; k++) {
186 out <<
SP <<
SP <<
"size_t i_" << k <<
" = i / " << inputStrides[k] <<
" % " <<
fShapeX[k] <<
";\n";
187 out <<
SP <<
SP <<
"outputIndex += i_" << k <<
" * " << outputStrides[k] <<
";\n";
191 out <<
SP <<
SP <<
"// compute reduction....\n";
193 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] *= tensor_" <<
fNX <<
"[i];\n";
195 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i];\n";
197 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i] * tensor_" <<
fNX
203 out <<
SP <<
"for (size_t i = 0; i < " << outputLength <<
"; i++) {\n";
204 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::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations