Repository navigation
[Triton/Gluon] Unified attention: opt-in prefill configs for head sizes 129+ - #5409
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The opt-in accepts undocumented values, warning fallback can flood logs, and the new selection paths lack automated coverage.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds an opt-in gfx950 unified-attention prefill fast path for large head sizes while preserving default behavior.
Changes:
- Adds tuned prefill configurations for head sizes 129–511.
- Applies large-KV compiler overrides when supported.
- Adds environment-gated config merging and runtime derivation.
File summaries
| File | Description |
|---|---|
unified_attention_utils.py |
Implements opt-in loading and large-KV override resolution. |
DEFAULT.json |
Adds gfx950 prefill tuning entries. |
Review details
Suppressed comments (2)
aiter/ops/triton/utils/unified_attention_utils.py:215
- On a Triton version without
llvm_fn_attrs, this warning runs on every affected attention invocation because_resolve_large_kvis deliberately outside the cached lookup. With warning logging enabled by default, an opt-in workload can flood logs and add host-side overhead in the hot path; cache the warning per unsupported option set (and check cache size before probing support) so the fallback is reported once.
if unsupported:
_LOGGER.warning(
"unified attention: this Triton takes no %s, so the opt-in entry's "
"large-KV overrides are unavailable",
", ".join(unsupported),
)
aiter/ops/triton/utils/unified_attention_utils.py:330
- The checked-in unified-attention tests run only with the flag unset, and no test references
AITER_TRITON_UA_FAST_PATH,OPT_IN, orLARGE_KV. Consequently the new merge precedence, dtype/size guards, unsupported-option stripping, and preservation of the flag-off path are not regression-tested even though this test suite is run by CI. Add focused tests for flag off/on and eachLARGE_KVgate described in the PR.
if _USE_OPT_IN:
# A merge, not an edit: an opt-in key may be new to the table, and then
# has to enter the lookup as a candidate rather than replace a config.
table = {**table, **config.get(op + _OPT_IN, {})}
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
MI355X benchmarks (cagrikymk) show the composite entries force BLOCK_M=128 at head 256/512 with 2-4 warps, exceeding the 256-VGPR budget at waves_per_eu=2: 300-600 spills per kernel, up to 3.4KB scratch, geomean ~8.8x slower on the affected shapes. On triton 3.7.0 one entry additionally aborts the compiler (LLVM assert). Our own gfx950 e2e (MI350X) also showed no gain (~0.4%, noise). gfx950 large-head prefill tuning is better served by ROCm#5409's opt-in mechanism (waves_per_eu=1 lifts the VGPR budget to 512), which lands per-shape behind AITER_TRITON_UA_FAST_PATH instead of changing defaults. Keep the gfx942 keys: the 5.9% e2e win there was measured with BLOCK_M=128 active on MI325X (head 256/512, fp8 KV) with no spill-related regression reported. Signed-off-by: Mustafa Yildirim <[email protected]>
|
@cpersson-amd Did you have a chance to look at why the register spill happens in newer triton versions? |
I think the spills are due to a change in the LLVM AMDGPU scheduler. I asked Claude to look for changes in LLVM between Triton 3.6 and 3.7 that could cause this regression. This PR llvm/llvm-project#147653 was pointed out as the possible regression point. This was briefly tested by running the identical kernel IR through Triton 3.7's pinned LLVM (
This PR was also suggested as a potential fix for this issue: llvm/llvm-project#213116. |
farlukas
left a comment
There was a problem hiding this comment.
Looks good, could you clean up the code a bit and remove some of the code comments, they seem excessive for your change.
|
Thanks for the review @farlukas! I cleaned up the code a bit and tests are passing. Let me know if you think anything else needs to be changed. |
A QK head that is not a power of two is padded to the next one, so head 192 runs a 256-wide Q.K dot with a quarter of it masked out. On gfx950 the padded 128-row 2D tile needs about 470 VGPRs (ROCm#5409), which caps the block size the tuner can use for long prefill. The 2D and 3D Triton kernels gain HEAD_SIZE_LO / HEAD_SIZE_HI constexprs. When set, they load Q and K in those two chunks and accumulate two dots: S = tl.dot(Q, K) S = tl.dot(Q_hi, K_hi, acc=S) HEAD_SIZE_LO = 0, the default, compiles the kernels exactly as before. Static asserts require the two chunks to add up to HEAD_SIZE and reject a shuffled KV cache. The wrapper's _head_split() sets 128 + 64 for head 192 with a BF16 or FP16 query and KV cache, no sliding window and an unshuffled cache: the case that has been measured (MiMo-V2.6's full-attention layers). Other heads, FP8, and sliding-window calls keep the padded dot until they are measured with the split. Tests: _head_split cases, and Triton-backend correctness for head 192 (V 128 and V 192, BF16 and FP16, block sizes 1 and 16) on a verify-like batch and a short extend (both on the 3D kernel) and on a 2048-token prefill chunk (2D). Co-authored-by: Cursor <[email protected]>
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
|
Hi @farlukas @azaidy, thanks for the review and approval! CI is green, is anything else needed before this can be merged? If @cagrikymk's or @valarLip's review is still required, I'm happy to answer questions. |
Also, for the modified configs, please try to see how config routing changes and how that affects perf. One more thing, the same issues might not be triggered for 3.8+. If you are targeting 3.7 specifically, what is the reasoning behind? Claude review: Good
Issues with the flag on
Suggestions
My results were measured on Triton 3.8 only; I haven't checked Triton 3.7. |
… default Replace the opt-in section (AITER_TRITON_UA_FAST_PATH, LARGE_KV and the llvm_fn_attrs scheduler override) with six D_LEQ_256 prefill entries in the default attn_2d table, retuned on current main and Triton 3.8. They are keyed on dtype (DT_fp8_fp8, DT_bf16_any) and on sliding window and shuffled cache, so only prefill at head sizes 129-256 with an fp8 or bf16 query changes config. Add a prefill test for head sizes 160-320 on the Triton backend.
|
Thanks for the thorough review @cagrikymk. I've reworked the PR along those lines:
While testing I found two failures that already exist on main: the Triton 2D kernel gives wrong output with a shuffled KV cache at head sizes 160, 192 and 320 (any config, main's included), and bf16 at 320 with a shuffled cache exceeds shared memory. I left both out of this PR; happy to open an issue. |
Thanks! I have another PR that changes the way shuffling works so it doesnt have to pin the block size to the page size. I will take a look at the odd head sizes and maybe add an assert so it doesnt fail silently. In the meantime, this PR LGTM! |
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
|
Closed and reponed the PR by mistake 🫨 |
A QK head that is not a power of two is padded to the next one, so head 192 runs a 256-wide Q.K dot with a quarter of it masked out. On gfx950 the padded 128-row 2D tile needs about 470 VGPRs (ROCm#5409), which caps the block size the tuner can use for long prefill. The 2D and 3D Triton kernels gain HEAD_SIZE_LO / HEAD_SIZE_HI constexprs. When set, they load Q and K in those two chunks and accumulate two dots: S = tl.dot(Q, K) S = tl.dot(Q_hi, K_hi, acc=S) HEAD_SIZE_LO = 0, the default, compiles the kernels exactly as before. Static asserts require the two chunks to add up to HEAD_SIZE and reject a shuffled KV cache. The wrapper's _head_split() sets 128 + 64 for head 192 with a BF16 or FP16 query and KV cache, no sliding window and an unshuffled cache: the case that has been measured (MiMo-V2.6's full-attention layers). Other heads, FP8, and sliding-window calls keep the padded dot until they are measured with the split. Tests: _head_split cases, and Triton-backend correctness for head 192 (V 128 and V 192, BF16 and FP16, block sizes 1 and 16) on a verify-like batch and a short extend (both on the 3D kernel) and on a 2048-token prefill chunk (2D). Co-authored-by: Cursor <[email protected]>
… head sizes 257-511 (#8400) test_triton_unified_attn_prefill_head_sizes (from #5409) fails on gfx942 for BF16, head_size=320, seq_lens0: the kernel requests 131072 bytes of shared memory and the device has 65536. Head size 320 pads to 512. On a 304-CU part this case launches too few 2D programs, so it runs the 3D split-KV kernel. kv_split "any" gives it TILE_SIZE 64, so the K and V tiles need 131072 bytes with a 16-bit KV cache. The head_size >= 512 rule that keeps the 3D kernel off for large heads does not apply, because it checks the head size before padding. Add a kv_split entry for head sizes 257-511 with a 16-bit KV cache. It is the "any" entry with TILE_SIZE_MAX and SEGMENT_TILE_MAX set to 32. An fp8 KV cache already fits and keeps its config. Compiled every case of the test for gfx942 (wave64, CU_NUM=304, Triton 3.8.0): on main only this case is above 65536 bytes; with this change it uses 65536 bytes and the other 47 cases do not change.
A QK head that is not a power of two is padded to the next one, so head 192 runs a 256-wide Q.K dot with a quarter of it masked out. On gfx950 the padded 128-row 2D tile needs about 470 VGPRs (ROCm#5409), which caps the block size the tuner can use for long prefill. The 2D and 3D Triton kernels gain HEAD_SIZE_LO / HEAD_SIZE_HI constexprs. When set, they load Q and K in those two chunks and accumulate two dots: S = tl.dot(Q, K) S = tl.dot(Q_hi, K_hi, acc=S) HEAD_SIZE_LO = 0, the default, compiles the kernels exactly as before. Static asserts require the two chunks to add up to HEAD_SIZE and reject a shuffled KV cache. The wrapper's _head_split() sets 128 + 64 for head 192 with a BF16 or FP16 query and KV cache, no sliding window and an unshuffled cache: the case that has been measured (MiMo-V2.6's full-attention layers). Other heads, FP8, and sliding-window calls keep the padded dot until they are measured with the split. Tests: _head_split cases, and Triton-backend correctness for head 192 (V 128 and V 192, BF16 and FP16, block sizes 1 and 16) on a verify-like batch and a short extend (both on the 3D kernel) and on a 2048-token prefill chunk (2D). Co-authored-by: Cursor <[email protected]>
…ds at main's retuned prefill defaults ROCm#5409 retuned the gfx950 prefill defaults for head sizes 129-256 (the D_LEQ_256 entries). The guards that keep non-split prefill off this PR's head-192 entry still pinned the previous generic config, which would have undone that retune for heads 129-191 and for sliding-window and shuffled-KV prefill at 192. They now carry the D_LEQ_256 values, so only full-attention BF16 prefill at head 192 changes. The config test expects the same. Co-authored-by: Cursor <[email protected]>
Motivation
This PR covers a Triton regression raised in #4137: Unified attention prefill regressed on gfx950 for head sizes 129 and above. On the Qwen3.5-397B debug shape the kernel went from about 4ms on Triton 3.6 to 16ms on 3.7 and 12ms on 3.8. This is due to three separate problems:
Register pressure. At
BLOCK_M=128a head size in 129-255 pads to a 256-element accumulator, and the kernel needs about 470 VGPRs (it uses no LDS at all).waves_per_eu=2caps the budget at 256, so the excess spills to scratch inside the KV loop.An LLVM scheduler regression in Triton >= 3.7. The same config that fit on 3.6 does not on 3.7+, which is what turned this from latent to visible. TTIR and TTGIR are byte-identical across the three versions and the LLVM IR instruction mix is the same; only the scratch size differs, so it is the AMDGPU machine scheduler producing longer live ranges, not anything in Triton's own passes.
Config shadowing. Because the lookup gives the leftmost axis priority,
D_GEQ_256outranksQ_GEQ_256, so head sizes 256+ in prefill were picking aBLOCK_M=16entry meant for decode. That is whyd=256was slower thand=192despite doing less work per program.The goal is to fix all three without changing the default behavior so when a fix eventually lands in Triton, this change does not end up regressing the performance. The tuned configs trade occupancy for registers against one measured shape family, so they are exposed as an opt-in rather than made the default.
Technical Details
Two files change: the gfx950
DEFAULT.jsonandunified_attention_utils.py.An opt-in config section. A new
attn_2d.OPT_INsection holds the retuned prefill entries and is merged into the lookup only whenAITER_TRITON_UA_FAST_PATH=1. With the variable unset nothing changes: the section is never consulted, and all five pre-existing sections ofDEFAULT.jsonare byte-identical to before. The core fix in these entries iswaves_per_eu=1, which lifts the VGPR budget from 256 to 512 and eliminates the spill. It is scoped to head sizes 129+ (D_GEQ_129/D_GEQ_256keys) because sizes below that never spilled, and halving their occupancy target only costs them speed.The AMD backend workaround. On top of the register fix, the opt-in entries can pass
amdgpu-sched-strategy=iterative-minregthrough Triton'sllvm_fn_attrsoption. This overrides the AMDGPU machine scheduler with a register-minimising strategy, recovering a further ~25% on large caches. It is not unconditionally good, so it sits in aLARGE_KVsub-block behind three guards:2**31elements the backend indexes the cache with 32-bit buffer ops instead of 64-bit global loads. Against that different code the attribute costs about 3x rather than saving 25%. The guard counts elements, so if what the backend really tests is bytes, a borderline cache is declined rather than wrongly taken.DT_fp8_fp8keys carry them.llvm_fn_attrsis read off the backend's own options rather than inferred from a version number, and the block is skipped with a warning where the option does not exist.Test Plan
A standalone harness replays the Qwen3.5-397B debug shape and swaps configs through the lookup:
LARGE_KVgates: large cache with the option supported (applies), small cache (size guard declines), and a backend withoutllvm_fn_attrs(support guard declines). Also asserts theLARGE_KVsub-block never leaks into the launched config.DEFAULT.jsonagainstHEADto confirm the flag-off path is untouched.Test Result
All pre-existing
DEFAULT.jsonsections are byte-identical toHEAD; the only addition isattn_2d.OPT_IN. The guard tests pass (overrides apply only on a large fp8 cache with a supporting Triton, and never leak), and the opt-in base and large-KV overrides are bitwise identical.gfx950, fp8, prefill. Small cache is 128 blocks, large is 4795. Each cell is p50 latency in ms (±0.1-0.2 ms run to run), with kernel scratch (register spill) in bytes in parentheses; "unchanged" means the opt-in section holds no entry for that head size, so the flag has no effect.
bf16, same shape, large cache is 2400 blocks. bf16 stays on the opt-in base config, since the large-KV overrides do not apply to it:
Spill drops from up to 920 B to at most 20 B on every affected head size (0 B except on the fp8 large cache, where the minreg override leaves 20 B). The
d=256bf16 row is the shadowing fix on its own; thed=192row is the register fix, worth 3.6x on the large cache. Decode is untouched under both settings, as are head sizes 64, 128 and 512.Notes and follow-ups
D_GEQ_256shadowsD_GEQ_129.Q_GEQ_256, so the config has to be repeated at eachDbound, and again split by dtype because only fp8 takes theLARGE_KVblock.waves_per_eu=1trades occupancy for registers, which is why this is opt-in rather than the default.Submission Checklist