Repository navigation
[Triton/Gluon] Window-aware split-KV for sliding-window attention - #6899
okorzh-amd wants to merge 5 commits into
Conversation
On gfx950 a sliding-window call with q > 1 never takes the split grid (_gfx950_split_grid returns False, and kv_split["SW"] caps MAX_SEGMENTS at 1), so a spec-decode verify on a sliding layer runs num_kv_heads programs per sequence over the whole window. Splitting it only pays once the KV range is cut to the window: the split carves seq_len, so over a full context the window lands in one or two segments and the rest are empty (31.9-37.6 us vs 29 us unsplit when forced). Split only when the KV range is bounded by the window. Row i of a q-row sequence of length L reads keys from L - q + i - (SW - 1) on, so a block table trimmed to whole pages holds at most SW + q - 1 + block_size - 1 keys. The new SWB config axis is sliding_window_bounds_kv(): q > 1 and max_seqlen_k <= sliding_window + max_seqlen_q + block_size - 2. A batch whose contexts are all that short is bounded as well, trimmed or not, and takes the same path. _gfx950_split_grid lets verify-sized SWB calls (max_seqlen_q <= 16 and max_seqlen_q * num_queries_per_kv <= 128) through and then decides like any other batch (NUM_SEGMENTS > 1). Longer queries (prefill chunks) keep the 2d grid: with q rows per sequence in the reduce, MHA q = 128 at window 256-512 ran up to 1.33x slower split. The gfx950 gluon kv_split table gains an "SW.SWB" entry, the SW entry without its segment cap, plus: - SPLIT_FIT_BUDGET (new): the largest power-of-two split that keeps the launch within SEGMENTS_PER_CU (1) programs per CU, sized by the real one-block-per-sequence query grid (_gfx950_split_params). The default ceil-rounded share splits 2 ways up to B = 51 and loses 30-40% from B = 36 (39.7-43.0 us vs 30.4-30.8 us on the 2d grid). - SPLIT_KV_WINDOW (new): count the KV tiles over the sliding_window + max_seqlen_q - 1 keys the rows can see, not the page slack of a trimmed table, so SPLIT_MIN_TILES 4 keeps window 128 unsplit at block 128 too (64 q / 8 kv heads, q = 4: 8.2 us split vs 6.7 us 2d). - SPLIT_SEQS_PER_TILE 1.5 (new): no split once num_seqs exceeds 1.5 x the KV tiles. The triton reduce_segments pass that every gfx950 split pays costs more with the batch (7.6 us at B = 32, 2 segments), while the 2d kernel's time follows the KV length, so at windows 256-1024 a split at B = 12-32 ran up to 1.87x slower than the 2d grid. Kernel: a split whose tiles all fall before the window start stores an empty partial, and split 0 is the only one that counts the attention sink, so a window starting past tile 0 (block size > 64, or a shorter row in an untrimmed bounded batch) dropped the sink from the softmax. The empty-partial path of split 0 now stores M = sink, L = 1. Unchanged dispatch: SW calls with a context past the bound, SW decode (q == 1 keeps the cap), q > 16, window 128, full attention, gfx1250 and the triton backend. Of 328,800 no-launch shapes (window 128-32768, q 1-128, B 1-128, 5 head layouts, D 64-256, bf16/fp8, block 16-256, ctx bound -300..+5000) only gluon SW calls with 1 < q <= 16 inside the bound change grid (22,392). MI355X, 16 q / 2 kv heads, head 64, bf16, block 16, window 2048, q = 8, ctx 32768 (us per call, CUDA-graph replay, natural dispatch): B 1 2 4 8 16 32 48 64 full 29.0 29.0 29.1 29.5 30.0 30.5 30.6 30.9 trimmed 6.7 7.2 8.6 10.9 15.8 25.1 30.6 30.8 segments 64 32 16 8 4 2 2d 2d Full-attention floor at 2056 keys: 6.6 / 6.8 / 8.1 / 9.4 / 14.4 / 21.9. The natural pick matches the fastest forced count (1-128) at every B. Shorter windows, trimmed, same heads (base 2d -> split, us): W 512: B4 9.78 -> 6.79, B8 9.79 -> 7.61, B12 9.82 -> 9.19, B16+ 2d W 1024: B4 16.30 -> 7.66, B8 16.35 -> 8.61, B16 16.71 -> 12.27, B32 2d W 256: B4 6.54 -> 6.30, B8+ 2d Known cost: a caller that captures a graph at max_seqlen_k = window bound and replays it for batches whose real contexts are a few hundred keys pays the split + reduce where the 2d kernel was cheaper (B >= 12, ctx <= 1000). A cheaper gfx950 reduce would remove that. Test: test_triton_unified_attn_sw_verify_split (full and trimmed tables vs reference with a dominant sink, exact segment counts: trimmed verify splits, full context stays 2d, SW decode stays 1 segment, q = 17 and q = 128 prefill chunks stay 2d, block 128 and an untrimmed bounded batch leave split 0 empty) and test_unified_attn_sw_kv_split_config (CPU-only kv_split picks at the bound edge, untrimmed, decode, window 128, and the batch sizes where the split stops). Signed-off-by: Oxana Korzh <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. The SPLIT_KV_WINDOW knob was set only in the SW.SWB entry, so it configured nothing. compute_segment_params now counts KV tiles over the sliding_window + max_seqlen_q - 1 keys the rows can see whenever sliding_window_bounds_kv() holds, with no config switch. The clamp is gated on the bound, not on sliding_window > 0. The split carves the whole seq_len, so on an untrimmed long context a window-sized count leaves most segments idle. No unbounded sliding-window call reaches a split lookup today, so the two gates pick the same grid. CPU dispatch sweep vs the previous commit, 2,048,256 shapes per arch on gfx950 (gluon and triton), gfx1250, gfx1100 and gfx1201: no 2d/3d, NUM_SEGMENTS or TILE_SIZE change. 1,184 gfx950 gluon shapes change only the triton reduce num_warps (head 256, block >= 64, window + q - 1 of about 257-512), since the reduce lookup now sees the same clamped length. test_unified_attn_sw_kv_split_config: 11 passed on CPU; removing the clamp fails the window 128, block 128 case. Signed-off-by: Oxana Korzh <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-up. The SW axis was a bool, so a table could not target a window size. SW is now "opt_num": the keys a row can see, min(sliding_window, max_seqlen_k), floored at 1, and 0 with no window. SW_EQ/LEQ/GEQ_<n> keys pick by size, a bare SW matches any window, and no window matches only "any". Tables that only use a bare SW key (gfx950 gluon and triton, gfx1250 gluon, gfx1201 triton) resolve as before. _index rejects a table that mixes SW_LEQ and SW_GEQ keys, because every matching LEQ key would outrank every GEQ key. It is cached on the key names, so the check runs once per table. The gfx950 gluon kv_split entry SW.SWB becomes SW_GEQ_256.SWB. Bare SW, with MAX_SEGMENTS 1, stays the fallback for every other window and for SW decode. 256 keys is 4 tiles of 64, the smallest window measured to win. Window 128 now falls back through the threshold, not SPLIT_MIN_TILES. SWB stays: an untrimmed long context also gives SW = window, and splitting that table was measured slower. CPU dispatch sweep vs the previous commit, 3,692,160 shapes per arch (grid, NUM_SEGMENTS, reduce config, and attn_2d/attn_3d configs for both backends) on gfx950, gfx1250, gfx1100 and gfx1201: the only change is 6,300 gfx950 gluon shapes at window 255 (q 4-16, just under the threshold), split -> 2d. Windows above 2048 still split; their perf is not measured. Tests: test_unified_attn_sw_kv_split_config gains window 255 vs 256 at the threshold and window 256 at block 128 (the clamp case). test_unified_attn_sw_axis_lookup covers the candidate order, the axis value and its floor, and the LEQ/GEQ guard. 15 passed on CPU. Each of these mutants fails at least one test: threshold, clamp, guard, bare-SW fallback, the no-window check, min(), and the floor. Signed-off-by: Oxana Korzh <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
Review follow-up (Copilot). A sliding-window split over a bounded table whose page is much longer than a tile can leave 32 or more empty leading segments, which store M = -inf. One example is head 256, q 8, KV 10246, window 2048, block 8192, 64 segments. At head 256 reduce_segments merges in SEGMENT_BLOCK = 32 chunks. Without sinks the first chunk then has no finite max, and exp2(-inf - -inf) turned alpha and weight into NaN for the whole row. With sinks, split 0 stores the sink and hides it. Rescale an all-empty chunk from 0 instead, so alpha and weight are 0. A chunk with a finite max computes exactly as before, and the single-chunk reduce (head <= 128) compiles to the same instructions. Under TRITON_INTERPRET=1, reduce_segments on the partials that both Copilot shapes leave gives NaN on every row with 32+ leading empty segments before this change. After it, no NaN, within 6e-8 of the single-chunk reduce. test_triton_unified_attn_sw_split_no_sinks runs both shapes on gfx950 (head 256, no sinks) and checks the 64-segment split and the output. Signed-off-by: Oxana Korzh <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
With a sliding window, every split-KV launch carved [0, seq_len) into NUM_SEGMENTS tile ranges. Over an untrimmed long context the window lands in one or two segments and the rest are empty. That is why ROCm#6889 split only window-bounded (trimmed) tables. Carve only the window instead. For SLIDING_WINDOW > 0, each sequence splits the tiles [tile_lo, cdiv(seq_len, TILE_SIZE)), where tile_lo is the tile of row 0's first visible key, max(context_len - SLIDING_WINDOW + 1, 0) // TILE_SIZE. It is per sequence, so every query block and the reduce agree. The gfx950 gluon split, the triton kernel_unified_attention_3d and reduce_segments all carve this way (the triton pair shares split_window_tiles). reduce_segments gets SLIDING_WINDOW from the launch. The gfx1250 3d wrapper asserts no window, since its split still carves seq_len and use_2d_kernel never sends a window there. With no window, all three kernels compile to the same gfx950 instructions as before. Dispatch: an untrimmed table now splits like a trimmed one. _gfx950_split_grid drops the bound check and keeps the q <= 16 and q * num_queries_per_kv <= 128 gates. The kv_len clamp applies to every sliding-window call. The SWB axis and sliding_window_bounds_kv are gone, and the split entry is SW_GEQ_256.Q_GEQ_2, so sliding-window decode stays on the capped bare SW entry. CPU dispatch sweep vs the parent commit, 3,692,160 shapes per arch: on gfx950 the only change is 75,220 gluon sliding-window calls with 1 < q <= 16 past the old bound going 2d -> split. gfx1250 and gfx1100 do not change. Tests: the verify-split grids for full tables become splits, and a whole-tile trim must give bit-identical output. The new test_triton_unified_attn_sw_split_carve forces gluon and triton splits with NaN-poisoned partials over verify, multi-query-block and decode batches. It checks the output and that exactly the window carve's segments are written. CPU config rows cover untrimmed contexts and the capped decode. GPU runs are pending. Signed-off-by: Oxana Korzh <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
fd3d75a to
0733d76
Compare
|
Closing: per review, the window-aware split is now folded directly into #6889 (same source change), so this follow-up isn't needed. |
Motivation
Follow-up to #6889, as suggested in its review: do the split and the reduce in a window-aware way.
#6889 splits a sliding-window spec-decode verify on gfx950 only when
max_seqlen_kis within the window bound,window + q + block_size - 2. That means a block table trimmed to the window, or a batch whose contexts are all that short. Stock vLLM does not trim. Only a vLLM fork branch does (okorzh-amd/vllmokorzh/rocm-aiter-fa-swa-verify-trim, behindVLLM_ROCM_AITER_FA_SWA_VERIFY_TRIM=1).The bound exists because every split-KV launch divides
[0, seq_len)intoNUM_SEGMENTStile ranges. Over an untrimmed long context only the few segments that overlap the window do work, and the rest are empty. For W 2048, q 8 that is 5 of 64 segments at ctx 32768 and 1 of 64 at ctx 200000. #6889 measured it: forcing the split over a full table took 12.3-37.6 us, against 29.0-30.5 us unsplit.This PR carves only the window, so a full table gets the same split as a trimmed one.
Stacked on #6889. Only the last commit (
fd3d75a884) is new. The diff will shrink to that commit once #6889 merges.Changes
The carve, for
SLIDING_WINDOW > 0: no row of a sequence can see a key before row 0's first one,context_len - SLIDING_WINDOW + 1. Each sequence therefore splits only the tiles[tile_lo, cdiv(seq_len, TILE_SIZE)), withtile_lo = max(context_len - SLIDING_WINDOW + 1, 0) // TILE_SIZE. Thentiles_per_split = cdiv(window_tiles, NUM_SEGMENTS), and splitsstarts attile_lo + s * tiles_per_split.seq_len,query_len), so every query block of a sequence and the reduce agree.TILE_SIZEis 64 on every gfx950 sliding-window split (thekv_splitentries pin it).tile_lois 0 for any context up towindow + 62, which includes every context shorter than the window. Then the carve is exactly the old one.Per file:
aiter/ops/triton/_gluon_kernels/gfx950/attention/unified_attention.py:AttentionProgram.initializecarves by window whenSLIDING_WINDOW > 0. The no-window branch is unchanged. The empty-split store and [Triton/Gluon] Window-aware split-KV for sliding-window verify on gfx950 #6889's split-0 sink fix stay as they are. The store still fires exactly for the segments the reduce reads.aiter/ops/triton/_triton_kernels/attention/unified_attention.py:split_window_tileshelper giveskernel_unified_attention_3dandreduce_segmentsthe same window tile range. Each kernel still derivestiles_per_segmentitself, and the gluon kernel keeps its own copy of the carve.kernel_unified_attention_3dcarves by window whenSLIDING_WINDOW > 0. It is unreachable with a window today, but it pairs with the same reduce.reduce_segmentsgetsSLIDING_WINDOW: tl.constexpr = 0. With a window it readsquery_lenfromcu_seqlens_qand computes the same carve to mask the unwritten segments.aiter/ops/triton/attention/unified_attention.py:_reduce_segments_tritonpassesSLIDING_WINDOW=params.sliding_window._unified_attention_3d_gfx1250assertssliding_window <= 0. Its split still carves[0, seq_len), anduse_2d_kernelnever sends a sliding-window call there._gfx950_split_gridno longer requiresmax_seqlen_kto be within the window bound. Theq <= 16andq * num_queries_per_kv <= 128gates stay.skip_reducereturn documents that sliding-window partials follow the window carve.aiter/ops/triton/utils/unified_attention_utils.py: thekv_len = min(max_seqlen_k, sliding_window + max_seqlen_q - 1)clamp incompute_segment_paramsapplies to every sliding-window lookup. It now matches what the split covers, to within one tile (see Known limitations). It now also reaches decode and the non-gluon tables, with no effect: decode is capped at one segment, and those backends never split with a window.sliding_window_bounds_kvand theSWBaxis are removed.aiter/ops/triton/configs/gfx950/gluon/attention/unified_attention/DEFAULT.json:SWBleaves thekv_splitschema, andSW_GEQ_256.SWBbecomesSW_GEQ_256.Q_GEQ_2with the same values. Sliding-window decode (q == 1) still lands on the bareSWentry,MAX_SEGMENTS 1.op_tests/triton_tests/attention/test_unified_attention.py:test_triton_unified_attn_sw_verify_split: the two full-table rows now expect the same split as their trimmed tables ([32, 32],[64, 64]). When the trim is a whole number of tiles, full and trimmed outputs must be bit-identical. Of the changed rows, that applies to the block-128 one.test_triton_unified_attn_sw_split_carve(24 cases, GPU). It forces a split on the gluon and the triton producer, with partial buffers filled with NaN. It runs over verify rows, sequences with several query blocks whose window and causal edges fall inside a tile, and a decode-only batch. It checks the output against the reference. Fromsegm_maxit also checks that exactly the segments the window carve gives each sequence are written.test_unified_attn_sw_kv_split_config(CPU): the two rows one key past the old bound and far past it (W 2048, q 8, ctx 2071 and 200000, B 1) now expect 64 segments instead of 1. [Triton/Gluon] Window-aware split-KV for sliding-window verify on gfx950 #6889's "one key past the bound stays unsplit" check is gone by design. The test also gains untrimmed rows and a capped decode row.test_unified_attn_sw_axis_lookup(CPU) now also checks which entry verify and decode land on.What does not change
kernel_unified_attention_3dandreduce_segmentscompile to the same gfx950 instruction stream as before. This was checked for the specialization compiled: 16 q / 2 kv heads, head 64, block 16, bf16, sinks, 8 segments. They were compiled without a GPU and only the debug line info differs.NUM_SEGMENTS,TILE_SIZE, the reduce config and theattn_2d/attn_3dconfigs for both backends. It covers 3,692,160 shapes per arch, base [Triton/Gluon] Window-aware split-KV for sliding-window verify on gfx950 #6889 head (584fe297fa) vs this PR.NUM_SEGMENTS,TILE_SIZEand reduce config.tile_lois 0. With block 128 a trimmed table can start up to one tile before the window, and with block 256 up to three. The split no longer counts those tiles, so segment boundaries shift. Results can differ by bf16 rounding there (not measured).Test plan / results
pytest -k "sw_kv_split_config or sw_axis_lookup": 19 passed (gfx950 driver faked). Each of these dispatch mutants fails at least one case: decode uncapped, the 256 threshold removed, the clamp removed, the clamp gated on the old bound, the LEQ/GEQ guard removed.kernel_unified_attention_3d+reduce_segmentsunderTRITON_INTERPRET=1: 42/42 within 3.9e-7 of an fp64 sliding-window + sink reference. These were direct fp32 launches with NaN-filled partials, at small shapes. The cases cover decode, several query blocks, more segments than window tiles, sinks on and off, and theSEGMENT_BLOCKreduce path. With the reduce kept on the old carve, 34/42 fail. This covers the triton producer only. The gluon producer has compile-only evidence until the GPU tests run.pytest op_tests/triton_tests/attention/test_unified_attention.py -k sw_(sw_verify_split6 cases,sw_split_carve24 cases).Performance
Not measured yet. The expectation is that an untrimmed verify gets the trimmed-table times from #6889's table. The grid is now the same, and each split covers the same number of window tiles; the same keys only when the trim is a whole number of tiles. From that table (W 2048, q 8, 16 q / 2 kv heads, head 64, bf16, block 16, ctx 32768, measured on an image with #6594), full-table base vs trimmed split:
Planned A/B (MI355X, CUDA-graph replay, base = #6889 head):
max_seqlen_k= 131072 at capture, real ctx 64-2100, B 1-33.SPLIT_SEQS_PER_TILE) for layouts other than 16/2 head 64, which is the layout the factor was fitted on.Known limitations
max_seqlen_kat capture. Before this PR, stock vLLM ran every sliding-window verify whosemax_seqlen_kexceededwindow + q + block_size - 2on the 2d grid. That includes a FULL graph captured at a longmax_seqlen_k.max_seqlen_ksplits at every replay, whatever the real contexts are.skip_reduce. With a window, the partials it returns follow the window carve, not[0, seq_len). No in-repo caller passesskip_reducewith a window. In vLLM the onlyskip_reduceuser is the MLA segmented decode, which has no window.reduce_segmentstakesSLIDING_WINDOWas a constexpr, so a model with several window sizes compiles one reduce variant per window. The producers already specialize on it.compute_segment_paramscountscdiv(window + q - 1, 64)tiles. The carve can span one more whenever the window's first key is not 64-aligned, e.g. W 2048 at q 8 counts 33 where the carve can span 34. That changesNUM_SEGMENTSonly whencdiv(window + q - 1, 64)is a power of two, e.g. W 2047 at q 2 (32 vs 33):tiles_per_splitbecomes 2 and about half the segments are empty. Power-of-two windows at q >= 2 avoid it, and trimmed tables at base had the same count. The sweep did not include such windows.