From bc5dd5749b4da611c8f8b9017703f1621a8e6366 Mon Sep 17 00:00:00 2001 From: NAUD Maxence <maxence.naud@cea.fr> Date: Mon, 25 Mar 2024 23:33:59 +0000 Subject: [PATCH] Remove template from ReduceMeanImpl and fix type in kernel --- .../ReduceMeanImpl_forward_kernels.hpp | 2 +- src/operator/ReshapeImpl.cpp | 24 +++--- unit_tests/operator/Test_ReduceMeanImpl.cpp | 78 ++++++++++++------- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/include/aidge/backend/cpu/operator/ReduceMeanImpl_forward_kernels.hpp b/include/aidge/backend/cpu/operator/ReduceMeanImpl_forward_kernels.hpp index 25796f22..d7a967e8 100644 --- a/include/aidge/backend/cpu/operator/ReduceMeanImpl_forward_kernels.hpp +++ b/include/aidge/backend/cpu/operator/ReduceMeanImpl_forward_kernels.hpp @@ -96,7 +96,7 @@ void ReduceMeanImpl_cpu_forward_kernel(const typename ReduceMean_Op::Attrs& attr // Copy elements from inputAccumulation to output while dividing by divisor I divisor = totalElements / outputElements; std::transform(inputAccumulation, inputAccumulation + outputElements, output, - [divisor](int element) { return element / divisor; }); + [divisor](I element) { return element / divisor; }); if (outputAccumulation) { delete[] outputAccumulation; } diff --git a/src/operator/ReshapeImpl.cpp b/src/operator/ReshapeImpl.cpp index 02dea1da..11df6f66 100644 --- a/src/operator/ReshapeImpl.cpp +++ b/src/operator/ReshapeImpl.cpp @@ -9,13 +9,13 @@ * ********************************************************************************/ -#include <cassert> +#include "aidge/backend/cpu/operator/ReshapeImpl.hpp" +#include "aidge/backend/cpu/operator/ReshapeImpl_forward_kernels.hpp" +#include "aidge/data/Tensor.hpp" #include "aidge/operator/Reshape.hpp" #include "aidge/utils/Types.h" - -#include "aidge/backend/cpu/operator/ReshapeImpl.hpp" -#include "aidge/backend/cpu/operator/ReshapeImpl_forward_kernels.hpp" +#include "aidge/utils/ErrorHandling.hpp" Aidge::NbElts_t Aidge::ReshapeImpl_cpu::getNbRequiredProtected(const Aidge::IOIndex_t /*inputIdx*/) const { // this implementation can be in-place @@ -23,17 +23,17 @@ Aidge::NbElts_t Aidge::ReshapeImpl_cpu::getNbRequiredProtected(const Aidge::IOIn } void Aidge::ReshapeImpl_cpu::forward() { - assert(std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->size() == - std::static_pointer_cast<Tensor>(mOp.getRawOutput(0))->size() - && "input must have the same overall size as shape"); + const Reshape_Op& op_ = static_cast<const Reshape_Op&>(mOp); + AIDGE_ASSERT(op_.getInput(0)->size() == op_.getOutput(0)->size(), + "input must have the same overall size as shape"); // Find the correct kernel type auto kernelFunc = Registrar<ReshapeImplForward_cpu>::create({ - std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->dataType(), - std::static_pointer_cast<Tensor>(mOp.getRawOutput(0))->dataType()}); + op_.getInput(0)->dataType(), + op_.getOutput(0)->dataType()}); // Call kernel - kernelFunc(std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->size(), - std::static_pointer_cast<Tensor>(mOp.getRawInput(0))->getImpl()->rawPtr(), - std::static_pointer_cast<Tensor>(mOp.getRawOutput(0))->getImpl()->rawPtr()); + kernelFunc(op_.getInput(0)->size(), + op_.getInput(0)->getImpl()->rawPtr(), + op_.getOutput(0)->getImpl()->rawPtr()); } diff --git a/unit_tests/operator/Test_ReduceMeanImpl.cpp b/unit_tests/operator/Test_ReduceMeanImpl.cpp index 494b7a6a..d9bf68b7 100644 --- a/unit_tests/operator/Test_ReduceMeanImpl.cpp +++ b/unit_tests/operator/Test_ReduceMeanImpl.cpp @@ -17,6 +17,7 @@ #include "aidge/operator/Conv.hpp" #include "aidge/backend/cpu.hpp" +#include "aidge/utils/TensorUtils.hpp" using namespace Aidge; @@ -138,35 +139,60 @@ TEST_CASE("[cpu/operator] ReduceMean(forward)", "[ReduceMean][CPU]") { } SECTION("all_axes") { - std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array3D<float,3,2,2> { - { - { - { 5.0, 1.0 }, - { 20.0, 2.0 } - }, - { - { 30.0, 1.0 }, - { 40.0, 2.0 } - }, + SECTION("1") { + std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array3D<float,3,2,2> { { - { 55.0, 1.0 }, - { 60.0, 2.0 } + { + { 5.0, 1.0 }, + { 20.0, 2.0 } + }, + { + { 30.0, 1.0 }, + { 40.0, 2.0 } + }, + { + { 55.0, 1.0 }, + { 60.0, 2.0 } + } } - } - }); - std::shared_ptr<Tensor> myOutput = std::make_shared<Tensor>(Array1D<float,1> { - {18.25} - }); + }); + std::shared_ptr<Tensor> myOutput = std::make_shared<Tensor>(Array1D<float,1> { + {18.25} + }); - std::shared_ptr<Node> myReduceMean = ReduceMean({0, 1, 2}, 0); - auto op = std::static_pointer_cast<OperatorTensor>(myReduceMean -> getOperator()); - op->associateInput(0,myInput); - op->setDataType(DataType::Float32); - op->setBackend("cpu"); - op->computeOutputDims(); - myReduceMean->forward(); - op->getOutput(0)->print(); + std::shared_ptr<Node> myReduceMean = ReduceMean({0, 1, 2}, 0); + auto op = std::static_pointer_cast<OperatorTensor>(myReduceMean -> getOperator()); + op->associateInput(0,myInput); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + op->computeOutputDims(); + myReduceMean->forward(); + op->getOutput(0)->print(); - REQUIRE(*(op->getOutput(0)) == *myOutput); + REQUIRE(*(op->getOutput(0)) == *myOutput); + } + SECTION("2") { + std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array2D<float,5,4> { + {{ 0.004232f, 0.105120f, 0.045124f, 0.009205f}, + { 0.000766f, 0.272162f, 0.503560f, 0.044163f}, + { 0.049755f, 0.000305f, 0.143634f, 0.013253f}, + { 0.096258f, 0.311231f, 0.358143f, 0.000452f}, + { 0.468617f, 0.015693f, 0.145316f, 0.000105f}} + }); + std::shared_ptr<Tensor> myOutput = std::make_shared<Tensor>(Array1D<float,1> { + {0.1293547f} + }); + + std::shared_ptr<Node> myReduceMean = ReduceMean({0, 1}, 0); + auto op = std::static_pointer_cast<OperatorTensor>(myReduceMean -> getOperator()); + op->associateInput(0,myInput); + op->setDataType(DataType::Float32); + op->setBackend("cpu"); + op->computeOutputDims(); + myReduceMean->forward(); + op->getOutput(0)->print(); + // approxEq<float>(*(op->getOutput(0)), *myOutput); + REQUIRE(approxEq<float>(*(op->getOutput(0)), *myOutput)); + } } } \ No newline at end of file -- GitLab