[Bugfix][DSv4.1] Skip SWA bounded replay for KV loads that carry the window (#59197)
Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Codex
Claude Opus 5.5
parent
d169c5a903
commit
ef63c23d35
@@ -67,8 +67,12 @@ class AuxOutputSchedulerConnector:
|
||||
block_hashes_by_request[request_id] = packed
|
||||
self._sent_hash_counts[request_id] = len(request.block_hashes)
|
||||
assert request.sampling_params is not None
|
||||
# A P/D prefiller may cut the prompt short of the requested start.
|
||||
scheduled_requests[request_id] = max(
|
||||
request.sampling_params.routed_experts_prompt_start,
|
||||
min(
|
||||
request.sampling_params.routed_experts_prompt_start,
|
||||
request.num_prompt_tokens,
|
||||
),
|
||||
0 if request.num_output_tokens == 0 else request.num_tokens - 1,
|
||||
)
|
||||
# A settled token can complete a hash block after the next async schedule
|
||||
|
||||
@@ -897,6 +897,10 @@ class MooncakeConnectorScheduler:
|
||||
# GDN is represented as a MambaSpec in vLLM. This Mooncake MambaSpec
|
||||
# path is currently tested with GDN; Mamba2 is not validated yet.
|
||||
self._has_mamba = kv_cache_config.has_mamba_layers
|
||||
self._bounded_replay = any(
|
||||
g.kv_cache_spec.prefix_replay_tokens > 0
|
||||
for g in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
|
||||
# Requests that need to start recv/send.
|
||||
# New requests are added by update_state_after_alloc in
|
||||
@@ -941,8 +945,10 @@ class MooncakeConnectorScheduler:
|
||||
|
||||
def _get_remote_prefill_token_count(self, num_prompt_tokens: int) -> int:
|
||||
"""D-side only. Returns N-1 for Mamba models since the decoder
|
||||
always recomputes the last token and must start from h(N-1)."""
|
||||
if self._has_mamba and num_prompt_tokens > 1:
|
||||
always recomputes the last token and must start from h(N-1), and for
|
||||
DSV41 SWA bounded replay, whose window is not replayed after a P/D load
|
||||
and must be laid out for that token."""
|
||||
if (self._has_mamba or self._bounded_replay) and num_prompt_tokens > 1:
|
||||
return num_prompt_tokens - 1
|
||||
return num_prompt_tokens
|
||||
|
||||
@@ -958,6 +964,9 @@ class MooncakeConnectorScheduler:
|
||||
params is not None
|
||||
and not params.get("_p_side_truncated")
|
||||
and request.num_prompt_tokens > 1
|
||||
# A request that skips reading the prefix cache (prompt logprobs)
|
||||
# loads nothing on the decoder, which recomputes its whole prompt.
|
||||
and not request.get_skip_reading_prefix_cache()
|
||||
):
|
||||
if request.prompt_token_ids is not None:
|
||||
request.prompt_token_ids.pop()
|
||||
@@ -973,7 +982,11 @@ class MooncakeConnectorScheduler:
|
||||
|
||||
def on_new_request(self, request: "Request") -> None:
|
||||
params = request.kv_transfer_params
|
||||
if params is not None and params.get("do_remote_decode") and self._has_mamba:
|
||||
if (
|
||||
params is not None
|
||||
and params.get("do_remote_decode")
|
||||
and (self._has_mamba or self._bounded_replay)
|
||||
):
|
||||
self._truncate_mamba_request_for_prefill(request)
|
||||
|
||||
def get_num_new_matched_tokens(
|
||||
|
||||
@@ -101,6 +101,10 @@ class NixlBaseConnectorScheduler:
|
||||
)
|
||||
)
|
||||
self._has_mamba = kv_cache_config.has_mamba_layers
|
||||
self._bounded_replay = any(
|
||||
g.kv_cache_spec.prefix_replay_tokens > 0
|
||||
for g in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
|
||||
logger.info("Initializing NIXL Scheduler %s", engine_id)
|
||||
if vllm_config.scheduler_config.disable_hybrid_kv_cache_manager:
|
||||
@@ -376,14 +380,16 @@ class NixlBaseConnectorScheduler:
|
||||
"""Trailing prompt tokens the prefiller must not compute; the decoder
|
||||
recomputes them locally.
|
||||
|
||||
Mamba needs h(N-1) so the decoder can derive h(N) itself. Multi-module
|
||||
Mamba needs h(N-1) so the decoder can derive h(N) itself. DSV41's sliding
|
||||
window under bounded replay is not replayed after a P/D load, so it must
|
||||
be laid out for the token the decoder recomputes. Multi-module
|
||||
MTP needs to keep its whole lookahead window off of the prefiller, which
|
||||
would otherwise embed the unverified drafts in the MTP layer's KV cache. The
|
||||
decoder would never rebuild them, because the update is sized by the rejection
|
||||
count, which is zero for the first decode.
|
||||
"""
|
||||
return max(
|
||||
1 if self._has_mamba else 0,
|
||||
1 if self._has_mamba or self._bounded_replay else 0,
|
||||
self.vllm_config.num_prefill_lookahead_tokens - 1,
|
||||
)
|
||||
|
||||
@@ -412,6 +418,9 @@ class NixlBaseConnectorScheduler:
|
||||
# Guard against repeated truncation after preemption/reschedule.
|
||||
and not params.get("_p_side_truncated")
|
||||
and request.num_prompt_tokens > backoff
|
||||
# A request that skips reading the prefix cache (prompt logprobs)
|
||||
# loads nothing on the decoder, which recomputes its whole prompt.
|
||||
and not request.get_skip_reading_prefix_cache()
|
||||
):
|
||||
if request.prompt_token_ids is not None:
|
||||
del request.prompt_token_ids[-backoff:]
|
||||
|
||||
@@ -290,10 +290,14 @@ class Scheduler(SchedulerInterface):
|
||||
# DSV41 SWA bounded replay: groups that declare a replay window are rebuilt
|
||||
# after a prefix hit by recomputing its trailing tokens. One window
|
||||
# for all such groups, so the rewind matches every group's allocation.
|
||||
replay_windows = {
|
||||
group.kv_cache_spec.prefix_replay_tokens
|
||||
for group in kv_cache_config.kv_cache_groups
|
||||
self.prefix_replay_group_ids = tuple(
|
||||
group_id
|
||||
for group_id, group in enumerate(kv_cache_config.kv_cache_groups)
|
||||
if group.kv_cache_spec.prefix_replay_tokens > 0
|
||||
)
|
||||
replay_windows = {
|
||||
kv_cache_config.kv_cache_groups[group_id].kv_cache_spec.prefix_replay_tokens
|
||||
for group_id in self.prefix_replay_group_ids
|
||||
}
|
||||
assert len(replay_windows) <= 1, (
|
||||
f"Prefix replay windows should agree: {sorted(replay_windows)}"
|
||||
@@ -973,14 +977,17 @@ class Scheduler(SchedulerInterface):
|
||||
skip_request(request_queue)
|
||||
continue
|
||||
|
||||
if self.prefix_replay_tokens:
|
||||
# SWA bounded replay recomputes the hit's last
|
||||
if self.prefix_replay_tokens and not (
|
||||
ext_tokens and self._load_restores_replay_window(request)
|
||||
):
|
||||
# DSV41 SWA bounded replay recomputes the hit's last
|
||||
# window, from the block holding its first
|
||||
# token; the sliding-window groups retire
|
||||
# whole blocks below the window of the hit's
|
||||
# next token. Hits end on a block boundary, or
|
||||
# a hit ending one token short of one would
|
||||
# retire the block the replay starts in.
|
||||
# retire the block the replay starts in. A load
|
||||
# that carries the window replays nothing.
|
||||
ext_tokens -= ext_tokens % self.block_size
|
||||
load_kv_async = load_kv_async and ext_tokens > 0
|
||||
|
||||
@@ -1026,9 +1033,17 @@ class Scheduler(SchedulerInterface):
|
||||
num_new_local_computed_tokens + num_external_computed_tokens
|
||||
)
|
||||
assert num_computed_tokens <= request.num_tokens
|
||||
if 0 < num_computed_tokens <= self.prefix_replay_tokens:
|
||||
# SWA bounded replay: a hit no longer than the replayed
|
||||
# window would be recomputed in full and save nothing.
|
||||
# DSV41 SWA bounded replay: a KV load that carries the
|
||||
# window is taken as is; any other hit no longer than the
|
||||
# replayed window would be recomputed in full and save
|
||||
# nothing.
|
||||
window_loaded = bool(
|
||||
num_external_computed_tokens
|
||||
) and self._load_restores_replay_window(request)
|
||||
if (
|
||||
not window_loaded
|
||||
and 0 < num_computed_tokens <= self.prefix_replay_tokens
|
||||
):
|
||||
new_computed_blocks = (
|
||||
self.kv_cache_manager.empty_kv_cache_blocks
|
||||
)
|
||||
@@ -1091,7 +1106,7 @@ class Scheduler(SchedulerInterface):
|
||||
if did_prefix_cache_lookup:
|
||||
# Fresh admission: local and/or sync external hit.
|
||||
num_replay_tokens = self._mark_prefix_replay(
|
||||
request, num_computed_tokens
|
||||
request, num_computed_tokens, window_loaded
|
||||
)
|
||||
num_computed_tokens -= num_replay_tokens
|
||||
# Number of tokens to be scheduled.
|
||||
@@ -3083,16 +3098,31 @@ class Scheduler(SchedulerInterface):
|
||||
self._request_remaining_blocks(req) for req in self._inflight_prefills
|
||||
)
|
||||
|
||||
def _mark_prefix_replay(self, request: Request, num_hit_tokens: int) -> int:
|
||||
def _load_restores_replay_window(self, request: Request) -> bool:
|
||||
"""Whether the KV connector's load for ``request`` restores every DSV41
|
||||
SWA bounded-replay group, as a P/D transfer of the request's own blocks
|
||||
does; a prefix-cache store holds no window."""
|
||||
assert self.connector is not None
|
||||
return bool(self.prefix_replay_group_ids) and set(
|
||||
self.prefix_replay_group_ids
|
||||
).issubset(self.connector.get_loaded_kv_cache_group_ids(request))
|
||||
|
||||
def _mark_prefix_replay(
|
||||
self, request: Request, num_hit_tokens: int, window_loaded: bool
|
||||
) -> int:
|
||||
"""Record where a prefix hit's replay starts and return the number of
|
||||
hit tokens to recompute; the caller rewinds the computed count by it.
|
||||
|
||||
The replayed tokens are the hit's last window (see
|
||||
``Request.replay_start``): the worker rebuilds their sliding-window KV
|
||||
and leaves their cached KV alone.
|
||||
and leaves their cached KV alone. A hit whose KV load carries the
|
||||
window replays nothing.
|
||||
"""
|
||||
if not self.prefix_replay_tokens:
|
||||
return 0
|
||||
if window_loaded:
|
||||
request.replay_start = 0
|
||||
return 0
|
||||
num_replay_tokens = min(self.prefix_replay_tokens, num_hit_tokens)
|
||||
request.replay_start = num_hit_tokens - num_replay_tokens
|
||||
return num_replay_tokens
|
||||
@@ -3106,7 +3136,8 @@ class Scheduler(SchedulerInterface):
|
||||
"""
|
||||
assert self.connector is not None
|
||||
|
||||
if request.request_id in self.failed_recving_kv_req_ids:
|
||||
load_failed = request.request_id in self.failed_recving_kv_req_ids
|
||||
if load_failed:
|
||||
# Request had KV load failures; num_computed_tokens was already
|
||||
# updated in _update_requests_with_invalid_blocks
|
||||
if request.num_computed_tokens:
|
||||
@@ -3132,11 +3163,15 @@ class Scheduler(SchedulerInterface):
|
||||
# This will cache the blocks iff caching is enabled.
|
||||
self.kv_cache_manager.cache_blocks(request, request.num_computed_tokens)
|
||||
|
||||
# SWA bounded replay recomputes the tail of the hit, which covers the
|
||||
# last token; otherwise a full prompt hit re-computes that token so the
|
||||
# next one can be sampled.
|
||||
# DSV41 SWA bounded replay recomputes the tail of a hit loaded without
|
||||
# its window, or of a failed load, which may have lost it; the replay
|
||||
# covers the last token. Otherwise a full prompt hit re-computes that
|
||||
# token so the next one can be sampled.
|
||||
num_replay_tokens = self._mark_prefix_replay(
|
||||
request, request.num_computed_tokens
|
||||
request,
|
||||
request.num_computed_tokens,
|
||||
window_loaded=not load_failed
|
||||
and self._load_restores_replay_window(request),
|
||||
)
|
||||
if num_replay_tokens > 0:
|
||||
request.num_computed_tokens -= num_replay_tokens
|
||||
|
||||
Reference in New Issue
Block a user