Skip to content

[Triton/Gluon] gfx950 Gluon unified attention: mask stale V rows past the sequence end - #6755

Open
WhatGhost wants to merge 1 commit into
ROCm:mainfrom
WhatGhost:fix/gfx950-gluon-ua-mask-oob-v
Open

WhatGhost wants to merge 1 commit into
ROCm:mainfrom
WhatGhost:fix/gfx950-gluon-ua-mask-oob-v

Conversation

@WhatGhost

@WhatGhost WhatGhost commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

[Triton/Gluon] gfx950 Gluon unified attention: mask stale V rows past the sequence end

Motivation

The last KV tile can load cache slots past seqused_k. Their scores are masked, so p is 0, but their V values go into the PV dot. These slots can hold stale NaN/Inf, and 0 * NaN = NaN makes the full output row NaN.

The gfx950 Gluon kernel does not mask V. The Triton 2D/3D kernels have the same problem. #6475 fixes them, so this PR changes only the Gluon kernel.

Technical details

  • In the last tile, zero_v_tail() sets the V rows at or past kv_len to 0 in shared memory, after the V copy and before the V read.
  • Each thread changes only the data that it copied (same blocked_v layout).
  • The hot loops do not change. Programs whose last tile has no such rows skip the step.
  • The shuffled KV cache layout (gfx1250) does not change.
  • The gfx950 attn_2d D_LEQ_64.SW config sets waves_per_eu: 3. The zeroing code adds VGPRs, and without this value the head size 64 sliding-window kernel drops from 3 to 2 waves per SIMD.

Test plan

  • New test_gluon_unified_attn_ignores_kv_tail (gfx950 only):
    • Writes NaN into the unused K/V slots: the tail of the last block and the unused blocks.
    • Covers three batches: mixed (split-KV grid), decode-only (ALL_DECODE) and prefill (2D grid, 4 warps). Also head size 64/128/256, block size 16/64, and bf16 and fp8 (Q and KV in the same dtype).
    • Asserts that each case goes to the Gluon kernel.
  • The same test with forced NUM_BUFFERS=1 and forced num_warps 1/2/4. Current configs do not select 1 buffer.
  • Full test_unified_attention.py.
  • Kernel time before and after the change, also on the spec-decode verify shapes of [Triton/Gluon] Route short sliding-window queries to the gfx950 split grid #6594.

Test results

gfx950 (MI355X), ROCm 7.2.4, Triton 3.8.0. Rebased onto main 0e995f195.

  • New test: 48 of 72 cases fail on main. All 72 pass with this PR.
  • Forced NUM_BUFFERS=1 and forced num_warps 1/2/4: all cases pass.
  • test_unified_attention.py: 2866 passed, 2963 skipped, 0 failed.

Kernel time change against main (backend="gluon", block size 64, median of 5, 2 alternating runs):

Case bf16 fp8
decode_short (~8 us) +2.4% +11.9%
decode_long +0.1% +0.9%
decode_long, SW 1024 +5.6% +3.8%
prefill_short +4.9% +3.6%
prefill_long +0.8% -2.7%
prefill_long, SW 1024 +0.2% -0.7%
prefill_long, h64, 64/8, SW 128 -1.4% -9.4%
prefill_long, h64, 64/8, SW 256 -4.3% +0.3%

Kernel time change against main on the spec-decode verify shapes of #6594 (16/2 heads, head size 64, sliding window 2048, kv 8704, q 2/4/8, batch 1–256, block size 64, median of 5, 2 alternating runs). The matched attn_2d key is from the config lookup of each call:

Cases attn_2d key bf16 / fp8
bf16, batch 64–256 (except q 8, batch 256) D_LEQ_64.Q_LEQ_{2,4,8}.P_LEQ_8.SW.DT_bf16_bf16 (#6594) -0.6% to +3.8%
batch 1–16 not recorded (cached lookup) -2.0% to +4.1%
fp8, batch 64–256; bf16, q 8, batch 256 D_LEQ_64.SW (this PR) -1.9% to -10.9%

The cost is in short kernels. In decode, the last tile always ends at the sequence end, so the zeroing must run (about 1 us on decode_short). Block size 16 gives the same pattern.

Tested only on gfx950.

PR Self-Review

Reviewed revision

5b63ca9

Behavior and intent

  • Before: in the last KV tile, the gfx950 Gluon unified attention kernel reads V rows past the sequence end. Their p is 0, but stale NaN/Inf in these cache slots makes 0 * NaN = NaN, and the full output row becomes NaN.
  • After: the kernel sets these V rows to 0 in shared memory before the PV dot. Only programs whose last tile has such rows do this step.
  • The gfx950 Gluon attn_2d config D_LEQ_64.SW sets waves_per_eu: 3. The zeroing code adds VGPRs, and this value keeps that kernel at 3 waves per SIMD.
  • The Triton kernels, other architectures and the shuffled KV cache layout do not change.

Validation and unverified scope

Hardware: MI355X (gfx950), ROCm 7.2.4, Triton 3.8.0. Base: main 0e995f195.

  • pytest op_tests/triton_tests/attention/test_unified_attention.py -k ignores_kv_tail: 72 passed. On main: 48 failed, 24 passed.
  • The same test with forced NUM_BUFFERS=1 and forced num_warps 1/2/4: all cases passed.
  • pytest op_tests/triton_tests/attention/test_unified_attention.py: 2866 passed, 2963 skipped, 0 failed.
  • Kernel time against main: see the tables in Test results.

Unverified:

  • Other architectures (gfx942, gfx1250). This PR changes only the gfx950 Gluon kernel and its config.
  • End-to-end serving.

PR-specific question

The config change is one value: attn_2d key D_LEQ_64.SW in configs/gfx950/gluon/attention/unified_attention/DEFAULT.json, waves_per_eu 0 -> 3. Only gfx950 Gluon attn_2d calls with head size <= 64 and a sliding window can use it, and only when no more specific key matches. The more specific D_LEQ_64.Q_LEQ_{2,4,8,16}.P_LEQ_8.SW.DT_bf16_bf16 keys of #6594 stay first, so short spec-decode verify batches keep their own configs.

Evidence: the JSON diff changes no other key, so calls that match other keys keep their previous config. In the #6594-shape benchmark, I recorded the matched key of each call. bf16 batch 64–256 (except q 8, batch 256) matched the #6594 keys: -0.6% to +3.8%, from the zeroing step only. fp8 batch 64–256 and bf16 q 8, batch 256 matched D_LEQ_64.SW: 1.9% to 10.9% faster.

Changed op tests

op_tests/triton_tests/attention/test_unified_attention.py: adds test_gluon_unified_attn_ignores_kv_tail. It writes NaN into the unused K/V cache slots (the tail of the last block and the unused blocks), asserts that each case runs on the gfx950 Gluon kernel, and compares the output with the reference. No existing test puts NaN into unused KV cache slots: the existing NaN uses only pre-fill the output buffer or run on gfx1250. On main, 48 of 72 cases fail.

@WhatGhost
WhatGhost requested a review from a team October 21, 2026 03:04
@github-actions

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 6755 --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.

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 regression test admits an unfixed gfx1250 path and misses native FP8 Gluon and decode-fast-path coverage.

3 open issues
What changed in this PR

This PR prevents stale NaN/Inf values beyond the KV sequence end from corrupting unified attention output.

Changes:

  • Mask unused V rows in Triton 2D and 3D kernels.
  • Clear unused V rows in gfx950 Gluon shared memory.
  • Add regression tests with poisoned KV cache tails.
File Description
op_tests/​triton_tests/​attention/​test_unified_attention.py Add poisoned-tail regression coverage.
aiter/​ops/​triton/​_triton_kernels/​attention/​unified_attention.py Mask V loads past the sequence end.
aiter/​ops/​triton/​_gluon_kernels/​gfx950/​attention/​unified_attention.py Clear V tails before shared-memory reads.

Review effort: Balanced

@cagrikymk

Copy link
Copy Markdown
Contributor

@WhatGhost Can you look into #6475 ?
I think they are both addressing the same issue.

Potential solutions:

  1. you can sync up and have single PR.
  2. You can change this PR to target the gluon kernel (so they dont overlap)

@WhatGhost
WhatGhost force-pushed the fix/gfx950-gluon-ua-mask-oob-v branch 2 times, most recently from 769e794 to 11671b8 Compare October 22, 2026 03:54
@WhatGhost WhatGhost changed the title [Triton/Gluon] Unified attention: mask stale V rows past the sequence… [Triton/Gluon] gfx950 Gluon unified attention: mask stale V rows past the sequence end Oct 22, 2026
@WhatGhost

Copy link
Copy Markdown
Contributor Author

@WhatGhost Can you look into #6475 ? I think they are both addressing the same issue.

Potential solutions:

  1. you can sync up and have single PR.
  2. You can change this PR to target the gluon kernel (so they dont overlap)

@cagrikymk Thanks for the pointer. #6475 fixes the same V tail in the Triton kernels, so I took option 2. This PR now changes only the gfx950 Gluon kernel. The Triton kernel change is removed, and the test runs only the Gluon backend on gfx950. The two PRs no longer overlap.

@cagrikymk

Copy link
Copy Markdown
Contributor

@WhatGhost Can you look into #6475 ? I think they are both addressing the same issue.
Potential solutions:

  1. you can sync up and have single PR.
  2. You can change this PR to target the gluon kernel (so they dont overlap)

@cagrikymk Thanks for the pointer. #6475 fixes the same V tail in the Triton kernels, so I took option 2. This PR now changes only the gfx950 Gluon kernel. The Triton kernel change is removed, and the test runs only the Gluon backend on gfx950. The two PRs no longer overlap.

I did some benchmarking:

scenario batch q_len kv_len head_size heads (q/kv) dtype SWA baseline (µs) fixed (µs) Δ note
decode_short 128 1 300 128 8/1 bf16 off 11.8 11.1 −5.9% sub-50µs, noisy
decode_short 128 1 300 128 8/1 fp8 off 11.7 11.8 +0.3% sub-50µs, noisy
decode_long 128 1 24013 128 8/1 bf16 off 257.2 260.1 +1.1% reliable
decode_long 128 1 24013 128 8/1 fp8 off 130.0 129.3 −0.5% reliable
decode_long 128 1 24013 128 8/1 bf16 1024 22.9 24.9 +8.9% sub-50µs, noisy
decode_long 128 1 24013 128 8/1 fp8 1024 24.0 24.8 +3.2% sub-50µs, noisy
prefill_short 8 500 500 128 8/1 bf16 off 17.2 17.4 +1.5% sub-50µs, noisy
prefill_short 8 500 500 128 8/1 fp8 off 14.4 14.6 +1.9% sub-50µs, noisy
prefill_long 2 8003 8003 128 8/1 bf16 off 309.9 310.3 +0.1% reliable
prefill_long 2 8003 8003 128 8/1 fp8 off 203.5 204.8 +0.6% reliable
prefill_long 2 8003 8003 128 8/1 bf16 1024 106.3 107.0 +0.6% reliable
prefill_long 2 8003 8003 128 8/1 fp8 1024 80.2 82.1 +2.3% reliable
prefill_long 2 8003 8003 64 64/8 fp8 128 112.1 131.5 +17.3% worst case, verified
prefill_long 2 8003 8003 64 64/8 fp8 256 146.8 169.4 +15.5% worst case, verified

Unfortunately, there are some cases this PR regresses perf. (SWA + prefill, which makes the epilogue masking overhead very visible).

Honestly, most correct solution would be to stop kv cache having NaN values. Not sure how those slip into the kv cache. However, I assume that is a lot trickier to handle correctly than masking NaNs here.

I tried also doing a masked load instead of LDS roundtrip but that causes register spill for some cases.

Unless we know what causes NaNs in kv cache, I think we can merge this PR. I will look into this further and see if we can handle NaNs without any visible perf. effect.

@cagrikymk

Copy link
Copy Markdown
Contributor

@WhatGhost Is it possible for you to take a closer look at the cases with perf. regression? And see if they can be improved?

@WhatGhost

Copy link
Copy Markdown
Contributor Author

@WhatGhost Is it possible for you to take a closer look at the cases with perf. regression? And see if they can be improved?

@cagrikymk Thanks for your review and test! I'll take a look .

@WhatGhost

Copy link
Copy Markdown
Contributor Author

@cagrikymk Thanks for the benchmark. I reproduced it on MI355X with block size 64 and found two causes. Both are fixed in the two new commits.

1. The zeroing ran for most prefill programs. The kernel compared the tail against max_seq_prefix_len. In causal prefill, this is the end of the causal range of the query block, not the end of the sequence. The slots between the two are valid KV of this step, so they need no zeroing. But almost every query block did one extra LDS round trip and barrier, and a sliding window makes each program short. The kernel now uses the sequence length (context_len + query_len), so only the last query block of each sequence does the step. Decode does not change.

2. The zeroing code pushed one config over an occupancy boundary. For head size 64, 64/8 heads, bf16 and a sliding window (attn_2d D_LEQ_64.SW), main uses 162 VGPRs. The zeroing code adds about 10, so the kernel used 172 VGPRs and dropped from 3 to 2 waves per SIMD. The config has waves_per_eu: 0. With waves_per_eu: 3, the kernel fits in 168 VGPRs with no VGPR spill. This value also makes main 2-4% faster on these cases. Moving the zeroing before the QK of the last tile (174 VGPRs) or masking V in the MFMA operand registers (202 VGPRs) did not help.

Results (gfx950, block size 64, median of 5, 2 alternating runs, change against main):

Case dtype previous commit this PR now
prefill_long, h64, 64/8, SW 128 bf16 / fp8 +12.8% / +3.7% -0.2% / -0.2%
prefill_long, h64, 64/8, SW 256 bf16 / fp8 +14.4% / +4.0% +1.4% / +0.4%
prefill_long, SW 1024 bf16 / fp8 +0.7% / +1.5% 0.0% / +0.3%
prefill_long bf16 / fp8 -0.5% / -0.1% -0.8% / -0.8%
prefill_short bf16 / fp8 +3.6% / +4.5% +3.4% / +4.1%
decode_long, SW 1024 bf16 / fp8 +6.0% / +8.4% +6.4% / +4.4%
decode_short (~8 us) bf16 / fp8 +3.3% / +12.4% +3.1% / +12.0%
decode_long bf16 / fp8 +0.3% / +0.4% +0.1% / -0.9%

The cost that stays is in short kernels. In decode, the last tile always ends at the sequence end, so the step must run. That is about 1 us on the 8 us decode_short kernel.

test_unified_attention.py: 2570 passed, 1888 skipped, 0 failed. The new test also passes with forced NUM_BUFFERS=1 and num_warps 2 and 4.

I agree that the best fix is to keep NaN out of the KV cache.

@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 11, 2026
@WhatGhost

Copy link
Copy Markdown
Contributor Author

@cagrikymk Hi, could you please help to review this PR again , thanks very much!

@WhatGhost
WhatGhost force-pushed the fix/gfx950-gluon-ua-mask-oob-v branch from c052d5d to 5b63ca9 Compare November 24, 2026 06:53
@aiter-bot

aiter-bot commented Nov 24, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 5b63ca9c390e · 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

- Before: in the last KV tile, the gfx950 Gluon unified attention kernel reads V rows past the sequence end. Their `p` is 0, but stale NaN/Inf in these cache slots makes `0 * NaN = NaN`, and the full output row becomes NaN.
- After: the kernel sets these V rows to 0 in shared memory before the PV dot. Only programs whose last tile has such rows do this step.
- The gfx950 Gluon `attn_2d` config `D_LEQ_64.SW` sets `waves_per_eu: 3`. The zeroing code adds VGPRs, and this value keeps that kernel at 3 waves per SIMD.
- The Triton kernels, other architectures and the shuffled KV cache layout do not change.

Validation and unverified scope

Hardware: MI355X (gfx950), ROCm 7.2.4, Triton 3.8.0. Base: `main` `0e995f195`.

- `pytest op_tests/triton_tests/attention/test_unified_attention.py -k ignores_kv_tail`: 72 passed. On `main`: 48 failed, 24 passed.
- The same test with forced `NUM_BUFFERS=1` and forced `num_warps` 1/2/4: all cases passed.
- `pytest op_tests/triton_tests/attention/test_unified_attention.py`: 2866 passed, 2963 skipped, 0 failed.
- Kernel time against `main`: see the tables in Test results.

Unverified:

- Other architectures (gfx942, gfx1250). This PR changes only the gfx950 Gluon kernel and its config.
- End-to-end serving.

PR-specific question

The config change is one value: `attn_2d` key `D_LEQ_64.SW` in `configs/gfx950/gluon/attention/unified_attention/DEFAULT.json`, `waves_per_eu` 0 -> 3. Only gfx950 Gluon `attn_2d` calls with head size <= 64 and a sliding window can use it, and only when no more specific key matches. The more specific `D_LEQ_64.Q_LEQ_{2,4,8,16}.P_LEQ_8.SW.DT_bf16_bf16` keys of #6594 stay first, so short spec-decode verify batches keep their own configs.

Evidence: the JSON diff changes no other key, so calls that match other keys keep their previous config. In the #6594-shape benchmark, I recorded the matched key of each call. bf16 batch 64–256 (except q 8, batch 256) matched the #6594 keys: -0.6% to +3.8%, from the zeroing step only. fp8 batch 64–256 and bf16 q 8, batch 256 matched `D_LEQ_64.SW`: 1.9% to 10.9% faster.

Changed op tests

`op_tests/triton_tests/attention/test_unified_attention.py`: adds `test_gluon_unified_attn_ignores_kv_tail`. It writes NaN into the unused K/V cache slots (the tail of the last block and the unused blocks), asserts that each case runs on the gfx950 Gluon kernel, and compares the output with the reference. No existing test puts NaN into unused KV cache slots: the existing NaN uses only pre-fill the output buffer or run on gfx1250. On `main`, 48 of 72 cases fail.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

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
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting 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.

This branch has not been deployed

No deployments
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