[optimizer] Deprecate optimizer factory
authorParichay Kapoor <pk.kapoor@samsung.com>
Mon, 26 Jul 2021 05:49:47 +0000 (14:49 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 3 Aug 2021 05:26:04 +0000 (14:26 +0900)
Deprecate usage of optimizer factory and replace it with app_context.
Further, app_context throw code update from runtime_error to
invalid_argument when the requested type of object is not found.

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
api/ccapi/src/factory.cpp
debian/nntrainer-dev.install
nntrainer/models/model_loader.cpp
nntrainer/models/neuralnet.h
nntrainer/optimizers/meson.build
nntrainer/optimizers/optimizer_factory.cpp [deleted file]
nntrainer/optimizers/optimizer_factory.h [deleted file]
packaging/nntrainer.spec
test/unittest/unittest_nntrainer_internal.cpp
test/unittest/unittest_nntrainer_modelfile.cpp

index 587a03a716c208e01b4fa46a83a808c40983281e..8b055c976db7c6daef8f04e8e60d554e430112f7 100644 (file)
@@ -22,7 +22,6 @@
 #include <neuralnet.h>
 #include <nntrainer_error.h>
 #include <optimizer.h>
-#include <optimizer_factory.h>
 
 namespace ml {
 namespace train {
@@ -44,8 +43,7 @@ std::unique_ptr<Optimizer>
 createOptimizer(const OptimizerType &type,
                 const std::vector<std::string> &properties) {
   auto &ac = nntrainer::AppContext::Global();
-  const std::string &t = nntrainer::optimizerIntToStrType(type);
-  return ac.createObject<Optimizer>(t, properties);
+  return ac.createObject<Optimizer>(type, properties);
 }
 
 /**
index 0300b0cf4740e92cb0476798711591eafe5463a5..7807d464d495c0062f6f8a4cd79e5725bed684d2 100644 (file)
@@ -19,7 +19,6 @@
 /usr/include/nntrainer/tensor.h
 /usr/include/nntrainer/optimizer_devel.h
 /usr/include/nntrainer/optimizer_impl.h
-/usr/include/nntrainer/optimizer_factory.h
 /usr/include/nntrainer/profiler.h
 /usr/include/nntrainer/dynamic_training_optimization.h
 /usr/include/nntrainer/layer_node.h
index 9031e5fa5dd07bdf40a7ec12661a5bd138c4001f..fe7b12a289a3e86df80e5eb1d1a0ec4459636846 100644 (file)
@@ -22,7 +22,6 @@
 #include <neuralnet.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
-#include <optimizer_factory.h>
 #include <parse_util.h>
 #include <sgd.h>
 #include <time_dist.h>
@@ -132,7 +131,9 @@ int ModelLoader::loadModelConfigIni(dictionary *ini, NeuralNetwork &model) {
     "Warning: create [ Optimizer ] section in ini to specify optimizers.");
 
   try {
-    model.opt = nntrainer::createOptimizer(opt_type);
+    std::shared_ptr<ml::train::Optimizer> optimizer =
+      app_context.createObject<ml::train::Optimizer>(opt_type, {});
+    model.setOptimizer(optimizer);
   } catch (std::exception &e) {
     ml_loge("%s %s", typeid(e).name(), e.what());
     return ML_ERROR_INVALID_PARAMETER;
index 631abdeb8f7c9de2badb69bd33f5571f98b25705..caa260d510eca896be695d59c5316f46013f9492 100644 (file)
@@ -402,6 +402,12 @@ public:
    */
   bool empty() const { return model_graph.empty(); }
 
+  /**
+   * @brief get the number of nodes in the model
+   * @param[out] number of nodes
+   */
+  size_t size() const { return model_graph.size(); }
+
   /**
    * @brief     get network graph
    * @retval NetowrkGraphType
index 58ca39321bbaf3b7bbff1d242de7b44d666762b7..a59ecb0978a7ba6017e6c7d25ee4af57ee7ce80b 100644 (file)
@@ -2,12 +2,10 @@ optimizer_sources = [
   'adam.cpp',
   'optimizer_devel.cpp',
   'optimizer_impl.cpp',
-  'optimizer_factory.cpp',
   'sgd.cpp'
 ]
 
 optimizer_headers = [
-  'optimizer_factory.h',
   'optimizer_devel.h',
   'optimizer_impl.h'
 ]
diff --git a/nntrainer/optimizers/optimizer_factory.cpp b/nntrainer/optimizers/optimizer_factory.cpp
deleted file mode 100644 (file)
index a59e0f7..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-/**
- * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
- *
- * @file   optimizer_factory.cpp
- * @date   7 October 2020
- * @see    https://github.com/nnstreamer/nntrainer
- * @author Parichay Kapoor <pk.kapoor@samsung.com>
- * @bug    No known bugs except for NYI items
- * @brief  This is the optimizer factory.
- */
-#include <algorithm>
-#include <sstream>
-
-#include <adam.h>
-#include <nntrainer_error.h>
-#include <optimizer_factory.h>
-#include <parse_util.h>
-#include <sgd.h>
-
-namespace nntrainer {
-
-/// helper function to convert enum to string
-/// @todo this should be integrated into appcontext
-const std::string optimizerIntToStrType(const OptType &type) {
-  switch (type) {
-  case OptType::ADAM:
-    return "adam";
-  case OptType::SGD:
-    return "sgd";
-  case OptType::UNKNOWN:
-  /// fall through intended
-  default:
-    throw exception::not_supported(
-      "[opt_integer_to_string_type] Not supported type given");
-  }
-
-  throw exception::not_supported(
-    "[opt_integer_to_string_type] Not supported type given");
-}
-/**
- * @brief Factory creator with copy constructor
- */
-std::unique_ptr<Optimizer> createOptimizer(const std::string &type,
-                                           const Optimizer &opt) {
-  /// #673: use context to create optimizer
-  if (istrequal(type, "sgd")) {
-    return std::make_unique<SGD>(static_cast<const SGD &>(opt));
-  }
-
-  if (istrequal(type, "adam")) {
-    return std::make_unique<Adam>(static_cast<const Adam &>(opt));
-  }
-
-  std::stringstream ss;
-  ss << "Unknown type for the optimizer, type: " << type;
-
-  throw std::invalid_argument(ss.str().c_str());
-}
-
-std::unique_ptr<Optimizer> createOptimizer(const OptType &type,
-                                           const Optimizer &opt) {
-  const std::string &s = optimizerIntToStrType(type);
-  return createOptimizer(s, opt);
-}
-
-/**
- * @brief Factory creator with constructor
- */
-std::unique_ptr<Optimizer> createOptimizer(const std::string &type) {
-  /// #673: use context to create optimizer
-  if (istrequal(type, "sgd")) {
-    return std::make_unique<SGD>();
-  }
-
-  if (istrequal(type, "adam")) {
-    return std::make_unique<Adam>();
-  }
-
-  std::stringstream ss;
-  ss << "Unknown type for the optimizer, type: " << type;
-
-  throw std::invalid_argument(ss.str().c_str());
-}
-
-std::unique_ptr<Optimizer> createOptimizer(const OptType &type) {
-  const std::string &actual_type = optimizerIntToStrType(type);
-  return createOptimizer(actual_type);
-}
-
-} // namespace nntrainer
diff --git a/nntrainer/optimizers/optimizer_factory.h b/nntrainer/optimizers/optimizer_factory.h
deleted file mode 100644 (file)
index a076788..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-/**
- * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
- *
- * @file   optimizer_factory.h
- * @date   7 October 2020
- * @see    https://github.com/nnstreamer/nntrainer
- * @author Parichay Kapoor <pk.kapoor@samsung.com>
- * @bug    No known bugs except for NYI items
- * @brief  This is the optimizer factory.
- */
-
-#ifndef __OPTIMIZER_FACTORY_H__
-#define __OPTIMIZER_FACTORY_H__
-#ifdef __cplusplus
-
-#include <optimizer_devel.h>
-
-namespace nntrainer {
-
-using OptType = ml::train::OptimizerType;
-
-/**
- * @brief change Optimizer Type to string
- *
- * @param type type to change
- * @return const std::string string representation of the type
- */
-const std::string optimizerIntToStrType(const OptType &type);
-/**
- * @brief Factory creator with copy constructor
- */
-std::unique_ptr<Optimizer> createOptimizer(const std::string &type,
-                                           const Optimizer &opt);
-
-/**
- * @brief Factory creator with copy constructor using enum(integer)
- */
-std::unique_ptr<Optimizer> createOptimizer(const OptType &type,
-                                           const Optimizer &opt);
-
-/**
- * @brief Factory creator with constructor
- */
-std::unique_ptr<Optimizer> createOptimizer(const std::string &type);
-
-/**
- * @brief Factory creator with constructor using enum(integer)
- */
-std::unique_ptr<Optimizer> createOptimizer(const OptType &type);
-
-} // namespace nntrainer
-
-#endif // __cplusplus
-#endif // __OPTIMIZER_FACTORY_H__
index af9e0cab082d68aff1b07e51f3b90ad38a0401ef..5208102ad10f0c7c1544621a762ce3b2ef6aeeed 100644 (file)
@@ -452,7 +452,6 @@ cp -r result %{buildroot}%{_datadir}/nntrainer/unittest/
 %{_includedir}/nntrainer/tensor.h
 %{_includedir}/nntrainer/optimizer_devel.h
 %{_includedir}/nntrainer/optimizer_impl.h
-%{_includedir}/nntrainer/optimizer_factory.h
 %{_includedir}/nntrainer/profiler.h
 %{_includedir}/nntrainer/dynamic_training_optimization.h
 %{_includedir}/nntrainer/layer_node.h
index 751ed1d3aa52ff5a71065273fc6109dc6a7fdbbf..db5e1422a255a1f609551214a5e9be6e4c63eb0c 100644 (file)
 
 #include <fstream>
 
+#include <app_context.h>
 #include <databuffer_file.h>
 #include <databuffer_func.h>
 #include <neuralnet.h>
 #include <nntrainer_error.h>
-#include <optimizer_factory.h>
+#include <optimizer.h>
 #include <util_func.h>
 
 #include <nntrainer_test_util.h>
  * @brief Optimizer create
  */
 TEST(nntrainer_Optimizer, create_01_p) {
-  std::shared_ptr<nntrainer::Optimizer> op;
-  EXPECT_NO_THROW(op = nntrainer::createOptimizer("adam"));
+  std::unique_ptr<ml::train::Optimizer> op;
+  auto &ac = nntrainer::AppContext::Global();
+  EXPECT_NO_THROW(op = ac.createObject<ml::train::Optimizer>("adam", {}));
 }
 
 /**
  * @brief Optimizer create
  */
 TEST(nntrainer_Optimizer, setType_02_p) {
-  std::shared_ptr<nntrainer::Optimizer> op;
-  EXPECT_NO_THROW(op = nntrainer::createOptimizer("sgd"));
+  std::unique_ptr<ml::train::Optimizer> op;
+  auto &ac = nntrainer::AppContext::Global();
+  EXPECT_NO_THROW(op = ac.createObject<ml::train::Optimizer>("sgd", {}));
 }
 
 /**
  * @brief Optimizer create
  */
 TEST(nntrainer_Optimizer, setType_03_n) {
-  std::shared_ptr<nntrainer::Optimizer> op;
-  EXPECT_THROW(op = nntrainer::createOptimizer("non-existing type"),
-               std::invalid_argument);
+  std::unique_ptr<ml::train::Optimizer> op;
+  auto &ac = nntrainer::AppContext::Global();
+  EXPECT_ANY_THROW(
+    op = ac.createObject<ml::train::Optimizer>("non-existing type", {}));
 }
 
 TEST(nntrainer_throw_if, throw_invalid_arg_p) {
index 2a62f1727ac0a497f7e5188a192496ac55a36c0f..f9dcf4967553c5e79a0c8844f9e52a80c1b9c1e0 100644 (file)
@@ -681,7 +681,7 @@ TEST(nntrainerIniTest, backbone_p_20) {
   EXPECT_EQ(NN.loadFromConfig(backbone.getIniName()), ML_ERROR_NONE);
   EXPECT_EQ(NN.compile(), ML_ERROR_NONE);
   EXPECT_EQ(NN.initialize(), ML_ERROR_NONE);
-  EXPECT_EQ(NN.getNetworkGraph().size(), 6u);
+  EXPECT_EQ(NN.size(), 6u);
 }
 
 /**