1#ifndef TMVA_SOFIE_ROPERATOR_RESHAPE
2#define TMVA_SOFIE_ROPERATOR_RESHAPE
11namespace Experimental{
38 :
fOpMode(opMode),
fNData(UTILITY::Clean_name(nameData)),
fNShape(UTILITY::Clean_name(nameShape)),
39 fNOutput(UTILITY::Clean_name(nameOutput))
56 auto ret = std::vector<ETensorType>(1, input[0]);
61 std::vector<std::vector<size_t>>
ShapeInference(std::vector<std::vector<size_t>> input){
62 std::vector<std::vector<size_t>> ret;
63 auto & input_shape = input[0];
66 if (input.size() != 2)
throw std::runtime_error(
"TMVA SOFIE Reshape Op needs 2 input tensors");
67 auto output_shape = input[1];
71 if (input_length != output_length) {
72 if (output_shape.size() > 1 && ((output_length == 0 &&
fAllowZero == 0) || output_length >
INT64_MAX)) {
74 for (
size_t i = 0; i < output_shape.size(); i++) {
75 if (output_shape[i] == 0 || output_shape[i] ==
static_cast<size_t>(-1)) {
76 auto tmp = output_shape;
77 tmp.erase(tmp.begin() + i);
79 output_shape[i] = input_length / tmp_length;
85 throw std::runtime_error(
"TMVA Reshape Op : Invalid shapes : " +
ConvertShapeToString(input_shape) +
89 ret.push_back(output_shape);
94 size_t b = input[0][0];
95 std::vector<size_t> newShape = {
b, inputSize /
b};
96 ret.push_back(newShape);
101 auto output_shape = input[0];
102 if (input.size() == 1) {
103 for (
size_t i = 0; i < output_shape.size(); i++) {
104 if (output_shape[i] == 1 ) {
105 output_shape.erase(output_shape.begin() + i);
108 }
else if (input.size() == 2) {
109 auto & axes = input[1];
110 for (
size_t i = 0; i < axes.size(); i++){
111 if (output_shape[axes[i]] != 1)
114 output_shape.erase(output_shape.begin() + axes[i]);
117 ret.push_back(output_shape);
122 assert(input.size() == 2);
123 auto output_shape = input[0];
124 auto &axes = input[1];
126 for (
auto & i : axes)
127 output_shape.insert(output_shape.begin() + i, 1);
130 for (
auto &i : axes) {
132 output_shape.insert(output_shape.begin() + (output_shape.size() + i - 1), 1);
135 ret.push_back(output_shape);
145 throw std::runtime_error(
"TMVA Reshape Op Input Tensor is not found in model");
153 auto input_shape =
static_cast<int64_t *
>(dptr.get());
155 assert(
vec.size() == 1);
158 std::vector<size_t> descShape(
n);
159 std::copy(input_shape, input_shape +
n, descShape.begin());
162 throw std::runtime_error(
"TMVA Reshape Op Input Tensor is not found in model");
166 std::vector<size_t> descShape(
fAttrAxes.size());
172 throw std::runtime_error(
"TMVA Reshape Op : Invalid Input/Attribute data");
179 OpName =
"op_" + OpName;
181 throw std::runtime_error(
"TMVA SOFIE Reshape Op called to Generate without being initialized first");
187 throw std::runtime_error(
"TMVA SOFIE Reshape Op : wrong output shape - is " +
194 std::stringstream out;
195 std::string opName =
"Reshape";
201 opName =
"Unsquueze";
203 out <<
SP <<
"///--------" << opName <<
" operator\n" << std::endl;
204 out <<
SP <<
"std::copy( fTensor_" <<
fNData <<
".begin(), fTensor_" <<
fNData <<
".end(), fTensor_" <<
fNOutput
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)
ROperator_Reshape(ReshapeOpMode opMode, std::vector< int64_t > attrAxes, std::string nameData, std::string nameOutput)
std::vector< int64_t > fAttrAxes
std::vector< size_t > fShapeOutput
ROperator_Reshape(ReshapeOpMode opMode, int attr_value, std::string nameData, std::string nameShape, 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::vector< size_t > fShapeInput
void Initialize(RModel &model)
std::string Generate(std::string OpName)
const std::string SP
space used to correctly indent the generated C++ code
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations