[Bugfix][Spec Decode] Cache the Mamba state at the block-grid position of EAGLE resume (#53945)
Signed-off-by: wzhao18 <wzhao18.sz@gmail.com> Signed-off-by: Adam Shaver <ashaver@nvidia.com> Signed-off-by: akshaver <168006157+akshaver@users.noreply.github.com> Co-authored-by: wzhao18 <wzhao18.sz@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: roikoren755 <26850796+roikoren755@users.noreply.github.com>
This commit is contained in:
co-authored by
wzhao18
Claude Opus 5
roikoren755
parent
414057a3d3
commit
263c4ff95f
@@ -195,6 +195,12 @@ class CacheConfig:
|
||||
when the token is at position i * block_size. This is the default when prefix
|
||||
caching is enabled.
|
||||
"""
|
||||
enable_mamba_fine_grained_prefix_cache: bool = False
|
||||
"""Also register a Mamba "align" checkpoint at the shared-prefix junction --
|
||||
where an EAGLE/MTP sibling was observed to resume -- instead of only at the
|
||||
prompt tail. Off by default; only takes effect with `mamba_cache_mode`
|
||||
"align", EAGLE on the Mamba group, and a prefix match unit smaller than the
|
||||
Mamba block size."""
|
||||
replayssm_buffer_len: int = Field(default=16, gt=0)
|
||||
"""ReplaySSM logical history length B for Mamba2. Triton uses B physical
|
||||
rows and FlashInfer uses B+1. Kimi-K3 speculative decode does not use B.
|
||||
@@ -274,6 +280,7 @@ class CacheConfig:
|
||||
"prefix_cache_retention_interval",
|
||||
# Prefix-caching implementation detail (doesn't affect compiled graph).
|
||||
"prefix_match_unit",
|
||||
"enable_mamba_fine_grained_prefix_cache",
|
||||
"mamba_page_size_padded",
|
||||
"skip_page_size_padded",
|
||||
"user_specified_block_size",
|
||||
|
||||
@@ -726,6 +726,9 @@ class EngineArgs:
|
||||
mamba_block_size: int | None = get_field(CacheConfig, "mamba_block_size")
|
||||
prefix_match_unit: int | None = get_field(CacheConfig, "prefix_match_unit")
|
||||
mamba_cache_mode: MambaCacheMode = CacheConfig.mamba_cache_mode
|
||||
enable_mamba_fine_grained_prefix_cache: bool = (
|
||||
CacheConfig.enable_mamba_fine_grained_prefix_cache
|
||||
)
|
||||
replayssm_buffer_len: int = CacheConfig.replayssm_buffer_len
|
||||
use_replayssm: bool = CacheConfig.use_replayssm
|
||||
|
||||
@@ -1287,6 +1290,10 @@ class EngineArgs:
|
||||
cache_group.add_argument(
|
||||
"--mamba-cache-mode", **cache_kwargs["mamba_cache_mode"]
|
||||
)
|
||||
cache_group.add_argument(
|
||||
"--enable-mamba-fine-grained-prefix-cache",
|
||||
**cache_kwargs["enable_mamba_fine_grained_prefix_cache"],
|
||||
)
|
||||
cache_group.add_argument(
|
||||
"--replayssm-buffer-len", **cache_kwargs["replayssm_buffer_len"]
|
||||
)
|
||||
@@ -2072,6 +2079,9 @@ class EngineArgs:
|
||||
mamba_block_size=self.mamba_block_size,
|
||||
prefix_match_unit=self.prefix_match_unit,
|
||||
mamba_cache_mode=self.mamba_cache_mode,
|
||||
enable_mamba_fine_grained_prefix_cache=(
|
||||
self.enable_mamba_fine_grained_prefix_cache
|
||||
),
|
||||
replayssm_buffer_len=self.replayssm_buffer_len,
|
||||
use_replayssm=self.use_replayssm,
|
||||
kv_offloading_size=self.kv_offloading_size,
|
||||
|
||||
@@ -309,6 +309,28 @@ class KVCacheCoordinator(ABC):
|
||||
for manager in self.single_type_managers
|
||||
)
|
||||
|
||||
def get_replay_boundary(self, request: Request) -> int:
|
||||
"""Return the position a later request replaying this prompt resumes at.
|
||||
|
||||
A cache hit is the shortest hit across all groups, so this is a
|
||||
model-level position: every group has to retain state here, whether or
|
||||
not it is the group that drops. Groups differ only in how much they
|
||||
keep around it -- EAGLE groups also keep the block above, which they
|
||||
match and drop back from (see ``reachable_block_mask``).
|
||||
|
||||
Under EAGLE that block must exist, so the boundary sits one alignment
|
||||
unit below the prompt's last aligned position; every group's block size
|
||||
divides the alignment, so the block above always fits in the prompt.
|
||||
"""
|
||||
if not self.eagle_group_ids:
|
||||
return request.num_prompt_tokens - 1
|
||||
aligned = (
|
||||
request.num_prompt_tokens
|
||||
// self.scheduler_block_size
|
||||
* self.scheduler_block_size
|
||||
)
|
||||
return max(aligned - self.scheduler_block_size, 0)
|
||||
|
||||
def cache_blocks(self, request: Request, num_computed_tokens: int) -> None:
|
||||
"""
|
||||
Cache the blocks for the request.
|
||||
@@ -319,6 +341,7 @@ class KVCacheCoordinator(ABC):
|
||||
that need to be cached
|
||||
(including tokens that are already cached).
|
||||
"""
|
||||
replay_boundary = self.get_replay_boundary(request)
|
||||
for manager in self.single_type_managers:
|
||||
# Only cache tokens with finalized KV. The last num_reprefillable_tokens
|
||||
# tokens can be re-prefilled during multi-module MTP.
|
||||
@@ -329,6 +352,7 @@ class KVCacheCoordinator(ABC):
|
||||
request,
|
||||
num_tokens_to_cache,
|
||||
retention_interval=self.retention_interval,
|
||||
replay_boundary=replay_boundary,
|
||||
)
|
||||
|
||||
def free(self, request_id: str) -> None:
|
||||
@@ -753,6 +777,7 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
|
||||
def cache_blocks(self, request: Request, num_computed_tokens: int) -> None:
|
||||
cached_num_computed_tokens = self._align_cacheable(num_computed_tokens)
|
||||
replay_boundary = self.get_replay_boundary(request)
|
||||
for manager in self.single_type_managers:
|
||||
num_tokens_to_cache = cached_num_computed_tokens
|
||||
# EAGLE groups match one block past each aligned boundary and drop
|
||||
@@ -779,6 +804,7 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
request,
|
||||
num_tokens_to_cache,
|
||||
retention_interval=self.retention_interval,
|
||||
replay_boundary=replay_boundary,
|
||||
)
|
||||
|
||||
def find_longest_cache_hit(
|
||||
|
||||
@@ -15,6 +15,7 @@ from vllm.v1.core.kv_cache_coordinator import (
|
||||
)
|
||||
from vllm.v1.core.kv_cache_metrics import KVCacheMetricsCollector
|
||||
from vllm.v1.core.kv_cache_utils import KVCacheBlock, KVCacheBlockCopy
|
||||
from vllm.v1.core.single_type_kv_cache_manager import MambaManager
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
AttentionSpec,
|
||||
CrossAttentionSpec,
|
||||
@@ -132,6 +133,7 @@ class KVCacheManager:
|
||||
pcp_world_size: int = 1,
|
||||
metrics_collector: KVCacheMetricsCollector | None = None,
|
||||
watermark: float = 0.0,
|
||||
enable_mamba_fine_grained_prefix_cache: bool = False,
|
||||
) -> None:
|
||||
self.max_model_len = max_model_len
|
||||
# When unset, fall back to `max_model_len` so the recycling-aware cap
|
||||
@@ -164,6 +166,21 @@ class KVCacheManager:
|
||||
metrics_collector=self.metrics_collector,
|
||||
num_prefill_lookahead=num_prefill_lookahead,
|
||||
)
|
||||
# One predicate, read by both sides of the feature, so the scheduler
|
||||
# cannot end a chunk at a junction the manager would refuse -- a refused
|
||||
# junction costs a forward pass and displaces the block-boundary stop.
|
||||
# Multi-module MTP is excluded because ``cache_blocks`` then hands the
|
||||
# manager ``num_computed - num_reprefillable`` rather than the chunk end.
|
||||
self.mamba_fine_grained_prefix_cache = (
|
||||
enable_mamba_fine_grained_prefix_cache
|
||||
and bool(self.coordinator.eagle_group_ids)
|
||||
and self.coordinator.enable_partial_hash_hits
|
||||
and self.coordinator.num_reprefillable_tokens == 0
|
||||
)
|
||||
if self.mamba_fine_grained_prefix_cache:
|
||||
for manager in self.coordinator.single_type_managers:
|
||||
if isinstance(manager, MambaManager):
|
||||
manager.fine_grained_prefix_cache = True
|
||||
self.num_kv_cache_groups = len(kv_cache_config.kv_cache_groups)
|
||||
self.block_pool = self.coordinator.block_pool
|
||||
self.kv_cache_config = kv_cache_config
|
||||
|
||||
@@ -312,6 +312,9 @@ class Scheduler(SchedulerInterface):
|
||||
hash_block_size=hash_block_size,
|
||||
metrics_collector=self.kv_metrics_collector,
|
||||
watermark=self.scheduler_config.watermark,
|
||||
enable_mamba_fine_grained_prefix_cache=(
|
||||
self.cache_config.enable_mamba_fine_grained_prefix_cache
|
||||
),
|
||||
)
|
||||
# Bind GPU block pool to the KV connector. This must happen after
|
||||
# kv_cache_manager is constructed so block_pool is available.
|
||||
@@ -362,6 +365,14 @@ class Scheduler(SchedulerInterface):
|
||||
and self.hash_block_size < self.block_size
|
||||
and self.kv_cache_manager.coordinator.enable_partial_hash_hits
|
||||
)
|
||||
# Opt-in: also stop at the junction, where an eagle sibling resumes. The
|
||||
# manager decides whether it can check-point there (per-group eagle bit,
|
||||
# no MTP re-prefill tail); splitting for a stop it would refuse costs a
|
||||
# forward pass and displaces the block-boundary stop.
|
||||
self.mamba_fine_grained_prefix_cache = (
|
||||
self.mamba_partial_cache_hit
|
||||
and self.kv_cache_manager.mamba_fine_grained_prefix_cache
|
||||
)
|
||||
|
||||
# Counts of non-empty steps scheduled / processed. update_from_output
|
||||
# is called once per scheduled step in FIFO order, so these stay in sync.
|
||||
@@ -475,6 +486,26 @@ class Scheduler(SchedulerInterface):
|
||||
if self.mamba_partial_cache_hit and not use_internal_checkpoint
|
||||
else 0
|
||||
)
|
||||
if tail_boundary and self.use_eagle_block_drop:
|
||||
# Eagle matches one hash unit past the candidate and drops it, so
|
||||
# nothing proves the prompt's own last hash boundary. Materialize
|
||||
# the state one unit lower, where the hit can actually land. Keyed on
|
||||
# the block-drop bit, not plain use_eagle: this shift exists only to
|
||||
# compensate for the drop, and the Mamba manager's matching gate
|
||||
# reads the same bit (the coordinator is handed use_eagle_block_drop).
|
||||
tail_boundary = max(tail_boundary - self.hash_block_size, 0)
|
||||
junction = request.shared_prefix_boundary
|
||||
# Block-floored: a sub-block junction's state is not separately cacheable.
|
||||
block_floored = start + (junction - start) // block_size * block_size
|
||||
# Past the prompt the manager writes nothing, so fall back to the
|
||||
# block-floored stop rather than dropping it: a resumed request replaying
|
||||
# output tokens can still observe a junction there.
|
||||
junction_stop = (
|
||||
junction
|
||||
if self.mamba_fine_grained_prefix_cache
|
||||
and junction <= request.num_prompt_tokens
|
||||
else block_floored
|
||||
)
|
||||
stops = (
|
||||
# Same invariant: a chunk starting mid-block stops at the boundary
|
||||
# rather than running past it.
|
||||
@@ -488,12 +519,9 @@ class Scheduler(SchedulerInterface):
|
||||
tail_boundary
|
||||
if last_cache_position < tail_boundary < request.num_prompt_tokens
|
||||
else 0,
|
||||
# Marconi shared-prefix junction, block-floored (a sub-block
|
||||
# junction's state is not separately cacheable): cache its state
|
||||
# so sibling requests sharing the prefix can reuse it.
|
||||
start + (request.shared_prefix_boundary - start) // block_size * block_size
|
||||
if start < request.shared_prefix_boundary < end
|
||||
else 0,
|
||||
# Marconi shared-prefix junction: cache its state so sibling
|
||||
# requests sharing the prefix can reuse it.
|
||||
junction_stop if start < junction < end else 0,
|
||||
)
|
||||
# Stop at the earliest mandatory position strictly inside the chunk.
|
||||
end = min((s for s in stops if start < s < end), default=end)
|
||||
|
||||
@@ -119,7 +119,9 @@ class SingleTypeKVCacheManager(ABC):
|
||||
# aligned segment (SWA). Initialized lazily by the coordinator after
|
||||
# determining the attention groups.
|
||||
self.use_eagle = False
|
||||
|
||||
# ``CacheConfig.enable_mamba_fine_grained_prefix_cache``, narrowed and set
|
||||
# by ``KVCacheManager``; only an EAGLE Mamba "align" group ever gets it.
|
||||
self.fine_grained_prefix_cache = False
|
||||
# Partial-hit copy-on-write bookkeeping. Populated only by fine-grained
|
||||
# managers (full attention, mamba "align"); harmlessly empty elsewhere.
|
||||
self._partial_hit_reqs: dict[str, tuple[int, KVCacheBlock]] = {}
|
||||
@@ -449,6 +451,8 @@ class SingleTypeKVCacheManager(ABC):
|
||||
request: Request,
|
||||
num_tokens: int,
|
||||
retention_interval: int | None = None,
|
||||
*,
|
||||
replay_boundary: int,
|
||||
) -> None:
|
||||
"""
|
||||
Cache the blocks for the request.
|
||||
@@ -471,7 +475,7 @@ class SingleTypeKVCacheManager(ABC):
|
||||
# Token boundaries whose reachable tail must be retained under sparse
|
||||
# retention: the replay boundary (``num_prompt - 1``, capped by
|
||||
# ``get_computed_blocks``) and any detected shared-prefix junction.
|
||||
reachable_boundaries = [request.num_prompt_tokens - 1]
|
||||
reachable_boundaries = [replay_boundary]
|
||||
if request.shared_prefix_boundary:
|
||||
reachable_boundaries.append(request.shared_prefix_boundary)
|
||||
|
||||
@@ -813,8 +817,15 @@ class FullAttentionManager(SingleTypeKVCacheManager):
|
||||
request: Request,
|
||||
num_tokens: int,
|
||||
retention_interval: int | None = None,
|
||||
*,
|
||||
replay_boundary: int,
|
||||
) -> None:
|
||||
super().cache_blocks(request, num_tokens, retention_interval=retention_interval)
|
||||
super().cache_blocks(
|
||||
request,
|
||||
num_tokens,
|
||||
retention_interval=retention_interval,
|
||||
replay_boundary=replay_boundary,
|
||||
)
|
||||
hash_block_size = self.block_pool.hash_block_size
|
||||
if self.block_size == hash_block_size:
|
||||
return
|
||||
@@ -1216,6 +1227,8 @@ class CircularBufferManager(FullAttentionManager):
|
||||
request: Request,
|
||||
num_tokens: int,
|
||||
retention_interval: int | None = None,
|
||||
*,
|
||||
replay_boundary: int,
|
||||
) -> None:
|
||||
return
|
||||
|
||||
@@ -1926,9 +1939,16 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
request: Request,
|
||||
num_tokens: int,
|
||||
retention_interval: int | None = None,
|
||||
*,
|
||||
replay_boundary: int,
|
||||
) -> None:
|
||||
num_cached_blocks_before = self.num_cached_block.get(request.request_id, 0)
|
||||
super().cache_blocks(request, num_tokens, retention_interval=retention_interval)
|
||||
super().cache_blocks(
|
||||
request,
|
||||
num_tokens,
|
||||
retention_interval=retention_interval,
|
||||
replay_boundary=replay_boundary,
|
||||
)
|
||||
num_cached_blocks_after = self.num_cached_block.get(request.request_id, 0)
|
||||
if self.mamba_cache_mode == "align":
|
||||
partial_hash = self._cache_partial_tail_block(request, num_tokens)
|
||||
@@ -1994,7 +2014,22 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
latest_prompt_hash_boundary = (
|
||||
request.num_prompt_tokens // hash_block_size
|
||||
) * hash_block_size
|
||||
if num_tokens != latest_prompt_hash_boundary:
|
||||
if self.use_eagle:
|
||||
# Eagle groups match one hash unit past the candidate and drop it,
|
||||
# so register the tail one unit lower.
|
||||
latest_prompt_hash_boundary = max(
|
||||
latest_prompt_hash_boundary - hash_block_size, 0
|
||||
)
|
||||
# The junction is the other position a sibling resumes at: where one was
|
||||
# observed to stop, and where the scheduler already ends a chunk. Bounded
|
||||
# to the prompt chunk being computed -- during decode the target is the
|
||||
# running state block, mutated in place, which equals what its key
|
||||
# promises only after that step's forward.
|
||||
if num_tokens != latest_prompt_hash_boundary and not (
|
||||
self.fine_grained_prefix_cache
|
||||
and num_tokens == request.shared_prefix_boundary
|
||||
and request.num_computed_tokens < num_tokens <= request.num_prompt_tokens
|
||||
):
|
||||
return None
|
||||
|
||||
block_idx = num_tokens // self.block_size
|
||||
@@ -2054,6 +2089,8 @@ class CrossAttentionManager(SingleTypeKVCacheManager):
|
||||
request: Request,
|
||||
num_tokens: int,
|
||||
retention_interval: int | None = None,
|
||||
*,
|
||||
replay_boundary: int,
|
||||
) -> None:
|
||||
# We do not cache blocks for cross-attention to be shared between
|
||||
# requests, so this method is not relevant.
|
||||
|
||||
Reference in New Issue
Block a user