Skip to content

[ASM] Mha v4: gfx950 hd256 BF16 kernel - #7939

Merged
jcaraban merged 4 commits into
mainfrom
mha4_hd256_bf16
Nov 17, 2026
Merged

jcaraban merged 4 commits into
mainfrom
mha4_hd256_bf16

Conversation

@jcaraban

@jcaraban jcaraban commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a gfx950 MHA v4 kernel for head dimension 256 with BF16 Q, K, V and O (dense; LSE, seqlens_k, GQA and kv_splits supported), and routes mha_v4 / mha_v4_packed to it: a manifest row for fwd_hd256_bf16.co and (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 exp2 softmax 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_bench medians of 7 rotated rounds; same-FLOP comparison against CK flash_attn_func at hd256 and against MHA v4 hd128 BF16 at twice the heads:

Workload hd256 BF16 hd256 BF16, kv_splits="auto" with #7942 CK hd256 hd128 BF16 (mha_v4, 2x heads)
b1 h16 s4096 1289 TFLOP/s 1288 (no split) 519 TFLOP/s 1255 TFLOP/s
b1 h32 s2048 1147 1146 (no split) 513 1022
b1 h5 s16384 1028 1274 (4 splits) 449 1283 (1344 with auto, 2 splits)
b1 h5 s65536 1363 1367 (no split) 441 1383

h5 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 auto only 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 the kv_splits and hd256 checks pass (97) and the 4-split output matches the unsplit one to 4.9e-4.

PR Self-Review

Reviewed revision

0a671d042f

Behavior 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 ValueError and now runs; BF16/FP8 and per-tensor FP8 at hd256 still raise, with the message listing the supported recipes. aiter/ops/mha_v4.md now lists BF16 among the hd256 recipes.

Validation and unverified scope

MI355X at 0a671d042f: python3 op_tests/test_mha_v4.py 357 passed, python3 op_tests/test_mha_v4_sparse.py 609 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, and seqlens_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_dim admits 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: new test_mha_v4_bf16_hd256_matches_attention, test_mha_v4_bf16_hd256_rebases_a_rising_max, test_mha_v4_bf16_hd256_seqlens_k and test_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 (renamed test_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.

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.
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7939 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@jcaraban

Copy link
Copy Markdown
Contributor Author

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.
@jcaraban
jcaraban marked this pull request as ready for review November 17, 2026 11:48
@jcaraban
jcaraban requested a review from a team November 17, 2026 11:48
@github-actions github-actions Bot added the ASM label Nov 17, 2026
@aiter-bot

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 0a671d042f20 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior 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 `ValueError` and now runs; BF16/FP8 and per-tensor FP8 at hd256 still raise, with the message listing the supported recipes. `aiter/ops/mha_v4.md` now lists BF16 among the hd256 recipes.

Validation and unverified scope

MI355X at `0a671d042f`: `python3 op_tests/test_mha_v4.py` 357 passed, `python3 op_tests/test_mha_v4_sparse.py` 609 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, and `seqlens_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_dim` admits 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`: new `test_mha_v4_bf16_hd256_matches_attention`, `test_mha_v4_bf16_hd256_rebases_a_rising_max`, `test_mha_v4_bf16_hd256_seqlens_k` and `test_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 (renamed `test_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.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

JohnNikolay84
JohnNikolay84 previously approved these changes Nov 17, 2026

@JohnNikolay84 JohnNikolay84 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does  #7939 have a hard dependency on #7942? @jcaraban

@JohnNikolay84
JohnNikolay84 dismissed their stale review November 17, 2026 17:14

I have further comments

@jcaraban
jcaraban merged commit 71ab024 into main Nov 17, 2026
@jcaraban
jcaraban deleted the mha4_hd256_bf16 branch November 17, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants