-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTensor.h
703 lines (560 loc) · 19.2 KB
/
Tensor.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
* TinyGPT
* @author : [email protected]
*
*/
#pragma once
#include <cstdint>
#include <limits>
#include <optional>
#include <random>
#include <sstream>
#include <string>
#include <vector>
namespace TinyGPT {
#define TENSOR_MAX_DIMS 16
typedef enum TensorError_ {
TensorError_None = 0,
TensorError_EmptyTensor,
TensorError_InvalidShape,
TensorError_InvalidAxis,
TensorError_InvalidSections,
TensorError_ShapeNotAligned,
TensorError_NotSupport,
} TensorError;
typedef enum ShapeCompatible_ {
ShapeCompatible_Error = 0,
ShapeCompatible_SameShape,
ShapeCompatible_Broadcast,
} ShapeCompatible;
typedef std::vector<int32_t> Shape;
typedef std::vector<float> Array1d;
typedef std::vector<std::vector<float>> Array2d;
typedef std::vector<std::vector<std::vector<float>>> Array3d;
class RandomGenerator {
public:
static void setSeed(const unsigned int seed) {
seed_ = seed;
randomEngine_ = std::default_random_engine(seed_.value());
}
static std::default_random_engine getGenerator() {
if (seed_.has_value()) {
return randomEngine_;
}
std::random_device r;
return std::default_random_engine(r());
}
private:
static std::optional<unsigned int> seed_;
static std::default_random_engine randomEngine_;
};
struct Size2D {
Size2D(int32_t n) : h(n), w(n) {}
Size2D(int32_t h, int32_t w) : h(h), w(w) {}
int32_t h = 0;
int32_t w = 0;
};
// one axis only
class Axis {
public:
Axis() = delete;
Axis(int32_t axis) : axis_(axis) {}
[[nodiscard]] int32_t get(int32_t axisCnt) const {
return axis_ >= 0 ? axis_ : axis_ + axisCnt;
}
private:
int32_t axis_ = 0;
};
class UFunc {
public:
virtual ~UFunc() = default;
virtual void op(const float &val) { idx_++; };
virtual float result() { return tmp; };
virtual void reset() {
idx_ = 0;
tmp = 0.f;
}
protected:
int32_t idx_ = 0;
float tmp = 0.f;
};
class UFuncSum : public UFunc {
public:
void op(const float &val) override { tmp += val; }
};
class UFuncMean : public UFunc {
public:
void op(const float &val) override {
idx_++;
tmp += val;
}
float result() override { return tmp / (float)idx_; }
};
class UFuncVar : public UFunc {
public:
void op(const float &val) override {
idx_++;
tmp += val;
squareSum_ += val * val;
}
float result() override {
float mean = tmp / (float)idx_;
return squareSum_ / (float)idx_ - mean * mean;
}
void reset() override {
idx_ = 0;
tmp = 0;
squareSum_ = 0;
}
private:
float squareSum_ = 0;
};
class UFuncMin : public UFunc {
public:
void op(const float &val) override {
if (val < tmp) {
tmp = val;
}
}
void reset() override { tmp = std::numeric_limits<float>::max(); }
};
class UFuncMax : public UFunc {
public:
void op(const float &val) override {
if (val > tmp) {
tmp = val;
}
}
void reset() override { tmp = -std::numeric_limits<float>::max(); }
};
class UFuncArgMin : public UFunc {
public:
void op(const float &val) override {
if (val < tmp) {
tmp = val;
minIdx_ = idx_;
}
idx_++;
}
float result() override { return (float)minIdx_; }
void reset() override {
tmp = std::numeric_limits<float>::max();
idx_ = 0;
minIdx_ = 0;
}
private:
int32_t minIdx_ = 0;
};
class UFuncArgMax : public UFunc {
public:
void op(const float &val) override {
if (val > tmp) {
tmp = val;
maxIdx_ = idx_;
}
idx_++;
}
float result() override { return (float)maxIdx_; }
void reset() override {
tmp = -std::numeric_limits<float>::max();
idx_ = 0;
maxIdx_ = 0;
}
private:
int32_t maxIdx_ = 0;
};
// float type elements only
class Tensor {
public:
Tensor() = default;
Tensor(const Tensor &other) {
dispose();
copyFrom(other);
initData(other.data_);
}
Tensor(Tensor &&other) noexcept {
copyFrom(other);
other.data_ = nullptr;
}
Tensor &operator=(const Tensor &other) {
if (this == &other) {
return *this;
}
dispose();
copyFrom(other);
initData(other.data_);
return *this;
}
Tensor &operator=(Tensor &&other) noexcept {
if (this == &other) {
return *this;
}
dispose();
copyFrom(other);
other.data_ = nullptr;
return *this;
}
void copyFrom(const Tensor &other) {
dimCount_ = other.dimCount_;
elemCount_ = other.elemCount_;
shape_ = other.shape_;
strides_ = other.strides_;
data_ = other.data_;
}
void dispose() {
dimCount_ = 0;
elemCount_ = 0;
shape_.clear();
strides_.clear();
delete[] data_;
data_ = nullptr;
}
~Tensor() { dispose(); }
static Tensor shape(const Shape &shape);
static Tensor scalar(const float &value);
static Tensor ones(const Shape &shape);
static Tensor onesLike(const Tensor &t);
static Tensor zeros(const Shape &shape);
static Tensor rand(const Shape &shape);
static Tensor randn(const Shape &shape);
static Tensor bernoulli(const Shape &shape, float p);
static Tensor tri(int32_t n, int32_t m = 0, int32_t k = 0);
// 1d array
explicit Tensor(const Array1d &values1d);
// 2d array
explicit Tensor(const Array2d &values2d);
// 3d array
explicit Tensor(const Array3d &values3d);
Tensor reshape(const Shape &shape);
static Tensor reshape(const Tensor &t, const Shape &shape);
[[nodiscard]] Tensor reshape(const Shape &shape) const;
[[nodiscard]] Tensor view(const Shape &shape) const { return reshape(shape); }
void flatten(int32_t startDim = 0, int32_t endDim = -1);
static Tensor flatten(const Tensor &t, int32_t startDim = 0,
int32_t endDim = -1);
void unflatten(int32_t dim, const std::vector<int32_t> &sizes);
static Tensor unflatten(const Tensor &t, int32_t dim,
const std::vector<int32_t> &sizes);
void squeeze(int32_t dim = -1);
void squeeze(const std::vector<int32_t> &dims);
static Tensor squeeze(const Tensor &t, int32_t dim = -1);
static Tensor squeeze(const Tensor &t, const std::vector<int32_t> &dims);
void unsqueeze(int32_t dim);
static Tensor unsqueeze(const Tensor &t, int32_t dim);
[[nodiscard]] bool empty() const { return elemCount_ == 0; }
[[nodiscard]] bool isScalar() const {
return dimCount_ == 0 && elemCount_ == 1;
}
[[nodiscard]] int32_t dim() const { return dimCount_; }
[[nodiscard]] int32_t size() const { return elemCount_; }
[[nodiscard]] const Shape &shape() const { return shape_; }
[[nodiscard]] const std::vector<int32_t> &strides() const { return strides_; }
[[nodiscard]] float item() const { return data_[0]; }
float &operator[](int32_t idx) { return data_[idx]; }
const float &operator[](int32_t idx) const { return data_[idx]; }
template <typename T = float> [[nodiscard]] std::vector<T> toArray() const;
// fill
void fill(float value);
static Tensor fill(const Tensor &t, float value);
// clamp
void clampMin(float min);
void clampMax(float max);
void clamp(float min, float max);
static Tensor clampMin(const Tensor &t, float min);
static Tensor clampMax(const Tensor &t, float max);
static Tensor clamp(const Tensor &t, float min, float max);
// range
static std::vector<int32_t> range(int32_t start, int32_t stop,
int32_t step = 1);
static Tensor arange(float start, float stop, float step = 1.f);
static Tensor linspace(float start, float end, int steps);
// indexing
template <typename... Args> [[nodiscard]] Tensor index(Args... args) const {
std::vector<int32_t> vec;
vec.reserve(sizeof...(args));
(vec.push_back(args), ...);
return indexInteger(vec);
}
Tensor indexInteger(const std::vector<int32_t> &idx,
float *dataPtr = nullptr) const;
[[nodiscard]] Tensor index(const std::vector<int32_t> &idx) const;
[[nodiscard]] Tensor
indexAdvance(const std::vector<std::vector<int32_t>> &indexes) const;
void indexIntegerSet(const std::vector<int32_t> &idx, float val);
void indexIntegerSet(const std::vector<int32_t> &idx, const Tensor &val);
void indexAdvanceSet(const std::vector<std::vector<int32_t>> &indexes,
float val);
void indexAdvanceSet(const std::vector<std::vector<int32_t>> &indexes,
const Tensor &val);
// im2col
[[nodiscard]] Tensor im2col(Size2D kernelSize, Size2D stride,
Size2D padding = 0) const;
// col2im
[[nodiscard]] Tensor col2im(const Shape &inputShape, Size2D kernelSize,
Size2D stride, Size2D padding = 0) const;
// transpose
[[nodiscard]] Tensor transpose(const std::vector<int32_t> &axis = {}) const;
static Tensor transpose(const Tensor &t,
const std::vector<int32_t> &axis = {}) {
return t.transpose(axis);
}
// split
[[nodiscard]] std::vector<Tensor> split(int32_t sections,
const Axis &axis = 0) const;
[[nodiscard]] std::vector<Tensor> vsplit(int32_t sections) const {
return split(sections, 0);
}
[[nodiscard]] std::vector<Tensor> hsplit(int32_t sections) const {
return split(sections, 1);
}
[[nodiscard]] std::vector<Tensor> dsplit(int32_t sections) const {
return split(sections, 2);
}
[[nodiscard]] std::vector<Tensor> split(const std::vector<int32_t> &indices,
const Axis &axis = 0) const;
[[nodiscard]] std::vector<Tensor>
vsplit(const std::vector<int32_t> &indices) const {
return split(indices, 0);
}
[[nodiscard]] std::vector<Tensor>
hsplit(const std::vector<int32_t> &indices) const {
return split(indices, 1);
}
[[nodiscard]] std::vector<Tensor>
dsplit(const std::vector<int32_t> &indices) const {
return split(indices, 2);
}
static std::vector<Tensor> split(const Tensor &t, int32_t sections,
const Axis &axis = 0) {
return t.split(sections, axis);
}
static std::vector<Tensor> vsplit(const Tensor &t, int32_t sections) {
return t.split(sections, 0);
}
static std::vector<Tensor> hsplit(const Tensor &t, int32_t sections) {
return t.split(sections, 1);
}
static std::vector<Tensor> dsplit(const Tensor &t, int32_t sections) {
return t.split(sections, 2);
}
static std::vector<Tensor> split(const Tensor &t,
const std::vector<int32_t> &indices,
const Axis &axis = 0) {
return t.split(indices, axis);
}
static std::vector<Tensor> vsplit(const Tensor &t,
const std::vector<int32_t> &indices) {
return t.split(indices, 0);
}
static std::vector<Tensor> hsplit(const Tensor &t,
const std::vector<int32_t> &indices) {
return t.split(indices, 1);
}
static std::vector<Tensor> dsplit(const Tensor &t,
const std::vector<int32_t> &indices) {
return t.split(indices, 2);
}
// concatenate
static Tensor
concatenate(const std::vector<std::reference_wrapper<Tensor>> &arrays);
static Tensor
concatenate(const std::vector<std::reference_wrapper<Tensor>> &arrays,
const Axis &axis);
// stack
static Tensor stack(const std::vector<std::reference_wrapper<Tensor>> &arrays,
const Axis &axis = 0);
static Tensor
vstack(const std::vector<std::reference_wrapper<Tensor>> &arrays);
static Tensor
hstack(const std::vector<std::reference_wrapper<Tensor>> &arrays);
static Tensor
dstack(const std::vector<std::reference_wrapper<Tensor>> &arrays);
// compare
Tensor operator<(const Tensor &other) const;
Tensor operator>(const Tensor &other) const;
Tensor operator==(const Tensor &other) const;
Tensor operator!=(const Tensor &other) const;
Tensor operator<(const float &other) const;
Tensor operator>(const float &other) const;
Tensor operator==(const float &other) const;
Tensor operator!=(const float &other) const;
// math
Tensor operator+(const Tensor &other) const;
Tensor operator-(const Tensor &other) const;
Tensor operator*(const Tensor &other) const;
Tensor operator/(const Tensor &other) const;
Tensor operator+(const float &other) const;
Tensor operator-(const float &other) const;
Tensor operator*(const float &other) const;
Tensor operator/(const float &other) const;
void operator+=(const Tensor &other);
void operator-=(const Tensor &other);
void operator*=(const Tensor &other);
void operator/=(const Tensor &other);
void operator+=(const float &other);
void operator-=(const float &other);
void operator*=(const float &other);
void operator/=(const float &other);
friend Tensor operator+(const float &other, const Tensor &obj);
friend Tensor operator-(const float &other, const Tensor &obj);
friend Tensor operator*(const float &other, const Tensor &obj);
friend Tensor operator/(const float &other, const Tensor &obj);
static Tensor sin(const Tensor &t);
static Tensor cos(const Tensor &t);
static Tensor sqrt(const Tensor &t);
static Tensor tanh(const Tensor &t);
static Tensor exp(const Tensor &t);
static Tensor log(const Tensor &t);
[[nodiscard]] Tensor sin() const { return Tensor::sin(*this); }
[[nodiscard]] Tensor cos() const { return Tensor::cos(*this); }
[[nodiscard]] Tensor sqrt() const { return Tensor::sqrt(*this); }
[[nodiscard]] Tensor tanh() const { return Tensor::tanh(*this); }
[[nodiscard]] Tensor exp() const { return Tensor::exp(*this); }
[[nodiscard]] Tensor log() const { return Tensor::log(*this); }
[[nodiscard]] Tensor pow(const Tensor &other) const;
[[nodiscard]] Tensor pow(const float &other) const;
static Tensor pow(const Tensor &x1, const Tensor &x2) { return x1.pow(x2); }
static Tensor pow(const Tensor &x1, const float &x2) { return x1.pow(x2); }
// linear algebra
static float dot(const float &a, const float &b);
static Tensor dot(const Tensor &a, const float &b);
static Tensor dot(const float &a, const Tensor &b);
static Tensor dot(const Tensor &a, const Tensor &b);
static Tensor matmul(const Tensor &a, const Tensor &b);
static Tensor matmulTrans(const Tensor &a, const Tensor &b);
// aggregation
static float min(const Tensor &t);
static float max(const Tensor &t);
static float mean(const Tensor &t);
static float sum(const Tensor &t);
static float var(const Tensor &t);
static float argmin(const Tensor &t);
static float argmax(const Tensor &t);
[[nodiscard]] float min() const { return Tensor::min(*this); };
[[nodiscard]] float max() const { return Tensor::max(*this); };
[[nodiscard]] float mean() const { return Tensor::mean(*this); };
[[nodiscard]] float sum() const { return Tensor::sum(*this); };
[[nodiscard]] float var() const { return Tensor::var(*this); };
[[nodiscard]] float argmin() const { return Tensor::argmin(*this); };
[[nodiscard]] float argmax() const { return Tensor::argmax(*this); };
static Tensor min(const Tensor &t, const Axis &axis, bool keepDims = false);
static Tensor max(const Tensor &t, const Axis &axis, bool keepDims = false);
static Tensor mean(const Tensor &t, const Axis &axis, bool keepDims = false);
static Tensor sum(const Tensor &t, const Axis &axis, bool keepDims = false);
static Tensor var(const Tensor &t, const Axis &axis, bool keepDims = false);
static Tensor argmin(const Tensor &t, const Axis &axis,
bool keepDims = false);
static Tensor argmax(const Tensor &t, const Axis &axis,
bool keepDims = false);
[[nodiscard]] Tensor min(const Axis &axis, bool keepDims = false) const {
return Tensor::min(*this, axis, keepDims);
}
[[nodiscard]] Tensor max(const Axis &axis, bool keepDims = false) const {
return Tensor::max(*this, axis, keepDims);
}
[[nodiscard]] Tensor mean(const Axis &axis, bool keepDims = false) const {
return Tensor::mean(*this, axis, keepDims);
}
[[nodiscard]] Tensor sum(const Axis &axis, bool keepDims = false) const {
return Tensor::sum(*this, axis, keepDims);
}
[[nodiscard]] Tensor var(const Axis &axis, bool keepDims = false) const {
return Tensor::var(*this, axis, keepDims);
}
[[nodiscard]] Tensor argmin(const Axis &axis, bool keepDims = false) const {
return Tensor::argmin(*this, axis, keepDims);
}
[[nodiscard]] Tensor argmax(const Axis &axis, bool keepDims = false) const {
return Tensor::argmax(*this, axis, keepDims);
}
public:
class Iterator {
public:
explicit Iterator(const float *ptr) : ptr(ptr) {}
const float &operator*() const { return *ptr; }
Iterator &operator++() {
++ptr;
return *this;
}
bool operator==(const Iterator &other) const { return ptr == other.ptr; }
bool operator!=(const Iterator &other) const { return ptr != other.ptr; }
private:
const float *ptr;
};
[[nodiscard]] Iterator begin() const { return Iterator(data_); }
[[nodiscard]] Iterator end() const { return Iterator(data_ + elemCount_); }
protected:
void initMeta();
void initData(const float *from = nullptr);
void traverse(UFunc &func, int32_t start, int32_t stride, int32_t cnt) const;
Tensor reduce(UFunc &func, int32_t axis, bool keepDims = false) const;
void splitAxis(std::vector<Tensor> &retTensors,
std::vector<int32_t> &splitIndices, int32_t axis) const;
static Tensor
arraysConcat(const std::vector<std::reference_wrapper<Tensor>> &arrays,
const Shape &retShape, const std::vector<int32_t> &concatIndices,
int32_t axis);
static ShapeCompatible checkCompatible(const Shape &t0, const Shape &t1,
Shape &retShape, int32_t skipLast = 0);
static bool
checkShapeEqual(const std::vector<std::reference_wrapper<Tensor>> &arrays,
int32_t exceptAxis);
static void error(const char *where, TensorError error);
private:
static float fastTanh(float x);
void indexIntegerSet(const std::vector<int32_t> &idx, const float *valPtr);
protected:
int32_t dimCount_ = 0;
int32_t elemCount_ = 0;
Shape shape_;
std::vector<int32_t> strides_;
float *data_ = nullptr;
};
template <typename T> std::vector<T> Tensor::toArray() const {
std::vector<T> ret;
ret.reserve(elemCount_);
for (int32_t i = 0; i < elemCount_; i++) {
ret.push_back((T)data_[i]);
}
return ret;
}
class TensorIter {
public:
explicit TensorIter(const Shape &shape);
// get shape
Shape shape();
// reshape
void reshape(const Shape &shape);
// get size
[[nodiscard]] int32_t size() const { return size_; }
// get current coordinates
[[nodiscard]] const int32_t *coordinates() const { return coordinates_; };
// return -1 if not available
int32_t next();
// reset to init states
void reset();
// broadcast to shape (no broadcast rules check)
void broadcast(const Shape &shape);
// transpose
void transpose(const std::vector<int32_t> &axis);
protected:
// reorder array
static void reorder(int32_t *v, const std::vector<int32_t> &order) {
auto n = order.size();
std::vector<int32_t> temp(n);
for (int i = 0; i < n; ++i) {
temp[i] = v[order[i]];
}
memcpy(v, temp.data(), sizeof(int32_t) * n);
}
protected:
int32_t ndM1_ = 0;
int32_t size_ = 0;
int32_t dimsM1_[TENSOR_MAX_DIMS]{};
int32_t strides_[TENSOR_MAX_DIMS]{};
int32_t backStrides_[TENSOR_MAX_DIMS]{};
int32_t coordinates_[TENSOR_MAX_DIMS]{};
int32_t index_ = 0;
int32_t itCnt_ = 0;
};
} // namespace TinyGPT