-
Notifications
You must be signed in to change notification settings - Fork 152
/
hgemm.cu
1071 lines (963 loc) · 47.3 KB
/
hgemm.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 <torch/types.h>
#include <torch/extension.h>
#define WARP_SIZE 32
#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 LDST64BITS(value) (reinterpret_cast<float2*>(&(value))[0])
#define LDST128BITS(value) (reinterpret_cast<float4*>(&(value))[0])
// -------------------------------------- FP16 --------------------------------------
// HGEMM naive: compute one c[i,j] element per threads, all row major
__global__ void hgemm_naive_f16_kernel(half* a, half* b, half* c, int M, int N, int K) {
int n = blockIdx.x * blockDim.x + threadIdx.x;
int m = blockIdx.y * blockDim.y + threadIdx.y;
if (m < M && n < N) {
half psum = 0.0;
#pragma unroll
for (int k = 0; k < K; k++) {
// m row in a matrix, n col in b matrix
psum += a[m * K + k] * b[k * N + n];
}
c[m * N + n] = psum; // c[m,n]
}
}
// HGEMM: Block Tile + K Tile, with smem
// Block Tile (BM, BN) + K Tile (BK=32)
// grid((N + BN - 1) / BN, (M + BM - 1) / BM), block(BN, BM)
// a: MxK, b: KxN, c: MxN, compute: c = a * b, all row major
template<const int BM=32, const int BN=32, const int BK=32>
__global__ void hgemm_sliced_k_f16_kernel(half* a, half* b, half* c, int M, int N, int K) {
// [1] Block Tile: 32x32的block处理c上一块32x32的元素计算
// [2] K Tile: 使用共享内存,并将K分块为BK大小的块
__shared__ half s_a[BM][BK], s_b[BK][BN];
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;
int tid = threadIdx.y * blockDim.x + tx; // tid within the block
// load values to shared memory, 32x32 threads working together
// to fetch data along the row direction of a and b both for s_a
// and s_b 32x32x4x2=8KB, we use 32x32 threads within block to
// load 32x32 elements from global memory to shared memory, namely,
// each thread will load 1 element.
int load_smem_a_m = tid / 32; // 0~31, tid / 32, tid / BM, threadIdx.y
int load_smem_a_k = tid % 32; // 0~31, tid % 32, tid % BK, threadIdx.x
int load_smem_b_k = tid / 32; // 0~31, tid / 32, tid / BK, threadIdx.y
int load_smem_b_n = tid % 32; // 0~31, tid % 32, tid % BN, threadIdx.x
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
// if (load_gmem_a_m >= M || load_gmem_b_n >= N) return;
half sum = __float2half(0.f);
for (int bk = 0; bk < (K + BK - 1) / BK; ++bk) {
int load_gmem_a_k = bk * BK + load_smem_a_k;
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
s_a[load_smem_a_m][load_smem_a_k] = a[load_gmem_a_addr];
int load_gmem_b_k = bk * BK + load_smem_b_k;
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
s_b[load_smem_b_k][load_smem_b_n] = b[load_gmem_b_addr];
__syncthreads();
#pragma unroll
for (int k = 0; k < BK; ++k) {
int comp_smem_a_m = load_smem_a_m;
int comp_smem_b_n = load_smem_b_n;
sum += s_a[comp_smem_a_m][k] * s_b[k][comp_smem_b_n];
}
__syncthreads();
}
int store_gmem_c_m = load_gmem_a_m;
int store_gmem_c_n = load_gmem_b_n;
int store_gmem_c_addr = store_gmem_c_m * N + store_gmem_c_n;
c[store_gmem_c_addr] = sum;
}
// HGEMM: Block Tile + Thread Tile + K Tile + half2x2, with smem
// BK:TILE_K=8 BM=BN=128
// TM=TN=8 增加计算密度 BM/TM=16 BN/TN=16
// dim3 blockDim(BN/TN, BM/TM);
// dim3 gridDim((N + BN - 1) / BN, (M + BM - 1) / BM)
template<const int BM=128, const int BN=128, const int BK=8, const int TM=8, const int TN=8>
__global__ void hgemm_t_8x8_sliced_k_f16x4_kernel(half* a, half* b, half* c, int M, int N, int K) {
// [1] Block Tile: 一个16x16的block处理C上大小为128X128的一个目标块
// [2] Thread Tile: 每个thread负责计算TM*TN(8*8)个元素,增加计算密度
// [3] K Tile: 将K分块,每块BK大小,迭代(K+BK-1/BK)次,
// 每次计算TM*TN个元素各自的部分乘累加
// [4] Vectorize: 减少load和store指令,使用half2
// 线程总数16x16=256,每个线程负责计算8x8的元素
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;
int tid = threadIdx.y * blockDim.x + tx; // tid within the block
__shared__ half s_a[BM][BK], s_b[BK][BN]; // 2*128*8*2=4KB
// 0. 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=128 BK=8 按行读取 A行主序
// 对于s_a每行8个数据,每个线程读取4个,需要2个线程;总共128行,需要128x2刚好256线程
int load_smem_a_m = tid / 2; // tid/2 (128/8)*(128/8)=256 threads per block, tid/2->[0,128), BM=128 0~127
int load_smem_a_k = (tid % 2 == 0) ? 0 : 4; // (tid%2 == 0) ? 0 : 4, col of s_a 0,4
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=8 BN=128 按行读取 B行主序
// 对于s_b每行128个数据,每个线程读4个数据,需要32个线程;总共8行,需要32x8=256个线程
int load_smem_b_k = tid / 32; // tid/32, row of s_b 256/32=8 行 0~7
int load_smem_b_n = (tid % 32) * 4; // (tid % 32) * 4, col of s_b 0,4,...,124
// 1. 再计算全局内存中的索引
// 要加载到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
half r_c[TM][TN] = {__float2half(0.0f)}; // 8x8
// 2. 先对K进行分块,每块BK大小
for (int bk = 0; bk < (K + BK - 1) / BK; ++bk) {
// 加载数据到共享内存smem s_a BM*BK 128*8 vectorize float4
int load_gmem_a_k = bk * BK + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
HALF2(s_a[load_smem_a_m][load_smem_a_k + 0]) = HALF2(a[load_gmem_a_addr + 0]);
HALF2(s_a[load_smem_a_m][load_smem_a_k + 2]) = HALF2(a[load_gmem_a_addr + 2]);
// 加载数据到共享内存smem s_b BK*BN 8*128 vectorize float4
int load_gmem_b_k = bk * BK + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
HALF2(s_b[load_smem_b_k][load_smem_b_n + 0]) = HALF2(b[load_gmem_b_addr + 0]);
HALF2(s_b[load_smem_b_k][load_smem_b_n + 2]) = HALF2(b[load_gmem_b_addr + 2]);
__syncthreads();
#pragma unroll
for (int k = 0; k < BK; k++) {
// 3. 每个线程负责计算BM*BN(12x128)中的TM*TN(8x8)个元素
#pragma unroll
for (int m = 0; m < TM; m++) {
#pragma unroll
for (int n = 0; n < TN; n++) {
// k from 0~7,0 ~ BK, ty and tx range from 0 to 15, 16x8=128
int comp_smem_a_m = ty * TM + m; // 128*8 128/TM(8)=16 M方向 16线程
int comp_smem_b_n = tx * TN + n; // 8*128 128/TN(8)=16 N方向 16线程
r_c[m][n] += s_a[comp_smem_a_m][k] * s_b[k][comp_smem_b_n];
}
}
}
__syncthreads();
}
#pragma unroll
for (int m = 0; m < TM; ++m) {
int store_gmem_c_m = by * BM + ty * TM + m;
#pragma unroll
for (int n = 0; n < TN; n += 2) {
int store_gmem_c_n = bx * BN + tx * TN + n;
int store_gmem_c_addr = store_gmem_c_m * N + store_gmem_c_n;
HALF2(c[store_gmem_c_addr]) = HALF2(r_c[m][n]);
}
}
}
template<const int BM=128, const int BN=128, const int BK=8, const int TM=8, const int TN=8>
__global__ void hgemm_t_8x8_sliced_k_f16x4_pack_kernel(
half* a, half* b, half* c, int M, int N, int K) {
// [1] Block Tile: 一个16x16的block处理C上大小为128X128的一个目标块
// [2] Thread Tile: 每个thread负责计算TM*TN(8*8)个元素,增加计算密度
// [3] K Tile: 将K分块,每块BK大小,迭代(K+BK-1/BK)次,
// 每次计算TM*TN个元素各自的部分乘累加
// [4] Vectorize: 减少load和store指令,使用half2
// 线程总数16x16=256,每个线程负责计算8x8的元素
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;
int tid = threadIdx.y * blockDim.x + tx; // tid within the block
__shared__ half s_a[BM][BK], s_b[BK][BN]; // 2*128*8*2=4KB
// 0. 先计算shared memory中的索引
// tid和需要加载的smem s_a[BM][BK] 之间的索引关系 BM=128 BK=8 按行读取 A行主序
// 对于s_a每行8个数据,每个线程读取4个,需要2个线程;总共128行,需要128x2刚好256线程
int load_smem_a_m = tid / 2; // tid/2 (128/8)*(128/8)=256 threads per block, tid/2->[0,128), BM=128 0~127
int load_smem_a_k = (tid % 2 == 0) ? 0 : 4; // (tid%2 == 0) ? 0 : 4, col of s_a 0,4
// tid和需要加载的smem s_b[BK][BN] 之间的索引关系 BK=8 BN=128 按行读取 B行主序
// 对于s_b每行128个数据,每个线程读4个数据,需要32个线程;总共8行,需要32x8=256个线程
int load_smem_b_k = tid / 32; // tid/32, row of s_b 256/32=8 行 0~7
int load_smem_b_n = (tid % 32) * 4; // (tid % 32) * 4, col of s_b 0,4,...,124
// 1. 再计算全局内存中的索引
// 要加载到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
half r_c[TM][TN] = {__float2half(0.0f)}; // 8x8
// 2. 先对K进行分块,每块BK大小
for (int bk = 0; bk < (K + BK - 1) / BK; ++bk) {
// 加载数据到共享内存smem s_a BM*BK 128*8 vectorize float4
int load_gmem_a_k = bk * BK + load_smem_a_k; // global col of a
int load_gmem_a_addr = load_gmem_a_m * K + load_gmem_a_k;
LDST64BITS(s_a[load_smem_a_m][load_smem_a_k]) = LDST64BITS(a[load_gmem_a_addr]);
// 加载数据到共享内存smem s_b BK*BN 8*128 vectorize float4
int load_gmem_b_k = bk * BK + load_smem_b_k; // global row of b
int load_gmem_b_addr = load_gmem_b_k * N + load_gmem_b_n;
LDST64BITS(s_b[load_smem_b_k][load_smem_b_n]) = LDST64BITS(b[load_gmem_b_addr]);
__syncthreads();
#pragma unroll
for (int k = 0; k < BK; k++) {
// 3. 每个线程负责计算BM*BN(12x128)中的TM*TN(8x8)个元素
#pragma unroll
for (int m = 0; m < TM; m++) {
#pragma unroll
for (int n = 0; n < TN; n++) {
// k from 0~7,0 ~ BK, ty and tx range from 0 to 15, 16x8=128
int comp_smem_a_m = ty * TM + m; // 128*8 128/TM(8)=16 M方向 16线程
int comp_smem_b_n = tx * TN + n; // 8*128 128/TN(8)=16 N方向 16线程
// r_c[m][n] += s_a[comp_smem_a_m][k] * s_b[k][comp_smem_b_n];
r_c[m][n] = __hfma(s_a[comp_smem_a_m][k], s_b[k][comp_smem_b_n],
r_c[m][n]); // HFMA(x,y,z)=x*y+z
}
}
}
__syncthreads();
}
#pragma unroll
for (int m = 0; m < TM; ++m) {
int store_gmem_c_m = by * BM + ty * TM + m;
#pragma unroll
for (int n = 0; n < TN; n += 4) {
int store_gmem_c_n = bx * BN + tx * TN + n;
int store_gmem_c_addr = store_gmem_c_m * N + store_gmem_c_n;
LDST64BITS(c[store_gmem_c_addr]) = LDST64BITS(r_c[m][n]);
}
}
}
template<const int BM=128, const int BN=128, const int BK=8, const int TM=8, const int TN=8>
__global__ void hgemm_t_8x8_sliced_k_f16x4_bcf_kernel(
half* a, half* b, half* c, const int M, const int N, const int K) {
const int bx = blockIdx.x;
const int by = blockIdx.y;
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
__shared__ half s_a[BK][BM];
__shared__ half s_b[BK][BN];
half r_load_a[TM/2]; // 4
half r_load_b[TN/2]; // 4
half r_comp_a[TM];
half r_comp_b[TN];
half r_c[TM][TN] = {__float2half(0.0f)};
// mapping tid to s_a[BK][BM], for each orginal m-th row, load 4 + 4 K-dim
// row major values from A matrix, and store it in COL major s_a[BK][BM].
int load_a_smem_m = tid / 2; // tid / 2,(0,1,2,...,128)
// (0b00000000 & 0b00000001) << 2 = 0
// (0b00000001 & 0b00000001) << 2 = 4
// (0b00000010 & 0b00000001) << 2 = 0
// (0b00000011 & 0b00000001) << 2 = 4
int load_a_smem_k = (tid & 1) << 2; // (0,4)
// mapping tid to s_b[BK][BN], for each orginal k-th row, load 4 + 4 N-dim
// row major values from B matrix, and store it in ROW major s_b[BK][BN].
int load_b_smem_k = tid / 32; // 0~8
// (0b00000000 & 0b00011111) << 2 = 0
// (0b00000001 & 0b00011111) << 2 = 4
// (0b00000010 & 0b00011111) << 2 = 8
// (0b00000011 & 0b00011111) << 2 = 12
int load_b_smem_n = (tid & 31) << 2; // (0,4,8,12,...,124)
int load_a_gmem_m = by * BM + load_a_smem_m;
int load_b_gmem_n = bx * BN + load_b_smem_n;
for (int bk = 0; bk < (K + BK - 1) / BK; bk++) {
int load_a_gmem_k = bk * BK + load_a_smem_k;
int load_a_gmem_addr = load_a_gmem_m * K + load_a_gmem_k;
int load_b_gmem_k = bk * BK + load_b_smem_k;
int load_b_gmem_addr = load_b_gmem_k * N + load_b_gmem_n;
HALF2(r_load_a[0]) = HALF2(a[load_a_gmem_addr + 0]);
HALF2(r_load_a[2]) = HALF2(a[load_a_gmem_addr + 2]);
HALF2(r_load_b[0]) = HALF2(b[load_b_gmem_addr + 0]);
HALF2(r_load_b[2]) = HALF2(b[load_b_gmem_addr + 2]);
s_a[load_a_smem_k ][load_a_smem_m] = r_load_a[0];
s_a[load_a_smem_k + 1][load_a_smem_m] = r_load_a[1];
s_a[load_a_smem_k + 2][load_a_smem_m] = r_load_a[2];
s_a[load_a_smem_k + 3][load_a_smem_m] = r_load_a[3];
HALF2(s_b[load_b_smem_k][load_b_smem_n + 0]) = HALF2(r_load_b[0]);
HALF2(s_b[load_b_smem_k][load_b_smem_n + 2]) = HALF2(r_load_b[2]);
__syncthreads();
#pragma unroll
for (int tk = 0; tk < BK; tk++) {
HALF2(r_comp_a[0]) = HALF2(s_a[tk][ty * TM / 2 ]);
HALF2(r_comp_a[2]) = HALF2(s_a[tk][ty * TM / 2 + 2 ]);
HALF2(r_comp_a[4]) = HALF2(s_a[tk][ty * TM / 2 + BM / 2 ]);
HALF2(r_comp_a[6]) = HALF2(s_a[tk][ty * TM / 2 + BM / 2 + 2]);
HALF2(r_comp_b[0]) = HALF2(s_b[tk][tx * TN / 2 ]);
HALF2(r_comp_b[2]) = HALF2(s_b[tk][tx * TN / 2 + 2 ]);
HALF2(r_comp_b[4]) = HALF2(s_b[tk][tx * TN / 2 + BN / 2 ]);
HALF2(r_comp_b[6]) = HALF2(s_b[tk][tx * TN / 2 + BN / 2 + 2]);
#pragma unroll
for (int tm = 0; tm < TM; tm++) {
#pragma unroll
for (int tn = 0; tn < TN; tn++) {
r_c[tm][tn] += r_comp_a[tm] * r_comp_b[tn];
}
}
}
__syncthreads();
}
#pragma unroll
for (int i = 0; i < TM / 2; i++) {
int store_c_gmem_m = by * BM + ty * TM / 2 + i;
int store_c_gmem_n = bx * BN + tx * TN / 2;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
HALF2(c[store_c_gmem_addr + 0]) = HALF2(r_c[i][0]);
HALF2(c[store_c_gmem_addr + 2]) = HALF2(r_c[i][2]);
HALF2(c[store_c_gmem_addr + BN / 2 + 0]) = HALF2(r_c[i][4]);
HALF2(c[store_c_gmem_addr + BN / 2 + 2]) = HALF2(r_c[i][6]);
}
#pragma unroll
for (int i = 0; i < TM / 2; i++) {
int store_c_gmem_m = by * BM + BM / 2 + ty * TM / 2 + i;
int store_c_gmem_n = bx * BN + tx * TN / 2;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
HALF2(c[store_c_gmem_addr + 0]) = HALF2(r_c[i + TM / 2][0]);
HALF2(c[store_c_gmem_addr + 2]) = HALF2(r_c[i + TM / 2][2]);
HALF2(c[store_c_gmem_addr + BN / 2 + 0]) = HALF2(r_c[i + TM / 2][4]);
HALF2(c[store_c_gmem_addr + BN / 2 + 2]) = HALF2(r_c[i + TM / 2][6]);
}
}
template<const int BM=128,
const int BN=128,
const int BK=8,
const int TM=8,
const int TN=8,
const int OFFSET=0>
__global__ void hgemm_t_8x8_sliced_k_f16x4_pack_bcf_kernel(
half* a, half* b, half* c, const int M, const int N, const int K) {
// threads: 128/8 * 128/8 = 256
const int bx = blockIdx.x;
const int by = blockIdx.y;
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
__shared__ half s_a[BK][BM + OFFSET]; // 8*128*2=2KB
__shared__ half s_b[BK][BN + OFFSET]; // 8*128*2=2KB
half r_load_a[TM/2]; // 4
half r_load_b[TN/2]; // 4
half r_comp_a[TM]; // 8
half r_comp_b[TN]; // 8
half r_c[TM][TN] = {__float2half(0.0f)}; // 8x8
// mapping tid to s_a[BK][BM], for each orginal m-th row, load 4 + 4 K-dim
// row major values from A matrix, and store it in COL major s_a[BK][BM].
int load_a_smem_m = tid / 2; // tid / 2,(0,1,2,...,128)
// (0b00000000 & 0b00000001) << 2 = 0
// (0b00000001 & 0b00000001) << 2 = 4
// (0b00000010 & 0b00000001) << 2 = 0
// (0b00000011 & 0b00000001) << 2 = 4
int load_a_smem_k = (tid & 1) << 2; // (0,4)
// mapping tid to s_b[BK][BN], for each orginal k-th row, load 4 + 4 N-dim
// row major values from B matrix, and store it in ROW major s_b[BK][BN].
int load_b_smem_k = tid / 32; // 0~8
// (0b00000000 & 0b00011111) << 2 = 0
// (0b00000001 & 0b00011111) << 2 = 4
// (0b00000010 & 0b00011111) << 2 = 8
// (0b00000011 & 0b00011111) << 2 = 12
int load_b_smem_n = (tid & 31) << 2; // (0,4,8,12,...,124)
int load_a_gmem_m = by * BM + load_a_smem_m;
int load_b_gmem_n = bx * BN + load_b_smem_n;
for (int bk = 0; bk < (K + BK - 1) / BK; bk++) {
int load_a_gmem_k = bk * BK + load_a_smem_k;
int load_a_gmem_addr = load_a_gmem_m * K + load_a_gmem_k;
int load_b_gmem_k = bk * BK + load_b_smem_k;
int load_b_gmem_addr = load_b_gmem_k * N + load_b_gmem_n;
LDST64BITS(r_load_a[0]) = LDST64BITS(a[load_a_gmem_addr]);
LDST64BITS(r_load_b[0]) = LDST64BITS(b[load_b_gmem_addr]);
// 0. bank layout analysis: s_a[8][128]
// 4 bytes per bank(32 banks, total 128 bytes, 64 half values),
// 2 half per bank. smem banks layout for s_a[8][128]:
// [k=0][m= [0,1], [2,3], [4,5],..., [62,63]]
// layer_0 [ b0], [ b1], [ b2],..., [ b31]
// [k=0][m=[64,65], [66,67], [68,69],..., [126,127]]
// layer_1 [ b0], [ b1], [ b2],..., [ b31]
// [k=1][m= [0,1], [2,3], [4,5],..., [62,63]]
// layer_2 [ b0], [ b1], [ b2],..., [ b31]
// [k=1][m=[64,65], [66,67], [68,69],..., [126,127]]
// layer_3 [ b0], [ b1], [ b2],..., [ b31]
// ... ... ... ... ...
// [k=7][m= [0,1], [2,3], [4,5],..., [62,63]]
// layer_14 [ b0], [ b1], [ b2],..., [ b31]
// [k=7][m=[64,65], [66,67], [68,69],..., [126,127]]
// layer_15 [ b0], [ b1], [ b2],..., [ b31]
// 1. bank conficts analysis: s_a[8][128]
// tid 0 -> m 0, k 0 -> all access bank 0 (layer_0/2/4/6)
// tid 1 -> m 0, k 4 -> all access bank 0 (layer_8/10/12/14)
// tid 2 -> m 1, k 0 -> all access bank 0 (layer_0/2/4/6)
// tid 3 -> m 1, k 4 -> all access bank 0 (layer_8/10/12/14)
// tid 4 -> m 2, k 0 -> all access bank 1 (layer_0/2/4/6)
// tid 5 -> m 2, k 4 -> all access bank 1 (layer_8/10/12/14)
// tid 6 -> m 3, k 0 -> all access bank 1 (layer_0/2/4/6)
// tid 7 -> m 3, k 4 -> all access bank 1 (layer_8/10/12/14)
// ... ... ... ...
// tid 28 -> m 14, k 0 -> all access bank 7 (layer_0/2/4/6)
// tid 29 -> m 14, k 4 -> all access bank 7 (layer_8/10/12/14)
// tid 30 -> m 15, k 0 -> all access bank 7 (layer_0/2/4/6)
// tid 31 -> m 15, k 4 -> all access bank 7 (layer_8/10/12/14)
// ... ... ... ...
// tid 252 -> m 126, k 0 -> all access bank 30 (layer_1/3/5/7)
// tid 253 -> m 126, k 4 -> all access bank 30 (layer_9/11/13/15)
// tid 254 -> m 127, k 0 -> all access bank 31 (layer_1/3/5/7)
// tid 255 -> m 127, k 4 -> all access bank 31 (layer_9/11/13/15)
// conclusion: we still have bank conflicts for smem_a write access,
// each 4 consecutive threads within warp access the same bank!
// thus, we still need 4 memory issues as least per warp.
s_a[load_a_smem_k ][load_a_smem_m] = r_load_a[0]; // e.g layer_0 b0
s_a[load_a_smem_k + 1][load_a_smem_m] = r_load_a[1]; // e.g layer_2 b0
s_a[load_a_smem_k + 2][load_a_smem_m] = r_load_a[2]; // e.g layer_4 b0
s_a[load_a_smem_k + 3][load_a_smem_m] = r_load_a[3]; // e.g layer_6 b0
// 2. bank layout analysis: s_b[8][128] same as s_a[8][128]
// 3. bank conficts analysis: s_b[8][128]
// tid 0 -> k 0, n 0 -> all access bank 0&1 (layer_0)
// tid 1 -> k 0, n 4 -> all access bank 2&3 (layer_0)
// tid 2 -> k 0, n 8 -> all access bank 4&5 (layer_0)
// ... ... ... ...
// tid 15 -> k 0, n 60 -> all access bank 30&31 (layer_0)
// tid 16 -> k 0, n 64 -> all access bank 0&1 (layer_1)
// ... ... ... ...
// tid 31 -> k 0, n 124 -> all access bank 30&31 (layer_1)
// conclusion: we still have bank conflicts within warp, 0&16 -> bank 0,
// 1&17 -> bank 1, etc. we still need 2 memory issues at least per warp.
LDST64BITS(s_b[load_b_smem_k][load_b_smem_n]) = LDST64BITS(r_load_b[0]);
__syncthreads();
#pragma unroll
for (int tk = 0; tk < BK; tk++) {
// bank conflicts analysis, tx/ty 0~15, 0~7 bank 4*8=32 bytes
// tid 0~15 access bank 0~1, tid 16~31 access bank 2~3, etc.
// tid 0, tk 0 -> ty 0 -> [0][0+0~3],[0][64+0~3] -> bank 0~1(layer_0/1), same address
// tid 0, tk 7 -> ty 0 -> [7][0+0~3],[0][64+0~3] -> bank 0~1(layer_14/15), same address
// tid 15, tk 0 -> ty 0 -> [0][0+0~3],[0][64+0~3] -> bank 0~1(layer_0/1), same address
// tid 15, tk 7 -> ty 0 -> [7][0+0~3],[0][64+0~3] -> bank 0~1(layer_14/15), same address
// tid 16, tk 0 -> ty 1 -> [0][0+4~7],[0][64+4~7] -> bank 2~3(layer_0/1), same address
// tid 16, tk 7 -> ty 1 -> [7][0+4~7],[0][64+4~7] -> bank 2~3(layer_14/15), same address
// tid 31, tk 0 -> ty 1 -> [0][0+4~7],[0][64+4~7] -> bank 2~3(layer_0/1), same address
// tid 31, tk 7 -> ty 1 -> [7][0+4~7],[0][64+4~7] -> bank 2~3(layer_14/15), same address
LDST64BITS(r_comp_a[0]) = LDST64BITS(s_a[tk][ty * TM / 2 ]);
LDST64BITS(r_comp_a[4]) = LDST64BITS(s_a[tk][ty * TM / 2 + BM / 2]);
// if (tid == < 32 && bx == 0 && by == 0) {
// printf("tid: %d, tx: %d, ty: %d, [%d][%d]\n", tid, tx, ty, tk, ty * TM / 2);
// printf("tid: %d, tx: %d, ty: %d, [%d][%d]\n", tid, tx, ty, tk, ty * TM / 2 + BM / 2);
// }
// conclusion: still have bank conflicts.
// tid 0/16 access bank 0~1, tid 1/17 access bank 2~3, tid 15/31 access bank 30~31.
// tid 2/10/18/26 access bank 8~11, tid 7/15/23/31 access bank 28~31, etc.
// tid 0, tk 0 -> tx 0 -> [0][0+0~3],[0][64+0~3] -> bank 0~1(layer_0/1), same address
// tid 0, tk 7 -> tx 0 -> [7][0+0~3],[0][64+0~3] -> bank 0~1(layer_14/15), same address
// tid 1, tk 0 -> tx 1 -> [0][0+4~7],[0][64+4~7] -> bank 2~3(layer_0/1), same address
// tid 1, tk 7 -> tx 1 -> [7][0+4~7],[0][64+4~7] -> bank 2~3(layer_14/15), same address
LDST64BITS(r_comp_b[0]) = LDST64BITS(s_b[tk][tx * TN / 2 ]);
LDST64BITS(r_comp_b[4]) = LDST64BITS(s_b[tk][tx * TN / 2 + BN / 2]);
// conclusion: s_b still have many bank conflicts within warp,
// tid 0/16 access same bank 0&1, etc. need 2 memory issues.
#pragma unroll
for (int tm = 0; tm < TM; tm++) {
#pragma unroll
for (int tn = 0; tn < TN; tn++) {
// r_c[tm][tn] += r_comp_a[tm] * r_comp_b[tn];
r_c[tm][tn] = __hfma(r_comp_a[tm], r_comp_b[tn], r_c[tm][tn]);
}
}
}
__syncthreads();
}
#pragma unroll
for (int i = 0; i < TM / 2; i++) {
int store_c_gmem_m = by * BM + ty * TM / 2 + i;
int store_c_gmem_n = bx * BN + tx * TN / 2;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
LDST64BITS(c[store_c_gmem_addr ]) = LDST64BITS(r_c[i][0]);
LDST64BITS(c[store_c_gmem_addr + BN / 2]) = LDST64BITS(r_c[i][4]);
}
#pragma unroll
for (int i = 0; i < TM / 2; i++) {
int store_c_gmem_m = by * BM + BM / 2 + ty * TM / 2 + i;
int store_c_gmem_n = bx * BN + tx * TN / 2;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
LDST64BITS(c[store_c_gmem_addr ]) = LDST64BITS(r_c[i + TM / 2][0]);
LDST64BITS(c[store_c_gmem_addr + BN / 2]) = LDST64BITS(r_c[i + TM / 2][4]);
}
}
template<const int BM=128,
const int BN=128,
const int BK=8,
const int TM=8,
const int TN=8,
const int OFFSET=0>
__global__ void hgemm_t_8x8_sliced_k_f16x8_pack_bcf_kernel(
half* a, half* b, half* c, const int M, const int N, const int K) {
// threads: 128/8 * 128/8 = 256
const int bx = blockIdx.x;
const int by = blockIdx.y;
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
__shared__ half s_a[BK][BM + OFFSET]; // 8*128*2=2KB
__shared__ half s_b[BK][BN + OFFSET]; // 8*128*2=2KB
half r_load_a[TM/2]; // 4
half r_load_b[TN/2]; // 4
half r_comp_a[TM]; // 8
half r_comp_b[TN]; // 8
half r_c[TM][TN] = {__float2half(0.0f)}; // 8x8
// mapping tid to s_a[BK][BM], for each orginal m-th row, load 4 + 4 K-dim
// row major values from A matrix, and store it in COL major s_a[BK][BM].
int load_a_smem_m = tid / 2; // tid / 2,(0,1,2,...,128)
// (0b00000000 & 0b00000001) << 2 = 0
// (0b00000001 & 0b00000001) << 2 = 4
// (0b00000010 & 0b00000001) << 2 = 0
// (0b00000011 & 0b00000001) << 2 = 4
int load_a_smem_k = (tid & 1) << 2; // (0,4)
// mapping tid to s_b[BK][BN], for each orginal k-th row, load 4 + 4 N-dim
// row major values from B matrix, and store it in ROW major s_b[BK][BN].
int load_b_smem_k = tid / 32; // 0~8
// (0b00000000 & 0b00011111) << 2 = 0
// (0b00000001 & 0b00011111) << 2 = 4
// (0b00000010 & 0b00011111) << 2 = 8
// (0b00000011 & 0b00011111) << 2 = 12
int load_b_smem_n = (tid & 31) << 2; // (0,4,8,12,...,124)
int load_a_gmem_m = by * BM + load_a_smem_m;
int load_b_gmem_n = bx * BN + load_b_smem_n;
for (int bk = 0; bk < (K + BK - 1) / BK; bk++) {
int load_a_gmem_k = bk * BK + load_a_smem_k;
int load_a_gmem_addr = load_a_gmem_m * K + load_a_gmem_k;
int load_b_gmem_k = bk * BK + load_b_smem_k;
int load_b_gmem_addr = load_b_gmem_k * N + load_b_gmem_n;
LDST64BITS(r_load_a[0]) = LDST64BITS(a[load_a_gmem_addr]);
LDST64BITS(r_load_b[0]) = LDST64BITS(b[load_b_gmem_addr]);
// s_a[8][128] write: 4路 bank conflicts
s_a[load_a_smem_k ][load_a_smem_m] = r_load_a[0];
s_a[load_a_smem_k + 1][load_a_smem_m] = r_load_a[1];
s_a[load_a_smem_k + 2][load_a_smem_m] = r_load_a[2];
s_a[load_a_smem_k + 3][load_a_smem_m] = r_load_a[3];
// s_b[8][128] write: 2路 bank conflicts
LDST64BITS(s_b[load_b_smem_k][load_b_smem_n]) = LDST64BITS(r_load_b[0]);
__syncthreads();
#pragma unroll
for (int tk = 0; tk < BK; tk++) {
// bank conflicts analysis, tx/ty 0~15, 0~7 bank 4*8=32 bytes
// 进入具体线程后,可以认为该线程对应的值都已经固定了,比如tid, tx, ty.
// 因此对于这个循环的理解,应该按照tk迭代,tid, tx, ty固定为某个值来理解.
// 但是分析bank conflicts需要考虑warp内线程的并发行为,因此,应该分析
// 不同线程在同一个时间点的bank访存情况.
// s_a[8][128] load: 16路 bank conflicts
// tid 0~15, tk 0~7 -> ty 0 -> [0~7][0+0~7] bank 0~3 layers_0~15
// tid 16~31, tk 0~7 -> ty 1 -> [0~7][0+8~15] bank 4~7 layers_0~15
LDST128BITS(r_comp_a[0]) = LDST128BITS(s_a[tk][ty * TM]);
// s_b[8][128] load: 4路 bank conflicts
// tid 0, tk 0~7 -> tx 0 -> [0~7][0+0~7] bank 0~3
// tid 1, tk 0~7 -> tx 1 -> [0~7][0+8~15] bank 4~7
// tid 7, tk 0~7 -> tx 7 -> [0~7][0+56~63] bank 28~31
// tid 0/8/16/24, tk 0~7 -> tx 0/8/16/24 -> [0~7][0+...] bank 0~3
LDST128BITS(r_comp_b[0]) = LDST128BITS(s_b[tk][tx * TN]);
// TODO: 手工实现 swizzle之行列号异或
// https://zhuanlan.zhihu.com/p/722286440
#pragma unroll
for (int tm = 0; tm < TM; tm++) {
#pragma unroll
for (int tn = 0; tn < TN; tn++) {
// r_c[tm][tn] += r_comp_a[tm] * r_comp_b[tn];
r_c[tm][tn] = __hfma(r_comp_a[tm], r_comp_b[tn], r_c[tm][tn]);
}
}
}
__syncthreads();
}
#pragma unroll
for (int i = 0; i < TM; i++) {
int store_c_gmem_m = by * BM + ty * TM + i;
int store_c_gmem_n = bx * BN + tx * TN;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
LDST128BITS(c[store_c_gmem_addr]) = LDST128BITS(r_c[i][0]);
}
}
// TODO: Double Buffering support
template<const int BM=128,
const int BN=128,
const int BK=8,
const int TM=8,
const int TN=8,
const int OFFSET=0>
__global__ void hgemm_t_8x8_sliced_k_f16x8_pack_bcf_dbuf_kernel(
half* a, half* b, half* c, const int M, const int N, const int K) {
// threads: 128/8 * 128/8 = 256
const int bx = blockIdx.x;
const int by = blockIdx.y;
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
__shared__ half s_a[2][BK][BM + OFFSET]; // 8*128*2=2KB
__shared__ half s_b[2][BK][BN + OFFSET]; // 8*128*2=2KB
half r_load_a[TM/2]; // 4
half r_load_b[TN/2]; // 4
half r_comp_a[TM]; // 8
half r_comp_b[TN]; // 8
half r_c[TM][TN] = {__float2half(0.0f)}; // 8x8
// mapping tid to s_a[BK][BM], for each orginal m-th row, load 4 + 4 K-dim
// row major values from A matrix, and store it in COL major s_a[BK][BM].
int load_a_smem_m = tid / 2; // tid / 2,(0,1,2,...,128)
// (0b00000000 & 0b00000001) << 2 = 0
// (0b00000001 & 0b00000001) << 2 = 4
// (0b00000010 & 0b00000001) << 2 = 0
// (0b00000011 & 0b00000001) << 2 = 4
int load_a_smem_k = (tid & 1) << 2; // (0,4)
// mapping tid to s_b[BK][BN], for each orginal k-th row, load 4 + 4 N-dim
// row major values from B matrix, and store it in ROW major s_b[BK][BN].
int load_b_smem_k = tid / 32; // 0~8
// (0b00000000 & 0b00011111) << 2 = 0
// (0b00000001 & 0b00011111) << 2 = 4
// (0b00000010 & 0b00011111) << 2 = 8
// (0b00000011 & 0b00011111) << 2 = 12
int load_b_smem_n = (tid & 31) << 2; // (0,4,8,12,...,124)
int load_a_gmem_m = by * BM + load_a_smem_m;
int load_b_gmem_n = bx * BN + load_b_smem_n;
// 1)主循环从bk = 1 开始,第一次数据加载在主循环之前,最后一次计算在主循环之后,这是pipeline 的特点决定的;
// 2)由于计算和下一次访存使用的Shared Memory不同,因此主循环中每次循环只需要一次__syncthreads()即可
// 3)由于GPU不能向CPU那样支持乱序执行,主循环中需要先将下一次循环计算需要的Gloabal Memory中的数据load
// 到寄存器,然后进行本次计算,之后再将load到寄存器中的数据写到Shared Memory,这样在LDG指令向Global
// Memory做load时,不会影响后续FFMA及其它运算指令的 launch 执行,也就达到了Double Buffering的目的。
// bk = 0 is loading here, buffer 0
{
int load_a_gmem_k = load_a_smem_k;
int load_a_gmem_addr = load_a_gmem_m * K + load_a_gmem_k;
int load_b_gmem_k = load_b_smem_k;
int load_b_gmem_addr = load_b_gmem_k * N + load_b_gmem_n;
LDST64BITS(r_load_a[0]) = LDST64BITS(a[load_a_gmem_addr]);
LDST64BITS(r_load_b[0]) = LDST64BITS(b[load_b_gmem_addr]);
s_a[0][load_a_smem_k + 0][load_a_smem_m] = r_load_a[0];
s_a[0][load_a_smem_k + 1][load_a_smem_m] = r_load_a[1];
s_a[0][load_a_smem_k + 2][load_a_smem_m] = r_load_a[2];
s_a[0][load_a_smem_k + 3][load_a_smem_m] = r_load_a[3];
LDST64BITS(s_b[0][load_b_smem_k][load_b_smem_n]) = LDST64BITS(r_load_b[0]);
}
// Without this synchronization, accuracy may occasionally be abnormal.
__syncthreads();
// bk start from 1,需要注意的是,虽然 bk 从 1 开始,但实际上 bk=1时,使用的是
// 第0块BK中的数据(已经加载到共享内存s_a[0]和s_b[0]);bk=2时,实际计算的是第1块
// BK中的数据。其余以此类推,这个循环结束后,剩下最后一块BK大小的数据需要计算。
for (int bk = 1; bk < (K + BK - 1) / BK; bk++) {
int smem_sel = (bk - 1) & 1; // bk 1->0, bk 2->1, bk 3->0, ...
int smem_sel_next = bk & 1; // bk 1->1, bk 2->0, bk 3->1, ...
int load_a_gmem_k = bk * BK + load_a_smem_k;
int load_a_gmem_addr = load_a_gmem_m * K + load_a_gmem_k;
int load_b_gmem_k = bk * BK + load_b_smem_k;
int load_b_gmem_addr = load_b_gmem_k * N + load_b_gmem_n;
LDST64BITS(r_load_a[0]) = LDST64BITS(a[load_a_gmem_addr]);
LDST64BITS(r_load_b[0]) = LDST64BITS(b[load_b_gmem_addr]);
#pragma unroll
for (int tk = 0; tk < BK; tk++) {
LDST128BITS(r_comp_a[0]) = LDST128BITS(s_a[smem_sel][tk][ty * TM]);
LDST128BITS(r_comp_b[0]) = LDST128BITS(s_b[smem_sel][tk][tx * TN]);
#pragma unroll
for (int tm = 0; tm < TM; tm++) {
#pragma unroll
for (int tn = 0; tn < TN; tn++) {
r_c[tm][tn] = __hfma(r_comp_a[tm], r_comp_b[tn], r_c[tm][tn]);
}
}
}
// 对比非double buffers版本,此处不需要__syncthreads(),总共节省了
// ((K + BK - 1) / BK) - 1 次block内的同步操作。比如,bk=1时,HFMA计算
// 使用的是s_a[0]和s_b[0],因此,和s_a[1]和s_b[1]的加载是没有依赖关系的。
// 从global内存到s_a[1]和s_b[1]和HFMA计算可以并行。s_a[1]和s_b[1]用于
// 加载下一块BK需要的数据到共享内存。
s_a[smem_sel_next][load_a_smem_k + 0][load_a_smem_m] = r_load_a[0];
s_a[smem_sel_next][load_a_smem_k + 1][load_a_smem_m] = r_load_a[1];
s_a[smem_sel_next][load_a_smem_k + 2][load_a_smem_m] = r_load_a[2];
s_a[smem_sel_next][load_a_smem_k + 3][load_a_smem_m] = r_load_a[3];
LDST128BITS(s_b[smem_sel_next][load_b_smem_k][load_b_smem_n]) = LDST128BITS(r_load_b[0]);
__syncthreads();
}
// 计算剩下最后一块BK
#pragma unroll
for (int tk = 0; tk < BK; tk++) {
LDST128BITS(r_comp_a[0]) = LDST128BITS(s_a[1][tk][ty * TM]);
LDST128BITS(r_comp_b[0]) = LDST128BITS(s_b[1][tk][tx * TN]);
#pragma unroll
for (int tm = 0; tm < TM; tm++) {
#pragma unroll
for (int tn = 0; tn < TN; tn++) {
r_c[tm][tn] = __hfma(r_comp_a[tm], r_comp_b[tn], r_c[tm][tn]);
}
}
}
#pragma unroll
for (int i = 0; i < TM; i++) {
int store_c_gmem_m = by * BM + ty * TM + i;
int store_c_gmem_n = bx * BN + tx * TN;
int store_c_gmem_addr = store_c_gmem_m * N + store_c_gmem_n;
LDST128BITS(c[store_c_gmem_addr]) = LDST128BITS(r_c[i][0]);
}
}
// --------------------- 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!"); \
}
// HGEMM naive: compute one c[i,j] element per threads, all row major
void hgemm_naive_f16(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 32;
constexpr int BN = 32;
dim3 block(BN, BM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_naive_f16_kernel<<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
void hgemm_sliced_k_f16(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 32;
constexpr int BN = 32;
constexpr int BK = 32;
dim3 block(BN, BM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_sliced_k_f16_kernel<BM, BN, BK><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// t 8x8 fp16x4
void hgemm_t_8x8_sliced_k_f16x4(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x4_kernel<BM, BN, BK, TM, TN><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// t 8x8 fp16x4 pack
void hgemm_t_8x8_sliced_k_f16x4_pack(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x4_pack_kernel<BM, BN, BK, TM, TN><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// reduce bank conflicts
void hgemm_t_8x8_sliced_k_f16x4_bcf(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x4_bcf_kernel<BM, BN, BK, TM, TN><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// reduce bank conflicts, f16x4 pack, t 8x8
void hgemm_t_8x8_sliced_k_f16x4_pack_bcf(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x4_pack_bcf_kernel<BM, BN, BK, TM, TN, 4><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// reduce bank conflicts, t 8x8 fp16x8 pack, pad
void hgemm_t_8x8_sliced_k_f16x8_pack_bcf(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x8_pack_bcf_kernel<BM, BN, BK, TM, TN, 8><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
void hgemm_t_8x8_sliced_k_f16x8_pack_bcf_dbuf(torch::Tensor a, torch::Tensor b, torch::Tensor c) {
CHECK_TORCH_TENSOR_DTYPE(a, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(b, torch::kHalf)
CHECK_TORCH_TENSOR_DTYPE(c, torch::kHalf)
const int M = a.size(0);
const int K = a.size(1);
const int N = b.size(1);
CHECK_TORCH_TENSOR_SHAPE(a, M, K)
CHECK_TORCH_TENSOR_SHAPE(b, K, N)
CHECK_TORCH_TENSOR_SHAPE(c, M, N)
constexpr int BM = 128;
constexpr int BN = 128;
constexpr int BK = 8;
constexpr int TM = 8;
constexpr int TN = 8;
dim3 block(BN/TN, BM/TM);
dim3 grid((N + BN - 1) / BN, (M + BM - 1) / BM);
hgemm_t_8x8_sliced_k_f16x8_pack_bcf_dbuf_kernel<BM, BN, BK, TM, TN, 8><<<grid, block>>>(
reinterpret_cast<half*>(a.data_ptr()),
reinterpret_cast<half*>(b.data_ptr()),
reinterpret_cast<half*>(c.data_ptr()),
M, N, K
);
}
// from hgemm_async.cu
void hgemm_t_8x8_sliced_k16_f16x8_pack_dbuf(torch::Tensor a, torch::Tensor b, torch::Tensor c);
void hgemm_t_8x8_sliced_k16_f16x8_pack_dbuf_async(torch::Tensor a, torch::Tensor b, torch::Tensor c);
void hgemm_t_8x8_sliced_k32_f16x8_pack_dbuf(torch::Tensor a, torch::Tensor b, torch::Tensor c);
void hgemm_t_8x8_sliced_k32_f16x8_pack_dbuf_async(torch::Tensor a, torch::Tensor b, torch::Tensor c);
void hgemm_t_16x8_sliced_k32_f16x8_pack_dbuf(torch::Tensor a, torch::Tensor b, torch::Tensor c);
void hgemm_t_16x8_sliced_k32_f16x8_pack_dbuf_async(torch::Tensor a, torch::Tensor b, torch::Tensor c);