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 IType>
19class ROperator_Slice final : public ROperator
20{
21
22private:
23
24 // flags to indicate if start/end and steps are not defined at compiled time
25 bool fIsStartUndef = false;
26 bool fIsEndUndef = false;
27 bool fIsStepUndef = false;
28 bool fIdentitySlice = false;
29 bool fIsAlias = false; // identity slice whose output shares the memory of the input
30 std::string fNData; // input data tensor name
31 std::string fNOutput; // output data name
32 std::vector<std::string> fNames; // tensor names for meta(axis) information
33 std::vector<Dim> fShapeInput; // input shape
34 std::vector<Dim> fShapeOutput; // output shape
35 std::vector<Dim> fOutputShapeData; // output shape data in case output is a shape param tensor
36
37 // saved Start/End.Steps are corrected from initial ONNX for negative/default values
38 // and are available for each axis
39 std::vector<Dim> fStart; // starting values of slices for all axes
40 std::vector<Dim> fEnd; // End values of slices for all axes
41 std::vector<Dim> fSteps; // step values of slices for all axes
42 std::vector<Dim> fStartDims; // input starting values of slices
43 std::vector<Dim> fEndDims; // input End values of slices
44 std::vector<Dim> fStepDims; // input step values of slices
45 std::vector<IType> fAxes; // axes for input start/emd/step values
46
47 std::vector<std::vector<IType>> fAttributes; // attributes for the version <=10 case
48
49
50public:
51
53
54 // ctor for versions >= 10
55 ROperator_Slice(std::string nameData, std::vector<std::string> names, std::string nameOutput)
56 : fNData(UTILITY::Clean_name(nameData)),
57 fNOutput(UTILITY::Clean_name(nameOutput))
58 {
59 fNames.resize(4);
60 // axes and steps can be optional
61 for (size_t i = 0; i < names.size(); ++i) {
62 fNames[i] = UTILITY::Clean_name(names[i]);
63 }
64
67 }
68 // ctor for versions < 10
69 ROperator_Slice(std::string nameData, std::vector<IType> starts, std::vector<IType> ends, std::vector<IType> axes, std::string nameOutput)
70 : fNData(UTILITY::Clean_name(nameData)),
71 fNOutput(UTILITY::Clean_name(nameOutput))
72 {
73 fAttributes.push_back(starts);
74 fAttributes.push_back(ends);
75 fAttributes.push_back(axes);
76 }
77
78
79
80 void Initialize(RModel& model) override {
81 if (model.CheckIfTensorAlreadyExist(fNData) == false){ //input must be a graph input, or already initialized intermediate tensor
82 throw std::runtime_error("TMVA Slice Op Input Tensor is not found in model");
83 }
84
85 std::vector<std::vector<Dim>> shapes;
86 fShapeInput = model.GetDimTensorShape(fNData);
87 shapes.push_back(fShapeInput);
88
89 std::vector<std::vector<IType>> itensors(4);
90
91 if (fNames.size() > 0) { // size has to be equal to 4
92 // loop on the extra 2 or 3 or 4 inputs
93 for (size_t i = 0; i < 4; ++i) {
94 if (!fNames[i].empty()) {
95 if (model.IsInitializedTensor(fNames[i])) {
96 auto dptr = model.GetInitializedTensorData(fNames[i]);
97 auto tensor = static_cast<IType *>(dptr.get());
98 auto vec = model.GetTensorShape(fNames[i]);
99 assert(vec.size() == 1);
100 itensors[i] = std::vector<IType>(tensor, tensor + vec[0]);
101
102 } else if (model.IsShapeTensor(fNames[i])) {
103 // case is a shape tensor
104 if (i == 0) {
105 fStartDims = model.GetShapeTensorValues(fNames[i]);
106 } else if (i == 1) {
107 fEndDims = model.GetShapeTensorValues(fNames[i]);
108 } else if (i == 3) {
109 fStepDims = model.GetShapeTensorValues(fNames[i]);
110 }
111 } else {
112 // case is an intermediate tensor
113 auto shape = model.GetTensorShape(fNames[i]);
114 size_t s = shape[0];
115 for (size_t k = 0; k < s; k++) {
116 if (i == 0) {
117 fStartDims.push_back( Dim{std::string("start_") + fNOutput + "_" + std::to_string(k)});
118 fIsStartUndef = true;
119 } else if (i == 1) {
120 fEndDims.push_back(Dim{std::string("end_") + fNOutput + "_" + std::to_string(k)});
121 fIsEndUndef = true;
122 } else if (i == 3) {
123 fStepDims.push_back(Dim{std::string("step_") + fNOutput + "_" + std::to_string(k)});
124 fIsStepUndef = true;
125 }
126 }
127 }
128 }
129 }
130 } else {
131 // old slice versions
132 assert(fAttributes.size() > 1);
133 for (size_t i = 0; i < fAttributes.size(); i++) {
134 itensors[i] = fAttributes[i];
135 }
136 }
137 size_t dim = fShapeInput.size();
138
139 // default values
140 fSteps = std::vector<Dim>(dim, Dim{1});
141 fStart = std::vector<Dim>(dim, Dim{0});
143
144 // default axes
145 if (itensors[2].empty()) {
146 fAxes.resize(dim);
147 std::iota(fAxes.begin(), fAxes.end(), 0);
148 } else {
149 fAxes = itensors[2];
150 for (size_t i = 0; i < fAxes.size(); i++) {
151 // negative axes - they count from the back
152 if (fAxes[i] < 0) fAxes[i] = dim + fAxes[i];
153 if (fAxes[i] < 0 || fAxes[i] >= static_cast<IType>(dim))
154 throw std::runtime_error("TMVA Slice Op : invalid axis value " + std::to_string(fAxes[i]) +
155 " for " + std::to_string(i));
156 }
157 }
158 // Loop on axis to get start/end/step values
159 for (size_t i = 0; i < fAxes.size(); i++) {
160 if (!itensors[0].empty() )
161 fStartDims.push_back(Dim{ static_cast<size_t>(itensors[0][i])});
162 if (fStartDims.empty())
163 throw std::runtime_error("TMVA Slice Op : Missing start input tensor");
164
165 if (!itensors[1].empty())
166 fEndDims.push_back(Dim{ static_cast<size_t>(itensors[1][i])});
167 else if (fEndDims.empty())
168 throw std::runtime_error("TMVA Slice Op : Missing end input tensor");
169
170 if (!itensors[3].empty()) {
171 fStepDims.push_back(Dim{ static_cast<size_t>(itensors[3][i])});
172 }
173 else if (fStepDims.size() < fAxes.size()) // this can happen since it is optional
174 fStepDims.push_back(Dim{size_t(1)});
175
176 if (!fShapeInput[fAxes[i]].isParam) {
177 size_t iAxisDim = fShapeInput[fAxes[i]].dim;
178 //correct values if too large or too small
179 IType istart = 0;
180 if (!fStartDims[i].isParam) {
181 istart = static_cast<IType>(fStartDims[i].dim);
182 if (istart < 0) istart = iAxisDim + istart;
183 }
184 IType iend = static_cast<IType>(iAxisDim);
185 if (!fEndDims[i].isParam) {
186 iend = static_cast<IType>(fEndDims[i].dim);
187 if (iend < 0) iend = iAxisDim + iend;
188 }
189 //steps
190 IType istep = 1;
191 if (!fStepDims[i].isParam) {
192 istep = static_cast<IType>(fStepDims[i].dim);
193 } else {
194 throw std::runtime_error("TMVA Slice Op : parametric step inputs are not supported");
195 }
196 // clamp start end values depending on steps
197 // start must be [0,N] for positive steps or [0,N-1] for negative
198 // end must be [0,N] for positive steps or [-1, N-1] for negative
199 if (istart < 0) istart = 0;
200 if (istep > 0) {
201 if (istart > static_cast<IType>(iAxisDim)) istart = static_cast<IType>(iAxisDim);
202 if (iend < 0) iend = 0;
203 if (iend > static_cast<IType>(iAxisDim)) iend = static_cast<IType>(iAxisDim);
204 } else if (istep < 0) {
205 if (istart > static_cast<IType>(iAxisDim)-1) istart = static_cast<IType>(iAxisDim) -1;
206 if (iend < -1) iend = -1;
207 if (iend > static_cast<IType>(iAxisDim)-1) iend = static_cast<IType>(iAxisDim) -1;
208 } else {
209 throw std::runtime_error("TMVA Slice Op : invalid step value " + std::to_string(istep) +
210 " for " + std::to_string(i));
211 }
212 // for parametric values clamping we will done at run time
213 if (fStartDims[i].isParam)
214 fStart[fAxes[i]] = fStartDims[i];
215 else
216 fStart[fAxes[i]] = Dim{size_t(istart)};
217 if (fStartDims[i].isParam)
218 fEnd[fAxes[i]] = fEndDims[i];
219 else
220 fEnd[fAxes[i]] = Dim{size_t(iend)};
221
222 fSteps[fAxes[i]] = Dim{size_t(istep)};
223 } else {
224 // emit symbolic expressions that clamp input dimensions at run time
225 const std::string &pdim = fShapeInput[fAxes[i]].param;
226 if (!fStartDims[i].isParam) {
227 IType istart = static_cast<IType>(fStartDims[i].dim);
228 if (istart < 0) {
229 // N - k, saturated at 0
230 std::string k = std::to_string(-istart);
231 fStart[fAxes[i]] = Dim{"(" + pdim + ">" + k + "?" + pdim + "-" + k + ":0)", size_t(-1)};
232 } else if (istart == 0) {
233 fStart[fAxes[i]] = Dim{size_t(0)};
234 } else {
235 std::string v = std::to_string(istart);
236 fStart[fAxes[i]] = Dim{"(" + pdim + "<" + v + "?" + pdim + ":" + v + ")", size_t(-1)};
237 }
238 } else {
239 fStart[fAxes[i]] = fStartDims[i];
240 }
241 if (!fEndDims[i].isParam) {
242 IType iend = static_cast<IType>(fEndDims[i].dim);
243 if (iend < 0) {
244 // N - k, saturated at 0
245 std::string k = std::to_string(-iend);
246 fEnd[fAxes[i]] = Dim{"(" + pdim + ">" + k + "?" + pdim + "-" + k + ":0)", size_t(-1)};
247 } else if (iend == std::numeric_limits<IType>::max()){
248 fEnd[fAxes[i]] = fShapeInput[fAxes[i]];
249 } else {
250 std::string v = std::to_string(iend);
251 fEnd[fAxes[i]] = Dim{"(" + pdim + "<" + v + "?" + pdim + ":" + v + ")", size_t(-1)};
252 }
253 } else {
254 fEnd[fAxes[i]] = fEndDims[i];
255 }
256
257 fSteps[fAxes[i]] = fStepDims[i];
258 }
259
260 }
261 // find output shape
262 fShapeOutput.resize(dim);
263 for (size_t i = 0; i < dim; i++) {
264 if (!fEnd[i].isParam && !fStart[i].isParam && !fSteps[i].isParam) {
265 int64_t istart = static_cast<int64_t>(fStart[i].dim);
266 int64_t iend = static_cast<int64_t>(fEnd[i].dim);
267 int64_t istep= static_cast<int64_t>(fSteps[i].dim);
268 // number of loop iterations is ceil((end-start)/step)
269 int64_t s = (istep > 0) ? (iend - istart + istep - 1) / istep
270 : (iend - istart + istep + 1) / istep;
271 if (s < 0) s = 0;
272 fShapeOutput[i] = Dim{static_cast<size_t>(s)};
273 } else {
274 std::string s;
275 if (fStart[i].GetVal() != "0")
276 s = "(" + fEnd[i].GetVal() + ">" + fStart[i].GetVal() + "?" + fEnd[i].GetVal() + "-" +
277 fStart[i].GetVal() + ":0)";
278 else
279 s = fEnd[i].GetVal();
280 if (fSteps[i].GetVal() != "1") {
281 // ceil division, so that the output holds all the elements visited by the slice loop
282 s = "((" + s + "+" + fSteps[i].GetVal() + "-1)/" + fSteps[i].GetVal() + ")";
283 }
284 fShapeOutput[i] = Dim{s,size_t(-1)};
285 // add also the shape parameters to RModel to declare them when
286 // allocating output tensor
287 if (fEnd[i].isParam && fEnd[i].dim != size_t(-1))
288 model.AddShapeParam(fEnd[i].param,fEnd[i].dim );
289 if (fStart[i].isParam && fStart[i].dim != size_t(-1))
290 model.AddShapeParam(fStart[i].param,fStart[i].dim );
291 if (fSteps[i].isParam && fSteps[i].dim != size_t(-1))
292 model.AddShapeParam(fSteps[i].param,fSteps[i].dim );
293
294 }
295 }
296 // case input is a constant tensor and of int64 type
297 if (model.IsInitializedTensor(fNData) && model.GetTensorType(fNData) == ETensorType::INT64) {
298 fIsOutputConstant = true;
299 auto inputData = static_cast<int64_t*>(model.GetInitializedTensorData(fNData).get());
300 size_t outputSize = ConvertShapeToLength(ConvertShapeToInt(fShapeOutput));
301 std::vector<int64_t> outputData(outputSize);
302 std::vector<size_t> inputStride = UTILITY::ComputeStrideFromShape(ConvertShapeToInt(fShapeInput));
303 if (model.Verbose()) {
304 std::cout << "Do slice for initialized input ..(start, end, step)\n";
305 for (size_t ii = 0; ii< fStart.size(); ii++)
306 std::cout << fStart [ii] << " " << fEnd[ii] << " " << fSteps[ii] << std::endl;
307 }
308 // perform slice using a recursive function- need to use two lambda functions for this
309 auto sliceRecursive = [&](size_t iaxis, size_t & outIdx, size_t & inOffset) {
310 auto slice_impl = [&](size_t iax, size_t & outputIdx, size_t & inputOffset, auto & sliceRecImpl) {
311 if (fStart[iax].isParam || fEnd[iax].isParam || fSteps[iax].isParam)
312 throw std::runtime_error("TMVA Slice Op : cannot have parametric values when input is constant");
313 // compute indices
314 std::vector<IType> indices;
315 for (IType i = (IType) fStart[iax].dim; (IType(fSteps[iax].dim) > 0) ? i < IType(fEnd[iax].dim) : i > IType(fEnd[iax].dim); i += IType(fSteps[iax].dim) )
316 indices.push_back(i);
317 if (iax == dim-1) { // last axis
318 for (size_t i = 0; i < indices.size(); i++) {
319 outputData[outputIdx] = inputData[inputOffset + indices[i]];
320 outputIdx++;
321 }
322 return;
323 } else {
324 for (size_t i = 0; i < indices.size(); i++) {
325 size_t offset = inputOffset + inputStride[iax]*indices[i];
326 sliceRecImpl(iax+1, outputIdx, offset,sliceRecImpl);
327 }
328 }
329 };
330 slice_impl(iaxis, outIdx, inOffset,slice_impl);
331 };
332 size_t idx = 0;
333 size_t offset = 0;
334 sliceRecursive(0, idx, offset);
335
336 model.AddConstantTensor<int64_t>(fNOutput, ConvertShapeToInt(fShapeOutput), outputData.data());
337 if (model.Verbose()) {
338 std::cout << "Slice: output is a constant tensor " << ConvertDimShapeToString(fShapeOutput) << " : "
339 << ConvertValuesToString(outputData) << std::endl;
340 }
341 }
342 else if (model.IsShapeTensor(fNData) && !fStart[0].isParam && !fEnd[0].isParam) {
343 // case of input is a shape tensor. In this case rank=1 always, axis =0 and Slice is trivial
344 auto inputData = model.GetShapeTensorValues(fNData);
345 fOutputShapeData = std::vector<Dim>(inputData.begin() + fStart[0].dim, inputData.begin() + fEnd[0].dim);
346 // try to convert to integer values if possible
347 auto outputData = ConvertShapeToInt(fOutputShapeData);
348 fShapeOutput = { Dim{fOutputShapeData.size()}};
349 if (outputData.empty()) {
350 // is a param shape tensor
351 model.AddShapeTensor(fNOutput, fOutputShapeData);
352 fIsOutputParamShape = true;
353 if (model.Verbose()) {
354 std::cout << "Slice: output is a shape tensor -> " << fNOutput << " " << ConvertDimShapeToString(fShapeOutput) << " with values "
355 << ConvertDimShapeToString(fOutputShapeData) << " (shape)" << std::endl;
356 }
357 } else {
358 fIsOutputConstant = true;
359 std::vector<int64_t> data(outputData.size());
360 std::copy(outputData.begin(), outputData.end(), data.begin());
361 model.AddConstantTensor<int64_t>(fNOutput, {data.size()}, data.data());
362 if (model.Verbose()) {
363 std::cout << "Slice: output is a constant tensor -> " << fNOutput << " " << ConvertDimShapeToString(fShapeOutput) << " with values "
364 << ConvertDimShapeToString(fOutputShapeData) << " constant " << std::endl;
365 }
366 }
367 }
368 else {
369 // check if Slice is just an Identity operator in case start = 0, end = input_shape and step=1
370 size_t ndim = fShapeInput.size();
371 fIdentitySlice = fShapeOutput.size() == ndim;
372 // check also if input data is not input to the model. In that case we copy the data since we cannot just copy from the input pointer
373 fIdentitySlice &= (!model.IsReadyInputTensor(fNData) && !model.IsDimInputTensor(fNData));
374 for (size_t idim = 0; idim < ndim; idim++) {
375 if (!fIdentitySlice) break;
376 fIdentitySlice &= (fStart[idim].GetVal() == "0");
377 fIdentitySlice &= (fSteps[idim].GetVal() == "1");
378 fIdentitySlice &= (fEnd[idim].GetVal() == fShapeInput[idim].GetVal());
379 }
380
381 model.AddIntermediateTensor(fNOutput, model.GetTensorType(fNData), fShapeOutput);
382 // an identity slice does not change the data, so the output can share the memory of the input
383 if (fIdentitySlice)
384 fIsAlias = model.AddAliasTensor(fNOutput, fNData);
385
386 if (model.Verbose()) {
387 std::cout << "Slice " << fNData << " " << ConvertDimShapeToString(fShapeInput)
388 << "---> " << fNOutput << " " << ConvertDimShapeToString(fShapeOutput);
389 if (fIsAlias)
390 std::cout << " (using alias tensor since slice is an identity) ";
391 std::cout << std::endl;
392
393 }
394 }
395 }
396
397 std::string Generate(std::string opName) override {
398
399 if (fShapeInput.empty() || fShapeOutput.empty()){
400 throw std::runtime_error("TMVA SOFIE Slice Op called to Generate without being initialized first");
401 }
402
403 std::stringstream out;
404
405 out << "///------- Slice operator " << opName << "---> " << fNOutput << " "
406 << ConvertDimShapeToString(fShapeOutput) << "\n" << std::endl;
407 if (fIsOutputConstant) return out.str(); //no op for constant tensors
408 if (fIsOutputParamShape) {
409 out << "/// Slice output is a shape tensor with values : " << ConvertDimShapeToString(fShapeOutput) << "\n";
410 // need to generate code assigning values to shape tensors
411 for (int i = 0; i < static_cast<int>(fShapeOutput[0].dim); i++) {
412 out << SP << "tensor_" << fNOutput << "[" << i << "] = " << fOutputShapeData[i] << ";\n";
413 }
414 return out.str();
415 }
416
417 size_t ndim = fShapeInput.size();
418
419 if (fIdentitySlice) {
420 if (fIsAlias) {
421 out << "/// Slice is just an identity: the output points to the memory of the input\n";
422 out << SP << "auto * tensor_" << fNOutput << " = tensor_" << fNData << ";\n";
423 } else {
424 out << "/// Slice is just an identity (copy) \n";
425 out << SP << "std::copy(tensor_" << fNData << ", tensor_" << fNData << " + "
426 << ConvertDimShapeToLength(fShapeInput) << ", tensor_" << fNOutput << ");\n";
427 }
428 return out.str();
429 }
430
431 // loop on the dimensions depending no the orders
432 auto strides = UTILITY::ComputeStrideFromShape(fShapeInput);
433
434
435 out << SP << "{\n"; // define operator scope
436 for (size_t i = 0; i < fStepDims.size(); i++) {
437 if (fStepDims[i].isParam) {
438 if (fIsStepUndef)
439 out << SP << "size_t " << fStepDims[i] << " = tensor_" << fNames[3] << "[" << i << "];\n";
440 }
441 }
442 // special case for parametric values for start/end. Need to do clipping
443 for (size_t i = 0; i < fStartDims.size(); i++) {
444 if (fStartDims[i].isParam && fStartDims[i].param != fShapeInput[fAxes[i]].param) {
445 std::string s_start = "start_" + std::to_string(i);
446 if (fIsStartUndef) {
447 s_start = fStartDims[i].param;
448 out << SP << "size_t " << s_start << " = tensor_" << fNames[0] << "[" << i << "];\n";
449 } else {
450 out << SP << "size_t " << s_start << " = " << fStartDims[i] << ";\n";
451 fStart[fAxes[i]] = s_start; // need to use this value later when slicing
452 }
453 out << SP << "if (" << s_start << " < 0) " << s_start << " += " << fShapeInput[fAxes[i]] <<";\n";
454 out << SP << "if (" << s_start << " < 0) " << s_start << " = 0;\n";
455 if (!fStepDims[i].isParam) {
456 if (static_cast<IType>(fStepDims[i].dim) > 0 )
457 out << SP << "if (" << s_start << " > " << fShapeInput[fAxes[i]] << " ) " << s_start << " = " << fShapeInput[fAxes[i]] <<";\n";
458 else
459 out << SP << "if (" << s_start << " > " << fShapeInput[fAxes[i]] << " - 1" << " ) " << s_start << " = " << fShapeInput[fAxes[i]] << " - 1;\n";
460 }
461 }
462 // special case if step is negative and shape are equal and step is negative
463 else if (fStartDims[i].isParam && fStartDims[i].param == fShapeInput[fAxes[i]].param && !fStepDims[i].isParam && static_cast<IType>(fStepDims[i].dim) < 0 ) {
464 fStart[fAxes[i]] = Dim{ fStartDims[i].param + "-1" };
465 }
466 }
467 // now to for end
468 for (size_t i = 0; i < fEndDims.size(); i++) {
469 if (fEndDims[i].isParam && fEndDims[i].param != fShapeInput[fAxes[i]].param) {
470 std::string s_end = "end_" + std::to_string(i);
471 if (fIsEndUndef) {
472 s_end = fEndDims[i].param;
473 out << SP << "size_t " << s_end << " = tensor_" << fNames[1] << "[" << i << "];\n";
474 } else {
475 out << SP << "size_t " << s_end << " = " << fEndDims[i] << ";\n";
476 fEnd[fAxes[i]] = s_end; // need to use this value later when slicing
477 }
478 out << SP << "if (" << s_end << " < 0) " << s_end << " += " << fShapeInput[fAxes[i]] <<";\n";
479 if (!fStepDims[i].isParam) {
480 if (static_cast<IType>(fStepDims[i].dim) > 0 ) {
481 out << SP << "if (" << s_end << " < 0) " << s_end << " = 0;\n";
482 out << SP << "if (" << s_end << " > " << fShapeInput[fAxes[i]] << " ) " << s_end << " = " << fShapeInput[fAxes[i]] <<";\n";
483 } else {
484 out << SP << "if (" << s_end << " < -1) " << s_end << " = -1;\n";
485 out << SP << "if (" << s_end << " > " << fShapeInput[fAxes[i]] << " - 1" << " ) " << s_end << " = " << fShapeInput[fAxes[i]] << " - 1;\n";
486 }
487 }
488 }
489 // special case if step is negative and shape are equal and step is negative
490 else if (fEndDims[i].isParam && fEndDims[i].param == fShapeInput[fAxes[i]].param && !fStepDims[i].isParam && static_cast<IType>(fStepDims[i].dim) < 0 ) {
491 fEnd[fAxes[i]] = Dim{ fEndDims[i].param + "-1" };
492 }
493 }
494
495 out << SP << "size_t iOut = 0;\n";
496 std::string MSP = SP;
497 for (size_t idim = 0; idim < ndim; idim++) {
498 out << MSP << "for (size_t i" << idim << " = " << fStart[idim] << "; i" << idim << " < " << fEnd[idim]
499 << "; i" << idim << "+= " << fSteps[idim] << ") {\n";
500 MSP += SP;
501 if (idim < ndim-1) out << MSP << "size_t stride" << idim << " = " << strides[idim] << "*i" << idim << ";\n";
502 }
503 out << MSP << "size_t iInput = ";
504 for (size_t idim = 0; idim < ndim-1; idim++) out << " stride" << idim << " + ";
505 // here should be step size ?
506 out << "i" << ndim-1 << ";\n";
507 out << MSP << "tensor_" << fNOutput << "[iOut++] = tensor_" <<fNData << "[iInput];\n";
508 for (size_t idim = 0; idim < ndim; idim++) {
509 MSP = MSP.replace(0,SP.length(),"");
510 out << MSP << "}\n";
511 }
512 out << SP << "}\n"; // end operator scope
513
514 return out.str();
515 }
516
517};
518
519}//SOFIE
520}//Experimental
521}//TMVA
522
523
524#endif //TMVA_SOFIE_ROPERATOR_SLICE
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
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)
ROperator_Slice(std::string nameData, std::vector< std::string > names, std::string nameOutput)
std::string Generate(std::string opName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:44
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:45
std::string Clean_name(std::string input_tensor_name)
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 ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations