Skip to content

[w2] mmvq: software-pipeline the IQ mat-vec kbx loop on RDNA3 (IQ2_S/IQ3_S/IQ3_XXS, opt-in IQ4_XS) - #176

Merged
rafasmour merged 1 commit into
masterfrom
opt/w2-iq-mmvq-latency
Dec 4, 2026
Merged

rafasmour merged 1 commit into
masterfrom
opt/w2-iq-mmvq-latency

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What

On RDNA3 (RX 7900 XT) the 1-column mat-vec of IQ2_S / IQ3_S / IQ3_XXS reaches 550-660 of ~760 GB/s. The ISA shows each loop trip waiting on DRAM for the weight block and only then issuing the dependent grid-table gather (iq2s_grid / iq3xxs_grid / iq3s_grid), with nothing overlapped (the L2 prefetch in this kernel is GB10 only).

This PR splits vec_dot_iq2_s / iq3_xxs / iq3_s / iq4_xs into a load stage and an arithmetic stage (mmvq_iq_load_* / mmvq_iq_dot_* in vecdotq.cuh) and software-pipelines the kbx loop with a register double buffer: the loads of trip n+1 are issued together with the grid gathers of trip n; the last trip is peeled. Same operations in the same order per thread, so the output is bit-identical (the fp32 accumulation order is unchanged).

Applies to ncols_dst == 1, !small_k, one row per block (the RDNA3_0 table for these types): plain, MUL_MAT_ID (expert ids) and the fused gate/up kernels, including the Q8_0 shared-expert variant. A new template bool iq_pipe on mul_mat_vec_q selects it, so the old kernel is still compiled and used when the switch is off.

Expected gain (estimate, not measured)

Scout estimate: Gemma-4-31B UD-IQ3_XXS tg +5-10 % if the gather latency hides behind the DRAM wait. Model: per-trip time goes from L_dram + L_gather + dp4a to about L_dram. Judge in-model only; isolated test-backend-ops is misleading because of the Infinity Cache.

Env switches

  • GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1 : kill switch, selects the old kernel for all types (default: pipeline on, RDNA3_0 only).
  • GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1 : opt-in for IQ4_XS (Qwen4 experts). Default off, see below. Overridden by the kill switch.

IQ4_XS finding (step 3)

Dumped the ncols_dst==1 ISA (type id 23): the 16-entry table is v_perm_b32 on SGPR constants, there is no dependent memory gather. It is load -> wait -> VALU, so the only thing to gain is overlapping the next trip's DRAM loads with the dp4a. The split is implemented but kept opt-in until measured on Qwen4.

ISA / metadata (gfx1100, device asm before/after, mmvq.cu)

No scratch anywhere (private_segment_fixed_size 0), LDS 0. All VGPR counts stay <= 96 (full 16 waves/SIMD, same as before):

kernel (1 col) VGPR old VGPR pipelined VGPR gate/up old pipelined
IQ3_XXS 37 50 50 89
IQ3_S 40 55 55 95
IQ2_S 43 56 55 92
IQ4_XS 43 56 50 89
  • Kill-switch kernels are instruction-for-instruction the same size as master (IQ3_XXS 286 -> 286, IQ3_S 276 -> 276, IQ2_S 252 -> 252, IQ4_XS 225 -> 225).
  • Pipelined kernel: the steady-state loop issues the 8 gathers of the current trip and the 6 loads of the next trip back to back, then waits only on the gathers (the DRAM wait of trip n+1 is now behind the dp4a of trip n). Static size grows (peeled last trip): IQ3_XXS 286 -> 468 instr, GLU 1022 -> 1656.
  • Rejected variants (ISA dumps kept in build-bins/w2/iq-mmvq-latency/): a sched_barrier that forces the next loads first, or an unrolled ping-pong of two buffers, give 84-183 VGPRs (occupancy loss on the fused kernel) and the in-order vmcnt makes the gather wait cover the next loads anyway.

Files

ggml/src/ggml-cuda/mmvq.cu (kbx loop body, trait, launch plumbing, host switch), ggml/src/ggml-cuda/vecdotq.cuh (split load/dot functions, HIP only), tests/test-backend-ops.cpp (own block: IQ2_S/IQ3_S/IQ3_XXS/IQ4_XS, K = 1..21 blocks so every trip count and tail of the loop is hit, plain, MUL_MAT_ID and fused MUL_MAT_ID).

Risks

  • Static VGPR growth (<= 96, no occupancy loss on paper) and a longer loop prologue/epilogue for short rows (K = 1-2 blocks do one trip, no gain).
  • The gain depends on the machine scheduler keeping the gathers ahead of the next loads; checked in the ISA, may move with compiler versions.
  • mmvq.cu conflicts with q6k-lane-waste / gemma-iq-verify: this touches only the kbx loop body (new if constexpr (pipelined) block before the existing loop), one template param, and the host launch plumbing.
  • Follow-up (not in this PR): persistent grid with the IQ table in LDS (2b).

Checks done (CPU only, no GPU used)

  • HIP compile of mmvq.cu.o for gfx1100: OK, no warnings.
  • COMMON-CPU build of test-backend-ops; the new MUL_MAT / MUL_MAT_ID / MUL_MAT_ID_FUSION cases run and pass on the CPU backend.

GPU validation tonight (NOT YET RUN)

Use the preamble (P) (R, K, BA, G, GA, Q4, Q4A ...). Eval:

./gpusmall.sh ./test2.sh w2-iqp $BA - MUL_MAT 'type_a=(iq2_s|iq3_s|iq3_xxs|iq4_xs),.*n=1,'
./gpusmall.sh ./test2.sh w2-iqp-id $BA - MUL_MAT_ID 'type_a=(iq3_xxs|iq4_xs)'

(for IQ4_XS add GPURUN_OPTS='-e GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1' to cover the opt-in path.)
In-model tg, interleave on and off (off = GPURUN_OPTS='-e GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1'):

./gpublock.sh ./lb.sh g-iqp-on $BA $G $GA -p 0 -n 128 -d 0,32768 -r 5
GPURUN_OPTS='-e GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1' ./gpublock.sh ./lb.sh g-iqp-off $BA $G $GA -p 0 -n 128 -d 0,32768 -r 5

The same pair for $Q4 $Q4A -p 0 -n 128 -r 5, plus a third arm with GPURUN_OPTS='-e GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1' (IQ4_XS opt-in).
Per-kernel GB/s (trace):

./gpublock.sh ./tr.sh g-iqp-on $BA $G 32 -- $GA -p 0 -n 32 -r 1
GPURUN_OPTS='-e GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1' ./gpublock.sh ./tr.sh g-iqp-off $BA $G 32 -- $GA -p 0 -n 32 -r 1

KLD (exactly 0):

./gpublock.sh env KL_ENV=GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1 ./kl.sh g-iqp-off $BA $G base $GA
./gpublock.sh ./kl.sh g-iqp-on $BA $G test $GA

Keep the switch only if tg gain exceeds the run-to-run spread (>= 5 interleaved reps) and KLD is exactly 0.

Rules followed

No GPU was used (no .gpu.lock, no ROCm device run); nothing in production or /home/tom/docker/homelab was touched; nothing was pushed to or opened against ggml-org/llama.cpp; this PR is not merged.

Assisted-by: Claude Sonnet 5.5

… IQ4_XS) on RDNA3

On RDNA3 (RX 7900 XT) the 1-column mat-vec of the grid IQ types waits on DRAM for
the weight block of each loop trip and only then issues the dependent grid-table
gather, with no overlap (nothing prefetches on this arch, the L2 prefetch is
GB10 only). Split vec_dot_iq{2_s,3_xxs,3_s,4_xs} into a load stage and an
arithmetic stage and issue the loads of trip n+1 together with the gathers of
trip n (register double buffer, last trip peeled). Same operations in the same
order per thread, so the result is bit-identical.

- plain, MUL_MAT_ID (ids) and fused gate/up kernels, ncols_dst == 1, !small_k,
  rows_per_block == 1 (RDNA3_0 table), via a new template bool on mul_mat_vec_q
- kill switch GGML_CUDA_NO_IQ_MMVQ_PIPELINE=1 (host side, selects the old kernel)
- IQ4_XS has no table gather (v_perm on registers): opt-in with
  GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1 until measured
- test-backend-ops: eval cases for every trip count / tail of the loop

Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit f694165 into master Dec 4, 2026
8 of 27 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant