From 4c5ed0597eefac21948348730e63374dcea4ffbf Mon Sep 17 00:00:00 2001 From: jiaqiang-dot-liu Date: Tue, 13 Oct 2026 22:40:53 +0800 Subject: [PATCH] Skip fully-masked KV tiles in sliding-window extend attention (#39769) Co-authored-by: Cursor --- .../kernels/ops/attention/extend_attention.py | 11 +++- .../attention/unittests/swa/test_triton.py | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/python/sglang/kernels/ops/attention/extend_attention.py b/python/sglang/kernels/ops/attention/extend_attention.py index cc88e4aad8..dd37f56111 100644 --- a/python/sglang/kernels/ops/attention/extend_attention.py +++ b/python/sglang/kernels/ops/attention/extend_attention.py @@ -722,7 +722,16 @@ def _fwd_kernel( else tl.minimum(cur_seq_len_extend, (cur_block_m + 1) * BLOCK_M) ) extend_end = 0 if SKIP_EXTEND else cur_block_m_end - for start_n in range(0, extend_end, BLOCK_N): + # Causal or not, the mask below keeps (q, kv) only if q <= kv + SLIDING_WINDOW_SIZE, + # so no tile under this floor holds an unmasked element -- tight for any + # BLOCK_M/BLOCK_N. SKIP_TILE already zeroes them; bounding the loop also drops + # their cross-wave tl.max. + extend_start = 0 + if SLIDING_WINDOW_SIZE > 0: + extend_start = ( + tl.maximum(cur_block_m * BLOCK_M - SLIDING_WINDOW_SIZE, 0) // BLOCK_N + ) * BLOCK_N + for start_n in range(extend_start, extend_end, BLOCK_N): start_n = tl.multiple_of(start_n, BLOCK_N) mask_n = (start_n + offs_n) < cur_block_m_end diff --git a/test/registered/attention/unittests/swa/test_triton.py b/test/registered/attention/unittests/swa/test_triton.py index aeeb84c085..209d6b3af1 100644 --- a/test/registered/attention/unittests/swa/test_triton.py +++ b/test/registered/attention/unittests/swa/test_triton.py @@ -252,6 +252,60 @@ class TestTritonSWAAttentionBackendCorrectness(CustomTestCase): with self.subTest(case=case.name, backend=case.backend): run_dense_attention_case(self, case) + # Every other SWA case here keeps the extend length under BLOCK_M (64 or + # 128 on the architectures we build for), so `cur_block_m` is always 0 and + # the extend-KV loop always starts at tile 0. The kernel bounds that loop + # below by the sliding-window floor, and that bound is only reachable once + # a query tile starts past the window. These lengths are therefore load + # bearing: they must stay above BLOCK_M with a window well below it, or + # the bound goes unexercised and a wrong or missing one stays green. + MULTI_TILE_SWA_CASES = ( + DenseAttentionCase( + name="swa_extend_multi_tile_no_prefix", + backend="triton", + forward_mode=ForwardMode.EXTEND, + num_heads=8, + num_kv_heads=4, + page_size=16, + prefix_lens=(0, 0), + extend_lens=(384, 321), + sliding_window_size=24, + ), + # Non-empty prefix so the prefix sweep runs too, and a window that is + # not a multiple of any BLOCK_N we use, which is where rounding the + # floor down to a tile boundary would show up. + DenseAttentionCase( + name="swa_extend_multi_tile_with_prefix", + backend="triton", + forward_mode=ForwardMode.EXTEND, + num_heads=8, + num_kv_heads=4, + page_size=16, + prefix_lens=(48, 130), + extend_lens=(384, 300), + sliding_window_size=72, + ), + # Window exactly on a tile boundary: the floor lands on a tile start, + # so an off-by-one in the bound drops a tile that still contributes. + DenseAttentionCase( + name="swa_extend_multi_tile_window_tile_aligned", + backend="triton", + forward_mode=ForwardMode.EXTEND, + num_heads=8, + num_kv_heads=4, + page_size=16, + prefix_lens=(0,), + extend_lens=(448,), + sliding_window_size=128, + ), + ) + + def test_multi_tile_swa_extend_cases(self): + for case in self.MULTI_TILE_SWA_CASES: + with self.subTest(case=case.name, backend=case.backend): + # The kit defaults to 64, which these lengths exceed. + run_dense_attention_case(self, case, max_context_len=512) + # Layout-robustness. See dense/test_triton.py for full rationale. # The default `shuffled_pages` layout is already exercised by # test_projected_swa_attention_cases; this method opts into the