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 factory.cpp
6 : * @date 14 October 2020
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Parichay Kapoor <pk.kapoor@samsung.com>
9 : * @author Debadri Samaddar <s.debadri@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : * @brief This is implementaion for factory builder interface for c++ API
12 : */
13 :
14 : #include <memory>
15 : #include <string>
16 : #include <vector>
17 :
18 : #include <databuffer.h>
19 : #include <databuffer_factory.h>
20 : #include <engine.h>
21 : #include <layer.h>
22 : #include <model.h>
23 : #include <neuralnet.h>
24 : #include <nntrainer_error.h>
25 : #include <optimizer.h>
26 : #include <optimizer_wrapped.h>
27 :
28 : #include <cstdlib>
29 :
30 : namespace ml {
31 : namespace train {
32 :
33 90 : std::unique_ptr<Layer> createLayer(const LayerType &type,
34 : const std::vector<std::string> &properties) {
35 90 : return nntrainer::createLayerNode(type, properties);
36 : }
37 :
38 : /**
39 : * @brief Factory creator with constructor for layer
40 : */
41 33 : std::unique_ptr<Layer> createLayer(const std::string &type,
42 : const std::vector<std::string> &properties) {
43 33 : return nntrainer::createLayerNode(type, properties);
44 : }
45 :
46 : std::unique_ptr<Optimizer>
47 29 : createOptimizer(const OptimizerType &type,
48 : const std::vector<std::string> &properties) {
49 29 : return nntrainer::createOptimizerWrapped(type, properties);
50 : }
51 :
52 : /**
53 : * @brief Factory creator with constructor for optimizer
54 : */
55 : std::unique_ptr<Optimizer>
56 153 : createOptimizer(const std::string &type,
57 : const std::vector<std::string> &properties) {
58 153 : return nntrainer::createOptimizerWrapped(type, properties);
59 : }
60 :
61 : /**
62 : * @brief Factory creator with constructor for model
63 : */
64 73 : std::unique_ptr<Model> createModel(ModelType type,
65 : const std::vector<std::string> &properties) {
66 73 : std::unique_ptr<Model> model;
67 73 : switch (type) {
68 71 : case ModelType::NEURAL_NET:
69 144 : model = std::make_unique<nntrainer::NeuralNetwork>();
70 : break;
71 2 : default:
72 2 : throw std::invalid_argument("This type of model is not yet supported");
73 : }
74 :
75 71 : model->setProperty(properties);
76 :
77 71 : return model;
78 : }
79 :
80 : /**
81 : * @brief creator by copying the configuration of other model
82 : */
83 3 : std::unique_ptr<Model> copyConfiguration(Model &from) {
84 : std::unique_ptr<nntrainer::NeuralNetwork> model =
85 3 : std::make_unique<nntrainer::NeuralNetwork>();
86 3 : nntrainer::NeuralNetwork &f = dynamic_cast<nntrainer::NeuralNetwork &>(from);
87 3 : model->copyConfiguration(f);
88 2 : return model;
89 : }
90 :
91 : /**
92 : * @brief Factory creator with constructor for dataset
93 : */
94 : std::unique_ptr<Dataset>
95 2 : createDataset(DatasetType type, const std::vector<std::string> &properties) {
96 2 : std::unique_ptr<Dataset> dataset = nntrainer::createDataBuffer(type);
97 0 : dataset->setProperty(properties);
98 :
99 0 : return dataset;
100 : }
101 :
102 : std::unique_ptr<Dataset>
103 29 : createDataset(DatasetType type, const char *file,
104 : const std::vector<std::string> &properties) {
105 29 : std::unique_ptr<Dataset> dataset = nntrainer::createDataBuffer(type, file);
106 27 : dataset->setProperty(properties);
107 :
108 27 : return dataset;
109 : }
110 :
111 : /**
112 : * @brief Factory creator with constructor for dataset
113 : */
114 : std::unique_ptr<Dataset>
115 35 : createDataset(DatasetType type, datagen_cb cb, void *user_data,
116 : const std::vector<std::string> &properties) {
117 : std::unique_ptr<Dataset> dataset =
118 70 : nntrainer::createDataBuffer(type, cb, user_data);
119 35 : dataset->setProperty(properties);
120 :
121 35 : return dataset;
122 : }
123 :
124 : /**
125 : * @brief Factory creator with constructor for learning rate scheduler type
126 : */
127 : std::unique_ptr<ml::train::LearningRateScheduler>
128 22 : createLearningRateScheduler(const LearningRateSchedulerType &type,
129 : const std::vector<std::string> &properties) {
130 22 : auto &eg = nntrainer::Engine::Global();
131 22 : return eg.createLearningRateSchedulerObject(type, properties);
132 : }
133 :
134 : /**
135 : * @brief Factory creator with constructor for learning rate scheduler
136 : */
137 : std::unique_ptr<ml::train::LearningRateScheduler>
138 1 : createLearningRateScheduler(const std::string &type,
139 : const std::vector<std::string> &properties) {
140 1 : auto &eg = nntrainer::Engine::Global();
141 1 : return eg.createLearningRateSchedulerObject(type, properties);
142 : }
143 :
144 0 : std::string getVersion() {
145 0 : std::string version = std::to_string(VERSION_MAJOR);
146 : version += ".";
147 0 : version += std::to_string(VERSION_MINOR);
148 : version += ".";
149 0 : version += std::to_string(VERSION_MICRO);
150 :
151 0 : return version;
152 : }
153 :
154 : } // namespace train
155 : } // namespace ml
|