From cd92997f401105aefcf83cb3a6fbdecc14a1ee0d Mon Sep 17 00:00:00 2001 From: Steven Atkinson Date: Sat, 19 Oct 2024 21:48:39 -0700 Subject: [PATCH] [BUGFIX] Handle when calibration fields are present but null-valued (#130) * Fix when dBu metadata are present but null. Needs tests. * Tests --- NAM/get_dsp.cpp | 8 +++-- NAM/get_dsp.h | 2 ++ tools/run_tests.cpp | 6 ++++ tools/test/test_activations.cpp | 54 +++++++++++++++++++++++++++++++++ tools/test/test_get_dsp.cpp | 29 ++++++++++++++++-- 5 files changed, 95 insertions(+), 4 deletions(-) diff --git a/NAM/get_dsp.cpp b/NAM/get_dsp.cpp index 1155915..7205ddf 100644 --- a/NAM/get_dsp.cpp +++ b/NAM/get_dsp.cpp @@ -137,8 +137,11 @@ std::unique_ptr get_dsp(dspData& conf) auto AssignOptional = [&conf](const std::string key, OptionalValue& v) { if (conf.metadata.find(key) != conf.metadata.end()) { - v.value = conf.metadata[key]; - v.have = true; + if (!conf.metadata[key].is_null()) + { + v.value = conf.metadata[key]; + v.have = true; + } } }; @@ -205,6 +208,7 @@ std::unique_ptr get_dsp(dspData& conf) } // "pre-warm" the model to settle initial conditions + // Can this be removed now that it's part of Reset()? out->prewarm(); return out; diff --git a/NAM/get_dsp.h b/NAM/get_dsp.h index 1d2c8d5..40bbbc9 100644 --- a/NAM/get_dsp.h +++ b/NAM/get_dsp.h @@ -1,5 +1,7 @@ #include +#include "dsp.h" + namespace nam { // Get NAM from a .nam file at the provided location diff --git a/tools/run_tests.cpp b/tools/run_tests.cpp index 443a729..ca15666 100644 --- a/tools/run_tests.cpp +++ b/tools/run_tests.cpp @@ -11,6 +11,10 @@ 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(); @@ -25,6 +29,8 @@ int main() 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(); std::cout << "Success!" << std::endl; return 0; diff --git a/tools/test/test_activations.cpp b/tools/test/test_activations.cpp index e83e9f6..75c472b 100644 --- a/tools/test/test_activations.cpp +++ b/tools/test/test_activations.cpp @@ -13,6 +13,60 @@ namespace test_activations { +// TODO get nonzero cases +class TestFastTanh +{ +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); + } + + // Get the singleton and test it + static void test_get_by_str() + { + const std::string name = "Fasttanh"; + auto a = nam::activations::Activation::get_activation(name); + _test_class(a); + } + +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); + } + }; +}; + class TestLeakyReLU { public: diff --git a/tools/test/test_get_dsp.cpp b/tools/test/test_get_dsp.cpp index 7695ba0..b9f2d10 100644 --- a/tools/test/test_get_dsp.cpp +++ b/tools/test/test_get_dsp.cpp @@ -10,7 +10,7 @@ namespace test_get_dsp { // Config -const std::string configStr = +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})"; // Copied over but shouldn't be publicly-exposed. @@ -27,7 +27,7 @@ std::vector GetWeights(nlohmann::json const& j) } } -nam::dspData _GetConfig() +nam::dspData _GetConfig(const std::string& configStr = basicConfigStr) { nlohmann::json j = nlohmann::json::parse(configStr); @@ -54,4 +54,29 @@ void test_gets_output_level() std::unique_ptr dsp = get_dsp(config); assert(dsp->HasOutputLevel()); } + +void test_null_input_level() +{ + // Issue 129 + const std::string configStr = + 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": null, "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})"; + 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()); +} + +void test_null_output_level() +{ + // Issue 129 + const std::string configStr = + 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": 19.0, "output_level_dbu": null, "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})"; + 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()); +} }; // namespace test_get_dsp \ No newline at end of file