Repository navigation
Conversation
…seqused_k On the TILE_SIZE == BLOCK_SIZE path, kernel_unified_attention_2d and kernel_unified_attention_3d load the whole KV page, including the slots past seqused_k in a sequence's last page. Those slots get softmax weight 0, but 0 * NaN = NaN in P @ V, so one NaN/Inf left in a page tail turns the output of every query that reads that page into NaN. Mask the V load to seq_len on that path. K keeps the full-page load: its tail columns are already replaced with -inf by the score mask. Add test_triton_unified_attn_masks_nonfinite_kv_page_tail, which writes NaN/Inf past seqused_k and runs the 2D, 3D and split-KV 3D paths. Signed-off-by: bingzhaodong <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The regression test bypasses production config resolution by hardcoding tuning parameters.
1 open finding
What changed in this PR
Fixes non-finite KV page-tail values contaminating Triton unified-attention output.
Changes:
- Masks V loads beyond
seqused_kin 2D and 3D kernels. - Adds NaN/Inf regression coverage for unsplit and split paths.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/attention/unified_attention.py |
Masks non-shuffled V page tails. |
op_tests/triton_tests/attention/test_unified_attention.py |
Adds page-tail regression tests. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| config["TILE_SIZE"] = block_size | ||
| seen_tile_sizes.append(block_size) | ||
| if op == "kv_split": | ||
| config["NUM_SEGMENTS"] = num_segments |
|
@aaab8b This is a nice catch! Regardless, this is something we probably need to address. Do you have access to any mi355 machine to check perf there as well? If not, I can also run some benchmarks. Given you skip masking for K load, hopefully this reduces the masking ops needed in the loop. When paired with splitting the masked loop, I think it should be okay in terms of perf. |
@cagrikymk Sorry for the late reply! Our team had a site building activity and I have forgot to check reply from github. I don't have a certain 355 cluster to use right now. Can you run the benchmarks for me? Thanks a lot! |
|
I see some regressions on MI355.
The overall median Δ is −0.04%; the mean is pulled up entirely by the D=256 cases. Regressions over 5%
Measurement noise (A/B spread) for these rows is 0.07–1.7%. Root cause of the D=256 regression: register spillsISA dumped from the compiled kernels (
|
|
No activity for 15 days, so this is now labelled |

Summary
On the
TILE_SIZE == BLOCK_SIZEpath (non-shuffled KV cache),kernel_unified_attention_2dandkernel_unified_attention_3dload the whole KV page, including the slots pastseqused_kin a sequence's last page. Those slots get softmax weight 0, but they still go throughP @ V, and0 * NaN = NaN,0 * Inf = NaN. A single non-finite value left in a page tail therefore turns the attention output of every query that reads that page into NaN.This PR masks the V load to the sequence length on that path (K keeps the full-page load) and adds a regression test.
This is the default path, not a corner case: on gfx942 and gfx950, a 64-token page with head_dim <= 128 gives
TILE_SIZE == BLOCK_SIZE == 64for every 2D and 3D Triton call, and vLLM'sROCM_AITER_UNIFIED_ATTNbackend picks a 64-token page by default.Motivation
We hit this in RL training (Qwen3-4B, vLLM with speculative decoding, MI308X): the target model's attention output occasionally contained NaN. In every captured input, Q and all K/V inside
seqused_kwere finite. The only non-finite values sat in KV-cache slots pastseqused_kin the last page, and the same inputs through vLLM's own Triton attention backend gave finite output.A page tail can hold anything: stale KV from a recycled page, KV of rejected draft tokens, or uninitialized memory. Whatever is there, slots past
seqused_kmust not affect the output. Why the tails held NaN/Inf in our runs is a separate issue that we are still tracking down.Root cause
In the masked tile loop of both kernels (
aiter/ops/triton/_triton_kernels/attention/unified_attention.py), the tile mask is dropped when a tile is exactly one page:With
TILE_SIZE == BLOCK_SIZE, tilejis pagej, so every address stays inside an allocated page. But the slots of the last page pastseqused_kare allocated without belonging to the sequence.S, andS = tl.where(... & seq_mask, S, float("-inf"))replaces that whole column. That is a select, not a multiply, so the NaN never reaches the result.Pis exactly 0 for those slots, butacc = tl.dot(P.to(V.dtype), V, acc=acc)computes0 * NaN = NaN. The NaN survivesacc / Land, on the 3D path, the weighted sum inreduce_segments.When the path is taken.
TILE_SIZEis derived asmax(TILE_SIZE_MIN, min(TILE_SIZE_MAX, next_pow2(block_size)))(compute_tile_params).TILE_SIZE = 64for every 2D andkv_splitentry, so every call takes this path.Q_LEQ_1) and 3D calls follow the page size and also take it. 2D calls withq > 1useTILE_SIZE = 64and the masked branch.Why it went unnoticed.
generate_dataintest_unified_attention.pyfills the whole cache withrandn, so page tails are always finite, and finite × 0 = 0. vLLM's upstream kernel, which this one is adapted from, masks both K and V withseq_offset < max_seq_prefix_lenand has no such shortcut.The shortcut has been there since unified attention was added in #1148. The kernel file is byte-identical from v0.1.22 through v0.1.24.post1, and current main is affected.
Minimal reproducer
Default configs, nothing monkeypatched. One NaN or Inf is written to the first V slot past
seqused_k; everything the sequence actually owns is finite. The reference reads only thekv_lentokens of the sequence.Output on MI308X with
AITER_TRITON_ONLY=1 python repro_ua_v_page_tail.py:The 64 non-finite outputs are 8 query tokens × 8 query heads × the one poisoned head-dim lane; the 3D case has a single query token, hence 8. Every value in the sequence is finite, but the output still contains NaN.
Fix
for j in range(masked_tile_start, tile_end): seq_offset = j * TILE_SIZE + offs_t - # to reduce the masking effect when not needed if TILE_SIZE == BLOCK_SIZE: + # The score mask discards K-tail columns after QK, but an unmasked + # non-shuffled V tail can still poison PV through 0 * NaN/Inf. tile_mask = tl.full((1,), 1, dtype=tl.int1) + v_tile_mask = seq_offset < seq_len else: tile_mask = seq_offset < max_seq_prefix_len + v_tile_mask = tile_mask ... - v_mask = v_dim_mask[None, :] & tile_mask[:, None] + v_mask = v_dim_mask[None, :] & v_tile_mask[:, None]The 3D kernel gets the same change.
tl.loadreturnsother=0.0for them, so the tail contributes0 * 0no matter what the page holds. This is not anan_to_num: a NaN computed from tokens inside the sequence still propagates.seq_lenis the boundary. Slots inside the sequence but beyond a q-block's causal prefix hold valid, finite KV and getP = 0, somax_seq_prefix_lenis not needed here.Test Plan
Hardware: AMD Instinct MI308X (gfx942), ROCm 7.14, PyTorch 2.12, Triton 3.7, Python 3.12, run with
AITER_TRITON_ONLY=1. I have no gfx950 or gfx1250 hardware, so those archs rely on CI.TILE_SIZEandNUM_SEGMENTS. Pages of 64 and 16 tokens.test_triton_unified_attn_masks_nonfinite_kv_page_tail:TILE_SIZE == BLOCK_SIZEand the route, so every arch exercises this path.pytest op_tests/triton_tests/attention/test_unified_attention.py -k "not gluon".black26.3.0 andruff0.15.7 on the changed files.Test Result
Reproducer: fails on main (4/4 cases produce NaN, exit code 1) and passes with this PR (4/4).
Default path: NaN count in the output, main → this PR.
TILE_SIZE64 != page, masked branch)In the chunked-prefill row, only 44 of the 100 query tokens are hit: exactly those whose causal range reaches the last page. Poisoning the K tail instead of the V tail gives 0 NaN on main and on this PR in every row.
Regression test: 6/6 fail against main's kernel with
KV page tail leaked into the output, and 6/6 pass with this PR.Existing tests: 1128 passed, 0 failed, 1120 skipped.
test_triton_unified_attn_3dcases are gfx950/gfx1250 only.Performance: MI308X, kernel time with
triton.testing.do_bench_cudagraph. Both kernels ran in the same process, swapped in the order main / PR / PR / main. A second full run agreed with this one within 0.15 percentage points on every shape.Lint:
blackandruffare clean.Known limitations and follow-ups
k_mask = v_mask = None), so the same0 * NaNreasoning applies. It is not changed here: gfx942 does not exercise that layout, and I could not test it. A post-loadtl.where(seq_offset[:, None] < seq_len, V, 0.0)would be the equivalent fix.tile_mask()helper, where K and V share one mask. Whichever PR lands first, the other needs to keep a separate V mask; we are happy to rebase.Authors
xiongxuyang, bingzhaodong
Submission Checklist