1#ifndef TMVA_SOFIE_ROPERATOR_Pad
2#define TMVA_SOFIE_ROPERATOR_Pad
30 std::vector<std::pair<int64_t, int64_t>>
fPads;
35 ROperator_Pad(
const std::string & nameX,
const std::string & nameP,
const std::string & nameCV,
36 const std::string & nameAX,
const std::string & nameY,
const std::string & mode) :
42 if (mode ==
"constant")
44 else if (mode ==
"reflect")
46 else if (mode ==
"edge")
48 else if (mode ==
"wrap")
55 std::vector<ETensorType>
TypeInference(std::vector<ETensorType> input)
override {
59 std::vector<std::vector<size_t>>
ShapeInference(std::vector<std::vector<size_t>> input)
override {
65 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
66 throw std::runtime_error(
"TMVA SOFIE Pad Op Input Tensor is not found in model");
72 throw std::runtime_error(
"TMVA SOFIE Pad Op supports now only Constant mode");
76 int64_t * padsData =
nullptr;
77 if (model.IsInitializedTensor(
fNP)) {
78 padsData =
static_cast<int64_t*
>(model.GetInitializedTensorData(
fNP).get());
80 throw std::runtime_error(
"TMVA SOFIE Pad Op supports now only initialized Pads data");
85 if (model.IsInitializedTensor(
fNCV)) {
86 T * cData =
static_cast<T*
>(model.GetInitializedTensorData(
fNCV).get());
89 throw std::runtime_error(
"TMVA SOFIE Pad Op supports now only initialized Constant Value data");
92 std::vector<int64_t> axes;
94 if (model.IsInitializedTensor(
fNAX)) {
95 auto shape = model.GetTensorShape(
fNAX);
97 size_t nax = shape[0];
100 auto data =
static_cast<int64_t*
>(model.GetInitializedTensorData(
fNAX).get());
101 axes = std::vector<int64_t>(data, data + nax);
103 auto data =
static_cast<int32_t*
>(model.GetInitializedTensorData(
fNAX).get());
105 for (
size_t i = 0; i < nax; i++)
108 throw std::runtime_error(
"TMVA SOFIE Pad Op invalid input Axes type");
111 throw std::runtime_error(
"TMVA SOFIE Pad Op supports now only initialized Axes data");
117 size_t axesSize = axes.size();
127 if (axes[i] == int64_t(i)) {
128 fPads[i].first = padsData[i];
129 fPads[i].second = padsData[axesSize + i];
132 throw std::runtime_error(
"TMVA SOFIE Pad Op : invalid Pads values");
139 if (model.Verbose()) {
140 std::cout <<
"initializing Pad operator with pads .. : ";
141 for (
auto & p :
fPads)
142 std::cout <<
"{ " << p.first <<
" , " << p.second <<
"} ";
143 std::cout << std::endl;
151 std::string
Generate(std::string OpName)
override {
152 OpName =
"op_" + OpName;
154 throw std::runtime_error(
"TMVA SOFIE Operator Pad called to Generate without being initialized first");
156 std::stringstream out;
159 out <<
"\n//------ Pad\n";
163 out <<
"std::fill(tensor_" <<
fNY <<
", tensor_" <<
fNY <<
" + " << length <<
","
167 for (
int i = 0; i < dims; i++) {
168 for (
int j = 1; j < i; j++) out <<
SP;
169 out <<
"for (int id" << i <<
" = 0; id" << i <<
" < " <<
fInputShape[i] <<
"; id"
174 for (
int j = 0; j < dims; j++) out <<
SP;
175 out <<
"tensor_" <<
fNY <<
"[";
176 for (
int i = 0; i < dims; i++) {
178 if (
fPads[i].first != 0) out <<
" + " <<
fPads[i].first;
180 if (i < dims-1) out <<
" * " << outStride[i] <<
" + ";
182 out <<
"] =\n tensor_" <<
fNX <<
"[";
183 for (
int i = 0; i < dims; i++) {
185 if (i < dims-1) out <<
" * " << inputStride[i] <<
" + ";
188 for (
int i = dims-1; i >= 0; i--) {
189 for (
int j = 1; j < i; j++) out <<
SP;
std::vector< size_t > fInputShape
std::vector< size_t > fOutputShape
void Initialize(RModel &model) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string OpName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< std::pair< int64_t, int64_t > > fPads
ROperator_Pad(const std::string &nameX, const std::string &nameP, const std::string &nameCV, const std::string &nameAX, const std::string &nameY, const std::string &mode)
std::vector< std::string_view > fInputTensorNames
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
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::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations