Commit 8d19519d4 for llama.cpp

commit 8d19519d4a956485a4bc8b513d724b735ca7546e
Author: Aman Gupta <amangupta052@gmail.com>
Date:   Sat Nov 28 20:27:36 2026 +0800

    HIP: few-row MMQ with WMMA for RDNA3.5 (#32224)

    * HIP: few-row MMQ with WMMA for RDNA3.5

    Port of the few-row MMQ kernel to RDNA3 with 16x16x16 int8 WMMA, for up
    to 16 src1 columns. src1 is the A operand and src0 the B operand so the
    row scales stay lane-local. src1 gets its own quantizer layout. Covers
    Q4_K, Q5_K, Q6_K and IQ4_NL. Enabled on RDNA3.5, where it was tuned.

    * Switch to mma.h and support rdna4

    * HIP/CUDA: ensure that the wmma mmq_few kernel is only selected when ne0 is a multiple of the superblock size

    ---------

    Co-authored-by: Carl Philipp Klemm <carl@uvos.xyz>

diff --git a/ggml/src/ggml-cuda/mmq-few.cu b/ggml/src/ggml-cuda/mmq-few.cu
index 2fe47ddd2..e4a85297b 100644
--- a/ggml/src/ggml-cuda/mmq-few.cu
+++ b/ggml/src/ggml-cuda/mmq-few.cu
@@ -7,8 +7,8 @@

 #include <type_traits>

-// NVIDIA only: the kernel uses the NVIDIA int8 MMA fragment layout and cp.async.
-#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
+// 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.
+#if !defined(GGML_USE_MUSA)

 using namespace ggml_cuda_mma;

@@ -719,6 +719,353 @@ static __global__ void mul_mat_q_few(
 #endif // defined(CP_ASYNC_AVAILABLE)
 }

+// RDNA version with WMMA for up to 16 src1 columns. A WMMA multiplies 16x16 int8 matrices, see mma.cuh:
+//   A = src1, B = src0, C = A @ B.
+//   A and B: lane l holds the column resp. row l%16, the values [16k, 16k + 16) on RDNA3 and [8*(l/16), 8*(l/16) + 8) on RDNA4.
+//   C: lane l holds the src1 columns 2v + l/16 (RDNA3) resp. 8*(l/16) + v (RDNA4) of the src0 row l%16.
+// So the src0 scales of a row stay in the lane that decodes it, the src1 scales of the C columns come with the layout in quantize.cuh.
+// The lanes decode their rows straight from global memory. A ring as in mul_mat_q_few, filled through registers without cp.async, was slower.
+// 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_IQ4_NL || type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K;
+}
+
+// the reduction over the warps
+static constexpr int MMQ_FEW_WMMA_NBYTES_SHARED = (MMQ_FEW_NWARPS - 1)*8*WARP_SIZE*sizeof(float);
+
+#if defined(AMD_WMMA_AVAILABLE)
+
+// The A and B tiles of a lane for one WMMA, MIRRORED on RDNA3 where both half-warps hold the same data.
+#if defined(AMD_WMMA_256B_AVAILABLE)
+using mmq_few_tile_ab = tile<16, 4, int, DATA_LAYOUT_I_MAJOR_MIRRORED>;
+#else
+using mmq_few_tile_ab = tile<16, 4, int, DATA_LAYOUT_I_MAJOR>;
+#endif
+using mmq_few_tile_c = tile<16, 16, int, DATA_LAYOUT_J_MAJOR>;
+
+// int32 per lane and WMMA, 4 on RDNA3 and 2 on RDNA4
+static constexpr int MMQ_FEW_WMMA_NE = mmq_few_tile_ab::ne;
+
+// The src1 fragments of a lane for one sub-block, see quantize.cuh.
+struct mmq_few_wmma_y {
+    int qs[2][MMQ_FEW_WMMA_NE]; // [hh] holds the values [16*hh, 16*hh + 16) of the WMMA
+    int ds[8];                  // d (D4) resp. (d, sum) (DS4) of the C columns, ds[v] of C.x[v]
+};
+
+// The C registers of a lane, on RDNA3 the ones with v >= 4 hold only the columns >= 8.
+template <bool half>
+static constexpr int mmq_few_wmma_nv() {
+    return half && MMQ_FEW_WMMA_NE == 4 ? 4 : 8;
+}
+
+// half: only the first 8 columns are used, the others are zero.
+template <bool half>
+static __device__ __forceinline__ mmq_few_wmma_y mmq_few_wmma_load_y(const int4 * y, const int s) {
+    const int m = threadIdx.x % 16;
+    const int h = threadIdx.x / 16;
+
+    mmq_few_wmma_y yf;
+    if constexpr (MMQ_FEW_WMMA_NE == 2) {
+        // RDNA4: the lane holds the values [16*hh + 8*h, 16*hh + 8*h + 8) of column m.
+        // The src1 quantize kernel zeroes the columns beyond ne1, a branch to skip the unused ones is more expensive than the loads.
+        const int2 * y2 = (const int2 *) y;
+        const int2 q0 = y2[(s*MMQ_FEW_WMMA_NCOLS + m)*4 + 0 + h];
+        const int2 q1 = y2[(s*MMQ_FEW_WMMA_NCOLS + m)*4 + 2 + h];
+        yf.qs[0][0] = q0.x; yf.qs[0][1] = q0.y;
+        yf.qs[1][0] = q1.x; yf.qs[1][1] = q1.y;
+    } else if (half && m >= MMQ_FEW_WMMA_NCOLS/2) {
+        yf.qs[0][0] = 0; yf.qs[0][1] = 0; yf.qs[0][2] = 0; yf.qs[0][3] = 0;
+        yf.qs[1][0] = 0; yf.qs[1][1] = 0; yf.qs[1][2] = 0; yf.qs[1][3] = 0;
+    } else {
+        const int4 q0 = y[(s*MMQ_FEW_WMMA_NCOLS + m)*2 + 0];
+        const int4 q1 = y[(s*MMQ_FEW_WMMA_NCOLS + m)*2 + 1];
+        yf.qs[0][0] = q0.x; yf.qs[0][1] = q0.y; yf.qs[0][2] = q0.z; yf.qs[0][3] = q0.w;
+        yf.qs[1][0] = q1.x; yf.qs[1][1] = q1.y; yf.qs[1][2] = q1.z; yf.qs[1][3] = q1.w;
+    }
+
+    if constexpr (MMQ_FEW_WMMA_NE == 2) {
+        // RDNA4: C.x[v] is the column 8*h + v
+        const int4 d0 = y[8*MMQ_FEW_WMMA_NCOLS*2 + 4*s + 0 + h];
+        const int4 d1 = y[8*MMQ_FEW_WMMA_NCOLS*2 + 4*s + 2 + h];
+        yf.ds[0] = d0.x; yf.ds[1] = d1.x; yf.ds[2] = d0.y; yf.ds[3] = d1.y;
+        yf.ds[4] = d0.z; yf.ds[5] = d1.z; yf.ds[6] = d0.w; yf.ds[7] = d1.w;
+    } else {
+        const int4 ds0 = y[8*MMQ_FEW_WMMA_NCOLS*2 + (s*2 + h)*2 + 0];
+        const int4 ds1 = half ? make_int4(0, 0, 0, 0) : y[8*MMQ_FEW_WMMA_NCOLS*2 + (s*2 + h)*2 + 1];
+        yf.ds[0] = ds0.x; yf.ds[1] = ds0.y; yf.ds[2] = ds0.z; yf.ds[3] = ds0.w;
+        yf.ds[4] = ds1.x; yf.ds[5] = ds1.y; yf.ds[6] = ds1.z; yf.ds[7] = ds1.w;
+    }
+    return yf;
+}
+
+template <mmq_q8_1_ds_layout ds_layout>
+static __device__ __forceinline__ float mmq_few_wmma_get_d(const mmq_few_wmma_y & yf, const int v) {
+    if constexpr (ds_layout == MMQ_Q8_1_DS_LAYOUT_D4) {
+        return __int_as_float(yf.ds[v]);
+    } else {
+        return __low2float(*(const half2 *) &yf.ds[v]);
+    }
+}
+
+static __device__ __forceinline__ float mmq_few_wmma_get_sum(const mmq_few_wmma_y & yf, const int v) {
+    return __high2float(*(const half2 *) &yf.ds[v]);
+}
+
+// n ints at the even byte offset o of the 4 byte aligned p, with whole 4 byte loads. In the unrolled decoders o is a constant.
+template <int n>
+static __device__ __forceinline__ void mmq_few_wmma_get_ints_b2(const char * p, const int o, int * dst) {
+    const int * w = (const int *) (p + (o & ~3));
+    if (o % 4 == 0) {
+#pragma unroll
+        for (int i = 0; i < n; ++i) {
+            dst[i] = w[i];
+        }
+    } else {
+        int lo = w[0];
+#pragma unroll
+        for (int i = 0; i < n; ++i) {
+            const int hi = w[i + 1];
+            dst[i] = __builtin_amdgcn_alignbyte(hi, lo, 2);
+            lo = hi;
+        }
+    }
+}
+
+static __device__ __forceinline__ void mmq_few_wmma(mmq_few_tile_c & C, const int * a, const int * b) {
+    mmq_few_tile_ab A;
+    mmq_few_tile_ab B;
+#pragma unroll
+    for (int l = 0; l < MMQ_FEW_WMMA_NE; ++l) {
+        A.x[l] = a[l];
+        B.x[l] = b[l];
+    }
+    mma(C, A, B);
+}
+
+// vec_dot: one unit of 256 values of row l%16, x points to its data, y to the src1 fragments of the unit.
+// The decoders write sub-block s of the row to b[8], the int32 at index i holds the values [4i, 4i + 4).
+// One WMMA uses b + 4*hh + i0, with i0 = (4 - MMQ_FEW_WMMA_NE)*(l/16) the offset of the values of the lane.
+
+template <ggml_type type, bool half>
+static __device__ __forceinline__ void mmq_few_wmma_vec_dot_q45_K(const char * x, const int4 * y, float (&sum)[8]) {
+    using block_t = std::conditional_t<type == GGML_TYPE_Q4_K, block_q4_K, block_q5_K>;
+    const block_t * bx = (const block_t *) __builtin_assume_aligned(x, 16);
+
+    const int4  hdr       = *(const int4 *) bx; // dm and the 12 scale bytes
+    const int   scales[3] = {hdr.y, hdr.z, hdr.w};
+    const int   sc[2]     = {unpack_scales_q45_K(scales, 0), unpack_scales_q45_K(scales, 1)};
+    const int   mn[2]     = {unpack_scales_q45_K(scales, 2), unpack_scales_q45_K(scales, 3)};
+    const float2 dm       = __half22float2(bx->dm);
+
+    [[maybe_unused]] int qh[8]; // Q5_K: bit s holds the high bit of sub-block s
+    if constexpr (type == GGML_TYPE_Q5_K) {
+#pragma unroll
+        for (int i = 0; i < 8; ++i) {
+            qh[i] = get_int_b4(bx->qh, i);
+        }
+    }
+
+    float sum_d[8] = {0.0f};
+    float sum_m[8] = {0.0f};
+    const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
+#pragma unroll
+    for (int j = 0; j < 4; ++j) {
+        int qs[8];
+#pragma unroll
+        for (int i = 0; i < 8; ++i) {
+            qs[i] = get_int_b4(bx->qs + 32*j, i);
+        }
+#pragma unroll
+        for (int s = 2*j; s < 2*j + 2; ++s) {
+            int b[8];
+#pragma unroll
+            for (int i = 0; i < 8; ++i) {
+                b[i] = (qs[i] >> (4*(s % 2))) & 0x0F0F0F0F;
+                if constexpr (type == GGML_TYPE_Q5_K) {
+                    b[i] |= ((qh[i] >> s) & 0x01010101) << 4;
+                }
+            }
+            const int   ls = (sc[s/4] >> (8*(s % 4))) & 0xFF;
+            const float m  = (mn[s/4] >> (8*(s % 4))) & 0xFF;
+
+            const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(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<half>(); ++v) {
+                sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_DS4>(yf, v) * float(C.x[v]*ls);
+                sum_m[v] += m * mmq_few_wmma_get_sum(yf, v);
+            }
+        }
+    }
+#pragma unroll
+    for (int v = 0; v < 8; ++v) {
+        sum[v] += dm.x*sum_d[v] - dm.y*sum_m[v];
+    }
+}
+
+// Q6_K has a scale per 16 values, one WMMA per half of a sub-block.
+template <bool half>
+static __device__ __forceinline__ void mmq_few_wmma_vec_dot_q6_K(const char * x, const int4 * y, float (&sum)[8]) {
+    const block_q6_K * bx = (const block_q6_K *) x;
+    const float d = __half2float(bx->d);
+
+    float sum_d[8] = {0.0f};
+    const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
+#pragma unroll
+    for (int n = 0; n < 2; ++n) {
+        int ql[2][8]; // [ql offset 0 or 32]
+        int qh[8];
+#pragma unroll
+        for (int i = 0; i < 8; ++i) {
+            ql[0][i] = get_int_b2(bx->ql + 64*n,      i);
+            ql[1][i] = get_int_b2(bx->ql + 64*n + 32, i);
+            qh[i]    = get_int_b2(bx->qh + 32*n,      i);
+        }
+        const int sc[2] = {get_int_b2(bx->scales + 8*n, 0), get_int_b2(bx->scales + 8*n, 1)};
+#pragma unroll
+        for (int j = 0; j < 4; ++j) {
+            const int s = 4*n + j;
+            int b[8];
+#pragma unroll
+            for (int i = 0; i < 8; ++i) {
+                b[i] = __vsub4(((ql[j % 2][i] >> (4*(j/2))) & 0x0F0F0F0F) | (((qh[i] >> (2*j)) & 0x03030303) << 4), 0x20202020);
+            }
+            const int sc_lo = (int8_t) (sc[j/2] >> (16*(j % 2) + 0)); // scale of the values [0, 16)
+            const int sc_hi = (int8_t) (sc[j/2] >> (16*(j % 2) + 8)); // scale of the values [16, 32)
+
+            const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(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<half>(); ++v) {
+                sum_d[v] += mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * float(C_lo.x[v]*sc_lo + C_hi.x[v]*sc_hi);
+            }
+        }
+    }
+#pragma unroll
+    for (int v = 0; v < 8; ++v) {
+        sum[v] += d*sum_d[v];
+    }
+}
+
+// IQ4_NL has a block with its own scale per 32 values.
+template <bool half>
+static __device__ __forceinline__ void mmq_few_wmma_vec_dot_iq4_nl(const char * x, const int4 * y, float (&sum)[8]) {
+    const int i0 = (4 - MMQ_FEW_WMMA_NE)*(threadIdx.x / 16);
+#pragma unroll
+    for (int s = 0; s < 8; ++s) {
+        // a unit starts 16 byte aligned, the qs of every second block 2 bytes off a 4 byte boundary
+        const char * xa = (const char *) __builtin_assume_aligned(x, 16);
+        int q4[4];
+        mmq_few_wmma_get_ints_b2<4>(xa, int(sizeof(block_iq4_nl))*s + 2, q4);
+        int b[8];
+#pragma unroll
+        for (int i = 0; i < 4; ++i) {
+            const int2 v = get_int_from_table_16(q4[i], kvalues_iq4nl);
+            b[i + 0] = v.x;
+            b[i + 4] = v.y;
+        }
+        const float d = __half2float(((const block_iq4_nl *) xa + s)->d);
+
+        const mmq_few_wmma_y yf = mmq_few_wmma_load_y<half>(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<half>(); ++v) {
+            sum[v] += d*mmq_few_wmma_get_d<MMQ_Q8_1_DS_LAYOUT_D4>(yf, v) * float(C.x[v]);
+        }
+    }
+}
+
+template <ggml_type type, bool half>
+static __device__ __forceinline__ void mmq_few_wmma_vec_dot(const char * x, const int4 * y, float (&sum)[8]) {
+    if constexpr (type == GGML_TYPE_IQ4_NL) {
+        mmq_few_wmma_vec_dot_iq4_nl<half>(x, y, sum);
+    } else if constexpr (type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K) {
+        mmq_few_wmma_vec_dot_q45_K<type, half>(x, y, sum);
+    } else if constexpr (type == GGML_TYPE_Q6_K) {
+        mmq_few_wmma_vec_dot_q6_K<half>(x, y, sum);
+    } else {
+        static_assert(type == GGML_TYPE_COUNT, "unsupported type");
+    }
+}
+
+#endif // defined(AMD_WMMA_AVAILABLE)
+
+// The warps split K in units of 256 values, a block has 16 src0 rows. half: at most 8 src1 columns.
+template <ggml_type type, bool half>
+__launch_bounds__(MMQ_FEW_NWARPS*WARP_SIZE, 1)
+static __global__ void mul_mat_q_few_wmma(
+        const char * x, const int4 * y_ptr, float * dst,
+        const int nunits, const int nrows_x, const int ncols_dst, const int64_t stride_row_x, const int stride_col_dst,
+        const int channel_ratio, const int64_t stride_channel_x, const int stride_channel_dst,
+        const int sample_ratio, const int64_t stride_sample_x, const int stride_sample_dst) {
+#if defined(AMD_WMMA_AVAILABLE)
+    constexpr int nwarps    = MMQ_FEW_NWARPS;
+    constexpr int warp_size = WARP_SIZE;
+    constexpr int unit_size = mmq_few_unit_size(type);
+    static_assert(mmq_few_wmma_supported(type), "unsupported type");
+
+    extern __shared__ float buf_sum_mmq_few_wmma[];
+
+    const int row0        = blockIdx.x*MMQ_FEW_NROWS;
+    const int channel_dst = blockIdx.y;
+    const int sample_dst  = blockIdx.z;
+    const int nchannels   = gridDim.y;
+
+    x   += int64_t(sample_dst/sample_ratio)*stride_sample_x + int64_t(channel_dst/channel_ratio)*stride_channel_x;
+    dst += int64_t(sample_dst)*stride_sample_dst + int64_t(channel_dst)*stride_channel_dst;
+    const int4 * GGML_CUDA_RESTRICT y = y_ptr + (int64_t(sample_dst)*nchannels + channel_dst)*nunits*MMQ_FEW_WMMA_Q8_1_INT4;
+
+    const char * xr = x + int64_t(min(row0 + int(threadIdx.x) % 16, nrows_x - 1))*stride_row_x;
+
+    float sum[8] = {0.0f};
+    for (int iu = threadIdx.y; iu < nunits; iu += nwarps) {
+        mmq_few_wmma_vec_dot<type, half>(xr + int64_t(iu)*unit_size, y + int64_t(iu)*MMQ_FEW_WMMA_Q8_1_INT4, sum);
+    }
+
+    // reduce the sums over the warps
+    float * buf_sum = buf_sum_mmq_few_wmma;
+    if (threadIdx.y > 0) {
+#pragma unroll
+        for (int v = 0; v < 8; ++v) {
+            buf_sum[((threadIdx.y - 1)*8 + v)*warp_size + threadIdx.x] = sum[v];
+        }
+    }
+    __syncthreads();
+    if (threadIdx.y > 0) {
+        return;
+    }
+#pragma unroll
+    for (int w = 0; w < nwarps - 1; ++w) {
+#pragma unroll
+        for (int v = 0; v < 8; ++v) {
+            sum[v] += buf_sum[(w*8 + v)*warp_size + threadIdx.x];
+        }
+    }
+
+    const int i = row0 + mmq_few_tile_c::get_j(0);
+#pragma unroll
+    for (int v = 0; v < mmq_few_wmma_nv<half>(); ++v) {
+        const int j = mmq_few_tile_c::get_i(v);
+        if (i < nrows_x && j < ncols_dst) {
+            dst[int64_t(j)*stride_col_dst + i] = sum[v];
+        }
+    }
+#else
+    GGML_UNUSED_VARS(x, y_ptr, dst, nunits, nrows_x, ncols_dst, stride_row_x, stride_col_dst,
+        channel_ratio, stride_channel_x, stride_channel_dst,
+        sample_ratio, stride_sample_x, stride_sample_dst);
+    NO_DEVICE_CODE;
+#endif // defined(AMD_WMMA_AVAILABLE)
+}
+
 // ggml_is_constant (static in ggml.c) on the tensor that a view points to
 static bool mmq_few_is_constant(const ggml_tensor * src0) {
     const ggml_tensor * x_root = src0->view_src ? src0->view_src : src0;
@@ -796,6 +1143,98 @@ static void launch_mul_mat_q_few_switch_ncg(const ggml_tensor * src0, const ggml
     }
 }

+template <ggml_type type, bool half>
+static void launch_mul_mat_q_few_wmma(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
+        const int4 * src1_q, cudaStream_t stream) {
+    GGML_TENSOR_BINARY_OP_LOCALS;
+
+    const int nunits = ne00 / QK_K;
+
+    const dim3 block_nums((ne01 + MMQ_FEW_NROWS - 1) / MMQ_FEW_NROWS, ne12, ne13);
+    const dim3 block_dims(WARP_SIZE, MMQ_FEW_NWARPS, 1);
+    const ggml_cuda_kernel_launch_params params(block_nums, block_dims, MMQ_FEW_WMMA_NBYTES_SHARED, stream);
+    ggml_cuda_kernel_launch(mul_mat_q_few_wmma<type, half>, params,
+        (const char *) src0->data, src1_q, (float *) dst->data,
+        nunits, (int) ne01, (int) ne1, (int64_t) nb01, (int) (nb1/sizeof(float)),
+        (int) (ne12/ne02), (int64_t) nb02, (int) (nb2/sizeof(float)),
+        (int) (ne13/ne03), (int64_t) nb03, (int) (nb3/sizeof(float)));
+}
+
+template <ggml_type type>
+static void launch_mul_mat_q_few_wmma_switch_ncols(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
+        const int4 * src1_q, cudaStream_t stream) {
+    if (src1->ne[1] <= MMQ_FEW_WMMA_NCOLS/2) {
+        launch_mul_mat_q_few_wmma<type, true>(src0, src1, dst, src1_q, stream);
+    } else {
+        launch_mul_mat_q_few_wmma<type, false>(src0, src1, dst, src1_q, stream);
+    }
+}
+
+// Min. and max. number of src1 columns for RDNA3 and RDNA4, 0 if the type is not supported.
+// Below the min. MMVQ is faster, above the max. MMQ. Q8_0 would need more than 16 columns to beat MMQ.
+static int2 mmq_few_wmma_ncols_range(const ggml_type type, const int cc) {
+    if (GGML_CUDA_CC_IS_RDNA4(cc)) { // tuned on the Radeon RX 9070 XT (gfx1201)
+        switch (type) {
+            case GGML_TYPE_Q4_K:   return make_int2(3, 16);
+            case GGML_TYPE_Q5_K:   return make_int2(3, 16);
+            case GGML_TYPE_Q6_K:   return make_int2(5, 16);
+            case GGML_TYPE_IQ4_NL: return make_int2(6, 16);
+            default:               return make_int2(0,  0);
+        }
+    }
+    if (GGML_CUDA_CC_IS_RDNA3_0(cc)) { // tuned on the Radeon RX 7900 XTX (gfx1100)
+        switch (type) {
+            case GGML_TYPE_Q4_K:   return make_int2(2, 16);
+            case GGML_TYPE_Q5_K:   return make_int2(2, 16);
+            case GGML_TYPE_Q6_K:   return make_int2(5, 16);
+            case GGML_TYPE_IQ4_NL: return make_int2(5, 16);
+            default:               return make_int2(0,  0);
+        }
+    }
+    switch (type) { // tuned on the Radeon 8060S (gfx1151)
+        case GGML_TYPE_Q4_K:   return make_int2(3, 16);
+        case GGML_TYPE_Q5_K:   return make_int2(3,  8);
+        case GGML_TYPE_Q6_K:   return make_int2(6, 16);
+        case GGML_TYPE_IQ4_NL: return make_int2(7, 16);
+        default:               return make_int2(0,  0);
+    }
+}
+
+static bool mmq_few_use_wmma(const int cc) {
+    return GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4(cc);
+}
+
+static void ggml_cuda_mul_mat_q_few_wmma(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
+    GGML_TENSOR_BINARY_OP_LOCALS;
+
+    GGML_ASSERT(ne11 <= MMQ_FEW_WMMA_NCOLS);
+
+    cudaStream_t stream = ctx.stream();
+
+    const int nunits = ne00 / QK_K;
+
+    ggml_cuda_pool_alloc<int4> src1_q(ctx.pool(), int64_t(ne12)*ne13*nunits*MMQ_FEW_WMMA_Q8_1_INT4);
+    quantize_mmq_few_wmma_q8_1_cuda(src1->data, src1->type, src1_q.get(), mmq_few_ds_layout(src0->type), ne10, nb11/nb10, nb12/nb10, nb13/nb10,
+        ne11, ne12, ne13, stream);
+
+    switch (src0->type) {
+        case GGML_TYPE_Q4_K:
+            launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_Q4_K>(src0, src1, dst, src1_q.get(), stream);
+            break;
+        case GGML_TYPE_Q5_K:
+            launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_Q5_K>(src0, src1, dst, src1_q.get(), stream);
+            break;
+        case GGML_TYPE_Q6_K:
+            launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_Q6_K>(src0, src1, dst, src1_q.get(), stream);
+            break;
+        case GGML_TYPE_IQ4_NL:
+            launch_mul_mat_q_few_wmma_switch_ncols<GGML_TYPE_IQ4_NL>(src0, src1, dst, src1_q.get(), stream);
+            break;
+        default:
+            GGML_ABORT("unsupported type for few-row MMQ with WMMA");
+    }
+}
+
 void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst) {
     GGML_TENSOR_BINARY_OP_LOCALS;

@@ -806,6 +1245,12 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
     GGML_ASSERT(nb0  == sizeof(float));
     GGML_ASSERT(ne00 % QK_K == 0);

+    if (mmq_few_use_wmma(ggml_cuda_info().devices[ctx.device].cc)) {
+        GGML_ASSERT(!ids);
+        ggml_cuda_mul_mat_q_few_wmma(ctx, src0, src1, dst);
+        return;
+    }
+
     cudaStream_t stream = ctx.stream();

     const int nunits = ne00 / QK_K;
@@ -875,6 +1320,10 @@ static int ggml_cuda_mmq_few_min_ncols(const ggml_type type, const int cc, const
     return 0;
 #endif // GGML_CUDA_FORCE_CUBLAS

+    if (mmq_few_use_wmma(cc)) {
+        return has_ids || !mmq_few_wmma_supported(type) || ne00 < 8*QK_K ? 0 : mmq_few_wmma_ncols_range(type, cc).x;
+    }
+
     // NVIDIA Ampere to Blackwell
     if (!ampere_mma_available(cc) || cc >= GGML_CUDA_CC_RUBIN) {
         return 0;
@@ -916,6 +1365,14 @@ bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor *
     if (ids && (2*src0->ne[2] + 1)*sizeof(int) > ggml_cuda_info().devices[ggml_cuda_get_device()].smpb) {
         return false;
     }
+    if (mmq_few_use_wmma(cc)) {
+        if (src1->ne[1] > mmq_few_wmma_ncols_range(src0->type, cc).y || src0->ne[0] % QK_K != 0) {
+            return false;
+        }
+        // the decoders read whole 4 byte values from 16 byte aligned units, Q6_K values 2 bytes at a time
+        const int x_alignment = mmq_few_unaligned(src0->type, 1) ? 2 : 16;
+        return ggml_cuda_is_aligned(src0, x_alignment) && ggml_cuda_is_aligned(src1, 16);
+    }
     // the row must hold a whole number of ring stages of the launched kernel
     const int ncg          = ids ? 1 : mmq_few_ncg(ncols);
     const int nmt          = ids ? 1 : mmq_few_nmt(src0->type, ncg, src0->ne[1]);
@@ -928,11 +1385,11 @@ bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor *
     return ggml_cuda_is_aligned(src0, x_alignment) && ggml_cuda_is_aligned(src1, 16);
 }

-#else // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
+#else // !defined(GGML_USE_MUSA)

 void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst) {
     GGML_UNUSED_VARS(ctx, src0, src1, ids, dst);
-    GGML_ABORT("few-row MMQ is not supported on HIP or MUSA");
+    GGML_ABORT("few-row MMQ is not supported on MUSA");
 }

 bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, const int cc) {
@@ -940,4 +1397,4 @@ bool ggml_cuda_should_use_mmq_few(const ggml_tensor * src0, const ggml_tensor *
     return false;
 }

-#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
+#endif // !defined(GGML_USE_MUSA)
diff --git a/ggml/src/ggml-cuda/quantize.cu b/ggml/src/ggml-cuda/quantize.cu
index 5f4d76750..526dabdea 100644
--- a/ggml/src/ggml-cuda/quantize.cu
+++ b/ggml/src/ggml-cuda/quantize.cu
@@ -662,8 +662,8 @@ void quantize_mmq_q8_1_cuda(
     });
 }

-// Stores the 4 values xi of a thread, values [iu, iu + 4) of column g in a unit of 256 values, in the column group yg of the unit, see quantize.cuh for the layout.
-template <mmq_q8_1_ds_layout ds_layout>
+// Stores the 4 values xi of a thread, values [iu, iu + 4) of column g in a unit of 256 values, in the column group yg of the unit, see quantize.cuh for the layouts.
+template <mmq_q8_1_ds_layout ds_layout, bool wmma>
 static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, int4 * yg, const int iu, const int g) {
     // the 32 values of a sub-block are held by 8 threads
     float amax = fmaxf(fmaxf(fabsf(xi.x), fabsf(xi.y)), fmaxf(fabsf(xi.z), fabsf(xi.w)));
@@ -685,6 +685,20 @@ static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, i

     const int s = iu / QK8_1; // sub-block
     const int k = iu % QK8_1; // first value in sub-block
+
+    if constexpr (wmma) {
+        ((int *) yg)[(s*MMQ_FEW_WMMA_NCOLS + g)*8 + k/4] = *(const int *) &q;
+        if (k == 0) {
+            int * ds = (int *) (yg + 8*MMQ_FEW_WMMA_NCOLS*2) + (s*2 + g % 2)*8 + g/2;
+            if constexpr (ds_layout == MMQ_Q8_1_DS_LAYOUT_D4) {
+                *(float *) ds = d;
+            } else {
+                *(half2 *) ds = make_half2(d, sum);
+            }
+        }
+        return;
+    }
+
     ((int *) (yg + (s/2)*WARP_SIZE + 4*g + (k % 16)/4))[2*(s % 2) + k/16] = *(const int *) &q;
     if (k == 0) {
         int4 * ds = yg + 4*WARP_SIZE + 4*(g/2) + s/2;
@@ -696,8 +710,8 @@ static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, i
     }
 }

-// Each thread quantizes 4 values of one column, see quantize.cuh for the layout.
-template <typename src_t, mmq_q8_1_ds_layout ds_layout>
+// Each thread quantizes 4 values of one column, see quantize.cuh for the layouts.
+template <typename src_t, mmq_q8_1_ds_layout ds_layout, bool wmma>
 static __global__ void quantize_mmq_few_q8_1(
         const src_t * x_ptr, void * vy_ptr, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
         const int ne1, const int ne2) {
@@ -715,9 +729,16 @@ static __global__ void quantize_mmq_few_q8_1(
     const int64_t i3 = blockIdx.z / ne2;
     const float4  xi = (int) blockIdx.x < ne1 ? quantize_load4(x + i3*s03 + i2*s02 + blockIdx.x*s01 + i0, 0) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);

+    if constexpr (wmma) {
+        const int m = blockIdx.x % MMQ_FEW_WMMA_NCOLS; // column in group
+        int4 * yg = vy + ((blockIdx.z*(ne00/QK_K) + i0/QK_K)*(gridDim.x/MMQ_FEW_WMMA_NCOLS) + blockIdx.x/MMQ_FEW_WMMA_NCOLS)*MMQ_FEW_WMMA_Q8_1_INT4;
+        quantize_mmq_few_store<ds_layout, true>(xi, yg, i0 % QK_K, m);
+        return;
+    }
+
     const int g = blockIdx.x % MMQ_FEW_NCOLS; // column in group
     int4 * yg = vy + ((blockIdx.z*(ne00/QK_K) + i0/QK_K)*(gridDim.x/MMQ_FEW_NCOLS) + blockIdx.x/MMQ_FEW_NCOLS)*MMQ_FEW_Q8_1_INT4;
-    quantize_mmq_few_store<ds_layout>(xi, yg, i0 % QK_K, g);
+    quantize_mmq_few_store<ds_layout, false>(xi, yg, i0 % QK_K, g);
 }

 // MUL_MAT_ID: block x is the (token, slot) entry, ids are as in mm_ids_helper. Each block finds the pair of its entry in shared memory, block (0, 0) also writes all pairs.
@@ -807,26 +828,26 @@ static __global__ void quantize_mmq_few_id_q8_1(

     const int r = rank(blockIdx.x, e);
     int4 * yg = vy + (int64_t(pair0[e] + r/MMQ_FEW_NCOLS)*(ne00/QK_K) + i0/QK_K)*MMQ_FEW_Q8_1_INT4;
-    quantize_mmq_few_store<ds_layout>(xi, yg, i0 % QK_K, r % MMQ_FEW_NCOLS);
+    quantize_mmq_few_store<ds_layout, false>(xi, yg, i0 % QK_K, r % MMQ_FEW_NCOLS);
 }

-template <typename src_t>
-static void quantize_mmq_few_q8_1_cuda(
+template <bool wmma, typename src_t>
+static void quantize_mmq_few_q8_1_launch(
         const src_t * x, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
         const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
     GGML_ASSERT(ne00 % QK_K == 0);

     const int64_t block_num_y = (ne00 + 4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ);
-    const dim3 num_blocks(GGML_PAD(ne1, MMQ_FEW_NCOLS), block_num_y, ne2*ne3);
+    const dim3 num_blocks(GGML_PAD(ne1, wmma ? MMQ_FEW_WMMA_NCOLS : MMQ_FEW_NCOLS), block_num_y, ne2*ne3);
     const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
     const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, 0, stream);
     switch (ds_layout) {
         case MMQ_Q8_1_DS_LAYOUT_D4:
-            ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4>, launch_params,
+            ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, wmma>, launch_params,
                 x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
             break;
         case MMQ_Q8_1_DS_LAYOUT_DS4:
-            ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4>, launch_params,
+            ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4, wmma>, launch_params,
                 x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
             break;
         default:
@@ -838,7 +859,15 @@ void quantize_mmq_few_q8_1_cuda(
         const void * x, const ggml_type type_src1, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
         const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
     quantize_launch_typed(x, type_src1, [&](auto * x_t) {
-        quantize_mmq_few_q8_1_cuda(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
+        quantize_mmq_few_q8_1_launch<false>(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
+    });
+}
+
+void quantize_mmq_few_wmma_q8_1_cuda(
+        const void * x, const ggml_type type_src1, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
+        const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
+    quantize_launch_typed(x, type_src1, [&](auto * x_t) {
+        quantize_mmq_few_q8_1_launch<true>(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
     });
 }

diff --git a/ggml/src/ggml-cuda/quantize.cuh b/ggml/src/ggml-cuda/quantize.cuh
index c8b39acbf..ac838cc51 100644
--- a/ggml/src/ggml-cuda/quantize.cuh
+++ b/ggml/src/ggml-cuda/quantize.cuh
@@ -42,6 +42,17 @@ void quantize_mmq_few_id_q8_1_cuda(
         const void * x, ggml_type type_src1, const int32_t * ids, void * vy, int * pairs, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s11, int64_t s12,
         int64_t ne11, int64_t n_experts, int64_t n_expert_used, int64_t n_tokens, int64_t si1, int64_t npairs_max, cudaStream_t stream);

+// src1 of the few-row MMQ for RDNA3 WMMA, which multiplies 16 src1 columns and needs the same A and B data in both half-warps.
+// Per channel, 256 values and group of MMQ_FEW_WMMA_NCOLS columns:
+//   qs: int4[8][16][2], [s][m][k] holds the values [16k, 16k + 16) of sub-block s of column m, the A fragments of lanes m and m + 16.
+//   ds: int4[8][2][2], [s][h][k] holds d (D4, float) or (d, sum) (DS4, half2) of sub-block s of the columns 2v + h, v = 4k .. 4k + 3.
+static constexpr int MMQ_FEW_WMMA_NCOLS     = 16;
+static constexpr int MMQ_FEW_WMMA_Q8_1_INT4 = 8*16*2 + 8*2*2;
+
+void quantize_mmq_few_wmma_q8_1_cuda(
+        const void * x, ggml_type type_src1, void * vy, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
+        int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
+
 void quantize_mmq_fp4_cuda(const void *    x,
                              ggml_type       type_src1,
                              const int32_t * ids,