[ROCm/CK] Use aiter's gfx950 asm backward kernels in the CK backend (1.2–2.25× faster backward on MI350X)

Author: JohnsonmsCreated Sep 27, 2026Updated Sep 29, 2026

Proposal

Let the CK backward (csrc/flash_attn_ck) run aiter's gfx950 assembly backward kernels (fmha_v3_bwd) where they match fmha_bwd against an fp32 reference, and fall back to fmha_bwd everywhere else. PyTorch's SDPA backward on gfx950 already runs these kernels (via AOTriton).

MI350X, bf16, causal backward, ms (CK → with aiter). Columns are batch × seqlen; heads are 32/21/16/10 for hdim 64/96/128/192.

hdim 2×8192 4×4096 16×1024
64 4.10 → 3.41 (1.20×) 2.23 → 1.80 (1.24×) 0.79 → 0.66 (1.20×)
96 4.68 → 3.18 (1.47×) 2.57 → 1.74 (1.48×) 1.19 → 0.61 (1.93×)
128 3.45 → 2.19 (1.57×) 1.78 → 1.20 (1.48×) 0.72 → 0.47 (1.54×)
192 8.33 → 6.22 (1.34×) 5.14 → 3.20 (1.60×) 2.15 → 0.96 (2.25×)

Non-causal is 1.3–1.9× for hdim 96/128. Non-causal hdim 64 and hdim 256 stay on CK (no gain, or no kernel).

How (a branch is ready; I'll open a PR that references this issue)

  • setup.py runs aiter's codegen and embeds the gfx950 fmha_v3_bwd code objects with .incbin (4.5 MB), so nothing is looked up at runtime. third_party/aiter is already a submodule for the Triton backend.
  • mha_bwd.cpp tries aiter first. A gate admits only the validated shapes: hdim 64–192 with hdim_q == hdim_v; no dropout, ALiBi or deterministic mode; seqlen_q ≥ 16; aligned seqlens when masked; bf16 only for hdim 64/192.
  • Switches: FLASH_ATTENTION_CK_BWD_ASM=0 at runtime, FLASH_ATTENTION_CK_AITER_BWD=FALSE at build time.

Things you may not want

  1. A C++ build dependency on aiter, whose codegen needs pandas at build time. If pandas is missing it warns and builds without the kernels.
  2. A build-time binary patch. aiter's hdim-128 causal kernels miss an s_barrier and can write zeros into dK when other processes share the GPU (ROCm/aiter#5894). setup.py changes one instruction in the two kernels FA uses, and only when the file's SHA256 matches. It is removed once aiter ships fixed binaries.
  3. gfx950 only. The gfx942 kernels exist, but I couldn't test them.

Validation (MI350X, ROCm 7.1)

  • 441 shapes vs fp32 reference; 580 edge shapes (ragged seqlens, GQA, masks); tests/test_flash_attn_ck.py (not kvcache/varlen): 51,842 passed.
  • Race stress: 0 bad calls in 38,400 with 32 processes per GPU. The unpatched kernels fail about 0.4% of calls under the same load.

Would you take this? Should it be opt-in (off by default) at first?

Source: Dao-AILab/flash-attention