[FMHA] gfx950: route dense short seq_len (<256) to generic fallback
Summary
On gfx950 (MI350X) the DUALWAVE_SWP software-pipelined FlashAttention forward
kernel pays a fixed pipeline/prologue cost that dominates very short dense
sequences. The existing generic M128/M256 fallback is substantially faster there.
This PR adds a narrow dense dispatch gate so short dense shapes use the faster
provider, while every other regime is untouched.
This builds on the DUALWAVE_SWP forward kernel from #683.
Change
Add _DUALWAVE_MIN_DENSE_SEQ = 256 in kernels/flash_attn_generic.py:
- Dense
seq_len < 256→ generic M128/M256 fallback (faster here). - Dense
seq_len >= 256→DUALWAVE_SWP(unchanged; it wins decisively). - Packed/varlen (
cu_seqlens) → alwaysDUALWAVE_SWP(the generic fallback has nocu_seqlenspath). FLYDSL_DISABLE_DUALWAVE_SWP=1→ still forces generic everywhere (unchanged).
A dense short-seq call is routed to the generic fallback only when it carries
no DUALWAVE_SWP-only launch params. The generic launcher signature is
(Q, K, V, O, batch_size, seq_len, stream) and cannot represent stride_kv_n,
stride_q_n, head_dim_runtime, debug_counts, or workspace. The guard:
- checks keyword presence (so an explicit
debug_counts=Nonestill stays onDUALWAVE_SWP), and - uses a
len(args) > 7positional threshold — the generic launcher's 7th positional is the sharedstream, whereas dualwave's 7th isstride_kv_n, so a validexe(Q,K,V,O,B,S,stream)correctly reaches the generic fallback.
Also refreshes stale seq_len >= 384 dispatch comments (the runtime floor was
already 1). kernels/flash_attn_gfx950.py is unchanged.
Measurements (MI350X, gfx950)
Forced-provider A/B (median of repeats), generic vs DUALWAVE_SWP:
| seq_len | generic vs dualwave | routing decision |
|---|---|---|
| 128 | ~1.4–1.8× faster (mean ~1.54×) | → generic |
| 256 | ~tie (mixed, within noise) | → dualwave (unchanged) |
| 384 | ~0.86× (dualwave faster) | → dualwave |
| 512 | ~0.83× | → dualwave |
| 4096 | ~0.57× | → dualwave |
| 8192 (GQA) | ~0.55× | → dualwave |
End-to-end, gate on vs off (auto-dispatch), per seq_len=128 bucket:
| shape | dtype | mode | speedup |
|---|---|---|---|
| B=1 S=128 H=64 | bf16 | causal / nocausal | 1.37× / 1.48× |
| B=8 S=128 H=64 | bf16 | causal / nocausal | 1.60× / 1.76× |
| B=1 S=128 H=64 | fp16 | causal / nocausal | 1.46× / 1.45× |
| B=8 S=128 H=64 | fp16 | causal / nocausal | 1.63× / 1.73× |
vs aiter on seq_len=128 (informational): before this change FlyDSL was ~62–79%
of aiter_ck on this bucket; after, B=8 beats aiter_ck (116–127%), B=1 causal is
near parity (~99%), and B=1 nocausal trails less (~84.5%). Mid/long buckets
already beat aiter and are unchanged.
Correctness / coverage
Validated with tests/kernels/test_flash_attn_fwd.py on MI350X (gfx950):
- Full
DEFAULT_CONFIGSsweep (bf16/fp16 × causal/nocausal): All tests PASSED, 0 FAIL / 0 ERROR (gatemax_err < 1e-2ANDmin_cos > 0.99unchanged). - Packed-varlen (
VARLEN_CONFIGS): 16/16 PASS. - No required bucket regresses beyond measurement noise.
- Invariants preserved: varlen stays on
DUALWAVE_SWP;FLYDSL_DISABLE_DUALWAVE_SWP=1still forces generic;seq_len >= 256unchanged;gfx942fallback and gfx950-only intrinsics untouched (flash_attn_gfx950.pyhas no diff).
Reproduction
GPU=<idle gfx950 id>
# correctness + perf (default sweep):
HIP_VISIBLE_DEVICES=$GPU python3 tests/kernels/test_flash_attn_fwd.py --warmup 10 --iters 20
# aiter comparison:
HIP_VISIBLE_DEVICES=$GPU python3 tests/kernels/test_flash_attn_fwd.py --compare --warmup 10 --iters 100
# the short-seq win:
HIP_VISIBLE_DEVICES=$GPU python3 tests/kernels/test_flash_attn_fwd.py --batch 8 --seq_len 128 --num_heads 64 --head_dim 128 --dtype bf16 --causal --warmup 10 --iters 50
# routing-invariant check (forced generic):
FLYDSL_DISABLE_DUALWAVE_SWP=1 HIP_VISIBLE_DEVICES=$GPU python3 tests/kernels/test_flash_attn_fwd.py --warmup 10 --iters 20
Notes
- Measured only on MI350X (gfx950); MI355X is a planned follow-up.
S=256is intentionally left onDUALWAVE_SWP(no reliable generic win there).- A
gfx950kernel-body VMEM-prefetch/waitcnt experiment was tried and rejected (relaxing the hand-scheduledvmcntwaits breaks the LDS double-buffer ordering); this PR is dispatch-only by design.
Updated this PR on top of current upstream/main and resolved the flash_attn_generic.py conflict.
One extra change folded into the conflict resolution is intentional: the dispatcher now rejects optional launch parameters passed positionally and requires them to be keyword arguments (stream=..., stride_kv_n=..., stride_q_n=..., head_dim_runtime=..., debug_counts=..., workspace=...).
Reason: after this PR, dense short sequences can route to the generic fallback while longer/varlen/dualwave-only calls route to DUALWAVE_SWP. The generic launcher historically treats the 7th positional argument as stream, but the DUALWAVE_SWP wrapper treats the 7th positional argument as stride_kv_n and has stream as keyword-only. Allowing that positional slot would make the same call shape-dependent, e.g. exe(Q, K, V, O, B, S, x) could mean stream=x for short dense shapes and stride_kv_n=x for dualwave shapes.
To avoid that potential bug, the public dispatch boundary now keeps only the common required arguments positional (Q, K, V, O, batch_size, seq_len) and makes all optional launch controls keyword-only. I also applied the same guard to .compile(...) for consistency.
Local checks run:
git diff --check upstream/main...HEADpython3 -m py_compile kernels/flash_attn_generic.py
@coderfeli @yanguahe — this is ready for review when you have a moment. @yanguahe since it sits right on top of the #683 DUALWAVE_SWP path, your read on the routing call would be especially valuable.
TL;DR: replaces the flat seq_len < 256 → generic dense gate with a batch-aware one — route to DUALWAVE_SWP iff seq_len ≥ 256, or seq_len ≥ 192 when batch ≥ 8. It's a perf policy only (both kernels compute any seq_len ≥ 1; no numerics change). Rationale: the pipeline's fixed prologue/epilogue cost amortizes over total work (batch * seq_len), not seq_len alone, so the crossover drops at high batch — mirroring the existing batch*seq*num_heads M128/M256 selector. The flat gate was sending B=8, S=192 shapes to the generic kernel where DUALWAVE_SWP is faster.
Verification: full dense shape-map on MI350X (B∈{1,8} × S∈{128,192,256,384,512} × {MHA,GQA} × {bf16,fp16} × {causal,non-causal}, 160 runs) — 160/160 PASS. The re-routed B=8/S=192 GQA cells are +5–8%; MHA there is neutral (±2% noise); every other shape keeps its prior routing. Reproducible via scripts/verify_dense_routing.sh + scripts/analyze_dense_routing.py; threshold also covered by a no-GPU unit test (tests/unit/test_flash_attn_dense_routing.py).
Specific things I'd value your eyes on:
- The threshold itself — is
S≥192 @ B≥8the right shape for the batch term, or would you prefer a singlebatch*seqexpression consistent with the M128/M256 selector? - The restored fail-loud guard on a 7th positional arg (the generic vs DUALWAVE_SWP launchers differ there —
streamvsstride_kv_n) — reasonable, or over-cautious? - Whether the two repro scripts belong in
scripts/or should be dropped from the PR. - The pre-existing
S=256boundary is itself slightly conservative (generic ~2–7% faster in a few cells) — left as a follow-up; flagging in case you'd rather fold it in here.
Trimmed the PR to keep it clear and concise — it's now just the dispatcher change in kernels/flash_attn_generic.py plus the no-GPU routing unit test (tests/unit/test_flash_attn_dense_routing.py), 2 files, +125/−35.
I moved the full-map verification tooling (verify_dense_routing.sh + analyze_dense_routing.py) out of the PR — it's PR-specific reproduction tooling rather than library code. It's preserved on our fork branch if you want to reproduce the measurements yourself:
➡️ https://github.com/jhinpan/FlyDSL-lab/tree/kda/dense-routing-verification/scripts
The verification result it produced is unchanged and summarized in the PR description (full dense shape-map, 160/160 PASS; re-routed B=8/S=192 GQA cells +5–8%, MHA neutral).
@yanguahe plz take a look.