From 63e7f43170bf12374348921f77934959a31e9aab Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Tue, 19 Oct 2021 17:39:59 +0900 Subject: [PATCH] [Test] Connect v2 test to the parameterized test This patch connect v2 test to the model param test for convenience + add validateFor_v2. This is separated out because label dimension is not given, although it should be merged to validateFor **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- test/unittest/models/models_golden_test.cpp | 44 ++++++++++------ test/unittest/models/models_golden_test.h | 58 +++++++++++++++++---- 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/test/unittest/models/models_golden_test.cpp b/test/unittest/models/models_golden_test.cpp index 3efca692..9c1193dd 100644 --- a/test/unittest/models/models_golden_test.cpp +++ b/test/unittest/models/models_golden_test.cpp @@ -17,6 +17,24 @@ #include #include +void nntrainerModelTest::compare(bool opt) { + GraphWatcher g(createModel(), opt); + if (options & (ModelTestOption::COMPARE_V2)) { + g.compareFor_V2(getGoldenName_V2()); + } else { + g.compareFor(getGoldenName(), getLabelDim(), getIteration()); + } +} + +void nntrainerModelTest::validate(bool opt) { + GraphWatcher g(createModel(), opt); + if (options & (ModelTestOption::COMPARE_V2)) { + g.validateFor_V2(); + } else { + g.validateFor(getLabelDim()); + } +} + /** * @brief check given ini is failing/suceeding at unoptimized running */ @@ -26,9 +44,7 @@ TEST_P(nntrainerModelTest, model_test) { return; } /** Check model with all optimizations off */ - - GraphWatcher g_unopt(createModel(), false); - g_unopt.compareFor(getGoldenName(), getLabelDim(), getIteration()); + compare(false); /// add stub test for tcm EXPECT_TRUE(true); @@ -44,8 +60,7 @@ TEST_P(nntrainerModelTest, model_test_optimized) { } /** Check model with all optimizations on */ - GraphWatcher g_opt(createModel(), true); - g_opt.compareFor(getGoldenName(), getLabelDim(), getIteration()); + compare(true); /// add stub test for tcm EXPECT_TRUE(true); @@ -55,10 +70,7 @@ TEST_P(nntrainerModelTest, model_test_optimized) { * @brief check given ini is failing/suceeding at validation */ TEST_P(nntrainerModelTest, model_test_validate) { - /** Check model with all optimizations on */ - GraphWatcher g_opt(createModel(), true); - g_opt.validateFor(getLabelDim()); - + validate(true); /// add stub test for tcm EXPECT_TRUE(true); } @@ -80,12 +92,7 @@ TEST_P(nntrainerModelTest, model_test_save_load_compare) { EXPECT_NO_THROW( nn->save(saved_ini_name, ml::train::ModelFormat::MODEL_FORMAT_INI)); - GraphWatcher g(saved_ini_name, false); - g.compareFor(getGoldenName(), getLabelDim(), getIteration()); - if (remove(saved_ini_name.c_str())) { - std::cerr << "remove ini " << saved_ini_name - << "failed, reason: " << strerror(errno); - } + compare(false); } TEST_P(nntrainerModelTest, model_test_save_load_verify) { @@ -137,3 +144,10 @@ mkModelTc(std::function()> generator, return ModelGoldenTestParamType(generator, name, label_dim, iteration, options); } + +ModelGoldenTestParamType mkModelTc_V2( + std::function()> generator, + const std::string &name, ModelTestOption options) { + /** iteration and label_dim is not used */ + return ModelGoldenTestParamType(generator, name, DIM_UNUSED, 1, options); +} diff --git a/test/unittest/models/models_golden_test.h b/test/unittest/models/models_golden_test.h index 4493d0e0..3ce98d73 100644 --- a/test/unittest/models/models_golden_test.h +++ b/test/unittest/models/models_golden_test.h @@ -21,6 +21,7 @@ #include #include +inline constexpr const char *DIM_UNUSED = "1:1:1"; namespace nntrainer { class NeuralNetwork; } @@ -33,6 +34,8 @@ typedef enum { COMPARE = 1 << 0, /**< Set this to compare the numbers */ SAVE_AND_LOAD_INI = 1 << 1, /**< Set this to check if saving and constructing a new model works okay (without weights) */ + COMPARE_V2 = 1 << 2, /**< compare with v2 model format */ + NO_THROW_RUN = 0, /**< no comparison, only validate execution without throw */ ALL = COMPARE | SAVE_AND_LOAD_INI /**< Set every option */ } ModelTestOption; @@ -108,15 +111,6 @@ protected: */ std::string getName() { return name; } - /** - * @brief Get the Golden Name object - * - * @return std::string - */ - std::string getGoldenName() { - return name.substr(0, name.find("__")) + ".info"; - } - /** * @brief Get the number of iteration * @@ -137,7 +131,7 @@ protected: * @return bool true if test should be done */ bool shouldCompare() { - return (options & ModelTestOption::COMPARE) == ModelTestOption::COMPARE; + return options & (ModelTestOption::COMPARE | ModelTestOption::COMPARE_V2); } /** @@ -149,7 +143,39 @@ protected: return options & ModelTestOption::SAVE_AND_LOAD_INI; } + /** + * @brief compare for the value + * + * @param opt set if compare while optimized + */ + void compare(bool opt); + + /** + * @brief validate for the value + * + * @param opt set if validate while optimized + */ + void validate(bool opt); + private: + /** + * @brief Get the Golden Name object + * + * @return std::string + */ + std::string getGoldenName() { + return name.substr(0, name.find("__")) + ".info"; + } + + /** + * @brief Get the GoldenName V2 object + * + * @return std::string + */ + std::string getGoldenName_V2() { + return name.substr(0, name.find("__")) + ".nnmodelgolden"; + } + std::function()> nn_creator; nntrainer::TensorDim label_dim; int iteration; @@ -186,4 +212,16 @@ mkModelTc(std::function()> generator, const unsigned int iteration, ModelTestOption options = ModelTestOption::ALL); +/** + * @brief helper function to generate tcs + * + * @param generator generator + * @param name name + * @param options options + * @return ModelGoldenTestParamType + */ +ModelGoldenTestParamType mkModelTc_V2( + std::function()> generator, + const std::string &name, ModelTestOption options = ModelTestOption::ALL); + #endif // __MODELS_GOLDEN_TEST_H__ -- 2.34.1