-
Notifications
You must be signed in to change notification settings - Fork 74
/
functions.h
251 lines (215 loc) · 5.92 KB
/
functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// SPDX-License-Identifier: Apache-2.0
/**
* Copyright (C) 2024 SeungBaek Hong <[email protected]>
*
* @file functions.h
* @date 15 March 2024
* @brief NNTrainer API for IR graph configurations.
* @see https://github.com/nnstreamer/nntrainer
* @author SeungBaek Hong <[email protected]>
* @bug No known bugs except for NYI items
*
* @note This is experimental API and not stable.
*/
#ifndef __ML_TRAIN_FUNCTIONS_H__
#define __ML_TRAIN_FUNCTIONS_H__
#if __cplusplus < MIN_CPP_VERSION
#error "CPP versions c++17 or over are only supported"
#endif // __cpluscplus
#include <iostream>
#include <memory>
#include <vector>
namespace ml {
namespace train {
class Function;
/**
* @brief TensorCore for graph configuration.
*/
class TensorNode {
public:
bool is_leaf = true;
bool requires_grad = false;
std::shared_ptr<Function> creator = nullptr;
/**
* @brief Construct a new TensorNode
*/
TensorNode(): is_leaf(true), requires_grad(false) {}
/**
* @brief Construct a new TensorNode from Function
*/
TensorNode(std::shared_ptr<Function> &func):
is_leaf(false), requires_grad(true), creator(func) {}
};
/**
* @brief Tensor API for users
*/
class Tensor {
private:
std::shared_ptr<TensorNode> node = nullptr;
public:
/**
* @brief Construct a new Tensor
*/
Tensor() {
node = std::make_shared<TensorNode>();
}
/**
* @brief Construct a new Tensor from Function
*/
Tensor(std::shared_ptr<Function> &func) {
node = std::make_shared<TensorNode>(func);
}
/**
* @brief Construct a new Tensor using TensorNode
*/
Tensor(std::shared_ptr<TensorNode> &node) {
node = node;
}
/**
* @brief Check if the tensor is a leaf tensor
*/
bool is_leaf() {
return node->is_leaf;
}
/**
* @brief Check if the tensor requires gradients
*/
bool get_requires_grad() {
return node->requires_grad;
}
/**
* @brief Set the requires_grad flag of the tensor
*/
bool set_requires_grad(bool requires_grad) {
return node->requires_grad = requires_grad;
}
/**
* @brief Return the creator function of the tensor
*/
std::shared_ptr<Function> get_creator() {
return node->creator;
}
/**
* @brief Return the tensor node
*/
std::shared_ptr<TensorNode> get_node() {
return node;
}
};
/**
* @brief Function(operation) API
*/
class Function {
private:
/**
* @brief Keep input tensors of the function
*/
std::vector<std::shared_ptr<TensorNode>> inputs;
/**
* @brief Keep output tensors of the function
*/
std::vector<std::shared_ptr<TensorNode>> outputs;
public:
std::string op_type = "";
/**
* @brief Forwarding operation for graph configurations
*/
std::vector<Tensor> forward(std::shared_ptr<Function> &func,
std::vector<Tensor> xs, int num_output_tensors=1) {
std::vector<Tensor> ys = std::vector<Tensor>();
for (int i=0; i < (int)xs.size(); ++i) {
inputs.push_back(xs[i].get_node());
}
for (int i=0; i < num_output_tensors; ++i) {
Tensor t = Tensor(func);
outputs.push_back(t.get_node());
ys.push_back(t);
}
return ys;
}
/**
* @brief Get input tensors of the function
*/
std::vector<std::shared_ptr<TensorNode>> get_inputs() {
return inputs;
}
/**
* @brief Get output tensors of the function
*/
std::vector<std::shared_ptr<TensorNode>> get_outputs() {
return outputs;
}
};
/**
* @brief Add Function Class
*/
class Add : public Function {
public:
Add(): Function() { op_type = "add"; }
};
/**
* @brief Sub Function Class
*/
class Sub : public Function {
public:
Sub(): Function() { op_type = "sub"; }
};
/**
* @brief Mul Function Class
*/
class Mul : public Function {
public:
Mul(): Function() { op_type = "mul"; }
};
/**
* @brief Div Function Class
*/
class Div : public Function {
public:
Div(): Function() { op_type = "div"; }
};
/**
* @brief Pow Function Class
*/
class Pow : public Function {
public:
Pow(): Function() { op_type = "pow"; }
};
/**
* @brief Add operation api
*/
Tensor add(Tensor &x1, Tensor &x2) {
std::shared_ptr<Function> f = std::make_shared<Add>();
return f->forward(f, {x1, x2})[0];
};
/**
* @brief Sub operation api
*/
Tensor sub(Tensor &x1, Tensor &x2) {
std::shared_ptr<Function> f = std::make_shared<Sub>();
return f->forward(f, {x1, x2})[0];
};
/**
* @brief Mul operation api
*/
Tensor mul(Tensor &x1, Tensor &x2) {
std::shared_ptr<Function> f = std::make_shared<Mul>();
return f->forward(f, {x1, x2})[0];
};
/**
* @brief Div operation api
*/
Tensor div(Tensor &x1, Tensor &x2) {
std::shared_ptr<Function> f = std::make_shared<Div>();
return f->forward(f, {x1, x2})[0];
};
/**
* @brief Pow operation api
*/
Tensor pow(Tensor &x) {
std::shared_ptr<Function> f = std::make_shared<Pow>();
return f->forward(f, {x})[0];
};
}
}
#endif // __ML_TRAIN_FUNCTIONS_H__