FlyDSL icon indicating copy to clipboard operation
FlyDSL copied to clipboard

[FMHA] gfx950: route dense short seq_len (<256) to generic fallback

Open jhinpan opened this issue 5 months ago • 1 comments

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) → always DUALWAVE_SWP (the generic fallback has no cu_seqlens path).
  • 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=None still stays on DUALWAVE_SWP), and
  • uses a len(args) > 7 positional threshold — the generic launcher's 7th positional is the shared stream, whereas dualwave's 7th is stride_kv_n, so a valid exe(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_CONFIGS sweep (bf16/fp16 × causal/nocausal): All tests PASSED, 0 FAIL / 0 ERROR (gate max_err < 1e-2 AND min_cos > 0.99 unchanged).
  • 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=1 still forces generic; seq_len >= 256 unchanged; gfx942 fallback and gfx950-only intrinsics untouched (flash_attn_gfx950.py has 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=256 is intentionally left on DUALWAVE_SWP (no reliable generic win there).
  • A gfx950 kernel-body VMEM-prefetch/waitcnt experiment was tried and rejected (relaxing the hand-scheduled vmcnt waits breaks the LDS double-buffer ordering); this PR is dispatch-only by design.

jhinpan avatar Jun 16 '26 05:06 jhinpan

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...HEAD
  • python3 -m py_compile kernels/flash_attn_generic.py

jhinpan avatar Jun 16 '26 23:06 jhinpan

@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:

  1. The threshold itself — is S≥192 @ B≥8 the right shape for the batch term, or would you prefer a single batch*seq expression consistent with the M128/M256 selector?
  2. The restored fail-loud guard on a 7th positional arg (the generic vs DUALWAVE_SWP launchers differ there — stream vs stride_kv_n) — reasonable, or over-cautious?
  3. Whether the two repro scripts belong in scripts/ or should be dropped from the PR.
  4. The pre-existing S=256 boundary 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.

jhinpan avatar Jun 17 '26 18:06 jhinpan

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).

jhinpan avatar Jun 17 '26 18:06 jhinpan

@yanguahe plz take a look.

coderfeli avatar Jun 18 '26 01:06 coderfeli