From f2897036336a3564df1303579e33829565a8d88d Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 00:26:58 +0100 Subject: [PATCH 1/7] Add unit tests on Clang to CI --- .github/workflows/unit_tests_clang_linux.yml | 29 +++++++++++++++++++ ...sts_linux.yml => unit_tests_gcc_linux.yml} | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/unit_tests_clang_linux.yml rename .github/workflows/{unit_tests_linux.yml => unit_tests_gcc_linux.yml} (95%) diff --git a/.github/workflows/unit_tests_clang_linux.yml b/.github/workflows/unit_tests_clang_linux.yml new file mode 100644 index 00000000..af3a6933 --- /dev/null +++ b/.github/workflows/unit_tests_clang_linux.yml @@ -0,0 +1,29 @@ +name: Unit tests Clang Linux + +on: [push] + +jobs: + unit-tests: + name: Build and run + runs-on: ubuntu-latest + + steps: + - name: Set up Clang + uses: egor-tensin/setup-clang@v1 + with: + version: 19 + platform: x64 + - name: Checkout the project + uses: actions/checkout@v2 + - name: CMake + run: | + cd ./build + CXX=/usr/bin/clang-19 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release + - name: Make + run: | + cd ./build + make + - name: Execute unit tests + run: | + cd ./build + ./tests/unit_tests/UnitTests --gtest_also_run_disabled_tests \ No newline at end of file diff --git a/.github/workflows/unit_tests_linux.yml b/.github/workflows/unit_tests_gcc_linux.yml similarity index 95% rename from .github/workflows/unit_tests_linux.yml rename to .github/workflows/unit_tests_gcc_linux.yml index 4cb4ed2d..bfd5f6d0 100644 --- a/.github/workflows/unit_tests_linux.yml +++ b/.github/workflows/unit_tests_gcc_linux.yml @@ -1,4 +1,4 @@ -name: Unit tests Linux +name: Unit tests GCC Linux on: [push] From bb67f2bd0afcf8e81acb8288c3bad0e13ed025c7 Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 00:40:50 +0100 Subject: [PATCH 2/7] Fix clang compilation --- CMakeLists.txt | 5 ++++- src/neural_network/layer/MaxPooling2D.hpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4d422c6..449b0943 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,12 @@ if(MSVC) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-s-x64-1_80.lib") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-gd-x64-1_80.lib") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-sgd-x64-1_80.lib") +elseif (UNIX AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + message("-- CMake run for clang") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libstdc++ -lc++abi") else() set(CMAKE_CXX_FLAGS_DEBUG "-g") - set(CMAKE_CXX_FLAGS_RELEASE "-Ofast") + set(CMAKE_CXX_FLAGS_RELEASE "-O3 -ffast-math") endif() add_subdirectory(src) diff --git a/src/neural_network/layer/MaxPooling2D.hpp b/src/neural_network/layer/MaxPooling2D.hpp index 2c362412..bd95447a 100644 --- a/src/neural_network/layer/MaxPooling2D.hpp +++ b/src/neural_network/layer/MaxPooling2D.hpp @@ -19,7 +19,7 @@ namespace snn::internal [[nodiscard]] std::vector computeBackOutput(std::vector& inputErrors) override; [[nodiscard]] std::vector computeOutput(const std::vector& inputs, bool temporalReset) override; - void computeTrain(std::vector& [[maybe_unused]] inputErrors) override {} + void computeTrain([[maybe_unused]] std::vector& inputErrors) override {} void buildKernelIndexes() override; public: From e07cb4a7a3cc1a77f39230bffd8ce12141ab44af Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 23:10:44 +0100 Subject: [PATCH 3/7] Fix CI pipelines --- .github/workflows/unit_tests_clang_linux.yml | 9 ++------- .github/workflows/unit_tests_gcc_linux.yml | 2 +- CMakeLists.txt | 3 --- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/.github/workflows/unit_tests_clang_linux.yml b/.github/workflows/unit_tests_clang_linux.yml index af3a6933..e9e4697b 100644 --- a/.github/workflows/unit_tests_clang_linux.yml +++ b/.github/workflows/unit_tests_clang_linux.yml @@ -5,20 +5,15 @@ on: [push] jobs: unit-tests: name: Build and run - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - - name: Set up Clang - uses: egor-tensin/setup-clang@v1 - with: - version: 19 - platform: x64 - name: Checkout the project uses: actions/checkout@v2 - name: CMake run: | cd ./build - CXX=/usr/bin/clang-19 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release + CC=/usr/bin/clang-18 CXX=/usr/bin/clang++-18 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release - name: Make run: | cd ./build diff --git a/.github/workflows/unit_tests_gcc_linux.yml b/.github/workflows/unit_tests_gcc_linux.yml index bfd5f6d0..72db186f 100644 --- a/.github/workflows/unit_tests_gcc_linux.yml +++ b/.github/workflows/unit_tests_gcc_linux.yml @@ -18,7 +18,7 @@ jobs: - name: CMake run: | cd ./build - CXX=/usr/bin/g++-13 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release + CC=/usr/bin/gcc-13 CXX=/usr/bin/g++-13 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release - name: Make run: | cd ./build diff --git a/CMakeLists.txt b/CMakeLists.txt index 449b0943..a286f6d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,6 @@ if(MSVC) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-s-x64-1_80.lib") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-gd-x64-1_80.lib") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libboost_serialization-vc143-mt-sgd-x64-1_80.lib") -elseif (UNIX AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - message("-- CMake run for clang") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libstdc++ -lc++abi") else() set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -ffast-math") From 722afdd8e1811c69176654b84072c7901a38b859 Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 23:39:01 +0100 Subject: [PATCH 4/7] Cannot NaN due to compilation options --- src/data/Data.cpp | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/data/Data.cpp b/src/data/Data.cpp index 9248c9a5..599fb0e4 100644 --- a/src/data/Data.cpp +++ b/src/data/Data.cpp @@ -268,26 +268,6 @@ void Data::unshuffle() int Data::isValid() { - for (auto& input : this->sets[training].inputs) - { - for (auto& value : input) - { - if (isnan(value)) - { - return 401; - } - } - } - for (auto& input : this->sets[testing].inputs) - { - for (auto& value : input) - { - if (isnan(value)) - { - return 401; - } - } - } if (!this->sets[testing].shuffledIndexes.empty() && this->sets[training].size != (int)this->sets[training].shuffledIndexes.size()) return 403; From 862ba19447db0fa611d34dcf0abab0897f1ec6a1 Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 23:40:22 +0100 Subject: [PATCH 5/7] Update CI to GCC 14 --- .github/workflows/unit_tests_gcc_linux.yml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/unit_tests_gcc_linux.yml b/.github/workflows/unit_tests_gcc_linux.yml index 72db186f..0365fcc4 100644 --- a/.github/workflows/unit_tests_gcc_linux.yml +++ b/.github/workflows/unit_tests_gcc_linux.yml @@ -5,20 +5,15 @@ on: [push] jobs: unit-tests: name: Build and run - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - - name: Set up GCC - uses: egor-tensin/setup-gcc@v1.3 - with: - version: 13 - platform: x64 - name: Checkout the project uses: actions/checkout@v2 - name: CMake run: | cd ./build - CC=/usr/bin/gcc-13 CXX=/usr/bin/g++-13 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release + CC=/usr/bin/gcc-14 CXX=/usr/bin/g++-14 cmake -G"Unix Makefiles" ./.. -DCMAKE_BUILD_TYPE=Release - name: Make run: | cd ./build From 01d1b92de020d7a66a806bd43324181284a27cdc Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Mon, 4 Nov 2024 23:43:14 +0100 Subject: [PATCH 6/7] Fix clang compilation --- CMakeLists.txt | 2 +- src/neural_network/NeuralNetwork.cpp | 5 +---- src/neural_network/layer/Layer.tpp | 2 +- src/neural_network/layer/MaxPooling1D.hpp | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a286f6d2..2efd4de9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,5 +63,5 @@ if(MSVC) remove_target_compile_options(DatasetTests /W3) else() target_compile_options(StraightforwardNeuralNetwork PUBLIC -fopenmp -Wall -Wextra -pedantic -Werror -Wno-unused-parameter) - target_compile_options(Boost PRIVATE -w) + target_compile_options(Boost INTERFACE -w) endif() \ No newline at end of file diff --git a/src/neural_network/NeuralNetwork.cpp b/src/neural_network/NeuralNetwork.cpp index 7bff81c0..43d8e71d 100644 --- a/src/neural_network/NeuralNetwork.cpp +++ b/src/neural_network/NeuralNetwork.cpp @@ -102,10 +102,7 @@ vector NeuralNetwork::calculateError(const vector& outputs, const vector errors(this->layers.back()->getNumberOfNeurons(), 0); for (size_t n = 0; n < errors.size(); ++n) { - if (isnan(desired[n])) - errors[n] = 0; - else - errors[n] = 2 * (desired[n] - outputs[n]); + errors[n] = 2 * (desired[n] - outputs[n]); } return errors; } diff --git a/src/neural_network/layer/Layer.tpp b/src/neural_network/layer/Layer.tpp index 26030727..80982977 100644 --- a/src/neural_network/layer/Layer.tpp +++ b/src/neural_network/layer/Layer.tpp @@ -85,7 +85,7 @@ float Layer::getAverageOfAbsNeuronWeights() const auto sum = 0.0f; for (auto& n : this->neurons) for (auto w : n.getWeights()) - sum += abs(w); + sum += std::abs(w); sum /= static_cast(this->neurons.size()); return sum; } diff --git a/src/neural_network/layer/MaxPooling1D.hpp b/src/neural_network/layer/MaxPooling1D.hpp index 48b1ce7b..dc0bfe4a 100644 --- a/src/neural_network/layer/MaxPooling1D.hpp +++ b/src/neural_network/layer/MaxPooling1D.hpp @@ -19,7 +19,7 @@ namespace snn::internal [[nodiscard]] std::vector computeBackOutput(std::vector& inputErrors) override; [[nodiscard]] std::vector computeOutput(const std::vector& inputs, bool temporalReset) override; - void computeTrain(std::vector & [[maybe_unused]] inputErrors) override {} + void computeTrain([[maybe_unused]] std::vector & inputErrors) override {} void buildKernelIndexes() override; public: From 315418149383b50e512e4ace38e868b16c62afdf Mon Sep 17 00:00:00 2001 From: Matthieu HERNANDEZ Date: Tue, 5 Nov 2024 00:41:06 +0100 Subject: [PATCH 7/7] Improve a test --- tests/unit_tests/IdentityTests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit_tests/IdentityTests.cpp b/tests/unit_tests/IdentityTests.cpp index 542d0d3e..6d4d57d0 100644 --- a/tests/unit_tests/IdentityTests.cpp +++ b/tests/unit_tests/IdentityTests.cpp @@ -12,7 +12,7 @@ TEST(Identity, WorksWithSmallNumbers) Data data(problem::regression, inputData, expectedOutputs); - StraightforwardNeuralNetwork neuralNetwork({Input(1), FullyConnected(4), FullyConnected(1, snn::activation::identity)}, + StraightforwardNeuralNetwork neuralNetwork({Input(1), FullyConnected(6), FullyConnected(1, snn::activation::identity)}, StochasticGradientDescent(0.02f, 0.99f)); neuralNetwork.train(data, 0.01_mae || 2_s);