-
Houssem ROUIS authoredHoussem ROUIS authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Test_DivImpl.cpp 5.10 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/Div.hpp"
#include "aidge/backend/cpu.hpp"
#include <memory>
using namespace Aidge;
TEST_CASE("[cpu/operator] Div(forward)") {
SECTION("2D Tensor") {
std::shared_ptr<Tensor> input_1 = std::make_shared<Tensor>(Array2D<float,2,2> {
{
{0.07607108, 0.44075000},
{0.19494885, 0.20071143}
}
});
std::shared_ptr<Tensor> input_2 = std::make_shared<Tensor>(Array2D<float,1,1>{{0.5}});
std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(Array2D<float,2,2> {
{
{0.15214217, 0.88150001},
{0.38989770, 0.40142286}
}
});
std::shared_ptr<Node> myDiv = Div();
myDiv->getOperator()->setDatatype(DataType::Float32);
myDiv->getOperator()->setBackend("cpu");
myDiv->getOperator()->associateInput(0, input_1);
myDiv->getOperator()->associateInput(1, input_2);
myDiv->getOperator()->computeOutputDims();
myDiv->forward();
float* resPtr = static_cast<float*>(myDiv->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("4D Tensor") {
std::shared_ptr<Tensor> input_1 = std::make_shared<Tensor>(Array4D<float,2,3,3,3> {
{
{
{{0.25675946, 0.36265653, 0.22386390},
{0.30483031, 0.97449398, 0.73871714},
{0.36169255, 0.04510212, 0.27525920}},
{{0.73255682, 0.03885978, 0.24181491},
{0.14465559, 0.86070061, 0.88848090},
{0.74408931, 0.87412918, 0.19800508}},
{{0.43551809, 0.73437816, 0.37513995},
{0.25414777, 0.06396711, 0.98708153},
{0.02140611, 0.84974837, 0.62108254}}
},
{
{{0.86227137, 0.69357753, 0.41814715},
{0.76048166, 0.46306920, 0.05907208},
{0.76625377, 0.91793799, 0.92988223}},
{{0.34362513, 0.85009813, 0.21107805},
{0.65575773, 0.38140792, 0.48540717},
{0.10045588, 0.85803932, 0.23778951}},
{{0.30316389, 0.04176688, 0.17290735},
{0.07942408, 0.48647392, 0.39440966},
{0.26543915, 0.92589515, 0.83948994}}
}
}
});
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>(Array4D<float,2,3,3,3> {
{
{
{{0.08558649, 0.12088551, 0.07462130},
{0.10161010, 0.32483134, 0.24623905},
{0.12056419, 0.01503404, 0.09175307}},
{{0.24418561, 0.01295326, 0.08060497},
{0.04821853, 0.28690019, 0.29616031},
{0.24802977, 0.29137638, 0.06600169}},
{{0.14517270, 0.24479271, 0.12504666},
{0.08471593, 0.02132237, 0.32902718},
{0.00713537, 0.28324947, 0.20702751}}
},
{
{{0.28742379, 0.23119251, 0.13938238},
{0.25349388, 0.15435641, 0.01969069},
{0.25541791, 0.30597934, 0.30996075}},
{{0.11454171, 0.28336605, 0.07035935},
{0.21858591, 0.12713598, 0.16180240},
{0.03348529, 0.28601310, 0.07926317}},
{{0.10105463, 0.01392229, 0.05763578},
{0.02647469, 0.16215797, 0.13146989},
{0.08847972, 0.30863172, 0.27982998}}
}
}
});
std::shared_ptr<Node> myDiv = Div();
myDiv->getOperator()->setDatatype(DataType::Float32);
myDiv->getOperator()->setBackend("cpu");
myDiv->getOperator()->associateInput(0, input_1);
myDiv->getOperator()->associateInput(1, input_2);
myDiv->getOperator()->computeOutputDims();
myDiv->forward();
float* resPtr = static_cast<float*>(myDiv->getOperator()->getOutput(0)->getImpl()->rawPtr());
float* expectedPtr = static_cast<float*>(expectedOutput->getImpl()->rawPtr());
for (std::size_t i = 0; i< 54; ++i) {
REQUIRE(std::abs(resPtr[i]-expectedPtr[i]) < 0.00001);
}
}
}