1#ifndef TMVA_SOFIE_ROPERATOR_POOL
2#define TMVA_SOFIE_ROPERATOR_POOL
16namespace Experimental {
75 fNX(UTILITY::Clean_name(nameX)),
fNY(UTILITY::Clean_name(nameY))
77 if(std::is_same<T, float>::value) {
81 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a Pool operator");
96 if (
input.size() != 1 ) {
97 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op Shape inference need 1 input tensor");
100 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op Shape inference only accept tensor with at least 3 dimensions");
104 throw std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op : tensors with dimension " + std::to_string(
input[0].
size()) +
" are not yet supported");
109 std::runtime_error(
"TMVA SOFIE Pool Op Shape inference - invalid inputs ");
117 size_t i1 = (
fDim > 1) ? ((
fDim > 2) ? 3 : 2) : 1;
118 size_t i2 = (
fDim > 2) ? 4 : 3;
158 std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op invalid Autopad value : " +
fAttrAutopad);
170 size_t input1 =
input[0][2];
171 size_t input2 = (
fDim > 1) ?
input[0][3] : 1;
172 size_t input3 = (
fDim > 2) ?
input[0][4] : 1;
177 size_t batch_size =
input[0][0];
178 size_t output_channels =
input[0][1];
180 std::vector<std::vector<size_t>> ret({{ batch_size, output_channels, output1 }});
188 ret[0].push_back(output2);
196 ret[0].push_back(output3);
206 std::runtime_error(
"TMVA SOFIE Pool op Input Tensor " +
fNX +
" is not found in model");
212 std::runtime_error(
"TMVA SOFIE Pool Op input data tensor" +
fNX +
" is not of 3,4 or 5 dimensions");
238 std::stringstream out;
245 opName =
"op_" + opName;
246 std::stringstream out;
249 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
253 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
258 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
267 OpName =
"op_" + OpName;
270 throw std::runtime_error(
"TMVA SOFIE Pool Op called to Generate without being initialized first");
273 std::stringstream out;
275 out <<
"\n//---- operator " <<
Name() <<
" " << OpName <<
"\n";
303 out <<
SP <<
"constexpr int hsize = " <<
fShapeX[2] <<
";\n";
304 out <<
SP <<
"constexpr int hmin = " <<
hmin <<
";\n";
305 out <<
SP <<
"constexpr int hmax = " <<
hmax <<
";\n";
309 out <<
SP <<
"constexpr int wsize = " << wsize <<
";\n";
310 out <<
SP <<
"constexpr int wmin = " <<
wmin <<
";\n";
311 out <<
SP <<
"constexpr int wmax = " <<
wmax <<
";\n";
315 out <<
SP <<
"constexpr int dsize = " << dsize <<
";\n";
316 out <<
SP <<
"constexpr int dwsize = " << dsize*wsize <<
";\n";
317 out <<
SP <<
"constexpr int dmin = " << dmin <<
";\n";
318 out <<
SP <<
"constexpr int dmax = " << dmax <<
";\n";
324 bool doPadding =
false;
326 doPadding |= (
e > 0);
331 out <<
SP <<
"size_t outIndex = 0;\n";
332 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
333 out <<
SP <<
SP <<
"size_t inputOffset = n*" <<
fShapeX[2] <<
";\n";
334 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
337 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
339 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
341 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
343 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kh;\n";
346 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
347 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
348 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l;\n";
350 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
351 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
355 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
363 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
366 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
368 out <<
SP <<
SP <<
"}\n";
373 out <<
SP <<
"size_t outIndex = 0;\n";
374 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
376 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
377 out <<
SP <<
SP <<
SP <<
"for (int j = wmin; j < wmax; j+=" <<
fAttrStrides[1] <<
") {\n";
380 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
382 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
384 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
386 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kw*kh;\n";
389 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
390 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
392 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int m = j; m < j + kw; m++) {\n";
393 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (m < 0 || m >= wsize) continue;\n";
394 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l*wsize + m;\n";
396 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
397 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
401 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
410 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
412 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
413 out <<
SP <<
SP <<
SP <<
"}\n";
414 out <<
SP <<
SP <<
"}\n";
419 out <<
SP <<
"size_t outIndex = 0;\n";
420 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
422 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
423 out <<
SP <<
SP <<
SP <<
"for (int j = wmin; j < wmax; j+=" <<
fAttrStrides[1] <<
") {\n";
424 out <<
SP <<
SP <<
SP <<
SP <<
"for (int k = dmin; k < dmax; k+=" <<
fAttrStrides[2] <<
") {\n";
427 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
429 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
431 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
433 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kw*kh*kd;\n";
436 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
437 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
439 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int m = j; m < j + kw; m++) {\n";
440 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (m < 0 || m >= wsize) continue;\n";
442 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int p = k; p < k + kd; p++) {\n";
443 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (p < 0 || p >= dsize) continue;\n";
444 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l*dwsize + m*dsize + p;\n";
447 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
448 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
452 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
459 out <<
SP <<
SP <<
SP <<
SP <<
"}\n";
462 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
465 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
466 out <<
SP <<
SP <<
SP <<
SP <<
"}\n" ;
467 out <<
SP <<
SP <<
SP <<
"}\n";
468 out <<
SP <<
SP <<
"}\n";
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t hmin
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t hmax
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t wmin
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t wmax
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
std::string ConvertShapeToString(std::vector< size_t > shape)
create variable transformations
std::vector< size_t > pads
std::vector< size_t > kernel_shape
std::vector< size_t > dilations
std::vector< size_t > strides