Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Reduce.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Reduce
2#define TMVA_SOFIE_ROPERATOR_Reduce
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <memory>
9#include <sstream>
10#include <algorithm>
11#include <stdexcept>
12#include <vector>
13#include <cassert>
14
15namespace TMVA{
16namespace Experimental{
17namespace SOFIE{
18
20
21template <typename T, EReduceOpMode Op>
22class ROperator_Reduce final : public ROperator
23{
24private:
25 /* Attributes*/
26 int fkeepdims = 1; //default value
27 std::vector<int64_t> fAttrAxes;
29 std::string fNX;
30 std::string fNAxes;
31 std::string fNY;
32 std::vector<size_t> fShapeX;
33 std::vector<size_t> fShapeY;
34 std::vector<size_t> fShapeYNotPruned; // needed for fKeepdims=0
35
36
37public:
38
39 std::string Name() {
40 if (fReduceOpMode == ReduceMean) return "ReduceMean";
41 else if (fReduceOpMode == ReduceSumsquare ) return "ReduceSumsquare";
42 else if (fReduceOpMode == ReduceProd ) return "ReduceProd";
43 else if (fReduceOpMode == ReduceSum) return "ReduceSum";
44 return "Invalid";
45 }
46
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)) {
50 fReduceOpMode = Op;
51 }
52
53 // type of output given input
54 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
55 return input;
56 }
57
58 // shape of output tensors given input tensors
59 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
60 auto ret = input; //suggest copy to compiler
61 auto & outputShape = ret[0];
62 for (size_t j = 0; j < fAttrAxes.size(); j++) {
63 if (fAttrAxes[j] < 0) fAttrAxes[j] += outputShape.size();
64 if (fAttrAxes[j] < 0 || (size_t) fAttrAxes[j] >= outputShape.size() )
65 throw std::runtime_error("TMVA SOFIE Reduce Op - invalid axes values " + std::to_string(fAttrAxes[j]));
66 // set to 1 the reduced dims
67 outputShape[fAttrAxes[j]] = 1;
68 }
69 fShapeYNotPruned = outputShape;
70 // in case of pruning dimension we need to sort axes attributes
71 if (fkeepdims == 0) {
72 auto ax = fAttrAxes;
73 std::sort(ax.begin(), ax.end());
74 for (size_t j = 0; j < ax.size(); j++) {
75 // erase reduced dimensions, but keep last one
76 if (outputShape.size() > 1) {
77 outputShape.erase(outputShape.begin() + ax[j]);
78 for (size_t k = j+1; k < ax.size(); k++)
79 ax[k] -= 1; // decrease by one since we have removed a value
80 }
81 }
82 }
83 return ret;
84 }
85 void Initialize(RModel &model) {
86
87 fUseSession = model.UseSession();
88
89 if (!model.CheckIfTensorAlreadyExist(fNX)) {
90 // input must be a graph input, or already initialized intermediate tensor
91 throw std::runtime_error("TMVA SOFIE Reduce Op Input Tensor " + fNX + " is not found in model");
92 }
93 fShapeX = model.GetTensorShape(fNX);
94 // check if tensor with axes is provided
95 if (!fNAxes.empty()) {
96 auto ax_shptr = model.GetInitializedTensorData(fNAxes);
97 auto ax_ptr = static_cast<int64_t *>(ax_shptr.get());
98 auto ax_shape = model.GetTensorShape(fNAxes);
99 size_t ax_length = ConvertShapeToLength(ax_shape);
100 fAttrAxes = std::vector<int64_t>(ax_ptr, ax_ptr+ax_length);
101 } else if (fAttrAxes.empty()) {
102 // in case no axes is passed assume full reduction
103 fAttrAxes.resize(fShapeX.size());
104 for (size_t i = 0; i < fAttrAxes.size(); i++)
105 fAttrAxes[i] = i;
106 }
107 // find shape of Y and add it in the list of intermediate tensors
110 }
111
112 std::string Generate(std::string OpName){
113 OpName = "op_" + OpName;
114 if (fShapeX.empty() || fShapeY.empty()) {
115 throw std::runtime_error("TMVA SOFIE Reduce Op called to Generate without being initialized first");
116 }
117
120
122 // output stride (or not pruned vector)
124
125 // write here according to size of shape
126 // in generation code can be done automatically
127 // i0 = i / stride0 % shape0; i1 = i / stride1 % shape1 and so on
128 // and we have for the inverse
129 // i = i0 * s0 + i1 * s1 + i2 * s2 + i3 * s3 ....
130
131 // don't need to divide by last stride s[n-1] since it is 1 by definition
132
133 std::stringstream out;
134 out << "\n//---- operator " << Name() << " " << OpName << "\n";
135 // reset output tensors
137 out << SP << "fTensor_" << fNY << ".assign(" << outputLength << ",1);\n";
138 else
139 out << SP << "fTensor_" << fNY << ".assign(" << outputLength << ",0);\n";
140
141 out << SP << "for (size_t i = 0; i < " << inputLength << "; i++) {\n";
142
143 size_t dim = fShapeX.size(); // this is the input dimension (e.g. 2, 3 or 4 or more)
144
145 // here we find output index
146 out << SP << SP << "size_t outputIndex = 0;\n";
147 for (size_t k = 0; k < dim; k++) {
148 if (std::find(fAttrAxes.begin(), fAttrAxes.end(), k) == fAttrAxes.end()) {
149 // do for not reducing axes
150 out << SP << SP << "size_t i_" << k << " = i / " << inputStrides[k] << " % " << fShapeX[k] << ";\n";
151 out << SP << SP << "outputIndex += i_" << k << " * " << outputStrides[k] << ";\n";
152 }
153 }
154 // now compute reduction
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";
160 else if(fReduceOpMode == ReduceSumsquare){
161 out << SP << SP << "tensor_" << fNY << "[outputIndex] += tensor_" << fNX << "[i] * tensor_" << fNX << "[i];\n";
162 }
163 out << SP << "}\n"; // end loop on input elements
164 //normalize for reduced mean
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";
169 out << SP << "}\n";
170 }
171
172 return out.str();
173 }
174
175};
176
177}//SOFIE
178}//Experimental
179}//TMVA
180
181
182#endif //TMVA_SOFIE_ROPERATOR_Reduce
183
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:91
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:196
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:116
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:56
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:257
ROperator_Reduce(int keepdims, std::vector< int64_t > attrAxes, std::string nameX, std::string nameAxes, std::string nameY)
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
Definition ROperator.hxx:41
bool fUseSession
flag to identify if using the session class
Definition ROperator.hxx:42
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