diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0fd922..8e29004 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,6 +26,6 @@ jobs: - name: Run tests working-directory: ${{github.workspace}} run: | - ./build/tools/run_tests + ./build/tools/run_tests -s ./build/tools/benchmodel ./example_models/wavenet.nam ./build/tools/benchmodel ./example_models/lstm.nam diff --git a/.gitmodules b/.gitmodules index 11c1984..0db1526 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "Dependencies/eigen"] path = Dependencies/eigen url = https://gitlab.com/libeigen/eigen +[submodule "Dependencies/catch2"] + path = Dependencies/catch2 + url = https://github.com/catchorg/Catch2 diff --git a/Dependencies/catch2 b/Dependencies/catch2 new file mode 160000 index 0000000..a1faad9 --- /dev/null +++ b/Dependencies/catch2 @@ -0,0 +1 @@ +Subproject commit a1faad9315ece8e7146e9d2263ceb3d42ea0619a diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 1e09436..89c1423 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,7 +1,10 @@ file(GLOB_RECURSE NAM_SOURCES ../NAM/*.cpp ../NAM/*.c ../NAM*.h) +file(GLOB TEST_SOURCES test/*.cpp) -# TODO: add loadmodel and run_tests to TOOLS? -set(TOOLS benchmodel) +add_subdirectory(${NAM_DEPS_PATH}/catch2 Catch2) + +# TODO: add loadmodel to TOOLS? +set(TOOLS benchmodel run_tests) add_custom_target(tools ALL DEPENDS ${TOOLS}) @@ -12,11 +15,15 @@ include_directories(tools ${NAM_DEPS_PATH}/nlohmann) add_executable(loadmodel loadmodel.cpp ${NAM_SOURCES}) add_executable(benchmodel benchmodel.cpp ${NAM_SOURCES}) -add_executable(run_tests run_tests.cpp ${NAM_SOURCES}) +add_executable(run_tests run_tests.cpp ${NAM_SOURCES} ${TEST_SOURCES}) +target_link_libraries(run_tests PRIVATE Catch2::Catch2WithMain) source_group(NAM ${CMAKE_CURRENT_SOURCE_DIR} FILES ${NAM_SOURCES}) -target_compile_features(${TOOLS} PUBLIC cxx_std_20) +foreach(TOOL IN LISTS TOOLS) + target_compile_features(${TOOL} PUBLIC cxx_std_20) +endforeach() + set_target_properties(${TOOLS} PROPERTIES @@ -26,20 +33,26 @@ set_target_properties(${TOOLS} ) if (CMAKE_SYSTEM_NAME STREQUAL "Windows") - target_compile_definitions(${TOOLS} PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN) + foreach(TOOL IN LISTS TOOLS) + target_compile_definitions(${TOOL} PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN) + endforeach() endif() if (MSVC) - target_compile_options(${TOOLS} PRIVATE - "$<$:/W4>" - "$<$:/O2>" - ) + foreach(TOOL IN LISTS TOOLS) + target_compile_options(${TOOL} PRIVATE + "$<$:/W4>" + "$<$:/O2>" + ) + endforeach() else() - target_compile_options(${TOOLS} PRIVATE - -Wall -Wextra -Wpedantic -Wstrict-aliasing -Wunreachable-code -Weffc++ -Wno-unused-parameter - "$<$:-Og;-ggdb;-Werror>" - "$<$:-Ofast>" - ) + foreach(TOOL IN LISTS TOOLS) + target_compile_options(${TOOL} PRIVATE + -Wall -Wextra -Wpedantic -Wstrict-aliasing -Wunreachable-code -Weffc++ -Wno-unused-parameter + "$<$:-Og;-ggdb;-Werror>" + "$<$:-Ofast>" + ) + endforeach() endif() # There's an error in eigen's diff --git a/tools/run_tests.cpp b/tools/run_tests.cpp index bee341a..9881655 100644 --- a/tools/run_tests.cpp +++ b/tools/run_tests.cpp @@ -1,40 +1,2 @@ -// Entry point for tests -// See the GitHub Action for a demo how to build and run tests. - -#include -#include "test/test_activations.cpp" -#include "test/test_dsp.cpp" -#include "test/test_get_dsp.cpp" -#include "test/test_wavenet.cpp" - -int main() -{ - std::cout << "Running tests..." << std::endl; - // TODO Automatically loop, catch exceptions, log results - - test_activations::TestFastTanh::test_core_function(); - test_activations::TestFastTanh::test_get_by_init(); - test_activations::TestFastTanh::test_get_by_str(); - - test_activations::TestLeakyReLU::test_core_function(); - test_activations::TestLeakyReLU::test_get_by_init(); - test_activations::TestLeakyReLU::test_get_by_str(); - - test_dsp::test_construct(); - test_dsp::test_get_input_level(); - test_dsp::test_get_output_level(); - test_dsp::test_has_input_level(); - test_dsp::test_has_output_level(); - test_dsp::test_set_input_level(); - test_dsp::test_set_output_level(); - - test_get_dsp::test_gets_input_level(); - test_get_dsp::test_gets_output_level(); - test_get_dsp::test_null_input_level(); - test_get_dsp::test_null_output_level(); - - test_wavenet::test_gated(); - - std::cout << "Success!" << std::endl; - return 0; -} \ No newline at end of file +#define CATCH_CONFIG_MAIN +#include diff --git a/tools/test/test_activations.cpp b/tools/test/test_activations.cpp index 75c472b..b4ce374 100644 --- a/tools/test/test_activations.cpp +++ b/tools/test/test_activations.cpp @@ -1,122 +1,89 @@ -// Tests for activation functions -// -// Things you want ot test for: -// 1. That the core elementwise funciton is snapshot-correct. -// 2. The class that wraps the core function for an array of data -// 3. .cpp: that you have the singleton defined, and that it's in the unordered map to get by string - -#include #include #include +#include +#include #include "NAM/activations.h" -namespace test_activations +TEST_CASE("Test fast_tanh core function", "[activations]") { -// TODO get nonzero cases -class TestFastTanh + REQUIRE(nam::activations::fast_tanh(0.0f) == 0.0f); + // The original test had these commented out. + // REQUIRE(nam::activations::fast_tanh(1.0f) == Catch::Approx(0.98f)); + // REQUIRE(nam::activations::fast_tanh(-1.0f) == Catch::Approx(-0.98f)); +} + +void _test_fast_tanh_class(nam::activations::Activation* a) { -public: - static void test_core_function() - { - auto TestCase = [](float input, float expectedOutput) { - float actualOutput = nam::activations::fast_tanh(input); - assert(actualOutput == expectedOutput); - }; - // A few snapshot tests - TestCase(0.0f, 0.0f); - // TestCase(1.0f, 1.0f); - // TestCase(-1.0f, -0.01f); - }; - - static void test_get_by_init() - { - auto a = nam::activations::ActivationLeakyReLU(); - _test_class(&a); - } + std::vector inputs, expectedOutputs; + + inputs.push_back(0.0f); + expectedOutputs.push_back(0.0f); + + // inputs.push_back(1.0f); + // expectedOutputs.push_back(1.0f); - // Get the singleton and test it - static void test_get_by_str() + // inputs.push_back(-1.0f); + // expectedOutputs.push_back(-0.01f); + + a->apply(inputs.data(), (long)inputs.size()); + for (size_t i = 0; i < inputs.size(); ++i) { - const std::string name = "Fasttanh"; - auto a = nam::activations::Activation::get_activation(name); - _test_class(a); + REQUIRE(inputs[i] == Catch::Approx(expectedOutputs[i])); } +} -private: - // Put the class through its paces - static void _test_class(nam::activations::Activation* a) - { - std::vector inputs, expectedOutputs; +TEST_CASE("Test FastTanh get by init", "[activations]") +{ + auto a = nam::activations::ActivationFastTanh(); + _test_fast_tanh_class(&a); +} - inputs.push_back(0.0f); - expectedOutputs.push_back(0.0f); +TEST_CASE("Test FastTanh get by str", "[activations]") +{ + const std::string name = "Fasttanh"; + auto a = nam::activations::Activation::get_activation(name); + REQUIRE(a != nullptr); + _test_fast_tanh_class(a); +} - // inputs.push_back(1.0f); - // expectedOutputs.push_back(1.0f); +TEST_CASE("Test LeakyReLU core function", "[activations]") +{ + REQUIRE(nam::activations::leaky_relu(0.0f) == 0.0f); + REQUIRE(nam::activations::leaky_relu(1.0f) == 1.0f); + REQUIRE(nam::activations::leaky_relu(-1.0f) == -0.01f); +} - // inputs.push_back(-1.0f); - // expectedOutputs.push_back(-0.01f); +void _test_leaky_relu_class(nam::activations::Activation* a) +{ + std::vector inputs, expectedOutputs; - a->apply(inputs.data(), (long)inputs.size()); - for (auto itActual = inputs.begin(), itExpected = expectedOutputs.begin(); itActual != inputs.end(); - ++itActual, ++itExpected) - { - assert(*itActual == *itExpected); - } - }; -}; + inputs.push_back(0.0f); + expectedOutputs.push_back(0.0f); -class TestLeakyReLU -{ -public: - static void test_core_function() - { - auto TestCase = [](float input, float expectedOutput) { - float actualOutput = nam::activations::leaky_relu(input); - assert(actualOutput == expectedOutput); - }; - // A few snapshot tests - TestCase(0.0f, 0.0f); - TestCase(1.0f, 1.0f); - TestCase(-1.0f, -0.01f); - }; - - static void test_get_by_init() - { - auto a = nam::activations::ActivationLeakyReLU(); - _test_class(&a); - } + inputs.push_back(1.0f); + expectedOutputs.push_back(1.0f); - // Get the singleton and test it - static void test_get_by_str() + inputs.push_back(-1.0f); + expectedOutputs.push_back(-0.01f); + + a->apply(inputs.data(), (long)inputs.size()); + for (size_t i = 0; i < inputs.size(); ++i) { - const std::string name = "LeakyReLU"; - auto a = nam::activations::Activation::get_activation(name); - _test_class(a); + REQUIRE(inputs[i] == Catch::Approx(expectedOutputs[i])); } +} -private: - // Put the class through its paces - static void _test_class(nam::activations::Activation* a) - { - std::vector inputs, expectedOutputs; - - inputs.push_back(0.0f); - expectedOutputs.push_back(0.0f); - - inputs.push_back(1.0f); - expectedOutputs.push_back(1.0f); - - inputs.push_back(-1.0f); - expectedOutputs.push_back(-0.01f); - - a->apply(inputs.data(), (long)inputs.size()); - for (auto itActual = inputs.begin(), itExpected = expectedOutputs.begin(); itActual != inputs.end(); - ++itActual, ++itExpected) - { - assert(*itActual == *itExpected); - } - }; -}; -}; // namespace test_activations +TEST_CASE("Test LeakyReLU get by init", "[activations]") +{ + auto a = nam::activations::ActivationLeakyReLU(); + _test_leaky_relu_class(&a); +} + +TEST_CASE("Test LeakyReLU get by str", "[activations]") +{ + const std::string name = "LeakyReLU"; + auto a = nam::activations::Activation::get_activation(name); + REQUIRE(a != nullptr); + _test_leaky_relu_class(a); +} \ No newline at end of file diff --git a/tools/test/test_dsp.cpp b/tools/test/test_dsp.cpp index bbdee63..e76c1cf 100644 --- a/tools/test/test_dsp.cpp +++ b/tools/test/test_dsp.cpp @@ -1,66 +1,60 @@ -// Tests for dsp +#include #include "NAM/dsp.h" -namespace test_dsp -{ -// Simplest test: can I construct something! -void test_construct() +TEST_CASE("DSP can be constructed", "[dsp]") { nam::DSP myDsp(48000.0); } -void test_get_input_level() +TEST_CASE("DSP GetInputLevel returns the value set by SetInputLevel", "[dsp]") { nam::DSP myDsp(48000.0); const double expected = 19.0; myDsp.SetInputLevel(expected); - assert(myDsp.HasInputLevel()); + REQUIRE(myDsp.HasInputLevel()); const double actual = myDsp.GetInputLevel(); - assert(actual == expected); + REQUIRE(actual == expected); } -void test_get_output_level() +TEST_CASE("DSP GetOutputLevel returns the value set by SetOutputLevel", "[dsp]") { nam::DSP myDsp(48000.0); const double expected = 12.0; myDsp.SetOutputLevel(expected); - assert(myDsp.HasOutputLevel()); + REQUIRE(myDsp.HasOutputLevel()); const double actual = myDsp.GetOutputLevel(); - assert(actual == expected); + REQUIRE(actual == expected); } -// Test correct function of DSP::HasInputLevel() -void test_has_input_level() +TEST_CASE("DSP HasInputLevel is true after SetInputLevel", "[dsp]") { nam::DSP myDsp(48000.0); - assert(!myDsp.HasInputLevel()); + REQUIRE(!myDsp.HasInputLevel()); myDsp.SetInputLevel(19.0); - assert(myDsp.HasInputLevel()); + REQUIRE(myDsp.HasInputLevel()); } -void test_has_output_level() +TEST_CASE("DSP HasOutputLevel is true after SetOutputLevel", "[dsp]") { nam::DSP myDsp(48000.0); - assert(!myDsp.HasOutputLevel()); + REQUIRE(!myDsp.HasOutputLevel()); myDsp.SetOutputLevel(12.0); - assert(myDsp.HasOutputLevel()); + REQUIRE(myDsp.HasOutputLevel()); } -// Test correct function of DSP::HasInputLevel() -void test_set_input_level() +TEST_CASE("DSP SetInputLevel does not crash", "[dsp]") { nam::DSP myDsp(48000.0); myDsp.SetInputLevel(19.0); } -void test_set_output_level() +TEST_CASE("DSP SetOutputLevel does not crash", "[dsp]") { nam::DSP myDsp(48000.0); myDsp.SetOutputLevel(19.0); -} -}; // namespace test_dsp +} \ No newline at end of file diff --git a/tools/test/test_get_dsp.cpp b/tools/test/test_get_dsp.cpp index b9f2d10..792bd2a 100644 --- a/tools/test/test_get_dsp.cpp +++ b/tools/test/test_get_dsp.cpp @@ -1,14 +1,12 @@ -#include #include #include #include +#include #include "json.hpp" #include "NAM/get_dsp.h" -namespace test_get_dsp -{ // Config const std::string basicConfigStr = R"({"version": "0.5.4", "metadata": {"date": {"year": 2024, "month": 10, "day": 9, "hour": 18, "minute": 44, "second": 41}, "loudness": -37.8406867980957, "gain": 0.13508800804658277, "name": "Test LSTM", "modeled_by": "Steve", "gear_type": "amp", "gear_make": "Darkglass Electronics", "gear_model": "Microtubes 900 v2", "tone_type": "clean", "input_level_dbu": 18.3, "output_level_dbu": 12.3, "training": {"settings": {"ignore_checks": false}, "data": {"latency": {"manual": null, "calibration": {"algorithm_version": 1, "delays": [-16], "safety_factor": 1, "recommended": -17, "warnings": {"matches_lookahead": false, "disagreement_too_high": false}}}, "checks": {"version": 3, "passed": true}}, "validation_esr": null}}, "architecture": "LSTM", "config": {"input_size": 1, "hidden_size": 3, "num_layers": 1}, "weights": [-0.21677088737487793, -0.6683622002601624, -0.2560940980911255, -0.3588429093360901, 0.17952610552310944, 0.19445613026618958, -0.01662646047770977, 0.5353694558143616, -0.2536540627479553, -0.5132213234901428, -0.020476307719945908, 0.08592455089092255, -0.6891753673553467, 0.3627359867095947, 0.008421811275184155, 0.3113192617893219, 0.14251480996608734, 0.07989779114723206, -0.18211324512958527, 0.7118963003158569, 0.41084015369415283, -0.6571938395500183, -0.13214066624641418, -0.2698603868484497, 0.49387243390083313, -0.3491725027561188, 0.6353667974472046, -0.5005152225494385, 0.2052856683731079, -0.4301638901233673, -0.15770092606544495, -0.7181791067123413, 0.056290093809366226, -0.49049463868141174, 0.6623441576957703, 0.09029324352741241, 0.34005245566368103, 0.16416560113430023, 0.15520110726356506, -0.4155678153038025, -0.36928507685661316, 0.3211132884025574, -0.6769840121269226, -0.1575538069009781, 0.05268515646457672, -0.4191459119319916, 0.599330484867096, 0.21518059074878693, -4.246325492858887, -3.315647840499878, -4.328850746154785, 4.496089458465576, 5.015639305114746, 3.6492037773132324, 0.14431169629096985, -0.6633821725845337, 0.11673200130462646, -0.1418764889240265, -0.4897872805595398, -0.8689419031143188, -0.06714004278182983, -0.4450395107269287, -0.02142983116209507, -0.15136894583702087, -2.775207996368408, -0.08681213855743408, 0.05702732503414154, 0.670292317867279, 0.31442636251449585, 0.30793967843055725], "sample_rate": 48000})"; @@ -19,7 +17,7 @@ std::vector GetWeights(nlohmann::json const& j) auto it = j.find("weights"); if (it != j.end()) { - return *it; + return it->get>(); } else { @@ -42,20 +40,21 @@ nam::dspData _GetConfig(const std::string& configStr = basicConfigStr) return returnedConfig; } -void test_gets_input_level() +TEST_CASE("get_dsp gets input level", "[get_dsp]") { nam::dspData config = _GetConfig(); std::unique_ptr dsp = get_dsp(config); - assert(dsp->HasInputLevel()); + REQUIRE(dsp->HasInputLevel()); } -void test_gets_output_level() + +TEST_CASE("get_dsp gets output level", "[get_dsp]") { nam::dspData config = _GetConfig(); std::unique_ptr dsp = get_dsp(config); - assert(dsp->HasOutputLevel()); + REQUIRE(dsp->HasOutputLevel()); } -void test_null_input_level() +TEST_CASE("get_dsp handles null input level", "[get_dsp]") { // Issue 129 const std::string configStr = @@ -64,11 +63,11 @@ void test_null_input_level() // The first part of this is that the following line doesn't fail: std::unique_ptr dsp = get_dsp(config); - assert(!dsp->HasInputLevel()); - assert(dsp->HasOutputLevel()); + REQUIRE(!dsp->HasInputLevel()); + REQUIRE(dsp->HasOutputLevel()); } -void test_null_output_level() +TEST_CASE("get_dsp handles null output level", "[get_dsp]") { // Issue 129 const std::string configStr = @@ -76,7 +75,6 @@ void test_null_output_level() nam::dspData config = _GetConfig(configStr); // The first part of this is that the following line doesn't fail: std::unique_ptr dsp = get_dsp(config); - assert(dsp->HasInputLevel()); - assert(!dsp->HasOutputLevel()); + REQUIRE(dsp->HasInputLevel()); + REQUIRE(!dsp->HasOutputLevel()); } -}; // namespace test_get_dsp \ No newline at end of file diff --git a/tools/test/test_wavenet.cpp b/tools/test/test_wavenet.cpp index 87390cd..bdf6b61 100644 --- a/tools/test/test_wavenet.cpp +++ b/tools/test/test_wavenet.cpp @@ -1,14 +1,11 @@ -// Tests for the WaveNet - #include -#include #include +#include +#include #include "NAM/wavenet.h" -namespace test_wavenet -{ -void test_gated() +TEST_CASE("Test WaveNet gated activation", "[wavenet]") { // Assert correct nuemrics of the gating activation. // Issue 101 @@ -33,7 +30,7 @@ void test_gated() 1.0f, 0.0f}; auto it = weights.begin(); layer.set_weights_(it); - assert(it == weights.end()); + REQUIRE(it == weights.end()); const long numFrames = 4; layer.SetMaxBufferSize(numFrames); @@ -69,8 +66,7 @@ void test_gated() const float actualOutput = output(0, i); const float actualHeadInput = headInput(0, i); // std::cout << actualOutput << std::endl; - assert(actualOutput == expectedOutput); - assert(actualHeadInput == expectedHeadInput); + REQUIRE(actualOutput == Catch::Approx(expectedOutput)); + REQUIRE(actualHeadInput == Catch::Approx(expectedHeadInput)); } } -}; // namespace test_wavenet \ No newline at end of file