From 9c67edb5fc54dca85cc31663daec562a947a499d Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:16:28 +0000 Subject: [PATCH 1/2] mmq: register prefetch of the next weight tile for Q4_0 and IQ4_XS on RDNA3 Port the Q4_K/Q8_0 prefetch schedule to the Q4_0 and IQ4_XS int8 WMMA MMQ: raw quants and scales of tile k+1 are read into registers before the WMMA of tile k and converted/stored to LDS afterwards. Same math, only the load schedule differs. IQ4_XS also covers the fused gate/up expert tile. Compile out with -DGGML_CUDA_MMQ_NO_EXTRA_PREFETCH. Adds eval test cases. Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/mmq-load-tiles.cuh | 165 ++++++++++++++++++++++++++ ggml/src/ggml-cuda/mmq.cuh | 73 ++++++++++++ tests/test-backend-ops.cpp | 12 ++ 3 files changed, 250 insertions(+) diff --git a/ggml/src/ggml-cuda/mmq-load-tiles.cuh b/ggml/src/ggml-cuda/mmq-load-tiles.cuh index d235cb1a9686..d1dceeec195e 100644 --- a/ggml/src/ggml-cuda/mmq-load-tiles.cuh +++ b/ggml/src/ggml-cuda/mmq-load-tiles.cuh @@ -1010,6 +1010,171 @@ static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_q8_0( x_df[i*sram_stride + kbxd] = d_cache[d_idx++]; } } + +// Prefetch/store split of the Q4_0 and IQ4_XS tile loaders (same math as ggml_cuda_mmq_load_tiles_q4_0 and _iq4_xs, only the +// schedule differs): the raw quants and scales of the next K tile are read into registers before the WMMA of the current tile +// and only converted and written to shared memory afterwards. qs_cache holds one int per row of the thread's row set, +// d_cache the (already combined) scale of the 4 scale rows of the thread. +template +static __device__ __forceinline__ void ggml_cuda_mmq_prefetch_tiles_q4_0( + const char * __restrict__ x, const int kbx0, const int i_max, const int stride, + int (&qs_cache)[NQ], float (&d_cache)[4]) { + constexpr int warp_size = ggml_cuda_get_physical_warp_size(); + constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8); + constexpr int nwarps = nthreads / warp_size; + constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8); + static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected Q4_0 MMQ configuration"); + static_assert(NQ == I/nwarps, "qs_cache has the wrong size"); + + constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_0); + static_assert(threads_per_row == warp_size, "one warp must cover one row"); + const int kbx = threadIdx.x / QI4_0; + const int kqsx = threadIdx.x % QI4_0; + +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps) { + int i = i0 + threadIdx.y; + if constexpr (fallback) { + i = min(i, i_max); + } + + const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbx; + qs_cache[i0/nwarps] = get_int_b2(bxi->qs, kqsx); + } + + constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_0; + constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row; + static_assert(I/(nwarps*rows_per_warp) <= 4, "d_cache too small"); + const int kbxd = threadIdx.x % blocks_per_tile_x_row; + int d_idx = 0; +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) { + int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row; + if constexpr (fallback) { + i = min(i, i_max); + } + + const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbxd; + d_cache[d_idx++] = bxi->d; + } +} + +template +static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_q4_0( + int * __restrict__ x_tile, const int (&qs_cache)[NQ], const float (&d_cache)[4]) { + constexpr int warp_size = ggml_cuda_get_physical_warp_size(); + constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8); + constexpr int nwarps = nthreads / warp_size; + constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8); + constexpr int sram_stride = ggml_cuda_mmq_get_sram_stride(type, J, fallback, GGML_PREC_Q8); + static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected Q4_0 MMQ configuration"); + static_assert(NQ == I/nwarps, "qs_cache has the wrong size"); + + int * x_qs = x_tile; + float * x_df = (float *) (x_tile + 2*MMQ_TILE_NE_K); + + const int kbx = threadIdx.x / QI4_0; + const int kqsx = threadIdx.x % QI4_0; + +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps) { + const int i = i0 + threadIdx.y; + const int qs0 = qs_cache[i0/nwarps]; + x_qs[i*sram_stride + kbx*(2*QI4_0) + kqsx + 0] = __vsub4((qs0 >> 0) & 0x0F0F0F0F, 0x08080808); + x_qs[i*sram_stride + kbx*(2*QI4_0) + kqsx + QI4_0] = __vsub4((qs0 >> 4) & 0x0F0F0F0F, 0x08080808); + } + + constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_0; + constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row; + const int kbxd = threadIdx.x % blocks_per_tile_x_row; + int d_idx = 0; +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) { + const int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row; + x_df[i*sram_stride + kbxd] = d_cache[d_idx++]; + } +} + +template +static __device__ __forceinline__ void ggml_cuda_mmq_prefetch_tiles_iq4_xs( + const char * __restrict__ x, const char * __restrict__ x_gate, const int kbx0, const int i_max, const int stride, + int (&qs_cache)[NQ], float (&d_cache)[4]) { + constexpr int warp_size = ggml_cuda_get_physical_warp_size(); + constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8); + constexpr int nwarps = nthreads / warp_size; + constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8); + static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected IQ4_XS MMQ configuration"); + static_assert(NQ == I/nwarps, "qs_cache has the wrong size"); + + constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_XS); + static_assert(threads_per_row == warp_size, "one warp must cover one row"); + const int kqsx = threadIdx.x; + +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps) { + int i = i0 + threadIdx.y; + if constexpr (fallback) { + i = min(i, i_max); + } + + const block_iq4_xs * bxi = ggml_cuda_mmq_x_row(x, x_gate, kbx0, i, stride); + qs_cache[i0/nwarps] = get_int_b4(bxi->qs, kqsx); + } + + constexpr int rows_per_warp = warp_size / 8; + static_assert(I/(nwarps*rows_per_warp) <= 4, "d_cache too small"); + int d_idx = 0; +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) { + int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / (MMQ_TILE_NE_K/4); + if constexpr (fallback) { + i = min(i, i_max); + } + + const block_iq4_xs * bxi = ggml_cuda_mmq_x_row(x, x_gate, kbx0, i, stride); + + const float d = __half2float(bxi->d); + + const int ls = ((bxi->scales_l[(threadIdx.x % 8)/2] >> (4*(threadIdx.x % 2))) & 0x0F) + | (((bxi->scales_h >> (2*(threadIdx.x % 8))) & 0x03) << 4); + + d_cache[d_idx++] = d * (ls - 32); + } +} + +template +static __device__ __forceinline__ void ggml_cuda_mmq_store_tiles_iq4_xs( + int * __restrict__ x_tile, const int (&qs_cache)[NQ], const float (&d_cache)[4]) { + constexpr int warp_size = ggml_cuda_get_physical_warp_size(); + constexpr int nthreads = ggml_cuda_mmq_get_nthreads(type, J, fallback, GGML_PREC_Q8); + constexpr int nwarps = nthreads / warp_size; + constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, GGML_PREC_Q8); + constexpr int sram_stride = ggml_cuda_mmq_get_sram_stride(type, J, fallback, GGML_PREC_Q8); + static_assert(warp_size == 32 && nthreads == 128 && I == 64, "unexpected IQ4_XS MMQ configuration"); + static_assert(NQ == I/nwarps, "qs_cache has the wrong size"); + + int * x_qs = x_tile; + float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2); + + const int kqsx = threadIdx.x; + const int k0 = 8 * (kqsx / 4) + kqsx % 4; + +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps) { + const int i = i0 + threadIdx.y; + const int2 v = get_int_from_table_16(qs_cache[i0/nwarps], kvalues_iq4nl); + x_qs[i*sram_stride + k0 + 0] = v.x; + x_qs[i*sram_stride + k0 + 4] = v.y; + } + + constexpr int rows_per_warp = warp_size / 8; + int d_idx = 0; +#pragma unroll + for (int i0 = 0; i0 < I; i0 += nwarps * rows_per_warp) { + const int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / (MMQ_TILE_NE_K/4); + x_df[i*sram_stride + threadIdx.x % 8] = d_cache[d_idx++]; + } +} #endif // defined(RDNA3) template static __device__ __forceinline__ void ggml_cuda_mmq_load_tiles_q5_K( diff --git a/ggml/src/ggml-cuda/mmq.cuh b/ggml/src/ggml-cuda/mmq.cuh index e2823e5ed11c..02b51e05a3eb 100644 --- a/ggml/src/ggml-cuda/mmq.cuh +++ b/ggml/src/ggml-cuda/mmq.cuh @@ -458,6 +458,14 @@ static __device__ __forceinline__ const block_t * ggml_cuda_mmq_x_row( } } +// Register prefetch of the next weight tile for Q4_0 and IQ4_XS on RDNA3 (bit-identical, only the load schedule differs). +// Compile with -DGGML_CUDA_MMQ_NO_EXTRA_PREFETCH to switch it off for an A/B build. +#if defined(GGML_CUDA_MMQ_NO_EXTRA_PREFETCH) +static constexpr bool ggml_cuda_mmq_extra_prefetch = false; +#else +static constexpr bool ggml_cuda_mmq_extra_prefetch = true; +#endif // defined(GGML_CUDA_MMQ_NO_EXTRA_PREFETCH) + #include "mmq-load-tiles.cuh" #include "mmq-vec-dot.cuh" @@ -1084,6 +1092,71 @@ static __device__ __forceinline__ void mul_mat_q_process_tile( ggml_cuda_mmq_store_tiles_q8_0(tile_x, qs_cache, d_cache); } + __syncthreads(); + } + } else if constexpr (ggml_cuda_mmq_extra_prefetch && prec_src1 == GGML_PREC_Q8 && I == 64 && + (type == GGML_TYPE_Q4_0 || type == GGML_TYPE_IQ4_XS) && + ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) == 128) { + // Same schedule as above for Q4_0 and IQ4_XS (also the fused gate/up IQ4_XS tile of the expert matmuls). + constexpr int qs_cache_size = I/nwarps; + + load_tiles(x, x_gate, tile_x, offset_x + kb0_start, tile_x_i_max, stride_row_x); + + for (int kb0 = kb0_start; kb0 < kb0_stop; kb0 += blocks_per_iter) { + { + const int * by0 = y + ncols_y * (kb0 * qk / ne_block) * sz; +#pragma unroll + for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) { + int l = l0 + threadIdx.y*warp_size + threadIdx.x; + + tile_y[l] = by0[l]; + } + } + + __syncthreads(); + + vec_dot(tile_x, tile_y, sum, 0); + + __syncthreads(); + + { + const int * by0 = y + ncols_y * ((kb0 * qk / ne_block) * sz + sz); +#pragma unroll + for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) { + int l = l0 + threadIdx.y*warp_size + threadIdx.x; + + tile_y[l] = by0[l]; + } + } + + __syncthreads(); + + int qs_cache[qs_cache_size]; + float d_cache[4]; + const int kb0_next = kb0 + blocks_per_iter; + const bool have_next = kb0_next < kb0_stop; + if (have_next) { + if constexpr (type == GGML_TYPE_Q4_0) { + ggml_cuda_mmq_prefetch_tiles_q4_0( + x, offset_x + kb0_next, tile_x_i_max, stride_row_x, qs_cache, d_cache); + } else { + ggml_cuda_mmq_prefetch_tiles_iq4_xs( + x, x_gate, offset_x + kb0_next, tile_x_i_max, stride_row_x, qs_cache, d_cache); + } + } + + vec_dot(tile_x, tile_y, sum, MMQ_TILE_NE_K); + + __syncthreads(); + + if (have_next) { + if constexpr (type == GGML_TYPE_Q4_0) { + ggml_cuda_mmq_store_tiles_q4_0(tile_x, qs_cache, d_cache); + } else { + ggml_cuda_mmq_store_tiles_iq4_xs(tile_x, qs_cache, d_cache); + } + } + __syncthreads(); } } else diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..426fb7cec923 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -17147,6 +17147,18 @@ static std::vector> make_test_cases_eval() { } } + // [w2 mmq-int8-efficiency] register prefetch of the next weight tile in the RDNA3 MMQ for Q4_0 and IQ4_XS: several K iterations + // (K = 1024..4096), the 64/96/128 wide tiles and unaligned row counts (fallback tile), plus the expert matmuls. + for (ggml_type type_a : {GGML_TYPE_Q4_0, GGML_TYPE_IQ4_XS}) { + for (int n : {16, 33, 64, 96, 128, 512}) { + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 4096, n, 4096, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 1000, n, 2048, {1, 1}, {1, 1})); + test_cases.emplace_back(new test_mul_mat(type_a, GGML_TYPE_F32, 128, n, 1024, {1, 1}, {1, 1})); + } + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 32, 8, false, 512, 256, 2048)); + test_cases.emplace_back(new test_mul_mat_id(type_a, GGML_TYPE_F32, 16, 4, true, 1000, 129, 1024)); + } + return test_cases; } #ifdef _MSC_VER From 2489ab0aa9d5350ace784f1f1171a3496a00d3a7 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:41:55 +0000 Subject: [PATCH 2/2] mmq: keep the Q4_0/IQ4_XS prefetch off the 16-column tile At J=16 the prefetch variant raised Q4_0 from 182 to 253 VGPRs and made IQ4_XS spill (256 VGPR + 16 B scratch). Restrict it to J >= 32. Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/mmq.cuh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-cuda/mmq.cuh b/ggml/src/ggml-cuda/mmq.cuh index 02b51e05a3eb..709da3ddb0ba 100644 --- a/ggml/src/ggml-cuda/mmq.cuh +++ b/ggml/src/ggml-cuda/mmq.cuh @@ -1094,10 +1094,11 @@ static __device__ __forceinline__ void mul_mat_q_process_tile( __syncthreads(); } - } else if constexpr (ggml_cuda_mmq_extra_prefetch && prec_src1 == GGML_PREC_Q8 && I == 64 && + } else if constexpr (ggml_cuda_mmq_extra_prefetch && prec_src1 == GGML_PREC_Q8 && I == 64 && J >= 32 && (type == GGML_TYPE_Q4_0 || type == GGML_TYPE_IQ4_XS) && ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) == 128) { // Same schedule as above for Q4_0 and IQ4_XS (also the fused gate/up IQ4_XS tile of the expert matmuls). + // Not for J = 16: the tile is so small that the prefetch only inflates the VGPR count there (Q4_0 182 -> 253, IQ4_XS spills). constexpr int qs_cache_size = I/nwarps; load_tiles(x, x_gate, tile_x, offset_x + kb0_start, tile_x_i_max, stride_row_x);