From 511a3ed94165eeb9ee8f1e20386316effaa754ed Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:50:48 +0000 Subject: [PATCH 1/5] cuda : no q8_1 copy of the activations for matmuls that few-row MMQ takes ggml_cuda_q8_1_consumer only asked ggml_cuda_should_use_mmvq, but ggml_cuda_mul_mat takes few-row MMQ first, which quantizes src1 in its own layout: the norm kernels wrote q8_1 copies nobody read (RDNA3: K-quants at 7-8 columns, the I-quants at 2-8). Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index 8f31a02e0f3d..b0e8fef79a11 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -218,9 +218,10 @@ static const ggml_tensor * ggml_cuda_q8_1_consumer(const ggml_backend_cuda_conte const ggml_tensor * src0 = n->src[0]; const ggml_tensor * src1 = n->src[1]; // MUL_MAT_ID shares the copy only with one src1 row per token, see ggml_cuda_mul_mat_vec_q + // few-row MMQ quantizes src1 itself and goes before MMVQ in ggml_cuda_mul_mat const bool mmvq = n->op == GGML_OP_MUL_MAT_ID ? n->ne[2] <= get_mmvq_mmid_max_batch(src0->type, cc) && src1->ne[1] == 1 - : ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]); + : ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]) && !ggml_cuda_should_use_mmq_few(src0, src1, nullptr, cc); const bool bad_padding_clear = ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE && ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) && src0->view_src; From 4561b3fcc7b17f90f60864ac0ac7be6e591ae934 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:42:19 +0000 Subject: [PATCH 2/5] cuda : IQ3_XXS in the few-row WMMA MMQ on RDNA3 (speculative verify batches) MMVQ decodes an IQ3_XXS row once for all src1 columns, but its per-column work (q8_1 loads, dot products, scales) makes a 7-column verify batch cost 2.15-2.37x of one column on the RX 7900 XT. The few-row WMMA MMQ decodes each sub-block once per 16 rows and serves up to 16 columns with two WMMAs. On RDNA3 both half-warps need the same 32 values, so each half-warp decodes 16 of them and takes the other 16 from the other half-warp with v_permlanex16. Experiment: GGML_CUDA_MMQ_FEW_IQ3_XXS_MIN sets the min. number of columns (default 2, 0 disables). Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/mmq-few.cu | 100 ++++++++++++++++++++++++++++++++-- tests/test-backend-ops.cpp | 6 ++ 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/ggml/src/ggml-cuda/mmq-few.cu b/ggml/src/ggml-cuda/mmq-few.cu index cd867a75c149..4db0f84df04f 100644 --- a/ggml/src/ggml-cuda/mmq-few.cu +++ b/ggml/src/ggml-cuda/mmq-few.cu @@ -5,6 +5,7 @@ #include "quantize.cuh" #include "vecdotq.cuh" +#include #include // mul_mat_q_few for NVIDIA uses the int8 MMA fragment layout and cp.async, mul_mat_q_few_wmma for RDNA the WMMA layout. Not on MUSA. @@ -33,6 +34,7 @@ static constexpr __host__ __device__ int mmq_few_unit_size(const ggml_type type) case GGML_TYPE_Q4_K: return mmq_few_unit_size_of(); case GGML_TYPE_Q5_K: return mmq_few_unit_size_of(); case GGML_TYPE_Q6_K: return mmq_few_unit_size_of(); + case GGML_TYPE_IQ3_XXS: return mmq_few_unit_size_of(); case GGML_TYPE_IQ3_S: return mmq_few_unit_size_of(); case GGML_TYPE_IQ4_NL: return mmq_few_unit_size_of(); case GGML_TYPE_IQ4_XS: return mmq_few_unit_size_of(); @@ -860,7 +862,8 @@ static __global__ void mul_mat_q_few( // A second group of 16 columns spills registers with ROCm 7.2. static constexpr __host__ __device__ bool mmq_few_wmma_supported(const ggml_type type) { - return type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K; + return type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K || + type == GGML_TYPE_IQ3_XXS; } // the reduction over the warps @@ -1121,9 +1124,87 @@ static __device__ __forceinline__ void mmq_few_wmma_vec_dot_q8_0_iq4_nl(const ch } } +// IQ3_XXS: d, then 8 grid indices (4 values each) per sub-block of 32 values, then per sub-block a uint32 with 4 x 7 sign bits +// (8 values each, the 8th sign is the parity) and a 4 bit scale. The units are 98 bytes, only 2 byte aligned. +// The gathers from the grid table make the decoding the expensive part: on RDNA3, where both half-warps need the same 32 values of a +// sub-block, each half-warp decodes 16 of them and takes the other 16 from the other half-warp (v_permlanex16). +template +static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_xxs(const char * x, const int4 * y, float (&sum)[8]) { + const block_iq3_xxs * bx = (const block_iq3_xxs *) x; + const float d = __half2float(bx->d); + + float sum_d[8] = {0.0f}; +#if defined(AMD_WMMA_256B_AVAILABLE) // RDNA3, MMQ_FEW_WMMA_NE == 4 + const int h = threadIdx.x / 16; +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int q3 = get_int_b2(bx->qs, 2*s + h); // grid indices of the values [16h, 16h + 16) + const uint32_t aux32 = get_int_b2(bx->qs, QK_K/16 + s); // signs and scale of the sub-block + const uint32_t sgn = aux32 >> (14*h); // sign groups 2h and 2h + 1 + + int own[4]; +#pragma unroll + for (int l = 0; l < 2; ++l) { + const uint32_t signs = unpack_ksigns(sgn >> (7*l)); + own[2*l + 0] = apply_ksigns_lo(iq3xxs_grid[(q3 >> (16*l + 0)) & 0xFF], signs); + own[2*l + 1] = apply_ksigns_hi(iq3xxs_grid[(q3 >> (16*l + 8)) & 0xFF], signs); + } + int b_lo[4]; // values [0, 16) + int b_hi[4]; // values [16, 32) +#pragma unroll + for (int i = 0; i < 4; ++i) { + const int other = __builtin_amdgcn_permlanex16(own[i], own[i], 0x76543210, 0xfedcba98, false, false); + b_lo[i] = h == 0 ? own[i] : other; + b_hi[i] = h == 0 ? other : own[i]; + } + const float ls = float(aux32 >> 28) + 0.5f; + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C; + mmq_few_wmma(C, yf.qs[0], b_lo); + mmq_few_wmma(C, yf.qs[1], b_hi); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * (ls*float(C.x[v])); + } + } +#else // RDNA4: the lane needs the values [8*(l/16), 8*(l/16) + 8) of each half of the sub-block, i.e. the sign groups l/16 and 2 + l/16 + const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16); +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int2 q3 = make_int2(get_int_b2(bx->qs, 2*s), get_int_b2(bx->qs, 2*s + 1)); + const uint32_t aux32 = get_int_b2(bx->qs, QK_K/16 + s); + int b[8]; +#pragma unroll + for (int l = 0; l < 4; ++l) { + const int q = l < 2 ? q3.x : q3.y; + const uint32_t signs = unpack_ksigns(aux32 >> (7*l)); + b[2*l + 0] = apply_ksigns_lo(iq3xxs_grid[(q >> (16*(l % 2) + 0)) & 0xFF], signs); + b[2*l + 1] = apply_ksigns_hi(iq3xxs_grid[(q >> (16*(l % 2) + 8)) & 0xFF], signs); + } + const float ls = float(aux32 >> 28) + 0.5f; + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C; + mmq_few_wmma(C, yf.qs[0], b + i0 + 0); + mmq_few_wmma(C, yf.qs[1], b + i0 + 4); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * (ls*float(C.x[v])); + } + } +#endif // defined(AMD_WMMA_256B_AVAILABLE) +#pragma unroll + for (int v = 0; v < 8; ++v) { + sum[v] += 0.5f*d*sum_d[v]; + } +} + template static __device__ __forceinline__ void mmq_few_wmma_vec_dot(const char * x, const int4 * y, float (&sum)[8]) { - if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) { + if constexpr (type == GGML_TYPE_IQ3_XXS) { + mmq_few_wmma_vec_dot_iq3_xxs(x, y, sum); + } else if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) { mmq_few_wmma_vec_dot_q8_0_iq4_nl(x, y, sum); } else if constexpr (type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K) { mmq_few_wmma_vec_dot_q45_K(x, y, sum); @@ -1332,6 +1413,13 @@ static int2 mmq_few_wmma_ncols_range(const ggml_type type, const int cc) { case GGML_TYPE_Q6_K: return make_int2(5, 16); case GGML_TYPE_IQ4_NL: return make_int2(5, 16); case GGML_TYPE_Q8_0: return make_int2(9, 16); // fork: tuned on the RX 7900 XT, MMQ is faster above 16 columns + case GGML_TYPE_IQ3_XXS: { // fork: MMVQ decodes the grid once per row but its per-column work grows with the columns + static const int min_ncols = [] { + const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ3_XXS_MIN"); // experiment: 0 disables + return e ? atoi(e) : 2; + }(); + return min_ncols > 0 ? make_int2(min_ncols, 16) : make_int2(0, 0); + } default: return make_int2(0, 0); } } @@ -1377,6 +1465,9 @@ static void ggml_cuda_mul_mat_q_few_wmma(ggml_backend_cuda_context & ctx, const case GGML_TYPE_IQ4_NL: launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); break; + case GGML_TYPE_IQ3_XXS: + launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); + break; default: GGML_ABORT("unsupported type for few-row MMQ with WMMA"); } @@ -1509,7 +1600,7 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const } const int min_ncols = mmq_few_wmma_ncols_range(type, cc).x; // fork: on the RX 7900 XT the mat-vec kernels with their fused variants are as fast or faster up to 6 columns - return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 ? std::max(min_ncols, 7) : min_ncols; + return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 && type != GGML_TYPE_IQ3_XXS ? std::max(min_ncols, 7) : min_ncols; } // NVIDIA Ampere to Blackwell @@ -1521,7 +1612,8 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const return 0; } // with fewer than 8 units of 256 values per row the warps idle, MMVQ and MMQ are better there (MUL_MAT_ID splits the rows instead) - if (mmq_few_unit_size(type) == 0 || (!has_ids && ne00 < 8*QK_K)) { + // IQ3_XXS only has a WMMA decoder + if (mmq_few_unit_size(type) == 0 || type == GGML_TYPE_IQ3_XXS || (!has_ids && ne00 < 8*QK_K)) { return 0; } if (has_ids && !mmq_few_id_supported(type)) { diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 35074227fed1..a992343c89e9 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -15453,6 +15453,12 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q6_K, GGML_TYPE_F32, 63, 5, 2304, {3, 2}, {2, 2})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q3_K, GGML_TYPE_F32, 64, 13, 4608, {2, 3}, {1, 1}, {0, 2, 1, 3})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_S, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); + // RDNA few-row WMMA MMQ for IQ3_XXS (2 byte aligned units of 98 bytes): 2 to 16 columns, odd rows, broadcast and permuted + for (int64_t n : {2, 3, 5, 7, 8, 9, 13, 16}) { + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 1000, n, 5376, {1, 1}, {1, 1})); + } + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); + test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 64, 13, 2304, {2, 3}, {1, 1}, {0, 2, 1, 3})); // partial column group, broadcast, padded rows, src1 in [0, 1], src0 in a weights buffer test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 1000, 5, 4608, {1, 1}, {1, 1})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 64, 13, 4608, {3, 2}, {2, 2})); From 0b6747bc47facce4cd2852bd5e14a40d5ef2afa6 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Thu, 3 Dec 2026 23:49:53 +0000 Subject: [PATCH 3/5] cuda : IQ2_S and IQ3_S in the few-row WMMA MMQ on RDNA3 Same scheme as IQ3_XXS: on RDNA3 each half-warp decodes 16 of the 32 values of a sub-block and takes the other 16 from the other half-warp. IQ2_S has a scale per 16 values, so the two WMMAs of a sub-block get their own accumulators. Experiment: GGML_CUDA_MMQ_FEW_IQ_MIN (min. number of columns, default 2, 0 disables) and GGML_CUDA_MMQ_FEW_IQ_TYPES (type ids, default 18,21,22) replace GGML_CUDA_MMQ_FEW_IQ3_XXS_MIN. Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/mmq-few.cu | 204 +++++++++++++++++++++++++++++++--- tests/test-backend-ops.cpp | 12 +- 2 files changed, 197 insertions(+), 19 deletions(-) diff --git a/ggml/src/ggml-cuda/mmq-few.cu b/ggml/src/ggml-cuda/mmq-few.cu index 4db0f84df04f..a7530ee35767 100644 --- a/ggml/src/ggml-cuda/mmq-few.cu +++ b/ggml/src/ggml-cuda/mmq-few.cu @@ -6,6 +6,7 @@ #include "vecdotq.cuh" #include +#include #include // mul_mat_q_few for NVIDIA uses the int8 MMA fragment layout and cp.async, mul_mat_q_few_wmma for RDNA the WMMA layout. Not on MUSA. @@ -34,6 +35,7 @@ static constexpr __host__ __device__ int mmq_few_unit_size(const ggml_type type) case GGML_TYPE_Q4_K: return mmq_few_unit_size_of(); case GGML_TYPE_Q5_K: return mmq_few_unit_size_of(); case GGML_TYPE_Q6_K: return mmq_few_unit_size_of(); + case GGML_TYPE_IQ2_S: return mmq_few_unit_size_of(); case GGML_TYPE_IQ3_XXS: return mmq_few_unit_size_of(); case GGML_TYPE_IQ3_S: return mmq_few_unit_size_of(); case GGML_TYPE_IQ4_NL: return mmq_few_unit_size_of(); @@ -863,7 +865,7 @@ static __global__ void mul_mat_q_few( static constexpr __host__ __device__ bool mmq_few_wmma_supported(const ggml_type type) { return type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K || - type == GGML_TYPE_IQ3_XXS; + type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S; } // the reduction over the warps @@ -1124,6 +1126,19 @@ static __device__ __forceinline__ void mmq_few_wmma_vec_dot_q8_0_iq4_nl(const ch } } +#if defined(AMD_WMMA_256B_AVAILABLE) +// RDNA3: half-warp h decoded the values [16h, 16h + 16) of a sub-block, the other 16 values come from the other half-warp. +static __device__ __forceinline__ void mmq_few_wmma_exchange_halves(const int (&own)[4], int (&b_lo)[4], int (&b_hi)[4]) { + const bool h1 = threadIdx.x >= 16; +#pragma unroll + for (int i = 0; i < 4; ++i) { + const int other = __builtin_amdgcn_permlanex16(own[i], own[i], 0x76543210, 0xfedcba98, false, false); + b_lo[i] = h1 ? other : own[i]; + b_hi[i] = h1 ? own[i] : other; + } +} +#endif // defined(AMD_WMMA_256B_AVAILABLE) + // IQ3_XXS: d, then 8 grid indices (4 values each) per sub-block of 32 values, then per sub-block a uint32 with 4 x 7 sign bits // (8 values each, the 8th sign is the parity) and a 4 bit scale. The units are 98 bytes, only 2 byte aligned. // The gathers from the grid table make the decoding the expensive part: on RDNA3, where both half-warps need the same 32 values of a @@ -1151,12 +1166,7 @@ static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_xxs(const char * } int b_lo[4]; // values [0, 16) int b_hi[4]; // values [16, 32) -#pragma unroll - for (int i = 0; i < 4; ++i) { - const int other = __builtin_amdgcn_permlanex16(own[i], own[i], 0x76543210, 0xfedcba98, false, false); - b_lo[i] = h == 0 ? own[i] : other; - b_hi[i] = h == 0 ? other : own[i]; - } + mmq_few_wmma_exchange_halves(own, b_lo, b_hi); const float ls = float(aux32 >> 28) + 0.5f; const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); @@ -1200,9 +1210,160 @@ static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_xxs(const char * } } +// IQ2_S: d, 32 bytes with the low 8 bits of the grid indices (8 values each), 32 sign bytes (8 values each), the high 2 bits of +// the indices (a byte per sub-block) and two 4 bit scales per sub-block (values [0, 16) and [16, 32)). 82 byte units. +template +static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq2_s(const char * x, const int4 * y, float (&sum)[8]) { + const block_iq2_s * bx = (const block_iq2_s *) x; + const float d = __half2float(bx->d); + + float sum_d[8] = {0.0f}; +#if defined(AMD_WMMA_256B_AVAILABLE) + const int h = threadIdx.x / 16; +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int qs = get_int_b2(bx->qs, s); // low bits of the 4 grid indices of the sub-block + const int signs = get_int_b2(bx->qs, QK_K/32 + s); // their 4 sign bytes + const int qh = bx->qh[s]; + const int sc = bx->scales[s]; + + int own[4]; +#pragma unroll + for (int l = 0; l < 2; ++l) { + const int g = 2*h + l; // the values [8g, 8g + 8) + const int2 grid = ((const int2 *) iq2s_grid)[((qs >> (8*g)) & 0xFF) | ((qh << (8 - 2*g)) & 0x300)]; + const uint8_t sg = signs >> (8*g); + own[2*l + 0] = apply_signs_lo(grid.x, sg); + own[2*l + 1] = apply_signs_hi(grid.y, sg); + } + int b_lo[4]; + int b_hi[4]; + mmq_few_wmma_exchange_halves(own, b_lo, b_hi); + const float ls_lo = float(sc & 0x0F) + 0.5f; + const float ls_hi = float(sc >> 4) + 0.5f; + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C_lo; + mmq_few_tile_c C_hi; + mmq_few_wmma(C_lo, yf.qs[0], b_lo); + mmq_few_wmma(C_hi, yf.qs[1], b_hi); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * (ls_lo*float(C_lo.x[v]) + ls_hi*float(C_hi.x[v])); + } + } +#else + const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16); +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int qs = get_int_b2(bx->qs, s); + const int signs = get_int_b2(bx->qs, QK_K/32 + s); + const int qh = bx->qh[s]; + const int sc = bx->scales[s]; + + int b[8]; +#pragma unroll + for (int g = 0; g < 4; ++g) { + const int2 grid = ((const int2 *) iq2s_grid)[((qs >> (8*g)) & 0xFF) | ((qh << (8 - 2*g)) & 0x300)]; + const uint8_t sg = signs >> (8*g); + b[2*g + 0] = apply_signs_lo(grid.x, sg); + b[2*g + 1] = apply_signs_hi(grid.y, sg); + } + const float ls_lo = float(sc & 0x0F) + 0.5f; + const float ls_hi = float(sc >> 4) + 0.5f; + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C_lo; + mmq_few_tile_c C_hi; + mmq_few_wmma(C_lo, yf.qs[0], b + i0 + 0); + mmq_few_wmma(C_hi, yf.qs[1], b + i0 + 4); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * (ls_lo*float(C_lo.x[v]) + ls_hi*float(C_hi.x[v])); + } + } +#endif // defined(AMD_WMMA_256B_AVAILABLE) +#pragma unroll + for (int v = 0; v < 8; ++v) { + sum[v] += 0.25f*d*sum_d[v]; + } +} + +// IQ3_S: d, 64 bytes with the low 8 bits of the grid indices (4 values each), a byte per sub-block with their high bits, +// 32 sign bytes (8 values each) and a 4 bit scale per sub-block. 110 byte units. +template +static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq3_s(const char * x, const int4 * y, float (&sum)[8]) { + const block_iq3_s * bx = (const block_iq3_s *) x; + const float d = __half2float(bx->d); + + float sum_d[8] = {0.0f}; +#if defined(AMD_WMMA_256B_AVAILABLE) + const int h = threadIdx.x / 16; +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int qs = get_int_b2(bx->qs, 2*s + h); // low bits of the grid indices 4h .. 4h + 3 + const int qh = bx->qh[s] >> (4*h); // their high bits + const int signs = get_int_b2(bx->signs, s) >> (16*h); // their sign nibbles + + int own[4]; +#pragma unroll + for (int k = 0; k < 4; ++k) { + const int grid = iq3s_grid[((qs >> (8*k)) & 0xFF) | (((qh >> k) & 1) << 8)]; + own[k] = apply_signs_lo(grid, (uint8_t) (signs >> (4*k))); + } + int b_lo[4]; + int b_hi[4]; + mmq_few_wmma_exchange_halves(own, b_lo, b_hi); + const int ls = 1 + 2*((bx->scales[s/2] >> (4*(s % 2))) & 0x0F); + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C; + mmq_few_wmma(C, yf.qs[0], b_lo); + mmq_few_wmma(C, yf.qs[1], b_hi); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * float(C.x[v]*ls); + } + } +#else + const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16); +#pragma unroll + for (int s = 0; s < 8; ++s) { + const int2 qs = make_int2(get_int_b2(bx->qs, 2*s), get_int_b2(bx->qs, 2*s + 1)); + const int qh = bx->qh[s]; + const int signs = get_int_b2(bx->signs, s); + + int b[8]; +#pragma unroll + for (int k = 0; k < 8; ++k) { + const int grid = iq3s_grid[(((k < 4 ? qs.x : qs.y) >> (8*(k % 4))) & 0xFF) | (((qh >> k) & 1) << 8)]; + b[k] = apply_signs_lo(grid, (uint8_t) (signs >> (4*k))); + } + const int ls = 1 + 2*((bx->scales[s/2] >> (4*(s % 2))) & 0x0F); + + const mmq_few_wmma_y yf = mmq_few_wmma_load_y(y, s); + mmq_few_tile_c C; + mmq_few_wmma(C, yf.qs[0], b + i0 + 0); + mmq_few_wmma(C, yf.qs[1], b + i0 + 4); +#pragma unroll + for (int v = 0; v < mmq_few_wmma_nv(); ++v) { + sum_d[v] += mmq_few_wmma_get_d(yf, v) * float(C.x[v]*ls); + } + } +#endif // defined(AMD_WMMA_256B_AVAILABLE) +#pragma unroll + for (int v = 0; v < 8; ++v) { + sum[v] += d*sum_d[v]; + } +} + template static __device__ __forceinline__ void mmq_few_wmma_vec_dot(const char * x, const int4 * y, float (&sum)[8]) { - if constexpr (type == GGML_TYPE_IQ3_XXS) { + if constexpr (type == GGML_TYPE_IQ2_S) { + mmq_few_wmma_vec_dot_iq2_s(x, y, sum); + } else if constexpr (type == GGML_TYPE_IQ3_S) { + mmq_few_wmma_vec_dot_iq3_s(x, y, sum); + } else if constexpr (type == GGML_TYPE_IQ3_XXS) { mmq_few_wmma_vec_dot_iq3_xxs(x, y, sum); } else if constexpr (type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_NL) { mmq_few_wmma_vec_dot_q8_0_iq4_nl(x, y, sum); @@ -1413,12 +1574,20 @@ static int2 mmq_few_wmma_ncols_range(const ggml_type type, const int cc) { case GGML_TYPE_Q6_K: return make_int2(5, 16); case GGML_TYPE_IQ4_NL: return make_int2(5, 16); case GGML_TYPE_Q8_0: return make_int2(9, 16); // fork: tuned on the RX 7900 XT, MMQ is faster above 16 columns - case GGML_TYPE_IQ3_XXS: { // fork: MMVQ decodes the grid once per row but its per-column work grows with the columns + case GGML_TYPE_IQ2_S: + case GGML_TYPE_IQ3_XXS: + case GGML_TYPE_IQ3_S: { // fork: MMVQ decodes the grid once per row but its per-column work grows with the columns + // experiment: GGML_CUDA_MMQ_FEW_IQ_MIN (default 2, 0 disables), GGML_CUDA_MMQ_FEW_IQ_TYPES (type ids, default 18,21,22) static const int min_ncols = [] { - const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ3_XXS_MIN"); // experiment: 0 disables + const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ_MIN"); return e ? atoi(e) : 2; }(); - return min_ncols > 0 ? make_int2(min_ncols, 16) : make_int2(0, 0); + static const std::string types = [] { + const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ_TYPES"); + return "," + std::string(e ? e : "18,21,22") + ","; + }(); + const bool on = min_ncols > 0 && types.find("," + std::to_string(int(type)) + ",") != std::string::npos; + return on ? make_int2(min_ncols, 16) : make_int2(0, 0); } default: return make_int2(0, 0); } @@ -1465,9 +1634,15 @@ static void ggml_cuda_mul_mat_q_few_wmma(ggml_backend_cuda_context & ctx, const case GGML_TYPE_IQ4_NL: launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); break; + case GGML_TYPE_IQ2_S: + launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); + break; case GGML_TYPE_IQ3_XXS: launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); break; + case GGML_TYPE_IQ3_S: + launch_mul_mat_q_few_wmma_switch_ncols(src0, src1, dst, src1_q.get(), stream); + break; default: GGML_ABORT("unsupported type for few-row MMQ with WMMA"); } @@ -1600,7 +1775,8 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const } const int min_ncols = mmq_few_wmma_ncols_range(type, cc).x; // fork: on the RX 7900 XT the mat-vec kernels with their fused variants are as fast or faster up to 6 columns - return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 && type != GGML_TYPE_IQ3_XXS ? std::max(min_ncols, 7) : min_ncols; + const bool iq = type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S; + return GGML_CUDA_CC_IS_RDNA3_0(cc) && min_ncols != 0 && !iq ? std::max(min_ncols, 7) : min_ncols; } // NVIDIA Ampere to Blackwell @@ -1612,8 +1788,8 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const return 0; } // with fewer than 8 units of 256 values per row the warps idle, MMVQ and MMQ are better there (MUL_MAT_ID splits the rows instead) - // IQ3_XXS only has a WMMA decoder - if (mmq_few_unit_size(type) == 0 || type == GGML_TYPE_IQ3_XXS || (!has_ids && ne00 < 8*QK_K)) { + // IQ2_S and IQ3_XXS only have a WMMA decoder + if (mmq_few_unit_size(type) == 0 || type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || (!has_ids && ne00 < 8*QK_K)) { return 0; } if (has_ids && !mmq_few_id_supported(type)) { diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index a992343c89e9..6326ddbf2ff6 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -15453,12 +15453,14 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q6_K, GGML_TYPE_F32, 63, 5, 2304, {3, 2}, {2, 2})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q3_K, GGML_TYPE_F32, 64, 13, 4608, {2, 3}, {1, 1}, {0, 2, 1, 3})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_S, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); - // RDNA few-row WMMA MMQ for IQ3_XXS (2 byte aligned units of 98 bytes): 2 to 16 columns, odd rows, broadcast and permuted - for (int64_t n : {2, 3, 5, 7, 8, 9, 13, 16}) { - test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 1000, n, 5376, {1, 1}, {1, 1})); + // RDNA few-row WMMA MMQ for the I-quants (2 byte aligned units of 82 / 98 / 110 bytes): 2 to 16 columns, odd rows, broadcast and permuted + for (ggml_type type_a : {GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S}) { + for (int64_t n : {2, 3, 5, 7, 8, 9, 13, 16}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, n, 5376, {1, 1}, {1, 1})); + } + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 64, 13, 2304, {2, 3}, {1, 1}, {0, 2, 1, 3})); } - test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); - test_cases.emplace_back(new test_mul_mat(GGML_TYPE_IQ3_XXS, GGML_TYPE_F32, 64, 13, 2304, {2, 3}, {1, 1}, {0, 2, 1, 3})); // partial column group, broadcast, padded rows, src1 in [0, 1], src0 in a weights buffer test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 1000, 5, 4608, {1, 1}, {1, 1})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 64, 13, 4608, {3, 2}, {2, 2})); From 0832c56c8618acb5cd53dffc3e6f837666fee093 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:23:18 +0000 Subject: [PATCH 4/5] cuda : multi-row IQ mat-vec for verify batches (opt-in), few-row IQ WMMA opt-in from 7 columns - GGML_CUDA_MMQ_FEW_IQ=1 (min. columns 7, GGML_CUDA_MMQ_FEW_IQ_MIN overrides) replaces the experiment switches of the IQ3_XXS/IQ2_S/IQ3_S few-row WMMA decoders; off by default, so the routing is that of master. - GGML_CUDA_MMVQ_IQ_MULTIROW=1|2|4: IQ2_S, IQ3_XXS and IQ3_S mat-vec with 4 to 8 columns (also the fused gate/up GLU launch) with 2 rows per block (2: and two sub-blocks per thread and step, 4: four rows per block). Alternate RDNA3_0 table ids, chosen per launch through a template parameter of mul_mat_vec_q. - Fused gate/up mat-vec is declined for the matmuls that the opt-in few-row IQ path takes, unless the multi-row mat-vec covers them. - test-backend-ops: MUL_MAT and MUL_MAT_FUSION cases for the I-quants, 2-16 columns, k 5376. Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/fusion.cu | 9 ++ ggml/src/ggml-cuda/mmq-few.cu | 21 ++--- ggml/src/ggml-cuda/mmvq.cu | 162 +++++++++++++++++++++++---------- ggml/src/ggml-cuda/mmvq.cuh | 3 + ggml/src/ggml-cuda/vecdotq.cuh | 20 ++++ tests/test-backend-ops.cpp | 19 ++++ 6 files changed, 177 insertions(+), 57 deletions(-) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index b0e8fef79a11..ab8820d1ebe8 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -194,6 +194,15 @@ bool ggml_cuda_should_fuse_mul_mat_vec_q( return false; } + // The opt-in few-row WMMA MMQ for the I-quants (GGML_CUDA_MMQ_FEW_IQ) has no fused gate/GLU: it takes these matmuls, which then run + // unfused, unless the opt-in multi-row mat-vec (GGML_CUDA_MMVQ_IQ_MULTIROW) covers them: with both on the fused mat-vec wins. + // Without GGML_CUDA_MMQ_FEW_IQ few-row MMQ does not take I-quants on RDNA3, so this does not change anything there. + if (tensor->op == GGML_OP_MUL_MAT && GGML_CUDA_CC_IS_RDNA3_0(cc) && + (src0->type == GGML_TYPE_IQ2_S || src0->type == GGML_TYPE_IQ3_XXS || src0->type == GGML_TYPE_IQ3_S) && + ggml_cuda_should_use_mmq_few(src0, src1, nullptr, cc) && !ggml_cuda_mmvq_iq_multirow(src0->type, cc, src1->ne[1])) { + return false; + } + return use_mul_mat_vec_q; } diff --git a/ggml/src/ggml-cuda/mmq-few.cu b/ggml/src/ggml-cuda/mmq-few.cu index a7530ee35767..27586073b5e3 100644 --- a/ggml/src/ggml-cuda/mmq-few.cu +++ b/ggml/src/ggml-cuda/mmq-few.cu @@ -6,7 +6,6 @@ #include "vecdotq.cuh" #include -#include #include // mul_mat_q_few for NVIDIA uses the int8 MMA fragment layout and cp.async, mul_mat_q_few_wmma for RDNA the WMMA layout. Not on MUSA. @@ -1576,18 +1575,18 @@ static int2 mmq_few_wmma_ncols_range(const ggml_type type, const int cc) { case GGML_TYPE_Q8_0: return make_int2(9, 16); // fork: tuned on the RX 7900 XT, MMQ is faster above 16 columns case GGML_TYPE_IQ2_S: case GGML_TYPE_IQ3_XXS: - case GGML_TYPE_IQ3_S: { // fork: MMVQ decodes the grid once per row but its per-column work grows with the columns - // experiment: GGML_CUDA_MMQ_FEW_IQ_MIN (default 2, 0 disables), GGML_CUDA_MMQ_FEW_IQ_TYPES (type ids, default 18,21,22) + case GGML_TYPE_IQ3_S: { // fork, opt-in: MMVQ decodes the grid once per row but its per-column work grows with the columns + // GGML_CUDA_MMQ_FEW_IQ=1 enables it from 7 columns (verify batches), GGML_CUDA_MMQ_FEW_IQ_MIN= changes the minimum (2 to 5 was + // break-even or worse in the prototype). Not used for the fused gate/up mat-vec of MMVQ, see GGML_CUDA_MMVQ_IQ_MULTIROW. static const int min_ncols = [] { - const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ_MIN"); - return e ? atoi(e) : 2; - }(); - static const std::string types = [] { - const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ_TYPES"); - return "," + std::string(e ? e : "18,21,22") + ","; + const char * e = getenv("GGML_CUDA_MMQ_FEW_IQ"); + if (!e || atoi(e) == 0) { + return 0; + } + const char * m = getenv("GGML_CUDA_MMQ_FEW_IQ_MIN"); + return m ? std::max(atoi(m), 0) : 7; }(); - const bool on = min_ncols > 0 && types.find("," + std::to_string(int(type)) + ",") != std::string::npos; - return on ? make_int2(min_ncols, 16) : make_int2(0, 0); + return min_ncols > 0 ? make_int2(min_ncols, 16) : make_int2(0, 0); } default: return make_int2(0, 0); } diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu index 6247ba2f77f3..ee69a94fd441 100644 --- a/ggml/src/ggml-cuda/mmvq.cu +++ b/ggml/src/ggml-cuda/mmvq.cu @@ -4,6 +4,7 @@ #include "vecdotq.cuh" #include +#include #include // only enabled on DGX Spark, where it is a gain on every type below. On the higher-bandwidth parts the kernel @@ -99,13 +100,41 @@ enum mmvq_parameter_table_id { MMVQ_PARAMETERS_RDNA3_0, MMVQ_PARAMETERS_RDNA4, MMVQ_PARAMETERS_GB10, - MMVQ_PARAMETERS_ORIN + MMVQ_PARAMETERS_ORIN, + // Never the table of a device: opt-in variants of MMVQ_PARAMETERS_RDNA3_0 for the grid table I-quants (IQ2_S, IQ3_XXS, IQ3_S) with 4 to 8 + // columns, selected per launch by GGML_CUDA_MMVQ_IQ_MULTIROW (see ggml_cuda_mmvq_iq_multirow). Several rows per block share the q8_1 loads + // and scales of the columns; _VDR4 also takes two sub-blocks of 32 values per step. + MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2, + MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4, + MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS4 }; +static constexpr __host__ __device__ bool mmvq_is_iq_multirow_table(const mmvq_parameter_table_id table_id) { + return table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2 || table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4 || + table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS4; +} + +static constexpr __host__ __device__ bool mmvq_is_iq_multirow_type(const ggml_type type) { + return type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_XXS || type == GGML_TYPE_IQ3_S; +} + +// The table a kernel uses: the one of the device, or on RDNA3 (RX 7900 XT) the IQ multi-row variant that it was instantiated for. +static constexpr __host__ __device__ mmvq_parameter_table_id mmvq_effective_table_id( + const mmvq_parameter_table_id device_table_id, const mmvq_parameter_table_id alt_table_id) { + return device_table_id == MMVQ_PARAMETERS_RDNA3_0 && mmvq_is_iq_multirow_table(alt_table_id) ? alt_table_id : device_table_id; +} + // The vdr of a type for a number of dst columns. On RDNA3 (RX 7900 XT) a thread takes wider steps with several // columns: the scales of the block and the scale and sum of each q8_1 column are then read and applied for more values // at once, and that per-column work is what limits the kernel there. static constexpr __host__ __device__ int get_vdr_mmvq(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id) { + if (mmvq_is_iq_multirow_table(table_id)) { + // the I-quant variants of RDNA3: with ncols_dst > 1 each thread of _VDR4 takes two of the sub-blocks (of 32 values) of a block + if (table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4 && ncols_dst > 1 && mmvq_is_iq_multirow_type(type)) { + return 4; + } + table_id = MMVQ_PARAMETERS_RDNA3_0; + } if (table_id == MMVQ_PARAMETERS_RDNA3_0) { switch (type) { case GGML_TYPE_Q4_K: @@ -141,6 +170,12 @@ static constexpr __device__ vec_dot_q_cuda_t get_vec_dot_q_cuda(ggml_type type, return vec_dot_q4_0_q8_1_vdr<4>; case GGML_TYPE_Q8_0: return vdr == 8 ? vec_dot_q8_0_q8_1_vdr<8> : vec_dot_q8_0_q8_1_vdr<4>; + case GGML_TYPE_IQ2_S: + return vec_dot_iq2_s_q8_1_vdr<4>; + case GGML_TYPE_IQ3_XXS: + return vec_dot_iq3_xxs_q8_1_vdr<4>; + case GGML_TYPE_IQ3_S: + return vec_dot_iq3_s_q8_1_vdr<4>; default: return nullptr; } @@ -201,6 +236,25 @@ static __host__ mmvq_parameter_table_id get_device_table_id(int cc) { return MMVQ_PARAMETERS_GENERIC; } +// GGML_CUDA_MMVQ_IQ_MULTIROW (opt-in, RDNA3 desktop only): IQ2_S, IQ3_XXS and IQ3_S mat-vec with 4 to 8 columns (speculative verify batches) +// with several rows per block, so that the q8_1 loads and the per-column scales are shared by the rows. Also the fused gate/up (GLU) launch. +// 1: 2 rows per block, 2: 2 rows per block and two sub-blocks per thread and step, 4: 4 rows per block. Any other non-zero value is 1. +// Returns MMVQ_PARAMETERS_GENERIC (never a device table here) when off. +static mmvq_parameter_table_id mmvq_iq_multirow_table_id() { + static const mmvq_parameter_table_id table_id = [] { + const char * e = getenv("GGML_CUDA_MMVQ_IQ_MULTIROW"); + const int v = e ? atoi(e) : 0; + return v == 0 ? MMVQ_PARAMETERS_GENERIC : v == 2 ? MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4 : + v == 4 ? MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS4 : MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2; + }(); + return table_id; +} + +bool ggml_cuda_mmvq_iq_multirow(const ggml_type type, const int cc, const int64_t ncols) { + return GGML_CUDA_CC_IS_RDNA3_0(cc) && mmvq_is_iq_multirow_type(type) && ncols >= 4 && ncols <= MMVQ_MAX_BATCH_SIZE && + mmvq_iq_multirow_table_id() != MMVQ_PARAMETERS_GENERIC; +} + // Per-architecture maximum batch size for which MMVQ should be used for MUL_MAT_ID. // Returns a value <= MMVQ_MAX_BATCH_SIZE. Default is MMVQ_MAX_BATCH_SIZE. // Check https://github.com/ggml-org/llama.cpp/pull/20905#issuecomment-4145835627 for details @@ -715,6 +769,12 @@ 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_RDNA3_0_IQ_ROWS2 || table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4) { + return ncols_dst >= 4 ? 2 : 1; + } + if (table_id == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS4) { + return ncols_dst >= 4 ? 4 : 1; // ncols_dst*4 <= 32 lanes for the fused outputs + } if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_PASCAL_DP4A || table_id == MMVQ_PARAMETERS_GCN || table_id == MMVQ_PARAMETERS_TURING || table_id == MMVQ_PARAMETERS_GB10 || table_id == MMVQ_PARAMETERS_RDNA1) { switch (ncols_dst) { case 1: @@ -783,7 +843,7 @@ static constexpr __host__ __device__ bool mmvq_mixed_shared_expert(const ggml_ty // type_shared: type of the shared expert if it differs from type, see mmvq_mixed_shared_expert template + ggml_type type_shared = GGML_TYPE_COUNT, mmvq_parameter_table_id alt_table_id = MMVQ_PARAMETERS_GENERIC> __launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, halve_iters)*ggml_cuda_get_physical_warp_size(), 1) static __global__ void mul_mat_vec_q( const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr, @@ -799,7 +859,7 @@ static __global__ void mul_mat_vec_q( constexpr int qk = ggml_cuda_type_traits::qk; constexpr int qi = ggml_cuda_type_traits::qi; - constexpr mmvq_parameter_table_id table_id = get_device_table_id(); + constexpr mmvq_parameter_table_id table_id = mmvq_effective_table_id(get_device_table_id(), alt_table_id); constexpr int vdr = get_vdr_mmvq(type, ncols_dst, table_id); constexpr int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters); constexpr int rows_per_cuda_block = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps); @@ -1277,7 +1337,7 @@ static std::pair calc_launch_params( return {block_nums, block_dims}; } -template +template static void mul_mat_vec_q_switch_fusion( const void * vx, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, @@ -1303,7 +1363,7 @@ static void mul_mat_vec_q_switch_fusion( } #endif // GGML_USE_HIP GGML_ASSERT(fusion.shared_type == GGML_TYPE_COUNT); - ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, + ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); @@ -1311,7 +1371,7 @@ static void mul_mat_vec_q_switch_fusion( } const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, nbytes_shared, stream); - ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, + ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); @@ -1450,6 +1510,41 @@ static void mul_mat_vec_q_switch_ncols_dst( return; } + // 4 to 8 columns. With GGML_CUDA_MMVQ_IQ_MULTIROW the I-quants with grid tables run the multi-row variant of the RDNA3 table. + const auto launch_few_columns = [&](auto ncols_tag) { + constexpr int c_ncols_dst = decltype(ncols_tag)::value; + + const auto launch = [&](auto alt_table_tag) { + constexpr mmvq_parameter_table_id c_alt = decltype(alt_table_tag)::value; + const mmvq_parameter_table_id launch_table_id = c_alt == MMVQ_PARAMETERS_GENERIC ? table_id : c_alt; + std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, launch_table_id); + mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, + channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, + sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, + dims.first, dims.second, 0, ids_stride, stream); + }; + + if constexpr (mmvq_is_iq_multirow_type(type)) { + if (ggml_cuda_mmvq_iq_multirow(type, cc, c_ncols_dst)) { + // rows past the end of the matrix would be read otherwise + const mmvq_parameter_table_id mr = mmvq_iq_multirow_table_id(); + if (mr == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS4 && nrows_x % 4 == 0) { + launch(std::integral_constant{}); + return; + } + if (mr == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2_VDR4 && nrows_x % 2 == 0) { + launch(std::integral_constant{}); + return; + } + if (mr == MMVQ_PARAMETERS_RDNA3_0_IQ_ROWS2 && nrows_x % 2 == 0) { + launch(std::integral_constant{}); + return; + } + } + } + launch(std::integral_constant{}); + }; + switch (ncols_dst) { case 1: { // static, else MSVC lambda capture breaks the constexpr uses below @@ -1498,46 +1593,21 @@ static void mul_mat_vec_q_switch_ncols_dst( sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, dims.first, dims.second, 0, ids_stride, stream); } break; - case 4: { - constexpr int c_ncols_dst = 4; - std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); - mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, - channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, - sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, - dims.first, dims.second, 0, ids_stride, stream); - } break; - case 5: { - constexpr int c_ncols_dst = 5; - std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); - mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, - channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, - sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, - dims.first, dims.second, 0, ids_stride, stream); - } break; - case 6: { - constexpr int c_ncols_dst = 6; - std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); - mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, - channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, - sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, - dims.first, dims.second, 0, ids_stride, stream); - } break; - case 7: { - constexpr int c_ncols_dst = 7; - std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); - mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, - channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, - sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, - dims.first, dims.second, 0, ids_stride, stream); - } break; - case 8: { - constexpr int c_ncols_dst = 8; - std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); - mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, - channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, - sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, - dims.first, dims.second, 0, ids_stride, stream); - } break; + case 4: + launch_few_columns(std::integral_constant{}); + break; + case 5: + launch_few_columns(std::integral_constant{}); + break; + case 6: + launch_few_columns(std::integral_constant{}); + break; + case 7: + launch_few_columns(std::integral_constant{}); + break; + case 8: + launch_few_columns(std::integral_constant{}); + break; default: GGML_ABORT("fatal error"); } diff --git a/ggml/src/ggml-cuda/mmvq.cuh b/ggml/src/ggml-cuda/mmvq.cuh index c774ffa9ea88..f069ef6993d1 100644 --- a/ggml/src/ggml-cuda/mmvq.cuh +++ b/ggml/src/ggml-cuda/mmvq.cuh @@ -4,6 +4,9 @@ bool ggml_cuda_should_use_mmvq(enum ggml_type type, int cc, int64_t ne11); +// whether the opt-in multi-row mat-vec of GGML_CUDA_MMVQ_IQ_MULTIROW covers a launch of this type with ncols columns (RDNA3 desktop) +bool ggml_cuda_mmvq_iq_multirow(ggml_type type, int cc, int64_t ncols); + // Returns the maximum batch size for which MMVQ should be used for MUL_MAT_ID, // based on the quantization type and GPU architecture (compute capability). int get_mmvq_mmid_max_batch(ggml_type type, int cc); diff --git a/ggml/src/ggml-cuda/vecdotq.cuh b/ggml/src/ggml-cuda/vecdotq.cuh index 9151eb2edf49..e00726f514fe 100644 --- a/ggml/src/ggml-cuda/vecdotq.cuh +++ b/ggml/src/ggml-cuda/vecdotq.cuh @@ -1418,6 +1418,26 @@ static __device__ __forceinline__ float vec_dot_iq3_s_q8_1( return d * sumi; } +// The grid table I-quants with a wider step (RDNA3 mat-vec with several columns): a call covers vdr/2 consecutive sub-blocks of 32 values, +// the dependent table loads of the sub-blocks are then in flight together. iqs is in 0, vdr, .., 16 - vdr. +#define MMVQ_IQ_VDR_WRAPPER(name) \ + template \ + static __device__ __forceinline__ float vec_dot_##name##_q8_1_vdr( \ + const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) { \ + float sum = 0.0f; \ + _Pragma("unroll") \ + for (int v = 0; v < vdr/2; ++v) { \ + const int iqs_v = iqs + 2*v; \ + sum += vec_dot_##name##_q8_1(vbq, bq8_1, kbx, iqs_v); \ + } \ + return sum; \ + } + +MMVQ_IQ_VDR_WRAPPER(iq2_s) +MMVQ_IQ_VDR_WRAPPER(iq3_xxs) +MMVQ_IQ_VDR_WRAPPER(iq3_s) +#undef MMVQ_IQ_VDR_WRAPPER + #define VDR_IQ1_S_Q8_1_MMVQ 1 #define VDR_IQ1_S_Q8_1_MMQ 1 diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 6326ddbf2ff6..125a75aacf3b 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -15461,6 +15461,25 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 63, 5, 4608, {3, 2}, {2, 2})); test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 64, 13, 2304, {2, 3}, {1, 1}, {0, 2, 1, 3})); } + // [w2 gemma-iq-verify] I-quant verify batches (Gemma 4 31B: k 5376): 2 to 8 columns (MMVQ, optionally with several rows per block + // (GGML_CUDA_MMVQ_IQ_MULTIROW), few-row WMMA MMQ from 7 columns (GGML_CUDA_MMQ_FEW_IQ)), 12 and 16 columns, plain and fused gate/up + GLU + for (ggml_type type_a : {GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S}) { + for (int64_t m : {16384, 8192}) { + for (int64_t n : {2, 3, 4, 5, 6, 7, 8, 12, 16}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, m, n, 5376, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat_fusion(type_a, GGML_GLU_OP_GEGLU, n, m, 5376, + false, 1, 1, false, false, true, false, { 1, 1 })); + } + } + // rows that are not a multiple of 2 or 4 (the multi-row variants must fall back) + for (int64_t m : {1002, 1001}) { + for (int64_t n : {4, 7, 8}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, m, n, 5376, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat_fusion(type_a, GGML_GLU_OP_SWIGLU, n, m, 5376, + false, 1, 1, false, false, true, false, { 1, 1 })); + } + } + } // partial column group, broadcast, padded rows, src1 in [0, 1], src0 in a weights buffer test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 1000, 5, 4608, {1, 1}, {1, 1})); test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 64, 13, 4608, {3, 2}, {2, 2})); From 81b858f24105a99483807e1f0a77eb4759bcc2ba Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:28:30 +0000 Subject: [PATCH 5/5] cuda : pass dst to ggml_cuda_should_use_mmq_few in the I-quant verify checks Upstream (synced in #177) added the dst argument that carries the requested src1 precision; the two calls of this branch still used the 4-argument form. Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/fusion.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index ab8820d1ebe8..ac7171a4aa74 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -199,7 +199,7 @@ bool ggml_cuda_should_fuse_mul_mat_vec_q( // Without GGML_CUDA_MMQ_FEW_IQ few-row MMQ does not take I-quants on RDNA3, so this does not change anything there. if (tensor->op == GGML_OP_MUL_MAT && GGML_CUDA_CC_IS_RDNA3_0(cc) && (src0->type == GGML_TYPE_IQ2_S || src0->type == GGML_TYPE_IQ3_XXS || src0->type == GGML_TYPE_IQ3_S) && - ggml_cuda_should_use_mmq_few(src0, src1, nullptr, cc) && !ggml_cuda_mmvq_iq_multirow(src0->type, cc, src1->ne[1])) { + ggml_cuda_should_use_mmq_few(src0, src1, nullptr, tensor, cc) && !ggml_cuda_mmvq_iq_multirow(src0->type, cc, src1->ne[1])) { return false; } @@ -230,7 +230,7 @@ static const ggml_tensor * ggml_cuda_q8_1_consumer(const ggml_backend_cuda_conte // few-row MMQ quantizes src1 itself and goes before MMVQ in ggml_cuda_mul_mat const bool mmvq = n->op == GGML_OP_MUL_MAT_ID ? n->ne[2] <= get_mmvq_mmid_max_batch(src0->type, cc) && src1->ne[1] == 1 - : ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]) && !ggml_cuda_should_use_mmq_few(src0, src1, nullptr, cc); + : ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]) && !ggml_cuda_should_use_mmq_few(src0, src1, nullptr, n, cc); const bool bad_padding_clear = ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE && ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) && src0->view_src;