Skip to content

Commit bc81d47

Browse files
CUDA: route batch>=4 quantized matmul to MMQ on AMD MFMA hardware (ggml-org#23227)
* CUDA: per-quant MMVQ/MMQ batch threshold on AMD MFMA hardware The dispatcher uses a single global threshold (MMVQ_MAX_BATCH_SIZE = 8) to choose between mul_mat_vec_q (per-row GEMV) and mul_mat_q (MFMA-tiled GEMM) for quantized matmul. On AMD CDNA, the optimal crossover differs substantially by quant family because the per-row GEMV cost is dominated by dequantisation, not the dot-product itself: K-quants pay a heavier super-block decode and so MMQ wins sooner; legacy and IQ quants have lean decode and stay ahead until the batch fully populates an MFMA tile. This patch introduces ggml_cuda_should_use_mmvq(type, cc, ne11) -> bool, mirroring the existing ggml_cuda_should_use_mmq, and gates per-quant thresholds on amd_mfma_available(cc): Q3_K, Q4_K, Q5_K : MMVQ <= 3 (MMQ wins from batch=4: +5% .. +76%) Q2_K, Q6_K : MMVQ <= 5 (MMQ wins from batch=6: +8% .. +35%) others : MMVQ <= 8 (legacy & IQ regress under MMQ; unchanged) Non-AMD-MFMA paths (NVIDIA, RDNA, CDNA1 without MFMA) are byte-identical to master. GGML_CUDA_FORCE_MMVQ=1 restores the original global threshold for A/B testing. Measured on MI250X (gfx90a, ROCm 7.2.1) with Llama-3.2-3B-Instruct, llama-bench pp512 across all 20 supported quants, ubatch 1..8, 10 reps. Full table in PR description. Selected pp512 throughput (tok/s, ub=8): Q4_K_S: 559 -> 940 (+68%) Q5_K_S: 503 -> 884 (+76%) Q3_K_S: 629 -> 879 (+40%) Q2_K : 615 -> 809 (+32%) Q6_K : 582 -> 776 (+33%) Selected pp512 throughput (tok/s, ub=4): Q4_K_S: 444 -> 480 (+ 8%) Q4_0 : 682 -> 685 (+ 0%) (no regression - retains MMVQ) IQ4_XS: 706 -> 698 (- 1%) (no regression - retains MMVQ) * CUDA: address review — inline MMVQ batch table, drop env hatch & doc block * tune kernel selection logic for CDNA1 --------- Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
1 parent 0b24686 commit bc81d47

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,6 +2570,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
25702570
use_mul_mat_q = use_mul_mat_q && ggml_cuda_should_use_mmq(src0->type, cc, src1->ne[1], /*n_experts=*/0);
25712571
use_mul_mat_f = use_mul_mat_f && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[1], /*mul_mat_id=*/false);
25722572
use_mul_mat_vec_f = use_mul_mat_vec_f && ggml_cuda_should_use_mmvf(src0->type, cc, src0->ne, src0->nb, src1->ne[1]);
2573+
use_mul_mat_vec_q = use_mul_mat_vec_q && ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]);
25732574
any_gpus_with_slow_fp16 = any_gpus_with_slow_fp16 || !fast_fp16_hardware_available(cc);
25742575
}
25752576
} else {
@@ -2578,6 +2579,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
25782579
use_mul_mat_q = use_mul_mat_q && ggml_cuda_should_use_mmq(src0->type, cc, src1->ne[1], /*n_experts=*/0);
25792580
use_mul_mat_f = use_mul_mat_f && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[1], /*mul_mat_id=*/false);
25802581
use_mul_mat_vec_f = use_mul_mat_vec_f && ggml_cuda_should_use_mmvf(src0->type, cc, src0->ne, src0->nb, src1->ne[1]);
2582+
use_mul_mat_vec_q = use_mul_mat_vec_q && ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]);
25812583
any_gpus_with_slow_fp16 = any_gpus_with_slow_fp16 || !fast_fp16_hardware_available(cc);
25822584
}
25832585

ggml/src/ggml-cuda/mmvq.cu

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,53 @@ int get_mmvq_mmid_max_batch(ggml_type type, int cc) {
271271
return MMVQ_MAX_BATCH_SIZE;
272272
}
273273

274+
bool ggml_cuda_should_use_mmvq(enum ggml_type type, int cc, int64_t ne11) {
275+
if (GGML_CUDA_CC_IS_CDNA(cc)) {
276+
if (GGML_CUDA_CC_IS_CDNA1(cc)) {
277+
switch (type) {
278+
case GGML_TYPE_Q4_0:
279+
case GGML_TYPE_Q4_1:
280+
return ne11 <= 7;
281+
case GGML_TYPE_Q5_1:
282+
return ne11 <= 7;
283+
case GGML_TYPE_Q8_0:
284+
return ne11 <= 6;
285+
case GGML_TYPE_Q2_K:
286+
return ne11 <= 4;
287+
case GGML_TYPE_Q3_K:
288+
return ne11 <= 3;
289+
case GGML_TYPE_Q4_K:
290+
return ne11 <= 2;
291+
case GGML_TYPE_Q5_K:
292+
return ne11 <= 3;
293+
case GGML_TYPE_Q6_K:
294+
return ne11 <= 4;
295+
case GGML_TYPE_IQ1_S:
296+
return ne11 <= 5;
297+
case GGML_TYPE_IQ2_XXS:
298+
case GGML_TYPE_IQ3_S:
299+
case GGML_TYPE_IQ4_XS:
300+
return ne11 <= 6;
301+
default:
302+
return ne11 <= MMVQ_MAX_BATCH_SIZE;
303+
}
304+
}
305+
switch (type) { // tuned for CDNA2
306+
case GGML_TYPE_Q2_K:
307+
return ne11 <= 5;
308+
case GGML_TYPE_Q3_K:
309+
case GGML_TYPE_Q4_K:
310+
case GGML_TYPE_Q5_K:
311+
return ne11 <= 3;
312+
case GGML_TYPE_Q6_K:
313+
return ne11 <= 5;
314+
default:
315+
return ne11 <= MMVQ_MAX_BATCH_SIZE;
316+
}
317+
}
318+
return ne11 <= MMVQ_MAX_BATCH_SIZE;
319+
}
320+
274321
// Device constexpr: returns the max batch size for the current arch+type at compile time.
275322
template <ggml_type type>
276323
static constexpr __device__ int get_mmvq_mmid_max_batch_for_device() {

ggml/src/ggml-cuda/mmvq.cuh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#define MMVQ_MAX_BATCH_SIZE 8 // Max. batch size for which to use MMVQ kernels.
44

5+
bool ggml_cuda_should_use_mmvq(enum ggml_type type, int cc, int64_t ne11);
6+
57
// Returns the maximum batch size for which MMVQ should be used for MUL_MAT_ID,
68
// based on the quantization type and GPU architecture (compute capability).
79
int get_mmvq_mmid_max_batch(ggml_type type, int cc);

0 commit comments

Comments
 (0)