[BugFix] Handle per-group prefix-hit divergence for hybrid models with KV connector (#48425)
This commit is contained in:
@@ -634,6 +634,15 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
key=lambda g: not isinstance(g.spec, FullAttentionSpec)
|
||||
)
|
||||
|
||||
# Dense reference group for per-group lookups (None when the model
|
||||
# has no full-attention layers): full attention is downward-closed,
|
||||
# so any group reporting a longer per-group hit implies the union of
|
||||
# per-group hits is not consistent at a single boundary (#46453).
|
||||
first = self.attention_groups[0]
|
||||
self.full_attention_group_id: int | None = (
|
||||
first.group_ids[0] if isinstance(first.spec, FullAttentionSpec) else None
|
||||
)
|
||||
|
||||
# Propagate the eagle bit to each manager (default to ``use_eagle=False``).
|
||||
for group in self.attention_groups:
|
||||
if group.use_eagle:
|
||||
|
||||
@@ -9,7 +9,10 @@ from typing import Literal, overload
|
||||
from vllm.distributed.kv_events import BlockStored, KVCacheEvent
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.v1.core.kv_cache_coordinator import get_kv_cache_coordinator
|
||||
from vllm.v1.core.kv_cache_coordinator import (
|
||||
HybridKVCacheCoordinator,
|
||||
get_kv_cache_coordinator,
|
||||
)
|
||||
from vllm.v1.core.kv_cache_metrics import KVCacheMetricsCollector
|
||||
from vllm.v1.core.kv_cache_utils import KVCacheBlock, KVCacheBlockCopy
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
@@ -287,6 +290,53 @@ class KVCacheManager:
|
||||
blocks = self.create_kv_cache_blocks(computed_blocks)
|
||||
return blocks, num_new_computed_tokens, shared_prefix_boundary
|
||||
|
||||
def get_computed_blocks_for_connector(
|
||||
self, request: Request
|
||||
) -> tuple[KVCacheBlocks, int, int, bool]:
|
||||
"""Local prefix-cache lookup for a request scheduled with a KV connector.
|
||||
|
||||
Hybrid (Mamba + full-attention) models can have per-group prefix hits
|
||||
diverge under block pressure: the full-attention tail may be evicted
|
||||
while a deeper Mamba state survives, or vice versa. Report the
|
||||
full-attention hit as the local prefix - the connector transfers the
|
||||
remaining suffix and the Mamba state is transferred unconditionally by
|
||||
nixl's ``_apply_prefix_caching`` - and flag when that hit ran deeper
|
||||
than a lagging group. Such a hit only has a valid Mamba state at its
|
||||
boundary if the connector supplies it, so the caller must fall back to
|
||||
``get_computed_blocks`` to reconcile when no external tokens are found.
|
||||
|
||||
Non-hybrid models and already-convergent hits use ``get_computed_blocks``.
|
||||
|
||||
Returns:
|
||||
The ``get_computed_blocks`` triple (blocks, number of local computed
|
||||
tokens, shared-prefix boundary) plus ``hit_diverged``.
|
||||
"""
|
||||
coordinator = self.coordinator
|
||||
if not (
|
||||
self.kv_cache_config.has_mamba_layers
|
||||
and isinstance(coordinator, HybridKVCacheCoordinator)
|
||||
and coordinator.full_attention_group_id is not None
|
||||
):
|
||||
return *self.get_computed_blocks(request), False
|
||||
|
||||
if not self.prefix_cache_lookup_enabled(request):
|
||||
return self.empty_kv_cache_blocks, 0, 0, False
|
||||
|
||||
fa_group_id = coordinator.full_attention_group_id
|
||||
computed, per_group_hits = coordinator.find_longest_cache_hit_per_group(
|
||||
request.block_hashes, request.num_tokens - 1
|
||||
)
|
||||
if any(hit > per_group_hits[fa_group_id] for hit in per_group_hits):
|
||||
# A lagging group hit deeper than full attention means its
|
||||
# full-attention blocks were evicted; use the reconciled boundary
|
||||
# that every group agrees on.
|
||||
return *self.get_computed_blocks(request), False
|
||||
|
||||
num_local = per_group_hits[fa_group_id]
|
||||
blocks = self.create_kv_cache_blocks(computed)
|
||||
# Per-group lookups do not detect an uncached shared prefix (boundary 0).
|
||||
return blocks, num_local, 0, min(per_group_hits) < num_local
|
||||
|
||||
def allocate_slots(
|
||||
self,
|
||||
request: Request,
|
||||
|
||||
@@ -35,7 +35,6 @@ from vllm.v1.core.encoder_cache_manager import (
|
||||
EncoderCacheManager,
|
||||
EncoderDecoderCacheManager,
|
||||
)
|
||||
from vllm.v1.core.kv_cache_coordinator import HybridKVCacheCoordinator
|
||||
from vllm.v1.core.kv_cache_manager import KVCacheBlocks, KVCacheManager
|
||||
from vllm.v1.core.kv_cache_metrics import KVCacheMetricsCollector
|
||||
from vllm.v1.core.kv_cache_utils import KVCacheBlock
|
||||
@@ -711,50 +710,24 @@ class Scheduler(SchedulerInterface):
|
||||
# Get already-cached tokens.
|
||||
if request.num_computed_tokens == 0:
|
||||
did_prefix_cache_lookup = True
|
||||
hit_diverged = False
|
||||
# Get locally-cached tokens.
|
||||
if (
|
||||
self.connector is not None
|
||||
and self.has_mamba_layers
|
||||
and isinstance(
|
||||
self.kv_cache_manager.coordinator, HybridKVCacheCoordinator
|
||||
if self.connector is not None:
|
||||
# A KV connector transfers the missing suffix, which needs a
|
||||
# hybrid-aware lookup that can diverge across groups.
|
||||
(
|
||||
new_computed_blocks,
|
||||
num_new_local_computed_tokens,
|
||||
request.shared_prefix_boundary,
|
||||
hit_diverged,
|
||||
) = self.kv_cache_manager.get_computed_blocks_for_connector(
|
||||
request
|
||||
)
|
||||
):
|
||||
# The per-group lookup does not detect an uncached shared
|
||||
# prefix, so there is no junction to pin in this path.
|
||||
request.shared_prefix_boundary = 0
|
||||
kv_cache_manager = self.kv_cache_manager
|
||||
if not kv_cache_manager.prefix_cache_lookup_enabled(request):
|
||||
# Mirror the get_computed_blocks() early-out: the
|
||||
# request must recompute its prompt.
|
||||
new_computed_blocks = kv_cache_manager.empty_kv_cache_blocks
|
||||
num_new_local_computed_tokens = 0
|
||||
else:
|
||||
computed, per_group_hits = (
|
||||
self.kv_cache_manager.coordinator.find_longest_cache_hit_per_group(
|
||||
request.block_hashes, request.num_tokens - 1
|
||||
)
|
||||
)
|
||||
new_computed_blocks = (
|
||||
self.kv_cache_manager.create_kv_cache_blocks(computed)
|
||||
)
|
||||
# NOTE(ZhanqiuHu): For Mamba hybrid models,
|
||||
# num_new_local_computed_tokens should be the FA hit
|
||||
# length. This value is passed to the connector's
|
||||
# get_num_new_matched_tokens which computes:
|
||||
# external = total - local_computed.
|
||||
# Using the FA hit skips re-transferring FA blocks
|
||||
# already cached on D-side. The Mamba state (always
|
||||
# the last block) is transferred unconditionally by
|
||||
# _apply_prefix_caching in nixl/worker.py.
|
||||
num_new_local_computed_tokens = max(per_group_hits)
|
||||
else:
|
||||
(
|
||||
new_computed_blocks,
|
||||
num_new_local_computed_tokens,
|
||||
# Junction to pin (Marconi-style APC) so its
|
||||
# sparse-retention state (Mamba block / sliding-window
|
||||
# tail) survives retention and serves a later hit; 0
|
||||
# if no uncached shared prefix was detected.
|
||||
# Marconi shared-prefix junction to pin; 0 if none.
|
||||
request.shared_prefix_boundary,
|
||||
) = self.kv_cache_manager.get_computed_blocks(request)
|
||||
|
||||
@@ -776,6 +749,16 @@ class Scheduler(SchedulerInterface):
|
||||
|
||||
num_external_computed_tokens = ext_tokens
|
||||
|
||||
if hit_diverged and num_external_computed_tokens == 0:
|
||||
# No external tokens back the deeper local hit, so its
|
||||
# resume boundary would have no valid Mamba state.
|
||||
# Reconcile to the boundary every group agrees on.
|
||||
(
|
||||
new_computed_blocks,
|
||||
num_new_local_computed_tokens,
|
||||
request.shared_prefix_boundary,
|
||||
) = self.kv_cache_manager.get_computed_blocks(request)
|
||||
|
||||
connector_prefix_cache_queries = (
|
||||
request.num_tokens - num_new_local_computed_tokens
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user