From 0a473686fcefa6859ba1b2bd382ae11ebf406420 Mon Sep 17 00:00:00 2001 From: Christophe Guillon <christophe.guillon@inria.fr> Date: Wed, 10 Jul 2024 11:03:17 +0200 Subject: [PATCH] [UnitTest] Make unit tests deterministic with fixed seed Use the getSeed() method provided by Catch to initialize the random number generator instead of using the default random device. This allows to make the tests deterministic when --rng-seed <seed> option is passed. Also this allows to report a test failure by specifying the seed as reported in the test output, which can then be reproduced with --rng-seed <reported_seed>. --- unit_tests/data/Test_Tensor.cpp | 9 +++++---- unit_tests/graph/Test_GraphView.cpp | 7 ++++--- unit_tests/operator/Test_Div_Op.cpp | 5 +++-- unit_tests/operator/Test_GlobalAveragePooling_Op.cpp | 5 +++-- unit_tests/operator/Test_MatMul_Op.cpp | 6 ++++-- unit_tests/operator/Test_Mul_Op.cpp | 5 +++-- unit_tests/operator/Test_Pow_Op.cpp | 5 +++-- unit_tests/operator/Test_Sub_Op.cpp | 5 +++-- unit_tests/scheduler/Test_Scheduler.cpp | 3 ++- 9 files changed, 30 insertions(+), 20 deletions(-) diff --git a/unit_tests/data/Test_Tensor.cpp b/unit_tests/data/Test_Tensor.cpp index 98d3193ff..a536f113f 100644 --- a/unit_tests/data/Test_Tensor.cpp +++ b/unit_tests/data/Test_Tensor.cpp @@ -14,13 +14,14 @@ #include <cstdint> // std::uint8_t, std::uint16_t, std::int32_t #include <numeric> // std::accumulate, std::inner_product #include <functional> // std::multiplies -#include <random> // std::random_device, std::mt19937, +#include <random> // std::mt19937, // std::uniform_int_distribution, std::uniform_real_distribution #include <set> #include <string> #include <vector> #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include "aidge/backend/cpu/data/TensorImpl.hpp" #include "aidge/data/Data.hpp" @@ -127,7 +128,7 @@ TEST_CASE("[core/data] Tensor(Construction)", "[Tensor][Constructor]") { constexpr std::uint16_t NBTRIALS = 10; // Create random number generators - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); @@ -169,7 +170,7 @@ TEST_CASE("[core/data] Tensor(getter/setter)", "[Tensor][Getter][Setter]") { constexpr std::uint16_t NBTRIALS = 10; // Create random number generators - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); @@ -261,7 +262,7 @@ TEST_CASE("[core/data] Tensor(other)", "[Tensor][extract][zeros][print]") { constexpr std::uint16_t NBTRIALS = 10; // Create random number generators - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); diff --git a/unit_tests/graph/Test_GraphView.cpp b/unit_tests/graph/Test_GraphView.cpp index 8e9f5a27e..d9289c4aa 100644 --- a/unit_tests/graph/Test_GraphView.cpp +++ b/unit_tests/graph/Test_GraphView.cpp @@ -17,6 +17,7 @@ #include <string> #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include "aidge/backend/OperatorImpl.hpp" #include "aidge/data/Tensor.hpp" @@ -35,7 +36,7 @@ TEST_CASE("genRandomGraph", "[GraphView][randomGen]") { size_t nbUnicity = 0; for (int test = 0; test < nbTests; ++test) { - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; const std::mt19937::result_type seed(rd()); RandomGraph randGraph; @@ -81,7 +82,7 @@ TEST_CASE("clone", "[GraphView][clone]") { const size_t nbTests = 100; for (int test = 0; test < nbTests; ++test) { - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; const std::mt19937::result_type seed(rd()); RandomGraph randGraph; @@ -155,7 +156,7 @@ TEST_CASE("remove", "[GraphView][remove]") { size_t nbTested = 0; for (int test = 0; test < nbTests; ++test) { - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; const std::mt19937::result_type seed(rd()); RandomGraph randGraph; diff --git a/unit_tests/operator/Test_Div_Op.cpp b/unit_tests/operator/Test_Div_Op.cpp index cef7bc53e..d35edec17 100644 --- a/unit_tests/operator/Test_Div_Op.cpp +++ b/unit_tests/operator/Test_Div_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -24,7 +25,7 @@ TEST_CASE("[core/operator] Div_Op(forwardDims)", "[Div][forwardDims]") { constexpr std::uint16_t NBTRIALS = 10; // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); diff --git a/unit_tests/operator/Test_GlobalAveragePooling_Op.cpp b/unit_tests/operator/Test_GlobalAveragePooling_Op.cpp index 1d99fc7a5..15c714b63 100644 --- a/unit_tests/operator/Test_GlobalAveragePooling_Op.cpp +++ b/unit_tests/operator/Test_GlobalAveragePooling_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -25,7 +26,7 @@ TEST_CASE("[core/operator] GlobalAveragePooling_Op(forwardDims)", "[GlobalAveragePooling][forwardDims]") { constexpr std::uint16_t NB_TRIALS = 10; // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> inf3DimsDistribution(1, 2); diff --git a/unit_tests/operator/Test_MatMul_Op.cpp b/unit_tests/operator/Test_MatMul_Op.cpp index 102a4ab4e..876c1ac76 100644 --- a/unit_tests/operator/Test_MatMul_Op.cpp +++ b/unit_tests/operator/Test_MatMul_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -22,10 +23,11 @@ namespace Aidge { TEST_CASE("[core/operator] MatMul_Op(forwardDims)", "[MatMul][forwardDims]") { // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dist(1, 10); + std::cerr << "Test case start, random " << dist(gen) << " " << rd() << std::endl; // Create MatMul Operator std::shared_ptr<Node> myMatMul = MatMul(); auto op = std::static_pointer_cast<OperatorTensor>(myMatMul -> getOperator()); diff --git a/unit_tests/operator/Test_Mul_Op.cpp b/unit_tests/operator/Test_Mul_Op.cpp index 8efd1c2dc..bee90d725 100644 --- a/unit_tests/operator/Test_Mul_Op.cpp +++ b/unit_tests/operator/Test_Mul_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -24,7 +25,7 @@ TEST_CASE("[core/operator] Mul_Op(forwardDims)", "[Mul][forwardDims]") { constexpr std::uint16_t NBTRIALS = 10; // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); diff --git a/unit_tests/operator/Test_Pow_Op.cpp b/unit_tests/operator/Test_Pow_Op.cpp index 90b865c1d..274f7c00b 100644 --- a/unit_tests/operator/Test_Pow_Op.cpp +++ b/unit_tests/operator/Test_Pow_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -24,7 +25,7 @@ TEST_CASE("[core/operator] Pow_Op(forwardDims)", "[Pow][forwardDims]") { constexpr std::uint16_t NBTRIALS = 10; // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); diff --git a/unit_tests/operator/Test_Sub_Op.cpp b/unit_tests/operator/Test_Sub_Op.cpp index 0797def12..110cbbfe6 100644 --- a/unit_tests/operator/Test_Sub_Op.cpp +++ b/unit_tests/operator/Test_Sub_Op.cpp @@ -10,9 +10,10 @@ ********************************************************************************/ #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include <cstddef> // std::size_t #include <memory> -#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution +#include <random> // std::mt19937, std::uniform_int_distribution #include <vector> #include "aidge/data/Tensor.hpp" @@ -24,7 +25,7 @@ TEST_CASE("[core/operator] Sub_Op(forwardDims)", "[Sub][forwardDims]") { constexpr std::uint16_t NBTRIALS = 10; // Create a random number generator - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; std::mt19937 gen(rd()); std::uniform_int_distribution<std::size_t> dimsDist(1, 10); std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5); diff --git a/unit_tests/scheduler/Test_Scheduler.cpp b/unit_tests/scheduler/Test_Scheduler.cpp index ceaa5e301..3c3026ff0 100644 --- a/unit_tests/scheduler/Test_Scheduler.cpp +++ b/unit_tests/scheduler/Test_Scheduler.cpp @@ -17,6 +17,7 @@ #include <string> #include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators_random.hpp> #include "aidge/backend/OperatorImpl.hpp" #include "aidge/data/Tensor.hpp" @@ -35,7 +36,7 @@ TEST_CASE("randomScheduling", "[Scheduler][randomGen]") { std::uniform_int_distribution<std::size_t> nb_nodes_dist(100, 500); for (int test = 0; test < nbTests; ++test) { - std::random_device rd; + auto rd = Catch::Generators::Detail::getSeed; const std::mt19937::result_type seed(rd()); std::mt19937 gen(rd()); -- GitLab