Repository navigation
[Triton/Gluon] unified_attention: keep 3D split-KV padding rows from storing segment stats - #7472
Conversation
…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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🔵 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
…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
…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
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:
BLOCK_Mis 16 andBLOCK_Qis 5. Each program has one padding row.query_mask_0only checksquery_pos < cur_batch_query_len, so it does not mask this row.segm_maxandsegm_expsumfor the same token and segment.(M=0, L=0). The other program stores(M=-inf, L=1). Both values are correct, but the last store wins.reduce_segmentsrescales by the maximum ofsegm_max. Thus the order of the stores changes the rounding of the output.The fix:
unified_attention.py: the 3D kernel also masks rows whereoffs_m // num_queries_per_kv >= BLOCK_Q. Only the program that owns a token loads and stores it.test_unified_attention.py: addtest_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_benchand the Triton backend. Each call uses the 3D path. Each value is the best of two alternating runs.All changes are in the run-to-run noise.
Testing
Test plan:
Test results on MI300X (gfx942):
Documentation
The fix changes only an internal mask. The API does not change.
Dependencies
Breaking Changes
None
PR Self-Review
Reviewed revision
8b5a9128aec7Behavior and intent
The padding rows of the 3D kernel do not load Q. They also do not store
segm_output,segm_maxorsegm_expsumfor 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_tritonon each call. With 16/4 heads, there is no padding row, and the old kernel already gives the same output on each run.Unverified:
PR-specific question
Before this PR,
test_triton_unified_attn_3d_padded_query_rows_deterministicfails 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 whenBLOCK_Mhas rows pastBLOCK_Qtokens. 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.