1#ifndef TMVA_SOFIE_ROPERATOR_SLICE
2#define TMVA_SOFIE_ROPERATOR_SLICE
13namespace Experimental{
18template <
typename IType>
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))
48 for (
size_t i = 0; i < names.size(); ++i) {
52 if (names.size() == 3) {
53 if (names[2] !=
"axes") {
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))
74 auto ret = std::vector<ETensorType>(1,
input[0]);
80 auto & input_shape =
input[0];
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++) {
93 throw std::runtime_error(
"TMVA Slice Op Input Tensor is not found in model");
96 std::vector<std::vector<size_t>>
shapes;
100 std::vector<std::vector<IType>> itensors(4);
103 for (
size_t i = 0; i <
fNames.size(); ++i) {
107 auto tensor =
static_cast<IType *
>(dptr.get());
109 assert(
vec.size() == 1);
110 itensors[i] = std::vector<IType>(tensor, tensor +
vec[0]);
116 itensors[2] = std::vector<IType>(
fShapeInput.size());
117 std::iota(itensors[2].begin(), itensors[2].end(), 0);
120 itensors[3] = std::vector<IType>(itensors[0].
size(), 1);
134 fSteps = std::vector<size_t>(dim, 1);
135 fStart = std::vector<size_t>(dim, 0);
138 auto istart = itensors[0];
139 auto iend = itensors[1];
140 auto iaxes = itensors[2];
141 auto isteps = itensors[3];
145 if (iaxes.size() > 0) {
146 for (
size_t i = 0; i < iaxes.size(); i++) {
148 if (iaxes[i] < 0) iaxes[i] = dim + iaxes[i];
149 size_t jaxis =
static_cast<size_t>(iaxes[i]);
153 IType start = (istart[i] >= 0) ? istart[i] : imax + istart[i];
154 if (start < 0) start = 0;
155 if (start >
static_cast<IType
>(imax))
158 IType ie = (iend[i] >= 0) ? iend[i] : imax + iend[i];
160 if (ie >
static_cast<IType
>(imax))
164 if (isteps.size() > 0) {
167 throw std::runtime_error(
"TMVA Slice Op : negative steps not supported");
169 fSteps[jaxis] = isteps[i];
180 OpName =
"op_" + OpName;
182 throw std::runtime_error(
"TMVA SOFIE Slice Op called to Generate without being initialized first");
185 std::stringstream out;
188 out <<
SP <<
"///------- Slice operator\n" << std::endl;
191 std::vector<size_t> strides(ndim,1);
192 for (
int i =
int(ndim-2); i >=0 ; i--) {
197 out <<
SP <<
"size_t iOut = 0;\n";
198 std::string MSP =
SP;
199 for (
size_t idim = 0; idim < ndim; idim++) {
200 out << MSP <<
"for (size_t i" << idim <<
" = " <<
fStart[idim] <<
"; i" << idim <<
" < " <<
fEnd[idim]
201 <<
"; i" << idim <<
"+= " <<
fSteps[idim] <<
") {\n";
203 if (idim < ndim-1) out << MSP <<
"size_t stride" << idim <<
" = " << strides[idim] <<
"*i" << idim <<
";\n";
205 out << MSP <<
"size_t iInput = ";
206 for (
size_t idim = 0; idim < ndim-1; idim++) out <<
" stride" << idim <<
" + ";
208 out <<
"i" << ndim-1 <<
";\n";
209 out << MSP <<
"tensor_" <<
fNOutput <<
"[iOut++] = tensor_" <<
fNData <<
"[iInput];\n";
210 for (
size_t idim = 0; idim < ndim; idim++) {
211 MSP = MSP.replace(0,
SP.length(),
"");
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)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
std::vector< std::vector< IType > > fAttributes
std::vector< size_t > fEnd
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::vector< size_t > fStart
std::vector< size_t > fSteps
std::vector< size_t > fShapeInput
ROperator_Slice(std::string nameData, std::vector< IType > starts, std::vector< IType > ends, std::vector< IType > axes, std::string nameOutput)
void Initialize(RModel &model)
ROperator_Slice(std::string nameData, std::vector< std::string > names, std::string nameOutput)
std::string Generate(std::string OpName)
std::vector< std::string > fNames
std::vector< size_t > fShapeOutput
const std::string SP
space used to correctly indent the generated C++ code
std::string Clean_name(std::string input_tensor_name)
create variable transformations