Skip to content

Commit 7ebb002

Browse files
harz05guitargeek
authored andcommitted
fix maxpool/avgpool reading wrong pad indices for asym padding
1 parent 6183bad commit 7ebb002

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

tmva/sofie/inc/TMVA/ROperator_Pool.hxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,20 +287,20 @@ public:
287287
// find lower bounds of filtered area
288288
int hmin = - fAttrPads[0]; // minimum lower bound value of filter area
289289
// use stride instead of 1 when ceil_mode=1, so the loop covers the extra partial window
290-
int hmax = fShapeX[2] + fAttrPads[1] - fAttrKernelShape[0] + (fAttrCeilMode ? (int)fAttrStrides[0] : 1);
290+
int hmax = fShapeX[2] + fAttrPads[fDim] - fAttrKernelShape[0] + (fAttrCeilMode ? (int)fAttrStrides[0] : 1);
291291
int wmin,wmax,dmin,dmax;
292292

293293
if(fDim >= 2){
294-
wmin = - fAttrPads[2]; // minimum lower bound value of filter area
295-
wmax = fShapeX[3] + fAttrPads[3] - fAttrKernelShape[1] + (fAttrCeilMode ? (int)fAttrStrides[1] : 1);
294+
wmin = -fAttrPads[1]; // minimum lower bound value of filter area
295+
wmax = fShapeX[3] + fAttrPads[fDim + 1] - fAttrKernelShape[1] + (fAttrCeilMode ? (int)fAttrStrides[1] : 1);
296296
}
297297
else{
298298
wmin=1;
299299
wmax=1;
300300
}
301301
if(fDim == 3){
302-
dmin = - fAttrPads[4]; // minimum lower bound value of filter area
303-
dmax = fShapeX[4] + fAttrPads[5] - fAttrKernelShape[2] + (fAttrCeilMode ? (int)fAttrStrides[2] : 1);
302+
dmin = -fAttrPads[2]; // minimum lower bound value of filter area
303+
dmax = fShapeX[4] + fAttrPads[fDim + 2] - fAttrKernelShape[2] + (fAttrCeilMode ? (int)fAttrStrides[2] : 1);
304304
}
305305
else{
306306
dmin=1;

tmva/sofie/test/TestCustomModelsFromONNX.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ constexpr auto modelDataSuffix = "_FromONNX.dat";
3232
#include "input_models/references/MaxPool2d.ref.hxx"
3333
#include "input_models/references/MaxPool2d_CeilMode.ref.hxx"
3434
#include "input_models/references/MaxPool3d.ref.hxx"
35+
#include "input_models/references/MaxPool2d_AsymPad.ref.hxx"
3536
#include "input_models/references/Max.ref.hxx"
3637
#include "input_models/references/MaxMultidirectionalBroadcast.ref.hxx"
3738
#include "input_models/references/MinMultidirectionalBroadcast.ref.hxx"
@@ -848,6 +849,25 @@ TEST(ONNX, MaxPool2d){
848849

849850
}
850851

852+
TEST(ONNX, MaxPool2d_AsymPad)
853+
{
854+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
855+
856+
// 1x1x4x4 input with values 0..15
857+
std::vector<float> input({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
858+
859+
ASSERT_INCLUDE_AND_RUN(std::vector<float>, "MaxPool2d_AsymPad", input);
860+
861+
// pads=[0,1,0,1] (width padded, height not) gives a 1x1x3x5 output;
862+
// the pre-fix code mis-read the pads and produced a 4x4 grid instead
863+
EXPECT_EQ(output.size(), std::size(MaxPool2d_AsymPad_ExpectedOutput::output));
864+
865+
float *correct = MaxPool2d_AsymPad_ExpectedOutput::output;
866+
for (size_t i = 0; i < output.size(); ++i) {
867+
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
868+
}
869+
}
870+
851871
TEST(ONNX, MaxPool2d_CeilMode)
852872
{
853873
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
165 Bytes
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace MaxPool2d_AsymPad_ExpectedOutput {
2+
float output[] = {4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 15};
3+
} // namespace MaxPool2d_AsymPad_ExpectedOutput

0 commit comments

Comments
 (0)