[Bugfix][Prefix caching] Fix inconsistent hybrid Mamba prefix cache boundaries (#60533)
Signed-off-by: Wei Zhao <weizha@aws-cmh-slurm-1-login-01.cm.cluster> Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: Wei Zhao <weizha@aws-cmh-slurm-1-login-01.cm.cluster> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Wei Zhao
Codex
zjy0516
Claude Opus 5.5
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
1664c4f444
commit
d8fa463e2b
@@ -479,8 +479,8 @@ class BlockPool:
|
||||
kv_cache_group_id: KV cache group that owns the partial entry.
|
||||
block_size: Cache block size for the owning group. The partial
|
||||
entry hash itself is always the prefix-chain hash at
|
||||
``num_tokens``; ``block_size`` is used to assert that the
|
||||
entry is partial within the owning cache block.
|
||||
``num_tokens``; ``block_size`` must be a multiple of the hash
|
||||
block size.
|
||||
replace_existing_hashes: Whether the block contents were replaced
|
||||
and all existing cache entries must be removed before the new
|
||||
entry is registered.
|
||||
@@ -494,9 +494,6 @@ class BlockPool:
|
||||
return None
|
||||
|
||||
assert block_size % self.hash_block_size == 0
|
||||
assert replace_existing_hashes or (
|
||||
block_size > self.hash_block_size and num_tokens % block_size != 0
|
||||
)
|
||||
block_hash = self._get_partial_block_hash(request, num_tokens)
|
||||
num_hash_blocks = num_tokens // self.hash_block_size
|
||||
block_hash_with_group_id = make_block_hash_with_group_id(
|
||||
|
||||
@@ -4,13 +4,14 @@ from abc import ABC, abstractmethod
|
||||
from collections.abc import Sequence
|
||||
from typing import NamedTuple
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.math_utils import cdiv, round_down
|
||||
from vllm.v1.core.block_pool import BlockPool
|
||||
from vllm.v1.core.kv_cache_metrics import KVCacheMetricsCollector
|
||||
from vllm.v1.core.kv_cache_utils import (
|
||||
BlockHash,
|
||||
KVCacheBlock,
|
||||
eagle_proof_margin,
|
||||
partial_hash_hits_enabled,
|
||||
)
|
||||
from vllm.v1.core.single_type_kv_cache_manager import (
|
||||
CrossAttentionManager,
|
||||
@@ -28,8 +29,6 @@ from vllm.v1.kv_cache_interface import (
|
||||
)
|
||||
from vllm.v1.request import Request
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def _validate_prefix_cache_retention_interval(
|
||||
retention_interval: int | None,
|
||||
@@ -695,37 +694,15 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
# managers in every group. TP needs hashing finer than the Mamba block;
|
||||
# DCP accepts equality because it scales the effective full-attention
|
||||
# block instead.
|
||||
has_partial_mamba_group = any(
|
||||
isinstance(g.kv_cache_spec, MambaSpec)
|
||||
and g.kv_cache_spec.mamba_cache_mode == "align"
|
||||
and (
|
||||
(dcp_world_size == 1 and g.kv_cache_spec.block_size > hash_block_size)
|
||||
or (
|
||||
dcp_world_size > 1 and g.kv_cache_spec.block_size >= hash_block_size
|
||||
)
|
||||
)
|
||||
for g in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
self.enable_partial_hash_hits = (
|
||||
allow_partial_hash_hits and has_partial_mamba_group
|
||||
allow_partial_hash_hits
|
||||
and partial_hash_hits_enabled(
|
||||
kv_cache_config.kv_cache_groups,
|
||||
hash_block_size,
|
||||
dcp_world_size,
|
||||
[type(manager) for manager in self.single_type_managers],
|
||||
)
|
||||
)
|
||||
if self.enable_partial_hash_hits:
|
||||
unsupported_partial_hit_managers = {
|
||||
type(manager).__name__
|
||||
for manager, group in zip(
|
||||
self.single_type_managers, kv_cache_config.kv_cache_groups
|
||||
)
|
||||
if group.kv_cache_spec.prefix_cacheable
|
||||
and not manager.supports_fine_grained_hash_lookup
|
||||
and manager.block_size != hash_block_size
|
||||
}
|
||||
if unsupported_partial_hit_managers:
|
||||
self.enable_partial_hash_hits = False
|
||||
logger.warning_once(
|
||||
"Disabling fine-grained prefix-cache hits because these KV "
|
||||
"cache managers require block-aligned lookups: %s.",
|
||||
", ".join(sorted(unsupported_partial_hit_managers)),
|
||||
)
|
||||
cache_hit_alignment_tokens = self._cache_hit_alignment_tokens
|
||||
for manager in self.single_type_managers:
|
||||
manager.cache_hit_alignment_tokens = cache_hit_alignment_tokens
|
||||
@@ -899,15 +876,15 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
# mamba: its finder never drops (draft models have no mamba
|
||||
# layers), so the hit would grow past the candidate.
|
||||
if drop_eagle_block and not isinstance(spec, MambaSpec):
|
||||
eagle_margin = (
|
||||
self.hash_block_size
|
||||
if self.enable_partial_hash_hits
|
||||
and manager_cls.supports_fine_grained_hash_lookup
|
||||
and group_block_size > self.hash_block_size
|
||||
else group_block_size
|
||||
eagle_margin = eagle_proof_margin(
|
||||
group_block_size,
|
||||
self.hash_block_size,
|
||||
self.enable_partial_hash_hits
|
||||
and manager_cls.supports_fine_grained_hash_lookup,
|
||||
)
|
||||
_max_length = min(
|
||||
curr_hit_length + eagle_margin, max_cache_hit_length
|
||||
curr_hit_length + eagle_margin,
|
||||
len(block_hashes) * self.hash_block_size,
|
||||
)
|
||||
hit_blocks, _new_hit_length = manager_cls.find_longest_cache_hit(
|
||||
block_hashes=block_hashes,
|
||||
@@ -983,9 +960,21 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
|
||||
|
||||
for spec, group_ids, manager_cls, use_eagle in self.attention_groups:
|
||||
manager = self.single_type_managers[group_ids[0]]
|
||||
lookup_length = max_cache_hit_length
|
||||
if use_eagle and not isinstance(spec, MambaSpec):
|
||||
eagle_margin = eagle_proof_margin(
|
||||
manager.block_size,
|
||||
self.hash_block_size,
|
||||
self.enable_partial_hash_hits
|
||||
and manager_cls.supports_fine_grained_hash_lookup,
|
||||
)
|
||||
lookup_length = min(
|
||||
max_cache_hit_length + eagle_margin,
|
||||
len(block_hashes) * self.hash_block_size,
|
||||
)
|
||||
blocks, group_hit = manager_cls.find_longest_cache_hit(
|
||||
block_hashes=block_hashes,
|
||||
max_length=max_cache_hit_length,
|
||||
max_length=lookup_length,
|
||||
kv_cache_group_ids=group_ids,
|
||||
block_pool=manager.block_pool,
|
||||
kv_cache_spec=spec,
|
||||
|
||||
@@ -53,6 +53,7 @@ from vllm.v1.utils import tensor_data
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.v1.core.block_pool import BlockPool
|
||||
from vllm.v1.core.single_type_kv_cache_manager import SingleTypeKVCacheManager
|
||||
|
||||
|
||||
# BlockHash represents the hash of a single KV-cache block used for
|
||||
@@ -747,6 +748,60 @@ def resolve_dcp_kv_cache_spec(spec: KVCacheSpec, dcp_world_size: int) -> KVCache
|
||||
return replace(spec, block_size=block_size)
|
||||
|
||||
|
||||
def partial_hash_hits_enabled(
|
||||
kv_cache_groups: Sequence[KVCacheGroupSpec],
|
||||
hash_block_size: int,
|
||||
dcp_world_size: int = 1,
|
||||
manager_classes: Sequence[type["SingleTypeKVCacheManager"]] | None = None,
|
||||
) -> bool:
|
||||
"""Whether aligned Mamba states support sub-block prefix-cache hits.
|
||||
|
||||
If ``manager_classes`` (one per group) is given, also require every other
|
||||
prefix-cacheable group to support fine-grained lookups.
|
||||
"""
|
||||
if not any(
|
||||
isinstance(spec, MambaSpec)
|
||||
and spec.mamba_cache_mode == "align"
|
||||
and (
|
||||
(dcp_world_size == 1 and spec.block_size > hash_block_size)
|
||||
or (dcp_world_size > 1 and spec.block_size >= hash_block_size)
|
||||
)
|
||||
for group in kv_cache_groups
|
||||
for spec in iter_layer_specs(group.kv_cache_spec)
|
||||
):
|
||||
return False
|
||||
if manager_classes is None:
|
||||
return True
|
||||
unsupported_managers = {
|
||||
manager_cls.__name__
|
||||
for group, manager_cls in zip(kv_cache_groups, manager_classes)
|
||||
if group.kv_cache_spec.prefix_cacheable
|
||||
and not manager_cls.supports_fine_grained_hash_lookup
|
||||
and resolve_dcp_kv_block_size(group.kv_cache_spec, dcp_world_size)
|
||||
!= hash_block_size
|
||||
}
|
||||
if unsupported_managers:
|
||||
logger.warning_once(
|
||||
"Disabling fine-grained prefix-cache hits because these KV "
|
||||
"cache managers require block-aligned lookups: %s.",
|
||||
", ".join(sorted(unsupported_managers)),
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def eagle_proof_margin(
|
||||
block_size: int, hash_block_size: int, fine_grained_lookup: bool
|
||||
) -> int:
|
||||
"""Tokens an EAGLE group matches past a cache hit before dropping them.
|
||||
|
||||
Fine-grained lookups drop one hash unit; others drop one cache block.
|
||||
"""
|
||||
if fine_grained_lookup and block_size > hash_block_size:
|
||||
return hash_block_size
|
||||
return block_size
|
||||
|
||||
|
||||
def resolve_kv_cache_block_sizes(
|
||||
kv_cache_config: KVCacheConfig,
|
||||
vllm_config: VllmConfig,
|
||||
@@ -775,8 +830,7 @@ def resolve_kv_cache_block_sizes(
|
||||
if groups and not groups[0].kv_cache_spec.dcp_sharded:
|
||||
dcp = 1
|
||||
bs = cache_config.block_size * dcp
|
||||
# The Mamba prefill checkpoint builder reads prefix_match_unit directly,
|
||||
# so a value dropped here puts its checkpoints off the scheduler's grid.
|
||||
# Reject a prefix_match_unit that would otherwise be silently ignored.
|
||||
if (
|
||||
cache_config.prefix_match_unit not in (None, bs)
|
||||
and len(groups) == 1
|
||||
@@ -797,8 +851,8 @@ def resolve_kv_cache_block_sizes(
|
||||
]
|
||||
scheduler_block_size = math.lcm(*group_block_sizes)
|
||||
|
||||
logger.info("kv cache group sizes %s", group_block_sizes)
|
||||
logger.info("kv lcm block sizes %s", scheduler_block_size)
|
||||
logger.info_once("kv cache group sizes %s", tuple(group_block_sizes))
|
||||
logger.info_once("kv lcm block sizes %s", scheduler_block_size)
|
||||
|
||||
# Block hashes are only consumed by prefix caching and KV connectors
|
||||
# (P/D, offloading); when neither is active, keep hash_block_size equal
|
||||
@@ -839,18 +893,10 @@ def resolve_kv_cache_block_sizes(
|
||||
and isinstance(spec.tokens_per_state, int)
|
||||
and spec.tokens_per_state > 1
|
||||
}
|
||||
has_partial_mamba_group = any(
|
||||
isinstance(spec, MambaSpec)
|
||||
and spec.mamba_cache_mode == "align"
|
||||
and (
|
||||
(dcp == 1 and block_size > hash_block_size)
|
||||
or (dcp > 1 and block_size >= hash_block_size)
|
||||
)
|
||||
for group, block_size in zip(groups, group_block_sizes)
|
||||
for spec in iter_layer_specs(group.kv_cache_spec)
|
||||
)
|
||||
cache_hit_alignment = (
|
||||
hash_block_size if has_partial_mamba_group else scheduler_block_size
|
||||
hash_block_size
|
||||
if partial_hash_hits_enabled(groups, hash_block_size, dcp)
|
||||
else scheduler_block_size
|
||||
)
|
||||
if any(cache_hit_alignment % alignment for alignment in prefix_alignments):
|
||||
raise ValueError(
|
||||
@@ -861,6 +907,38 @@ def resolve_kv_cache_block_sizes(
|
||||
return scheduler_block_size, hash_block_size
|
||||
|
||||
|
||||
def resolve_cache_hit_alignment_tokens(
|
||||
kv_cache_config: KVCacheConfig,
|
||||
vllm_config: VllmConfig,
|
||||
scheduler_block_size: int,
|
||||
hash_block_size: int,
|
||||
) -> int:
|
||||
"""Token granularity at which prefix-cache hits land.
|
||||
|
||||
Mirrors the alignment the hybrid KV cache coordinator pushes to its
|
||||
managers: fine-grained hits land on ``hash_block_size``, all others on
|
||||
``scheduler_block_size``.
|
||||
"""
|
||||
groups = kv_cache_config.kv_cache_groups
|
||||
if not vllm_config.cache_config.enable_prefix_caching or len(groups) <= 1:
|
||||
return scheduler_block_size
|
||||
manager_classes = []
|
||||
for group in groups:
|
||||
manager_cls = KVCacheSpecRegistry.get_manager_class(
|
||||
group.kv_cache_spec, group.role
|
||||
)
|
||||
assert manager_cls is not None
|
||||
manager_classes.append(manager_cls)
|
||||
if partial_hash_hits_enabled(
|
||||
groups,
|
||||
hash_block_size,
|
||||
vllm_config.parallel_config.decode_context_parallel_size,
|
||||
manager_classes,
|
||||
):
|
||||
return hash_block_size
|
||||
return scheduler_block_size
|
||||
|
||||
|
||||
def get_request_block_hasher(
|
||||
hash_block_size: int,
|
||||
caching_hash_fn: Callable[[Any], bytes],
|
||||
|
||||
@@ -381,9 +381,8 @@ class Scheduler(SchedulerInterface):
|
||||
or group.kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
for group in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
# A finer prefix_match_unit is configured: a mamba partial tail entry
|
||||
# can only be registered by a step ending exactly at the prompt's last
|
||||
# hash boundary, so the split adds that stop.
|
||||
# Fine-grained hits reuse the prompt's hash-aligned Mamba checkpoint.
|
||||
# Split there only if the backend cannot save it within the final chunk.
|
||||
self.mamba_partial_cache_hit = (
|
||||
self.need_mamba_block_aligned_split
|
||||
and self.hash_block_size < self.block_size
|
||||
@@ -460,14 +459,19 @@ class Scheduler(SchedulerInterface):
|
||||
# The last block-aligned position whose state can be cached. With
|
||||
# Eagle, FullAttn prunes the last matching block, so back off one
|
||||
# block to avoid a Mamba cache miss.
|
||||
last_cache_position = request.num_tokens - request.num_tokens % block_size
|
||||
if self.use_eagle_block_drop:
|
||||
last_cache_position = max(last_cache_position - block_size, 0)
|
||||
ckpt_block_size = (
|
||||
self.hash_block_size if self.mamba_partial_cache_hit else self.block_size
|
||||
)
|
||||
last_cache_position = get_mamba_prefill_checkpoint_position(
|
||||
request.num_tokens,
|
||||
block_size,
|
||||
self.use_eagle_block_drop,
|
||||
)
|
||||
|
||||
end = start + num_new_tokens
|
||||
checkpoint_position = get_mamba_prefill_checkpoint_position(
|
||||
prefill_end,
|
||||
self.hash_block_size,
|
||||
ckpt_block_size,
|
||||
drop_eagle_block=self.use_eagle_block_drop,
|
||||
)
|
||||
use_internal_checkpoint = (
|
||||
@@ -500,18 +504,14 @@ class Scheduler(SchedulerInterface):
|
||||
|
||||
next_block_boundary = (start // block_size + 1) * block_size
|
||||
tail_boundary = (
|
||||
request.num_prompt_tokens // self.hash_block_size * self.hash_block_size
|
||||
get_mamba_prefill_checkpoint_position(
|
||||
request.num_prompt_tokens,
|
||||
self.hash_block_size,
|
||||
self.use_eagle_block_drop,
|
||||
)
|
||||
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
|
||||
@@ -532,8 +532,7 @@ class Scheduler(SchedulerInterface):
|
||||
else 0,
|
||||
# Never run past the last cacheable block boundary mid-chunk.
|
||||
last_cache_position,
|
||||
# Fine-grained hits: the prompt's partial-tail entry can only be
|
||||
# registered by a chunk ending exactly at its last hash boundary.
|
||||
# Stop at the prompt checkpoint unless the backend saves it internally.
|
||||
tail_boundary
|
||||
if last_cache_position < tail_boundary < request.num_prompt_tokens
|
||||
else 0,
|
||||
|
||||
@@ -876,7 +876,14 @@ class FullAttentionManager(SingleTypeKVCacheManager):
|
||||
block are intentionally skipped.
|
||||
"""
|
||||
hash_block_size = self.block_pool.hash_block_size
|
||||
boundary_tokens = request.num_prompt_tokens // hash_block_size * hash_block_size
|
||||
# A resend matches at most `prompt_len - 1` tokens, but EAGLE lookups
|
||||
# read the whole prompt before dropping a hash unit.
|
||||
token_limit = (
|
||||
request.num_prompt_tokens
|
||||
if self.use_eagle
|
||||
else request.num_prompt_tokens - 1
|
||||
)
|
||||
boundary_tokens = token_limit // hash_block_size * hash_block_size
|
||||
if boundary_tokens == 0 or boundary_tokens > num_tokens:
|
||||
return
|
||||
if boundary_tokens % self.block_size == 0:
|
||||
@@ -1788,7 +1795,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
# is kept, so `prefill_end` skips reserving blocks for the others.
|
||||
checkpoint_position = get_mamba_prefill_checkpoint_position(
|
||||
num_tokens,
|
||||
self.block_pool.hash_block_size,
|
||||
self.cache_hit_alignment_tokens,
|
||||
self.drop_eagle_checkpoint_block,
|
||||
)
|
||||
if not self._needs_internal_checkpoint(
|
||||
@@ -2073,20 +2080,29 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
checkpoint_position
|
||||
== get_mamba_prefill_checkpoint_position(
|
||||
request.num_prompt_tokens,
|
||||
hash_block_size,
|
||||
self.cache_hit_alignment_tokens,
|
||||
self.drop_eagle_checkpoint_block,
|
||||
)
|
||||
)
|
||||
is_retained_checkpoint = False
|
||||
if checkpoint_position % self.block_size == 0:
|
||||
block_idx = checkpoint_position // self.block_size - 1
|
||||
retained = self.reachable_block_mask(
|
||||
block_idx,
|
||||
block_idx + 1,
|
||||
self.cache_hit_alignment_tokens,
|
||||
self.kv_cache_spec,
|
||||
self.drop_eagle_checkpoint_block,
|
||||
retention_interval=retention_interval,
|
||||
)
|
||||
is_retained_checkpoint = retained is None or retained[0]
|
||||
if (
|
||||
retention_interval == 0
|
||||
and num_tokens < request.num_prompt_tokens
|
||||
and not is_prompt_checkpoint
|
||||
not is_prompt_checkpoint
|
||||
and not is_retained_checkpoint
|
||||
and checkpoint_position != request.shared_prefix_boundary
|
||||
):
|
||||
# retention_interval == 0 keeps this transient checkpoint
|
||||
# request-local. The slot may carry a hash from this step's
|
||||
# full-block pass; that must go too, since the checkpoint
|
||||
# state is about to overwrite the block.
|
||||
# The transient state overwrites this slot, so neither it nor
|
||||
# the full-block hash registered earlier this step is reusable.
|
||||
self.block_pool._maybe_evict_cached_block(checkpoint_block)
|
||||
return None
|
||||
if checkpoint_block.block_hash_num_tokens == checkpoint_position:
|
||||
@@ -2099,21 +2115,13 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
block_size=self.block_size,
|
||||
replace_existing_hashes=True,
|
||||
)
|
||||
if self.block_size == hash_block_size:
|
||||
if num_tokens <= 0 or num_tokens % hash_block_size != 0:
|
||||
return None
|
||||
if num_tokens % self.block_size == 0:
|
||||
return None
|
||||
if num_tokens % hash_block_size != 0:
|
||||
return None
|
||||
latest_prompt_hash_boundary = (
|
||||
request.num_prompt_tokens // hash_block_size
|
||||
) * hash_block_size
|
||||
if self.drop_eagle_checkpoint_block:
|
||||
# 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
|
||||
)
|
||||
latest_prompt_hash_boundary = get_mamba_prefill_checkpoint_position(
|
||||
request.num_prompt_tokens,
|
||||
hash_block_size,
|
||||
self.drop_eagle_checkpoint_block,
|
||||
)
|
||||
# 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
|
||||
@@ -2126,7 +2134,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
):
|
||||
return None
|
||||
|
||||
block_idx = num_tokens // self.block_size
|
||||
block_idx = cdiv(num_tokens, self.block_size) - 1
|
||||
blocks = self.req_to_blocks[request.request_id]
|
||||
if block_idx >= len(blocks):
|
||||
return None
|
||||
@@ -2141,7 +2149,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
kv_cache_group_id=self.kv_cache_group_id,
|
||||
block_size=self.block_size,
|
||||
)
|
||||
if partial_hash is not None:
|
||||
if partial_hash is not None and num_tokens % self.block_size:
|
||||
self._partial_hit_reqs[request.request_id] = (block_idx, source_block)
|
||||
self.num_cached_block[request.request_id] = block_idx
|
||||
# Producer of this partial tail: the boundary state currently lives
|
||||
|
||||
Reference in New Issue
Block a user