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>
#include <neuralnet.h>
#include <nntrainer_error.h>
#include <optimizer.h>
-#include <optimizer_factory.h>
namespace ml {
namespace train {
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);
}
/**
/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
#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>
"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;
*/
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
'adam.cpp',
'optimizer_devel.cpp',
'optimizer_impl.cpp',
- 'optimizer_factory.cpp',
'sgd.cpp'
]
optimizer_headers = [
- 'optimizer_factory.h',
'optimizer_devel.h',
'optimizer_impl.h'
]
+++ /dev/null
-// 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
+++ /dev/null
-// 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__
%{_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
#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) {
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);
}
/**