Repository navigation
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
Pull request overview
Adds opt-in fast-tile and tail-split scheduling for unified attention to improve gfx950 prefill performance.
Changes:
- Splits attention processing into unmasked bulk and masked tail loops.
- Adds optional fused scaling and pipelining controls.
- Adds tail work partitioning and segment reduction.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 12 comments.
| File | Description |
|---|---|
attention/unified_attention.py |
Adds feature dispatch and tuning flags. |
attention/kr_ua.py |
Adds the tail-split wrapper and planning. |
_triton_kernels/attention/unified_attention.py |
Refactors tile computation into a helper. |
_triton_kernels/attention/kr_split.py |
Adds split-attention and reduction kernels. |
Suppressed comments (1)
aiter/ops/triton/_triton_kernels/attention/kr_split.py:218
- The new reduction kernel is launchable but lacks the required config-aware
repr, leaving profiler/JIT trace names unable to distinguishBLOCK_M,HEAD_SIZE, andNUM_SEGMENTSspecializations. Addmake_kernel_reprand attach it via@triton.jit(repr=...).
@triton.jit
def reduce_segments_tail(
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if _KR2_BM: | ||
| BLOCK_M = _KR2_BM | ||
| BLOCK_Q = max(1, BLOCK_M // num_queries_per_kv) | ||
| if _KR2_NW: | ||
| num_warps = _KR2_NW |
There was a problem hiding this comment.
num_stages is left as an override
768f09f to
e10841e
Compare
e10841e to
e32d731
Compare
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
|
@zufayu I have addressed all the comments and have rebased to the newly landed changes! Please let me know what the next steps are |
There was a problem hiding this comment.
🟡 Changes recommended
The dtype gate, fused-scale invariant, and ULP validation currently leave correctness gaps.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 4
- Review effort level: Balanced
| if _FASTTILE == "off" or DEVICE_ARCH not in _FASTTILE_ARCHS: | ||
| return False, False | ||
| if params.sliding_window > 0 or params.all_decode: | ||
| return False, False | ||
| return True, _FASTTILE == "on" |
There was a problem hiding this comment.
Validated fp8, but not nvfp4 so left check
num_stages override is not global, it applies only where _fasttile_modes returns split=True, so it never reaches decode, windowed layers, or any non-gfx950 device.
e32d731 to
386fdb6
Compare
|
@alexnails This PR is overlap quite a bit with #4761 , is it possible for you to sync. up with that PR ? |
|
@cagrikymk @vorapolsiloai they are functionally the same optimization, however I believe 5158 is a bit more robust and cleaner. why: where 4761 is better: let me know what you guys are thinking! |
9290284 to
4943830
Compare
|
@alexnails thanks for comparing the two. Since #4761 has already been through several rounds of review and testing and is pretty close to landing, I’d prefer to merge the smaller, validated change first. I don’t have easy access to MI355 compute at the moment, so another round of E2E iteration and validation would take time and delay things further. Could we use #5158 as a follow-up on top of #4761 to expand the supported configs, add shuffled KV support, and bring in the shared tile helper? @cagrikymk does that sound reasonable? |
Yes, that sounds good. I think that is the most reasonable path. I will review this PR once the #4761 is merged and yours is rebased on top. |
… to shuffled KV Follow-up to ROCm#4761, which split the 2-D prefill tile loop into an unmasked bulk pass plus a masked tail pass behind SPLIT_UNMASKED_LOOP. That change carried the bulk pass as a second inline copy of the tile body, and the copy implemented only the per-token cache layout, so shuffled KV was excluded by static_assert. - Factor the tile body into _attention_tile(MASKED: tl.constexpr), called by both loops. One body, so future masking/scaling changes cannot drift apart between them, and the shuffled-cache branch comes along for free: the static_assert excluding shuffled KV is dropped. - Clamp the bulk bound into [tile_start, tile_end]. context_len goes negative when a sequence carries more query tokens than cached keys, and the floor division would otherwise hand the masked loop a negative first tile. - tl.static_assert that the unmasked pass never sees a sliding window, and strip SPLIT_UNMASKED_LOOP in the wrapper for windowed and decode calls whatever the table says: the bulk pass drops the window bound along with the causal one, so an entry missing its SW sibling would otherwise be silently wrong rather than slow. - Add FUSE_QK_SCALE, an opt-in config key that folds qk_scale into the exp2 argument. Refused for softcap / ALiBi / qq-bias inside the kernel and for a non-positive softmax_scale in the wrapper, since the folded maximum relies on scaling being monotone. The tuned table is unchanged: the existing SPLIT_UNMASKED_LOOP entry keeps its behaviour and no new entries are enabled here. Tests force the config keys per case, check each shape really reaches the 2-D kernel, compare split against stock elementwise within a few bf16 ULP, and cover bf16 and fp8, with and without sinks, head_size 64 and 512, a ragged batch with negative context_len, and shuffled KV. Co-Authored-By: Claude Opus 5.5 <[email protected]>
4943830 to
2eca6ce
Compare
|
@cagrikymk I think this is ready for rereview, one thing I want to note is that num_stages:= 2 seems to be a better choice in most cases from different workloads I have run. Any thoughts on this? |
@alexnails I went through the code a bit, while this is a step towards the right direction to keep the UA kernel easier to maintain, I think this will need a bigger rewrite. What I have in mind is more aligned with the gfx950 gluon kernel: https://github.com/ROCm/aiter/blob/main/aiter/ops/triton/_gluon_kernels/gfx950/attention/unified_attention.py Having single 2d/3d implementation and parameterizing it based on the workload. It might better for me to manage this more closely so I can verify codegen doesnt change enough on any arch, given this kernel is used by many archs. What do you think? |
What I have in mind is alongside: https://github.com/ROCm/aiter/blob/21fd4b40f5c0d5ceba918e391c6ce8f1a656757b/aiter/ops/triton/_triton_kernels/attention/unified_attention.py I need to verify the codegen on different archs to make sure this still produces ~identical code across archs. I think we can close this PR and continue with the design I have for better modularity, if you also agree. |
|
@cagrikymk I have closed this for now at your request, but please reach out once your new changes are ready, thank you |
What
Follow-up to #4761, as agreed in the review thread. #4761 split the 2-D prefill tile loop into an unmasked bulk pass plus a masked tail pass, behind the
SPLIT_UNMASKED_LOOPconfig key. This builds on it; it does not replace it, and the gate stays in the tuned table.Shared tile body. [Triton/Gluon] [PERF] Optimize Triton unified attention prefill and decode #4761 carried the bulk pass as a second inline copy of the tile body. It is now one helper,
_attention_tile(MASKED: tl.constexpr), that both loops call, so masking and scaling changes cannot drift between two copies.Shuffled KV support. The inline copy only implemented the per-token cache layout, so shuffled KV was excluded by
static_assert. The shared helper already has the per-tile shuffled branch, so the exclusion is dropped.Bulk-bound clamp. The bulk bound is clamped into
[tile_start, tile_end].context_lengoes negative when a sequence has more query tokens than cached keys; without the clamp, the floor division hands the masked loop a negative first tile.Sliding-window guard. The bulk pass drops the sliding-window bound along with the causal one. So:
tl.static_assertrefuses an unmasked pass with a window at compile time;SPLIT_UNMASKED_LOOPfor windowed and decode calls, whatever the table says.Without these, a table entry missing its
.SW.sibling would give silently wrong results rather than slow ones.FUSE_QK_SCALE, an opt-in config key that foldsqk_scaleinto the exp2 argument. It is refused in two places, since the folded maximum relies on scaling being monotone:softmax_scale.The tuned table is unchanged. The existing
SPLIT_UNMASKED_LOOPentry behaves as before, and this PR enables no new entries. Which cases to opt in, and at whatnum_stages, is a tuning decision that belongs in a separate change (see the note below).Tests
The tests force the config keys per case, rather than relying on which table entries are enabled.
rtolof 4 bf16 ULP, and anatolof 1 ULP of the output scale for cancellation residue. The NaN pattern has to match exactly.context_len;Validated on gfx950 (MI350X) with the CI toolchain (torch 2.10, Triton 3.8): all 59 split-loop tests pass.
A note on
num_stagesfor whoever tunes the tableThis measures current
mainon the CI toolchain: gfx950, 64/8 heads, d=64, sinks, 4096 query tokens,backend="triton". On this stacknum_stages=2is faster than 1 at every context length, for both the stock and the split loop:Three things follow from this:
backend="triton", softcap / ALiBi / qq-bias, head_size > 256, or mismatched q/KV dtypes.🤖 Generated with Claude Code