1#ifndef TMVA_SOFIE_ROPERATOR_SLICE
2#define TMVA_SOFIE_ROPERATOR_SLICE
11namespace Experimental{
16template <
typename T,
typename IType>
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))
46 for (
size_t i = 0; i < names.size(); ++i) {
51 if (names.size() == 3 && names[2] !=
"axes") {
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))
68 auto ret = std::vector<ETensorType>(1, input[0]);
73 std::vector<std::vector<size_t>>
ShapeInference(std::vector<std::vector<size_t>> input){
74 auto & input_shape = input[0];
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++) {
87 throw std::runtime_error(
"TMVA Slice Op Input Tensor is not found in model");
90 std::vector<std::vector<size_t>>
shapes;
94 std::vector<std::vector<IType>> itensors(4);
97 for (
size_t i = 0; i <
fNames.size(); ++i) {
99 std::cout <<
" i " << i <<
" getting data for tensor " <<
fNames[i] << std::endl;
101 auto tensor =
static_cast<IType *
>(dptr.get());
103 assert(
vec.size() == 1);
104 itensors[i] = std::vector<IType>(tensor, tensor +
vec[0]);
115 fSteps = std::vector<size_t>(dim, 1);
116 fStart = std::vector<size_t>(dim, 0);
119 auto istart = itensors[0];
120 auto iend = itensors[1];
121 auto iaxes = itensors[2];
122 auto isteps = itensors[3];
126 if (iaxes.size() > 0) {
127 for (
size_t i = 0; i < iaxes.size(); i++) {
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);
134 IType start = (istart[i] > 0) ? istart[i] : imax + istart[i];
135 if (start < 0) start = 0;
136 if (start >
static_cast<IType
>(imax))
139 IType ie = (iend[i] > 0) ? iend[i] : imax + iend[i];
141 if (ie >
static_cast<IType
>(imax))
145 if (isteps.size() > 0) {
148 throw std::runtime_error(
"TMVA Slice Op : negative steps not supported");
150 fSteps[jaxis] = isteps[i];
161 OpName =
"op_" + OpName;
163 throw std::runtime_error(
"TMVA SOFIE Slice Op called to Generate without being initialized first");
166 std::stringstream out;
169 out <<
SP <<
"///------- Slice operator\n" << std::endl;
172 std::vector<size_t> strides(ndim,1);
173 for (
int i =
int(ndim-2); i >=0 ; i--) {
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";
183 if (idim < ndim-1) out << MSP <<
"size_t stride" << idim <<
" = " << strides[idim] <<
"*i" << idim <<
";\n";
185 out << MSP <<
"size_t iInput = ";
186 for (
size_t idim = 0; idim < ndim-1; idim++) out <<
" stride" << idim <<
" + ";
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(),
"");
const ETensorType & GetTensorType(std::string name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > 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< size_t > fShapeOutput
std::vector< std::string > fNames
std::vector< size_t > fEnd
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< size_t > fStart
void Initialize(RModel &model)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< size_t > fSteps
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::string Generate(std::string OpName)
std::vector< size_t > fShapeInput
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
std::string Clean_name(std::string input_tensor_name)
create variable transformations