-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtensor.hpp
1193 lines (1112 loc) · 45.1 KB
/
tensor.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
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* tensor.cc for LEICHT
* Copyright (C) 2017 Mo Zhou <[email protected]>
* MIT License
*/
#if !defined(_LEICHT_TENSOR_HPP)
#define _LEICHT_TENSOR_HPP
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <climits>
#include <cfloat>
#include <iostream>
#include <fstream>
#include <string>
#include <sys/time.h>
#include <unordered_map> // faster than <map>
#include <vector>
#include <jsoncpp/json/json.h>
#include "leicht.hpp"
#if defined(USE_BLAS)
#include <cblas-openblas.h>
#endif
#if defined(USE_OPENMP) && !defined(__clang__)
#include <omp.h>
#elif defined(USE_OPENMP) && defined(__clang__)
#include "/usr/lib/gcc/x86_64-linux-gnu/7/include/omp.h" // FIXME: dirty hack
#endif
using namespace std;
///////////////////////////////////////////////////////////////////////////////
//
// lLAS: lumin/leicht (Non-Basic) Linear Algebra Subroutines
//
///////////////////////////////////////////////////////////////////////////////
//
// llas -> BLAS-1 : level 1, vector operation
// llas -> BLAS-2 : level 2, matrix vector operation
// llas -> BLAS-3 : level 3, matrix matrix operation
// Reference: Netlib BLAS/LAPACK
// TODO: will the tricks used in reference BLAS help?
// TODO: support negative increment?
//
// begin [[ lLAS ]] -----------------------------------------------------------
namespace llas {
// BLAS-1 function: ASUM
template <typename Dtype> Dtype
asum(size_t n, Dtype* x, int incx)
{
Dtype ret = 0.;
#if defined(USE_OPENMP)
#pragma omp parallel for reduction (+:ret)
#endif
for (size_t i = 0; i < n; i++) {
ret += x[i*incx] > (Dtype)0. ? x[i*incx] : -x[i*incx];
}
return ret;
}
//ut llas/asum
//> cout << endl; Tensor<double> x(5); x.rand_(); x.dump();
//> cout << llas::asum(x.getSize(), x.data, 1) << endl;
//ut llas/asum incx
//> cout << endl; Tensor<double> x(3,5); x.rand_(); x.dump();
//> cout << llas::asum(3, x.data, 5) << endl;
// BLAS-1 routine: AXPY: Y <- aX + Y
template <typename Dtype> void
axpy(size_t n, Dtype alpha, Dtype* x, int incx, Dtype* y, int incy)
{
#if defined(USE_OPENMP)
#pragma omp parallel for shared(n,alpha,x,y,incx,incy)
#endif
for (size_t i = 0; i < n; i++)
y[i*incy] += alpha * x[i*incx];
}
//ut llas/axpy
//> cout << endl; Tensor<double> x(5); x.rand_(); x.dump();
//> Tensor<double> y(5); y.rand_(); y.dump();
//> llas::axpy(5, .5, x.data, 1, y.data, 1);
//> y.dump();
// BLAS-1 routine: COPY: Y <- X
template <typename Dtype> void
copy(size_t n, Dtype* x, int incx, Dtype* y, int incy)
{
if (incx == 1 && incy == 1) {
memcpy(y, x, n*sizeof(Dtype)); // Contiguous memory: Very Fast
} else {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(n,x,incx,y,incy)
#endif
for (size_t i = 0; i < n; i++)
y[i*incy] = x[i*incx];
}
}
// BLAS-1 function: DOT : scalar <- X^T \cdot Y
template <typename Dtype> Dtype
dot(size_t n, Dtype* x, int incx, Dtype* y, int incy)
{
Dtype ret = 0.;
#if defined(USE_OPENMP)
#pragma omp parallel for reduction(+:ret) shared(n,x,incx,y,incy)
#endif
for (size_t i = 0; i < n; i++) {
ret += x[i*incx] * y[i*incy];
}
return ret;
}
// BLAS-1 function: NRM2
// XXX: different from the reference BLAS
template <typename Dtype> Dtype
nrm2(size_t n, Dtype* x, int incx)
{
Dtype ret = 0.;
#if defined(USE_OPENMP)
#pragma omp parallel for reduction(+:ret) shared(n,x,incx)
#endif
for (size_t i = 0; i < n; i++) {
ret += x[i*incx] * x[i*incx];
}
return std::sqrt(ret);
}
// BLAS-1 routine: SCAL
template <typename Dtype> void
scal(size_t n, Dtype alpha, Dtype* x, int incx) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(n,alpha,x,incx)
#endif
for (size_t i = 0; i < n; i++)
x[i*incx] *= alpha;
}
// BLAS-1 function: AMAX
// BLAS-1 function: AMIN
// BLAS-1 routine: SWAP
// BLAS-1 routine: ROT
// BLAS-2 routine: GEMV
// BLAS-3 routine: GEMM: C <- aAB + bC
// Optimized for less Cache Misses, far better than a Naive GEMM impl.
// reference: Netlib Lapack/Blas
#if defined(USE_BLAS)
void
gemm(bool transA, bool transB, size_t M, size_t N, size_t K,
double alpha, double* A, int lda, double* B, int ldb,
double beta, double* C, int ldc) {
cblas_dgemm(CblasRowMajor, transA ? CblasTrans : CblasNoTrans,
transB ? CblasTrans : CblasNoTrans, M, N, K,
alpha, A, lda, B, ldb, beta, C, ldc);
}
void
gemm(bool transA, bool transB, size_t M, size_t N, size_t K,
float alpha, float* A, int lda, float* B, int ldb,
float beta, float* C, int ldc) {
cblas_sgemm(CblasRowMajor, transA ? CblasTrans : CblasNoTrans,
transB ? CblasTrans : CblasNoTrans, M, N, K,
alpha, A, lda, B, ldb, beta, C, ldc);
}
#else
template <typename Dtype> void
gemm(bool transA, bool transB, size_t M, size_t N, size_t K,
Dtype alpha, Dtype* A, int lda, Dtype* B, int ldb,
Dtype beta, Dtype* C, int ldc)
{
if (!transA && !transB) { // A * B
//#pragma omp parallel for collapse(2)
//for (size_t i = 0; i < M; i++) {
// for (size_t j = 0; j < N; j++) {
// Dtype vdot = beta * C[i*ldc+j];
// for (size_t k = 0; k < K; k++) {
// vdot += alpha * A[i*lda+k] * B[k*ldb+j];
// }
// C[i*ldc+j] = vdot;
// }
//} // I5-2520M, OMPthread=2, 512x512 double gemm, 10 run, 2384 ms. (Naive version)
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif // USE_OPENMP
for (size_t i = 0; i < M; i++) {
if (beta != 1.) for (size_t j = 0; j < N; j++)
C[i*ldc+j] *= beta;
for (size_t k = 0; k < K; k++) {
Dtype temp = alpha * A[i*lda+k];
#if defined(USE_OPENMP)
#pragma omp simd
#endif // USE_OPENMP
for (size_t j = 0; j < N; j++)
C[i*ldc+j] += temp * B[k*ldb+j];
}
} // I5-2520M, OMPthread=2, 512x512 double gemm, 10 run, 553 ms.
} else if (transA && !transB) { // A^T * B
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif // USE_OPENMP
for (size_t i = 0; i < M; i++) {
if (beta != 1.) for (size_t j = 0; j < N; j++)
C[i*ldc+j] *= beta;
for (size_t k = 0; k < K; k++) {
Dtype temp = alpha * A[k*lda+i];
#if defined(USE_OPENMP)
#pragma omp simd
#endif // USE_OPENMP
for (size_t j = 0; j < N; j++)
C[i*ldc+j] += temp * B[k*ldb+j];
}
} // I5-2520M, OMPthread=2, 512x512 double gemm, 10 run, 629 ms. (Naive version 13295 ms)
} else if (!transA && transB) { // A * B^T
#if defined(USE_OPENMP)
#pragma omp parallel for collapse(2)
#endif // USE_OPENMP
for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
Dtype temp = 0.;
#if defined(USE_OPENMP)
#pragma omp simd
#endif // USE_OPENMP
for (size_t k = 0; k < K; k++) {
temp += A[i*lda+k] * B[j*ldb+k];
}
C[i*ldc+j] = alpha * temp + beta * C[i*ldc+j];
}
} // I5-2520M, OMPthread=2, 512x512 double gemm, 10 run, 710 ms. (= Naive version)
} else { // A^T * B^T
#if defined(USE_OPENMP)
#pragma omp parallel for collapse(2)
#endif // USE_OPENMP
for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
Dtype temp = 0.;
#if defined(USE_OPENMP)
#pragma omp simd
#endif // USE_OPENMP
for (size_t k = 0; k < K; k++) {
temp += A[k*lda+i] * B[j*ldb+k];
}
C[i*ldc+j] = alpha * temp + beta * C[i*ldc+j];
}
} // I5-2520M, OMPthread=2, 512x512 double gemm, 10 run, 2421 ms. (Naive version 4750 ms)
}
}
#endif // USE_BLAS
//ut llas/gemm noTrans noTrans
//> Tensor<double> x(3,5); x.rand_(); Tensor<double> y(5,2); y.rand_();
//> Tensor<double> z(3,2);
//> llas::gemm(false, false, 3, 2, 5, 1., x.data, 5, y.data, 2, 0., z.data, 2);
//> z.dump();
//>cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 3, 2, 5, 1., x.data, 5, y.data, 2, 0., z.data, 2);
//> z.dump();
//
//ut llas/gemm noTrans Trans
//> Tensor<double> x(3,5); x.rand_(); Tensor<double> y(2,5); y.rand_();
//> Tensor<double> z(3,2);
//> llas::gemm(false, true, 3, 2, 5, 1., x.data, 5, y.data, 5, 0., z.data, 2);
//> z.dump();
//>cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, 3, 2, 5, 1., x.data, 5, y.data, 5, 0., z.data, 2);
//> z.dump();
//
//ut llas/gemm Trans noTrans
//> Tensor<double> x(5,3); x.rand_(); Tensor<double> y(5,2); y.rand_();
//> Tensor<double> z(3,2);
//> llas::gemm(true, false, 3, 2, 5, 1., x.data, 3, y.data, 2, 0., z.data, 2);
//> z.dump();
//>cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, 3, 2, 5, 1., x.data, 3, y.data, 2, 0., z.data, 2);
//> z.dump();
//
//ut llas/gemm Trans Trans
//> Tensor<double> x(5,3); x.rand_(); Tensor<double> y(2,5); y.rand_();
//> Tensor<double> z(3,2);
//> llas::gemm(true, true, 3, 2, 5, 1., x.data, 3, y.data, 5, 0., z.data, 2);
//> z.dump();
//>cblas_dgemm(CblasRowMajor, CblasTrans, CblasTrans, 3, 2, 5, 1., x.data, 3, y.data, 5, 0., z.data, 2);
//> z.dump();
}
// end [[ lLAS ]] -----------------------------------------------------------
template <typename Dtype>
class Tensor {
public:
// Optional name of the tensor
std::string name;
// Tensor shape, shape.size() = tensor dimension
std::vector<size_t> shape;
// Dynamic linear memory block for holding data
Dtype* data = nullptr;
// Destructor: common
~Tensor(void) {
shape.clear();
if (nullptr != data) free(data);
}
//ut destruct
//> Tensor<bool> x;
// setName: common
void setName(string name) {
this->name = name;
}
//ut setname
//> Tensor<double> x; x.setName("x");
// Constructor: empty
Tensor(void) { }
//ut create
//> auto x = new Tensor<double>();
//> delete x;
// Constructor: 1D (e.g. vector) (from raw data)
Tensor(size_t length, Dtype* mem = nullptr) {
this->shape.push_back(length);
this->resetMem();
if (nullptr != mem) memcpy(data, mem, sizeof(Dtype)*length);
}
//ut 1d tensor creation
//> Tensor<double> vector (10); vector.dump();
// Constructor: 2D (e.g. matrix) (from raw data)
Tensor(size_t row, size_t col, Dtype* mem = nullptr) {
for (size_t i : {row, col}) this->shape.push_back(i);
this->resetMem();
if (nullptr != mem) memcpy(data, mem, sizeof(Dtype)*row*col);
}
//ut 2d tensor creation
//> Tensor<double> matrix (10, 10); matrix.dump();
//> auto x = new Tensor<double> (100, 100); delete x;
// Constructor: 3D (e.g. image) (from raw data)
Tensor(size_t channel, size_t height, size_t width, Dtype* mem = nullptr) {
for (size_t i : {channel, height, width}) this->shape.push_back(i);
this->resetMem();
if (nullptr != mem) memcpy(data, mem, sizeof(Dtype)*channel*height*width);
}
//ut create 3d tensor
//> Tensor<double> x (2,3,3);
// Constructor: 4D (e.g. video) (from raw data)
Tensor(size_t time, size_t channel, size_t height, size_t width, Dtype* mem = nullptr) {
for (size_t i : {time, channel, height, width}) this->shape.push_back(i);
this->resetMem();
if (nullptr != mem) memcpy(data, mem, sizeof(Dtype)*time*channel*height*width);
}
//ut create 4d tensor
//> Tensor<double> x (2,3,4,5);
// Locator: 1D offset
inline Dtype* at(size_t offset) {
return this->data + offset;
}
//ut 1d offset
//> Tensor<int> x (10); ++*x.at(0); ++*x.at(9);
// Locator: 2D offset
inline Dtype* at(size_t row, size_t col) {
return this->data + row*shape[1] + col;
}
//ut 2d offset
//> Tensor<int> x(10,10);
//> ++*x.at(0,0); ++*x.at(0,9); ++*x.at(9,0); ++*x.at(9,9);
// Locator: 3D offset
inline Dtype* at(size_t c, size_t h, size_t w) {
return this->data + c*shape[1]*shape[2] + h*shape[2] + w;
}
//ut 3d offset
//> Tensor<int> x(10,10,10);
//> ++*x.at(0,0,0); ++*x.at(9,9,9);
// locator: 4D offset
inline Dtype* at(size_t t, size_t c, size_t h, size_t w) {
return this->data + t*shape[1]*shape[2]*shape[3] +
c*shape[2]*shape[3] + h*shape[3] + w;
}
// Copier: common, copy raw data into this Tensor
void copy(Dtype* mem, size_t sz) {
if (sz > getSize())
fprintf(stderr, "Tensor::copy Error: getSize()=%ld but you want to put data of size %ld.\n", getSize(), sz);
assert(sz <= getSize());
memcpy(data, mem, sizeof(Dtype)*sz);
}
// Copier: common, copy data from another instance
void copy(Tensor<Dtype>* x) {
assert(getSize() == x->getSize());
memcpy(data, x->data, getSize());
}
// Slicer: 1D, slice of linear data [start,end), (non-inplace)
// XXX: mem leak if forgot to delete
Tensor<Dtype>* slice(size_t start, size_t end) {
assert(end <= getSize()); assert(start <= end);
return new Tensor<Dtype>(end-start, data+start);
}
// Dumper: Dump all the data shipped by this tensor to the screen
// in a pretty format. The printing format is supported by the Julia
// interpreter so you can copy the data to Julia.
static inline void _edump(int x) { printf(" \x1b[36m% d\x1b[m", x); }
static inline void _edump(long x) { printf(" \x1b[36m% ld\x1b[m", x); }
static inline void _edump(float x) { printf(" \x1b[36m% .2f\x1b[m", x); }
static inline void _edump(double x) { printf(" \x1b[36m% .3lf\x1b[m", x); }
void dump() {
if (shape.size() == 0) {
std::cout << "[ ]" << std::endl << "Tensor(,)" << std::endl;
std::cout << "Tensor of name \"\x1b[36m" << name << "\x1b[m\", shape \x1b[36m(,)\x1b[m"
<< std::endl;
} else if (shape.size() == 1) {
std::cout << "[";
for (size_t i = 0; i < this->getSize(0); i++)
_edump(*this->at(i));
std::cout << " ]" << std::endl;
std::cout << "Tensor of name \"\x1b[36m" << name << "\x1b[m\", shape \x1b[36m("
<< this->getSize(0) << ",)\x1b[m" << std::endl;
} else if (shape.size() == 2) {
std::cout << "[" << std::endl;;
for (size_t i = 0; i < this->getSize(0); i++) {
std::cout << " [";
for (size_t j = 0; j < this->getSize(1); j++) {
_edump(*this->at(i,j));
}
std::cout << " ]" << std::endl;
}
std::cout << "]" << std::endl;
std::cout << "Tensor of name \"\x1b[36m" << name << "\x1b[m\", shape \x1b[36m("
<< this->getSize(0) << "," << this->getSize(1) << ")\x1b[m"
<< std::endl;
} else if (shape.size() == 3) {
std::cout << "[" << std::endl;
for (size_t chan = 0; chan < shape[0]; chan++) {
std::cout << " [" << std::endl;
for (size_t h = 0; h < shape[1]; h++) {
std::cout << " [";
for (size_t w = 0; w < shape[2]; w++) {
_edump(*this->at(chan,h,w));
}
std::cout << " ]" << std::endl;
}
std::cout << " ]," << std::endl;
}
std::cout << "]" << std::endl;
std::cout << "Tensor of name \"\x1b[36m" << name << "\x1b[m\", shape \x1b[36m("
<< this->getSize(0) << "," << this->getSize(1) << ","
<< getSize(2) << ")\x1b[m" << std::endl;
} else if (shape.size() == 4) {
std::cout << "[" << std::endl;
for (size_t t = 0; t < shape[0]; t++) {
std::cout << " [" << std::endl;
for (size_t c = 0; c < shape[1]; c++) {
std::cout << " [" << std::endl;
for (size_t h = 0; h < shape[2]; h++) {
std::cout << " [";
for (size_t w = 0; w < shape[3]; w++) {
_edump(*this->at(t,c,h,w));
}
std::cout << " ]" << std::endl;
}
std::cout << " ]," << std::endl;
}
std::cout << " ]," << std::endl;
}
std::cout << "]" << std::endl;
std::cout << "Tensor of name \"\x1b[36m" << name << "\x1b[m\", shape \x1b[36m("
<< this->getSize(0) << "," << this->getSize(1) << ","
<< getSize(2) << "," << getSize(3) << ")\x1b[m" << std::endl;
} else {
fprintf(stderr, "Tensor<*>::dump() for %ld-D tensor not implemented.\n", getDim());
}
}
//ut 4d tensor dump
//> Tensor<double> X (2,3,5,5); X.rand_(); X.dump();
//
//ut float tensor, long tensor, int tensor dump
//> Tensor<int> Xint (2,2); Xint.dump();
//> Tensor<long> Xlong (2,2); Xlong.dump();
//> Tensor<float> Xf32 (2,2); Xf32.dump();
//> Tensor<double> Xf64 (2,2); Xf64.dump();
// getDimension: common
size_t getDim(void) const {
return shape.size();
}
// getSize: common, size of the linear memory
size_t getSize(void) const {
if (shape.empty()) return 0;
size_t size = 1;
for (auto i: shape) size *= i;
return size;
}
// getSize: common, the i-th dimension
size_t getSize(size_t i) const {
return (i >= shape.size()) ? -1 : shape[i];
}
// resetMem: common, reset the linear memory space
void resetMem() {
if (data != nullptr) free(data);
data = nullptr;
data = (Dtype*)malloc(sizeof(Dtype)*getSize());
memset(data, 0x0, sizeof(Dtype)*getSize());
}
// Resizer: resize this instance from *D to 1D
Tensor<Dtype>* resize(size_t length) {
shape.clear();
shape.push_back(length);
resetMem();
return this;
}
//ut resize from empty tensor
//> Tensor<double> empty;
//> empty.resize(10);
//> empty.dump();
//> empty.resize(10, 10);
//> empty.dump();
//> empty.resize(1);
//> empty.dump();
//> empty.resize(0);
// Resizer: resize this instance from *D to 2D
Tensor<Dtype>* resize(size_t row, size_t col) {
shape.clear();
for (auto i : {row, col}) shape.push_back(i);
resetMem();
return this;
}
// Resizer: resize this instance from *D to 3D
Tensor<Dtype>* resize(size_t c, size_t h, size_t w) {
shape.clear();
for (auto i : {c, h, w}) shape.push_back(i);
resetMem();
return this;
}
// Resizer: resize this instance from *D to 4D
Tensor<Dtype>* resize(size_t t, size_t c, size_t h, size_t w) {
shape.clear();
for (auto i : {t, c, h, w}) shape.push_back(i);
resetMem();
return this;
}
// Resizer: resize this instance from *D to *D
Tensor<Dtype>* resize(std::vector<size_t>& sz) {
shape.clear();
for (auto i : sz) shape.push_back(i);
resetMem();
return this;
}
// Resizer: resize this instance as another instance
Tensor<Dtype>* resizeAs(Tensor<Dtype>* x) {
this->resize(x->shape);
return this;
}
// Resizer: expand: repeat a vector for many times
// (d,)->(d,n), (d,1)->(d,n)
// optional transpose, (d,)->(n,d), (d,1)->(n,d)
// NOTE: don't forget to delete
Tensor<Dtype>* expand(size_t N, bool trans = false) {
size_t D = this->getSize();
auto y = new Tensor<Dtype> ();
if (!trans) { // no trans
y->resize(D, N);
for (size_t i = 0; i < D; i++)
for (size_t j = 0; j < N; j++)
*y->at(i,j) = data[i];
} else { // trans == true
y->resize(N, D);
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < D; j++)
*y->at(i,j) = data[j];
}
return y;
}
//ut expand w or w/o transpose
//> Tensor<double> x (5); x.rand_(); x.dump();
//> auto y = x.expand(3); y->dump(); delete y;
//> auto z = x.expand(3, true); z->dump(); delete z;
// Resizer: unexpand: fold a matrix into a vector
// axis = 1 -> (d,n) -> (d,)
// axis = 0 -> (n,d) -> (d,)
Tensor<Dtype>* unexpand(size_t axis = 1) {
size_t D = (axis==1) ? shape[0] : shape[1];
size_t N = (axis==1) ? shape[1] : shape[0];
auto y = new Tensor<Dtype> (D); y->zero_();
if (axis == 1) {
for (size_t i = 0; i < D; i++)
for (size_t j = 0; j < N; j++)
*y->at(i) += *at(i,j);
} else if (axis == 0) {
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < D; j++)
*y->at(j) += *at(i,j);
} else {
assert(false);
}
return y;
}
//ut unexpand by different axis
//> Tensor<double> x (3,5); x.rand_(); x.dump();
//> auto y = x.unexpand(1); y->dump(); delete y;
//> auto z = x.unexpand(0); z->dump(); delete z;
// Filler: inplace, fill with zero for *D tensor
Tensor<Dtype>* zero_() {
memset(data, 0x0, sizeof(Dtype)*getSize());
return this;
}
// Filler: inplace, fill with constant for *D tensor
Tensor<Dtype>* fill_(Dtype value) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data,value)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) = (Dtype) value;
return this;
}
//ut inplace fill
//> Tensor<double> ones;
//> ones.resize(10, 10)->fill_(4.2);
//> ones.dump();
// BLAS: inplace, scaling by a factor for *D tensor
Tensor<Dtype>* scal_(Dtype factor) {
llas::scal(getSize(), factor, data, 1);
return this;
}
//ut inplace scal
//> Tensor<double> ones;
//> ones.resize(10, 10)->fill_(4.2);
//> ones.scal_(0.5);
//> ones.dump();
// BLAS: inplace, *D, add constant to tensor
void add_(Dtype constant) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data,constant)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) += constant;
}
// BLAS: inplace, *D, add *D tensor to *D tensor
void add_(Tensor<Dtype>* X) {
assert(getSize() == X->getSize());
llas::axpy(getSize(), (Dtype)1., X->data, 1, data, 1);
}
// BLAS: SUM, *D, sum_i x_i
Dtype sum(void) {
Dtype ret = 0.;
#if defined(USE_OPENMP)
#pragma omp parallel for reduction (+:ret)
#endif
for (size_t i = 0; i < getSize(); i++)
ret += *at(i);
return ret;
}
// BLAS: ASUM, *D, sum_i |x_i|
Dtype asum(void) {
return llas::asum(getSize(), data, 1);
}
// BLAS, inplace, *D, y_i = exp(x_i)
Tensor<Dtype>* exp_(void) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) = std::exp(*(data + i));
return this;
}
// MISC: inplace Sqrt, *D, x_i <- sqrt(x_i)
Tensor<Dtype>* sqrt_(void) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) = std::sqrt(*(data + i));
return this;
}
//ut inplace sqrt
//> Tensor<double> x (10, 10); x.rand_(); x.sqrt_();
// MISC: inplace, *D, rand ~U[0.0, 1.0)
Tensor<Dtype>* rand_(void) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) = drand48();
return this;
}
//ut inplace random
//> Tensor<double> x (5, 10);
//> x.rand_();
//> x.dump();
// MISC: inplace, *D, rand ~U[l, u)
Tensor<Dtype>* uniform_(Dtype l, Dtype u) {
#if defined(USE_OPENMP)
#pragma omp parallel for shared(data, l, u)
#endif
for (size_t i = 0; i < getSize(); i++)
*(data + i) = drand48() * (u-l) + l;
return this;
}
//ut uniform_
//> Tensor<double> x (10, 10);
//> x.uniform_(-10, 10);
//> x.dump();
// MISC: MAE: y <- sum_i ||a_i - b_i||_1
Dtype MAE(Tensor<Dtype>* B) {
assert(this->getSize() == B->getSize());
Dtype ret = 0.;
size_t size = this->getSize();
#if defined(USE_OPENMP)
#pragma omp parallel for reduction (+:ret) shared(size)
#endif
for (size_t i = 0; i < size; i++) {
Dtype tmp = *this->at(i) - *B->at(i);
ret += (tmp > (Dtype)0.) ? tmp : -tmp;
}
return ret / (Dtype)this->getSize();
}
// MISC: MSE: y <- sum_i ||a_i - b_i||_2^2
Dtype MSE(Tensor<Dtype>* B) {
assert(this->getSize() == B->getSize());
Dtype ret = 0.;
size_t size = this->getSize();
#if defined(USE_OPENMP)
#pragma omp parallel for reduction (+:ret) shared(size)
#endif
for (size_t i = 0; i < size; i++) {
Dtype tmp = *this->at(i) - *B->at(i);
ret += tmp * tmp;
}
return ret / (Dtype)this->getSize();
}
// MISC: rot180, non-inplace
// XXX: don't forget to delete!
Tensor<Dtype>* rot180(void) {
Tensor<Dtype>* rot = this->clone();
auto eswap = [](Dtype* a, Dtype* b) {
Dtype tmp = *a; *a = *b; *b = tmp;
};
if (getDim() == 1 || getDim() == 2) {
size_t curl = 0, curr = rot->getSize()-1;
while (curl < curr) eswap(rot->at(curl++), rot->at(curr--));
} else if (getDim() == 3) {
for (size_t c = 0; c < shape[0]; c++) {
size_t HxW = rot->shape[1] * rot->shape[2];
size_t curl = c * HxW, curr = (c+1) * HxW - 1;
while (curl < curr) eswap(rot->at(curl++), rot->at(curr--));
}
} else if (getDim() == 4) {
for (size_t t = 0; t < shape[0]; t++) {
size_t CxHxW = rot->shape[1] * rot->shape[2] * rot->shape[3];
for (size_t c = 0; c < shape[1]; c++) {
size_t HxW = rot->shape[2] * rot->shape[3];
size_t curl = t*CxHxW + c*HxW;
size_t curr = t*CxHxW + (c+1)*HxW - 1;
while (curl < curr) eswap(rot->at(curl++), rot->at(curr--));
}
}
} else {
fprintf(stderr, "Tensor::rot180 not implemented for %ld-D tensor!\n", getDim());
exit(EXIT_FAILURE);
}
return rot;
}
//ut rot180
//> Tensor<double> X (10); X.rand_(); X.dump();
//> auto xx = X.rot180(); xx->dump(); delete xx;
//> X.resize(5, 5); X.rand_(); X.dump();
//> xx = X.rot180(); xx->dump(); delete xx;
//> X.resize(3, 5, 5); X.rand_(); X.dump();
//> xx = X.rot180(); xx->dump(); delete xx;
//> X.resize(2,2,5,5); X.rand_(); X.dump();
//> xx = X.rot180(); xx->dump(); delete xx;
// Clone: deep copy of this Tensor
// XXX: mem leaks if forgot to delete!
Tensor<Dtype>* clone(void) {
Tensor<Dtype>* y = new Tensor<Dtype> ();
y->resizeAs(this);
//#pragma omp parallel for // slower than memcpy
//for (size_t i = 0; i < getSize(); i++) *(y->data + i) = *(data + i);
memcpy(y->data, this->data, sizeof(Dtype)*this->getSize()); // BEST
return y;
}
//ut clone
//> Tensor<double> x (10, 10);
//> x.rand_();
//> x.dump();
//> Tensor<double>* y = x.clone();
//> y->dump();
//> cout << &x << " " << y << endl;
//> delete y;
// MISC: sign: apply the sign function to the tensor
// XXX: remember to delete the created tensor.
Tensor<Dtype>* sign(void) {
Tensor<Dtype>* y = this->clone();
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif
for (size_t i = 0; i < getSize(); i++)
y->data[i] = (y->data[i] > 0.) ? 1.
: (y->data[i] < 0.) ? -1. : 0.;
return y;
}
// Transpose: 2D transpose, non-inplace
// XXX: don't forget to delete
Tensor<Dtype>* transpose(void) {
if (shape.size() != 2) {
fprintf(stderr, "transpose(): ERROR: shape.size = %ld\n", shape.size());
exit(EXIT_FAILURE);
}
auto xT = new Tensor<Dtype> ((size_t)shape[1], (size_t)shape[0]);
#if defined(USE_OPENMP)
#pragma omp parallel for collapse(2)
#endif
for (size_t i = 0; i < shape[0]; i++)
for (size_t j = 0; j < shape[1]; j++)
*xT->at(j, i) = *at(i, j);
return xT;
}
// Transpose: 2D transpose in-place
void transpose_(bool ushape=true) {
assert(getDim() == 2);
size_t oldcol = shape[1];
size_t newrow = shape[1], newcol = shape[0];
vector<bool> visited (getSize(), false);
Dtype c = 0., t = 0.;
for (size_t n = 0; n < getSize(); n++) {
if (visited[n]) continue;
visited[n] = true;
size_t srcrow = n / oldcol, srccol = n % oldcol;
size_t dstrow = srccol, dstcol = srcrow;
size_t next = dstrow * newcol + dstcol;
c = *(data + n);
while (!visited[next]) {
visited[next] = true;
t = *(data + next); *(data + next) = c; c = t;
srcrow = next / oldcol, srccol = next % oldcol;
dstrow = srccol, dstcol = srcrow;
next = dstrow * newcol + dstcol;
}
*(data + n) = c;
}
if (ushape) { shape[0] = newrow; shape[1] = newcol; }
}
//ut tensor transpose inplace
//> Tensor<double> X (5,3); X.rand_(); X.dump();
//> X.transpose_(); X.dump();
// DEBUG: compares size of two tensors
bool sameSize(Tensor<Dtype>* x) {
if (x->getDim() != getDim()) return false;
for (size_t i = 0; i < shape.size(); i++)
if (x->getSize(i) != getSize(i)) return false;
return true;
}
//ut int sameSize
//> Tensor<int> x (10, 10);
//> Tensor<int> y (1);
//> Tensor<int> z (10, 11);
//> assert(x.sameSize(&x) == true);
//> assert(x.sameSize(&y) == false);
//> assert(x.sameSize(&z) == false);
// I/O: save *D tensor to ASCII-encoded json file
void save(string fname) {
std::ofstream f (fname); assert(f);
Json::Value jroot;
Json::StyledWriter jwriter;
// fill in the json object
jroot["type"] = "Leicht::Tensor";
jroot["version"] = LEICHT_VERSION;
jroot["name"] = this->name;
for (auto i : this->shape) jroot["shape"].append((const Json::Value::UInt64)i);
for (size_t i = 0; i < getSize(); i++)
jroot["data"].append((double)data[i]);
// write
f << jwriter.write(jroot) << endl;
f.close();
}
//ut 3D tensor creation and save
//> Tensor<double> x (3, 6, 6);
//> x.rand_();
//> x.dump();
//> x.save("test.leicht");
// I/O: load *D tensor from ASCII-encoded json file
void load(string fname) {
Json::Value jroot;
Json::Reader jreader;
std::fstream f (fname, ios::in); assert(f);
jreader.parse(f, jroot);
assert(jroot["type"] == "Leicht::Tensor");
// read data in
this->name = jroot["name"].asString();
std::vector<size_t> sz;
for (size_t i = 0; i < jroot["shape"].size(); i++)
sz.push_back(jroot["shape"][(int)i].asUInt64());
this->resize(sz);
for (size_t j = 0; j < jroot["data"].size(); j++)
data[j] = jroot["data"][(int)j].asDouble();
// cleanup
f.close();
}
//ut 3D tensor load
//> Tensor<double> x;
//> x.load("test.leicht");
//> x.dump();
// Shortcut: operator += in-place addition, element-wise
void operator+= (Tensor<Dtype>& x) { Tensor<Dtype>::axpy((Dtype)1.,&x,this); }
//ut operator +=
//> Tensor<double> x (5,5); x.fill_(2.1); x.dump(); x += x; x.dump();
// Shortcut: operator -= in-place subtraction, element-wise
void operator-= (Tensor<Dtype>& x) { Tensor<Dtype>::axpy((Dtype)-1.,&x,this); }
//ut operator -=
//> Tensor<double> x (5,5); x.fill_(2.1); x.dump(); x -= x; x.dump();
// Shortcut: operator *= in-place multiplication, element-wise
void operator*= (Tensor<Dtype>& x) {
assert(x.getSize() == this->getSize());
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif
for (size_t i = 0; i < this->getSize(); i++) *at(i) *= *x.at(i);
}
//ut operator *=
//> Tensor<double> x (5,5); x.fill_(2.1); x.dump(); x *= x; x.dump();
// Shortcut: operator /= in-place division, element-wise
// XXX: Be care of the division-by-zero issue.
void operator/= (Tensor<Dtype>& x) {
assert(x.getSize() == this->getSize());
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif
for (size_t i = 0; i < this->getSize(); i++) *at(i) /= *x.at(i);
}
//ut operator /=
//> Tensor<double> x (5,5); x.fill_(2.1); x.dump(); x /= x; x.dump();
// Shortcut: operator + create new tensor c, add a and b to it
// XXX: don't forget to delete
Tensor<Dtype>* operator+ (Tensor<Dtype>& a) {
auto ret = this->clone(); *ret += a; return ret;
}
//ut operator +
//> Tensor<double> x (5,5); x.fill_(2.1); x.dump();
//> Tensor<double> y (5,5); y.fill_(4.2); y.dump();
//> auto z = x + y; z->setName("z=x+y"); z->dump(); delete z;
// Shortcut: operator > create mask tensor where this_i > a
// XXX: don't forget to delete
Tensor<Dtype>* operator> (Dtype a) {
auto ret = this->clone();
#if defined(USE_OPENMP)
#pragma omp parallel for
#endif
for (size_t i = 0; i < this->getSize(); i++) *ret->at(i) = (Dtype)(*at(i) > a);
return ret;
}
//ut operator > scalar
//> Tensor<double> x (5,5); x.rand_()->add_(-.5); x.dump();
//> auto y = x > 0.; y->dump(); delete y;
// LEVEL1 BLAS: AXPY (Tensor)