You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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_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.
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.
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.
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.
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.
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.
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]>
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR aims to productize the optimization in ROCm/gfx950-gluon-tutorials - fmha_v4 to aiter MHA gluon kernel.
Main changes:
mha.jsontoDEFAULT.jsonto align with other gluon kernels.The performance over all shapes in
bench_mha.pycould have 4-16% improvement.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:LAZY_RESCALE_THRESHOLD(8 for bf16/fp16/fp32, 4 for fp8).window_size=(left, 0)as causal, and fp8 PE (PE width a multiple of 64).mha.jsontoDEFAULT.jsonwith 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.
pyteston main'sop_tests/triton_tests/attention/filestest_mha.py,test_mha_with_pe.py,test_mha_with_sink.py,test_mha_dao_ai.py,test_mha_spatial_swizzle.pyandtest_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 gluon987 passed,test_mha_with_sink.py586 passed,test_mha_with_pe.py288 passed.python op_tests/op_benchmarks/triton/bench_mha.py -backend gluonover 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.bench_mha.pyon one card.Unverified / limitations:
PR-specific question
Only the gfx950 Gluon MHA forward reads
aiter/ops/triton/configs/gfx950/gluon/attention/mha/DEFAULT.json, through_get_configin the gfx950 Gluon kernel, andis_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 oldmha.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 gainsLAZY_RESCALE_THRESHOLDandamdgpu-agpr-alloc=0.The selection changes only where
_get_configroutes 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. Previouslype(128x32, 4 warps).causal_short_padded_head(128x32): causal, 64 < V < 128, seqlen_k < 4096. Previouslycausal_short(128x64).causal_small_head(V <= 64) andcausal_padded_head(64 < V < 128, eager) keep thecausaltile.fp8_short(causal fp8, V > 64, seqlen_k < 2048) keeps thefp8tile with eager rescale.causal_window.Changed op tests
op_tests/triton_tests/attention/test_mha.py:test_mha_fp32_gluonandtest_mha_varlen_fp32_gluoncover 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_thresholdcovers 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_peandtest_mha_varlen_fp8_gluon_with_pecover 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_sinkcoverssink=1e10with 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_windowcovers(0, 0),(0, -1),(-1, 0)and(32, 0)withcausal=False, dense and varlen inputs, and unequal sequence lengths in both directions. It checks output and LSE againstattention_ref, including fully masked rows. The existing Gluon window tests use a positive left window withcausal=Trueand do not cover these paths.