-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.hpp
1475 lines (1274 loc) · 49.3 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
#pragma once
#ifndef TENSOR_H
#define TENSOR_H
#include <array>
#include <functional>
#include <iostream>
#include <torch/torch.h>
namespace evol {
///
///
///
/// DEVICE
///
///
///
namespace device {
#define DEF_DEVICE(name, value, default) \
template <int64_t N = default> struct name { \
static constexpr torch::DeviceType DEVICE = value; \
static constexpr int64_t INDEX = N; \
};
DEF_DEVICE(CPU, torch::kCPU, 0);
DEF_DEVICE(CUDA, torch::kCUDA, 0);
DEF_DEVICE(HIP, torch::kHIP, 0);
DEF_DEVICE(FPGA, torch::kFPGA, 0);
DEF_DEVICE(MAIA, torch::kMAIA, 0);
DEF_DEVICE(XLA, torch::kXLA, 0);
DEF_DEVICE(MPS, torch::kMPS, 0);
DEF_DEVICE(Meta, torch::kMeta, 0);
DEF_DEVICE(Vulkan, torch::kVulkan, 0);
DEF_DEVICE(Metal, torch::kMetal, 0);
DEF_DEVICE(XPU, torch::kXPU, 0);
DEF_DEVICE(HPU, torch::kHPU, 0);
DEF_DEVICE(VE, torch::kVE, 0);
DEF_DEVICE(Lazy, torch::kLazy, 0);
DEF_DEVICE(IPU, torch::kIPU, 0);
DEF_DEVICE(MTIA, torch::kMTIA, 0);
DEF_DEVICE(PrivateUse1, torch::kPrivateUse1, 0);
DEF_DEVICE(OPENGL, torch::DeviceType::OPENGL, 0);
DEF_DEVICE(OPENCL, torch::DeviceType::OPENCL, 0);
DEF_DEVICE(IDEEP, torch::DeviceType::IDEEP, 0);
DEF_DEVICE(MKLDNN, torch::DeviceType::MKLDNN, 0);
#undef DEF_DEVICE
} // namespace device
template <int64_t N = 0> using CPU = device::CPU<N>;
template <int64_t N = 0> using CUDA = device::CUDA<N>;
template <int64_t N = 0> using HIP = device::HIP<N>;
template <int64_t N = 0> using FPGA = device::FPGA<N>;
template <int64_t N = 0> using MAIA = device::MAIA<N>;
template <int64_t N = 0> using XLA = device::XLA<N>;
template <int64_t N = 0> using MPS = device::MPS<N>;
template <int64_t N = 0> using Meta = device::Meta<N>;
template <int64_t N = 0> using Vulkan = device::Vulkan<N>;
template <int64_t N = 0> using Metal = device::Metal<N>;
template <int64_t N = 0> using XPU = device::XPU<N>;
template <int64_t N = 0> using HPU = device::HPU<N>;
template <int64_t N = 0> using VE = device::VE<N>;
template <int64_t N = 0> using Lazy = device::Lazy<N>;
template <int64_t N = 0> using IPU = device::IPU<N>;
template <int64_t N = 0> using MTIA = device::MTIA<N>;
template <int64_t N = 0> using PrivateUse1 = device::PrivateUse1<N>;
template <int64_t N = 0> using OPENGL = device::OPENGL<N>;
template <int64_t N = 0> using OPENCL = device::OPENCL<N>;
template <int64_t N = 0> using IDEEP = device::IDEEP<N>;
template <int64_t N = 0> using MKLDNN = device::MKLDNN<N>;
///
///
///
/// DTYPE
///
///
///
namespace dtype {
#define DEF_DTYPE(name, value, size) \
struct alignas(size) name { \
static constexpr torch::Dtype DTYPE = torch::value; \
static constexpr size_t SIZE = size; \
};
DEF_DTYPE(i8, kInt8, 1);
DEF_DTYPE(i16, kInt16, 2);
DEF_DTYPE(i32, kInt32, 4);
DEF_DTYPE(i64, kInt64, 8);
DEF_DTYPE(u8, kUInt8, 1);
DEF_DTYPE(u16, kUInt16, 2);
DEF_DTYPE(u32, kUInt32, 4);
DEF_DTYPE(u64, kUInt64, 8);
DEF_DTYPE(f16, kFloat16, 2);
DEF_DTYPE(f32, kFloat32, 4);
DEF_DTYPE(f64, kFloat64, 8);
DEF_DTYPE(f128, kLong, 16);
DEF_DTYPE(byte, kByte, 1);
DEF_DTYPE(c16, kComplexHalf, 4);
DEF_DTYPE(c32, kComplexFloat, 8);
DEF_DTYPE(c64, kComplexDouble, 16);
DEF_DTYPE(q8, kQInt8, 1);
DEF_DTYPE(q32, kQInt32, 2);
DEF_DTYPE(q2x4, kQUInt2x4, 1);
DEF_DTYPE(q4x2, kQUInt4x2, 1);
DEF_DTYPE(bits8, kBits8, 1);
DEF_DTYPE(bits16, kBits16, 2);
DEF_DTYPE(bits2x4, kBits2x4, 1);
DEF_DTYPE(bits4x2, kBits4x2, 1);
DEF_DTYPE(bits1x8, kBits1x8, 1);
#undef DEF_DTYPE
template <typename T> struct alignas(sizeof(T)) DType;
#define DEF_DTYPE_IMPL(name) \
template <> struct alignas(name::SIZE) DType<name> { \
static constexpr torch::Dtype DTYPE = name::DTYPE; \
static constexpr size_t SIZE = name::SIZE; \
};
#define DEF_DTYPE_IMPL_BASE(name, val) \
template <> struct alignas(sizeof(name)) DType<name> { \
static constexpr torch::Dtype DTYPE = torch::val; \
static constexpr size_t SIZE = sizeof(name); \
};
DEF_DTYPE_IMPL(i8);
DEF_DTYPE_IMPL(i16);
DEF_DTYPE_IMPL(i32);
DEF_DTYPE_IMPL(i64);
DEF_DTYPE_IMPL(u8);
DEF_DTYPE_IMPL(u16);
DEF_DTYPE_IMPL(u32);
DEF_DTYPE_IMPL(u64);
DEF_DTYPE_IMPL(f16);
DEF_DTYPE_IMPL(f32);
DEF_DTYPE_IMPL(f64);
DEF_DTYPE_IMPL(f128);
DEF_DTYPE_IMPL(byte);
DEF_DTYPE_IMPL(c16);
DEF_DTYPE_IMPL(c32);
DEF_DTYPE_IMPL(c64);
DEF_DTYPE_IMPL(q8);
DEF_DTYPE_IMPL(q32);
DEF_DTYPE_IMPL(q2x4);
DEF_DTYPE_IMPL(q4x2);
DEF_DTYPE_IMPL(bits8);
DEF_DTYPE_IMPL(bits16);
DEF_DTYPE_IMPL(bits2x4);
DEF_DTYPE_IMPL(bits4x2);
DEF_DTYPE_IMPL(bits1x8);
#undef DEF_DTYPE_IMPL
DEF_DTYPE_IMPL_BASE(bool, kBool);
DEF_DTYPE_IMPL_BASE(char, kChar);
DEF_DTYPE_IMPL_BASE(int, kInt);
DEF_DTYPE_IMPL_BASE(int8_t, kInt8);
DEF_DTYPE_IMPL_BASE(int16_t, kInt16);
DEF_DTYPE_IMPL_BASE(int64_t, kInt64);
DEF_DTYPE_IMPL_BASE(unsigned int, kUInt32);
DEF_DTYPE_IMPL_BASE(uint8_t, kUInt8);
DEF_DTYPE_IMPL_BASE(uint16_t, kUInt16);
DEF_DTYPE_IMPL_BASE(uint64_t, kUInt64);
DEF_DTYPE_IMPL_BASE(float, kFloat);
DEF_DTYPE_IMPL_BASE(double, kDouble);
DEF_DTYPE_IMPL_BASE(long double, kLong);
DEF_DTYPE_IMPL_BASE(long long, kLong);
DEF_DTYPE_IMPL_BASE(unsigned long long, kLong);
#undef DEF_DTYPE_IMPL_BASE
}; // namespace dtype
using i8 = dtype::i8;
using i16 = dtype::i16;
using i32 = dtype::i32;
using i64 = dtype::i64;
using u8 = dtype::u8;
using u16 = dtype::u16;
using u32 = dtype::u32;
using u64 = dtype::u64;
using f16 = dtype::f16;
using f32 = dtype::f32;
using f64 = dtype::f64;
using f128 = dtype::f128;
using byte = dtype::byte;
using c16 = dtype::c16;
using c32 = dtype::c32;
using c64 = dtype::c64;
using q8 = dtype::q8;
using q32 = dtype::q32;
using q2x4 = dtype::q2x4;
using q4x2 = dtype::q4x2;
using bits8 = dtype::bits8;
using bits16 = dtype::bits16;
using bits2x4 = dtype::bits2x4;
using bits4x2 = dtype::bits4x2;
using bits1x8 = dtype::bits1x8;
///
/// TensorPair
///
template <int64_t N, int64_t D> struct TensorPair {
static constexpr int64_t K = N;
static constexpr int64_t DIM = D;
};
template <int64_t K, int64_t Dim> using Tp = TensorPair<K, Dim>;
///
/// TensorRange
///
template <int64_t S, int64_t E, int64_t D> struct TensorRange {
static constexpr int64_t START = S;
static constexpr int64_t END = E;
static constexpr int64_t DIM = D;
};
template <int64_t Start, int64_t End, int64_t Dim>
using Tr = TensorRange<Start, End, Dim>;
///
///
///
/// SHAPE
///
///
///
template <int64_t... Dims> struct Shape;
template <> struct Shape<> {
static constexpr int64_t DIMS = 0;
static constexpr int64_t NELEMS = 0;
static constexpr std::array<int64_t, DIMS> SHAPE_DIMS = {};
template <int64_t... NewDims> using prepend = Shape<NewDims...>;
template <int64_t... NewDims> using append = Shape<NewDims...>;
};
template <int64_t First> struct Shape<First> {
static constexpr int64_t DIMS = 1;
static constexpr int64_t NELEMS = First;
static constexpr int64_t LAST = First;
static constexpr std::array<int64_t, DIMS> SHAPE_DIMS = {First};
template <int64_t... NewDims> using prepend = Shape<NewDims..., First>;
template <int64_t... NewDims> using append = Shape<First, NewDims...>;
};
template <int64_t First, int64_t Second> struct Shape<First, Second> {
static constexpr int64_t DIMS = 2;
static constexpr int64_t NELEMS = First * Second;
static constexpr int64_t LAST = Second;
static constexpr int64_t PENULTIMATE = First;
static constexpr std::array<int64_t, DIMS> SHAPE_DIMS = {First, Second};
using AllButLastTwo = Shape<>;
template <int64_t... NewDims>
using prepend = Shape<NewDims..., First, Second>;
template <int64_t... NewDims> using append = Shape<First, Second, NewDims...>;
};
template <int64_t First, int64_t Second, int64_t... Rest>
struct Shape<First, Second, Rest...> {
static constexpr int64_t DIMS = 2 + sizeof...(Rest);
static constexpr int64_t NELEMS = First * Second * Shape<Rest...>::NELEMS;
static constexpr int64_t LAST = Shape<Rest...>::LAST;
static constexpr int64_t PENULTIMATE = Shape<Second, Rest...>::PENULTIMATE;
static constexpr std::array<int64_t, DIMS> SHAPE_DIMS = {First, Second,
Rest...};
using AllButLastTwo =
typename Shape<Second, Rest...>::AllButLastTwo::template prepend<First>;
template <int64_t... NewDims>
using prepend = Shape<NewDims..., First, Second, Rest...>;
template <int64_t... NewDims>
using append = Shape<First, Second, Rest..., NewDims...>;
};
///
/// IndicesPair
///
template <int64_t D, int64_t... I> struct IndicesPair {
static constexpr int64_t DIM = D;
using Indices = Shape<I...>;
};
template <int64_t Dim, int64_t... Indices>
using Ip = IndicesPair<Dim, Indices...>;
///
///
///
/// UTILS
///
///
///
///
/// IsSamePack
///
template <typename P1, typename P2> struct IsSameAsPack;
template <int64_t First1, int64_t First2>
struct IsSameAsPack<Shape<First1>, Shape<First2>>
: std::__bool_constant<First1 == First2> {};
template <int64_t First1, int64_t... Rest1, int64_t First2, int64_t... Rest2>
struct IsSameAsPack<Shape<First1, Rest1...>, Shape<First2, Rest2...>>
: std::__bool_constant<
(First1 == First2) &&
IsSameAsPack<Shape<Rest1...>, Shape<Rest2...>>::value> {};
///
/// IsCatCompatible
///
template <int Idx, typename P1, typename P2> struct IsCatCompatible;
template <int64_t First1, int64_t First2>
struct IsCatCompatible<0, Shape<First1>, Shape<First2>> : std::true_type {};
template <int64_t First1, int64_t... Rest1, int64_t First2, int64_t... Rest2>
struct IsCatCompatible<0, Shape<First1, Rest1...>, Shape<First2, Rest2...>>
: std::__bool_constant<
IsSameAsPack<Shape<Rest1...>, Shape<Rest2...>>::value> {};
template <int Idx, int64_t First1, int64_t... Rest1, int64_t First2,
int64_t... Rest2>
struct IsCatCompatible<Idx, Shape<First1, Rest1...>, Shape<First2, Rest2...>>
: std::__bool_constant<
First1 == First2 &&
IsCatCompatible<Idx - 1, Shape<Rest1...>, Shape<Rest2...>>::value> {};
///
/// SetDimAtIdx
///
template <int64_t Idx, int64_t Dim, typename T> struct SetDimAtIdx;
template <int64_t Dim, int64_t First, int64_t... Dims>
struct SetDimAtIdx<0, Dim, Shape<First, Dims...>> {
using type = Shape<Dim, Dims...>;
};
template <int64_t Idx, int64_t Dim, int64_t First, int64_t... Rest>
struct SetDimAtIdx<Idx, Dim, Shape<First, Rest...>> {
static_assert(Idx <= sizeof...(Rest), "\nError: Index out of bounds.\n");
using type =
typename SetDimAtIdx<Idx - 1, Dim,
Shape<Rest...>>::type::template prepend<First>;
};
///
/// IsNotInPack
///
template <int64_t N, typename T> struct IsNotInPack;
template <int64_t N, int64_t First>
struct IsNotInPack<N, Shape<First>> : std::__bool_constant<N != First> {};
template <int64_t N, int64_t First, int64_t... Rest>
struct IsNotInPack<N, Shape<First, Rest...>>
: std::__bool_constant<N != First &&
IsNotInPack<N, Shape<Rest...>>::value> {};
///
/// AreAllUnique
///
template <typename T> struct AreAllUnique;
template <> struct AreAllUnique<Shape<>> : std::true_type {};
template <int64_t First> struct AreAllUnique<Shape<First>> : std::true_type {};
template <int64_t First, int64_t... Rest>
struct AreAllUnique<Shape<First, Rest...>>
: std::__bool_constant<IsNotInPack<First, Shape<Rest...>>::value &&
AreAllUnique<Shape<Rest...>>::value> {};
///
/// AreAllLessThanN
///
template <int64_t N, typename T> struct AreAllLessThanN;
template <int64_t N> struct AreAllLessThanN<N, Shape<>> : std::true_type {};
template <int64_t N, int64_t First>
struct AreAllLessThanN<N, Shape<First>> : std::__bool_constant < First<N> {
};
template <int64_t N, int64_t First, int64_t... Rest>
struct AreAllLessThanN<N, Shape<First, Rest...>>
: std::__bool_constant <
First<N && AreAllLessThanN<N, Shape<Rest...>>::value> {};
///
/// AreAllSameAs
///
template <typename As, typename... T> struct AreAllSameAs;
template <typename As, typename First>
struct AreAllSameAs<As, First>
: std::__bool_constant<std::is_same<As, First>::value> {};
template <typename As, typename First, typename... Rest>
struct AreAllSameAs<As, First, Rest...>
: std::__bool_constant<std::is_same<As, First>::value &&
AreAllSameAs<As, Rest...>::value> {};
///
/// AreAllInIncreasingOrder
///
template <int64_t... Dims> struct AreAllInIncreasingOrder;
template <int64_t First>
struct AreAllInIncreasingOrder<First> : std::true_type {};
template <int64_t First, int64_t Second>
struct AreAllInIncreasingOrder<First, Second> : std::__bool_constant <
First<Second> {};
template <int64_t First, int64_t Second, int64_t... Rest>
struct AreAllInIncreasingOrder<First, Second, Rest...>
: std::__bool_constant <
First<Second && AreAllInIncreasingOrder<Second, Rest...>::value> {};
///
/// AreTensorRangesAllInDecreasingOrder
///
template <typename... TensorRanges> struct AreTensorRangesAllInDecreasingOrder;
template <typename First>
struct AreTensorRangesAllInDecreasingOrder<First> : std::true_type {};
template <typename First, typename Second>
struct AreTensorRangesAllInDecreasingOrder<First, Second>
: std::__bool_constant < Second::DIM<First::DIM> {};
template <typename First, typename Second, typename... Rest>
struct AreTensorRangesAllInDecreasingOrder<First, Second, Rest...>
: std::__bool_constant <
Second::DIM<First::DIM &&
AreTensorRangesAllInDecreasingOrder<Second, Rest...>::value> {
};
///
/// AreTensorRangesAllInIncreasingOrder
///
template <typename... TensorRanges> struct AreTensorRangesAllInIncreasingOrder;
template <typename First>
struct AreTensorRangesAllInIncreasingOrder<First> : std::true_type {};
template <typename First, typename Second>
struct AreTensorRangesAllInIncreasingOrder<First, Second>
: std::__bool_constant < First::DIM<Second::DIM> {};
template <typename First, typename Second, typename... Rest>
struct AreTensorRangesAllInIncreasingOrder<First, Second, Rest...>
: std::__bool_constant <
First::DIM<Second::DIM &&
AreTensorRangesAllInDecreasingOrder<Second, Rest...>::value> {
};
///
/// AreTensorPairDimsUnique
///
template <typename... RepeatPairs> struct AreTensorPairDimsUnique;
template <typename First>
struct AreTensorPairDimsUnique<First> : std::true_type {};
template <typename First, typename Second>
struct AreTensorPairDimsUnique<First, Second>
: std::__bool_constant<First::DIM != Second::DIM> {};
template <typename First, typename Second, typename... Rest>
struct AreTensorPairDimsUnique<First, Second, Rest...>
: std::__bool_constant<First::DIM != Second::DIM &&
AreTensorPairDimsUnique<Second, Rest...>::value> {};
///
///
///
/// SHAPE GENERATORS
///
///
///
///
/// CatArrayShape
///
template <typename S, int64_t Idx, int64_t Mul> struct CatArrayShape;
template <int64_t Mul> struct CatArrayShape<Shape<>, 0, Mul> {
using type = Shape<>;
};
template <int64_t First, int64_t Mul>
struct CatArrayShape<Shape<First>, 0, Mul> {
using type = Shape<First * Mul>;
};
template <int64_t First, int64_t... Rest, int64_t Mul>
struct CatArrayShape<Shape<First, Rest...>, 0, Mul> {
using type = Shape<First * Mul, Rest...>;
};
template <int64_t First, int64_t... Rest, int64_t Idx, int64_t Mul>
struct CatArrayShape<Shape<First, Rest...>, Idx, Mul> {
static_assert(Idx <= sizeof...(Rest), "Error: Index out of bounds.");
using type = typename CatArrayShape<Shape<Rest...>, Idx - 1,
Mul>::type::template prepend<First>;
};
//
// CatShape
//
template <int64_t Idx, typename ShapeType, typename... Shapes> struct CatShape;
template <int64_t Idx, typename ShapeType> struct CatShape<Idx, ShapeType> {
static_assert(Idx < ShapeType::DIMS, "Index out of bounds.");
};
template <int64_t Idx, typename ShapeType, typename FirstShape>
struct CatShape<Idx, ShapeType, FirstShape> {
static_assert(Idx < ShapeType::DIMS, "Index out of bounds.");
static_assert(FirstShape::DIMS == ShapeType::DIMS,
"Shapes must have the same number of dimensions.");
static_assert(
IsCatCompatible<Idx, ShapeType, FirstShape>::value,
"Shapes must have the same dimensions except for the one at Idx");
static constexpr int64_t new_dim =
ShapeType::SHAPE_DIMS[Idx] + FirstShape::SHAPE_DIMS[Idx];
using type = typename SetDimAtIdx<Idx, new_dim, ShapeType>::type;
};
template <int64_t Idx, typename ShapeType, typename FirstShape,
typename... RestShapes>
struct CatShape<Idx, ShapeType, FirstShape, RestShapes...> {
static_assert(Idx < ShapeType::DIMS, "Index out of bounds.");
static_assert(FirstShape::DIMS == ShapeType::DIMS,
"Shapes must have the same number of dimensions.");
static_assert(
IsCatCompatible<Idx, ShapeType, FirstShape>::value,
"Shapes must have the same dimensions except for the one at Idx");
static constexpr int64_t new_dim =
FirstShape::SHAPE_DIMS[Idx] +
CatShape<Idx, ShapeType, RestShapes...>::new_dim;
using type = typename SetDimAtIdx<Idx, new_dim, ShapeType>::type;
};
///
/// Matmul
///
template <typename S1, typename S2> struct MatmulShape {
static_assert(S1::DIMS == S2::DIMS,
"\nShape mismatch: To perform matrix multiplication both "
"shapes must have the same number of elements.\n");
static_assert(S1::DIMS > 1,
"\nShape mismatch: To perform matrix multiplication S1 "
"must have at least 2 dimensions\n");
static_assert(S2::DIMS > 1,
"\nShape mismatch: To perform matrix multiplication S2 "
"must have at least 2 dimensions\n");
static_assert(
std::is_same<typename S1::AllButLastTwo, typename S2::AllButLastTwo>(),
"\nShape mismatch: To perform matrix multiplication"
" all dimensions except the last two must be the same.\n");
static_assert(
S1::LAST == S2::PENULTIMATE,
"\nShape mismatch: To perform matrix multiplication"
"the last dimension of S1 must match the penultimate dimension of S2");
using type = typename S1::AllButLastTwo::template append<
S1::PENULTIMATE>::template append<S2::LAST>;
};
///
/// TransposeShape
///
template <typename S1, typename S2> struct TransposeShape;
template <int64_t First, typename S2> struct TransposeShape<Shape<First>, S2> {
static_assert(
First < S2::DIMS,
"\nError: The order contains too many items, must be == Shape::DIMS.\n");
using type = Shape<S2::SHAPE_DIMS[First]>;
};
template <int64_t First, int64_t... Rest, typename S2>
struct TransposeShape<Shape<First, Rest...>, S2> {
static_assert(
First < S2::DIMS,
"\nError: The order contains too many items, must be == Shape::DIMS.\n");
using type = typename TransposeShape<
Shape<Rest...>, S2>::type::template prepend<S2::SHAPE_DIMS[First]>;
};
///
/// Unsqueeze
///
template <int64_t Dim, typename S> struct UnsqueezeShape;
template <> struct UnsqueezeShape<0, Shape<>> {
using type = Shape<1>;
};
template <int64_t First, int64_t... Dims>
struct UnsqueezeShape<0, Shape<First, Dims...>> {
using type = Shape<1, First, Dims...>;
};
template <int64_t Dim, int64_t First, int64_t... Rest>
struct UnsqueezeShape<Dim, Shape<First, Rest...>> {
using type =
typename UnsqueezeShape<Dim - 1,
Shape<Rest...>>::type::template prepend<First>;
};
///
/// VariadicUnsqueeze
///
template <int64_t Curr, typename D, typename S> struct VariadicUnsqueezeShape;
template <int64_t Curr, int64_t First, typename S>
struct VariadicUnsqueezeShape<Curr, Shape<First>, S> {
using type = typename UnsqueezeShape<First + Curr, S>::type;
};
template <int64_t Curr, int64_t First, int64_t... Rest, typename S>
struct VariadicUnsqueezeShape<Curr, Shape<First, Rest...>, S> {
static_assert(AreAllInIncreasingOrder<First, Rest...>::value,
"\nThe provided dimensions must be in increasing order.\n");
using type = typename VariadicUnsqueezeShape<
Curr + 1, Shape<Rest...>,
typename UnsqueezeShape<First + Curr, S>::type>::type;
};
///
/// UnsqueezeDimShape
///
template <int64_t Dim, typename S> struct UnsqueezeDimShape;
template <typename S> struct UnsqueezeDimShape<0, S> {
using type = typename S::template prepend<1>;
};
template <int64_t Dim, typename S> struct UnsqueezeDimShape {
using type =
typename UnsqueezeDimShape<Dim - 1,
typename S::template prepend<1>>::type;
};
///
/// Squeeze
///
template <typename T> struct SqueezeShape;
template <> struct SqueezeShape<Shape<>> {
using type = Shape<>;
};
template <int64_t First> struct SqueezeShape<Shape<First>> {
using type =
typename std::conditional<First == 1, Shape<>, Shape<First>>::type;
};
template <int64_t First, int64_t... Rest>
struct SqueezeShape<Shape<First, Rest...>> {
using type = typename std::conditional<
First == 1, typename SqueezeShape<Shape<Rest...>>::type,
typename SqueezeShape<Shape<Rest...>>::type::template prepend<First>>::
type;
};
///
/// SqueezeShapeAtIdx
///
template <int64_t Idx, typename S> struct SqueezeShapeAtIdx;
template <int64_t First, int64_t... Rest>
struct SqueezeShapeAtIdx<0, Shape<First, Rest...>> {
using type = typename std::conditional<First == 1, Shape<Rest...>,
Shape<First, Rest...>>::type;
};
template <int64_t Idx, int64_t First, int64_t... Rest>
struct SqueezeShapeAtIdx<Idx, Shape<First, Rest...>> {
static_assert(Idx < Shape<First, Rest...>::DIMS,
"\nIdx must be < Shape::DIMS.\n");
using type =
typename SqueezeShapeAtIdx<Idx - 1,
Shape<Rest...>>::type::template prepend<First>;
};
///
/// VariadicSqueeze
///
template <int64_t Curr, typename T, typename S> struct VariadicSqueezeShape;
template <int64_t Curr, int64_t First, typename S>
struct VariadicSqueezeShape<Curr, Shape<First>, S> {
using type = typename SqueezeShapeAtIdx<First - Curr, S>::type;
};
template <int64_t Curr, int64_t First, int64_t... Rest, typename S>
struct VariadicSqueezeShape<Curr, Shape<First, Rest...>, S> {
static_assert(AreAllInIncreasingOrder<First, Rest...>::value,
"\nThe provided dimensions must be in increasing order.\n");
using type = typename std::conditional<
S::SHAPE_DIMS[First - Curr] == 1,
typename VariadicSqueezeShape<
Curr + 1, Shape<Rest...>,
typename SqueezeShapeAtIdx<First - Curr, S>::type>::type,
typename VariadicSqueezeShape<Curr, Shape<Rest...>,
S>::type::template prepend<First>>::type;
};
///
/// SwapDimsShape
///
template <int64_t I, int64_t J, typename S> struct SwapDimsShape {
static_assert(I < S::DIMS, "\nI must be < Shape::DIMS.\n");
static_assert(J < S::DIMS, "\nJ must be < Shape::DIMS.\n");
using type = typename SetDimAtIdx<
J, S::SHAPE_DIMS[I],
typename SetDimAtIdx<I, S::SHAPE_DIMS[J], S>::type>::type;
};
///
/// RepeatShape
///
template <typename S, typename... RepeatPairs> struct RepeatShape;
template <typename S, typename First> struct RepeatShape<S, First> {
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
using type =
typename SetDimAtIdx<First::Dim, S::SHAPE_DIMS[First::Dim] * First::K,
S>::type;
};
template <typename S, typename First, typename... Rest>
struct RepeatShape<S, First, Rest...> {
static_assert(
AreTensorPairDimsUnique<First, Rest...>::value &&
sizeof...(Rest) <= S::DIMS,
"\n.You don't need to repeat the same dimension more than once\n");
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
using type = typename RepeatShape<
typename SetDimAtIdx<First::Dim, S::SHAPE_DIMS[First::Dim] * First::K,
S>::type,
Rest...>::type;
};
///
/// TopShape
///
template <typename S, typename... TopPairs> struct TopShape;
template <typename S, typename First> struct TopShape<S, First> {
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(
First::K <= S::SHAPE_DIMS[First::Dim],
"\nError: K is > than the number of elements in the given dimension.\n");
using type = typename SetDimAtIdx<First::Dim, First::K, S>::type;
};
template <typename S, typename First, typename... Rest>
struct TopShape<S, First, Rest...> {
static_assert(AreTensorPairDimsUnique<First, Rest...>::value &&
sizeof...(Rest) <= S::DIMS,
"\n.You don't need to get the top elements of the same "
"dimension more than once\n");
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(
First::K <= S::SHAPE_DIMS[First::Dim],
"\nError: K is > than the number of elements in the given dimension.\n");
using type =
typename TopShape<typename SetDimAtIdx<First::Dim, First::K, S>::type,
Rest...>::type;
};
///
/// NarrowShape
///
template <typename S, typename... TensorRanges> struct NarrowShape;
template <typename S, typename First> struct NarrowShape<S, First> {
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(First::Start <= First::End,
"\nSlice::Start must be <= Slice::End.\n");
static_assert(First::End <= S::SHAPE_DIMS[First::Dim],
"\nSlice index out of bounds, End > Dim.\n");
static_assert(First::End - First::Start <= S::SHAPE_DIMS[First::Dim],
"\nError: The range size is > than the number of elements in "
"the given dimension.\n");
using type =
typename SetDimAtIdx<First::Dim, First::End - First::Start, S>::type;
};
template <typename S, typename First, typename... Rest>
struct NarrowShape<S, First, Rest...> {
static_assert(
AreTensorPairDimsUnique<First, Rest...>::value &&
sizeof...(Rest) <= S::DIMS,
"\n.You don't need to slice the same dimension more than once\n");
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(First::Start <= First::End,
"\nSlice::Start must be <= Slice::End.\n");
static_assert(First::End <= S::SHAPE_DIMS[First::Dim],
"\nSlice index out of bounds, End > Dim.\n");
static_assert(First::End - First::Start <= S::SHAPE_DIMS[First::Dim],
"\nError: The range size is > than the number of elements in "
"the given dimension.\n");
using type = typename NarrowShape<
typename SetDimAtIdx<First::Dim, First::End - First::Start, S>::type,
Rest...>::type;
};
///
/// SliceShape
///
template <int64_t Curr, typename S, typename... TensorRanges> struct SliceShape;
template <typename S, typename First, typename... Rest>
struct SliceShape<0, S, First, Rest...> {
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(First::Start <= First::End,
"\nSlice::Start must be <= Slice::End.\n");
static_assert(First::End <= S::SHAPE_DIMS[First::Dim],
"\nSlice index out of bounds, End > Dim.\n");
static_assert(First::End - First::Start <= S::SHAPE_DIMS[First::Dim],
"\nError: The range size is > than the number of elements in "
"the given dimension.\n");
using type = typename std::conditional<
First::Dim == 0, Shape<First::End - First::Start>, Shape<>>::type;
};
template <int64_t Curr, typename S, typename First>
struct SliceShape<Curr, S, First> {
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(First::Start <= First::End,
"\nSlice::Start must be <= Slice::End.\n");
static_assert(First::End <= S::SHAPE_DIMS[First::Dim],
"\nSlice index out of bounds, End > Dim.\n");
static_assert(First::End - First::Start <= S::SHAPE_DIMS[First::Dim],
"\nError: The range size is > than the number of elements in "
"the given dimension.\n");
using type =
typename std::conditional <
S::DIMS<Curr, Shape<>,
typename std::conditional<
Curr == First::Dim, Shape<First::End - First::Start>,
typename SliceShape<Curr - 1, S, First>::type>::type>::type;
};
template <int64_t Curr, typename S, typename First, typename... Rest>
struct SliceShape<Curr, S, First, Rest...> {
static_assert(AreTensorRangesAllInDecreasingOrder<First, Rest...>::value,
"\nThe provided dimensions must be in decreasing order.\n");
static_assert(
AreTensorPairDimsUnique<First, Rest...>::value &&
sizeof...(Rest) <= S::DIMS,
"\n.You don't need to slice the same dimension more than once\n");
static_assert(First::Dim < S::DIMS, "\nError: Index out of bounds.\n");
static_assert(First::Start <= First::End,
"\nSlice::Start must be <= Slice::End.\n");
static_assert(First::End <= S::SHAPE_DIMS[First::Dim],
"\nSlice index out of bounds, End > Dim.\n");
static_assert(First::End - First::Start <= S::SHAPE_DIMS[First::Dim],
"\nError: The range size is > than the number of elements in "
"the given dimension.\n");
using type = typename std::conditional<
Curr == First::Dim,
typename SliceShape<Curr - 1, S, Rest...>::type::template append<
First::End - First::Start>,
typename SliceShape<Curr - 1, S, First, Rest...>::type>::type;
};
///
/// SelectOnlyShape
///
template <typename S, typename... SelectPairs> struct SelectOnlyShape;
template <typename S, typename First> struct SelectOnlyShape<S, First> {
static_assert(S::DIMS > First::DIM, "\n.Error: Index out of bounds\n");
static_assert(
AreAllLessThanN<S::SHAPE_DIMS[First::DIM], typename First::Indices>(),
"\nAll indices must be less than the relative dim.\n");
static_assert(AreAllUnique<typename First::Indices>(),
"\n.You can't have duplicate indices.\n");
using type = Shape<First::Indices::DIMS>;
};
template <typename S, typename First, typename... Rest>
struct SelectOnlyShape<S, First, Rest...> {
static_assert(S::DIMS > First::DIM, "\n.Error: Index out of bounds\n");
static_assert(
AreAllLessThanN<S::SHAPE_DIMS[First::DIM], typename First::Indices>(),
"\nAll indices must be less than the relative dim.\n");
static_assert(AreAllUnique<typename First::Indices>::value,
"\n.You can't have duplicate indices.\n");
static_assert(AreTensorPairDimsUnique<First, Rest...>::value,
"\nYou can't have duplicate indices for the same dimension.\n");
static_assert(AreTensorRangesAllInIncreasingOrder<First, Rest...>::value,
"\nThe SelectPairs Dims must be in increasing order.\n");
using type = typename SelectOnlyShape<S, Rest...>::type::template prepend<
First::Indices::DIMS>;
};
///
/// SelectShape
///
template <typename S, typename... SelectPairs> struct SelectShape;
template <typename S, typename First> struct SelectShape<S, First> {
static_assert(S::DIMS > First::DIM, "\n.Error: Index out of bounds\n");
static_assert(
AreAllLessThanN<S::SHAPE_DIMS[First::DIM], typename First::Indices>(),
"\nAll indices must be less than the relative dim.\n");
static_assert(AreAllUnique<typename First::Indices>(),
"\n.You can't have duplicate indices.\n");
using type = typename SetDimAtIdx<First::DIM, First::Indices::DIMS, S>::type;
};
template <typename S, typename First, typename... Rest>
struct SelectShape<S, First, Rest...> {
static_assert(S::DIMS > First::DIM, "\n.Error: Index out of bounds\n");
static_assert(
AreAllLessThanN<S::SHAPE_DIMS[First::DIM], typename First::Indices>(),
"\nAll indices must be less than the relative dim.\n");
static_assert(AreAllUnique<typename First::Indices>::value,
"\n.You can't have duplicate indices.\n");
static_assert(AreTensorPairDimsUnique<First, Rest...>::value,
"\nYou can't have duplicate indices for the same dimension.\n");
static_assert(AreTensorRangesAllInIncreasingOrder<First, Rest...>::value,
"\nThe SelectPairs Dims must be in increasing order.\n");
using type = typename SelectShape<
typename SetDimAtIdx<First::DIM, First::Indices::DIMS, S>::type,
Rest...>::type;
};
///
/// PadShape
///
template <typename S, int64_t Height, int64_t Width> struct PadShape {
static_assert(S::DIMS > 1,
"\nPad only works on tensors with at least 2 dimensions.\n");
using type = typename SetDimAtIdx<
S::DIMS - 1, S::SHAPE_DIMS[S::DIMS - 1] + Width,
typename SetDimAtIdx<S::DIMS - 2, S::SHAPE_DIMS[S::DIMS - 2] + Height,
S>::type>::type;
};
///
///
///
/// Tensor
///
///
///
template <typename TShape, typename TType = f32, typename TDevice = CPU<0>>
class Tensor : public at::TensorBase {
private:
// This cannot be checked at comptime so it's declared as private
Tensor(torch::Tensor base) : base(base) {}
protected:
public:
torch::Tensor base;
using TensorShape = TShape;
using TensorType = dtype::DType<TType>;
using TensorDevice = TDevice;
static constexpr int64_t DIMS = TShape::DIMS;
static constexpr int64_t NELEMS = TShape::NELEMS;
static constexpr std::array<int64_t, DIMS> SHAPE_DIMS = TShape::SHAPE_DIMS;
static constexpr torch::ScalarType DTYPE = TensorType::DTYPE;
static constexpr size_t DTYPE_SIZE = TensorType::SIZE;
static constexpr torch::DeviceType DEVICE = TDevice::DEVICE;
static constexpr int64_t DEVICE_IDX = TDevice::INDEX;
// Constructors
Tensor()
: base(torch::zeros(SHAPE_DIMS,