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 enum EReduceDim {kFirst, kLast, kMiddle};
140 EReduceDim reduceDims = kLast;
142 for (
int k =
fShapeX.size()-1; k >= kmin; k--) {
145 reduceDims = kMiddle;
149 if (reduceDims == kMiddle) {
152 for (
size_t k = 0; k <
fAttrAxes.size(); k++) {
155 reduceDims = kMiddle;
160 size_t reducedLength = inputLength / outputLength;
161 if (reduceDims == kLast) {
168 out <<
SP <<
"for (size_t i = 0; i < " << outputLength <<
"; i++) {\n";
171 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] = " << startingValue <<
";\n";
172 out <<
SP <<
SP <<
"for (size_t j = 0; j < " << reducedLength <<
"; j++) {\n";
175 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] *= tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
177 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] += tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
179 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] += tensor_" <<
fNX <<
"[i * " << reducedLength <<
" + j] * tensor_"
180 <<
fNX <<
"[i * " << reducedLength <<
" + j];\n";
181 out <<
SP <<
SP <<
"}\n";
183 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] /= static_cast<float>(" << reducedLength <<
");\n";
186 }
else if (reduceDims == kFirst) {
191 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",1);\n";
193 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",0);\n";
195 out <<
SP <<
"for (size_t i = 0; i < " << reducedLength <<
"; i++) {\n";
196 out <<
SP <<
SP <<
"for (size_t j = 0; j < " << outputLength <<
"; j++) {\n";
199 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[j] *= tensor_" <<
fNX <<
"[i * " << outputLength <<
" + j];\n";
201 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[j] += tensor_" <<
fNX <<
"[i * " << outputLength <<
" + j];\n";
203 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[j] += tensor_" <<
fNX <<
"[i * " << outputLength <<
" + j] * tensor_"
204 <<
fNX <<
"[i * " << outputLength <<
" + j];\n";
205 out <<
SP <<
SP <<
"}\n";
208 out <<
SP <<
"for (size_t j = 0; i < " << outputLength <<
"; j++) {\n";
209 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[j] /= static_cast<float>(" << reducedLength <<
");\n";
218 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",1);\n";
220 out <<
SP <<
"fTensor_" <<
fNY <<
".assign(" << outputLength <<
",0);\n";
222 out <<
SP <<
"for (size_t i = 0; i < " << inputLength <<
"; i++) {\n";
227 out <<
SP <<
SP <<
"size_t outputIndex = 0;\n";
228 for (
size_t k = 0; k < dim; k++) {
231 out <<
SP <<
SP <<
"size_t i_" << k <<
" = i / " << inputStrides[k] <<
" % " <<
fShapeX[k] <<
";\n";
232 out <<
SP <<
SP <<
"outputIndex += i_" << k <<
" * " << outputStrides[k] <<
";\n";
236 out <<
SP <<
SP <<
"// compute reduction....\n";
238 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] *= tensor_" <<
fNX <<
"[i];\n";
240 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i];\n";
242 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outputIndex] += tensor_" <<
fNX <<
"[i] * tensor_" <<
fNX
248 out <<
SP <<
"for (size_t i = 0; i < " << outputLength <<
"; i++) {\n";
249 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::string Generate(std::string opName)
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::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