Skip to content
Snippets Groups Projects
Commit 6262f0d2 authored by Houssem ROUIS's avatar Houssem ROUIS
Browse files

add forwardDims unit tests for ArgMax

parent e7fde4c3
No related branches found
No related tags found
2 merge requests!93Release v0.3.0,!75Learning backend cuda
......@@ -11,6 +11,8 @@
#include <catch2/catch_test_macros.hpp>
#include <memory>
#include <numeric> // std::accumulate
#include <random> // std::random_device, std::mt19937, std::uniform_real_distribution
#include "aidge/data/Tensor.hpp"
#include "aidge/operator/ArgMax.hpp"
......@@ -22,6 +24,82 @@
using namespace Aidge;
TEST_CASE("[cpu/operator] ArgMax(forward)", "[ArgMax][CPU]") {
SECTION("ForwardDims")
{
constexpr std::uint16_t NBTRIALS = 10;
// Create a random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> valueDist(0.1f, 1.1f); // Random float distribution between 0 and 1
std::uniform_int_distribution<std::size_t> dimSizeDist(std::size_t(2), std::size_t(10));
std::uniform_int_distribution<std::size_t> nbDimsDist(std::size_t(1), std::size_t(5));
std::uniform_int_distribution<int> boolDist(0,1);
SECTION("KeepDims") {
for (std::uint16_t trial = 0; trial < NBTRIALS; ++trial) {
DimSize_t nbDims = nbDimsDist(gen);
std::vector<DimSize_t> dims(nbDims);
std::vector<DimSize_t> expectedOutDims(nbDims);
std::uniform_int_distribution<std::int32_t> axisDist(std::int32_t(0), std::int32_t(nbDims-1));
std::int32_t axis = axisDist(gen);
for (std::size_t i = 0; i < nbDims; i++) {
dims[i] = dimSizeDist(gen);
if (i == axis) {
expectedOutDims[i] = 1;
}
else {
expectedOutDims[i] = dims[i];
}
}
std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(dims);
myInput->setBackend("cpu");
myInput->setDataType(DataType::Float32);
myInput->zeros();
std::shared_ptr<Node> myArgMax = ArgMax(axis);
auto op = std::static_pointer_cast<OperatorTensor>(myArgMax -> getOperator());
op->associateInput(0,myInput);
op->setDataType(DataType::Float32);
op->setBackend("cpu");
op->forwardDims();
const auto outputDims = op->getOutput(0)->dims();
REQUIRE(outputDims == expectedOutDims);
}
}
SECTION("Not KeepDims") {
for (std::uint16_t trial = 0; trial < NBTRIALS; ++trial) {
DimSize_t nbDims = nbDimsDist(gen);
std::vector<DimSize_t> dims(nbDims);
std::vector<DimSize_t> expectedOutDims;
std::uniform_int_distribution<std::int32_t> axisDist(std::int32_t(0), std::int32_t(nbDims-1));
std::int32_t axis = axisDist(gen);
for (std::size_t i = 0; i < nbDims; i++) {
dims[i] = dimSizeDist(gen);
if(i != axis) {
expectedOutDims.push_back(dims[i]);
}
}
if(expectedOutDims.empty()) {
expectedOutDims.push_back(1);
}
std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(dims);
myInput->setBackend("cpu");
myInput->setDataType(DataType::Float32);
std::shared_ptr<Node> myArgMax = ArgMax(axis, false);
auto op = std::static_pointer_cast<OperatorTensor>(myArgMax -> getOperator());
op->associateInput(0,myInput);
op->setDataType(DataType::Float32);
op->setBackend("cpu");
op->forwardDims();
const auto outputDims = op->getOutput(0)->dims();
REQUIRE(outputDims == expectedOutDims);
}
}
}
SECTION("3D Tensor") {
std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array3D<float,2,3,4> {
{
......
......@@ -310,8 +310,7 @@ TEST_CASE("[cpu/operator] ReduceMean(forward)", "[ReduceMean][CPU]") {
op->setDataType(DataType::Float32);
op->setBackend("cpu");
myReduceMean->forward();
op->getOutput(0)->print();
// approxEq<float>(*(op->getOutput(0)), *myOutput);
REQUIRE(approxEq<float>(*(op->getOutput(0)), *myOutput));
}
SECTION("noop_with_empty_axes") {
......
......@@ -11,6 +11,8 @@
#include <catch2/catch_test_macros.hpp>
#include <memory>
#include <numeric> // std::accumulate
#include <random> // std::random_device, std::mt19937, std::uniform_real_distribution
#include "aidge/data/Tensor.hpp"
#include "aidge/operator/ReduceSum.hpp"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment