-
Houssem ROUIS authoredHoussem ROUIS authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Test_MulImpl.cpp 4.68 KiB
/********************************************************************************
* 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< 12; ++i) {
REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
}
}
}