-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_tensor.h
More file actions
337 lines (320 loc) · 14.6 KB
/
nn_tensor.h
File metadata and controls
337 lines (320 loc) · 14.6 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2023 Jeff Boody
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef nn_tensor_H
#define nn_tensor_H
#include "../libcc/jsmn/cc_jsmnStream.h"
#include "../libcc/jsmn/cc_jsmnWrapper.h"
#include "../libvkk/vkk.h"
#include "nn_dim.h"
typedef enum
{
NN_TENSOR_INIT_ZERO = 0,
NN_TENSOR_INIT_XAVIER = 1,
NN_TENSOR_INIT_HE = 2,
} nn_tensorInit_e;
typedef enum
{
NN_TENSOR_MODE_IO = 0,
NN_TENSOR_MODE_COMPUTE = 1,
} nn_tensorMode_e;
// SN: Spectral Normalization
// BSSN: Bidirectional Scaled Spectral Normalization
typedef enum
{
NN_TENSOR_NORM_NONE = 0,
NN_TENSOR_NORM_SN = 1,
NN_TENSOR_NORM_BSSN = 2,
} nn_tensorNorm_e;
#define NN_TENSOR_NORM_COUNT 3
typedef struct nn_tensorOpUs0Idx_s
{
uint32_t x1n;
uint32_t x2n;
uint32_t yn;
uint32_t count;
uint32_t x1i;
uint32_t x2i;
uint32_t yi;
uint32_t height;
uint32_t x1j;
uint32_t x2j;
uint32_t yj;
uint32_t width;
uint32_t x1k;
uint32_t x2k;
uint32_t yk;
uint32_t depth;
float value;
} nn_tensorOpUs0Idx_t;
typedef struct nn_tensorOpUs0Data_s
{
vkk_buffer_t* sb006_idx;
vkk_uniformSet_t* us0;
} nn_tensorOpUs0Data_t;
nn_tensorOpUs0Data_t* nn_tensorOpUs0Data_new(nn_tensor_t* X1,
nn_tensor_t* X2,
nn_tensor_t* Y,
nn_tensorOpUs0Idx_t* idx);
void nn_tensorOpUs0Data_delete(nn_tensorOpUs0Data_t** _self);
int nn_tensorOpUs0Data_update(nn_tensorOpUs0Data_t* self,
nn_tensor_t* X1,
nn_tensor_t* X2,
nn_tensor_t* Y,
nn_tensorOpUs0Idx_t* idx);
typedef struct nn_tensor_s
{
nn_engine_t* engine;
nn_tensorMode_e mode;
nn_dim_t dim;
// IO tensor (optional)
float* data;
// compute tensor (optional)
// sb_dim/sb_data index varies by use case
vkk_buffer_t* sb_dim;
vkk_buffer_t* sb_data;
vkk_uniformSet_t* us0;
// spectral normalization (optional)
nn_tensorNorm_e norm;
vkk_buffer_t* sb100_data_u1;
vkk_buffer_t* sb101_data_v1;
vkk_buffer_t* sb102_data_u2;
vkk_buffer_t* sb103_data_v2;
vkk_buffer_t* sb104_c;
vkk_uniformSet_t* us1_norm;
} nn_tensor_t;
/*
* Tensors may be stored in CPU accessible memory or GPU
* only memory. The engine, arch, layers and loss will only
* accept compute tensors unless specified otherwise. Each
* tensor function has different memory requirements in
* order operate. The IO and compute functions require the
* corresponding mode to be set. The remaining functions may
* be used in either case. It is the users responsibility to
* copy IO tensors to/from compute tensors as required.
*
* The compute tensor functions may only be called when the
* engine is in the computing state. The auxillary functions
* (e.g. import/export/copy) may be called on a compute
* tensor which is not in use by the compute engine. Keep in
* mind that that the compute pipeline operations are not
* guaranteed to complete until the engine compute pass
* ends.
*
* The compute functions submit their commands to be
* executed on the GPU in an arbitrary order. In some
* scenarios, a set of commands may require a specific
* exection order to produce the correct result. The correct
* execution order is guaranteed by specifying a hazard flag
* which describes write-after-read (WAR) and
* read-after-write (RAW) conflicts. The computeOp
* functions may be used to write to separate regions of a
* tensor across multiple calls, however, this should be
* treated as a RAW conflict.
*/
nn_tensor_t* nn_tensor_new(nn_engine_t* engine,
nn_dim_t* dim,
nn_tensorInit_e init,
nn_tensorMode_e mode);
void nn_tensor_delete(nn_tensor_t** _self);
int nn_tensor_import(nn_tensor_t* self,
cc_jsmnVal_t* val);
int nn_tensor_export(nn_tensor_t* self,
cc_jsmnStream_t* stream);
nn_dim_t* nn_tensor_dim(nn_tensor_t* self);
nn_tensorMode_e nn_tensor_mode(nn_tensor_t* self);
int nn_tensor_copy(nn_tensor_t* X,
nn_tensor_t* Y,
uint32_t xn,
uint32_t yn,
uint32_t count);
int nn_tensor_ioClear(nn_tensor_t* self,
uint32_t n,
uint32_t count);
int nn_tensor_ioCopy(nn_tensor_t* X,
nn_tensor_t* Y,
uint32_t xn,
uint32_t yn,
uint32_t count);
int nn_tensor_ioCopyRegion(nn_tensor_t* X,
nn_tensor_t* Y,
uint32_t xn,
uint32_t yn,
uint32_t count,
uint32_t xi,
uint32_t yi,
uint32_t height,
uint32_t xj,
uint32_t yj,
uint32_t width,
uint32_t xk,
uint32_t yk,
uint32_t depth);
float nn_tensor_ioGet(nn_tensor_t* self,
uint32_t n, uint32_t i,
uint32_t j, uint32_t k);
void nn_tensor_ioSet(nn_tensor_t* self,
uint32_t n, uint32_t i,
uint32_t j, uint32_t k,
float v);
int nn_tensor_ioExportPng(nn_tensor_t* self,
const char* fname,
uint32_t n,
uint32_t k,
uint32_t depth,
float min,
float max);
int nn_tensor_computeFill(nn_tensor_t* self,
vkk_hazard_e hazard,
uint32_t n,
uint32_t count,
float value);
int nn_tensor_computeCopy(nn_tensor_t* X,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t xn,
uint32_t yn,
uint32_t count);
int nn_tensor_computeFillOp(nn_tensor_t* self,
vkk_hazard_e hazard,
uint32_t n,
uint32_t count,
uint32_t i,
uint32_t height,
uint32_t j,
uint32_t width,
uint32_t k,
uint32_t depth,
float value);
int nn_tensor_computeCopyOp(nn_tensor_t* X,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t xn,
uint32_t yn,
uint32_t count,
uint32_t xi,
uint32_t yi,
uint32_t height,
uint32_t xj,
uint32_t yj,
uint32_t width,
uint32_t xk,
uint32_t yk,
uint32_t depth);
int nn_tensor_computeAddOp(nn_tensor_t* X1,
nn_tensor_t* X2,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t x1n,
uint32_t x2n,
uint32_t yn,
uint32_t count,
uint32_t x1i,
uint32_t x2i,
uint32_t yi,
uint32_t height,
uint32_t x1j,
uint32_t x2j,
uint32_t yj,
uint32_t width,
uint32_t x1k,
uint32_t x2k,
uint32_t yk,
uint32_t depth);
int nn_tensor_computeMixOp(nn_tensor_t* X1,
nn_tensor_t* X2,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t x1n,
uint32_t x2n,
uint32_t yn,
uint32_t count,
uint32_t x1i,
uint32_t x2i,
uint32_t yi,
uint32_t height,
uint32_t x1j,
uint32_t x2j,
uint32_t yj,
uint32_t width,
uint32_t x1k,
uint32_t x2k,
uint32_t yk,
uint32_t depth,
float value);
int nn_tensor_computeMulOp(nn_tensor_t* X,
vkk_hazard_e hazard,
uint32_t xn,
uint32_t count,
uint32_t xi,
uint32_t height,
uint32_t xj,
uint32_t width,
uint32_t xk,
uint32_t depth,
float value);
int nn_tensor_computeScaleOp(nn_tensor_t* X,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t xn,
uint32_t yn,
uint32_t count,
uint32_t xi,
uint32_t yi,
uint32_t height,
uint32_t xj,
uint32_t yj,
uint32_t width,
uint32_t xk,
uint32_t yk,
uint32_t depth,
float value);
int nn_tensor_computeScaleAddOp(nn_tensor_t* X1,
nn_tensor_t* X2,
nn_tensor_t* Y,
vkk_hazard_e hazard,
uint32_t x1n,
uint32_t x2n,
uint32_t yn,
uint32_t count,
uint32_t x1i,
uint32_t x2i,
uint32_t yi,
uint32_t height,
uint32_t x1j,
uint32_t x2j,
uint32_t yj,
uint32_t width,
uint32_t x1k,
uint32_t x2k,
uint32_t yk,
uint32_t depth,
float value);
int nn_tensor_computeNormalize(nn_tensor_t* self,
vkk_hazard_e hazard,
nn_tensorNorm_e norm,
float c);
int nn_tensor_computeStats(nn_tensor_t* self,
vkk_hazard_e hazard,
uint32_t count,
nn_tensorStats_t* stats);
#endif