This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
MM-inl.cuh
816 lines (728 loc) · 33.7 KB
/
MM-inl.cuh
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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "cuda/Complex.cuh"
#include "cuda/ComputeCapabilities.cuh"
#include "cuda/CudaUtils.cuh"
#include "cuda/DeviceTensor.cuh"
#include <algorithm>
#include <cuda_runtime.h>
#include <glog/logging.h>
namespace facebook { namespace cuda {
namespace detail {
__device__ __forceinline__ constexpr int max(int i, int j) {
return (i < j) ? j : i;
}
__device__ __forceinline__ constexpr int max(int i, int j, int k) {
return facebook::cuda::detail::max(facebook::cuda::detail::max(i, j), k);
}
__device__ __forceinline__ Complex ldg(const Complex* p) {
return Complex(__ldg((const float2*)p));
}
__device__ __forceinline__ void ldg(Complex& c1, Complex&c2, const Complex* p) {
const float4 f = __ldg((const float4*)p);
c1 = Complex(f.x, f.y);
c2 = Complex(f.z, f.w);
}
template <bool ConjugateTransposeA,
bool ConjugateTransposeB,
int FFTSize,
int FFTElements,
int TileI,
int TileJ,
int TileK,
int TileIThreadIdxY,
int TileJThreadIdxZ,
bool Accumulate>
__launch_bounds__(32 * 4 * 2, 2) // 128 registers on K40
__global__ void transposeMMTiledKernelSmall(const DeviceTensor<Complex, 3> A,
const DeviceTensor<Complex, 3> B,
DeviceTensor<Complex, 3> C,
Complex invNorm)
{
const auto xyBase = blockIdx.z * blockDim.x;
const auto xy = blockIdx.z * blockDim.x + threadIdx.x;
const int numRed = (ConjugateTransposeA) ? A.getSize(0) : A.getSize(1);
// Conditions must hold for float4 implementation to be valid
assert(xy < FFTSize * (FFTSize / 2 + 1));
assert(FFTElements == blockDim.x);
assert(TileIThreadIdxY == blockDim.y);
assert(TileJThreadIdxZ == blockDim.z);
assert(numRed % TileK == 0);
Complex c[TileI][TileJ];
// for (int i = TileI * blockIdx.x; i < C.getSize(0); i += TileI * gridDim.x) {
// for (int j = TileJ * blockIdx.y; j < C.getSize(1); j += TileJ * gridDim.y) {
{
{
// blockIdx.x/y are the ceils
int i = TileI * (threadIdx.y + blockDim.y * blockIdx.x);
int j = TileJ * (threadIdx.z + blockDim.z * blockIdx.y);
// Guard against overflows
assert(i + TileI <= C.getSize(0));
assert(j + TileJ <= C.getSize(1));
for (int ii = 0; ii < TileI; ++ii) {
for (int jj = 0; jj < TileJ; ++jj) {
c[ii][jj] = (Accumulate) ?
C[i + ii][j + jj][xy] : Complex(0.0f);
}
}
for (int k = 0; k < numRed; k += TileK) {
Complex a[TileK][TileI];
Complex b[TileK][TileJ];
__shared__ Complex swap
[TileJThreadIdxZ]
[TileIThreadIdxY]
[facebook::cuda::detail::max(TileI, TileJ, TileK)]
[2]
[FFTElements];
// View float2[2][FFTElements] as float4[FFTElements], let the
// compiler worry about the indexing.
auto swapViewFloat4 =
(float4(*)
[TileIThreadIdxY]
[facebook::cuda::detail::max(TileI, TileJ, TileK)]
[FFTElements])swap;
// Illustration with blockDim.x == 8
// Goal
// th 0 1 2 3 4 5 6 7
// a A0 A1 A2 A3 A4 A5 A6 A7
// b B0 B1 B2 B3 B4 B5 B6 B7
//
// Threads < blockDim.x / 2 load A0 - A7 into shared float4
// Threads >= blockDim.x / 2 load B0 - B7 into shared float4
// Actual
// s A0/A1 A2/A3 A4/A5 A6/A7 | B0/B1 B2/B3 B4/B5 B6/B7
const auto xdim = (threadIdx.x < blockDim.x / 2) ?
xyBase + 2 * threadIdx.x :
xyBase + 2 * (threadIdx.x - blockDim.x / 2);
for (int kk = 0; kk < TileK; ++kk) {
// This statically unrolls for max(TileI, TileJ, TileK) and computes
// a base pointer for Threads < blockDim.x / 2 and
// Threads >= blockDim.x / 2
// If there is imbalance, the pointer computed is nullptr
// and the load is not generated.
for (int ij = 0;
ij < facebook::cuda::detail::max(TileI, TileJ, TileK); ++ij) {
const Complex* baseA = (ij >= TileI) ?
nullptr :
((!ConjugateTransposeA) ?
A[i + ij][k + kk][xdim].data() :
A[k + kk][i + ij][xdim].data()) ;
const Complex* baseB = (ij >= TileJ) ?
nullptr :
((!ConjugateTransposeB) ?
B[k + kk][j + ij][xdim].data() :
B[j + ij][k + kk][xdim].data()) ;
const Complex* base =
(threadIdx.x < blockDim.x / 2) ? baseA : baseB;
if (base) {
swapViewFloat4[threadIdx.z][threadIdx.y][ij][threadIdx.x] =
__ldg((const float4*)(base));
}
}
for (int ii = 0; ii < TileI; ++ii) {
a[kk][ii] = swap[threadIdx.z][threadIdx.y][ii][0][threadIdx.x];
}
for (int jj = 0; jj < TileJ; ++jj) {
b[kk][jj] = swap[threadIdx.z][threadIdx.y][jj][1][threadIdx.x];
}
}
if (ConjugateTransposeA) {
for (int kk = 0; kk < TileK; ++kk) {
for (int ii = 0; ii < TileI; ++ii) {
a[kk][ii] = a[kk][ii].conjugate();
}
}
}
if (ConjugateTransposeB) {
for (int kk = 0; kk < TileK; ++kk) {
for (int jj = 0; jj < TileJ; ++jj) {
b[kk][jj] = b[kk][jj].conjugate();
}
}
}
for (int kk = 0; kk < TileK; ++kk) {
for (int jj = 0; jj < TileJ; ++jj) {
for (int ii = 0; ii < TileI; ++ii) {
c[ii][jj] += a[kk][ii] * b[kk][jj];
}
}
}
}
// Actual
// c C0 C2 C4 C6 C1 C3 C5 C7
for (int ii = 0; ii < TileI; ++ii) {
for (int jj = 0; jj < TileJ; ++jj) {
c[ii][jj].re() *= invNorm.re();
c[ii][jj].im() *= invNorm.re();
*(C[i + ii][j + jj][xy].dataAs<float2>()) = (float2)(c[ii][jj]);
}
}
}
}
}
// By construction, x * y is contiguous.
// doall xy
// doall i, j
// red k
// C[i][j][x * y] += A[i][k][x * y] * B[k][j][x * y]
//
// UpdateOutput : xy times o(b, f) <- i(b, p) . conj(f(f, p))
// AccGradParameters: xy times f(f, p) <- conj(o(b, f)) . i(b, p)
// UpdateGradInput : xy times i(b, p) <- o(b, f) . f(f, p)
template <bool ConjugateTransposeA,
bool ConjugateTransposeB,
int FFTSize,
int C_J_Unroll,
int C_I_Tile,
int C_J_Tile,
int ReductionUnroll,
bool Accumulate>
__launch_bounds__(32 * 32, 1) // 64 registers on K40
__global__ void transposeMMTiledKernelUnrolled(const DeviceTensor<Complex, 3> A,
const DeviceTensor<Complex, 3> B,
DeviceTensor<Complex, 3> C,
Complex invNorm) {
assert(C_J_Unroll == blockDim.y);
assert(A.getSize(2) == C.getSize(2));
assert(B.getSize(2) == C.getSize(2));
assert(ConjugateTransposeA || A.getSize(0) == C.getSize(0));
assert(!ConjugateTransposeA || A.getSize(1) == C.getSize(0));
assert(ConjugateTransposeB || B.getSize(1) == C.getSize(1));
assert(!ConjugateTransposeB || B.getSize(0) == C.getSize(1));
assert(ConjugateTransposeA || ConjugateTransposeB ||
A.getSize(1) == B.getSize(0));
const int numRed =
(ConjugateTransposeA) ? A.getSize(0) : A.getSize(1);
// const int ubi = C.getSize(0);
// assert(C.getSize(0) % (C_I_Tile * gridDim.x) == 0);
// const int ubj = C.getSize(1);
// assert(C.getSize(1) % (C_J_Tile * gridDim.y * blockDim.y) == 0);
const int ubk = numRed;
assert(numRed % ReductionUnroll == 0);
const int numBatches = A.getSize(2);
const int ubxy =
ceil(numBatches, (int)(gridDim.z * blockDim.x)) *
gridDim.z * blockDim.x;
// for (int i = C_I_Tile * blockIdx.x; i < ubi; i += C_I_Tile * gridDim.x) {
// for (int j = C_J_Tile * (blockIdx.y * blockDim.y + threadIdx.y);
// j < ubj;
// j += C_J_Tile * gridDim.y * blockDim.y) {
{
{
int i = C_I_Tile * blockIdx.x;
int j = C_J_Tile * (blockIdx.y * blockDim.y + threadIdx.y);
// for (int xy = blockDim.x * blockIdx.z + threadIdx.x;
// xy < ubxy;
// xy += gridDim.z * blockDim.x) {
{
int xy = blockDim.x * blockIdx.z + threadIdx.x;
Complex a[C_I_Tile];
Complex b[C_J_Tile][ReductionUnroll];
Complex c[C_I_Tile][C_J_Tile];
for (int k = 0; k < ubk; k += ReductionUnroll) {
// Load B from device to registers with boundary check and static
// optimization of those checks.
for (int jj = 0; jj < C_J_Tile; ++jj) {
for (int kk = 0; kk < ReductionUnroll; ++kk) {
b[jj][kk] = (ConjugateTransposeB) ?
ldg(&B[j + jj][k + kk][xy]) : // delay conjugate reduces dep
ldg(&B[k + kk][j + jj][xy]);
}
}
// Use init to hide some latencies
if (k == 0) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
for (int jj = 0; jj < C_J_Tile; ++jj) {
c[ii][jj] = (Accumulate) ?
C[i + ii][j + jj][xy] : Complex(0.0f);
}
}
}
// Load A from device to shared with boundary check and static
// optimization of those checks.
// Distribute loads across blockIdx.y
__shared__ Complex
as[C_I_Tile][ReductionUnroll][FFTSize];
assert(C_I_Tile <= blockDim.y);
// Kill WAW dependence
__syncthreads();
if (threadIdx.y < C_I_Tile) {
int ii = threadIdx.y;
for (int kk = 0; kk < ReductionUnroll; ++kk) {
as[ii][kk][threadIdx.x] = (ConjugateTransposeA) ?
ldg(&A[k + kk][i + ii][xy]) : // delay conjugate reduces dep
ldg(&A[i + ii][k + kk][xy]);
}
}
// Kill RAW dependence
__syncthreads();
// Perform partial accumulation
for (int kk = 0; kk < ReductionUnroll; ++kk) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
a[ii] = as[ii][kk][threadIdx.x];
}
for (int jj = 0; jj < C_J_Tile; ++jj) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
if (ConjugateTransposeA) {
c[ii][jj] = a[ii].conjugate() * b[jj][kk] + c[ii][jj];
} else if (ConjugateTransposeB) {
c[ii][jj] = a[ii] * b[jj][kk].conjugate() + c[ii][jj];
} else {
c[ii][jj] = a[ii] * b[jj][kk] + c[ii][jj];
}
}
}
}
}
for (int ii = 0; ii < C_I_Tile; ++ii) {
for (int jj = 0; jj < C_J_Tile; ++jj) {
c[ii][jj].re() *= invNorm.re();
c[ii][jj].im() *= invNorm.re();
*(C[i + ii][j + jj][xy].dataAs<float2>()) = (float2)(c[ii][jj]);
}
}
}
}
}
}
// By construction, x * y is contiguous.
// doall xy
// doall i, j
// red k
// C[i][j][x * y] += A[i][k][x * y] * B[k][j][x * y]
//
// UpdateOutput : xy times o(b, f) <- i(b, p) . conj(f(f, p))
// AccGradParameters: xy times f(f, p) <- conj(o(b, f)) . i(b, p)
// UpdateGradInput : xy times i(b, p) <- o(b, f) . f(f, p)
template <bool ConjugateTransposeA,
bool ConjugateTransposeB,
int C_XY_Placement_ThreadIdx_X,
int C_J_Unroll,
int C_I_Tile,
int C_J_Tile,
int ReductionUnroll,
bool StaticUnrollA,
bool StaticUnrollB,
bool StaticUnrollCI,
bool StaticUnrollCJ,
bool StaticUnrollXY,
bool StaticUnrollReduction,
bool Accumulate>
__launch_bounds__(32 * 32, 1)
__global__ void transposeMMTiledKernel(const DeviceTensor<Complex, 3> A,
const DeviceTensor<Complex, 3> B,
DeviceTensor<Complex, 3> C,
Complex invNorm) {
assert(C_J_Unroll == blockDim.y);
assert(C_XY_Placement_ThreadIdx_X == blockDim.x);
assert(A.getSize(2) == C.getSize(2));
assert(B.getSize(2) == C.getSize(2));
assert(ConjugateTransposeA || A.getSize(0) == C.getSize(0));
assert(!ConjugateTransposeA || A.getSize(1) == C.getSize(0));
assert(ConjugateTransposeB || B.getSize(1) == C.getSize(1));
assert(!ConjugateTransposeB || B.getSize(0) == C.getSize(1));
assert(ConjugateTransposeA || ConjugateTransposeB ||
A.getSize(1) == B.getSize(0));
const int numRed =
(ConjugateTransposeA) ? A.getSize(0) : A.getSize(1);
const int ubi = (StaticUnrollCI) ?
C.getSize(0) :
ceil(C.getSize(0), (int)(C_I_Tile * gridDim.x))
* C_I_Tile * gridDim.x;
assert(!StaticUnrollCI || C.getSize(0) % (C_I_Tile * gridDim.x) == 0);
const int ubj = (StaticUnrollCJ) ?
C.getSize(1) :
ceil(C.getSize(1), (int)(C_J_Tile * gridDim.y * blockDim.y))
* C_J_Tile * gridDim.y * blockDim.y;
assert(!StaticUnrollCJ ||
C.getSize(1) % (C_J_Tile * gridDim.y * blockDim.y) == 0);
const int ubk = (StaticUnrollReduction) ?
numRed : ceil(numRed, ReductionUnroll) * ReductionUnroll;
assert(!StaticUnrollReduction || numRed % ReductionUnroll == 0);
const int numBatches = A.getSize(2);
const int ubxy =
ceil(numBatches, (int)(gridDim.z * blockDim.x)) *
gridDim.z * blockDim.x;
for (int i = C_I_Tile * blockIdx.x; i < ubi; i += C_I_Tile * gridDim.x) {
for (int j = C_J_Tile * (blockIdx.y * blockDim.y + threadIdx.y);
j < ubj;
j += C_J_Tile * gridDim.y * blockDim.y) {
for (int xy = blockDim.x * blockIdx.z + threadIdx.x;
xy < ubxy;
xy += gridDim.z * blockDim.x) {
Complex a[C_I_Tile];
Complex b[C_J_Tile][ReductionUnroll];
Complex c[C_I_Tile][C_J_Tile];
for (int k = 0; k < ubk; k += ReductionUnroll) {
// Kill WAW dependence
__syncthreads();
// Load B from device to registers with boundary check and static
// optimization of those checks.
for (int jj = 0; jj < C_J_Tile; ++jj) {
if ((StaticUnrollXY || xy < numBatches) &&
(StaticUnrollB ||
(ConjugateTransposeB && j + jj < B.getSize(0)) ||
(!ConjugateTransposeB && j + jj < B.getSize(1)))) {
for (int kk = 0; kk < ReductionUnroll; ++kk) {
b[jj][kk] = (StaticUnrollReduction || k + kk < numRed) ?
((ConjugateTransposeB) ?
ldg(&B[j + jj][k + kk][xy]).conjugate() :
ldg(&B[k + kk][j + jj][xy]))
: Complex(0.0f);
}
} else {
for (int kk = 0; kk < ReductionUnroll; ++kk) {
b[jj][kk] = Complex(0.0f);
}
}
}
// Load A from device to shared with boundary check and static
// optimization of those checks.
// Distribute loads across blockIdx.y
__shared__ Complex
as[C_I_Tile][ReductionUnroll][C_XY_Placement_ThreadIdx_X + 1];
assert(C_I_Tile <= blockDim.y);
if (threadIdx.y < C_I_Tile) {
int ii = threadIdx.y;
if ((StaticUnrollXY || xy < numBatches) &&
(StaticUnrollA ||
(ConjugateTransposeA && i + ii < A.getSize(1)) ||
(!ConjugateTransposeA && i + ii < A.getSize(0)))) {
for (int kk = 0; kk < ReductionUnroll; ++kk) {
as[ii][kk][threadIdx.x] =
(StaticUnrollReduction || k + kk < numRed) ?
((ConjugateTransposeA) ?
ldg(&A[k + kk][i + ii][xy]).conjugate() :
ldg(&A[i + ii][k + kk][xy]))
: Complex(0.0f);
}
} else {
for (int kk = 0; kk < ReductionUnroll; ++kk) {
as[ii][kk][threadIdx.x] = Complex(0.0f);
}
}
}
// Use init to hide some latencies
if (k == 0) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
for (int jj = 0; jj < C_J_Tile; ++jj) {
c[ii][jj] =
(Accumulate &&
(StaticUnrollCI || i + ii < C.getSize(0)) &&
(StaticUnrollCJ || j + jj < C.getSize(1)) &&
(StaticUnrollXY || xy < numBatches)) ?
C[i + ii][j + jj][xy] : Complex(0.0f);
}
}
}
// Kill RAW dependence
__syncthreads();
// Perform partial accumulation
for (int kk = 0; kk < ReductionUnroll; ++kk) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
a[ii] = as[ii][kk][threadIdx.x];
}
for (int jj = 0; jj < C_J_Tile; ++jj) {
for (int ii = 0; ii < C_I_Tile; ++ii) {
c[ii][jj] = a[ii] * b[jj][kk] + c[ii][jj];
}
}
}
}
if (StaticUnrollXY || xy < numBatches) {
for (int ii = 0;
ii < C_I_Tile && (StaticUnrollCI || i + ii < C.getSize(0));
++ii) {
for (int jj = 0;
jj < C_J_Tile && (StaticUnrollCJ || j + jj < C.getSize(1));
++jj) {
c[ii][jj].re() *= invNorm.re();
c[ii][jj].im() *= invNorm.re();
*(C[i + ii][j + jj][xy].dataAs<float2>()) = (float2)(c[ii][jj]);
}
}
}
}
}
}
}
struct HalfFtor {
HalfFtor() {}
void operator()(int& n) { n >>= 1; }
};
} // ns detail
template
<int Dim, bool ConjugateTransposeA, bool ConjugateTransposeB, bool Accumulate>
void transposeMM(DeviceTensor<float, Dim>& A,
DeviceTensor<float, Dim>& B,
DeviceTensor<float, Dim>& C,
float invNorm,
cudaStream_t s) {
int szA[Dim - 1];
int stA[Dim - 1];
std::copy(A.sizes(), A.sizes() + Dim - 1, szA);
std::copy(A.strides(), A.strides() + Dim - 1, stA);
std::for_each(&stA[0], &stA[Dim - 1], detail::HalfFtor());
int szB[Dim - 1];
int stB[Dim - 1];
std::copy(B.sizes(), B.sizes() + Dim - 1, szB);
std::copy(B.strides(), B.strides() + Dim - 1, stB);
std::for_each(&stB[0], &stB[Dim - 1], detail::HalfFtor());
int szC[Dim - 1];
int stC[Dim - 1];
std::copy(C.sizes(), C.sizes() + Dim - 1, szC);
std::copy(C.strides(), C.strides() + Dim - 1, stC);
std::for_each(&stC[0], &stC[Dim - 1], detail::HalfFtor());
DeviceTensor<Complex, Dim - 1> cA(A.template dataAs<Complex>(), szA, stA);
DeviceTensor<Complex, Dim - 1> cB(B.template dataAs<Complex>(), szB, stB);
DeviceTensor<Complex, Dim - 1> cC(C.template dataAs<Complex>(), szC, stC);
DeviceTensor<Complex, 3> dcA = cA.template downcastInner<3>();
DeviceTensor<Complex, 3> dcB = cB.template downcastInner<3>();
DeviceTensor<Complex, 3> dcC = cC.template downcastInner<3>();
#define INSTANTIATE_FBMM_FULLY_UNROLLED( \
C_J_Unroll, \
C_I_Tile, \
C_J_Tile, \
ReductionUnroll) \
{ \
bool StaticUnrollA = \
((ConjugateTransposeA && (dcA.getSize(1) % C_I_Tile == 0)) || \
(!ConjugateTransposeA && (dcA.getSize(0) % C_I_Tile == 0))); \
bool StaticUnrollB = \
((ConjugateTransposeB && (dcB.getSize(0) % C_J_Tile == 0)) || \
(!ConjugateTransposeB && (dcB.getSize(1) % C_J_Tile == 0))); \
bool StaticUnrollCI = (dcC.getSize(0) % C_I_Tile == 0); \
bool StaticUnrollCJ = (dcC.getSize(1) % (C_J_Unroll * C_J_Tile) == 0); \
const int numRed = \
(ConjugateTransposeA) ? dcA.getSize(0) : dcA.getSize(1); \
bool StaticUnrollReduction = (numRed % ReductionUnroll == 0); \
if (debug) { \
LOG(INFO) << StaticUnrollA << " " << StaticUnrollB << " " \
<< StaticUnrollCI << " " << StaticUnrollCJ << " " \
<< StaticUnrollReduction; \
LOG(INFO) << StaticUnrollA << " " << StaticUnrollB << " " \
<< StaticUnrollCI << " " << StaticUnrollCJ << " " \
<< StaticUnrollReduction; \
} \
if (StaticUnrollA && StaticUnrollB && StaticUnrollCI && \
StaticUnrollCJ && StaticUnrollReduction) { \
if (debug) { \
LOG(INFO) << "Params: " << C_J_Unroll << " " << \
C_I_Tile << " " << \
C_J_Tile << " " << \
ReductionUnroll; \
} \
/* Needed for proper loading of data */ \
CHECK_LE(C_I_Tile, C_J_Unroll); \
dim3 blocks(ceil(dcC.getSize(0), C_I_Tile), \
ceil(dcC.getSize(1), C_J_Unroll * C_J_Tile), \
FFTSize / 2 + 1); \
dim3 threads(FFTSize, C_J_Unroll); \
detail::transposeMMTiledKernelUnrolled<ConjugateTransposeA, \
ConjugateTransposeB, \
FFTSize, \
C_J_Unroll, \
C_I_Tile, \
C_J_Tile, \
ReductionUnroll, \
Accumulate> \
<<<blocks, threads, 0, s>>>(dcA, dcB, dcC, Complex(invNorm)); \
return; \
} \
}
#define INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEI, TILEJ, TILEK, TILEITHY, TILEJTHZ, FFTELEMENTS) \
{ \
constexpr int TileI = TILEI; \
constexpr int TileJ = TILEJ; \
constexpr int TileK = TILEK; \
constexpr int TileIThreadIdxY = TILEITHY; \
constexpr int TileJThreadIdxZ = TILEJTHZ; \
constexpr int FFTElements = FFTELEMENTS; \
if (dcC.getSize(0) % (TileI * TileIThreadIdxY) == 0 && \
dcC.getSize(1) % (TileJ * TileJThreadIdxZ) == 0 && \
( \
(!ConjugateTransposeA && ((dcA.getSize(1) % TileK) == 0)) || \
( ConjugateTransposeA && ((dcA.getSize(0) % TileK) == 0)) \
) && \
(FFTSize * (FFTSize / 2 + 1)) % (2 * FFTElements) == 0) { \
if (debug) { \
LOG(INFO) << " TileI = " << TileI \
<< " TileJ = " << TileJ \
<< " TileK = " << TileK \
<< " TileIThreadIdxY = " << TileIThreadIdxY \
<< " TileJThreadIdxZ = " << TileJThreadIdxZ \
<< " FFTElements = " << FFTElements; \
} \
static_assert(FFTSize % FFTElements == 0, \
"float4 reads requires FFTSize % FFTElements == 0");\
dim3 blocks(ceil(dcC.getSize(0), TileI * TileIThreadIdxY), \
ceil(dcC.getSize(1), TileJ * TileJThreadIdxZ), \
ceil(FFTSize * (FFTSize / 2 + 1), FFTElements)); \
dim3 threads(FFTElements, TileIThreadIdxY, TileJThreadIdxZ); \
detail::transposeMMTiledKernelSmall<ConjugateTransposeA, \
ConjugateTransposeB, \
FFTSize, \
FFTElements, \
TileI, \
TileJ, \
TileK, \
TileIThreadIdxY, \
TileJThreadIdxZ, \
Accumulate> \
<<<blocks, threads, 0, s>>> (dcA, dcB, dcC, Complex(invNorm)); \
return; \
} \
}
// Always look permutations of (TILEI, TILEJ, TILEK) and (TILEITHY, TILEJTHZ)
#define INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED( \
TILEI, TILEJ, TILEK, TILEITHY, TILEJTHZ, FFTELEMENTS) \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEI, TILEJ, TILEK, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEI, TILEJ, TILEK, TILEJTHZ, TILEITHY, FFTELEMENTS); \
\
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEI, TILEK, TILEJ, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEI, TILEK, TILEJ, TILEJTHZ, TILEITHY, FFTELEMENTS); \
\
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEJ, TILEI, TILEK, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEJ, TILEI, TILEK, TILEJTHZ, TILEITHY, FFTELEMENTS); \
\
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEJ, TILEK, TILEI, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEJ, TILEK, TILEI, TILEJTHZ, TILEITHY, FFTELEMENTS); \
\
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEK, TILEI, TILEJ, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEK, TILEI, TILEJ, TILEJTHZ, TILEITHY, FFTELEMENTS); \
\
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEK, TILEJ, TILEI, TILEITHY, TILEJTHZ, FFTELEMENTS); \
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED_IMPL( \
TILEK, TILEJ, TILEI, TILEJTHZ, TILEITHY, FFTELEMENTS);
bool debug = false;
if (debug) {
LOG(INFO) << "ConjugateTransposeA: " << ConjugateTransposeA
<< " ConjugateTransposeB: " << ConjugateTransposeB
<< "\nA: " << A << " -> " << cA << " -> " << dcA
<< "\nB: " << B << " -> " << cB << " -> " << dcB
<< "\nC: " << C << " -> " << cC << " -> " << dcC;
}
// INSTANTIATE_FBMM_FULLY_UNROLLED(C_J_Unroll,
// C_I_Tile,
// C_J_Tile,
// ReductionUnroll)
// TODO: Add more instantiations to cover use cases properly
if (dcA.getSize(2) == 3 * 4) {
} else if (dcA.getSize(2) == 5 * 8) {
constexpr int FFTSize = 8;
INSTANTIATE_FBMM_FULLY_UNROLLED(32, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED(16, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED(12, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED( 8, /* */ 8, 2, 2);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 4, 4, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 4, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 4, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 2, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 2, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 1, 1, 4);
// InputPlane = 3*k (RGB input mostly)
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 4, 4, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 4, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 4, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 2, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 2, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 1, 1, 4);
// Batch size = 1
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 4, 4, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 4, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 4, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 2, 2, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 2, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 1, 1, 4);
// Fallback
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(2, 2, 2, 1, 1, 4);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 1, 1, 1, 1, 4);
} else if (dcA.getSize(2) == 9 * 16) {
constexpr int FFTSize = 16;
INSTANTIATE_FBMM_FULLY_UNROLLED(16, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED(12, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED( 8, /* */ 8, 2, 2);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 4, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 4, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 2, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 2, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 1, 1, 8);
// InputPlane = 3*k (RGB input mostly)
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 4, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 4, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 2, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 2, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 1, 1, 8);
// Batch size = 1
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 4, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 4, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 2, 2, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 2, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 1, 1, 8);
// Fallback
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(2, 2, 2, 1, 1, 8);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 1, 1, 1, 1, 8);
} else if (dcA.getSize(2) == 17 * 32) {
constexpr int FFTSize = 32;
INSTANTIATE_FBMM_FULLY_UNROLLED(8, /* */ 8, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED(6, /* */ 4, 2, 2);
INSTANTIATE_FBMM_FULLY_UNROLLED(4, /* */ 4, 2, 2);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 2, 2, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(4, 4, 4, 1, 1, 16);
// InputPlane = 3*k (RGB input mostly)
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 1, 4, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 1, 2, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(3, 4, 4, 1, 1, 16);
// Batch size = 1
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 1, 4, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 1, 2, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 4, 4, 1, 1, 16);
// Fallback
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(2, 2, 2, 1, 1, 16);
INSTANTIATE_FBMM_SMALL_FULLY_UNROLLED(1, 1, 1, 1, 1, 16);
} else if (dcA.getSize(2) == 33 * 64) {
} else if (dcA.getSize(2) == 65 * 128) {
}
// Fallback cases
if (debug) {
LOG(WARNING) << "Unspecialized case, performance will be very bad";
}
// Default case, performance wil most likely be bad if we get here
#define C_I_Tile 4
#define C_J_Tile 2
#define ReductionUnroll 1
#define C_J_Unroll 4
#define C_XY_Placement_ThreadIdx_X 4
#define C_XY_Placement_BlockIdx_Z 1
dim3 blocks(ceil(dcC.getSize(0), C_I_Tile),
ceil(dcC.getSize(1), C_J_Unroll * C_J_Tile),
C_XY_Placement_BlockIdx_Z);
dim3 threads(C_XY_Placement_ThreadIdx_X, C_J_Unroll);
detail::transposeMMTiledKernel<ConjugateTransposeA,
ConjugateTransposeB,
C_XY_Placement_ThreadIdx_X,
C_J_Unroll,
C_I_Tile,
C_J_Tile,
ReductionUnroll,
false,
false,
false,
false,
false,
false,
Accumulate>
<<<blocks, threads, 0, s>>>(dcA, dcB, dcC, Complex(invNorm));
}
}} // ns