Skip to content

MHA v4: fix torch 2.10 Dynamo graph break - #7647

Merged
gyohuangxin merged 2 commits into
ROCm:mainfrom
nsakkine:fix_mha_v4_dynamo_break
Nov 9, 2026
Merged

gyohuangxin merged 2 commits into
ROCm:mainfrom
nsakkine:fix_mha_v4_dynamo_break

Conversation

@nsakkine

@nsakkine nsakkine commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix the Dynamo graph break in block-sparse mha_v4 under torch 2.10, which turns test_mha_v4_sparse.py red on MI300X CI.

Motivation

test_mha_v4_sparse_block_mask_compiles_without_graph_breaks fails on the CI image (torch 2.10) with SourcelessBuilder.create does not know how to wrap <class 'method'>. The cause is AttentionScaleMode(k_scale_mode) in mha_v4_packed: torch 2.10's Dynamo can't trace building an enum from a value. Newer torch can, so this didn't show up locally.

Changes

  • aiter/ops/mha_v4.py: compare k_scale_mode to AttentionScaleMode.E8M0_PER_1X32 directly instead of wrapping it in the enum constructor. AttentionScaleMode is an IntEnum and the scale modes are already checked by _validate_scale_recipe, so behaviour doesn't change.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • No documentation change is needed: no user-facing behaviour changes.

Dependencies

  • No new third-party dependencies added

Breaking Changes

None

PR Self-Review

Reviewed revision

80b9b43

Behavior and intent

No functional change. mha_v4_packed decided whether to make the K scales head-major with AttentionScaleMode(k_scale_mode) == AttentionScaleMode.E8M0_PER_1X32. Under torch 2.10 (the CI image), Dynamo can't trace building an enum from a value. It graph-breaks with SourcelessBuilder.create does not know how to wrap <class 'method'>, which fails test_mha_v4_sparse_block_mask_compiles_without_graph_breaks and fails torch.compile(fullgraph=True) on the block-sparse path. The fix compares k_scale_mode to the enum constant directly. AttentionScaleMode is an IntEnum, so the result is the same whether the value is an enum or a plain int. _validate_scale_recipe has already checked the value at that point, so the constructor's own validation was redundant.

Validation and unverified scope

MI300X (gfx942), CI image rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.10.0:

  • python op_tests/test_mha_v4_sparse.py: before the fix, 126 ran and 1 failed (the graph-break check, with the error above). After it: 127 ran, 168 skipped, 0 failed.
  • python op_tests/test_mha_v4.py: 66 ran, 261 skipped, 0 failed.
  • torch._dynamo.explain on mha_v4(..., block_mask=mask) with FP8 operands: 1 graph break before the fix, 0 after.
  • ruff 0.16.0 and black pass on aiter/ops/mha_v4.py.

Unverified:

  • gfx950 was not run.
  • The fixed code was not rerun on torch 2.13 (the code before the fix passed there).
  • The branch where the condition is true (64-token KV tile with E8M0 K scales, i.e. the MXFP8/MXFP4 sparse rows) has no rows on gfx942, so only the false branch ran here.

PR-specific question

The only path whose behaviour could change is the head-major K-scale branch (kv_tile == 64 with E8M0 K scales and FP8 or MXFP4 K). Getting this comparison wrong would silently skip the K-scale re-layout on gfx950 MXFP8/MXFP4 64x64 sparse calls and corrupt their output. Evidence that it doesn't:

  • Python's IntEnum semantics: 5 == AttentionScaleMode.E8M0_PER_1X32 and AttentionScaleMode(5) == AttentionScaleMode.E8M0_PER_1X32 are both true, and every other validated value compares false either way.
  • The input is validated by _validate_scale_recipe before this point, so the constructor never rejected anything here.
  • On gfx942, the false branch is covered by the sparse suite above. The true branch is covered on gfx950 by the existing 64x64 MXFP8/MXFP4 sparse checks in test_mha_v4_sparse.py (e.g. test_mha_v4_sparse_64x64_all_true_mask_matches_dense), which the gfx950 CI job runs.

Changed op tests

None

AttentionScaleMode(k_scale_mode) in mha_v4_packed graph-breaks under
torch 2.10's Dynamo. Compare the IntEnum value directly instead; the
result is unchanged.

Signed-off-by: Niko Säkkinen <[email protected]>
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
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 7647 --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.

@nsakkine
nsakkine marked this pull request as ready for review November 9, 2026 07:35
@nsakkine
nsakkine requested a review from a team November 9, 2026 07:35
@aiter-bot

aiter-bot commented Nov 9, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 80b9b43c237a · 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

No functional change. `mha_v4_packed` decided whether to make the K scales head-major with `AttentionScaleMode(k_scale_mode) == AttentionScaleMode.E8M0_PER_1X32`. Under torch 2.10 (the CI image), Dynamo can't trace building an enum from a value. It graph-breaks with `SourcelessBuilder.create does not know how to wrap <class 'method'>`, which fails `test_mha_v4_sparse_block_mask_compiles_without_graph_breaks` and fails `torch.compile(fullgraph=True)` on the block-sparse path. The fix compares `k_scale_mode` to the enum constant directly. `AttentionScaleMode` is an `IntEnum`, so the result is the same whether the value is an enum or a plain int. `_validate_scale_recipe` has already checked the value at that point, so the constructor's own validation was redundant.

Validation and unverified scope

MI300X (gfx942), CI image `rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.10.0`:
- `python op_tests/test_mha_v4_sparse.py`: before the fix, 126 ran and 1 failed (the graph-break check, with the error above). After it: 127 ran, 168 skipped, 0 failed.
- `python op_tests/test_mha_v4.py`: 66 ran, 261 skipped, 0 failed.
- `torch._dynamo.explain` on `mha_v4(..., block_mask=mask)` with FP8 operands: 1 graph break before the fix, 0 after.
- ruff 0.16.0 and black pass on `aiter/ops/mha_v4.py`.

Unverified:
- gfx950 was not run.
- The fixed code was not rerun on torch 2.13 (the code before the fix passed there).
- The branch where the condition is true (64-token KV tile with E8M0 K scales, i.e. the MXFP8/MXFP4 sparse rows) has no rows on gfx942, so only the false branch ran here.

PR-specific question

The only path whose behaviour could change is the head-major K-scale branch (`kv_tile == 64` with E8M0 K scales and FP8 or MXFP4 K). Getting this comparison wrong would silently skip the K-scale re-layout on gfx950 MXFP8/MXFP4 64x64 sparse calls and corrupt their output. Evidence that it doesn't:
- Python's `IntEnum` semantics: `5 == AttentionScaleMode.E8M0_PER_1X32` and `AttentionScaleMode(5) == AttentionScaleMode.E8M0_PER_1X32` are both true, and every other validated value compares false either way.
- The input is validated by `_validate_scale_recipe` before this point, so the constructor never rejected anything here.
- On gfx942, the false branch is covered by the sparse suite above. The true branch is covered on gfx950 by the existing 64x64 MXFP8/MXFP4 sparse checks in `test_mha_v4_sparse.py` (e.g. `test_mha_v4_sparse_64x64_all_true_mask_matches_dense`), which the gfx950 CI job runs.

Changed op tests

None

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
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

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

@gyohuangxin gyohuangxin added the ci:mi300x Run MI300X standard and OPUS CI on PRs label Nov 9, 2026
@gyohuangxin
gyohuangxin merged commit 1eab5ae into ROCm:main Nov 9, 2026
ruanjm pushed a commit that referenced this pull request Nov 10, 2026
…#7647)

AttentionScaleMode(k_scale_mode) in mha_v4_packed graph-breaks under
torch 2.10's Dynamo. Compare the IntEnum value directly instead; the
result is unchanged.

Signed-off-by: Niko Säkkinen <[email protected]>
Co-authored-by: Xin Huang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:mi300x Run MI300X standard and OPUS CI on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants