Skip to content

[Triton/Gluon] [GFX950] Mha v4 production - #7192

Merged
leonling-ll merged 3 commits into
ROCm:mainfrom
leonling-ll:liyang/gfx950-mha-v4
Nov 10, 2026
Merged

leonling-ll merged 3 commits into
ROCm:mainfrom
leonling-ll:liyang/gfx950-mha-v4

Conversation

@leonling-ll

@leonling-ll leonling-ll commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

This PR aims to productize the optimization in ROCm/gfx950-gluon-tutorials - fmha_v4 to aiter MHA gluon kernel.

Main changes:

  • Lazy rescale: The row max only moves when it rises by more than LAZY_RESCALE_THRESHOLD (8.0 for bf16/fp16, 4.0 for fp8).
  • Softmax split across the two dot clusters: _sc_vec1 computes the row max and exp2 for 5/8 of the columns; _sc_vec2 does the remaining 3/8 plus the row sum and the P downcast.
  • Fix gpr spills by instruction reordering.
  • More tuned configs
  • Rename mha.json to DEFAULT.json to align with other gluon kernels.

The performance over all shapes in bench_mha.py could have 4-16% improvement.

mode shape non-causal main non-causal latest Δ causal main causal latest Δ
MHA d=128 B2 S8192 H16/16 908.8 982.6 +8.1% 820.9 885.5 +7.9%
GQA d=128 B2 S8192 H16/2 934.8 1001.3 +7.1% 848.0 907.8 +7.1%
MQA d=128 B2 S8192 H16/1 931.0 1007.7 +8.2% 852.5 903.1 +5.9%
MHA d=64 B4 S4096 H16/16 712.9 774.4 +8.6% 587.5 644.6 +9.7%
padded d=40 B4 S4096 H16/1 489.2 522.9 +6.9% 412.8 440.9 +6.8%
padded d=96 B2 S4096 H16/16 827.9 862.6 +4.2% 692.5 722.7 +4.4%
PE d=128 dv=64 B2 S4096 H16/16 837.0 879.0 +5.0% 695.7 727.9 +4.6%
PE d=192 dv=128 B2 S4096 H16/16 837.9 924.7 +10.4% 686.1 772.3 +12.6%
SWA left=1024 d=128 B2 S8192 H16/16 839.3 925.3 +10.2% 503.4 588.2 +16.8%
sink d=128 B2 S8192 H16/16 910.3 970.4 +6.6% 825.0 880.2 +6.7%
varlen d=128 B2 S8192 H16/16 915.8 979.4 +6.9% 817.5 879.5 +7.6%
fp8 d=128 B2 S8192 H16/16 1416.3 1524.7 +7.7% 1317.7 1404.3 +6.6%

cc: @Dewei-Wang-sh

To satisfy the auto PR description check, below are drafted by AI and reviewed by me.

PR Self-Review

Reviewed revision

4948a67

Behavior and intent

Ports the fmha_v4 schedule from ROCm/gfx950-gluon-tutorials into aiter's gfx950 Gluon MHA forward (backend="gluon" only; the Triton backend, the backward pass and other architectures are unchanged). On main, the kernel rescales the whole accumulator on every K tile and computes the softmax in one burst, so the MFMAs wait on VALU work; causal, PE and padded-head modes also spill registers. This PR:

  • Adds lazy rescale: the row max only moves when it rises by more than LAZY_RESCALE_THRESHOLD (8 for bf16/fp16/fp32, 4 for fp8).
  • Splits the softmax across the two dot clusters.
  • Removes the spills (instruction order, runtime seqlens for lazy causal with a Q/K head of 128 or more, Q-load layout) and caches the host-side async-copy check.
  • For fp8, lifts P to e4m3's full range and shifts each row by a power of two, which the P@V MFMA undoes through its e8m0 A-scale. A fixed lift reduced by the lazy threshold biased long tails.
  • Keeps the fp16 softmax scale on the fp32 scores, because pre-scaling Q can underflow.
  • Computes the sink's initial max with an fma, so fp fusion cannot turn its rounding error into a bad first rescale (sink = 1e10 gave NaN).
  • Adds fp32 inputs, window_size=(left, 0) as causal, and fp8 PE (PE width a multiple of 64).
  • Moves the configs from mha.json to DEFAULT.json with new keys.

Validation and unverified scope

Hardware: MI350X (gfx950, 1000 W cap), ROCm 7.2, with Triton 3.8.0+amd.rocm7.2.0.git7cb7b059 (aiter's CI pin) and 3.9.0+git3b0c7f08.

  • pytest on main's op_tests/triton_tests/attention/ files test_mha.py, test_mha_with_pe.py, test_mha_with_sink.py, test_mha_dao_ai.py, test_mha_spatial_swizzle.py and test_mxfp8_attention_kernel.py: 4149 tests, 0 failed on the CI pin. The skips are the files' own (dropout on Gluon, known causal+dropout NaN cases, padded head 33). With this revision's kernel: test_mha.py -k gluon 987 passed, test_mha_with_sink.py 586 passed, test_mha_with_pe.py 288 passed.
  • python op_tests/op_benchmarks/triton/bench_mha.py -backend gluon over 24 configurations (bf16/fp16/fp32/fp8, causal and not, GQA/MQA, d40 to d256, PE 192/128 and 128/64, sink, varlen, sliding window): every run exits 0. The only skipped rows are Gluon with a right window, which the bench itself skips.
  • Performance: the table above, from bench_mha.py on one card.

Unverified / limitations:

  • Not tested on MI300X or MI250X. The Gluon backend is gfx950-only, so other architectures never select it.
  • Forward only; the backward pass is unchanged.
  • The fp8 long-tail, fp16 small-scale and sink = 1e10 cases are checked by local scripts, not by op tests.
  • Absolute TFLOPS come from a power-capped MI350X.

PR-specific question

Only the gfx950 Gluon MHA forward reads aiter/ops/triton/configs/gfx950/gluon/attention/mha/DEFAULT.json, through _get_config in the gfx950 Gluon kernel, and is_gluon_available() limits that backend to gfx950. The Triton backend's configs and every other architecture are untouched, and no code or CI path references the old mha.json.

Within Gluon MHA, every key that already existed (default, causal, causal_short, causal_window, pe, pe_narrow_v, wide_head, fp8, fp8_small_head) keeps main's tile (BLOCK_M / BLOCK_N / num_warps / waves_per_eu). It only gains LAZY_RESCALE_THRESHOLD and amdgpu-agpr-alloc=0.

The selection changes only where _get_config routes to a new key:

  • fp32: fp32 inputs, newly supported.
  • pe_wide (256x32, 8 warps): PE with a V head over 64, no window, and non-causal or seqlen_k >= 4096. Previously pe (128x32, 4 warps).
  • causal_short_padded_head (128x32): causal, 64 < V < 128, seqlen_k < 4096. Previously causal_short (128x64).
  • causal_small_head (V <= 64) and causal_padded_head (64 < V < 128, eager) keep the causal tile.
  • fp8_short (causal fp8, V > 64, seqlen_k < 2048) keeps the fp8 tile with eager rescale.
  • A left window of 0 now counts as a window and takes causal_window.

Changed op tests

  • op_tests/triton_tests/attention/test_mha.py:
    • test_mha_fp32_gluon and test_mha_varlen_fp32_gluon cover the fp32 inputs this PR enables (fp32 MFMA): output and LSE for one-tile and pipelined K, padded heads 53 and 96, causal, and varlen. No existing test feeds fp32 to the Gluon backend; the Gluon tests use bf16 or fp8.
    • test_mha_gluon_lazy_rescale_threshold covers the lazy-rescale gate this PR adds. Its deterministic row-max jumps sit just below, exactly at and just above the configured threshold, on keys long enough for the pipeline, with output and LSE checked for bf16, fp16 and fp8. The existing random-input tests never cross the threshold on purpose. It fails when the correction or the fp8 shift is removed.
  • op_tests/triton_tests/attention/test_mha_with_pe.py: test_mha_fp8_gluon_with_pe and test_mha_varlen_fp8_gluon_with_pe cover the fp8 positional encoding this PR enables (removed guard; PE width a multiple of 64), dense and varlen, causal and non-causal, against fp32 attention on the dequantized inputs. The existing PE tests are bf16-only, and the existing fp8 Gluon tests use equal Q/K and V heads. They fail when the PE dot product is dropped.
  • op_tests/triton_tests/attention/test_mha_with_sink.py:
    • test_mha_gluon_large_sink covers sink=1e10 with short and pipelined K for bf16, fp16, fp32 and fp8. It checks finite zero output and LSE equal to the sink. The existing tests use unit-scale random sinks and do not cover this regression.
    • test_mha_gluon_zero_window covers (0, 0), (0, -1), (-1, 0) and (32, 0) with causal=False, dense and varlen inputs, and unequal sequence lengths in both directions. It checks output and LSE against attention_ref, including fully masked rows. The existing Gluon window tests use a positive left window with causal=True and do not cover these paths.

@leonling-ll leonling-ll self-assigned this Oct 30, 2026
@leonling-ll
leonling-ll marked this pull request as ready for review November 5, 2026 13:32
@leonling-ll
leonling-ll requested review from a team and a balanced review from Copilot November 5, 2026 13:32
@ROCm ROCm deleted a comment from aiter-bot Nov 5, 2026
@github-actions github-actions Bot changed the title [Triton/Gluon][GFX950] Mha v4 production [Triton/Gluon] [GFX950] Mha v4 production Nov 5, 2026
@aiter-bot

aiter-bot commented Nov 5, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 4948a671d681 · 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

Ports the fmha_v4 schedule from ROCm/gfx950-gluon-tutorials into aiter's gfx950 Gluon MHA forward (`backend="gluon"` only; the Triton backend, the backward pass and other architectures are unchanged). On main, the kernel rescales the whole accumulator on every K tile and computes the softmax in one burst, so the MFMAs wait on VALU work; causal, PE and padded-head modes also spill registers. This PR:
- Adds lazy rescale: the row max only moves when it rises by more than `LAZY_RESCALE_THRESHOLD` (8 for bf16/fp16/fp32, 4 for fp8).
- Splits the softmax across the two dot clusters.
- Removes the spills (instruction order, runtime seqlens for lazy causal with a Q/K head of 128 or more, Q-load layout) and caches the host-side async-copy check.
- For fp8, lifts P to e4m3's full range and shifts each row by a power of two, which the P@V MFMA undoes through its e8m0 A-scale. A fixed lift reduced by the lazy threshold biased long tails.
- Keeps the fp16 softmax scale on the fp32 scores, because pre-scaling Q can underflow.
- Computes the sink's initial max with an fma, so fp fusion cannot turn its rounding error into a bad first rescale (sink = 1e10 gave NaN).
- Adds fp32 inputs, `window_size=(left, 0)` as causal, and fp8 PE (PE width a multiple of 64).
- Moves the configs from `mha.json` to `DEFAULT.json` with new keys.

Validation and unverified scope

Hardware: MI350X (gfx950, 1000 W cap), ROCm 7.2, with Triton 3.8.0+amd.rocm7.2.0.git7cb7b059 (aiter's CI pin) and 3.9.0+git3b0c7f08.
- `pytest` on main's `op_tests/triton_tests/attention/` files `test_mha.py`, `test_mha_with_pe.py`, `test_mha_with_sink.py`, `test_mha_dao_ai.py`, `test_mha_spatial_swizzle.py` and `test_mxfp8_attention_kernel.py`: 4149 tests, 0 failed on the CI pin. The skips are the files' own (dropout on Gluon, known causal+dropout NaN cases, padded head 33). With this revision's kernel: `test_mha.py -k gluon` 987 passed, `test_mha_with_sink.py` 586 passed, `test_mha_with_pe.py` 288 passed.
- `python op_tests/op_benchmarks/triton/bench_mha.py -backend gluon` over 24 configurations (bf16/fp16/fp32/fp8, causal and not, GQA/MQA, d40 to d256, PE 192/128 and 128/64, sink, varlen, sliding window): every run exits 0. The only skipped rows are Gluon with a right window, which the bench itself skips.
- Performance: the table above, from `bench_mha.py` on one card.

Unverified / limitations:
- Not tested on MI300X or MI250X. The Gluon backend is gfx950-only, so other architectures never select it.
- Forward only; the backward pass is unchanged.
- The fp8 long-tail, fp16 small-scale and sink = 1e10 cases are checked by local scripts, not by op tests.
- Absolute TFLOPS come from a power-capped MI350X.

PR-specific question

Only the gfx950 Gluon MHA forward reads `aiter/ops/triton/configs/gfx950/gluon/attention/mha/DEFAULT.json`, through `_get_config` in the gfx950 Gluon kernel, and `is_gluon_available()` limits that backend to gfx950. The Triton backend's configs and every other architecture are untouched, and no code or CI path references the old `mha.json`.

Within Gluon MHA, every key that already existed (`default`, `causal`, `causal_short`, `causal_window`, `pe`, `pe_narrow_v`, `wide_head`, `fp8`, `fp8_small_head`) keeps main's tile (BLOCK_M / BLOCK_N / num_warps / waves_per_eu). It only gains `LAZY_RESCALE_THRESHOLD` and `amdgpu-agpr-alloc=0`.

The selection changes only where `_get_config` routes to a new key:
- `fp32`: fp32 inputs, newly supported.
- `pe_wide` (256x32, 8 warps): PE with a V head over 64, no window, and non-causal or seqlen_k >= 4096. Previously `pe` (128x32, 4 warps).
- `causal_short_padded_head` (128x32): causal, 64 < V < 128, seqlen_k < 4096. Previously `causal_short` (128x64).
- `causal_small_head` (V <= 64) and `causal_padded_head` (64 < V < 128, eager) keep the `causal` tile.
- `fp8_short` (causal fp8, V > 64, seqlen_k < 2048) keeps the `fp8` tile with eager rescale.
- A left window of 0 now counts as a window and takes `causal_window`.

Changed op tests

- `op_tests/triton_tests/attention/test_mha.py`:
  - `test_mha_fp32_gluon` and `test_mha_varlen_fp32_gluon` cover the fp32 inputs this PR enables (fp32 MFMA): output and LSE for one-tile and pipelined K, padded heads 53 and 96, causal, and varlen. No existing test feeds fp32 to the Gluon backend; the Gluon tests use bf16 or fp8.
  - `test_mha_gluon_lazy_rescale_threshold` covers the lazy-rescale gate this PR adds. Its deterministic row-max jumps sit just below, exactly at and just above the configured threshold, on keys long enough for the pipeline, with output and LSE checked for bf16, fp16 and fp8. The existing random-input tests never cross the threshold on purpose. It fails when the correction or the fp8 shift is removed.
- `op_tests/triton_tests/attention/test_mha_with_pe.py`: `test_mha_fp8_gluon_with_pe` and `test_mha_varlen_fp8_gluon_with_pe` cover the fp8 positional encoding this PR enables (removed guard; PE width a multiple of 64), dense and varlen, causal and non-causal, against fp32 attention on the dequantized inputs. The existing PE tests are bf16-only, and the existing fp8 Gluon tests use equal Q/K and V heads. They fail when the PE dot product is dropped.
- `op_tests/triton_tests/attention/test_mha_with_sink.py`:
  - `test_mha_gluon_large_sink` covers `sink=1e10` with short and pipelined K for bf16, fp16, fp32 and fp8. It checks finite zero output and LSE equal to the sink. The existing tests use unit-scale random sinks and do not cover this regression.
  - `test_mha_gluon_zero_window` covers `(0, 0)`, `(0, -1)`, `(-1, 0)` and `(32, 0)` with `causal=False`, dense and varlen inputs, and unequal sequence lengths in both directions. It checks output and LSE against `attention_ref`, including fully masked rows. The existing Gluon window tests use a positive left window with `causal=True` and do not cover these paths.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • FAIL — an active Copilot finding has no substantive reply from the PR author

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.

Copilot AI 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.

🟡 Changes recommended

Active FP8 tail-rounding bias and FP16 scaling underflow produce incorrect attention outputs, requiring numerical fixes and regression coverage.

2 open issues
What changed in this PR

This PR brings the FMHA v4 optimizations into AITER’s gfx950 Gluon attention backend, with lazy rescaling and pipeline changes aimed at reducing register spills and improving forward performance.

Changes:

  • Splits softmax work across pipeline stages and adds tuned configurations in DEFAULT.json.
  • Extends FP32 and window handling, and adjusts input preparation and specialization.
  • Adds numerical regression tests, updates benchmark handling, and sanitizes negative kernel-name values.
File Description
op_tests/​triton_tests/​attention/​test_mha.py Adds rescaling, dtype, padded-head, varlen, window, and FP16 range tests.
op_tests/​op_benchmarks/​triton/​bench_mha.py Updates window eligibility and dtype-sweep input selection.
aiter/​ops/​triton/​utils/​_triton/​kernel_repr.py Sanitizes negative integers and integer-valued floats.
aiter/​ops/​triton/​configs/​gfx950/​gluon/​attention/​mha/​mha.json Removes the old configuration file.
aiter/​ops/​triton/​configs/​gfx950/​gluon/​attention/​mha/​DEFAULT.json Adds replacement configurations with rescaling thresholds and tuning values.
aiter/​ops/​triton/​attention/​mha.py Updates dispatch, window handling, alignment checks, scaling, and specialization.
aiter/​ops/​triton/​_gluon_kernels/​gfx950/​attention/​mha.py Implements lazy rescaling, split softmax scheduling, FP32 math, and revised config selection.

Review effort: Balanced

Comment thread aiter/ops/triton/attention/mha.py Resolved
@azaidy
azaidy requested a review from vgokhale November 5, 2026 14:05
Copilot AI balanced review requested due to automatic review settings November 6, 2026 01:06

Copilot AI 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.

🟡 Changes recommended

The non-causal FP8 path can produce NaN row-sum updates with a large sink value because its shift lacks the required -64 clamp.

6 open issues

Review effort: Balanced

Comment thread aiter/ops/triton/attention/mha.py Resolved
Copilot AI balanced review requested due to automatic review settings November 6, 2026 08:41

Copilot AI 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.

🟡 Changes recommended

The new tests leave source dtypes implicit, risking unintended precision and MFMA coverage, and lack Gluon regressions for right=0 and zero-left window masking, including unequal Q/K lengths.

7 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Add regression tests for zero and right-bound windows

aiter/​ops/​triton/​attention/​mha.py:325

The new right=0 behavior and zero-left window masking have no Gluon regression tests; existing sliding-window tests use positive left bounds and right=-1. Add dense and varlen checks against attention_ref for window_size=(-1, 0) and (0, 0), including unequal Q/K lengths.

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 9, 2026 03:10
leonling-ll and others added 2 commits November 9, 2026 03:12
Optimize gfx950 Gluon MHA with fmha_v4 rescaling

Add shape-specific lazy rescaling and split softmax scheduling, native FP32 MFMA support, causal window normalization, and FP8 padding and specialization choices. Keep eager paths where they avoid spills. Validate the README shape against the live tutorial and benchmark-derived model shapes across four dtypes.
Pass runtime sequence lengths to causal lazy-rescale kernels on Q/K heads of
128+ when the pipelined loop runs; constant lengths spill there.  Enable lazy
rescale for causal configs and add an eager causal_padded_head config for
64 < head_dim < 128.  Cache the K/V async-copy check on the host as
is_gluon_async_copy_ok.  Use a NaN-propagating row max (v_maximum3 without
canonicalizing copies), and stop row aranges from being merged and kept live
across the K loop.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

Copilot AI 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.

🔵 Needs a closer look

Human assessment is needed because Gluon regression coverage is missing for zero-bound attention windows and the large-sink rounding fix across both execution paths.

3 open issues
4 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 9, 2026 05:59

Copilot AI 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.

🟡 Changes recommended

Regression tests are missing for the new zero-window Gluon paths and the large-sink fix, leaving their output and LSE behavior unverified across key sequence-length cases.

5 open issues

Review effort: Balanced

Comment thread aiter/ops/triton/attention/mha.py Resolved
Add explicit dtype to test cases tensor init

Address review comments
Copilot AI balanced review requested due to automatic review settings November 9, 2026 16:30

Copilot AI 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.

🟡 Changes recommended

The attention sentinel causes weights to underflow for large finite BF16 scores, producing zero instead of one, so empty rows need explicit handling.

1 open issue
7 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants