-
Notifications
You must be signed in to change notification settings - Fork 159
/
hgemm_wmma_stage.cu
1460 lines (1333 loc) · 64.9 KB
/
hgemm_wmma_stage.cu
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
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <vector>
#include <algorithm>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cuda_bf16.h>
#include <cuda_fp8.h>
#include <mma.h>
#include <torch/types.h>
#include <torch/extension.h>
using namespace nvcuda;
#define WARP_SIZE 32
#define DEVICE_INLINE __device__ inline
#define HOST_DEVICE_INLINE __device__ __host__ inline
#define INT4(value) (reinterpret_cast<int4*>(&(value))[0])
#define FLOAT4(value) (reinterpret_cast<float4*>(&(value))[0])
#define HALF2(value) (reinterpret_cast<half2*>(&(value))[0])
#define BFLOAT2(value) (reinterpret_cast<__nv_bfloat162*>(&(value))[0])
#define LDST32BITS(value) (reinterpret_cast<half2*>(&(value))[0])
#define LDST64BITS(value) (reinterpret_cast<float2*>(&(value))[0])
#define LDST128BITS(value) (reinterpret_cast<float4*>(&(value))[0])
#define CP_ASYNC_COMMIT_GROUP() asm volatile("cp.async.commit_group;\n" ::)
#define CP_ASYNC_WAIT_ALL() asm volatile("cp.async.wait_all;\n" ::)
#define CP_ASYNC_WAIT_GROUP(n) asm volatile("cp.async.wait_group %0;\n" ::"n"(n))
// ca(cache all, L1 + L2): support 4, 8, 16 bytes, cg(cache global, L2): only support 16 bytes.
#define CP_ASYNC_CA(dst, src, bytes) asm volatile("cp.async.ca.shared.global.L2::128B [%0], [%1], %2;\n" ::"r"(dst), "l"(src), "n"(bytes))
#define CP_ASYNC_CG(dst, src, bytes) asm volatile("cp.async.cg.shared.global.L2::128B [%0], [%1], %2;\n" ::"r"(dst), "l"(src), "n"(bytes))
// Support A and B matrix with row-major inorder to compare with the kernels using CUDA Cores in
// hgemm.cu and hgemm_async.cu.
HOST_DEVICE_INLINE
int div_ceil(int a, int b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
// stage2/3/4 (stage2=double buffers+copy async), 128x128, mma4x2, warp2x4(32,64,16)
// 1. When using shared memory exceeds 48 KB, dynamic shared memory needs to be used,
// i.e., declare a block of dynamic shared memory with extern shared half smem[];.
// When calling the kernel, the size of the dynamic shared memory needs to be specified,
// and smem addressing should be used in a one-dimensional array manner.
// 2. Improve L2 Cache locality (Thread Block Swizzle): https://zhuanlan.zhihu.com/p/555339335
// 3. __launch_bounds__: avoid error 'too many resources required for launch'
// reference: https://blog.csdn.net/feng__shuai/article/details/124395023
template<const int WMMA_M=16,
const int WMMA_N=16,
const int WMMA_K=16,
const int WMMA_TILE_M=4,
const int WMMA_TILE_N=2,
const int WARP_TILE_M=2,
const int WARP_TILE_N=4,
const int A_PAD=0,
const int B_PAD=0,
const int K_STAGE=2,
const bool BLOCK_SWIZZLE=false>
__global__ void __launch_bounds__(256)
hgemm_wmma_m16n16k16_mma4x2_warp2x4_stages_kernel(
half* A, half* B, half* C, int M, int N, int K) {
// 256 threads(8 warps) per block.
// const int bx = blockIdx.x;
// BLOCK_SWIZZLE 0/1 control use block swizzle or not.
const int bx = ((int) BLOCK_SWIZZLE) * blockIdx.z * gridDim.x + blockIdx.x;
const int by = blockIdx.y;
const int NUM_K_TILES = div_ceil(K, WMMA_K);
constexpr int BM = WMMA_M * WMMA_TILE_M * WARP_TILE_M; // 16x4*2=128
constexpr int BN = WMMA_N * WMMA_TILE_N * WARP_TILE_N; // 16x2*4=128
constexpr int BK = WMMA_K; // 16
// s2: 2*128*(16+8)*2=12KB, 2*16*(128+8)*2=8.50KB, ~21KB
// s3: 3*128*(16+8)*2=18KB, 3*16*(128+8)*2=12.75KB, ~31KB
// s4: 4*128*(16+8)*2=24KB, 4*16*(128+8)*2=17KB, ~41KB
__shared__ half s_a[K_STAGE][BM][BK + A_PAD], s_b[K_STAGE][BK][BN + B_PAD];
constexpr int s_a_stage_offset = BM * (BK + A_PAD);
constexpr int s_b_stage_offset = BK * (BN + B_PAD);
// 要保证相同的warp下thread执行相同的指令
const int tid = threadIdx.y * blockDim.x + threadIdx.x;
const int warp_id = tid / WARP_SIZE; // tid >> 5; // 0~7 warp_id within block
const int warp_m = warp_id / 2; // warp_id >> 1; // 0,1,2,3
const int warp_n = warp_id % 2; // 0,1
// 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=128 BK=16 按行读取 A行主序
// 对于s_a每行16个数据,每个线程读取8个,需要2个线程;总共128行,需要128x2刚好256线程
int load_smem_a_m = tid / 2; // tid >> 1; // row 0~127
int load_smem_a_k = (tid % 2 == 0) ? 0 : 8; // col 0,8
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=16 BN=128 按行读取 B行主序
// 对于s_b每行128个数据,每个线程读8个数据,需要16个线程;总共16行,需要16x16=256个线程
int load_smem_b_k = tid / 16; // tid >> 4; // row 0~15
int load_smem_b_n = (tid % 16) * 8; // ((tid & 0xF) << 3); // col 0,8,...,120
// 再计算全局内存中的索引
// 要加载到s_a中的元素对应到A全局内存中的行数 每个block负责出C中大小为BM*BN的块
int load_gmem_a_m = by * BM + load_smem_a_m; // global row of a and c
int load_gmem_b_n = bx * BN + load_smem_b_n; // global col of b and c
wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, half>
C_frag[WARP_TILE_M][WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::fill_fragment(C_frag[i][j], 0.0);
}
}
// may avoid cvta overhead ? only cvta smem base ptr once for cp.async.
uint32_t smem_a_base_ptr = __cvta_generic_to_shared(s_a);
uint32_t smem_b_base_ptr = __cvta_generic_to_shared(s_b);
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); ++k) { // 0, 1
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (k * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (k * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2); // s2->0, s3->1, s4->2
__syncthreads();
#pragma unroll
for (int k = (K_STAGE - 1); k < NUM_K_TILES; k++) {
// s2/4 can use bitwise ops but s3 can not, so, we use mod
// ops for all stages kernel. s2: (k + 1)&1, s4: (k + 1)&3
// s3: (k + 1) % 3
int smem_sel = (k + 1) % K_STAGE; // s3 k 2->0, k 3->1, k 4->2...
int smem_sel_next = k % K_STAGE; // s3 k 2->2, k 3->0, k 4->1...
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (smem_sel_next * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (smem_sel_next * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
// compute stage 0
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
const int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
wmma::load_matrix_sync(A_frag[i], &s_a[smem_sel][warp_smem_a_m][0], BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
const int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::load_matrix_sync(B_frag[j], &s_b[smem_sel][0][warp_smem_b_n], BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2);
__syncthreads();
}
// make sure all memory issues ready.
if ((K_STAGE - 2) > 0) {
CP_ASYNC_WAIT_GROUP(0);
__syncthreads();
}
// processing last (K_STAGE-1) k iters.
{
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); k++) {
const int stage_sel = ((NUM_K_TILES - (K_STAGE - 1) + k) % K_STAGE);
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
const int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
wmma::load_matrix_sync(A_frag[i], &s_a[stage_sel][warp_smem_a_m][0], BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
const int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::load_matrix_sync(B_frag[j], &s_b[stage_sel][0][warp_smem_b_n], BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
}
}
// finally, store back to C matrix.
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
const int store_gmem_a_m = by * BM + warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
const int store_gmem_a_n = bx * BN + warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::store_matrix_sync(C + store_gmem_a_m * N + store_gmem_a_n, C_frag[i][j], N,
wmma::mem_row_major);
}
}
}
// stage2/3/4 (stage2=double buffers+copy async), 128x128,mma4x2, warp2x4(32,64,16)
// 1. When using shared memory exceeds 48 KB, dynamic shared memory needs to be used,
// i.e., declare a block of dynamic shared memory with extern shared half smem[];.
// When calling the kernel, the size of the dynamic shared memory needs to be specified,
// and smem addressing should be used in a one-dimensional array manner.
// 2. Improve L2 Cache locality (Thread Block Swizzle): https://zhuanlan.zhihu.com/p/555339335
// 3. __launch_bounds__: avoid error 'too many resources required for launch'
// reference: https://blog.csdn.net/feng__shuai/article/details/124395023
template<const int WMMA_M=16,
const int WMMA_N=16,
const int WMMA_K=16,
const int WMMA_TILE_M=4,
const int WMMA_TILE_N=2,
const int WARP_TILE_M=2,
const int WARP_TILE_N=4,
const int A_PAD=0,
const int B_PAD=0,
const int K_STAGE=2,
const bool BLOCK_SWIZZLE=false>
__global__ void __launch_bounds__(256)
hgemm_wmma_m16n16k16_mma4x2_warp2x4_stages_dsmem_kernel(
half* A, half* B, half* C, int M, int N, int K) {
// 256 threads(8 warps) per block.
// const int bx = blockIdx.x;
// BLOCK_SWIZZLE 0/1 control use block swizzle or not.
const int bx = ((int) BLOCK_SWIZZLE) * blockIdx.z * gridDim.x + blockIdx.x;
const int by = blockIdx.y;
const int NUM_K_TILES = div_ceil(K, WMMA_K);
constexpr int BM = WMMA_M * WMMA_TILE_M * WARP_TILE_M; // 16x4*2=128
constexpr int BN = WMMA_N * WMMA_TILE_N * WARP_TILE_N; // 16x2*4=128
constexpr int BK = WMMA_K; // 16
// s2: 2*128*(16+8)*2=12KB, 2*16*(128+8)*2=8.50KB, ~21KB
// s3: 3*128*(16+8)*2=18KB, 3*16*(128+8)*2=12.75KB, ~31KB
// s4: 4*128*(16+8)*2=24KB, 4*16*(128+8)*2=17KB, ~41KB
// s5: 5*128*(16+8)*2=30KB, 5*16*(128+8)*2=21.25KB, ~52KB > 48KB
extern __shared__ half smem[];
half* s_a = smem;
half* s_b = smem + K_STAGE * BM * (BK + A_PAD);
constexpr int s_a_stage_offset = BM * (BK + A_PAD);
constexpr int s_b_stage_offset = BK * (BN + B_PAD);
// 要保证相同的warp下thread执行相同的指令
const int tid = threadIdx.y * blockDim.x + threadIdx.x;
const int warp_id = tid / WARP_SIZE; // 0~7 warp_id within block
const int warp_m = warp_id / 2; // 0,1,2,3
const int warp_n = warp_id % 2; // 0,1
// 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=128 BK=16 按行读取 A行主序
// 对于s_a每行16个数据,每个线程读取8个,需要2个线程;总共128行,需要128x2刚好256线程
int load_smem_a_m = tid / 2; // row 0~127
int load_smem_a_k = (tid % 2 == 0) ? 0 : 8; // col 0,8
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=16 BN=128 按行读取 B行主序
// 对于s_b每行128个数据,每个线程读8个数据,需要16个线程;总共16行,需要16x16=256个线程
int load_smem_b_k = tid / 16; // row 0~15
int load_smem_b_n = (tid % 16) * 8; // col 0,8,...,120
// 再计算全局内存中的索引
// 要加载到s_a中的元素对应到A全局内存中的行数 每个block负责出C中大小为BM*BN的块
int load_gmem_a_m = by * BM + load_smem_a_m; // global row of a and c
int load_gmem_b_n = bx * BN + load_smem_b_n; // global col of b and c
wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, half>
C_frag[WARP_TILE_M][WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::fill_fragment(C_frag[i][j], 0.0);
}
}
// only cvta smem base ptr once for cp.async.
uint32_t smem_a_base_ptr = __cvta_generic_to_shared(s_a);
uint32_t smem_b_base_ptr = __cvta_generic_to_shared(s_b);
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); ++k) { // 0, 1
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (k * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (k * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2); // s2->0, s3->1, s4->2
__syncthreads();
#pragma unroll
for (int k = (K_STAGE - 1); k < NUM_K_TILES; k++) {
// s2/4 can use bitwise ops but s3 can not, so, we use mod
// ops for all stages kernel. s2: (k + 1)&1, s4: (k + 1)&3
// s3: (k + 1) % 3
int smem_sel = (k + 1) % K_STAGE; // s3 k 2->0, k 3->1, k 4->2...
int smem_sel_next = k % K_STAGE; // s3 k 2->2, k 3->0, k 4->1...
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
// load stage 2, k start from 2
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (smem_sel_next * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (smem_sel_next * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
// compute stage 0
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + smem_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD)
+ 0); // BK=WMMA_K=16
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + smem_sel * s_b_stage_offset +
0 * (BN + B_PAD) +
warp_smem_b_n); // BK=WMMA_K=16
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2);
__syncthreads();
}
// make sure all memory issues ready.
if ((K_STAGE - 2) > 0) {
CP_ASYNC_WAIT_GROUP(0);
__syncthreads();
}
// processing last (K_STAGE-1) k iters.
{
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); k++) {
const int stage_sel = ((NUM_K_TILES - (K_STAGE - 1) + k) % K_STAGE);
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + stage_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD)
+ 0); // BK=WMMA_K=16
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + stage_sel * s_b_stage_offset +
0 * (BN + B_PAD) +
warp_smem_b_n); // BK=WMMA_K=16
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
}
}
// finally, store back to C matrix.
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
const int store_gmem_a_m = by * BM + warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
const int store_gmem_a_n = bx * BN + warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::store_matrix_sync(C + store_gmem_a_m * N + store_gmem_a_n, C_frag[i][j], N,
wmma::mem_row_major);
}
}
}
// stage with 256x256 block, mma4x4, warp4x4(64,64,16), dynamic smem
// __launch_bounds__: avoid error 'too many resources required for launch'
// reference: https://blog.csdn.net/feng__shuai/article/details/124395023
template<const int WMMA_M=16,
const int WMMA_N=16,
const int WMMA_K=16,
const int WMMA_TILE_M=4,
const int WMMA_TILE_N=4,
const int WARP_TILE_M=4,
const int WARP_TILE_N=4,
const int A_PAD=0,
const int B_PAD=0,
const int K_STAGE=2,
const bool BLOCK_SWIZZLE=false>
__global__ void __launch_bounds__(512)
hgemm_wmma_m16n16k16_mma4x4_warp4x4_stages_dsmem_kernel(
half* A, half* B, half* C, int M, int N, int K) {
// 512 threads(16 warps) per block / 256 threads, 8 warps
// const int bx = blockIdx.x;
// BLOCK_SWIZZLE 0/1 控制是否使用 block swizzle
const int bx = ((int) BLOCK_SWIZZLE) * blockIdx.z * gridDim.x + blockIdx.x;
const int by = blockIdx.y;
const int NUM_K_TILES = div_ceil(K, WMMA_K);
constexpr int BM = WMMA_M * WMMA_TILE_M * WARP_TILE_M; // 16x4*4=256
constexpr int BN = WMMA_N * WMMA_TILE_N * WARP_TILE_N; // 16x4*4=256
constexpr int BK = WMMA_K; // 16
extern __shared__ half smem[];
half* s_a = smem;
half* s_b = smem + K_STAGE * BM * (BK + A_PAD);
constexpr int s_a_stage_offset = BM * (BK + A_PAD);
constexpr int s_b_stage_offset = BK * (BN + B_PAD);
// 要保证相同的warp下thread执行相同的指令
const int tid = threadIdx.y * blockDim.x + threadIdx.x;
const int warp_id = tid / WARP_SIZE; // 0~15 warp_id within block
const int warp_m = warp_id / 4; // 0,1,2,3
const int warp_n = warp_id % 4; // 0,1,2,3
// 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=256 BK=16 按行读取 A行主序
// 对于s_a每行16个数据,每个线程读取8个,需要2个线程;总共256行,需要刚好256x2=512线程
int load_smem_a_m = tid / 2; // row 0~255
int load_smem_a_k = (tid % 2 == 0) ? 0 : 8; // col 0, 8
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=16 BN=256 按行读取 B行主序
// 对于s_b每行256个数据,每个线程读8个数据,需要32个线程;总共16行,需要32x16=512个线程
int load_smem_b_k = tid / 32; // row 0~15
int load_smem_b_n = (tid % 32) * 8; // col 0,8,...,256
// 再计算全局内存中的索引
// 要加载到s_a中的元素对应到A全局内存中的行数 每个block负责出C中大小为BM*BN的块
int load_gmem_a_m = by * BM + load_smem_a_m; // global row of a and c
int load_gmem_b_n = bx * BN + load_smem_b_n; // global col of b and c
wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, half>
C_frag[WARP_TILE_M][WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::fill_fragment(C_frag[i][j], 0.0);
}
}
// only cvta smem base ptr once for cp.async.
uint32_t smem_a_base_ptr = __cvta_generic_to_shared(s_a);
uint32_t smem_b_base_ptr = __cvta_generic_to_shared(s_b);
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); ++k) { // 0, 1
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (k * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (k * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2); // s2->0, s3->1, s4->2
__syncthreads();
#pragma unroll
for (int k = (K_STAGE - 1); k < NUM_K_TILES; k++) {
// s2/4 can use bitwise ops but s3 can not, so, we use mod
// ops for all stages kernel. s2: (k + 1)&1, s4: (k + 1)&3
// s3: (k + 1) % 3
int smem_sel = (k + 1) % K_STAGE; // s3 k 2->0, k 3->1, k 4->2...
int smem_sel_next = k % K_STAGE; // s3 k 2->2, k 3->0, k 4->1...
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * WMMA_K + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * WMMA_K + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
// load stage 2, k start from 2
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (smem_sel_next * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (smem_sel_next * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
// compute stage 0
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + smem_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD)
+ 0); // BK=WMMA_K=16
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + smem_sel * s_b_stage_offset +
0 * (BN + B_PAD) +
warp_smem_b_n); // BK=WMMA_K=16
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2);
__syncthreads();
}
// make sure all memory issues ready.
if ((K_STAGE - 2) > 0) {
CP_ASYNC_WAIT_GROUP(0);
__syncthreads();
}
// processing last (K_STAGE-1) k iters.
{
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); k++) {
const int stage_sel = ((NUM_K_TILES - (K_STAGE - 1) + k) % K_STAGE);
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + stage_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD)
+ 0); // BK=WMMA_K=16
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + stage_sel * s_b_stage_offset +
0 * (BN + B_PAD) +
warp_smem_b_n); // BK=WMMA_K=16
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
}
}
// finally, store back to C matrix.
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
const int store_gmem_a_m = by * BM + warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
const int store_gmem_a_n = bx * BN + warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::store_matrix_sync(C + store_gmem_a_m * N + store_gmem_a_n, C_frag[i][j], N,
wmma::mem_row_major);
}
}
}
// 256x128, stages, mma4x2, warp4x4(64,64,16)
template<const int WMMA_M=16,
const int WMMA_N=16,
const int WMMA_K=16,
const int WMMA_TILE_M=4,
const int WMMA_TILE_N=2,
const int WARP_TILE_M=4,
const int WARP_TILE_N=4,
const int WARP_TILE_K=1,
const int A_PAD=0,
const int B_PAD=0,
const int K_STAGE=2,
const bool BLOCK_SWIZZLE=false>
__global__ void __launch_bounds__(256)
hgemm_wmma_m16n16k16_mma4x2_warp4x4_stages_dsmem_kernel(
half* A, half* B, half* C, int M, int N, int K) {
// 256 threads(8 warps) per block.
// const int bx = blockIdx.x;
// BLOCK_SWIZZLE 0/1 control use block swizzle or not.
const int bx = ((int) BLOCK_SWIZZLE) * blockIdx.z * gridDim.x + blockIdx.x;
const int by = blockIdx.y;
const int NUM_K_TILES = div_ceil(K, WMMA_K * WARP_TILE_K);
constexpr int BM = WMMA_M * WMMA_TILE_M * WARP_TILE_M; // 16x4*4=256
constexpr int BN = WMMA_N * WMMA_TILE_N * WARP_TILE_N; // 16x2*4=128
constexpr int BK = WMMA_K * WARP_TILE_K; // 16*2=32
// s2: 2*128*(32)*2=16KB, 2*32*(128+16)*2=18KB, ~42KB
// s3: 3*128*(32)*2=24KB, 3*32*(128+16)*2=27KB, ~51KB
// s4: 4*128*(32)*2=32KB, 4*32*(128+16)*2=36KB, ~68KB
// s4: 5*128*(32)*2=40KB, 5*32*(128+16)*2=45KB, ~85KB
extern __shared__ half smem[];
half* s_a = smem;
half* s_b = smem + K_STAGE * BM * (BK + A_PAD);
constexpr int s_a_stage_offset = BM * (BK + A_PAD);
constexpr int s_b_stage_offset = BK * (BN + B_PAD);
const int tid = threadIdx.y * blockDim.x + threadIdx.x;
const int warp_id = tid / WARP_SIZE; // 0~7 warp_id within block
const int warp_m = warp_id / 2; // 0,1,2,3
const int warp_n = warp_id % 2; // 0,1
// 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=256 BK=32 按行读取 A行主序
// 对于s_a每行16个数据,每个线程读取16个,需要1个线程;总共256行,刚好256线程
int load_smem_a_m = tid; // row 0~255
int load_smem_a_k = 0; // col 0,16
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=16 BN=128 按行读取 B行主序
// 对于s_b每行128个数据,每个线程读8个数据,需要16个线程;总共16行,需要16x16=256个线程
int load_smem_b_k = tid / 16; // row 0~15
int load_smem_b_n = (tid % 16) * 8; // col 0,8,...,120
// 再计算全局内存中的索引
// 要加载到s_a中的元素对应到A全局内存中的行数 每个block负责出C中大小为BM*BN的块
int load_gmem_a_m = by * BM + load_smem_a_m; // global row of a and c
int load_gmem_b_n = bx * BN + load_smem_b_n; // global col of b and c
wmma::fragment<wmma::accumulator, WMMA_M, WMMA_N, WMMA_K, half>
C_frag[WARP_TILE_M][WARP_TILE_N];
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::fill_fragment(C_frag[i][j], 0.0);
}
}
// only cvta smem base ptr once for cp.async.
uint32_t smem_a_base_ptr = __cvta_generic_to_shared(s_a);
uint32_t smem_b_base_ptr = __cvta_generic_to_shared(s_b);
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); ++k) { // 0, 1
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * (WMMA_K * WARP_TILE_K) + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * (WMMA_K * WARP_TILE_K) + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (k * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (k * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
CP_ASYNC_CG(load_smem_a_ptr + 16, &A[load_gmem_a_addr + 8], 16);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2); // s2->0, s3->1, s4->2
__syncthreads();
#pragma unroll
for (int k = (K_STAGE - 1); k < NUM_K_TILES; k++) {
// s2/4 can use bitwise ops but s3 can not, so, we use mod
// ops for all stages kernel. s2: (k + 1)&1, s4: (k + 1)&3
// s3: (k + 1) % 3
int smem_sel = (k + 1) % K_STAGE; // s3 k 2->0, k 3->1, k 4->2...
int smem_sel_next = k % K_STAGE; // s3 k 2->2, k 3->0, k 4->1...
// k * WMMA_K, WMMA_K=16 -> (k << 4)
int load_gmem_a_k = k * (WMMA_K * WARP_TILE_K) + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
int load_gmem_b_k = k * (WMMA_K * WARP_TILE_K) + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
// load stage 2, k start from 2
uint32_t load_smem_a_ptr = (
smem_a_base_ptr + (smem_sel_next * s_a_stage_offset +
load_smem_a_m * (BK + A_PAD) +
load_smem_a_k) * sizeof(half)
);
uint32_t load_smem_b_ptr = (
smem_b_base_ptr + (smem_sel_next * s_b_stage_offset +
load_smem_b_k * (BN + B_PAD) +
load_smem_b_n) * sizeof(half)
);
CP_ASYNC_CG(load_smem_a_ptr, &A[load_gmem_a_addr], 16);
CP_ASYNC_CG(load_smem_a_ptr + 16, &A[load_gmem_a_addr + 8], 16);
CP_ASYNC_CG(load_smem_b_ptr, &B[load_gmem_b_addr], 16);
CP_ASYNC_COMMIT_GROUP();
#pragma unroll
for (int warp_k = 0; warp_k < WARP_TILE_K; ++warp_k) {
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
const int warp_smem_k = warp_k * WMMA_K; // 0,16
// compute stage 0
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + smem_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD) +
warp_smem_k);
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + smem_sel * s_b_stage_offset +
warp_smem_k * (BN + B_PAD) +
warp_smem_b_n);
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
}
CP_ASYNC_WAIT_GROUP(K_STAGE-2);
__syncthreads();
}
// make sure all memory issues ready.
if ((K_STAGE - 2) > 0) {
CP_ASYNC_WAIT_GROUP(0);
__syncthreads();
}
// processing last (K_STAGE-1) k iters.
{
#pragma unroll
for (int k = 0; k < (K_STAGE - 1); k++) {
const int stage_sel = ((NUM_K_TILES - (K_STAGE - 1) + k) % K_STAGE);
#pragma unroll
for (int warp_k = 0; warp_k < WARP_TILE_K; ++warp_k) {
wmma::fragment<wmma::matrix_a, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> A_frag[WARP_TILE_M];
wmma::fragment<wmma::matrix_b, WMMA_M, WMMA_N, WMMA_K, half,
wmma::row_major> B_frag[WARP_TILE_N];
const int warp_smem_k = warp_k * WMMA_K; // 0,16
// compute stage 0
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
// load 2 tiles -> reg, smem a -> frags a, warp_m 0~3
int warp_smem_a_m = warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
half* load_smem_a_frag_ptr = (s_a + stage_sel * s_a_stage_offset +
warp_smem_a_m * (BK + A_PAD) +
warp_smem_k);
wmma::load_matrix_sync(A_frag[i], load_smem_a_frag_ptr, BK + A_PAD);
}
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
// load 4 tiles -> reg, smem b -> frags b, warp_n 0~2
int warp_smem_b_n = warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
half* load_smem_b_frag_ptr = (s_b + stage_sel * s_b_stage_offset +
warp_smem_k * (BN + B_PAD) +
warp_smem_b_n);
wmma::load_matrix_sync(B_frag[j], load_smem_b_frag_ptr, BN + B_PAD);
}
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
wmma::mma_sync(C_frag[i][j], A_frag[i], B_frag[j], C_frag[i][j]);
}
}
}
}
}
// finally, store back to C matrix.
#pragma unroll
for (int i = 0; i < WARP_TILE_M; ++i) {
#pragma unroll
for (int j = 0; j < WARP_TILE_N; ++j) {
const int store_gmem_a_m = by * BM + warp_m * (WMMA_M * WARP_TILE_M) + i * WMMA_M;
const int store_gmem_a_n = bx * BN + warp_n * (WMMA_N * WARP_TILE_N) + j * WMMA_N;
wmma::store_matrix_sync(C + store_gmem_a_m * N + store_gmem_a_n, C_frag[i][j], N,
wmma::mem_row_major);
}
}
}
// TODO: Warp swizzle/permute support ? (MMA, not WMMA)
// --------------------- PyTorch bindings for custom kernel -----------------------
#define STRINGFY(str) #str
#define TORCH_BINDING_COMMON_EXTENSION(func) \
m.def(STRINGFY(func), &func, STRINGFY(func));
#define CHECK_TORCH_TENSOR_DTYPE(T, th_type) \
if(((T).options().dtype() != (th_type))) { \
std::cout << "Tensor Info:" << (T).options() << std::endl; \
throw std::runtime_error("values must be "#th_type); \
}
#define CHECK_TORCH_TENSOR_SHAPE(T, S0, S1) \
if (((T).size(0) != (S0)) || ((T).size(1) != (S1))) { \
throw std::runtime_error("Tensor size mismatch!"); \
}
// 128x128, mma4x2, warp2x4(32,64) w/o dynamic smem
#define LAUNCH_161616_STAGE_SWIZZLE_MMA4x2_WARP2x4_KERNEL(stages, stride) \
{ \
const int N_SWIZZLE = (N + (stride) - 1) / (stride); \
dim3 block(NUM_THREADS); \
dim3 grid((div_ceil(N, BN) + N_SWIZZLE - 1) / N_SWIZZLE, \
div_ceil(M, BM), \
N_SWIZZLE); \
hgemm_wmma_m16n16k16_mma4x2_warp2x4_stages_kernel< \
WMMA_M, WMMA_N, WMMA_K, WMMA_TILE_M, WMMA_TILE_N, \
WARP_TILE_M, WARP_TILE_N, A_PAD, B_PAD, \
(stages), true><<<grid, block>>>( \
reinterpret_cast<half*>(a.data_ptr()), \
reinterpret_cast<half*>(b.data_ptr()), \
reinterpret_cast<half*>(c.data_ptr()), \
M, N, K \
); \
}
#define LAUNCH_161616_STAGE_NO_SWIZZLE_MMA4x2_WARP2x4_KERNEL(stages) \
{ \
dim3 block(NUM_THREADS); \
dim3 grid(div_ceil(N, BN), div_ceil(M, BM)); \
hgemm_wmma_m16n16k16_mma4x2_warp2x4_stages_kernel< \
WMMA_M, WMMA_N, WMMA_K, WMMA_TILE_M, WMMA_TILE_N, \
WARP_TILE_M, WARP_TILE_N, A_PAD, B_PAD, \
(stages), false><<<grid, block>>>( \
reinterpret_cast<half*>(a.data_ptr()), \
reinterpret_cast<half*>(b.data_ptr()), \
reinterpret_cast<half*>(c.data_ptr()), \
M, N, K \
); \
}
// 128x128, mma4x2, warp2x4(32,64) stage 2/3/4 w/o block swizzle across N dim, static smem < 48KB
void hgemm_wmma_m16n16k16_mma4x2_warp2x4_stages(
torch::Tensor a, torch::Tensor b, torch::Tensor c,
int stages, bool swizzle, int swizzle_stride) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)