Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions ggml/src/ggml-cuda/mmq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "mmid.cuh"

#include <cstdint>
#include <cstdlib>

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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17973,6 +17973,21 @@ static std::vector<std::unique_ptr<test_case>> 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<int64_t, int64_t>{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;
}
Expand Down
Loading