[ini] Prepare model properties
authorJihoon Lee <jhoon.it.lee@samsung.com>
Sat, 28 Aug 2021 03:15:37 +0000 (12:15 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 7 Sep 2021 10:13:50 +0000 (19:13 +0900)
This patch prepare model properties in `model_common_properties`

**Additional Changes proposed in this PR:**
- Add PositiveIntegerProperty base class
- Fix copy assignment/ctor in base_properties

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
jni/Android.mk
nntrainer/models/meson.build
nntrainer/models/model_common_properties.cpp [new file with mode: 0644]
nntrainer/models/model_common_properties.h [new file with mode: 0644]
nntrainer/utils/base_properties.cpp
nntrainer/utils/base_properties.h

index e182465..d35c020 100644 (file)
@@ -124,6 +124,7 @@ include $(CLEAR_VARS)
 
 NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/models/model_loader.cpp \
+                  $(NNTRAINER_ROOT)/nntrainer/models/model_common_properties.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/models/dynamic_training_optimization.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/dataset/iteration_queue.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/dataset/databuffer.cpp \
index 13d3d5e..1579afd 100644 (file)
@@ -1,7 +1,8 @@
 model_sources = [
   'model_loader.cpp',
   'neuralnet.cpp',
-  'dynamic_training_optimization.cpp'
+  'model_common_properties.cpp',
+  'dynamic_training_optimization.cpp',
 ]
 
 model_headers = []
diff --git a/nntrainer/models/model_common_properties.cpp b/nntrainer/models/model_common_properties.cpp
new file mode 100644 (file)
index 0000000..d0be97f
--- /dev/null
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file   model_common_properties.cpp
+ * @date   27 Aug 2021
+ * @brief  This file contains common properties for model
+ * @see    https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug    No known bugs except for NYI items
+ *
+ */
+#include <model_common_properties.h>
+
+#include <nntrainer_log.h>
+#include <util_func.h>
+
+namespace nntrainer::props {
+Epochs::Epochs(unsigned int value) { set(value); }
+
+bool LossType::isValid(const std::string &value) const {
+  ml_logw("Model loss property is deprecated, use loss layer directly instead");
+  return istrequal(value, "cross") || istrequal(value, "mse");
+}
+
+BatchSize::BatchSize(unsigned int value) { set(value); }
+
+ContinueTrain::ContinueTrain(bool value) { set(value); }
+
+} // namespace nntrainer::props
diff --git a/nntrainer/models/model_common_properties.h b/nntrainer/models/model_common_properties.h
new file mode 100644 (file)
index 0000000..fc5faa5
--- /dev/null
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file   model_common_properties.h
+ * @date   27 Aug 2021
+ * @brief  This file contains common properties for model
+ * @see    https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug    No known bugs except for NYI items
+ *
+ */
+#ifndef __MODEL_COMMON_PROPERTIES_H__
+#define __MODEL_COMMON_PROPERTIES_H__
+
+#include <base_properties.h>
+
+#ifdef __cplusplus
+namespace nntrainer::props {
+
+/**
+ * @brief model epoch property
+ *
+ */
+class Epochs : public PositiveIntegerProperty {
+public:
+  static constexpr const char *key = "epochs"; /**< unique key to access */
+  using prop_tag = uint_prop_tag;              /**< property type */
+  /**
+   * @brief Construct a new Epochs object
+   *
+   * @param value value to set
+   */
+  Epochs(unsigned int value = 1);
+};
+
+/**
+ * @brief model loss property (deprecated)
+ *
+ */
+class LossType : public Property<std::string> {
+public:
+  static constexpr const char *key = "loss"; /**< unique key to access */
+  using prop_tag = str_prop_tag;             /**< property type */
+
+  /**
+   * @brief check if valid
+   *
+   * @param value value to check
+   * @return bool true if valid
+   */
+  bool isValid(const std::string &value) const override;
+};
+
+/**
+ * @brief model save path property
+ *
+ */
+class SavePath : public Property<std::string> {
+public:
+  static constexpr const char *key = "save_path"; /**< unique key to access */
+  using prop_tag = str_prop_tag;                  /**< property type */
+};
+
+/**
+ * @brief model batch size property
+ *
+ */
+class BatchSize : public PositiveIntegerProperty {
+public:
+  static constexpr const char *key = "batch_size"; /**< unique key to access */
+  using prop_tag = uint_prop_tag;                  /**< property type */
+
+  /**
+   * @brief Construct a new Batch Size object
+   *
+   * @param value value to set, defaults to 1
+   */
+  BatchSize(unsigned int value = 1);
+};
+
+/**
+ * @brief model continue property
+ *
+ */
+class ContinueTrain : public Property<bool> {
+public:
+  static constexpr const char *key =
+    "continue_train";             /**< unique key to access */
+  using prop_tag = bool_prop_tag; /**< property type */
+
+  /**
+   * @brief Constructor
+   *
+   * @param value value to set, defaults to false
+   */
+  ContinueTrain(bool value = false);
+};
+
+} // namespace nntrainer::props
+
+#endif
+
+#endif // __MODEL_COMMON_PROPERTIES_H__
index 885145b..dc616f3 100644 (file)
@@ -18,6 +18,9 @@
 #include <vector>
 
 namespace nntrainer {
+bool PositiveIntegerProperty::isValid(const unsigned int &value) const {
+  return value > 0;
+}
 
 template <>
 std::string
index 259f712..4c00c32 100644 (file)
@@ -149,7 +149,7 @@ public:
    * @param rhs right side to copy from
    */
   Property(const Property &rhs) {
-    if (this != &rhs) {
+    if (this != &rhs && rhs.value) {
       value = std::make_unique<T>(*rhs.value);
     }
   }
@@ -161,7 +161,7 @@ public:
    * @return Property& this
    */
   Property &operator=(const Property &rhs) {
-    if (this != &rhs) {
+    if (this != &rhs && rhs.value) {
       value = std::make_unique<T>(*rhs.value);
     }
     return *this;
@@ -255,6 +255,26 @@ private:
 };
 
 /**
+ * @brief abstract class for positive integer
+ *
+ */
+class PositiveIntegerProperty : public Property<unsigned int> {
+public:
+  /**
+   * @brief Destroy the Positive Integer Property object
+   *
+   */
+  virtual ~PositiveIntegerProperty() = default;
+
+  /**
+   * @brief isValid override, check if value > 0
+   *
+   * @param value value to check
+   * @retval true if value > 0
+   */
+  virtual bool isValid(const unsigned int &value) const override;
+};
+/**
  * @brief meta function to cast tag to it's base
  * @code below is the test spec for the cast
  *