diff --git a/include/aidge/backend/cpu.hpp b/include/aidge/backend/cpu.hpp index 71d8499972e419c903e24809b1bed2cb4cad70af..f3af2bf3015fb45bda9736e9925c8527e2b0cad0 100644 --- a/include/aidge/backend/cpu.hpp +++ b/include/aidge/backend/cpu.hpp @@ -23,6 +23,7 @@ #include "aidge/backend/cpu/operator/FCImpl.hpp" #include "aidge/backend/cpu/operator/LeakyReLUImpl.hpp" #include "aidge/backend/cpu/operator/MatMulImpl.hpp" +#include "aidge/backend/cpu/operator/MulImpl.hpp" #include "aidge/backend/cpu/operator/PadImpl.hpp" #include "aidge/backend/cpu/operator/PowImpl.hpp" #include "aidge/backend/cpu/operator/ProducerImpl.hpp" diff --git a/include/aidge/backend/cpu/operator/MulImpl.hpp b/include/aidge/backend/cpu/operator/MulImpl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..54361e4f5f7a361032c9f4928392f18f183724ac --- /dev/null +++ b/include/aidge/backend/cpu/operator/MulImpl.hpp @@ -0,0 +1,50 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#ifndef AIDGE_CPU_OPERATOR_MULIMPL_H_ +#define AIDGE_CPU_OPERATOR_MULIMPL_H_ + +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/operator/Mul.hpp" +#include "aidge/utils/Registrar.hpp" +#include "aidge/utils/Types.h" +#include <memory> +#include <vector> + +namespace Aidge { +// class Mul_Op; + +// compute kernel registry for forward and backward +class MulImplForward_cpu + : public Registrable<MulImplForward_cpu, std::tuple<DataType, DataType, DataType>, void(const std::size_t, const std::size_t, const void*, const void*,void*)> { +}; +class MulImplBackward_cpu + : public Registrable<MulImplBackward_cpu, std::tuple<DataType, DataType, DataType>, void(const std::size_t, const std::size_t, const void*, const void*, void*)> { +}; + +class MulImpl_cpu : public OperatorImpl { +public: + MulImpl_cpu(const Mul_Op& op) : OperatorImpl(op) {} + + static std::unique_ptr<MulImpl_cpu> create(const Mul_Op& op) { + return std::make_unique<MulImpl_cpu>(op); + } + + NbElts_t getNbRequiredProtected(const IOIndex_t inputIdx) const override final; + void forward() override; +}; + +namespace { +static Registrar<Mul_Op> registrarMulImpl_cpu("cpu", Aidge::MulImpl_cpu::create); +} +} // namespace Aidge + +#endif /* AIDGE_CPU_OPERATOR_MULIMPL_H_ */ diff --git a/include/aidge/backend/cpu/operator/MulImpl_forward_kernels.hpp b/include/aidge/backend/cpu/operator/MulImpl_forward_kernels.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9caef8b88af3ca779309b60eba984a72db35f84a --- /dev/null +++ b/include/aidge/backend/cpu/operator/MulImpl_forward_kernels.hpp @@ -0,0 +1,64 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#ifndef AIDGE_CPU_OPERATOR_MULIMPL_FORWARD_KERNEL_H_ +#define AIDGE_CPU_OPERATOR_MULIMPL_FORWARD_KERNEL_H_ + +#include "aidge/utils/Registrar.hpp" + +#include "aidge/backend/cpu/operator/MulImpl.hpp" + +namespace Aidge { +template <class I1, class I2, class O> +void MulImpl_cpu_forward_kernel(std::size_t input1Length, + std::size_t input2Length, + const void* input1_, + const void* input2_, + void* output_) { + + const I1* input_1 = static_cast<const I1*>(input1_); + const I2* input_2 = static_cast<const I2*>(input2_); + O* output = static_cast<O*>(output_); + if (input2Length == input1Length) + { + for (std::size_t i = 0; i < input1Length; ++i) { + output[i] = input_1[i] * input_2[i]; + } + } + else if (input2Length == 1) + { + for (std::size_t i = 0; i < input1Length; ++i) { + output[i] = input_1[i] * input_2[0]; + } + } + else // input_2 is 1d and of size the number of channels of input_1 + { + for (std::size_t i = 0; i < input1Length; ++i) { + std::size_t channelIdx = i % input2Length; + output[i] = input_1[i] * input_2[channelIdx]; + } + } +} + +namespace { +static Registrar<MulImplForward_cpu> registrarMulImplForward_cpu_Float32( + {DataType::Float32, DataType::Float32, DataType::Float32}, + Aidge::MulImpl_cpu_forward_kernel<float, float, float>); +static Registrar<MulImplForward_cpu> registrarMulImplForward_cpu_Int32( + {DataType::Int32, DataType::Int32, DataType::Int32}, + Aidge::MulImpl_cpu_forward_kernel<int, int, int>); +static Registrar<MulImplForward_cpu> registrarMulImplForward_cpu_Float64( + {DataType::Float64, DataType::Float64, DataType::Float64}, + Aidge::MulImpl_cpu_forward_kernel<double, double, double>); +} // namespace +} // namespace Aidge + +#endif /* AIDGE_CPU_OPERATOR_MULIMPL_FORWARD_KERNEL_H_ */ diff --git a/src/operator/MulImpl.cpp b/src/operator/MulImpl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6eb245cf0b1afc8893dfbab13d3294b945b3e0e --- /dev/null +++ b/src/operator/MulImpl.cpp @@ -0,0 +1,51 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <cassert> +#include <chrono> // std::chrono::milliseconds +#include <numeric> // std::accumulate +#include <thread> // std::this_thread::sleep_for +#include <vector> + +#include "aidge/operator/Mul.hpp" +#include "aidge/utils/Types.h" + +#include "aidge/backend/cpu/operator/MulImpl.hpp" +#include "aidge/backend/cpu/operator/MulImpl_forward_kernels.hpp" + +Aidge::NbElts_t Aidge::MulImpl_cpu::getNbRequiredProtected(const Aidge::IOIndex_t /*inputIdx*/) const { + // this implementation can be in-place + return 0; +} + +void Aidge::MulImpl_cpu::forward() { + assert(mOp.getInput(0) && "missing input #0"); + assert(mOp.getInput(1) && "missing input #1"); + + assert(((mOp.getInput(1)->size() == 1) || + (mOp.getInput(1)->size() == mOp.getInput(0)->size()) || + (mOp.getInput(1)->nbDims() == 1 && mOp.getInput(1)->size() == mOp.getInput(0)->dims()[mOp.getInput(0)->nbDims()-1]) + ) && + "input #1 must either be a tensor of size 1, the number of channels of input # or the same size of input #0"); + + // Find the correct kernel type + auto kernelFunc = Registrar<MulImplForward_cpu>::create({ + mOp.getInput(0)->dataType(), + mOp.getInput(1)->dataType(), + mOp.getOutput(0)->dataType()}); + + // Call kernel + kernelFunc(std::static_pointer_cast<Tensor>(mOp.getInput(0))->size(), + std::static_pointer_cast<Tensor>(mOp.getInput(1))->size(), + mOp.getInput(0)->getImpl()->rawPtr(), + mOp.getInput(1)->getImpl()->rawPtr(), + mOp.getOutput(0)->getImpl()->rawPtr()); +} diff --git a/unit_tests/operator/Test_MulImpl.cpp b/unit_tests/operator/Test_MulImpl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfb4ffc2d3aea74ada4ed3656041deb7cafb5a99 --- /dev/null +++ b/unit_tests/operator/Test_MulImpl.cpp @@ -0,0 +1,129 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <catch2/catch_test_macros.hpp> + +#include "aidge/data/Tensor.hpp" +#include "aidge/operator/Mul.hpp" + +#include "aidge/backend/cpu.hpp" + +#include <memory> + +using namespace Aidge; + +TEST_CASE("[cpu/operator] Mul(forward)") { + SECTION("2D Tensor by Singleton") { + std::shared_ptr<Tensor> input_1 = std::make_shared<Tensor>(Array2D<float,2,2> { + { + {0.38977361, 0.34064174}, + {0.00427264, 0.90872520} + } + }); + std::shared_ptr<Tensor> input_2 = std::make_shared<Tensor>(Array2D<float,1,1>{{3.0}}); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,2> { + { + {1.16932082, 1.02192521}, + {0.01281792, 2.72617555} + } + }); + + std::shared_ptr<Node> myMul = Mul(); + myMul->getOperator()->setDatatype(DataType::Float32); + myMul->getOperator()->setBackend("cpu"); + myMul->getOperator()->associateInput(0, input_1); + myMul->getOperator()->associateInput(1, input_2); + myMul->getOperator()->computeOutputDims(); + myMul->forward(); + + float* resPtr = static_cast<float*>(myMul->getOperator()->getOutput(0)->getImpl()->rawPtr()); + float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr()); + for (std::size_t i = 0; i< 4; ++i) { + REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001); + } + + } + + SECTION("2D Tensors") { + std::shared_ptr<Tensor> input_1 = std::make_shared<Tensor>(Array2D<float,2,2> { + { + {0.38977361, 0.34064174}, + {0.00427264, 0.90872520} + } + }); + std::shared_ptr<Tensor> input_2 = std::make_shared<Tensor>(Array2D<float,2,2>{ + { + {0.02362096, 0.24084556}, + {0.94690859, 0.13512510} + } + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,2> { + { + {0.00920683, 0.08204205}, + {0.00404580, 0.12279158} + } + }); + + std::shared_ptr<Node> myMul = Mul(); + myMul->getOperator()->setDatatype(DataType::Float32); + myMul->getOperator()->setBackend("cpu"); + myMul->getOperator()->associateInput(0, input_1); + myMul->getOperator()->associateInput(1, input_2); + myMul->getOperator()->computeOutputDims(); + myMul->forward(); + + float* resPtr = static_cast<float*>(myMul->getOperator()->getOutput(0)->getImpl()->rawPtr()); + float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr()); + for (std::size_t i = 0; i< 4; ++i) { + REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001); + } + + } + + SECTION("3D Tensor by 1D Tensor") { + std::shared_ptr<Tensor> input_1 = std::make_shared<Tensor>(Array3D<float,2,2,3> { + { + {{0.33647752, 0.89360154, 0.46586215}, + {0.71518236, 0.71481097, 0.97991812}}, + + {{0.17393428, 0.56849813, 0.18489265}, + {0.78397650, 0.00348300, 0.65758008}} + } + }); + std::shared_ptr<Tensor> input_2 = std::make_shared<Tensor>(Array1D<float,3>{ + {0.15380561, 0.51063120, 0.93031412} + }); + std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array3D<float,2,2,3> { + { + {{0.05175213, 0.45630082, 0.43339813}, + {0.10999906, 0.36500478, 0.91163164}}, + + {{0.02675207, 0.29029289, 0.17200825}, + {0.12057999, 0.00177853, 0.61175603}} + } + }); + + std::shared_ptr<Node> myMul = Mul(); + myMul->getOperator()->setDatatype(DataType::Float32); + myMul->getOperator()->setBackend("cpu"); + myMul->getOperator()->associateInput(0, input_1); + myMul->getOperator()->associateInput(1, input_2); + myMul->getOperator()->computeOutputDims(); + myMul->forward(); + + float* resPtr = static_cast<float*>(myMul->getOperator()->getOutput(0)->getImpl()->rawPtr()); + float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr()); + for (std::size_t i = 0; i< 4; ++i) { + REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001); + } + + } +} \ No newline at end of file