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