Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Slice.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_SLICE
2#define TMVA_SOFIE_ROPERATOR_SLICE
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <cassert>
9#include <sstream>
10#include <numeric>
11
12namespace TMVA{
13namespace Experimental{
14namespace SOFIE{
15
16// slice operator
17
18template <typename T, typename IType>
19class ROperator_Slice final : public ROperator
20{
21
22private:
23
24 std::string fNData; // input data tensor name
25 std::string fNOutput; // output data name
26 std::vector<std::string> fNames; // tensor names for meta(axis) information
27 std::vector<size_t> fShapeInput; // input shape data
28 std::vector<size_t> fShapeOutput; // output shape data
29 // saved Start/End.Steps are corrected from initial ONNX for negative/default values
30 // and are available for each axis
31 std::vector<size_t> fStart; // starting values of slices
32 std::vector<size_t> fEnd; // End values of slices
33 std::vector<size_t> fSteps; // step values of slices
34
35 std::vector<std::vector<IType>> fAttributes; // attributes for the version <=10 case
36
37
38public:
39
41
42 // ctor for versions >= 10
43 ROperator_Slice(std::string nameData, std::vector<std::string> names, std::string nameOutput)
44 : fNData(UTILITY::Clean_name(nameData)),
45 fNOutput(UTILITY::Clean_name(nameOutput))
46 {
47 fNames.resize(4);
48 for (size_t i = 0; i < names.size(); ++i) {
49 fNames[i] = UTILITY::Clean_name(names[i]);
50 }
51
52 if (names.size() == 3) {
53 if (names[2] != "axes") { //steps provided instead of axis
54 fNames[3] = fNames[2];
55 fNames[2] = "";
56 }
57 else { // steps not provided
58 fNames[3] = "";
59 }
60 }
61 }
62 // ctor for versions < 10
63 ROperator_Slice(std::string nameData, std::vector<IType> starts, std::vector<IType> ends, std::vector<IType> axes, std::string nameOutput)
64 : fNData(UTILITY::Clean_name(nameData)),
65 fNOutput(UTILITY::Clean_name(nameOutput))
66 {
67 fAttributes.push_back(starts);
68 fAttributes.push_back(ends);
69 fAttributes.push_back(axes);
70 }
71
72 // output type is same as input
73 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
74 auto ret = std::vector<ETensorType>(1, input[0]);
75 return ret;
76 }
77
78 // output shape
79 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
80 auto & input_shape = input[0];
81 // assume dimension of output shape is SAME AS INPUT !
82 std::vector<std::vector<size_t>> ret(1, input_shape);
83 auto & output_shape = ret[0];
84 for (size_t i = 0; i < input_shape.size(); i++) {
85 output_shape[i] = (fEnd[i]-fStart[i])/ fSteps[i];
86 }
87 return ret;
88 }
89
90
91 void Initialize(RModel& model){
92 if (model.CheckIfTensorAlreadyExist(fNData) == false){ //input must be a graph input, or already initialized intermediate tensor
93 throw std::runtime_error("TMVA Slice Op Input Tensor is not found in model");
94 }
95
96 std::vector<std::vector<size_t>> shapes;
98 shapes.push_back(fShapeInput);
99
100 std::vector<std::vector<IType>> itensors(4);
101 if (fNames.size() > 0) {
102 // loop on the extra 2 or 3 or 4 inputs
103 for (size_t i = 0; i < fNames.size(); ++i) {
104 if (!fNames[i].empty()) {
105 // std::cout << " i " << i << " getting data for tensor " << fNames[i] << std::endl;
106 auto dptr = model.GetInitializedTensorData(fNames[i]);
107 auto tensor = static_cast<IType *>(dptr.get());
108 auto vec = model.GetTensorShape(fNames[i]);
109 assert(vec.size() == 1);
110 itensors[i] = std::vector<IType>(tensor, tensor + vec[0]);
111 }
112 else {
113 switch (i)
114 {
115 case 2: // missing axes
116 itensors[2] = std::vector<IType>(fShapeInput.size());
117 std::iota(itensors[2].begin(), itensors[2].end(), 0);
118 break;
119 case 3: // missing steps
120 itensors[3] = std::vector<IType>(itensors[0].size(), 1);
121 default:
122 break;
123 }
124 }
125 }
126 } else {
127 assert (fAttributes.size() > 1);
128 for (size_t i = 0; i < fAttributes.size(); i++) {
129 itensors[i] = fAttributes[i];
130 }
131 }
132 size_t dim = fShapeInput.size();
133
134 fSteps = std::vector<size_t>(dim, 1);
135 fStart = std::vector<size_t>(dim, 0);
137
138 auto istart = itensors[0];
139 auto iend = itensors[1];
140 auto iaxes = itensors[2];
141 auto isteps = itensors[3];
142
143 // make tensor axis
144 // if iaxes.size is =0 tensor axis is missing and use defaults
145 if (iaxes.size() > 0) {
146 for (size_t i = 0; i < iaxes.size(); i++) {
147 // negative axes - they count from the back
148 if (iaxes[i] < 0) iaxes[i] = dim + iaxes[i];
149 size_t jaxis = static_cast<size_t>(iaxes[i]);
150 assert(jaxis < dim);
151 size_t imax = fShapeInput[jaxis];
152 // find start/end/step for given axis
153 IType start = (istart[i] >= 0) ? istart[i] : imax + istart[i];
154 if (start < 0) start = 0;
155 if (start > static_cast<IType>(imax))
156 start = imax;
157 fStart[jaxis] = start;
158 IType ie = (iend[i] >= 0) ? iend[i] : imax + iend[i];
159 if (ie < 0) ie = 0;
160 if (ie > static_cast<IType>(imax))
161 ie = imax;
162 fEnd[jaxis] = ie;
163
164 if (isteps.size() > 0) {
165 if (isteps[i] < 0) {
166 // to be done
167 throw std::runtime_error("TMVA Slice Op : negative steps not supported");
168 }
169 fSteps[jaxis] = isteps[i];
170 assert(fSteps[jaxis] > 0 && fSteps[jaxis] < fShapeInput[jaxis]);
171 }
172 }
173 }
174
177 }
178
179 std::string Generate(std::string OpName){
180 OpName = "op_" + OpName;
181 if (fShapeInput.empty() || fShapeOutput.empty()){
182 throw std::runtime_error("TMVA SOFIE Slice Op called to Generate without being initialized first");
183 }
184
185 std::stringstream out;
186 //std::string opName = "Slice";
187
188 out << SP << "///------- Slice operator\n" << std::endl;
189 // loop on the dimensions depending no the orders
190 size_t ndim = fShapeInput.size();
191 std::vector<size_t> strides(ndim,1);
192 for (int i = int(ndim-2); i >=0 ; i--) {
193 strides[i] = strides[i+1]*fShapeInput[i+1];
194 }
195
196 out << SP << "size_t iOut = 0;\n";
197 std::string MSP = SP;
198 for (size_t idim = 0; idim < ndim; idim++) {
199 out << MSP << "for (size_t i" << idim << " = " << fStart[idim] << "; i" << idim << " < " << fEnd[idim]
200 << "; i" << idim << "+= " << fSteps[idim] << ") {\n";
201 MSP += SP;
202 if (idim < ndim-1) out << MSP << "size_t stride" << idim << " = " << strides[idim] << "*i" << idim << ";\n";
203 }
204 out << MSP << "size_t iInput = ";
205 for (size_t idim = 0; idim < ndim-1; idim++) out << " stride" << idim << " + ";
206 // here should be step size ?
207 out << "i" << ndim-1 << ";\n";
208 out << MSP << "tensor_" << fNOutput << "[iOut++] = tensor_" <<fNData << "[iInput];\n";
209 for (size_t idim = 0; idim < ndim; idim++) {
210 MSP = MSP.replace(0,SP.length(),"");
211 out << MSP << "}\n";
212 }
213
214 return out.str();
215 }
216
217};
218
219}//SOFIE
220}//Experimental
221}//TMVA
222
223
224#endif //TMVA_SOFIE_ROPERATOR_SLICE
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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:76
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition RModel.cxx:156
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:97
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:55
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:188
std::vector< std::vector< IType > > fAttributes
ROperator_Slice(std::string nameData, std::vector< IType > starts, std::vector< IType > ends, std::vector< IType > axes, std::string nameOutput)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::string Generate(std::string OpName)
ROperator_Slice(std::string nameData, std::vector< std::string > names, std::string nameOutput)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
std::string Clean_name(std::string input_tensor_name)
create variable transformations