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