From 764075e0d4a6da50947126e1ac5dc5ee71ba3de2 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:28:19 +0000 Subject: [PATCH] cuda: env-overridable RDNA3_0 MMQ column limits for Q6_K/IQ2_XS/IQ2_S/Q2_K GGML_CUDA_MMQ_MAX_COLS_{Q6_K,IQ2_XS,IQ2_S,Q2_K} set the largest src1 batch that still uses MMQ instead of dequant + hipBLAS on RDNA3_0. Default 128 (unchanged); 0 never uses MMQ above the generic rules, a large value always does. Read once per type. Adds perf cases to test-backend-ops for the Nemotron, Gemma and Qwen4 hc_up shapes. Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/mmq.cu | 46 ++++++++++++++++++++++++++++++++------ tests/test-backend-ops.cpp | 15 +++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/ggml/src/ggml-cuda/mmq.cu b/ggml/src/ggml-cuda/mmq.cu index 5a2520b6fb04..19838954d3a9 100644 --- a/ggml/src/ggml-cuda/mmq.cu +++ b/ggml/src/ggml-cuda/mmq.cu @@ -4,6 +4,7 @@ #include "mmid.cuh" #include +#include static void ggml_cuda_mul_mat_q_switch_type(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream, const ggml_prec prec_src1) { switch (args.type_x) { @@ -403,6 +404,23 @@ void ggml_cuda_mul_mat_q( ggml_cuda_mul_mat_q_switch_type(ctx, args, stream, prec_src1); } +// RDNA3_0 only: largest batch (columns of src1) that still uses MMQ instead of dequant + hipBLAS for the types whose +// MMQ kernel has lower peak TOPS than the BLAS path. 128 is the default; 0 never uses MMQ above the generic rules, a large +// value always uses it. Read once per type. Invalid values warn and keep the default. +static int64_t ggml_cuda_mmq_rdna3_0_max_cols(const char * name) { + const char * env_c = getenv(name); + if (env_c == nullptr || env_c[0] == '\0') { + return 128; + } + char * end = nullptr; + const long long v = strtoll(env_c, &end, 10); + if (end == env_c || *end != '\0' || v < 0) { + GGML_LOG_WARN("%s: invalid value '%s' for %s, using 128\n", __func__, env_c, name); + return 128; + } + return v; +} + bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t n_experts, int64_t ne00, int64_t ne01) { #ifdef GGML_CUDA_FORCE_CUBLAS return false; @@ -520,14 +538,28 @@ bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t // For some quantization types MMQ can have lower peak TOPS than hipBLAS // so it's only faster for sufficiently small batch sizes: + // On RDNA3_0 the column limits can be overridden (default 128 = unchanged): + // GGML_CUDA_MMQ_MAX_COLS_{Q2_K,Q6_K,IQ2_XS,IQ2_S} + // The ne01 <= 64 and Q4_K exceptions above still apply first. switch (type) { - case GGML_TYPE_Q2_K: - return ne11 <= 128; - case GGML_TYPE_Q6_K: - return ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? 128 : 256); - case GGML_TYPE_IQ2_XS: - case GGML_TYPE_IQ2_S: - return GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || ne11 <= 128; + case GGML_TYPE_Q2_K: { + static const int64_t max_cols = ggml_cuda_mmq_rdna3_0_max_cols("GGML_CUDA_MMQ_MAX_COLS_Q2_K"); + return ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? max_cols : 128); + } + case GGML_TYPE_Q6_K: { + static const int64_t max_cols = ggml_cuda_mmq_rdna3_0_max_cols("GGML_CUDA_MMQ_MAX_COLS_Q6_K"); + return ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? max_cols : 256); + } + case GGML_TYPE_IQ2_XS: { + static const int64_t max_cols = ggml_cuda_mmq_rdna3_0_max_cols("GGML_CUDA_MMQ_MAX_COLS_IQ2_XS"); + return GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || + ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? max_cols : 128); + } + case GGML_TYPE_IQ2_S: { + static const int64_t max_cols = ggml_cuda_mmq_rdna3_0_max_cols("GGML_CUDA_MMQ_MAX_COLS_IQ2_S"); + return GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || + ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? max_cols : 128); + } default: return true; } diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..2d669f02edac 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -17973,6 +17973,21 @@ static std::vector> make_test_cases_perf() { test_cases.emplace_back(new test_l2_norm_batch(GGML_TYPE_F32, { n, 16, 16, 1 }, 4, 1e-12f, true)); } + // [w2 q6k-iq2-mmq] prompt matmuls around the RDNA3_0 MMQ vs dequant+hipBLAS crossover + // (GGML_CUDA_MMQ_MAX_COLS_{Q6_K,IQ2_XS,IQ2_S,Q2_K}): Nemotron Q6_K shapes, Gemma IQ2 shapes and the + // Qwen4 hc_up shape (q6_K, m=8192, k=256) + for (ggml_type type_a : { GGML_TYPE_Q6_K, GGML_TYPE_IQ2_S, GGML_TYPE_IQ2_XS, GGML_TYPE_Q2_K }) { + for (int64_t n : { 256, 512, 1024 }) { + for (auto mk : { std::pair{10240, 2560}, {2560, 10240}, {2048, 2560}, + {16384, 5376}, {8192, 5376}, {4096, 5376} }) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, mk.first, n, mk.second, {1, 1}, {1, 1})); + } + } + } + for (int64_t n : { 256, 512, 1024 }) { + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q6_K, GGML_TYPE_F32, 8192, n, 256, {1, 1}, {1, 1})); + } + return test_cases; }