diff --git a/nntrainer/models/model_common_properties.cpp b/nntrainer/models/model_common_properties.cpp index 7599fd3a85..b9589c67f6 100644 --- a/nntrainer/models/model_common_properties.cpp +++ b/nntrainer/models/model_common_properties.cpp @@ -44,4 +44,9 @@ ModelTensorDataType::ModelTensorDataType(ModelTensorDataTypeInfo::Enum value) { } LossScale::LossScale(float value) { set(value); } +bool LossScale::isValid(const float &value) const { + ml_loge("Loss scale cannot be 0"); + return value != 0; +} + } // namespace nntrainer::props diff --git a/nntrainer/models/model_common_properties.h b/nntrainer/models/model_common_properties.h index 0c259f2aa6..8b5d539762 100644 --- a/nntrainer/models/model_common_properties.h +++ b/nntrainer/models/model_common_properties.h @@ -238,6 +238,14 @@ class LossScale : public Property { LossScale(float value = 1.0f); static constexpr const char *key = "loss_scale"; /**< unique key to access */ using prop_tag = float_prop_tag; /**< property type */ + + /** + * @brief check if valid + * + * @param value value to check + * @return bool true if valid + */ + bool isValid(const float &value) const override; }; } // namespace nntrainer::props diff --git a/test/unittest/integration_tests/integration_test_mixed_precision.cpp b/test/unittest/integration_tests/integration_test_mixed_precision.cpp index 92fcc999c1..a84a442c86 100644 --- a/test/unittest/integration_tests/integration_test_mixed_precision.cpp +++ b/test/unittest/integration_tests/integration_test_mixed_precision.cpp @@ -23,6 +23,11 @@ #include using namespace nntrainer; +/** + * Supported Layer list + * [InputLayer, BatchNormalizationLayer, FullyConnectedLayer, Conv1DLayer] + */ + TEST(mixed_precision, input_only_model_test) { std::unique_ptr nn = @@ -44,6 +49,32 @@ TEST(mixed_precision, input_only_model_test) { EXPECT_EQ(nn->reinitialize(), ML_ERROR_NONE); } +TEST(mixed_precision, conv1d_model_test) { + + std::unique_ptr nn = + ml::train::createModel(ml::train::ModelType::NEURAL_NET, {"loss=mse"}); + nn->setProperty( + {"batch_size=1", "model_tensor_type=FP16-FP16", "loss_scale=65536"}); + + auto graph = makeGraph({ + {"input", {"name=in", "input_shape=3:1:3"}}, + {"conv1d", {"name=conv1d_0", "filters=1", "kernel_size=3"}}, + {"conv1d", {"name=conv1d_1", "filters=1", "kernel_size=1"}}, + {"batch_normalization", {"name=bn_0", "epsilon=0.00001", "momentum=0.9"}}, + {"batch_normalization", {"name=bn_1", "epsilon=0.00001", "momentum=0.9"}}, + }); + + for (auto &node : graph) { + nn->addLayer(node); + } + + nn->setOptimizer(ml::train::createOptimizer("adam", {"learning_rate = 0.1"})); + + EXPECT_EQ(nn->compile(), ML_ERROR_NONE); + EXPECT_EQ(nn->initialize(), ML_ERROR_NONE); + EXPECT_EQ(nn->reinitialize(), ML_ERROR_NONE); +} + TEST(mixed_precision, loss_scale_test) { std::unique_ptr nn = ml::train::createModel(ml::train::ModelType::NEURAL_NET, {"loss=mse"}); @@ -52,10 +83,8 @@ TEST(mixed_precision, loss_scale_test) { "loss_scale=0"}), std::invalid_argument); - EXPECT_NO_THROW( - nn->setProperty( - {"batch_size=1", "model_tensor_type=FP16-FP16", "loss_scale=65536"}), - std::invalid_argument); + EXPECT_NO_THROW(nn->setProperty( + {"batch_size=1", "model_tensor_type=FP16-FP16", "loss_scale=65536"})); } TEST(mixed_precision, model_tensor_type_test) {