Skip to content

Commit 7b7983f

Browse files
[TMVA][SOFIE] Fixed the implementation of MaxPool ONNX operator for 1d and 3d case (root-project#10768)
* Fix the issue related to 1d and 3d MaxPool operators * Update ROperator_Pool.hxx * The issue related to Maxpool for 1d and 3d fixed * Test related to MaxPool 1d added * MaxPool Operator fixed for 1d and 3d cases and tests added * Max Pool operator errors related to 1d and 3d case resolved * Max Pool operator errors related to 1d and 3d case resolved * Updated the Pool Operator * Fix warning in new implementation of Pool operator * Added the tests for MaxPool 2d and 3d Operators * Fix 2d and 3d MaxPool operators Use the correct stride for the height when loopoing at the tensor element. The height stride for 2d is equal to wsize, and in 3d is equal to wsize*dsize. Precompute the hstride in 3d to avoid a multiplication when looping through the tensor elements. * Tests added for AvgPool * Corrected the spelling errors Co-authored-by: moneta <[email protected]>
1 parent 08c41c6 commit 7b7983f

File tree

10 files changed

+455
-103
lines changed

10 files changed

+455
-103
lines changed

tmva/sofie/inc/TMVA/ROperator_Pool.hxx

Lines changed: 284 additions & 103 deletions
Large diffs are not rendered by default.

tmva/sofie/test/TestCustomModelsFromONNX.cxx

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
#include "ConvWithAsymmetricPadding_FromONNX.hxx"
3737
#include "input_models/references/ConvWithAsymmetricPadding.ref.hxx"
3838

39+
#include "MaxPool1d_FromONNX.hxx"
40+
#include "input_models/references/MaxPool1d.ref.hxx"
41+
42+
#include "MaxPool2d_FromONNX.hxx"
43+
#include "input_models/references/MaxPool2d.ref.hxx"
44+
45+
#include "MaxPool3d_FromONNX.hxx"
46+
#include "input_models/references/MaxPool3d.ref.hxx"
47+
48+
#include "AvgPool_FromONNX.hxx"
49+
#include "input_models/references/AvgPool.ref.hxx"
50+
3951
#include "RNNBatchwise_FromONNX.hxx"
4052
#include "input_models/references/RNNBatchwise.ref.hxx"
4153

@@ -360,6 +372,131 @@ TEST(DISABLED_ONNX, ConvWithAsymmetricPadding)
360372
}
361373
}
362374

375+
TEST(ONNX, MaxPool1d){
376+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
377+
378+
// Preparing the standard input
379+
std::vector<float> input({0.0907, 0.1029, 0.8143, 1.4497, -0.7785, 0.3825, -0.3764,
380+
1.5785, -0.0835, 0.1622,
381+
1.5867, 0.9823, -0.8821, 0.4439, -0.1378, -0.2273, -0.0198,
382+
-2.0230, 0.0905, 0.6674,
383+
-1.4290, -1.3100, -0.9439, -0.0833, -0.1919, 0.6886, 0.9389,
384+
-1.2914, -1.3584, -2.0341,
385+
-0.3269, 0.1704, 1.1776, 1.3972, -1.8874, -1.5334, 1.1541,
386+
0.3011, 0.6569, -2.3504,
387+
0.4033, 0.1142, 2.2846, -1.3948, -0.8573, 0.5756, -1.0864,
388+
0.2283, 0.8947, 1.7627,
389+
-0.1657, 0.0649, -1.6066, 0.4162, -1.1525, -0.8184, 1.1324,
390+
-1.1086, 0.1061, 1.0071});
391+
392+
TMVA_SOFIE_MaxPool1d::Session s("MaxPool1d_FromONNX.dat");
393+
std::vector<float> output = s.infer(input.data());
394+
// Checking output size
395+
EXPECT_EQ(output.size(), sizeof(MaxPool1d_ExpectedOutput::output) / sizeof(float));
396+
397+
float *correct = MaxPool1d_ExpectedOutput::output;
398+
399+
// Checking every output value, one by one
400+
for (size_t i = 0; i < output.size(); ++i) {
401+
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
402+
}
403+
404+
}
405+
406+
TEST(ONNX, MaxPool2d){
407+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
408+
409+
// Preparing the standard input
410+
std::vector<float> input({
411+
0.6266, 0.1656, 0.2753, -0.4558, -1.4592, 0.9285, -1.3410,
412+
1.3223, -0.5936, -1.3648,
413+
-0.2989, 0.5901, -0.8845, -0.0433, 0.8314, -1.7159, -0.5765,
414+
0.8678, 1.0257, 0.7847,
415+
-0.3421, -1.2364, -0.5805, 0.4421, 1.2184, 0.5043, 1.6823,
416+
-1.0483, -2.2798, -1.8927,
417+
0.7716, 0.0405, 0.3121, -0.3011, -0.3266, -1.9660, 1.0837,
418+
0.2317, 0.9084, -0.3285,
419+
-0.9398, -0.2065, -0.9499, -0.9739, -0.1288, -0.1375, -1.2612,
420+
0.8810, 0.8506, 0.4455
421+
});
422+
423+
TMVA_SOFIE_MaxPool2d::Session s("MaxPool2d_FromONNX.dat");
424+
std::vector<float> output = s.infer(input.data());
425+
// Checking output size
426+
EXPECT_EQ(output.size(), sizeof(MaxPool2d_ExpectedOutput::output) / sizeof(float));
427+
428+
float *correct = MaxPool2d_ExpectedOutput::output;
429+
430+
// Checking every output value, one by one
431+
for (size_t i = 0; i < output.size(); ++i) {
432+
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
433+
}
434+
435+
}
436+
437+
TEST(ONNX, MaxPool3d){
438+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
439+
440+
// Preparing the standard input
441+
std::vector<float> input({
442+
-2.6496, 1.0476, -0.5153,
443+
0.3771, 0.4129, -0.3077,
444+
-0.8717, -0.8040, -0.3525,
445+
446+
-0.1765, -0.3364, 0.8737,
447+
-0.2381, -0.8297, 0.4666,
448+
0.6984, -0.6760, 0.6298,
449+
450+
1.3833, 0.1101, 0.2039,
451+
-0.5477, 0.2341, 0.9181,
452+
0.3842, 0.2428, 1.7924
453+
});
454+
455+
TMVA_SOFIE_MaxPool3d::Session s("MaxPool3d_FromONNX.dat");
456+
std::vector<float> output = s.infer(input.data());
457+
// Checking output size
458+
EXPECT_EQ(output.size(), sizeof(MaxPool3d_ExpectedOutput::output) / sizeof(float));
459+
460+
float *correct = MaxPool3d_ExpectedOutput::output;
461+
462+
// Checking every output value, one by one
463+
for (size_t i = 0; i < output.size(); ++i) {
464+
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
465+
}
466+
467+
}
468+
469+
TEST(ONNX, AvgPool){
470+
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
471+
472+
// Preparing the standard input
473+
std::vector<float> input({
474+
0.4764, -0.1976, 1.6506, -0.2421, 0.6412, 1.9985, 0.3938,
475+
0.1347, 0.2204, -0.7503,
476+
0.2139, 0.7285, -0.0210, -0.4585, -1.5333, -0.4772, 0.5560,
477+
0.6323, -2.5372, 1.4906,
478+
-1.1062, -0.9703, 0.2366, -0.9184, 0.3014, 0.7985, -0.6841,
479+
-2.2854, -2.7728, -1.2806,
480+
-1.0947, -0.5990, -0.3033, -1.9042, -0.5403, 0.2332, 0.9215,
481+
-0.1549, 0.0557, -0.5567,
482+
-1.4971, 0.5386, -0.2922, 0.4860, -0.3973, -0.4624, 0.4514,
483+
0.2385, 0.3783, -1.0500
484+
});
485+
486+
TMVA_SOFIE_AvgPool::Session s("AvgPool_FromONNX.dat");
487+
std::vector<float> output = s.infer(input.data());
488+
// Checking output size
489+
EXPECT_EQ(output.size(), sizeof(AvgPool_ExpectedOutput::output) / sizeof(float));
490+
491+
float *correct = AvgPool_ExpectedOutput::output;
492+
493+
// Checking every output value, one by one
494+
for (size_t i = 0; i < output.size(); ++i) {
495+
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
496+
}
497+
498+
}
499+
363500
TEST(ONNX, RNNBatchwise)
364501
{
365502
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
220 Bytes
Binary file not shown.
202 Bytes
Binary file not shown.
218 Bytes
Binary file not shown.
234 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace AvgPool_ExpectedOutput {
2+
float output[] = {-0.1425, 0.2378, 0.0412, -0.3683, 0.2882, 0.4309, -0.2088,
3+
-1.1013, -0.9383,
4+
-0.7881, -0.2316, -0.4493, -0.4955, -0.0111, 0.2097, -0.2522,
5+
-0.7568, -0.8710};
6+
} // namespace AvgPool_ExpectedOutput
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MaxPool1d_ExpectedOutput {
2+
float output[] = {0.8143, 1.4497, 1.4497, 1.4497, 0.3825, 1.5785, 1.5785,
3+
1.5785,
4+
1.5867, 0.9823, 0.4439, 0.4439, -0.0198, -0.0198, 0.0905,
5+
0.6674,
6+
-0.9439, -0.0833, -0.0833, 0.6886, 0.9389, 0.9389, 0.9389,
7+
-1.2914,
8+
1.1776, 1.3972, 1.3972, 1.3972, 1.1541, 1.1541, 1.1541,
9+
0.6569,
10+
2.2846, 2.2846, 2.2846, 0.5756, 0.5756, 0.5756, 0.8947,
11+
1.7627,
12+
0.0649, 0.4162, 0.4162, 0.4162, 1.1324, 1.1324, 1.1324,
13+
1.0071};
14+
} // namespace MaxPool1d_ExpectedOutput
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MaxPool2d_ExpectedOutput {
2+
float output[] = {0.6266, 0.5901, 0.4421, 1.2184, 1.2184, 1.6823, 1.6823, 1.3223,
3+
1.0257,
4+
0.7716, 0.5901, 0.4421, 1.2184, 1.2184, 1.6823, 1.6823, 1.0257,
5+
1.0257,
6+
0.7716, 0.3121, 0.4421, 1.2184, 1.2184, 1.6823, 1.6823, 0.9084,
7+
0.9084};
8+
} // namespace MaxPool2d_ExpectedOutput
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace MaxPool3d_ExpectedOutput {
2+
float output[] = {
3+
1.3833, 1.0476,
4+
0.6984, 1.7924
5+
};
6+
} // namespace MaxPool3d_ExpectedOutput

0 commit comments

Comments
 (0)