-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatmul.hip
3048 lines (2801 loc) · 118 KB
/
matmul.hip
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
// Copyright 2024 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common.hip"
#include <cstdio>
#include <cxxabi.h>
#include <optional>
#include <random>
#include <typeinfo>
#include <vector>
typedef void (*mmt_func_t)(const void *, const void *, void *, void *, int, int,
int);
typedef int (*tile_layout_func_t)(int, int);
struct TiledMatrixShape {
int rows_outer, cols_outer;
int rows_tile, cols_tile;
tile_layout_func_t tile_layout;
};
struct TiledMmtShape {
MNKShape outer, tile;
tile_layout_func_t A_tile_layout, B_tile_layout, C_tile_layout;
};
__device__ __host__ TiledMatrixShape A_shape(const TiledMmtShape &s) {
return {s.outer.M, s.outer.K, s.tile.M, s.tile.K, s.A_tile_layout};
}
__device__ __host__ TiledMatrixShape B_shape(const TiledMmtShape &s) {
return {s.outer.N, s.outer.K, s.tile.N, s.tile.K, s.B_tile_layout};
}
__device__ __host__ TiledMatrixShape C_shape(const TiledMmtShape &s) {
return {s.outer.M, s.outer.N, s.tile.M, s.tile.N, s.C_tile_layout};
}
__device__ __host__ int flatsize(const TiledMatrixShape &s) {
return s.rows_outer * s.cols_outer * s.rows_tile * s.cols_tile;
}
__device__ __host__ int offset(const TiledMatrixShape &s, int r_outer,
int c_outer, int r_tile, int c_tile) {
return s.tile_layout(r_tile, c_tile) +
s.rows_tile * s.cols_tile * (c_outer + s.cols_outer * r_outer);
}
// Base class for matrix-times-matrix-transposed ("mmt") kernels.
// As the RHS is transposed, the dimensions are:
// LHS = "A-matrix" : MxK
// RHS = "B-matrix" : NxK
// Accumulator = "C-matrix": MxN
//
// The data layout is tiled with tile sizes given by the {M,N,K}_tile methods
// and tile layouts given by the {A,B,C}_layout methods.
class MmtKernel {
public:
virtual ~MmtKernel() {};
// Returns the element type of the A-matrix (LHS)
virtual Type A_type() const = 0;
// Returns the element type of the B-matrix (RHS)
virtual Type B_type() const = 0;
// Returns the element type of the C-matrix (accumulator/result)
virtual Type C_type() const = 0;
// Returns the M-dimension tile size (rows of accumulator)
virtual int M_tile() const = 0;
// Returns the N-dimension tile size (columns of accumulator)
virtual int N_tile() const = 0;
// Returns the K-dimension tile size (reduction dimension)
virtual int K_tile() const = 0;
// Returns the offset-computation function describing the A-matrix layout.
virtual tile_layout_func_t A_tile_layout() const = 0;
// Returns the offset-computation function describing the B-matrix layout.
virtual tile_layout_func_t B_tile_layout() const = 0;
// Returns the offset-computation function describing the C-matrix layout.
virtual tile_layout_func_t C_tile_layout() const = 0;
// Returns the number of threads that the kernel requires running on.
virtual int num_threads() const = 0;
// Returns a pointer to the device kernel.
virtual mmt_func_t mmt_func() const = 0;
// Optional: kernels may override this method to override the default grid.
virtual std::optional<dim3>
get_work_centric_grid(const MNKShape & /*outer*/) const {
return {};
}
// Optional: kernels may override this to get an auxiliary device buffer of
// the given size in bytes.
virtual int aux_buffer_size(const MNKShape & /*outer*/) const { return 0; }
};
MNKShape getBenchmarkMNKShape(const MmtKernel &kernel) {
int M = getIntEnvVar("M", 4096);
int N = getIntEnvVar("N", 4096);
int K = getIntEnvVar("K", 4096);
MNKShape o;
o.M = std::max(1, M / kernel.M_tile());
o.N = std::max(1, N / kernel.N_tile());
o.K = std::max(1, K / kernel.K_tile());
return o;
}
TiledMmtShape getTestShape(const MmtKernel &kernel, const MNKShape &o) {
TiledMmtShape s;
s.outer = o;
s.tile.M = kernel.M_tile();
s.tile.N = kernel.N_tile();
s.tile.K = kernel.K_tile();
s.A_tile_layout = kernel.A_tile_layout();
s.B_tile_layout = kernel.B_tile_layout();
s.C_tile_layout = kernel.C_tile_layout();
return s;
}
dim3 getLaunchGrid(const MmtKernel &kernel, const TiledMmtShape &s) {
return kernel.get_work_centric_grid(s.outer).value_or(
dim3(s.outer.M, s.outer.N));
}
template <Type A_type, Type B_type, Type C_type>
void checkMmtResults(const void *A_data_void, const void *B_data_void,
const void *C_data_void, const TiledMmtShape &s) {
using TA = CType<A_type>;
using TB = CType<B_type>;
using TC = CType<C_type>;
const TA *A_data = static_cast<const TA *>(A_data_void);
const TB *B_data = static_cast<const TB *>(B_data_void);
const TC *C_data = static_cast<const TC *>(C_data_void);
// This reference code is slow. To make the checks not too slow on
// large matmuls, we only check the 4 corner tiles.
for (int m_outer : {0, s.outer.M - 1}) {
for (int n_outer : {0, s.outer.N - 1}) {
for (int m_tile = 0; m_tile < s.tile.M; ++m_tile) {
for (int n_tile = 0; n_tile < s.tile.N; ++n_tile) {
float c = 0.f;
for (int k_outer = 0; k_outer < s.outer.K; ++k_outer) {
for (int k_tile = 0; k_tile < s.tile.K; ++k_tile) {
TA a =
A_data[offset(A_shape(s), m_outer, k_outer, m_tile, k_tile)];
TB b =
B_data[offset(B_shape(s), n_outer, k_outer, n_tile, k_tile)];
c += static_cast<TC>(a) * static_cast<TC>(b);
}
}
TC expected = c;
TC actual =
C_data[offset(C_shape(s), m_outer, n_outer, m_tile, n_tile)];
if (actual != expected) {
fprintf(stderr,
"matmul numerical error: actual(%g) != "
"expected(%g), at m_outer=%d n_outer=%d m_tile=%d "
"n_tile=%d, at %s:%d. Note: outer MxNxK = %dx%dx%d\n",
static_cast<float>(actual), static_cast<float>(expected),
m_outer, n_outer, m_tile, n_tile, __FILE__, __LINE__,
s.outer.M, s.outer.N, s.outer.K);
abort();
}
}
}
}
}
}
void checkMmtResults(Type A_type, Type B_type, Type C_type,
const void *A_data_void, const void *B_data_void,
const void *C_data_void, const TiledMmtShape &s) {
#define HANDLE_CASE(A, B, C) \
if (A_type == Type::A && B_type == Type::B && C_type == Type::C) { \
checkMmtResults<Type::A, Type::B, Type::C>(A_data_void, B_data_void, \
C_data_void, s); \
return; \
}
HANDLE_CASE(FP32, FP32, FP32)
HANDLE_CASE(FP16, FP16, FP32)
HANDLE_CASE(SI8, SI8, SI32)
#undef HANDLE_CASE
fprintf(stderr, "%s:%d: unhandled types\n", __FILE__, __LINE__);
abort();
}
void check(const MmtKernel &kernel, const MNKShape &o) {
TiledMmtShape s = getTestShape(kernel, o);
std::minstd_rand random_engine;
std::vector<std::byte> A_host_data =
makeRandomBuffer(kernel.A_type(), flatsize(A_shape(s)), random_engine);
std::vector<std::byte> B_host_data =
makeRandomBuffer(kernel.B_type(), flatsize(B_shape(s)), random_engine);
std::vector<std::byte> C_host_data =
makeRandomBuffer(kernel.C_type(), flatsize(C_shape(s)), random_engine);
void *A_device_buffer{};
void *B_device_buffer{};
void *C_device_buffer{};
void *aux_device_buffer{};
TiledMmtShape *shape_device_buffer{};
HIP_CHECK(hipMalloc(&A_device_buffer, A_host_data.size()));
HIP_CHECK(hipMalloc(&B_device_buffer, B_host_data.size()));
HIP_CHECK(hipMalloc(&C_device_buffer, C_host_data.size()));
HIP_CHECK(hipGetLastError());
int aux_buffer_size = kernel.aux_buffer_size(o);
HIP_CHECK(hipMalloc(&aux_device_buffer, aux_buffer_size));
HIP_CHECK(hipMemset(aux_device_buffer, 0, aux_buffer_size));
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipMalloc(&shape_device_buffer, sizeof s));
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipMemcpy(A_device_buffer, A_host_data.data(), A_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(B_device_buffer, B_host_data.data(), B_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(C_device_buffer, C_host_data.data(), C_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(
hipMemcpy(shape_device_buffer, &s, sizeof s, hipMemcpyHostToDevice));
HIP_CHECK(hipGetLastError());
const dim3 grid_dim = getLaunchGrid(kernel, s);
const dim3 block_dim(kernel.num_threads());
HIP_CHECK(hipGetLastError());
kernel.mmt_func()<<<grid_dim, block_dim, 0, hipStreamDefault>>>(
A_device_buffer, B_device_buffer, C_device_buffer, aux_device_buffer,
s.outer.M, s.outer.N, s.outer.K);
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipMemcpy(C_host_data.data(), C_device_buffer, C_host_data.size(),
hipMemcpyDeviceToHost));
checkMmtResults(kernel.A_type(), kernel.B_type(), kernel.C_type(),
A_host_data.data(), B_host_data.data(), C_host_data.data(),
s);
HIP_CHECK(hipFree(A_device_buffer));
HIP_CHECK(hipFree(B_device_buffer));
HIP_CHECK(hipFree(C_device_buffer));
HIP_CHECK(hipFree(aux_device_buffer));
HIP_CHECK(hipFree(shape_device_buffer));
}
void check(const MmtKernel &kernel) {
std::printf(" Checking correctness... ");
// Test with more generic shapes than just M==N==K==2^x.
for (MNKShape o : {MNKShape{1, 1, 1}, MNKShape{2, 1, 1}, MNKShape{1, 2, 1},
MNKShape{1, 1, 2}, MNKShape{1, 1, 3}, MNKShape{1, 1, 4},
MNKShape{1, 1, 5}, MNKShape{2, 2, 2}, MNKShape{2, 3, 4},
MNKShape{5, 2, 3}, MNKShape{1, 1, 10}, MNKShape{4, 4, 8},
MNKShape{305, 1, 1}, MNKShape{20, 20, 20}}) {
check(kernel, o);
}
std::printf("OK\n");
}
void benchmark(const MmtKernel &kernel, const MNKShape &o) {
TiledMmtShape s = getTestShape(kernel, o);
std::printf(" Benchmarking: total MxNxK=%dx%dx%d, outer MxNxK=%dx%dx%d ... ",
s.outer.M * s.tile.M, s.outer.N * s.tile.N, s.outer.K * s.tile.K,
s.outer.M, s.outer.N, s.outer.K);
std::minstd_rand random_engine;
std::vector<std::byte> A_host_data =
makeRandomBuffer(kernel.A_type(), flatsize(A_shape(s)), random_engine);
std::vector<std::byte> B_host_data =
makeRandomBuffer(kernel.B_type(), flatsize(B_shape(s)), random_engine);
std::vector<std::byte> C_host_data =
makeRandomBuffer(kernel.C_type(), flatsize(C_shape(s)), random_engine);
void *A_device_buffer{};
void *B_device_buffer{};
void *C_device_buffer{};
void *aux_device_buffer{};
TiledMmtShape *shape_device_buffer{};
HIP_CHECK(hipMalloc(&A_device_buffer, A_host_data.size()));
HIP_CHECK(hipMalloc(&B_device_buffer, B_host_data.size()));
HIP_CHECK(hipMalloc(&C_device_buffer, C_host_data.size()));
int aux_buffer_size = kernel.aux_buffer_size(o);
HIP_CHECK(hipMalloc(&aux_device_buffer, aux_buffer_size));
HIP_CHECK(hipMemset(aux_device_buffer, 0, aux_buffer_size));
HIP_CHECK(hipMalloc(&shape_device_buffer, sizeof s));
HIP_CHECK(hipMemcpy(A_device_buffer, A_host_data.data(), A_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(B_device_buffer, B_host_data.data(), B_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(C_device_buffer, C_host_data.data(), C_host_data.size(),
hipMemcpyHostToDevice));
HIP_CHECK(
hipMemcpy(shape_device_buffer, &s, sizeof s, hipMemcpyHostToDevice));
const dim3 grid_dim = getLaunchGrid(kernel, s);
const dim3 block_dim(kernel.num_threads());
hipEvent_t start, stop;
HIP_CHECK(hipEventCreate(&start));
HIP_CHECK(hipEventCreate(&stop));
float elapsed_ms{};
float min_elapsed_ms = getIntEnvVar("BENCHMARK_MIN_MS", 100);
int fixed_iterations = getIntEnvVar("FIXED_ITERATIONS", 0);
int iterations = fixed_iterations ? fixed_iterations : 1;
while (true) {
HIP_CHECK(hipEventRecord(start, hipStreamDefault));
for (int b = 0; b < iterations; ++b) {
kernel.mmt_func()<<<grid_dim, block_dim, 0, hipStreamDefault>>>(
A_device_buffer, B_device_buffer, C_device_buffer, aux_device_buffer,
s.outer.M, s.outer.N, s.outer.K);
}
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipEventRecord(stop, hipStreamDefault));
HIP_CHECK(hipEventSynchronize(stop));
HIP_CHECK(hipEventElapsedTime(&elapsed_ms, start, stop));
if (elapsed_ms >= min_elapsed_ms || fixed_iterations) {
break;
}
if (iterations > (1 << 20)) {
fprintf(stderr, "Vacuous kernel? Only taking %g ms at iterations=%d.\n",
elapsed_ms, iterations);
abort();
}
iterations *= 2;
}
// Calculate the actual amount of memory read during the calculation, taking
// into account the tile sizes.
float A_element_bytes = type_size(kernel.A_type());
float B_element_bytes = type_size(kernel.B_type());
float MNK = static_cast<float>(s.outer.M) * s.outer.N * s.outer.K;
float kernel_bytes_read = MNK * ((A_element_bytes * s.tile.M * s.tile.K) +
(B_element_bytes * s.tile.N * s.tile.K));
float kernel_ms = elapsed_ms / iterations;
float kernel_ops =
2.f * s.outer.M * s.outer.N * s.outer.K * s.tile.M * s.tile.N * s.tile.K;
float kernel_ops_per_s = 1000.f * kernel_ops / kernel_ms;
float kernel_bytes_read_per_s = 1000.f * kernel_bytes_read / kernel_ms;
std::printf("%.4g Tflop/s, read %.4g TB/s, latency %.2g ms, iterations=%d\n",
1.e-12f * kernel_ops_per_s, 1e-12f * kernel_bytes_read_per_s,
kernel_ms, iterations);
HIP_CHECK(hipEventDestroy(start));
HIP_CHECK(hipEventDestroy(stop));
HIP_CHECK(hipFree(A_device_buffer));
HIP_CHECK(hipFree(B_device_buffer));
HIP_CHECK(hipFree(C_device_buffer));
HIP_CHECK(hipFree(aux_device_buffer));
HIP_CHECK(hipFree(shape_device_buffer));
}
void test(const MmtKernel &kernel) {
char *name =
abi::__cxa_demangle(typeid(kernel).name(), nullptr, nullptr, nullptr);
const char *filter = getenv("FILTER");
if (filter && !strstr(name, filter)) {
return;
}
std::printf("%s: A:%s, B:%s, C:%s, tile MxNxK=%dx%dx%d, num_threads=%d\n",
name, str(kernel.A_type()), str(kernel.B_type()),
str(kernel.C_type()), kernel.M_tile(), kernel.N_tile(),
kernel.K_tile(), kernel.num_threads());
free(name);
if (!getenv("SKIP_CHECK")) {
check(kernel);
}
MNKShape o = getBenchmarkMNKShape(kernel);
benchmark(kernel, o);
}
template <Type T_A_type, Type T_B_type, Type T_C_type, int T_M_tile,
int T_N_tile, int T_K_tile>
class MmtKernel_generic : public MmtKernel {
virtual Type A_type() const override { return T_A_type; }
virtual Type B_type() const override { return T_B_type; }
virtual Type C_type() const override { return T_C_type; }
virtual int M_tile() const override { return T_M_tile; }
virtual int N_tile() const override { return T_N_tile; }
virtual int K_tile() const override { return T_K_tile; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return k + T_K_tile * m; };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return k + T_K_tile * n; };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return n + T_N_tile * m; };
}
virtual int num_threads() const override { return T_M_tile * T_N_tile; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ static void run(const void *A_data, const void *B_data,
void *C_data, void * /*aux_data*/, int /*M_outer*/,
int N_outer, int K_outer) {
using TA = CType<T_A_type>;
using TB = CType<T_B_type>;
using TC = CType<T_C_type>;
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int m_tile = threadIdx.x / T_N_tile;
int n_tile = threadIdx.x % T_N_tile;
TC c = {0};
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
for (int k_tile = 0; k_tile < T_K_tile; ++k_tile) {
TA a = static_cast<const TA *>(
A_data)[k_tile +
T_K_tile *
(m_tile + T_M_tile * (k_outer + K_outer * m_outer))];
TB b = static_cast<const TB *>(
B_data)[k_tile +
T_K_tile *
(n_tile + T_N_tile * (k_outer + K_outer * n_outer))];
c += static_cast<TC>(a) * static_cast<TC>(b);
}
}
static_cast<TC *>(
C_data)[n_tile + T_N_tile * (m_tile + T_M_tile * (n_outer +
N_outer * m_outer))] =
c;
}
};
class MmtKernel_64t_amdgcn_mfma_f32_16x16x4f32_rowmajor : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 16; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return 4 * m + k; };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return 16 * k + n; };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return 16 * m + n; };
}
virtual int num_threads() const override { return 64; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(64) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
int ai = tid % 16;
int ak = tid / 16;
int bj = tid % 16;
int bk = tid / 16;
const float *A_ptr = static_cast<const float *>(A_data) +
m_outer * K_outer * 64 + ai * 4 + ak;
const float *B_ptr = static_cast<const float *>(B_data) +
n_outer * K_outer * 64 + bk * 16 + bj;
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(*A_ptr, *B_ptr, acc, 0, 0, 0);
A_ptr += 64;
B_ptr += 64;
}
for (int gpr = 0; gpr < 4; ++gpr) {
int ci = 4 * (tid / 16) + gpr;
int cj = tid % 16;
static_cast<float *>(
C_data)[m_outer * N_outer * 256 + n_outer * 256 + ci * 16 + cj] =
acc[gpr];
}
}
};
class MmtKernel_64t_amdgcn_mfma_f32_16x16x4f32_directAB_rowmajorC
: public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 16; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return m + 16 * k; };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return n + 16 * k; };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return 16 * m + n; };
}
virtual int num_threads() const override { return 64; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(64) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const float *A_ptr =
static_cast<const float *>(A_data) + m_outer * K_outer * 64 + tid;
const float *B_ptr =
static_cast<const float *>(B_data) + n_outer * K_outer * 64 + tid;
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(*A_ptr, *B_ptr, acc, 0, 0, 0);
A_ptr += 64;
B_ptr += 64;
}
for (int gpr = 0; gpr < 4; ++gpr) {
int ci = 4 * (tid / 16) + gpr;
int cj = tid % 16;
static_cast<float *>(
C_data)[m_outer * N_outer * 256 + n_outer * 256 + ci * 16 + cj] =
acc[gpr];
}
}
};
class MmtKernel_64t_amdgcn_mfma_f32_16x16x4f32_direct : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 16; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return m + 16 * k; };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return n + 16 * k; };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return 64 * (m / 4) + 4 * n + (m % 4); };
}
virtual int num_threads() const override { return 64; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(64) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const float *A_ptr =
static_cast<const float *>(A_data) + m_outer * K_outer * 64 + tid;
const float *B_ptr =
static_cast<const float *>(B_data) + n_outer * K_outer * 64 + tid;
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(*A_ptr, *B_ptr, acc, 0, 0, 0);
A_ptr += 64;
B_ptr += 64;
}
static_cast<floatx4_t *>(C_data)[64 * (N_outer * m_outer + n_outer) + tid] =
acc;
}
};
class MmtKernel_64t_amdgcn_mfma_f32_16x16x4f32_direct_Kx4 : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 16; }
virtual int K_tile() const override { return 16; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return (k / 4) + 4 * (m + 16 * (k % 4)); };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return (k / 4) + 4 * (n + 16 * (k % 4)); };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return 64 * (m / 4) + 4 * n + (m % 4); };
}
virtual int num_threads() const override { return 64; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(64) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const float *A_ptr =
static_cast<const float *>(A_data) + m_outer * K_outer * 256 + 4 * tid;
const float *B_ptr =
static_cast<const float *>(B_data) + n_outer * K_outer * 256 + 4 * tid;
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(A_ptr[0], B_ptr[0], acc, 0, 0,
0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(A_ptr[1], B_ptr[1], acc, 0, 0,
0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(A_ptr[2], B_ptr[2], acc, 0, 0,
0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(A_ptr[3], B_ptr[3], acc, 0, 0,
0);
A_ptr += 256;
B_ptr += 256;
}
static_cast<floatx4_t *>(C_data)[64 * (N_outer * m_outer + n_outer) + tid] =
acc;
}
};
class MmtKernel_64t_amdgcn_mfma_f32_16x16x4f32_direct_Kx4_unrollx4
: public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 16; }
virtual int K_tile() const override { return 16; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return (k / 4) + 4 * (m + 16 * (k % 4)); };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) { return (k / 4) + 4 * (n + 16 * (k % 4)); };
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) { return 64 * (m / 4) + 4 * n + (m % 4); };
}
virtual int num_threads() const override { return 64; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(64) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const floatx4_t *A_ptr =
static_cast<const floatx4_t *>(A_data) + m_outer * K_outer * 64 + tid;
const floatx4_t *B_ptr =
static_cast<const floatx4_t *>(B_data) + n_outer * K_outer * 64 + tid;
int k_outer = 0;
for (; k_outer <= K_outer - 4; k_outer += 4) {
floatx4_t a0 = A_ptr[0];
floatx4_t b0 = B_ptr[0];
floatx4_t a1 = A_ptr[64];
floatx4_t b1 = B_ptr[64];
floatx4_t a2 = A_ptr[128];
floatx4_t b2 = B_ptr[128];
floatx4_t a3 = A_ptr[192];
floatx4_t b3 = B_ptr[192];
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[0], b0[0], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[1], b0[1], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[2], b0[2], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[3], b0[3], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a1[0], b1[0], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a1[1], b1[1], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a1[2], b1[2], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a1[3], b1[3], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a2[0], b2[0], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a2[1], b2[1], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a2[2], b2[2], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a2[3], b2[3], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a3[0], b3[0], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a3[1], b3[1], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a3[2], b3[2], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a3[3], b3[3], acc, 0, 0, 0);
A_ptr += 256;
B_ptr += 256;
}
for (; k_outer < K_outer; ++k_outer) {
floatx4_t a0 = A_ptr[0];
floatx4_t b0 = B_ptr[0];
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[0], b0[0], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[1], b0[1], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[2], b0[2], acc, 0, 0, 0);
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(a0[3], b0[3], acc, 0, 0, 0);
A_ptr += 64;
B_ptr += 64;
}
static_cast<floatx4_t *>(C_data)[64 * (N_outer * m_outer + n_outer) + tid] =
acc;
}
};
class MmtKernel_128t_1x2_amdgcn_mfma_f32_16x16x4f32_direct : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 16; }
virtual int N_tile() const override { return 32; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) { return 16 * k + m; };
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) {
int ni = n % 16;
int no = n / 16;
return 64 * no + 16 * k + ni;
};
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) {
int ni = n % 16;
int no = n / 16;
return 256 * no + 64 * (m / 4) + 4 * ni + (m % 4);
};
}
virtual int num_threads() const override { return 128; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(128) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const float *A_ptr = static_cast<const float *>(A_data) +
m_outer * K_outer * 64 + (tid % 64);
const float *B_ptr =
static_cast<const float *>(B_data) + n_outer * K_outer * 128 + tid;
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(*A_ptr, *B_ptr, acc, 0, 0, 0);
A_ptr += 64;
B_ptr += 128;
}
static_cast<floatx4_t *>(
C_data)[128 * (N_outer * m_outer + n_outer) + tid] = acc;
}
};
class MmtKernel_256t_2x2_amdgcn_mfma_f32_16x16x4f32_direct : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 32; }
virtual int N_tile() const override { return 32; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) {
int mi = m % 16;
int mo = m / 16;
return 64 * mo + 16 * k + mi;
};
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) {
int ni = n % 16;
int no = n / 16;
return 64 * no + 16 * k + ni;
};
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) {
int mi = m % 16;
int mo = m / 16;
int ni = n % 16;
int no = n / 16;
return 512 * mo + 256 * no + 64 * (mi / 4) + 4 * ni + (mi % 4);
};
}
virtual int num_threads() const override { return 256; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(256) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
const float *A_ptr = static_cast<const float *>(A_data) +
m_outer * K_outer * 128 + (tid % 64) +
64 * (tid / 128);
const float *B_ptr = static_cast<const float *>(B_data) +
n_outer * K_outer * 128 + (tid % 128);
for (int k_outer = 0; k_outer < K_outer; ++k_outer) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(*A_ptr, *B_ptr, acc, 0, 0, 0);
A_ptr += 128;
B_ptr += 128;
}
static_cast<floatx4_t *>(
C_data)[256 * (N_outer * m_outer + n_outer) + tid] = acc;
}
};
class MmtKernel_256t_2x2_amdgcn_mfma_f32_16x16x4f32_shared : public MmtKernel {
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return 32; }
virtual int N_tile() const override { return 32; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) {
int mi = m % 16;
int mo = m / 16;
return 64 * mo + 16 * k + mi;
};
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) {
int ni = n % 16;
int no = n / 16;
return 64 * no + 16 * k + ni;
};
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) {
int mi = m % 16;
int mo = m / 16;
int ni = n % 16;
int no = n / 16;
return 512 * mo + 256 * no + 64 * (mi / 4) + 4 * ni + (mi % 4);
};
}
virtual int num_threads() const override { return 256; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(256) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc = {0};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
int A_thread_offset = (tid % 64) + 64 * (tid / 128);
int B_thread_offset = tid % 128;
constexpr int A_tile_size = 32 * 4;
constexpr int B_tile_size = 32 * 4;
const float *A_global =
static_cast<const float *>(A_data) + m_outer * K_outer * A_tile_size;
const float *B_global =
static_cast<const float *>(B_data) + n_outer * K_outer * B_tile_size;
constexpr int K_outer_shared_size = 4; // Tuned.
__shared__ float A_shared[K_outer_shared_size * A_tile_size];
__shared__ float B_shared[K_outer_shared_size * B_tile_size];
const float *A_global_ptr = A_global + A_thread_offset;
const float *B_global_ptr = B_global + B_thread_offset;
float *A_shared_base_ptr = A_shared + A_thread_offset;
float *B_shared_base_ptr = B_shared + B_thread_offset;
// Main loop: handle full-size shared tiles.
int k_outer_global = 0;
for (; k_outer_global <= K_outer - K_outer_shared_size;
k_outer_global += K_outer_shared_size) {
{
float *A_shared_ptr = A_shared_base_ptr;
float *B_shared_ptr = B_shared_base_ptr;
for (int k_outer_shared = 0; k_outer_shared < K_outer_shared_size;
++k_outer_shared) {
*A_shared_ptr = *A_global_ptr;
*B_shared_ptr = *B_global_ptr;
A_shared_ptr += A_tile_size;
B_shared_ptr += B_tile_size;
A_global_ptr += A_tile_size;
B_global_ptr += B_tile_size;
}
}
__syncthreads();
{
const float *A_shared_ptr = A_shared_base_ptr;
const float *B_shared_ptr = B_shared_base_ptr;
for (int k_outer_shared = 0; k_outer_shared < K_outer_shared_size;
++k_outer_shared) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(
*A_shared_ptr, *B_shared_ptr, acc, 0, 0, 0);
A_shared_ptr += A_tile_size;
B_shared_ptr += B_tile_size;
}
}
__syncthreads();
}
// Handle remainder: the last shared tile has a smaller K-size.
if (k_outer_global < K_outer) {
int K_remaining_outer_size = K_outer - k_outer_global;
{
float *A_shared_ptr = A_shared_base_ptr;
float *B_shared_ptr = B_shared_base_ptr;
for (int k_outer_shared = 0; k_outer_shared < K_remaining_outer_size;
++k_outer_shared) {
*A_shared_ptr = *A_global_ptr;
*B_shared_ptr = *B_global_ptr;
A_shared_ptr += A_tile_size;
B_shared_ptr += B_tile_size;
A_global_ptr += A_tile_size;
B_global_ptr += B_tile_size;
}
}
__syncthreads();
{
const float *A_shared_ptr = A_shared_base_ptr;
const float *B_shared_ptr = B_shared_base_ptr;
for (int k_outer_shared = 0; k_outer_shared < K_remaining_outer_size;
++k_outer_shared) {
acc = __builtin_amdgcn_mfma_f32_16x16x4f32(
*A_shared_ptr, *B_shared_ptr, acc, 0, 0, 0);
A_shared_ptr += A_tile_size;
B_shared_ptr += B_tile_size;
}
}
__syncthreads();
}
static_cast<floatx4_t *>(
C_data)[256 * (N_outer * m_outer + n_outer) + tid] = acc;
}
};
template <int MS, int NS>
class MmtKernel_256t_MSxNS_amdgcn_mfma_f32_16x16x4f32_directA_sharedB
: public MmtKernel {
static_assert(MS >= 4 && !(MS % 4));
static_assert(NS >= 4 && !(NS % 4));
virtual Type A_type() const override { return Type::FP32; }
virtual Type B_type() const override { return Type::FP32; }
virtual Type C_type() const override { return Type::FP32; }
virtual int M_tile() const override { return MS * 16; }
virtual int N_tile() const override { return NS * 16; }
virtual int K_tile() const override { return 4; }
virtual tile_layout_func_t A_tile_layout() const override {
return [](int m, int k) {
int mi = m % 16;
int mo = m / 16;
return 64 * mo + 16 * k + mi;
};
}
virtual tile_layout_func_t B_tile_layout() const override {
return [](int n, int k) {
int ni = n % 16;
int no = n / 16;
return 64 * no + 16 * k + ni;
};
}
virtual tile_layout_func_t C_tile_layout() const override {
return [](int m, int n) {
int mi = m % 16;
int mo = m / 16;
int ni = n % 16;
int no = n / 16;
return NS * 256 * mo + 256 * no + 64 * (mi / 4) + 4 * ni + (mi % 4);
};
}
virtual int num_threads() const override { return 256; }
virtual mmt_func_t mmt_func() const override { return run; };
__global__ __launch_bounds__(256) static void run(
const void *A_data, const void *B_data, void *C_data, void * /*aux_data*/,
int /*M_outer*/, int N_outer, int K_outer) {
using floatx4_t = __attribute__((__vector_size__(4 * sizeof(float)))) float;
floatx4_t acc[MS][NS / 4] = {{0}};
int m_outer = blockIdx.x;
int n_outer = blockIdx.y;
int tid = threadIdx.x;
constexpr int A_tile_size = MS * 16 * 4;
constexpr int B_tile_size = NS * 16 * 4;
const float *A_global =
static_cast<const float *>(A_data) + m_outer * K_outer * A_tile_size;
const float *B_global =
static_cast<const float *>(B_data) + n_outer * K_outer * B_tile_size;
constexpr int K_outer_shared_size = 4; // Tuned.
__shared__ float B_shared[K_outer_shared_size * B_tile_size];
const float *A_global_ptr = A_global + (tid % 64);