Line data Source code
1 : /**
2 : * Copyright (C) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : * Unless required by applicable law or agreed to in writing, software
9 : * distributed under the License is distributed on an "AS IS" BASIS,
10 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 : * See the License for the specific language governing permissions and
12 : * limitations under the License.
13 : *
14 : *
15 : * @file neuralnet.h
16 : * @date 04 December 2019
17 : * @brief This is Neural Network Class
18 : * @see https://github.com/nnstreamer/nntrainer
19 : * @author Jijoong Moon <jijoong.moon@samsung.com>
20 : * @bug No known bugs except for NYI items
21 : *
22 : */
23 : #ifndef __NEURALNET_H__
24 : #define __NEURALNET_H__
25 : #ifdef __cplusplus
26 :
27 : #include <array>
28 : #include <map>
29 : #include <memory>
30 : #include <tuple>
31 : #include <vector>
32 : #ifdef PROFILE
33 : #include <chrono>
34 : #endif
35 :
36 : #include <common_properties.h>
37 : #include <compiler_fwd.h>
38 : #include <dynamic_training_optimization.h>
39 : #include <engine.h>
40 : #include <layer_node.h>
41 : #include <model_common_properties.h>
42 : #include <network_graph.h>
43 : #include <optimizer_wrapped.h>
44 : #include <tensor.h>
45 :
46 : #include <model.h>
47 : #include <nntrainer-api-common.h>
48 : #include <nntrainer_error.h>
49 : #include <node_exporter.h>
50 :
51 : namespace ml::train {
52 : class DataSet;
53 : enum class DatasetType;
54 : enum class DatasetModeType;
55 : enum class ExecutionMode;
56 : } // namespace ml::train
57 :
58 : namespace nntrainer {
59 :
60 : class Exporter;
61 :
62 : /**
63 : * @brief Enumeration of Network Type
64 : */
65 : using NetType = ml::train::ModelType;
66 : using ExecutionMode = ml::train::ExecutionMode;
67 :
68 : class DataBuffer;
69 : using DatasetType = ml::train::DatasetType;
70 : using DatasetModeType = ml::train::DatasetModeType;
71 : using RunStats = ml::train::RunStats;
72 :
73 : /**
74 : * @class NeuralNetwork Class
75 : * @brief NeuralNetwork Class which has Network Configuration & Layers
76 : */
77 : class NeuralNetwork : public ml::train::Model {
78 : friend class ModelLoader; /** access private members of ModelLoader */
79 :
80 : public:
81 : using NodeType = std::shared_ptr<LayerNode>; /** Type of a Node */
82 : using GraphType = std::vector<NodeType>; /** actual graph type */
83 : using FlatGraphType =
84 : std::vector<NodeType>; /** topological sorted, iterable 1-D list of nodes */
85 : using NetworkGraphType = nntrainer::NetworkGraph;
86 :
87 : /**
88 : * @brief Constructor of NeuralNetwork Class
89 : */
90 : NeuralNetwork();
91 :
92 : /**
93 : * @brief Constructor of NeuralNetwork Class
94 : */
95 : NeuralNetwork(const Engine *ct_engine_);
96 :
97 : /**
98 : * @brief Destructor of NeuralNetwork Class
99 : */
100 : ~NeuralNetwork();
101 :
102 : /**
103 : * @brief Get Loss from the previous ran batch of data
104 : * @retval loss value
105 : */
106 : float getLoss() override;
107 :
108 : /**
109 : * @brief returns compilation state of a network
110 : * @retval initialized value
111 : */
112 0 : bool getCompiled() const override { return compiled; }
113 :
114 : /**
115 : * @brief returns initialization state of a network
116 : * @retval initialized value
117 : */
118 0 : bool getInitialized() const override { return initialized; }
119 :
120 : /**
121 : * @brief returns loadedFromConfig state of a network
122 : * @retval loadedFromConfig value
123 : */
124 0 : bool getLoadedFromConfig() const override { return loadedFromConfig; }
125 :
126 : /**
127 : * @brief Get Loss from the previous epoch of training data
128 : * @retval loss value
129 : */
130 5 : float getTrainingLoss() override { return training.loss; }
131 :
132 : /**
133 : * @brief Get Loss from the previous epoch of validation data
134 : * @retval loss value
135 : */
136 5 : float getValidationLoss() override { return validation.loss; }
137 :
138 0 : RunStats getTrainingStats() override { return training; }
139 :
140 0 : RunStats getValidStats() override { return validation; }
141 :
142 0 : RunStats getTestStats() override { return testing; }
143 :
144 : /**
145 : * @brief Get Learning rate
146 : * @retval Learning rate
147 : *
148 : * @todo update to return the last used learning rate
149 : */
150 1 : float getLearningRate() { return opt->getLearningRate(0); };
151 :
152 : /**
153 : * @brief Create and load the Network with ini configuration file.
154 : * @param[in] config config file path
155 : * @retval #ML_ERROR_NONE Successful.
156 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
157 : */
158 : int loadFromConfig(const std::string &config) override;
159 :
160 : /**
161 : * @brief Compile the graph in the model
162 : * @retval #ML_ERROR_NONE Successful.
163 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
164 : */
165 : int compile(ExecutionMode mode = ExecutionMode::TRAIN) override;
166 :
167 : /**
168 : * @brief set Property of Network
169 : * @param[in] values values of property
170 : * @retval #ML_ERROR_NONE Successful.
171 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
172 : */
173 : void setProperty(const std::vector<std::string> &values) override;
174 :
175 : /**
176 : * @brief Initialize Network. This should be called after set all
177 : * hyperparameters.
178 : * @retval #ML_ERROR_NONE Successful.
179 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
180 : */
181 : int initialize(ExecutionMode mode = ExecutionMode::TRAIN) override;
182 :
183 : /**
184 : * @brief Reinitialize Network. This should be called after initialize
185 : * @retval #ML_ERROR_NONE Successful.
186 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
187 : */
188 : int reinitialize() override;
189 :
190 : /**
191 : * @brief Allocate memory for the model. This should be called after
192 : * initialize.
193 : * @param[in] exec_mode allocate memory based on the given execution mode
194 : * @retval #ML_ERROR_NONE Successful.
195 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
196 : */
197 : int allocate(ExecutionMode mode = ExecutionMode::TRAIN) override;
198 :
199 : /**
200 : * @brief Deallocate memory for the model.
201 : * @param[in] trainable Assign memory for inference or train mode
202 : * @retval #ML_ERROR_NONE Successful.
203 : * @note This does not free the model graph but only the weight tensors, and
204 : * input/output/gradient/derivative tensors if any.
205 : */
206 : int deallocate();
207 :
208 : /**
209 : * @brief Update graph to make batch normalization in-place
210 : * @note This assumes that the batch normalization implementation does
211 : * not need input/output of itself while backwarding. The reason is that the
212 : * batch normalization layer caches a processed form of its own input than the
213 : * input tensor itself.
214 : * @note This optimization might break the working when some other
215 : * implementation of batch normalization layer is used or delegated to some
216 : * other backend. Ensure to verify this optimization with other
217 : * implementations once added.
218 : */
219 : void inPlaceOptimization(const std::string &layer_type);
220 :
221 : /**
222 : * @brief Forward Propagation of the neural network
223 : */
224 : sharedConstTensors forwarding(
225 : bool training = true,
226 : std::function<bool(void *userdata)> stop_cb =
227 : [](void *user_data) { return false; },
228 : void *user_data = nullptr);
229 :
230 : /**
231 : * @brief Forward Propagation of the neural network
232 : * @param[in] input List of Input Tensors taken by the neural network
233 : * @param[in] label List of Label Tensors for the model
234 : * @retval List of Output Tensors
235 : */
236 : sharedConstTensors forwarding(sharedConstTensors input,
237 : sharedConstTensors label = {},
238 : bool training = true);
239 :
240 : /**
241 : * @brief Incremental forward Propagation of the neural network
242 : */
243 : sharedConstTensors incremental_forwarding(
244 : unsigned int from, unsigned int to, bool training = true,
245 : std::function<bool(void *userdata)> stop_cb =
246 : [](void *user_data) { return false; },
247 : void *user_data = nullptr);
248 :
249 : /**
250 : * @brief Incremental forward Propagation of the neural network
251 : * @param[in] input List of Input Tensors taken by the neural network
252 : * @param[in] label List of Label Tensors for the model
253 : * @retval List of Output Tensors
254 : */
255 : sharedConstTensors incremental_forwarding(unsigned int from, unsigned int to,
256 : sharedConstTensors input,
257 : sharedConstTensors label = {},
258 : bool training = true);
259 :
260 : /**
261 : * @brief Backward Propagation of the neural network
262 : * @param[in] iteration Iteration Number for the optimizer
263 : */
264 : void backwarding(
265 : int iteration,
266 : std::function<bool(void *userdata)> stop_cb =
267 : [](void *user_data) { return false; },
268 : void *user_data = nullptr);
269 :
270 : /**
271 : * @copydoc Model::save(const std::string &file_path, ml::train::ModelFormat
272 : * format);
273 : */
274 : void save(const std::string &file_path,
275 : ml::train::ModelFormat format =
276 : ml::train::ModelFormat::MODEL_FORMAT_BIN) override;
277 :
278 : /**
279 : * @copydoc Model::load(const std::string &file_path, ml::train::ModelFormat
280 : * format);
281 : */
282 : void load(const std::string &file_path,
283 : ml::train::ModelFormat format =
284 : ml::train::ModelFormat::MODEL_FORMAT_BIN) override;
285 :
286 : /**
287 : * @brief get Epochs
288 : * @retval epochs
289 : */
290 : unsigned int getEpochs() {
291 10486 : return std::get<props::Epochs>(model_flex_props);
292 : };
293 :
294 : /**
295 : * @brief get current epoch_idx
296 : * @retval current epoch_idx
297 : */
298 : unsigned int getCurrentEpoch() override;
299 :
300 : /**
301 : * @brief Copy Neural Network
302 : * @param[in] from NeuralNetwork Object to copy
303 : * @retval NeuralNewtork Object copyed
304 : * @todo Need to implement the copy of graph core
305 : */
306 : NeuralNetwork ©(NeuralNetwork &from);
307 :
308 : /**
309 : * @brief Copy Neural Network Configuration
310 : * @param[in] from NeuralNetwork Object to copy
311 : * @retval NeuralNewtork Object copyed
312 : * @note This does not copy the context of neural network model. It only
313 : * copies the configuration of the network model. Therefore, it needs the
314 : * compile and initialization to run the model. Also if you need the
315 : * initialized the weight, load call is required.
316 : */
317 : NeuralNetwork ©Configuration(NeuralNetwork &from);
318 :
319 : /**
320 : * @brief Run NeuralNetwork train
321 : * @param[in] values hyper parameters
322 : * @param[in] stop_cb callback function to decide stop training or not
323 : * ~~~~~
324 : * @a stop_user_data user_data to be used in stop_cb
325 : * @a bool true if stop the training
326 : * ~~~~~
327 : * @param[in] epoch_complete_cb Called the end of an epoch.
328 : * @a epoch_user_data user_data to be used in epoch_complete_cb
329 : * ~~~~~
330 : * @retval #ML_ERROR_NONE Successful.
331 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
332 : */
333 : int train(
334 : const std::vector<std::string> &values = {},
335 : std::function<bool(void *)> stop_cb =
336 : [](void *stop_user_data) { return false; },
337 : void *stop_user_data = nullptr,
338 : std::function<void(void *)> epoch_complete_cb =
339 : [](void *epoch_user_data) { return false; },
340 : void *epoch_user_data = nullptr) override;
341 :
342 : /**
343 : * @brief Run NeuralNetwork inference
344 : * @param[in] X input tensor
345 : * @param[in] free_mem true to free memory. used only in training mode.
346 : * @retval shared_ptr<const Tensor>
347 : */
348 : sharedConstTensors inference(sharedConstTensors X, bool free_mem = false);
349 :
350 : /**
351 : * @brief Run NeuralNetwork inference
352 : * @param[in] X input tensor
353 : * @param[in] label label tensor
354 : * @param[in] free_mem true to free memory. used only in training mode.
355 : * @retval shared_ptr<const Tensor>
356 : */
357 : sharedConstTensors inference(sharedConstTensors X, sharedConstTensors label,
358 : bool free_mem = false);
359 :
360 : /**
361 : * @brief Run the inference of the model
362 : * @param[in] batch batch size of current input
363 : * @param[in] input inputs as a list of each input data
364 : * @param[in] label labels as a list of each label data
365 : * @retval list of output as float *
366 : * @note The output memory must not be freed by the caller
367 : */
368 : std::vector<float *> inference(
369 : unsigned int batch, const std::vector<float *> &input,
370 : const std::vector<float *> &label = std::vector<float *>()) override;
371 :
372 : /**
373 : * @brief Run NeuralNetwork incremental inference
374 : * @param[in] X input tensor
375 : * @param[in] init_seq_len initial sequence length
376 : * @param[in] from current working step index
377 : * @param[in] to next working step index
378 : * @retval shared_ptr<const Tensor>
379 : */
380 : sharedConstTensors incremental_inference(sharedConstTensors X,
381 : unsigned int init_seq_len,
382 : unsigned int from, unsigned int to);
383 :
384 : /**
385 : * @brief Run NeuralNetwork incremental inference
386 : * @param[in] X input tensor
387 : * @param[in] label label tensor
388 : * @param[in] init_seq_len initial sequence length
389 : * @param[in] from current working step index
390 : * @param[in] to next working step index
391 : * @retval shared_ptr<const Tensor>
392 : */
393 : sharedConstTensors incremental_inference(sharedConstTensors X,
394 : sharedConstTensors label,
395 : unsigned int init_seq_len,
396 : unsigned int from, unsigned int to);
397 :
398 : /**
399 : * @brief Run the incremental inference of the model
400 : * @param[in] batch batch size of current input
401 : * @param[in] input inputs as a list of each input data
402 : * @param[in] label labels as a list of each label data
403 : * @param[in] init_seq_len initial sequence length
404 : * @param[in] from current working step index
405 : * @param[in] to next working step index
406 : * @param[in] output_hidden_state return last hidden state if true else return
407 : * all hidden state
408 : * @retval list of output as float *
409 : * @note The output memory must not be freed by the caller
410 : */
411 : std::vector<float *>
412 : incremental_inference(unsigned int batch, const std::vector<float *> &input,
413 : const std::vector<float *> &label,
414 : unsigned int init_seq_len, unsigned int from,
415 : unsigned int to,
416 : bool output_hidden_state = false) override;
417 :
418 : /**
419 : * @brief reset input dimensions of a model
420 : * @param[in] dims input dimensions
421 : * @note Similar to reinitialize, the resetInputDimension API is used for
422 : * modifying input dimensions after model initialization. The reinitialize
423 : * function should be the officially called API when changing input
424 : * dimensions, as it properly recalculates weights, tensors, and outputs for
425 : * each layer. On the other hand, resetInputDimension is a specialized API
426 : * created to modify only specific dimensions (specifically height values)
427 : * within input/output dimensions. Since this API uniformly adjusts the height
428 : * across all model layers, developers must verify that every layer in their
429 : * model architecture can safely accommodate such height modifications.
430 : */
431 : void resetInputDimension(std::vector<TensorDim> dims) override;
432 :
433 : /**
434 : * @brief Run NeuralNetwork train with callback function by user
435 : * @param[in] dt datatype (mode) where it should be
436 : * @param[in] dataset set the dataset
437 : * @retval #ML_ERROR_NONE Successful.
438 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
439 : */
440 : int setDataset(const DatasetModeType &dt,
441 : std::shared_ptr<ml::train::Dataset> dataset) override;
442 :
443 : /**
444 : * @copydoc void forEachLayer(std::function<void(Layer &,
445 : * nntrainer::RunLayerContext &), void *user_data> fn);
446 : *
447 : */
448 : void forEachLayer(
449 : std::function<void(ml::train::Layer & /**< layer */,
450 : RunLayerContext & /**< rc */, void *user_data)>
451 : fn,
452 : void *user_data = nullptr) override;
453 :
454 : /**
455 : * @brief Run NeuralNetwork train with callback function by user
456 : * @param[in] dt datatype (mode) where it should be
457 : * @param[in] databuffer set the databuffer
458 : * @retval #ML_ERROR_NONE Successful.
459 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
460 : */
461 : int setDataBuffer(const DatasetModeType &dt,
462 : std::shared_ptr<DataBuffer> data_buffer);
463 :
464 : /**
465 : * @brief add layer into neural network model
466 : * @param[in] layer layer to add
467 : * @retval #ML_ERROR_NONE Successful.
468 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
469 : */
470 78 : int addLayer(std::shared_ptr<ml::train::Layer> layer) override {
471 155 : return addLayer(std::static_pointer_cast<LayerNode>(layer));
472 : }
473 :
474 : /**
475 : * @brief add layer into neural network model
476 : * @retval #ML_ERROR_NONE Successful.
477 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
478 : */
479 : int addLayer(NodeType layer);
480 :
481 : /**
482 : * @brief set optimizer for the neural network model
483 : * @retval #ML_ERROR_NONE Successful.
484 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
485 : */
486 : int setOptimizer(std::shared_ptr<ml::train::Optimizer> optimizer) override;
487 :
488 : /**
489 : * @brief get layer by name from neural network model
490 : * @param[in] name name of the layer to get
491 : * @param[out] layer shared_ptr to hold the layer to get
492 : * @retval #ML_ERROR_NONE Successful.
493 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
494 : */
495 : int getLayer(const char *name,
496 : std::shared_ptr<ml::train::Layer> *layer) override;
497 :
498 : /**
499 : * @brief this function helps exporting the layer in a predefined format,
500 : * while workarounding issue caused by templated function type eraser
501 : *
502 : * @param exporter exporter that conatins exporting logic
503 : * @param method enum value to identify how it should be exported to
504 : */
505 : void exportTo(Exporter &exporter,
506 : const ml::train::ExportMethods &method) const;
507 :
508 : /**
509 : * @brief get input dimension of neural network
510 : * @retval std::vector<TensorDim> input dimension
511 : */
512 666 : std::vector<TensorDim> getInputDimension() override {
513 666 : if (!compiled) {
514 1 : throw std::logic_error("model should be compiled before get dimension");
515 : }
516 665 : return model_graph.getInputDimension();
517 : }
518 :
519 : /**
520 : * @brief get output dimension of neural network
521 : * @retval std::vector<TensorDim> output dimension
522 : */
523 144 : std::vector<TensorDim> getOutputDimension() override {
524 144 : if (!compiled) {
525 1 : throw std::logic_error("model should be compiled before get dimension");
526 : }
527 143 : return model_graph.getOutputDimension();
528 : }
529 :
530 : /**
531 : * @brief get FlatGraph of current graph
532 : * @note flat graph contains pointer to the actual nodes, which is not deeply
533 : * copied.
534 : * @retval flatGraph of the current graph
535 : * @note these layers will be in sorted order if the model is compiled,
536 : * otherwise the order is the order of addition of layers in the model.
537 : */
538 5 : FlatGraphType getFlatGraph() { return model_graph.getLayerNodes(); }
539 :
540 : /**
541 : * @brief get if the model is empty
542 : * @param[out] true if empty, else false
543 : */
544 : bool empty() const { return model_graph.empty(); }
545 :
546 : /**
547 : * @brief get the number of nodes in the model
548 : * @param[out] number of nodes
549 : */
550 1 : size_t size() const { return model_graph.size(); }
551 :
552 : /**
553 : * @brief get network graph
554 : * @retval NetowrkGraphType
555 : */
556 315 : NetworkGraphType getNetworkGraph() { return model_graph; }
557 :
558 : /**
559 : * @brief get current graph from the model
560 : * @note graph contains pointer to the actual nodes, which is not deeply
561 : * copied.
562 : * @retval current graph
563 : */
564 : GraphType getUnsortedLayers(const std::string &input_layer = "",
565 : const std::string &output_layer = "");
566 :
567 : /**
568 : * @brief Summarize the model
569 : * @param out std::ostream to get the model summary
570 : * @param verbosity verbosity of the summary
571 : */
572 17 : virtual void summarize(std::ostream &out,
573 : ml_train_summary_type_e verbosity) override {
574 17 : printPreset(out, (unsigned int)verbosity);
575 17 : }
576 :
577 : /**
578 : * @brief Print Option when printing model info. The function delegates to the
579 : * `print`
580 : * @param out std::ostream to print
581 : * @param preset preset from `ml_train_summary_type_e`
582 : */
583 : virtual void printPreset(std::ostream &out, unsigned int preset);
584 :
585 : /**
586 : * @brief Enable dynamic fine-tuning optimization
587 : * @param threshold Comparison limit to decide if weight updated or not
588 : * @param mode dynamic fine-tuning optimization mode. Supported modes are
589 : * "max" and "norm" for now
590 : */
591 : void enableDynamicTraining(
592 : float threshold, std::string op = DynamicTrainingOptimization::dft_opt_norm,
593 : std::string mode = DynamicTrainingOptimization::dft_opt_mode_derivative) {
594 : dynamic_training_opt.setThreshold(threshold);
595 : dynamic_training_opt.setOp(op);
596 : dynamic_training_opt.setMode(mode);
597 : dynamic_training_opt.enable();
598 : }
599 :
600 : /**
601 : * @brief Disable dynamic fine-tuning optimization
602 : */
603 : void disableDynamicFineTuning() { dynamic_training_opt.disable(); }
604 :
605 : /**
606 : * @copydoc void ml::train::Model::addWithReferenceLayers(
607 : * const std::vector<std::shared_ptr<Layer>> &reference,
608 : * const std::string &scope, const std::vector<std::string> &input_layers,
609 : * const std::vector<std::string> &start_layers,
610 : * const std::vector<std::string> &end_layers, ReferenceLayersType type,
611 : * const std::vector<std::string> &type_properties = {})
612 : *
613 : */
614 : void addWithReferenceLayers(
615 : const std::vector<std::shared_ptr<ml::train::Layer>> &reference,
616 : const std::string &scope, const std::vector<std::string> &input_layers,
617 : const std::vector<std::string> &start_layers,
618 : const std::vector<std::string> &end_layers,
619 : ml::train::ReferenceLayersType type,
620 : const std::vector<std::string> &type_properties = {}) override;
621 :
622 : /**
623 : * @copydoc void ml::train::Model::addWithReferenceLayers(
624 : * const std::vector<std::shared_ptr<Layer>> &reference,
625 : * const std::string &scope, const std::vector<std::string> &input_layers,
626 : * const std::vector<std::string> &start_layers,
627 : * const std::vector<std::string> &end_layers, ReferenceLayersType type,
628 : * const std::vector<std::string> &type_properties = {})
629 : */
630 : void addWithReferenceLayers(
631 : const std::vector<std::shared_ptr<LayerNode>> &reference,
632 : const std::string &scope, const std::vector<std::string> &input_layers,
633 : const std::vector<std::string> &start_layers,
634 : const std::vector<std::string> &end_layers,
635 : ml::train::ReferenceLayersType type,
636 : const std::vector<std::string> &type_properties = {});
637 :
638 : /**
639 : * @brief export the model according to given export method
640 : * @param method export method
641 : * @param file_path path to be serialized
642 : */
643 : void exports(const ml::train::ExportMethods &method,
644 : const std::string file_path) override;
645 :
646 : private:
647 : using FlexiblePropTypes =
648 : std::tuple<props::Epochs, props::TrainingBatchSize, props::SavePath,
649 : props::ContinueTrain, props::SaveBestPath,
650 : props::MemoryOptimization, props::Fsu, props::FsuPath,
651 : props::FsuLookahead, props::TensorFormat,
652 : props::ModelTensorDataType>;
653 : using RigidPropTypes =
654 : std::tuple<props::LossType, std::vector<props::InputConnection>,
655 : std::vector<props::LabelLayer>, props::ClipGradByGlobalNorm,
656 : props::LossScale>;
657 :
658 : RigidPropTypes model_props; /**< model props */
659 : FlexiblePropTypes model_flex_props; /**< model train props */
660 : std::string load_path; /**< path to load weights when initialize */
661 : int model_file_fd = -1;
662 :
663 : /**
664 : * @brief Print Options when printing layer info
665 : */
666 : typedef enum {
667 : // clang-format off
668 : PRINT_INST_INFO = (1 << 0), /**< Option to print type & instance address info */
669 : PRINT_GRAPH_INFO = (1 << 1), /**< Option to print graph topology info */
670 : PRINT_PROP = (1 << 2), /**< Option to print properties */
671 : PRINT_OPTIMIZER = (1 << 3), /**< Option to print optimizer */
672 : PRINT_METRIC = (1 << 4), /**< Option to print if current network is set to training */
673 : // clang-format on
674 : } PrintOption;
675 :
676 : unsigned int epoch_idx; /**< Number of epoch_idx */
677 :
678 : unsigned int iter; /**< iterations trained */
679 :
680 : float loss; /**< loss */
681 :
682 : std::shared_ptr<OptimizerWrapped> opt; /**< Optimizer; this gets copied into
683 : each layer, do not use this directly */
684 :
685 : std::array<std::shared_ptr<DataBuffer>, 3>
686 : data_buffers; /**< Data Buffers to get Input */
687 :
688 : bool initialized; /**< Network is initialized */
689 :
690 : bool compiled; /**< Network is compiled */
691 :
692 : bool loadedFromConfig; /**< Check if config is loaded to prevent load twice */
693 :
694 : RunStats validation; /** validation statistics of the model */
695 : RunStats training; /** training statistics of the model */
696 : RunStats testing; /** testing statistics of the model */
697 :
698 : ExecutionMode exec_mode; /** execution mode : train : inference */
699 :
700 : const Engine *ct_engine =
701 : nullptr; /** Configurations bound to current engine */
702 :
703 : NetworkGraph model_graph; /** Network Model Graph */
704 :
705 : GraphRepresentation graph_representation; /** Unsorted graph representation */
706 :
707 : DynamicTrainingOptimization dynamic_training_opt; /**< Dynamic fine-tuning
708 : optimization mode. supported modes are "max" and "norm" */
709 :
710 : /**
711 : * @brief save model in ini
712 : *
713 : * @param file_path file path
714 : */
715 : void saveModelIni(const std::string &file_path);
716 :
717 : /**
718 : * @brief print function for neuralnet
719 : * @param[in] out outstream
720 : * @param[in] flags bit combination of Neuralnet::PrintOption
721 : * @param[in] Layer::PrintPreset print preset when to print layer properties
722 : */
723 : void print(std::ostream &out, unsigned int flags = 0,
724 : LayerNode::PrintPreset layerPrintPreset =
725 : LayerNode::PrintPreset::PRINT_SUMMARY);
726 :
727 : /**
728 : * @brief Set Loss
729 : * @param[in] l loss value
730 : */
731 : void setLoss(float l);
732 :
733 : /**
734 : * @brief Run NeuralNetwork train
735 : * @param[in] stop_cb callback function to decide stop training or not
736 : * @param[in] epoch_complete_cb Called the end of an epoch.
737 : * @retval #ML_ERROR_NONE Successful.
738 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
739 : */
740 : int train_run(
741 : std::function<bool(void *)> stop_cb = [](void *) { return false; },
742 : void *user_data = nullptr,
743 : std::function<void(void *)> epoch_complete_cb =
744 : [](void *) { return false; },
745 : void *data = nullptr);
746 :
747 : /**
748 : * @brief Swap function for the class
749 : */
750 : friend void swap(NeuralNetwork &lhs, NeuralNetwork &rhs);
751 :
752 : /**
753 : * @brief set Property/Configuration of Network for training after the
754 : * network has been initialized
755 : * @param[in] values values of property
756 : * @retval #ML_ERROR_NONE Successful.
757 : * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
758 : */
759 : void setTrainConfig(const std::vector<std::string> &values);
760 :
761 : /**
762 : * @brief print metrics function for neuralnet
763 : * @param[in] out outstream
764 : * @param[in] flags verbosity from ml_train_summary_type_e
765 : */
766 : void printMetrics(std::ostream &out, unsigned int flags = 0);
767 :
768 : /**
769 : * @brief Match the given tensor shape with input shape of the model
770 : * @param[in] X input tensor
771 : * @retval true if matches, false is error
772 : */
773 : bool validateInput(sharedConstTensors X);
774 : };
775 :
776 : } /* namespace nntrainer */
777 :
778 : #endif /* __cplusplus */
779 : #endif /* __NEURALNET_H__ */
|