Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions python/sglang/kernels/ops/attention/extend_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,19 @@ def _fwd_kernel(
e_max = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")

prefix_end = 0 if SKIP_PREFIX else cur_seq_len_prefix
for start_n in range(0, prefix_end, BLOCK_N):
# The extend stage's floor, with q at its absolute position
# (cur_seq_len_prefix + cur_block_m * BLOCK_M) because kv is indexed within the
# prefix; tight for any BLOCK_M/BLOCK_N, and SKIP_TILE already made the skipped
# tiles no-ops. Past the first window's m-blocks no query reaches the prefix.
prefix_start = 0
if SLIDING_WINDOW_SIZE > 0:
prefix_start = (
tl.maximum(
cur_seq_len_prefix + cur_block_m * BLOCK_M - SLIDING_WINDOW_SIZE, 0
)
// BLOCK_N
) * BLOCK_N
for start_n in range(prefix_start, prefix_end, BLOCK_N):
start_n = tl.multiple_of(start_n, BLOCK_N)
mask_n = (start_n + offs_n) < cur_seq_len_prefix

Expand Down Expand Up @@ -1088,7 +1100,20 @@ def _fwd_kernel_unified(
e_max = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")

# Unified loop: process all KV tokens (prefix + extend)
for start_n in range(0, cur_seq_kv_len, BLOCK_N):
#
# The two-stage kernel's floor. cur_window_start appears on both sides of the
# mask below and cancels, so A = cur_seq_prefix_len + cur_block_m * BLOCK_M -
# SLIDING_WINDOW_SIZE. This loop spans prefix and extend KV alike, so the one
# floor drops the fully-masked tiles of both.
unified_start = 0
if SLIDING_WINDOW_SIZE > 0:
unified_start = (
tl.maximum(
cur_seq_prefix_len + cur_block_m * BLOCK_M - SLIDING_WINDOW_SIZE, 0
)
// BLOCK_N
) * BLOCK_N
for start_n in range(unified_start, cur_seq_kv_len, BLOCK_N):
start_n = tl.multiple_of(start_n, BLOCK_N)
mask_n = (start_n + offs_n) < cur_seq_kv_len

Expand Down
Loading