Repository navigation
[ASM] Mha v4: gfx950 hd256 BF16 kernel - #7939
Conversation
Dense hd256 with BF16 Q/K/V/O, exact softmax, LSE, seqlens_k and GQA. MI355X vs CK flash_attn hd256: 1272 vs 518 TFLOP/s (h16 s4096), 1330 vs 440 (h5 s65536). O rel error 2.3e-3 and LSE 2e-6 vs float32. test_mha_v4: 339 passed.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
|
I expect conflicts with #7756, please @JohnNikolay84 rebase when the other merges |
BF16 is the first hd256 recipe that kv_splits accepts, so the fold and the LSE merge had no check at head dimension 256.
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
Summary
Adds a gfx950 MHA v4 kernel for head dimension 256 with BF16 Q, K, V and O (dense; LSE,
seqlens_k, GQA andkv_splitssupported), and routesmha_v4/mha_v4_packedto it: a manifest row forfwd_hd256_bf16.coand (BF16, BF16) in the head-dim-256 recipe set. Until now hd256 was MXFP4/MXFP6/MXFP8 only and a BF16 hd256 call raised.The kernel keeps the hd128 BF16 numerics (exact
exp2softmax with a frozen running max and an out-of-line rebase, L from a sparse ones@P MFMA) in the hd256 256x64 tile: K and V stream through a small register ring, so the O accumulator, a resident Q and the scores fit 256 VGPRs, and two K tiles plus three V slots fit 160 KiB of LDS.Performance
MI355X, one GPU,
do_benchmedians of 7 rotated rounds; same-FLOP comparison against CKflash_attn_funcat hd256 and against MHA v4 hd128 BF16 at twice the heads:kv_splits="auto"with #7942h5 s16384 runs 320 workgroups on 256 CUs, so the second round is a quarter full; hd128 at twice the heads runs 640, 2.5 rounds, and loses less to that tail. Splitting the keys fills the tail: on main
autoonly splits grids up to half a wave, so it leaves this shape unsplit; #7942 lets it pick the split by a wave-rounding cost model, which recovers the 24% here and stays unsplit on the other three. With both PRs applied thekv_splitsand hd256 checks pass (97) and the 4-split output matches the unsplit one to 4.9e-4.PR Self-Review
Reviewed revision
0a671d042fBehavior and intent
New capability only: inputs that reached an object before reach the same object. A BF16 Q/K/V call at head dimension 256 used to raise
ValueErrorand now runs; BF16/FP8 and per-tensor FP8 at hd256 still raise, with the message listing the supported recipes.aiter/ops/mha_v4.mdnow lists BF16 among the hd256 recipes.Validation and unverified scope
MI355X at
0a671d042f:python3 op_tests/test_mha_v4.py357 passed,python3 op_tests/test_mha_v4_sparse.py609 passed (5 skipped). New checks against float32 attention: O relative error <= 0.01 (measured 2.3e-3) and LSE max error <= 0.01 (measured <= 2e-6) over aligned, batched, partial-KV, under-two-tiles, single-key and partial-query-tile shapes, a key ramp that forces the frozen-max rebase, andseqlens_k; GQA is bitwise equal to repeated K/V; 2 and 3 key splits stay within 10% of the unsplit error, and a merge that ignores the range LSEs fails that check (O error 0.54 against 0.002). The object is byte-identical to the one first validated: its repeated launches were bitwise identical (27 frozen-input configurations, 300-1000 launches each, 0 mismatches). Unverified: nothing was run on MI300X; the manifest row and the object are gfx950-only.PR-specific question
Selected only on gfx950, dense, for BF16 Q/K/V at head dimension 256: the row is in the gfx950 manifest and the launcher matches formats, scale modes, head dimension and mode.
_check_head_dimadmits hd256 only for the listed format pairs and rejects every sparse or Sol-Attn call at hd256, so their routing is unchanged. Other architectures have no such row, so the manifest lookup raises instead of dispatching.Changed op tests
op_tests/test_mha_v4.py: newtest_mha_v4_bf16_hd256_matches_attention,test_mha_v4_bf16_hd256_rebases_a_rising_max,test_mha_v4_bf16_hd256_seqlens_kandtest_mha_v4_bf16_hd256_kv_splits_match_the_unsplit_call(BF16 is the first hd256 recipe that splits its keys, so the fold and the LSE merge had no check at head dimension 256); the hd256 GQA check (renamedtest_mha_v4_hd256_gqa_matches_repeated_kv) gains a BF16 case; the hd256 rejection check now covers BF16/FP8 and per-tensor FP8 instead of BF16.