Repository navigation
[w2] mmvq: software-pipeline the IQ mat-vec kbx loop on RDNA3 (IQ2_S/IQ3_S/IQ3_XXS, opt-in IQ4_XS) - #176
Merged
Conversation
… 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
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_xsinto a load stage and an arithmetic stage (mmvq_iq_load_*/mmvq_iq_dot_*invecdotq.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 booliq_pipeonmul_mat_vec_qselects 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_b32on 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):
build-bins/w2/iq-mmvq-latency/): asched_barrierthat 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
if constexpr (pipelined)block before the existing loop), one template param, and the host launch plumbing.Checks done (CPU only, no GPU used)
mmvq.cu.ofor gfx1100: OK, no warnings.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:
(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'):The same pair for
$Q4 $Q4A -p 0 -n 128 -r 5, plus a third arm withGPURUN_OPTS='-e GGML_CUDA_IQ4_XS_MMVQ_PIPELINE=1'(IQ4_XS opt-in).Per-kernel GB/s (trace):
KLD (exactly 0):
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/homelabwas touched; nothing was pushed to or opened against ggml-org/llama.cpp; this PR is not merged.Assisted-by: Claude Sonnet 5.5