[Fix] deepseek-v4.1: Serve prompt-logprob requests under decoder SWA bounded replay (#44850)

Co-authored-by: Pedram Ghazi <pedram.ghazi@amd.com>
Co-authored-by: David Orman <ormandj@corenode.com>
This commit is contained in:
Liangsheng Yin
2026-10-17 01:25:33 -07:00
committed by GitHub
co-authored by Pedram Ghazi David Orman
parent 7a8804edb5
commit a2953d7b7f
12 changed files with 376 additions and 25 deletions
@@ -437,18 +437,44 @@ class BuildCausalSwaPageIndices:
)
def late_layer_tail_lens(
extend_lens_cpu: list[int],
tail_len: int,
whole_extend: Optional[list[bool]] = None,
) -> list[int]:
"""Each request's late-layer tail: its last min(tail_len, extend_len) tokens,
or its whole extend where whole_extend is set."""
if whole_extend is None:
return [min(tail_len, n) for n in extend_lens_cpu]
return [
n if whole else min(tail_len, n)
for n, whole in zip(extend_lens_cpu, whole_extend)
]
def late_layer_tail_layout(
*,
extend_lens_cpu: list[int],
seq_lens_cpu: list[int],
tail_len: int,
device: torch.device,
whole_extend: Optional[list[bool]] = None,
) -> tuple[torch.Tensor, list[int], torch.Tensor]:
"""Tail rows of each prefill extend: its last min(tail_len, extend_len) tokens.
"""Tail rows of each prefill extend: its last min(tail_len, extend_len) tokens,
or its whole extend where whole_extend is set.
Returns (token indices into the extend, per-request tail lengths, per-row
absolute window floor). The floor is seq_len - tail_len, so a tail shorter
than tail_len reads rows the request wrote before this extend."""
tail_lens_cpu = [min(tail_len, n) for n in extend_lens_cpu]
than tail_len reads rows the request wrote before this extend. A whole
extend keeps every row's plain window."""
if whole_extend is not None and any(whole_extend):
return _whole_extend_tail_layout(
extend_lens_cpu=extend_lens_cpu,
seq_lens_cpu=seq_lens_cpu,
tail_len=tail_len,
device=device,
whole_extend=whole_extend,
)
tail_lens_cpu = late_layer_tail_lens(extend_lens_cpu, tail_len)
if len(extend_lens_cpu) == 1:
n, t, s = extend_lens_cpu[0], tail_lens_cpu[0], seq_lens_cpu[0]
floor = torch.full((t,), max(0, s - tail_len), dtype=torch.int32, device=device)
@@ -469,6 +495,32 @@ def late_layer_tail_layout(
return token_indices, tail_lens_cpu, floor
def _whole_extend_tail_layout(
*,
extend_lens_cpu: list[int],
seq_lens_cpu: list[int],
tail_len: int,
device: torch.device,
whole_extend: list[bool],
) -> tuple[torch.Tensor, list[int], torch.Tensor]:
tail_lens_cpu = late_layer_tail_lens(extend_lens_cpu, tail_len, whole_extend)
token_indices, floor, start = [], [], 0
for n, t, s, whole in zip(
extend_lens_cpu, tail_lens_cpu, seq_lens_cpu, whole_extend
):
token_indices.append(torch.arange(start + n - t, start + n))
# A whole extend's rows sit at s - t or later, so its floor never cuts
# their plain window.
row_floor = max(0, s - t - tail_len) if whole else max(0, s - tail_len)
floor.append(torch.full((t,), row_floor, dtype=torch.int32))
start += n
return (
torch.cat(token_indices).to(device, non_blocking=True),
tail_lens_cpu,
torch.cat(floor).to(device, non_blocking=True),
)
def build_causal_swa_page_indices(
*,
req_to_token: torch.Tensor,