Skip to content

Commit eb4e244

Browse files
ROCm/HIP: add RDNA4_M family for gfx1170/gfx1171 (ggml-org#31373)
gfx117X is more like RDNA4, with 128b WMMA. But they're not identical. Adjust the defines accordingly to have a new RDNA4_M and use it where it makes sense. Assisted-by: Codex Assisted-by: GitHub Copilot Co-authored-by: Mario Limonciello <mario.limonciello@amd.com>
1 parent 68d20b8 commit eb4e244

9 files changed

Lines changed: 72 additions & 53 deletions

File tree

‎ggml/src/ggml-cuda/common.cuh‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@
8282
#define GGML_CUDA_CC_RDNA2 (GGML_CUDA_CC_OFFSET_AMD + 0x1030) // RX 6000, minimum for dp4a
8383
#define GGML_CUDA_CC_RDNA3 (GGML_CUDA_CC_OFFSET_AMD + 0x1100) // RX 7000, minimum for WMMA
8484
#define GGML_CUDA_CC_RDNA3_5 (GGML_CUDA_CC_OFFSET_AMD + 0x1150) // AI 370, AI Max 395 laptops.
85+
#define GGML_CUDA_CC_RDNA4_M (GGML_CUDA_CC_OFFSET_AMD + 0x1170) // gfx117X
8586
#define GGML_CUDA_CC_RDNA4 (GGML_CUDA_CC_OFFSET_AMD + 0x1200) // RX 9000
8687

8788
#define GGML_CUDA_CC_IS_AMD(cc) (cc >= GGML_CUDA_CC_OFFSET_AMD)
8889
#define GGML_CUDA_CC_IS_RDNA(cc) (cc >= GGML_CUDA_CC_RDNA1)
8990
#define GGML_CUDA_CC_IS_RDNA1(cc) (cc >= GGML_CUDA_CC_RDNA1 && cc < GGML_CUDA_CC_RDNA2)
9091
#define GGML_CUDA_CC_IS_RDNA2(cc) (cc >= GGML_CUDA_CC_RDNA2 && cc < GGML_CUDA_CC_RDNA3)
9192
#define GGML_CUDA_CC_IS_RDNA3_0(cc) (cc >= GGML_CUDA_CC_RDNA3 && cc < GGML_CUDA_CC_RDNA3_5)
92-
#define GGML_CUDA_CC_IS_RDNA3_5(cc) (cc >= GGML_CUDA_CC_RDNA3_5 && cc < GGML_CUDA_CC_RDNA4)
93+
#define GGML_CUDA_CC_IS_RDNA3_5(cc) (cc >= GGML_CUDA_CC_RDNA3_5 && cc < GGML_CUDA_CC_RDNA4_M)
9394
#define GGML_CUDA_CC_IS_RDNA3(cc) (GGML_CUDA_CC_IS_RDNA3_0(cc) || GGML_CUDA_CC_IS_RDNA3_5(cc))
95+
#define GGML_CUDA_CC_IS_RDNA4_M(cc) (cc >= GGML_CUDA_CC_RDNA4_M && cc < GGML_CUDA_CC_RDNA4)
9496
#define GGML_CUDA_CC_IS_RDNA4(cc) (cc >= GGML_CUDA_CC_RDNA4)
9597
#define GGML_CUDA_CC_IS_GCN_APU(cc) ((cc) == GGML_CUDA_CC_GFX909 || (cc) == GGML_CUDA_CC_GFX90C)
9698
#define GGML_CUDA_CC_IS_GCN(cc) ((cc > GGML_CUDA_CC_OFFSET_AMD && cc < GGML_CUDA_CC_CDNA1) || GGML_CUDA_CC_IS_GCN_APU(cc))
@@ -277,9 +279,17 @@ static const char * cu_get_error_str(CUresult err) {
277279
#define AMD_MFMA_AVAILABLE
278280
#endif // defined(GGML_USE_HIP) && defined(CDNA) && !defined(GGML_HIP_NO_MMQ_MFMA)
279281

280-
#if defined(GGML_USE_HIP) && (defined(RDNA4) || defined(RDNA3))
282+
#if defined(GGML_USE_HIP) && (defined(RDNA4) || defined(RDNA4_M))
283+
#define AMD_WMMA_128B_AVAILABLE
284+
#endif // defined(GGML_USE_HIP) && (defined(RDNA4) || defined(RDNA4_M))
285+
286+
#if defined(GGML_USE_HIP) && defined(RDNA3)
287+
#define AMD_WMMA_256B_AVAILABLE
288+
#endif // defined(GGML_USE_HIP) && defined(RDNA3)
289+
290+
#if defined(AMD_WMMA_128B_AVAILABLE) || defined(AMD_WMMA_256B_AVAILABLE)
281291
#define AMD_WMMA_AVAILABLE
282-
#endif // defined(GGML_USE_HIP) && defined(RDNA4)
292+
#endif // defined(AMD_WMMA_128B_AVAILABLE) || defined(AMD_WMMA_256B_AVAILABLE)
283293

284294
// The Volta instructions are in principle available on Turing or newer but they are effectively unusable:
285295
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ == GGML_CUDA_CC_VOLTA
@@ -326,7 +336,7 @@ static bool fast_fp16_hardware_available(const int cc) {
326336
// To be used for feature selection of external libraries, e.g. cuBLAS.
327337
static bool fp16_mma_hardware_available(const int cc) {
328338
return (GGML_CUDA_CC_IS_NVIDIA(cc) && cc >= GGML_CUDA_CC_VOLTA) ||
329-
GGML_CUDA_CC_IS_CDNA(cc) || GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4(cc) ||
339+
GGML_CUDA_CC_IS_CDNA(cc) || GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || GGML_CUDA_CC_IS_RDNA4(cc) ||
330340
(GGML_CUDA_CC_IS_MTHREADS(cc) && cc >= GGML_CUDA_CC_QY2);
331341
}
332342

@@ -355,7 +365,7 @@ static bool amd_mfma_available(const int cc) {
355365
}
356366

357367
static bool amd_wmma_available(const int cc) {
358-
return (GGML_CUDA_CC_IS_RDNA4(cc) || GGML_CUDA_CC_IS_RDNA3(cc));
368+
return GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || GGML_CUDA_CC_IS_RDNA4(cc);
359369
}
360370

361371
static bool volta_mma_available(const int cc) {
@@ -713,7 +723,7 @@ static __device__ __forceinline__ int ggml_cuda_dp4a(const int a, const int b, i
713723
#if defined(GGML_USE_HIP)
714724
#if defined(CDNA) || defined(RDNA2) || defined(__gfx906__)
715725
c = __builtin_amdgcn_sdot4(a, b, c, false);
716-
#elif defined(RDNA3) || defined(RDNA4)
726+
#elif defined(RDNA3) || defined(RDNA4_M) || defined(RDNA4)
717727
c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
718728
#elif defined(RDNA1) || defined(__gfx900__)
719729
int tmp1;
@@ -758,9 +768,9 @@ static __device__ __forceinline__ void ggml_cuda_mad(float & acc, const float2 v
758768
acc += v.y*u.y;
759769
}
760770

761-
#if defined(GGML_USE_HIP) && (defined(RDNA2) || defined(RDNA3) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))
771+
#if defined(GGML_USE_HIP) && (defined(RDNA2) || defined(RDNA3) || defined(RDNA4_M) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))
762772
#define V_DOT2_F32_F16_AVAILABLE
763-
#endif // defined(GGML_USE_HIP) && (defined(RDNA2) || defined(RDNA3) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))
773+
#endif // defined(GGML_USE_HIP) && (defined(RDNA2) || defined(RDNA3) || defined(RDNA4_M) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))
764774

765775
static __device__ __forceinline__ void ggml_cuda_mad(float & acc, const half2 v, const half2 u) {
766776
#ifdef V_DOT2_F32_F16_AVAILABLE

‎ggml/src/ggml-cuda/fattn-mma-f16.cuh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ static __device__ __forceinline__ void flash_attn_ext_f16_process_tile(
12551255
T_B_KQ Q_B[(Q_in_reg ? DKQ/(2*T_B_KQ::J) : 1)];
12561256
#if defined(TURING_MMA_AVAILABLE)
12571257
T_C_VKQ VKQ_C[cols_per_warp == 8 ? DV/T_C_VKQ::I : DV/(2*T_C_VKQ::J)];
1258-
#elif defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)
1258+
#elif defined(AMD_WMMA_256B_AVAILABLE)
12591259
T_C_VKQ VKQ_C[DV % 32 != 0 ? DV/T_C_VKQ::J : DV/(2*T_C_VKQ::J)];
12601260
#elif defined(AMD_MFMA_AVAILABLE)
12611261
T_C_VKQ VKQ_C[ DV/T_C_VKQ::J];

‎ggml/src/ggml-cuda/ggml-cuda.cu‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ static void ggml_cuda_mul_mat_cublas_impl(ggml_backend_cuda_context & ctx, const
15651565
if (compute_type == GGML_TYPE_F16) {
15661566
prefer_f32_output = cc == GGML_CUDA_CC_VOLTA || GGML_CUDA_CC_IS_RDNA4(cc) || GGML_CUDA_CC_IS_CDNA(cc);
15671567
} else if (compute_type == GGML_TYPE_BF16) {
1568-
prefer_f32_output = !GGML_CUDA_CC_IS_RDNA3(cc) && !GGML_CUDA_CC_IS_CDNA(cc);
1568+
prefer_f32_output = !GGML_CUDA_CC_IS_RDNA3(cc) && !GGML_CUDA_CC_IS_RDNA4_M(cc) && !GGML_CUDA_CC_IS_CDNA(cc);
15691569
}
15701570

15711571
if (prefer_f32_output) {

‎ggml/src/ggml-cuda/mma.cuh‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ namespace ggml_cuda_mma {
7676
// For the A/C matrices this means I major == row major, J major == column major.
7777
// For the B matrix this means I major == column major, J major == row major.
7878
// MIRRORED == Each data value is held exactly once per thread subgroup.
79-
DATA_LAYOUT_I_MAJOR = 0, // Always used for Turing, Ampere, Ada Lovelace, consumer Blackwell, matrix A&B for RDNA4 and CDNA.
80-
DATA_LAYOUT_J_MAJOR = 10, // Matrix C for CDNA and RDNA4, int and float matrix C for RDNA3.
79+
DATA_LAYOUT_I_MAJOR = 0, // Always used for Turing, Ampere, Ada Lovelace, consumer Blackwell, matrix A&B for WMMA128 and CDNA.
80+
DATA_LAYOUT_J_MAJOR = 10, // Matrix C for CDNA and WMMA128, int and float matrix C for WMMA256.
8181
DATA_LAYOUT_I_MAJOR_MIRRORED = 20, // Volta, matrix A&B for RDNA3.
8282
DATA_LAYOUT_J_MAJOR_MIRRORED = 30,
83-
DATA_LAYOUT_I_MAJOR_SCRAMBLED = 40, // Scrambled matrix C for faster transposition (RDNA4/CDNA), convert to float to unscramble.
83+
DATA_LAYOUT_I_MAJOR_SCRAMBLED = 40, // Scrambled matrix C for faster transposition (WMMA128/CDNA), convert to float to unscramble.
8484
};
8585
// Implemented mma combinations are:
8686
// - (I_MAJOR, I_MAJOR) -> I_MAJOR
8787
// - (I_MAJOR, I_MAJOR_MIRRORED) -> I_MAJOR
8888
// - (I_MAJOR, J_MAJOR_MIRRORED) -> I_MAJOR
8989

9090
static constexpr __device__ data_layout get_input_data_layout() {
91-
#if defined(RDNA3) || defined(VOLTA_MMA_AVAILABLE)
91+
#if defined(AMD_WMMA_256B_AVAILABLE) || defined(VOLTA_MMA_AVAILABLE)
9292
return DATA_LAYOUT_I_MAJOR_MIRRORED;
9393
#else
9494
return DATA_LAYOUT_I_MAJOR;
95-
#endif // defined(RDNA3) || defined(VOLTA_MMA_AVAILABLE)
95+
#endif // defined(AMD_WMMA_256B_AVAILABLE) || defined(VOLTA_MMA_AVAILABLE)
9696
}
9797

9898
template <int I_, int J_, typename T, data_layout ds_=DATA_LAYOUT_I_MAJOR>
@@ -201,7 +201,7 @@ namespace ggml_cuda_mma {
201201

202202
static __device__ __forceinline__ int get_j(const int l) {
203203
if constexpr (I == 16 && J == 16) {
204-
#if defined(RDNA3)
204+
#if defined(AMD_WMMA_256B_AVAILABLE)
205205
if constexpr (std::is_same_v<T, float> || std::is_same_v<T, int>) {
206206
// matrix C
207207
return 2 * l + (threadIdx.x / 16);
@@ -210,11 +210,11 @@ namespace ggml_cuda_mma {
210210
return l;
211211
}
212212
#else
213-
// matrix C is the transposed matrix A&B on RDNA4
213+
// matrix C is the transposed matrix A&B on WMMA128
214214
return ne * (threadIdx.x / 16) + l;
215-
#endif // defined(RDNA3)
215+
#endif // defined(AMD_WMMA_256B_AVAILABLE)
216216
} else if constexpr (I == 16 && J == 8) {
217-
// mmq input for RDNA4
217+
// mmq input for WMMA128
218218
return ne * (threadIdx.x / 16) + l;
219219
} else if constexpr (I == 16 && J == 4) {
220220
return ne * (threadIdx.x / 16) + l;
@@ -573,7 +573,7 @@ namespace ggml_cuda_mma {
573573
static constexpr int I = I_;
574574
static constexpr int J = J_;
575575
static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED;
576-
#if defined(RDNA3)
576+
#if defined(AMD_WMMA_256B_AVAILABLE)
577577
static constexpr int ne = tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::ne;
578578

579579
half2 x[ne] = {{0.0f, 0.0f}};
@@ -616,7 +616,7 @@ namespace ggml_cuda_mma {
616616
return -1;
617617
}
618618
}
619-
#endif // defined(RDNA3)
619+
#endif // defined(AMD_WMMA_256B_AVAILABLE)
620620
};
621621

622622
template <int I_, int J_>
@@ -694,7 +694,7 @@ namespace ggml_cuda_mma {
694694
};
695695

696696
static __device__ __forceinline__ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> unscramble(const tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & t) {
697-
#if defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))
697+
#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE)
698698
tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> ret;
699699
#pragma unroll
700700
for (int l0 = 0; l0 < t.ne/2; ++l0) {
@@ -705,7 +705,7 @@ namespace ggml_cuda_mma {
705705
#else
706706
NO_DEVICE_CODE;
707707
GGML_UNUSED(t);
708-
#endif // defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))
708+
#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE)
709709
}
710710

711711
#if defined(TURING_MMA_AVAILABLE)
@@ -726,7 +726,7 @@ namespace ggml_cuda_mma {
726726

727727
return ret;
728728
}
729-
#elif defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)
729+
#elif defined(AMD_WMMA_256B_AVAILABLE)
730730
static __device__ __forceinline__ tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> get_half2(
731731
const tile<16, 16, float, DATA_LAYOUT_I_MAJOR> & tile_float) {
732732
tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> ret;
@@ -1171,7 +1171,7 @@ namespace ggml_cuda_mma {
11711171
: "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[3]));
11721172
#endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
11731173
#elif defined(AMD_WMMA_AVAILABLE)
1174-
#if defined(RDNA4)
1174+
#if defined(AMD_WMMA_128B_AVAILABLE)
11751175
using halfx8_t = __attribute__((ext_vector_type(8))) _Float16;
11761176
halfx8_t& acc_frag = reinterpret_cast<halfx8_t&>(D.x[0]);
11771177
const halfx8_t& a_frag = reinterpret_cast<const halfx8_t&>(A.x[0]);
@@ -1180,7 +1180,7 @@ namespace ggml_cuda_mma {
11801180
#else
11811181
GGML_UNUSED_VARS(D, A, B);
11821182
NO_DEVICE_CODE;
1183-
#endif // defined(RDNA4)
1183+
#endif // defined(AMD_WMMA_128B_AVAILABLE)
11841184
#elif defined(AMD_MFMA_AVAILABLE)
11851185
// MFMA: FP16 input, FP32 accumulate, convert back to half2.
11861186
using halfx4_t = __attribute__((ext_vector_type(4))) _Float16;
@@ -1218,15 +1218,15 @@ namespace ggml_cuda_mma {
12181218
static __device__ __forceinline__ void mma(
12191219
tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR> & A,
12201220
const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR> & B) {
1221-
#if defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))
1221+
#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE)
12221222
tile<16, 8, half2> * D16 = (tile<16, 8, half2> *) &D;
12231223
const tile<16, 8, half2> * A16 = (const tile<16, 8, half2> *) &A;
12241224
mma(D16[0], A16[0], B);
12251225
mma(D16[1], A16[1], B);
12261226
#else
12271227
GGML_UNUSED_VARS(D, A, B);
12281228
NO_DEVICE_CODE;
1229-
#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) && defined(RDNA4)
1229+
#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE)
12301230
}
12311231

12321232
template <data_layout dl_ab, data_layout dl_d>
@@ -1373,14 +1373,14 @@ namespace ggml_cuda_mma {
13731373
: "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[3]));
13741374
#endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
13751375
#elif defined(AMD_WMMA_AVAILABLE)
1376-
#if defined(RDNA4)
1376+
#if defined(AMD_WMMA_128B_AVAILABLE)
13771377
using halfx8_t = __attribute__((ext_vector_type(8))) _Float16;
13781378
using floatx8_t = __attribute__((ext_vector_type(8))) float;
13791379
floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);
13801380
const halfx8_t& a_frag = reinterpret_cast<const halfx8_t&>(A.x[0]);
13811381
const halfx8_t& b_frag = reinterpret_cast<const halfx8_t&>(B.x[0]);
13821382
acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a_frag, b_frag, acc_frag);
1383-
#elif defined(RDNA3)
1383+
#elif defined(AMD_WMMA_256B_AVAILABLE)
13841384
using halfx16_t = __attribute__((ext_vector_type(16))) _Float16;
13851385
using floatx8_t = __attribute__((ext_vector_type(8))) float;
13861386
floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);
@@ -1390,7 +1390,7 @@ namespace ggml_cuda_mma {
13901390
#else
13911391
GGML_UNUSED_VARS(D, A, B);
13921392
NO_DEVICE_CODE;
1393-
#endif // RDNA4
1393+
#endif // AMD_WMMA_128B_AVAILABLE
13941394
#elif defined(AMD_MFMA_AVAILABLE)
13951395
using halfx4_t = __attribute__((ext_vector_type(4))) _Float16;
13961396
using floatx4_t = __attribute__((ext_vector_type(4))) float;
@@ -1408,14 +1408,14 @@ namespace ggml_cuda_mma {
14081408
static __device__ __forceinline__ void mma(
14091409
tile<16, 16, float, dl_d> & D, const tile<16, 8, nv_bfloat162, dl_ab> & A, const tile<16, 8, nv_bfloat162, dl_ab> & B) {
14101410
#if defined(AMD_WMMA_AVAILABLE)
1411-
#if defined(RDNA4)
1411+
#if defined(AMD_WMMA_128B_AVAILABLE)
14121412
using bf16x8_t = __attribute__((ext_vector_type(8))) short;
14131413
using floatx8_t = __attribute__((ext_vector_type(8))) float;
14141414
floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);
14151415
const bf16x8_t& a_frag = reinterpret_cast<const bf16x8_t&>(A.x[0]);
14161416
const bf16x8_t& b_frag = reinterpret_cast<const bf16x8_t&>(B.x[0]);
14171417
acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a_frag, b_frag, acc_frag);
1418-
#elif defined(RDNA3)
1418+
#elif defined(AMD_WMMA_256B_AVAILABLE)
14191419
using bf16x16_t = __attribute__((ext_vector_type(16))) short;
14201420
using floatx8_t = __attribute__((ext_vector_type(8))) float;
14211421
floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);
@@ -1425,7 +1425,7 @@ namespace ggml_cuda_mma {
14251425
#else
14261426
GGML_UNUSED_VARS(D, A, B);
14271427
NO_DEVICE_CODE;
1428-
#endif // defined(RDNA4)
1428+
#endif // defined(AMD_WMMA_128B_AVAILABLE)
14291429
#elif defined(AMD_MFMA_AVAILABLE)
14301430
using floatx4_t = __attribute__((ext_vector_type(4))) float;
14311431
floatx4_t& acc_frag = reinterpret_cast<floatx4_t&>(D.x[0]);
@@ -1467,19 +1467,19 @@ namespace ggml_cuda_mma {
14671467
#elif defined(AMD_WMMA_AVAILABLE)
14681468
using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int;
14691469
int32x8_t * acc = (int32x8_t *) D.x;
1470-
#if defined(RDNA4)
1470+
#if defined(AMD_WMMA_128B_AVAILABLE)
14711471
using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int;
14721472
int32x2_t * a_vec = (int32x2_t *) A.x;
14731473
int32x2_t * b_vec = (int32x2_t *) B.x;
14741474
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], true);
14751475
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[1], true, b_vec[1], acc[0], true);
1476-
#elif defined(RDNA3)
1476+
#elif defined(AMD_WMMA_256B_AVAILABLE)
14771477
using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;
14781478
int32x4_t * a_vec = (int32x4_t *) A.x;
14791479
int32x4_t * b_vec = (int32x4_t *) B.x;
14801480
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], true);
14811481
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[1], true, b_vec[1], acc[0], true);
1482-
#endif // RDNA4
1482+
#endif // AMD_WMMA_128B_AVAILABLE
14831483
#else
14841484
GGML_UNUSED_VARS(D, A, B);
14851485
NO_DEVICE_CODE;
@@ -1556,7 +1556,7 @@ namespace ggml_cuda_mma {
15561556
static __device__ __forceinline__ void mma(
15571557
tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & A,
15581558
const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & B) {
1559-
#if defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)
1559+
#if defined(AMD_WMMA_256B_AVAILABLE)
15601560
using halfx16_t = __attribute__((ext_vector_type(16))) _Float16;
15611561
halfx16_t * xD = (halfx16_t *) D.x;
15621562
const halfx16_t * xA = (const halfx16_t *) A.x;
@@ -1585,17 +1585,17 @@ namespace ggml_cuda_mma {
15851585
#elif defined(AMD_WMMA_AVAILABLE)
15861586
using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int;
15871587
int32x8_t * acc = (int32x8_t *) D.x;
1588-
#if defined(RDNA4)
1588+
#if defined(AMD_WMMA_128B_AVAILABLE)
15891589
using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int;
15901590
int32x2_t * a_vec = (int32x2_t *) A.x;
15911591
int32x2_t * b_vec = (int32x2_t *) B.x;
15921592
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], false);
1593-
#elif defined(RDNA3)
1593+
#elif defined(AMD_WMMA_256B_AVAILABLE)
15941594
using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;
15951595
int32x4_t * a_vec = (int32x4_t *) A.x;
15961596
int32x4_t * b_vec = (int32x4_t *) B.x;
15971597
acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], false);
1598-
#endif // RDNA4
1598+
#endif // AMD_WMMA_128B_AVAILABLE
15991599
#else
16001600
GGML_UNUSED(D);
16011601
GGML_UNUSED(A);

‎ggml/src/ggml-cuda/mmq.cu‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void ggml_cuda_mul_mat_q(
242242

243243
// Each expert only sees ne12*n_expert_used/ne02 tokens on average.
244244
// On RDNA3 and RDNA4 it is faster to pick the tile size against this value instead of ne12.
245-
if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4(cc)) {
245+
if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || GGML_CUDA_CC_IS_RDNA4(cc)) {
246246
ncols_opt = (ne12*n_expert_used + ne02 - 1) / ne02;
247247
}
248248
}
@@ -470,7 +470,7 @@ bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t
470470
}
471471

472472
if (amd_wmma_available(cc)) {
473-
if (GGML_CUDA_CC_IS_RDNA3(cc)) {
473+
if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc)) {
474474
// High expert counts are almost always better on MMQ due to
475475
// the synchronization overhead in the cuBLAS/hipBLAS path:
476476
// https://github.com/ggml-org/llama.cpp/pull/18202
@@ -487,7 +487,7 @@ bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t
487487
return ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? 128 : 256);
488488
case GGML_TYPE_IQ2_XS:
489489
case GGML_TYPE_IQ2_S:
490-
return GGML_CUDA_CC_IS_RDNA3_5(cc) || ne11 <= 128;
490+
return GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || ne11 <= 128;
491491
default:
492492
return true;
493493
}

‎ggml/src/ggml-cuda/mmq.cuh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ static __host__ ggml_cuda_mmq_config ggml_cuda_mmq_get_config(const ggml_type ty
240240
if (GGML_CUDA_CC_IS_RDNA4(cc)) {
241241
return ggml_cuda_mmq_get_config_rdna4(type, J, fallback);
242242
}
243+
if (GGML_CUDA_CC_IS_RDNA4_M(cc)) {
244+
return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback);
245+
}
243246
if (GGML_CUDA_CC_IS_RDNA3_5(cc)) {
244247
return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback);
245248
}
@@ -272,6 +275,8 @@ static constexpr __device__ ggml_cuda_mmq_config ggml_cuda_mmq_get_config(ggml_t
272275
return ggml_cuda_mmq_get_config_cdna(type, J, fallback);
273276
#elif defined(RDNA4)
274277
return ggml_cuda_mmq_get_config_rdna4(type, J, fallback);
278+
#elif defined(RDNA4_M)
279+
return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback);
275280
#elif defined(RDNA3_5)
276281
return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback);
277282
#elif defined(RDNA3)

0 commit comments

Comments
 (0)