1#ifndef TMVA_SOFIE_ROPERATOR_POOL
2#define TMVA_SOFIE_ROPERATOR_POOL
16namespace Experimental {
74 fNX(UTILITY::Clean_name(nameX)),
fNY(UTILITY::Clean_name(nameY))
76 if(std::is_same<T, float>::value) {
80 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a Pool operator");
85 std::vector<ETensorType>
TypeInference(std::vector<ETensorType> input) {
91 std::vector<std::vector<size_t>>
ShapeInference(std::vector<std::vector<size_t>> input) {
95 if (input.size() != 1 ) {
96 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op Shape inference need 1 input tensor");
98 if (input[0].
size() < 3) {
99 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op Shape inference only accept tensor with at leat 3 dimensions");
102 if (input[0].
size() != 4) {
103 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op : tensors with dimension " + std::to_string(input[0].
size()) +
" are not yet supported");
130 std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op invalid Autopad value : " +
fAttrAutopad);
137 size_t outputHeight =
145 std::vector<std::vector<size_t>> ret({{input[0][0], input[0][1], outputHeight, outputWidth}});
152 std::runtime_error(
"TMVA SOFIE Conv op Input Tensor " +
fNX +
" is not found in model");
157 std::runtime_error(
"TMVA SOFIE Conv Op input tensor" +
fNX +
" is not of 4 dimensions");
182 std::stringstream out;
189 opName =
"op_" + opName;
190 std::stringstream out;
192 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
201 OpName =
"op_" + OpName;
204 throw std::runtime_error(
"TMVA SOFIE Pool Op called to Generate without being initialized first");
207 std::stringstream out;
209 out <<
"\n//---- operator " <<
Name() <<
" " << OpName <<
"\n";
232 out <<
SP <<
"constexpr int hsize = " <<
fShapeX[2] <<
";\n";
233 out <<
SP <<
"constexpr int wsize = " <<
fShapeX[3] <<
";\n";
234 out <<
SP <<
"constexpr int hmin = " << hmin <<
";\n";
235 out <<
SP <<
"constexpr int hmax = " << hmax <<
";\n";
236 out <<
SP <<
"constexpr int wmin = " << wmin <<
";\n";
237 out <<
SP <<
"constexpr int wmax = " << wmax <<
";\n";
241 bool doPadding =
false;
243 doPadding |= (
e > 0);
246 out <<
SP <<
"size_t outIndex = 0;\n";
247 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
249 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
250 out <<
SP <<
SP <<
SP <<
"for (int j = wmin; j < wmax; j+=" <<
fAttrStrides[1] <<
") {\n";
253 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
255 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
257 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
259 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kw*kh;\n";
262 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
263 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
265 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int k = j; k < j + kw; k++) {\n";
266 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (k<0 || k>= wsize) continue;\n";
267 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l*hsize + k;\n";
269 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
270 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
274 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
280 out <<
SP <<
SP <<
SP <<
SP <<
"}\n";
283 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
286 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
287 out <<
SP <<
SP <<
SP <<
"}\n";
288 out <<
SP <<
SP <<
"}\n";
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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)
void AddNeededStdLib(std::string libname)
const std::vector< size_t > & GetTensorShape(std::string name)
std::vector< size_t > fShapeY
void Initialize(RModel &model)
size_t fAttrCountIncludePad
std::vector< size_t > fAttrKernelShape
std::vector< size_t > fAttrStrides
std::vector< size_t > fAttrDilations
std::vector< size_t > fAttrPads
std::string Generate(std::string OpName)
std::string GenerateInitCode()
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::vector< size_t > fShapeX
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
virtual std::string GenerateSessionMembersCode(std::string opName)
ROperator_Pool(PoolOpMode mode, RAttributes_Pool attr, std::string nameX, std::string nameY)
const std::string SP
space used to correctly indent the generated C++ code
create variable transformations
std::vector< size_t > pads
std::vector< size_t > kernel_shape
std::vector< size_t > dilations
std::vector< size_t > strides