From 54988d11af0e7e7dfb3ea8c56bc54eb03216bda0 Mon Sep 17 00:00:00 2001 From: hrouis <houssemeddine.rouis92@gmail.com> Date: Mon, 3 Feb 2025 15:09:26 +0100 Subject: [PATCH] add ceil_mode tests for Avg and Max Pooling --- .../cpu/operator/MaxPoolingImpl_kernels.hpp | 1 + unit_tests/operator/Test_AvgPoolingImpl.cpp | 57 +++++++++++++++++++ unit_tests/operator/Test_MaxPoolingImpl.cpp | 57 +++++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/include/aidge/backend/cpu/operator/MaxPoolingImpl_kernels.hpp b/include/aidge/backend/cpu/operator/MaxPoolingImpl_kernels.hpp index d5ac02fe..027fc02a 100644 --- a/include/aidge/backend/cpu/operator/MaxPoolingImpl_kernels.hpp +++ b/include/aidge/backend/cpu/operator/MaxPoolingImpl_kernels.hpp @@ -16,6 +16,7 @@ #include <cmath> #include <tuple> + #include "aidge/backend/cpu/operator/MaxPoolingImpl.hpp" #include "aidge/backend/cpu/data/GetCPUPtr.h" #include "aidge/data/Data.hpp" diff --git a/unit_tests/operator/Test_AvgPoolingImpl.cpp b/unit_tests/operator/Test_AvgPoolingImpl.cpp index 21a7a680..f116934c 100644 --- a/unit_tests/operator/Test_AvgPoolingImpl.cpp +++ b/unit_tests/operator/Test_AvgPoolingImpl.cpp @@ -144,4 +144,61 @@ TEST_CASE("[cpu/operator] AvgPooling(forward)", "[AvgPooling][CPU]") { op->getOutput(0)->print(); REQUIRE(*(op->getOutput(0)) == *myOutput3); } + SECTION("Ceil Mode") { + std::shared_ptr<Tensor> myInput4 = std::make_shared<Tensor>(Array4D<float,1,1,5,5> { // NCHW + { + { + { + { 1, 2, 3, 4, 5}, + { 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15}, + {16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25} + } + } + } + }); + + // AvgPool with ceil_mode = true + std::shared_ptr<Node> myAvgPool1 = AvgPooling({2,2}, "mycdw", {2,2}, {1,1}, true); + auto op1 = std::static_pointer_cast<AvgPooling_Op<2>>(myAvgPool1 -> getOperator()); + + std::shared_ptr<Tensor> myOutput4 = std::make_shared<Tensor>(Array4D<float,1,1,3,3> { + { + { + { + { 4.0, 6.0, 7.5 }, + { 14.0, 16.0, 17.5 }, + { 21.5, 23.5, 25.0 } + } + } + } + }); + op1->associateInput(0, myInput4); + op1->setDataType(DataType::Float32); + op1->setBackend("cpu"); + myAvgPool1->forward(); + op1->getOutput(0)->print(); + REQUIRE(*(op1->getOutput(0)) == *myOutput4); + + // AvgPool with ceil_mode = false + std::shared_ptr<Node> myAvgPool2 = AvgPooling({2,2}, "mycdw", {2,2}, {1,1}, false); + auto op2 = std::static_pointer_cast<AvgPooling_Op<2>>(myAvgPool2 -> getOperator()); + std::shared_ptr<Tensor> myOutput5 = std::make_shared<Tensor>(Array4D<float,1,1,2,2> { + { + { + { + { 4.0, 6.0 }, + { 14.0, 16.0 } + } + } + } + }); + op2->associateInput(0, myInput4); + op2->setDataType(DataType::Float32); + op2->setBackend("cpu"); + myAvgPool2->forward(); + op2->getOutput(0)->print(); + REQUIRE(*(op2->getOutput(0)) == *myOutput5); + } } \ No newline at end of file diff --git a/unit_tests/operator/Test_MaxPoolingImpl.cpp b/unit_tests/operator/Test_MaxPoolingImpl.cpp index 6b7e6d2f..d480fc30 100644 --- a/unit_tests/operator/Test_MaxPoolingImpl.cpp +++ b/unit_tests/operator/Test_MaxPoolingImpl.cpp @@ -115,4 +115,61 @@ TEST_CASE("[cpu/operator] MaxPooling(forward)", "[MaxPooling][CPU]") { op->getOutput(0)->print(); REQUIRE(*(op->getOutput(0)) == *myOutput); } + SECTION("Ceil Mode") { + std::shared_ptr<Tensor> myInput4 = std::make_shared<Tensor>(Array4D<float,1,1,5,5> { // NCHW + { + { + { + { 1, 2, 3, 4, 5}, + { 6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15}, + {16, 17, 18, 19, 20}, + {21, 22, 23, 24, 25} + } + } + } + }); + + // MaxPool with ceil_mode = true + std::shared_ptr<Node> myMaxPool1 = MaxPooling({2,2}, "mycdw", {2,2}, {1,1}, true); + auto op1 = std::static_pointer_cast<OperatorTensor>(myMaxPool1 -> getOperator()); + + std::shared_ptr<Tensor> myOutput4 = std::make_shared<Tensor>(Array4D<float,1,1,3,3> { + { + { + { + { 7.0, 9.0, 10.0 }, + { 17.0, 19.0, 20.0 }, + { 22.0, 24.0, 25.0 } + } + } + } + }); + op1->associateInput(0, myInput4); + op1->setDataType(DataType::Float32); + op1->setBackend("cpu"); + myMaxPool1->forward(); + op1->getOutput(0)->print(); + REQUIRE(*(op1->getOutput(0)) == *myOutput4); + + // MaxPool with ceil_mode = false + std::shared_ptr<Node> myMaxPool2 = MaxPooling({2,2}, "mycdw", {2,2}, {1,1}, false); + auto op2 = std::static_pointer_cast<OperatorTensor>(myMaxPool2 -> getOperator()); + std::shared_ptr<Tensor> myOutput5 = std::make_shared<Tensor>(Array4D<float,1,1,2,2> { + { + { + { + { 7.0, 9.0 }, + { 17.0, 19.0 } + } + } + } + }); + op2->associateInput(0, myInput4); + op2->setDataType(DataType::Float32); + op2->setBackend("cpu"); + myMaxPool2->forward(); + op2->getOutput(0)->print(); + REQUIRE(*(op2->getOutput(0)) == *myOutput5); + } } \ No newline at end of file -- GitLab