Skip to content

[w2] fattn-mma: spill-free RDNA3 D256 config for ncols2 up to 8 (Qwen4 GQA8) - #163

Merged
rafasmour merged 1 commit into
masterfrom
opt/w2-fa-mma-spillfree-d256
Dec 4, 2026
Merged

rafasmour merged 1 commit into
masterfrom
opt/w2-fa-mma-spillfree-d256

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What

One-line widening in ggml_cuda_fattn_mma_get_config_rdna3 (ggml/src/ggml-cuda/fattn-mma-f16.cuh): the streaming-Q D256 config CONFIG(256,256,64,256,2,32,128,128,64,1,false) now applies for ncols2 2..8 instead of 2..4. The stale comment ("with 8 this config is not faster") is replaced with the measured numbers. D512, Gemma and all other rows are untouched. The D128 ncols2==1 line of survey variant A is intentionally NOT ported (see inkling-kld-config).

Why

flash_attn_ext_f16<256,256,*,8> spilled on RDNA3 with the default config. Qwen4 full-attention layers are exactly D256 GQA8 (16 Q / 2 KV heads), the case Qwen3.6 used to cover.

Spill check (CPU, HIP gfx1100 device asm of the four fattn-mma-f16-instance-f16-f16-ncols1_{1,2,4,8}-ncols2_8.cu, master 928d721 vs this branch)

Flags: flash_attn_ext_f16<256,256,ncols1,8,softcap,V_is_K_view,sparse,...>; fields vgpr / sgpr / private_segment (B) / group_segment (B) / vgpr_spill.

ncols1 variant master branch
8 dense 256 / 107 / 932 / 0 / 357 206 / 107 / 0 / 0 / 0
8 dense, softcap 256 / 107 / 932 / 0 / 357 206 / 107 / 0 / 0 / 0
8 sparse (use_sparse=true) 256 / 107 / 1344 / 0 / 548 214 / 107 / 32 / 0 / 0
4 dense 256 / 107 / 868 / 0 / 299 same (unchanged)
2 dense 256 / 107 / 808 / 0 / 236 same (unchanged)
1 (stub, not an RDNA3 config) 40 / 36 / 16 same
  • Scratch goes to 0 for the full 8x8 tile; metadata of every ncols1 1/2/4 symbol (all D, all variants) is identical to master. The only code diff in those objects is a __LINE__-derived constant in a trap/assert path (0x812 vs 0x814) caused by the comment growing by 2 lines.
  • ncols1 2 and 4 with ncols2 8 (ncols 16/32, i.e. decode-ish batches such as MTP verify) still spill (808/868 B) because the streaming config exists only for the 64-column tile. Not changed here (the task says leave other rows untouched); a possible follow-up is a 16/32-column streaming config for D256 ncols2 8.
  • fattn.cu.o builds on the branch (host-side config queries OK); no new warnings.

Sparse 8x8 note (qwen4-qsa-sparse-fa)

The config lookup depends only on (DKQ, DV, ncols=ncols1*ncols2, ncols2), not on use_sparse. The sparse 8x8 D256 kernel therefore uses this same (256,256,64,ncols2=8) config, and its spill also drops (1344 B scratch / 548 spilled VGPRs -> 32 B / 0 VGPR spill). No code change needed for that PR.

Expected gain (ESTIMATE from prototype variant A, not yet measured on this tree)

D256 GQA8 FA kernel 11437 -> 8559 us (-25%); pp512 at d=32768 +10.2% on Qwen3.6 (same shape). Qwen4 prompt at depth expected similar while dense FA is used. Short context was neutral in the prototype.

Env switch

None: compile-time config table. Isolated tonight by B0 vs BA with ALL_OFF (or the optional spill-only build BS).

Files

ggml/src/ggml-cuda/fattn-mma-f16.cuh

Risks

  • Prototype KLD on Qwen3.6 was 0.0046 for a binary that also had direct-KV, GDN WMMA and stream-k, so attribution is unknown; tonight's KLD gate decides.
  • nbatch_fa 32 means more online-softmax rescales in half-precision VKQ. If KLD fails try nbatch_fa 64 for the 8-column case or F32 VKQ accumulation before dropping.
  • No short-context regression expected; verify it.

Rules followed: no GPU used, no production/homelab changes, nothing touched in ggml-org/llama.cpp, build only under the build lock (nice 19, -j6) and build trees deleted.

GPU validation tonight (NOT YET RUN)

R=/home/tom/docker/projects/llama.cpp-RDNA3; K=$R/build-bins/kern; TB=$R/build-bins/tools/bench; cd $K
B0=$R/build-bins/w2/base-build/bin; BA=$R/build-bins/w2/all-build/bin
# (P) preamble: Q4, Q4A, Q38, Q38A, G, GA, OFFE, OFF as in the wave plan

# Eval
./gpusmall.sh ./test2.sh w2-spill-eval $BA - FLASH_ATTN_EXT 'hsk=256,hsv=256'
# Kernel perf (expect about -25% on BA)
./gpusmall.sh ./perf2.sh w2-spill-b0 $B0 - FLASH_ATTN_EXT 'hsk=256,hsv=256,nh=2,nr23=\[8,1\].*nb=512'
GPURUN_OPTS="$OFF" ./gpusmall.sh ./perf2.sh w2-spill-ba $BA - FLASH_ATTN_EXT 'hsk=256,hsv=256,nh=2,nr23=\[8,1\].*nb=512'
# Prompt bench, Qwen4
./gpublock.sh ./lb.sh q4-pp-b0 $B0 $Q4 $Q4A -p 512 -n 0 -d 0,32768,98304 -r 3
GPURUN_OPTS="$OFF" ./gpublock.sh ./lb.sh q4-pp-ba $BA $Q4 $Q4A -p 512 -n 0 -d 0,32768,98304 -r 3
# No-regression with the same pairs: $Q38 $Q38A -p 512 -n 0 -d 0,32768 ; $G $GA -p 512 -n 0 -d 0,32768
# KLD gate
./gpublock.sh ./kl.sh q4-spill-base $B0 $Q4 base $Q4A -c 16384 --chunks 3
./gpublock.sh env KL_ENV="$OFFE" ./kl.sh q4-spill $BA $Q4 test $Q4A -c 16384 --chunks 3
# Pass: Mean KLD <= 0.001 and Same top p >= 99%. If it fails: build BS (master + this branch) and repeat with $BS.

Assisted-by: Claude Sonnet 5.5

…on RDNA3

flash_attn_ext_f16<256,256,8,8> (D256, GQA8, full 64-column tile) spilled with the
default RDNA config: 256 VGPR, 932 B scratch (1344 B for the sparse variant).
Widening the existing streaming-Q config condition from ncols2 2..4 to 2..8
gives 206 VGPR and no scratch. Prototype measurement (variant A): D256 GQA8
kernel 11437 -> 8559 us, pp512@d32768 +10.2% on Qwen3.6 (same shape as Qwen4
full attention: 16 Q / 2 KV heads).

Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit bc23739 into master Dec 4, 2026
4 of 15 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