-
Notifications
You must be signed in to change notification settings - Fork 115
/
dl_variable.hpp
553 lines (501 loc) · 16.6 KB
/
dl_variable.hpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#pragma once
#include <stdio.h>
#include <vector>
#include <assert.h>
#include <iostream>
#include "dl_tool.hpp"
namespace dl
{
/**
* @brief Tensor
*
* @tparam T support uint8_t, int8_t, int16_t and float.
*/
template <typename T>
class Tensor
{
private:
int size; /*<! size of element including padding */
bool auto_free; /*<! free element when object destroy */
std::vector<int> axis_offset; /*<! element offset of each axis */
public:
T *element; /*<! point to element */
int exponent; /*<! exponent of element */
std::vector<int> shape; /*<! shape of Tensor */
/**
* @brief Construct a new Tensor object
*
*/
Tensor() : auto_free(true), element(NULL), exponent(0) { this->set_shape({0}); }
/**
* @brief Construct a new Tensor object by copying from input.
*
* @param input an input Tensor
* @param deep one of true or false
* - true: apply a new memory, copy value from input.element to this new memory
* - false: take over input.element to this->element
*/
Tensor(Tensor<T> &input, bool deep) : size(input.size),
auto_free(input.auto_free),
exponent(input.exponent)
{
this->set_shape(input.shape);
if (deep && (input.element != NULL))
{
int size_real = input.get_size();
T *new_element = (T *)tool::calloc_aligned_prefer(size_real, sizeof(T), 16);
tool::copy_memory(new_element, input.element, size_real * sizeof(T));
this->element = new_element;
}
else
{
this->element = input.element;
this->auto_free = false;
}
}
/**
* @brief Destroy the Tensor object
*
*/
~Tensor()
{
if (this->auto_free)
this->free_element();
}
/**
* @brief copy the element of the input Tensor.
*
* @param input an input Tensor
* @param deep one of true or false
* - true: apply a new memory, copy value from input.element to this new memory
* - false: take over input.element to this->element
* @return Tensor<T>& self
*/
Tensor<T> ©_element(Tensor<T> &input, bool deep)
{
assert(this->get_size() == input.get_size());
assert(input.element != NULL);
this->malloc_element();
if (deep)
{
tool::copy_memory(this->element, input.element, this->get_size() * sizeof(T));
}
else
{
this->element = input.element;
this->auto_free = false;
}
return *this;
}
/**
* @brief Set the auto free object.
*
* @param auto_free one of true or false
* - true: free element when object destroyed
* - false: do not
* @return self
*/
Tensor<T> &set_auto_free(const bool auto_free)
{
this->auto_free = auto_free;
return *this;
}
/**
* @brief Set the element.
*
* @param element point to element memory
* @return self
*/
Tensor<T> &set_element(T *element, const bool auto_free = false)
{
assert(this->element == NULL);
this->element = element;
this->auto_free = auto_free;
return *this;
}
/**
* @brief Set the exponent.
*
* @param exponent exponent of element
* @return self
*/
Tensor<T> &set_exponent(const int exponent)
{
this->exponent = exponent;
return *this;
}
/**
* @brief Set the shape of Tensor.
*
* @param shape the target shape
*
* @return self
*/
Tensor<T> &set_shape(const std::vector<int> shape);
/**
* @brief print the shape of the Tensor
*
*/
void print_shape()
{
if (this->shape.size())
{
printf("shape = (");
for (int i = 0; i < this->shape.size() - 1; i++)
{
printf("%d, ", this->shape[i]);
}
printf("%d)\n", this->shape.back());
}
else
{
printf("shape = ()\n");
}
}
/**
* @brief flatten the Tensor
*
* @return Tensor<T>& self
*/
Tensor<T> &flatten();
/**
* @brief Change a new shape to the Tensor without changing its data.
*
* @param shape the target shape
* @return Tensor<T>& self
*/
Tensor<T> &reshape(std::vector<int> shape);
/**
* @brief Remove dims with length==1 from Tensor
*
* @param axis the dim to to be remove. make sure the length of the dim is equal to 1.
* if axis == INT32_MAX, all the dims with length==1 will be removed.
* @return Tensor<T>& self
*/
Tensor<T> &squeeze(int axis = INT32_MAX);
/**
* @brief Insert a new dim that will appear at the axis position in the expanded Tensor shape.
*
* @param axis the dim to be inserted
* @return Tensor<T>& self
*/
Tensor<T> &expand_dims(int axis);
/**
* @brief Insert a new dim that will appear at the axis position in the expanded Tensor shape.
*
* @param axis the dim to be inserted
* @return Tensor<T>& self
*/
Tensor<T> &expand_dims(std::vector<int> axis);
/**
* @brief Reverse or permute the axes of the Tensor
*
* @param perm the new arangement of the dims. if perm == {}, the dims arangement will be reversed.
* @return Tensor<T>& self
*/
Tensor<T> &transpose(std::vector<int> perm = {});
/**
* @brief Reverse or permute the axes of the input Tensor
*
* @param input the input Tensor
* @param perm the new arangement of the dims. if perm == {}, the dims arangement will be reversed.
* @return Tensor<T>& self
*/
Tensor<T> &transpose(Tensor<T> &input, std::vector<int> perm = {});
/**
* @brief Get the element pointer.
*
* @return pointer to memory
*/
T *get_element_ptr()
{
return this->element;
}
/**
* @brief Get the element value.
*
* @param index the index of each dim.
* @return T element value
*/
T get_element_value(const std::vector<int> index)
{
return this->element[this->get_element_index(index)];
}
/**
* @brief Get the element value.
*
* @param index the index of the element.
* @return T element value
*/
T get_element_value(int index)
{
return this->element[index];
}
/**
* @brief Set the all the element to value.
*
* @param value target value
* @return Tensor<T>& self
*/
Tensor<T> &set_value(T value);
/**
* @brief Set the the element to value
*
* @param value target value, it will be broadcast automatically.
* @return Tensor<T>& self
*/
Tensor<T> &set_value(Tensor<T> &value);
/**
* @brief Set the sliced element to value
*
* @param axis_index_range range of slices
* @param value target value
* @return Tensor<T>& self
*/
Tensor<T> &set_value(std::vector<int> axis_index_range, T value);
/**
* @brief Set the sliced element to value
*
* @param axis_index_range range of slices
* @param value target value, it will be broadcast automatically.
* @return Tensor<T>& self
*/
Tensor<T> &set_value(std::vector<int> axis_index_range, Tensor<T> &value);
/**
* @brief Extracts a slice from the Tensor.
*
* @param axis_index_range range of slices
* @return Tensor<T> output
*/
Tensor<T> slice(std::vector<int> axis_index_range);
/**
* @brief Reverses specific dims of the tensor.
*
* @param axis The dims to be reversed
* @return Tensor<T>&
*/
Tensor<T> &reverse(std::vector<int> axis);
/**
* @brief Get the size of Tensor.
*
* @return the size of Tensor.
*/
int get_size()
{
return this->size;
}
/**
* @brief Get the axis offset
*
* @return std::vector<int> the axis offset
*/
std::vector<int> get_axis_offset()
{
return this->axis_offset;
}
/**
* @brief Apply memory with zero-initialized only if this->element is NULL.
*
* @param auto_free one of true or false
* - true: free element when object destroyed
* - false: do not
* @return
* - true: on success
* - false: if applying failed
*/
bool calloc_element(const bool auto_free = true)
{
if (this->element != NULL)
return false;
this->element = (T *)dl::tool::calloc_aligned_prefer(this->get_size(), sizeof(T), 16);
this->auto_free = auto_free;
return true;
}
/**
* @brief Apply memory without initialized only if this->element is NULL.
*
* @param auto_free one of true or false
* - true: free element when object destroyed
* - false: do not
* @return
* - true: on success
* - false: if applying failed
*/
bool malloc_element(const bool auto_free = true)
{
if (this->element != NULL)
return false;
this->element = (T *)tool::malloc_aligned_prefer(this->get_size(), sizeof(T), 16);
this->auto_free = auto_free;
return true;
}
/**
* @brief free element only if this->element != NULL
* set this->element to NULL, after free
* @brief Free element if this->element is not NULL.
*/
void free_element()
{
if (this->auto_free && this->element)
{
tool::free_aligned_prefer(this->element);
this->element = NULL;
}
}
/**
* @brief print the element of the tensor
*
* @param axis_index_range the element range of each dims to be print. if axis_index_range == {}, all the element will be print.
* @param message to print
*/
void print(std::vector<int> axis_index_range = {}, const char *message = "");
/**
* @brief print all the element of the Tensor.
*
* @param message to print
*/
void print_all(const char *message = "")
{
std::cout << "\n"
<< message << " | ";
this->print_shape();
for (int i = 0; i < this->get_size(); i++)
{
std::cout << this->element[i] << " ";
}
std::cout << "\n";
return;
}
/**
* @brief Get the index of each dims
*
* @param element_index the index of the element
* @return std::vector<int> the index of each dims
*/
std::vector<int> get_axis_index(int element_index);
/**
* @brief Get the index of element
*
* @param axis_index the index of each dims
* @return int the index of element
*/
int get_element_index(const std::vector<int> axis_index);
/**
* @brief Check the element value with input ground-truth.
*
* @param gt_element ground-truth value of element
* @param bias permissible error
* @param info one of true or false
* - true: shape and result
* - false: do not
* @param failed_number maximum number of wrong element that will be printed
*
* @return
* - true: in permissible error
* - false: not
*/
bool check_element(T *gt_element, int bias = 2, bool info = true, int failed_number = 0)
{
int count = 0;
if (info)
this->print_shape();
int size = this->get_size();
for (int i = 0; i < size; i++)
{
if (DL_ABS(this->element[i] - gt_element[i]) > bias)
{
std::vector<int> index = get_axis_index(i);
std::cout << "element[";
for (int j = 0; j < index.size() - 1; j++)
{
std::cout << index[j] << ", ";
}
std::cout << index.back() << "]: ";
std::cout << +this->element[i] << " v.s. " << +gt_element[i] << "\n";
count++;
if (count > failed_number)
return false;
}
}
if (count)
return false;
if (info)
printf("PASS\n");
return true;
}
/**
* @brief Check the shape is the same as the shape of input.
*
* @param input an input tensor
* @return
* - true: same shape
* - false: not
*/
bool is_same_shape(Tensor<T> &input)
{
if (input.shape.size() != this->shape.size())
{
return false;
}
for (int i = 0; i < this->shape.size(); i++)
{
if (input.shape[i] != this->shape[i])
{
return false;
}
}
return true;
}
Tensor<T> &operator=(const Tensor<T> &input)
{
this->auto_free = input.auto_free;
this->exponent = input.exponent;
int size_real_tmp = this->size;
int size_input_real = input.size;
this->set_shape(input.shape);
if (input.element)
{
if (this->element)
{
if (size_real_tmp != size_input_real)
{
tool::free_aligned_prefer(this->element);
T *new_element = (T *)tool::malloc_aligned_prefer(size_input_real, sizeof(T), 16);
tool::copy_memory(new_element, input.element, size_input_real * sizeof(T));
this->element = new_element;
}
else
{
tool::copy_memory(this->element, input.element, size_input_real * sizeof(T));
}
}
else
{
T *new_element = (T *)tool::malloc_aligned_prefer(size_input_real, sizeof(T), 16);
tool::copy_memory(new_element, input.element, size_input_real * sizeof(T));
this->element = new_element;
}
return *this;
}
else
{
if (this->element)
{
tool::free_aligned_prefer(this->element);
this->element = NULL;
}
return *this;
}
}
static Tensor<T> arange(int size)
{
Tensor<T> output;
output.set_auto_free(true).set_exponent(0).set_shape({size}).malloc_element();
for (int i = 0; i < size; ++i)
{
output.element[i] = i;
}
return output;
}
};
} // namespace dl