Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Parichay Kapoor <pk.kapoor@samsung.com>
4 : *
5 : * @file optimizer.h
6 : * @date 14 October 2020
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Jijoong Moon <jijoong.moon@samsung.com>
9 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : * @brief This is optimizers interface for c++ API
12 : *
13 : * @note This is experimental API and not stable.
14 : */
15 :
16 : #ifndef __ML_TRAIN_OPTIMIZER_H__
17 : #define __ML_TRAIN_OPTIMIZER_H__
18 :
19 : #if __cplusplus >= MIN_CPP_VERSION
20 :
21 : #include <string>
22 : #include <vector>
23 :
24 : #include <common.h>
25 :
26 : namespace ml {
27 : namespace train {
28 :
29 : /** forward declaration */
30 : class LearningRateScheduler;
31 :
32 : /**
33 : * @brief Enumeration of optimizer type
34 : */
35 : enum OptimizerType {
36 : ADAM = ML_TRAIN_OPTIMIZER_TYPE_ADAM, /** adam */
37 : ADAMW = ML_TRAIN_OPTIMIZER_TYPE_ADAMW, /** AdamW */
38 : SGD = ML_TRAIN_OPTIMIZER_TYPE_SGD, /** sgd */
39 : UNKNOWN = ML_TRAIN_OPTIMIZER_TYPE_UNKNOWN /** unknown */
40 : };
41 :
42 : /**
43 : * @class Optimizer Base class for optimizers
44 : * @brief Base class for all optimizers
45 : */
46 : class Optimizer {
47 : public:
48 : /**
49 : * @brief Destructor of Optimizer Class
50 : */
51 : virtual ~Optimizer() = default;
52 :
53 : /**
54 : * @brief get Optimizer Type
55 : * @retval Optimizer type
56 : */
57 : virtual const std::string getType() const = 0;
58 :
59 : /**
60 : * @brief Default allowed properties
61 : * Available for all optimizers
62 : * - learning_rate : float
63 : *
64 : * Available for SGD and Adam optimizers
65 : * - decay_rate : float,
66 : * - decay_steps : float,
67 : *
68 : * Available for Adam optimizer
69 : * - beta1 : float,
70 : * - beta2 : float,
71 : * - epsilon : float,
72 : */
73 :
74 : /**
75 : * @brief set Optimizer Parameters
76 : * @param[in] values Optimizer Parameter list
77 : * @details This function accepts vector of properties in the format -
78 : * { std::string property_name, void * property_val, ...}
79 : */
80 : virtual void setProperty(const std::vector<std::string> &values) = 0;
81 :
82 : /**
83 : * @brief Set the Learning Rate Scheduler object
84 : *
85 : * @param lrs the learning rate scheduler object
86 : */
87 : virtual int setLearningRateScheduler(
88 : std::shared_ptr<ml::train::LearningRateScheduler> lrs) = 0;
89 : };
90 :
91 : /**
92 : * @brief Factory creator with constructor for optimizer
93 : */
94 : std::unique_ptr<Optimizer>
95 : createOptimizer(const std::string &type,
96 : const std::vector<std::string> &properties = {});
97 :
98 : /**
99 : * @brief Factory creator with constructor for optimizer
100 : */
101 : std::unique_ptr<Optimizer>
102 : createOptimizer(const OptimizerType &type,
103 : const std::vector<std::string> &properties = {});
104 :
105 : /**
106 : * @brief General Optimizer Factory function to register optimizer
107 : *
108 : * @param props property representation
109 : * @return std::unique_ptr<ml::train::Optimizer> created object
110 : */
111 : template <typename T,
112 : std::enable_if_t<std::is_base_of<Optimizer, T>::value, T> * = nullptr>
113 : std::unique_ptr<Optimizer>
114 : createOptimizer(const std::vector<std::string> &props = {}) {
115 : std::unique_ptr<Optimizer> ptr = std::make_unique<T>();
116 :
117 : ptr->setProperty(props);
118 : return ptr;
119 : }
120 :
121 : namespace optimizer {
122 :
123 : /**
124 : * @brief Helper function to create adam optimizer
125 : */
126 : inline std::unique_ptr<Optimizer>
127 : Adam(const std::vector<std::string> &properties = {}) {
128 5 : return createOptimizer(OptimizerType::ADAM, properties);
129 : }
130 :
131 : /**
132 : * @brief Helper function to create sgd optimizer
133 : */
134 : inline std::unique_ptr<Optimizer>
135 : SGD(const std::vector<std::string> &properties = {}) {
136 3 : return createOptimizer(OptimizerType::SGD, properties);
137 : }
138 :
139 : /**
140 : * @brief Helper function to create AdamW Optimizer
141 : */
142 : inline std::unique_ptr<Optimizer>
143 : AdamW(const std::vector<std::string> &properties = {}) {
144 1 : return createOptimizer(OptimizerType::ADAMW, properties);
145 : }
146 :
147 : } // namespace optimizer
148 :
149 : /**
150 : * @brief Enumeration of learning rate scheduler type
151 : */
152 : enum LearningRateSchedulerType {
153 : CONSTANT = ML_TRAIN_LR_SCHEDULER_TYPE_CONSTANT, /**< constant */
154 : EXPONENTIAL =
155 : ML_TRAIN_LR_SCHEDULER_TYPE_EXPONENTIAL, /**< exponentially decay */
156 : STEP = ML_TRAIN_LR_SCHEDULER_TYPE_STEP, /**< step wise decay */
157 : COSINE = ML_TRAIN_LR_SCHEDULER_TYPE_COSINE /**< cosine annealing */
158 : };
159 :
160 : /**
161 : * @class Learning Rate Schedulers Base class
162 : * @brief Base class for all Learning Rate Schedulers
163 : */
164 : class LearningRateScheduler {
165 :
166 : public:
167 : /**
168 : * @brief Destructor of learning rate scheduler Class
169 : */
170 : virtual ~LearningRateScheduler() = default;
171 :
172 : /**
173 : * @brief Default allowed properties
174 : * Constant Learning rate scheduler
175 : * - learning_rate : float
176 : *
177 : * Exponential Learning rate scheduler
178 : * - learning_rate : float
179 : * - decay_rate : float,
180 : * - decay_steps : float,
181 : *
182 : * Step Learning rate scheduler
183 : * - learing_rate : float, float, ...
184 : * - iteration : uint, uint, ...
185 : *
186 : * more to be added
187 : */
188 :
189 : /**
190 : * @brief set learning rate scheduler properties
191 : * @param[in] values learning rate scheduler properties list
192 : * @details This function accepts vector of properties in the format -
193 : * { std::string property_name = std::string property_val, ...}
194 : */
195 : virtual void setProperty(const std::vector<std::string> &values) = 0;
196 :
197 : /**
198 : * @brief get learning rate scheduler Type
199 : * @retval learning rate scheduler type
200 : */
201 : virtual const std::string getType() const = 0;
202 : };
203 :
204 : /**
205 : * @brief Factory creator with constructor for learning rate scheduler type
206 : */
207 : std::unique_ptr<ml::train::LearningRateScheduler>
208 : createLearningRateScheduler(const LearningRateSchedulerType &type,
209 : const std::vector<std::string> &properties = {});
210 :
211 : /**
212 : * @brief Factory creator with constructor for learning rate scheduler
213 : */
214 : std::unique_ptr<ml::train::LearningRateScheduler>
215 : createLearningRateScheduler(const std::string &type,
216 : const std::vector<std::string> &properties = {});
217 :
218 : /**
219 : * @brief General LR Scheduler Factory function to create LR Scheduler
220 : *
221 : * @param props property representation
222 : * @return std::unique_ptr<nntrainer::LearningRateScheduler> created object
223 : */
224 : template <typename T,
225 : std::enable_if_t<std::is_base_of<LearningRateScheduler, T>::value, T>
226 : * = nullptr>
227 : std::unique_ptr<LearningRateScheduler>
228 74 : createLearningRateScheduler(const std::vector<std::string> &props = {}) {
229 74 : std::unique_ptr<LearningRateScheduler> ptr = std::make_unique<T>();
230 74 : ptr->setProperty(props);
231 74 : return ptr;
232 : }
233 :
234 : namespace optimizer {
235 : namespace learning_rate {
236 :
237 : /**
238 : * @brief Helper function to create constant learning rate scheduler
239 : */
240 : inline std::unique_ptr<LearningRateScheduler>
241 : Constant(const std::vector<std::string> &properties = {}) {
242 : return createLearningRateScheduler(LearningRateSchedulerType::CONSTANT,
243 : properties);
244 : }
245 :
246 : /**
247 : * @brief Helper function to create exponential learning rate scheduler
248 : */
249 : inline std::unique_ptr<LearningRateScheduler>
250 : Exponential(const std::vector<std::string> &properties = {}) {
251 2 : return createLearningRateScheduler(LearningRateSchedulerType::EXPONENTIAL,
252 2 : properties);
253 : }
254 :
255 : /**
256 : * @brief Helper function to create step learning rate scheduler
257 : */
258 : inline std::unique_ptr<LearningRateScheduler>
259 : Step(const std::vector<std::string> &properties = {}) {
260 : return createLearningRateScheduler(LearningRateSchedulerType::STEP,
261 : properties);
262 : }
263 :
264 : /**
265 : * @brief Helper function to create cosine learning rate scheduler
266 : */
267 : inline std::unique_ptr<LearningRateScheduler>
268 : Cosine(const std::vector<std::string> &properties = {}) {
269 : return createLearningRateScheduler(LearningRateSchedulerType::COSINE,
270 : properties);
271 : }
272 :
273 : } // namespace learning_rate
274 : } // namespace optimizer
275 :
276 : } // namespace train
277 : } // namespace ml
278 :
279 : #else
280 : #error "CPP versions c++17 or over are only supported"
281 : #endif // __cpluscplus
282 : #endif // __ML_TRAIN_OPTIMIZER_H__
|