Skip to content

[Triton/Gluon] unified_attention: keep 3D split-KV padding rows from storing segment stats - #7472

Merged
azaidy merged 1 commit into
mainfrom
akaratza_ua3d_padding_row_mask
Nov 5, 2026
Merged

azaidy merged 1 commit into
mainfrom
akaratza_ua3d_padding_row_mask

Conversation

@AndreasKaratzas

@AndreasKaratzas AndreasKaratzas commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR masks the padding rows of kernel_unified_attention_3d. Split-KV prefill then gives the same output on each run.

Motivation

The 3D kernel gives different outputs from run to run for some head ratios. The difference is approximately 1e-3. In vLLM, this caused Granite 4.0-H prefix-cache restores to differ from a local prefix hit on gfx942 (vllm-project/vllm#64656). The bug is also in v0.1.27 and v0.1.28.

#5935 fixed a related write when NUM_SEGMENTS_PER_SEQ == 1. It does not fix this write.

Changes

Technical details:

  • With 3 query heads for each KV head, BLOCK_M is 16 and BLOCK_Q is 5. Each program has one padding row.
  • The padding row maps to the first token of the next q block. query_mask_0 only checks query_pos < cur_batch_query_len, so it does not mask this row.
  • Thus two programs store segm_max and segm_expsum for the same token and segment.
  • For a segment past the causal limit of the token, one program stores (M=0, L=0). The other program stores (M=-inf, L=1). Both values are correct, but the last store wins.
  • reduce_segments rescales by the maximum of segm_max. Thus the order of the stores changes the rounding of the output.

The fix:

  • unified_attention.py: the 3D kernel also masks rows where offs_m // num_queries_per_kv >= BLOCK_Q. Only the program that owns a token loads and stores it.
  • test_unified_attention.py: add test_triton_unified_attn_3d_padded_query_rows_deterministic. The test runs split-KV prefill 20 times with 12 query heads and 4 KV heads. The outputs must be bitwise equal.

Performance (if applicable)

The benchmark uses triton.testing.do_bench and the Triton backend. Each call uses the 3D path. Each value is the best of two alternating runs.

Hardware Workload/configuration Before After Change
MI300X heads 12/4, D 64, ctx 896, q 448 125.3 us 125.1 us -0.2%
MI300X heads 12/4, D 64, ctx 3584, q 512 131.0 us 131.9 us +0.7%
MI300X heads 12/4, D 64, ctx 896, q 448, 2 seqs 127.7 us 126.0 us -1.4%
MI300X heads 32/8, D 128, ctx 896, q 448 123.6 us 123.6 us 0.0%
MI300X heads 64/8, D 128, ctx 3584, q 512 238.6 us 237.9 us -0.3%

All changes are in the run-to-run noise.

Testing

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

Test plan:

  1. Run the new test before and after the fix.
  2. Run the other unified attention tests after the fix.
  3. Run the vLLM test that found the bug, with this fix on v0.1.27.

Test results on MI300X (gfx942):

pytest op_tests/triton_tests/attention/test_unified_attention.py -k padded_query_rows_deterministic
before: 2 failed (3 of 3 runs)
after:  2 passed (10 of 10 runs)

pytest op_tests/triton_tests/attention/test_unified_attention.py -k "not deterministic"
after:  1128 passed, 3721 skipped

vLLM test_mori_token_e2e.py::test_hybrid_restore_matches_local_prefix_hit
before: 5 of 5 failed
after:  13 of 13 passed

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

The fix changes only an internal mask. The API does not change.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None

PR Self-Review

Reviewed revision

8b5a9128aec7

Behavior and intent

The padding rows of the 3D kernel do not load Q. They also do not store segm_output, segm_max or segm_expsum for a token that a different program owns. The output values do not change, except for rounding. The maximum error against an fp32 reference is 9.50e-4 before and after the fix (12/4 heads). The result does not depend on the order of the stores.

Validation and unverified scope

Validated on MI300X (gfx942). The commands and results are in the Testing section. A probe shows that the new test uses _unified_attention_3d_triton on each call. With 16/4 heads, there is no padding row, and the old kernel already gives the same output on each run.

Unverified:

  • gfx950, gfx12 and MI250X. vLLM uses the Gluon kernel on gfx950. We did not check the Gluon kernel for the same problem.
  • The 2D Triton kernel. It has the same unmasked padding row. But both programs store equal values, and the 2D path was deterministic in the tests. This PR does not change it.

PR-specific question

Before this PR, test_triton_unified_attn_3d_padded_query_rows_deterministic fails on gfx942: repeated calls with the same inputs give different outputs (3 of 3 runs failed). The existing tests do not find this problem. They compare one call with a reference, and the difference (approximately 1e-3) is in their tolerance. No existing test runs the 3D path two times and compares the outputs.

Changed op tests

test_unified_attention.py: the new test checks that the 3D split-KV path gives bitwise equal output on each run when BLOCK_M has rows past BLOCK_Q tokens. No other test checks this. The tests that compare with a reference pass with each store order, because the difference is in their tolerance.

AI assistance (Claude Code) was used to draft this change. Every line was reviewed by the submitter.

…g segment stats

When BLOCK_Q * num_queries_per_kv < BLOCK_M (e.g. 3 query heads per KV
head: BLOCK_M=16, BLOCK_Q=5), the leftover rows of
kernel_unified_attention_3d map to the first token of the next q block.
query_mask_0 only checked query_pos < cur_batch_query_len, so two programs
stored segm_max / segm_expsum for that token. For a segment past its
causal limit one stored (M=0, L=0) and the other (M=-inf, L=1); the last
store won, changing the max reduce_segments rescales against, so the
output varied run to run by ~1e-3.

Mask those rows and add a test that repeated split-KV prefill with three
query heads per KV head is bitwise identical.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Andreas Karatzas <[email protected]>
@github-actions

github-actions Bot commented Nov 4, 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 gfx1201 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 7472 --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.

@aiter-bot

aiter-bot commented Nov 4, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 8b5a9128aec7 · 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

The padding rows of the 3D kernel do not load Q. They also do not store `segm_output`, `segm_max` or `segm_expsum` for a token that a different program owns. The output values do not change, except for rounding. The maximum error against an fp32 reference is 9.50e-4 before and after the fix (12/4 heads). The result does not depend on the order of the stores.

Validation and unverified scope

Validated on MI300X (gfx942). The commands and results are in the Testing section. A probe shows that the new test uses `_unified_attention_3d_triton` on each call. With 16/4 heads, there is no padding row, and the old kernel already gives the same output on each run.

Unverified:

- gfx950, gfx12 and MI250X. vLLM uses the Gluon kernel on gfx950. We did not check the Gluon kernel for the same problem.
- The 2D Triton kernel. It has the same unmasked padding row. But both programs store equal values, and the 2D path was deterministic in the tests. This PR does not change it.

PR-specific question

Before this PR, `test_triton_unified_attn_3d_padded_query_rows_deterministic` fails on gfx942: repeated calls with the same inputs give different outputs (3 of 3 runs failed). The existing tests do not find this problem. They compare one call with a reference, and the difference (approximately 1e-3) is in their tolerance. No existing test runs the 3D path two times and compares the outputs.

Changed op tests

`test_unified_attention.py`: the new test checks that the 3D split-KV path gives bitwise equal output on each run when `BLOCK_M` has rows past `BLOCK_Q` tokens. No other test checks this. The tests that compare with a reference pass with each store order, because the difference is in their tolerance.

AI assistance (Claude Code) was used to draft this change. Every line was reviewed by the submitter.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • WAIT — Black: missing
  • WAIT — Ruff: missing
  • WAIT — Repository dependency: missing
  • PASS — Aiter Test Gate: passed
  • Required CI is incomplete; this PR is not ready for 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.

@AndreasKaratzas
AndreasKaratzas marked this pull request as ready for review November 4, 2026 17:32
@AndreasKaratzas
AndreasKaratzas requested a review from a team November 4, 2026 17:32
@github-actions github-actions Bot changed the title [Triton] unified_attention: keep 3D split-KV padding rows from storing segment stats [Triton/Gluon] unified_attention: keep 3D split-KV padding rows from storing segment stats Nov 4, 2026
@Boss2002n
Boss2002n requested a balanced review from Copilot November 4, 2026 18:33

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

The gfx942 split-KV write-race fix needs runtime confirmation of numerical correctness and repeated-run determinism because GPU tests cannot run in this review environment.

No open issues 🎉

What changed in this PR

This PR fixes nondeterministic output in Triton unified attention’s 3D split-KV path by preventing padded query rows from accessing tokens owned by the next query block.

Changes:

  • Extend the query mask to exclude rows beyond BLOCK_Q.
  • Add a regression test that checks bitwise-equal outputs across repeated calls for two prefill shapes with a 3:1 query-to-KV head ratio.
File Description
op_tests/​triton_tests/​attention/​test_unified_attention.py Add repeated-run determinism coverage for padded query rows.
aiter/​ops/​triton/​_triton_kernels/​attention/​unified_attention.py Mask padding rows to prevent overlapping segment-stat stores.

Review effort: Balanced

@zufayu
zufayu requested review from a team and Boss2002n November 5, 2026 01:19
@azaidy
azaidy self-requested a review November 5, 2026 14:10

@azaidy azaidy 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.

LGTM!

@azaidy
azaidy merged commit 656e98e into main Nov 5, 2026
@azaidy
azaidy deleted the akaratza_ua3d_padding_row_mask branch November 5, 2026 14:48
cagrikymk added a commit that referenced this pull request Nov 6, 2026
…kernel

The rebase onto main dropped what main changed in the old 2d/3d kernels:
- with a sliding window the split carves only the window's tiles
  (split_window_tiles), as reduce_segments now expects; without it the
  split outputs were wrong or NaN
- rows past BLOCK_Q tokens are masked, so two q blocks no longer both
  store segment stats for the same token (#7472)
- the qq_bias load masks padded query rows
cagrikymk added a commit that referenced this pull request Nov 9, 2026
…kernel

The rebase onto main dropped what main changed in the old 2d/3d kernels:
- with a sliding window the split carves only the window's tiles
  (split_window_tiles), as reduce_segments now expects; without it the
  split outputs were wrong or NaN
- rows past BLOCK_Q tokens are masked, so two q blocks no longer both
  store segment stats for the same token (#7472)
- the qq_bias load masks padded query rows
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