From e87b96b60606330577167d8251f877eb6e0d3918 Mon Sep 17 00:00:00 2001 From: Escher2112 Date: Sun, 1 Nov 2026 13:20:06 -0500 Subject: [PATCH 1/2] CUDA: MMVQ parameter table for Maxwell (sm_5x): 1 warp, 4 rows per block for single-token decode Maxwell fell through to MMVQ_PARAMETERS_GENERIC (4 warps, 1 row per block), never tuned for it. On short rows (k=2560: 10 Q4_K super-blocks) that leaves most of each block idle. Measured 2026-11-01, Quadro M4000 8 GB (sm_52), CUDA 12.9, gemma-3-4b Q4_K_M fully offloaded, llama-bench tg128: nwarps x rows: 4x1 (stock) 18.88 | 2x1 20.24 | 1x1 23.77 | 8x1 16.48 | 4x2 23.65 | 2x2 24.97 1x2 28.53 | 2x4 25.94 | 1x4 29.15 | 1x8 26.24 (Vulkan on the same card: 29.8) Hot ops at 1x4 (test-backend-ops perf, the model's own shapes): ffn_gate q4_K 2560->10240 329.6 -> 183.3 us, result_output q6_K 2560->262144 9414 -> 5259 us. Tunables GGML_CUDA_MMVQ_MAXWELL_NWARPS / _RPB. Correctness and other types: being verified. Co-Authored-By: Claude Opus 5.5 --- ggml/src/ggml-cuda/mmvq.cu | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index 1a9cf55cb..c9139101e 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -100,7 +100,8 @@ enum mmvq_parameter_table_id { MMVQ_PARAMETERS_RDNA2, MMVQ_PARAMETERS_RDNA3_0, MMVQ_PARAMETERS_RDNA4, - MMVQ_PARAMETERS_GB10 + MMVQ_PARAMETERS_GB10, + MMVQ_PARAMETERS_MAXWELL // sm_5x (no __dp4a), tuned on a Quadro M4000, 2026-11-01 }; static constexpr __device__ mmvq_parameter_table_id get_device_table_id() { @@ -116,6 +117,8 @@ static constexpr __device__ mmvq_parameter_table_id get_device_table_id() { return MMVQ_PARAMETERS_TURING; #elif __CUDA_ARCH__ == GGML_CUDA_CC_DGX_SPARK return MMVQ_PARAMETERS_GB10; +#elif defined(__CUDA_ARCH__) && __CUDA_ARCH__ < GGML_CUDA_CC_PASCAL + return MMVQ_PARAMETERS_MAXWELL; #else return MMVQ_PARAMETERS_GENERIC; #endif @@ -140,6 +143,9 @@ static __host__ mmvq_parameter_table_id get_device_table_id(int cc) { if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) == GGML_CUDA_CC_DGX_SPARK) { return MMVQ_PARAMETERS_GB10; } + if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) < GGML_CUDA_CC_PASCAL) { + return MMVQ_PARAMETERS_MAXWELL; + } return MMVQ_PARAMETERS_GENERIC; } @@ -449,7 +455,17 @@ static constexpr __device__ int get_mmvq_mmid_max_batch_for_device() { #endif } +#ifndef GGML_CUDA_MMVQ_MAXWELL_NWARPS +#define GGML_CUDA_MMVQ_MAXWELL_NWARPS 1 // warps per block, single token (generic = 4) +#endif +#ifndef GGML_CUDA_MMVQ_MAXWELL_RPB +#define GGML_CUDA_MMVQ_MAXWELL_RPB 4 // rows per block, single token (generic = 1) +#endif + static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id, bool small_k = false, bool halve_iters = false) { + if (table_id == MMVQ_PARAMETERS_MAXWELL) { + return ncols_dst == 1 ? GGML_CUDA_MMVQ_MAXWELL_NWARPS : calc_nwarps(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, halve_iters); + } if (table_id == MMVQ_PARAMETERS_GENERIC) { switch (ncols_dst) { case 1: @@ -593,6 +609,9 @@ static constexpr __host__ __device__ bool mmvq_two_rows_ncols1(ggml_type type) { } static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int ncols_dst, int table_id, bool small_k = false, int nwarps = 1) { + if (table_id == MMVQ_PARAMETERS_MAXWELL) { + return ncols_dst == 1 && !small_k ? GGML_CUDA_MMVQ_MAXWELL_RPB : calc_rows_per_block(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, nwarps); + } if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_GCN || table_id == MMVQ_PARAMETERS_TURING || table_id == MMVQ_PARAMETERS_GB10) { switch (ncols_dst) { case 1: -- 2.56.0 From e6161d44ab98644e078cd4a5791014d691952b89 Mon Sep 17 00:00:00 2001 From: Escher2112 Date: Sun, 1 Nov 2026 14:07:10 -0500 Subject: [PATCH 2/2] CUDA: keep NVFP4 on the generic MMVQ layout on Maxwell (measured 2.6% slower with 1x4) Co-Authored-By: Claude Opus 5.5 --- ggml/src/ggml-cuda/mmvq.cu | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index c9139101e..1532df3b2 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -464,7 +464,8 @@ static constexpr __device__ int get_mmvq_mmid_max_batch_for_device() { static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id, bool small_k = false, bool halve_iters = false) { if (table_id == MMVQ_PARAMETERS_MAXWELL) { - return ncols_dst == 1 ? GGML_CUDA_MMVQ_MAXWELL_NWARPS : calc_nwarps(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, halve_iters); + // NVFP4 measured 2.6% slower with the Maxwell layout (580 -> 595 us), so it keeps the generic one. + return ncols_dst == 1 && type != GGML_TYPE_NVFP4 ? GGML_CUDA_MMVQ_MAXWELL_NWARPS : calc_nwarps(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, halve_iters); } if (table_id == MMVQ_PARAMETERS_GENERIC) { switch (ncols_dst) { @@ -610,7 +611,7 @@ static constexpr __host__ __device__ bool mmvq_two_rows_ncols1(ggml_type type) { static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int ncols_dst, int table_id, bool small_k = false, int nwarps = 1) { if (table_id == MMVQ_PARAMETERS_MAXWELL) { - return ncols_dst == 1 && !small_k ? GGML_CUDA_MMVQ_MAXWELL_RPB : calc_rows_per_block(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, nwarps); + return ncols_dst == 1 && !small_k && type != GGML_TYPE_NVFP4 ? GGML_CUDA_MMVQ_MAXWELL_RPB : calc_rows_per_block(type, ncols_dst, MMVQ_PARAMETERS_GENERIC, small_k, nwarps); } if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_GCN || table_id == MMVQ_PARAMETERS_TURING || table_id == MMVQ_PARAMETERS_GB10) { switch (ncols_dst) { -- 2.56.0