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;
159 std::runtime_error(
"TMVA SOFIE" +
Name() +
"Op invalid Autopad value : " +
fAttrAutopad);
171 size_t input1 =
input[0][2];
172 size_t input2 = (
fDim > 1) ?
input[0][3] : 1;
173 size_t input3 = (
fDim > 2) ?
input[0][4] : 1;
178 size_t batch_size =
input[0][0];
179 size_t output_channels =
input[0][1];
181 std::vector<std::vector<size_t>> ret({{ batch_size, output_channels, output1 }});
189 ret[0].push_back(output2);
197 ret[0].push_back(output3);
207 std::runtime_error(
"TMVA SOFIE Pool op Input Tensor " +
fNX +
" is not found in model");
213 std::runtime_error(
"TMVA SOFIE Pool Op input data tensor" +
fNX +
" is not of 3,4 or 5 dimensions");
239 std::stringstream out;
246 opName =
"op_" + opName;
247 std::stringstream out;
250 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
254 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
259 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xpad = std::vector<" <<
fType <<
">("
268 OpName =
"op_" + OpName;
271 throw std::runtime_error(
"TMVA SOFIE Pool Op called to Generate without being initialized first");
274 std::stringstream out;
276 out <<
"\n//---- operator " <<
Name() <<
" " << OpName <<
"\n";
304 out <<
SP <<
"constexpr int hsize = " <<
fShapeX[2] <<
";\n";
305 out <<
SP <<
"constexpr int hmin = " <<
hmin <<
";\n";
306 out <<
SP <<
"constexpr int hmax = " <<
hmax <<
";\n";
310 out <<
SP <<
"constexpr int wsize = " << wsize <<
";\n";
311 out <<
SP <<
"constexpr int wmin = " <<
wmin <<
";\n";
312 out <<
SP <<
"constexpr int wmax = " <<
wmax <<
";\n";
316 out <<
SP <<
"constexpr int dsize = " << dsize <<
";\n";
317 out <<
SP <<
"constexpr int dwsize = " << dsize*wsize <<
";\n";
318 out <<
SP <<
"constexpr int dmin = " << dmin <<
";\n";
319 out <<
SP <<
"constexpr int dmax = " << dmax <<
";\n";
325 bool doPadding =
false;
327 doPadding |= (
e > 0);
332 out <<
SP <<
"size_t outIndex = 0;\n";
333 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
334 out <<
SP <<
SP <<
"size_t inputOffset = n*" <<
fShapeX[2] <<
";\n";
335 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
338 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
340 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
342 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
344 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kh;\n";
347 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
348 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
349 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l;\n";
351 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
352 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
356 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
364 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
367 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
369 out <<
SP <<
SP <<
"}\n";
374 out <<
SP <<
"size_t outIndex = 0;\n";
375 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
377 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
378 out <<
SP <<
SP <<
SP <<
"for (int j = wmin; j < wmax; j+=" <<
fAttrStrides[1] <<
") {\n";
381 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
383 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
385 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
387 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kw*kh;\n";
390 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
391 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
393 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int m = j; m < j + kw; m++) {\n";
394 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (m < 0 || m >= wsize) continue;\n";
395 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l*wsize + m;\n";
397 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
398 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
402 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
411 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
413 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
414 out <<
SP <<
SP <<
SP <<
"}\n";
415 out <<
SP <<
SP <<
"}\n";
420 out <<
SP <<
"size_t outIndex = 0;\n";
421 out <<
SP <<
"for (size_t n = 0; n < " <<
fShapeX[0]*
fShapeX[1] <<
"; n++) {\n";
423 out <<
SP <<
SP <<
"for (int i = hmin; i < hmax; i+=" <<
fAttrStrides[0] <<
") {\n";
424 out <<
SP <<
SP <<
SP <<
"for (int j = wmin; j < wmax; j+=" <<
fAttrStrides[1] <<
") {\n";
425 out <<
SP <<
SP <<
SP <<
SP <<
"for (int k = dmin; k < dmax; k+=" <<
fAttrStrides[2] <<
") {\n";
428 out <<
SP <<
SP <<
SP <<
SP <<
"float value = -INFINITY;\n";
430 out <<
SP <<
SP <<
SP <<
SP <<
"float value = 0;\n";
432 out <<
SP <<
SP <<
SP <<
SP <<
"int nsum = 0;\n";
434 out <<
SP <<
SP <<
SP <<
SP <<
"constexpr int nsum = kw*kh*kd;\n";
437 out <<
SP <<
SP <<
SP <<
SP <<
"for (int l = i; l < i + kh; l++) {\n";
438 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (l < 0 || l >= hsize) continue;\n";
440 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int m = j; m < j + kw; m++) {\n";
441 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (m < 0 || m >= wsize) continue;\n";
443 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"for (int p = k; p < k + kd; p++) {\n";
444 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (p < 0 || p >= dsize) continue;\n";
445 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"int index = inputOffset + l*dwsize + m*dsize + p;\n";
448 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"auto xval = tensor_" <<
fNX <<
"[index];\n";
449 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"if (xval > value) value = xval;\n";
453 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
SP <<
"value += tensor_" <<
fNX <<
"[index];\n";
460 out <<
SP <<
SP <<
SP <<
SP <<
"}\n";
463 out <<
SP <<
SP <<
SP <<
SP <<
"value /= float(nsum);\n";
466 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[outIndex++] = value;\n";
467 out <<
SP <<
SP <<
SP <<
SP <<
"}\n" ;
468 out <<
SP <<
SP <<
SP <<
"}\n";
469 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
void AddNeededStdLib(std::string libname)
const ETensorType & GetTensorType(std::string name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
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