[Bugfix][KV Offload] Honor speculative cacheability in SimpleCPU (#60071)

Signed-off-by: Sherif Waly <sherif.waly@mistral.ai>
This commit is contained in:
Sherif Waly
2026-10-06 14:25:08 +02:00
committed by GitHub
parent 3e182185aa
commit 25385102fe
3 changed files with 214 additions and 53 deletions
+24 -38
View File
@@ -341,6 +341,14 @@ class KVCacheCoordinator(ABC):
extension = request.num_prompt_tokens // block * block
return tuple(sorted({max(resend - block, 0), max(extension - block, 0)}))
def get_num_cacheable_tokens(
self, num_computed_tokens: int, kv_cache_group_id: int
) -> int:
"""Return the prefix eligible for hashing in this group."""
# Only cache tokens with finalized KV. The last num_reprefillable_tokens
# tokens can be re-prefilled during multi-module MTP.
return max(0, num_computed_tokens - self.num_reprefillable_tokens)
def cache_blocks(self, request: Request, num_computed_tokens: int) -> None:
"""Cache the blocks for the request.
@@ -352,17 +360,12 @@ class KVCacheCoordinator(ABC):
"""
boundaries = self.get_replay_boundaries(request)
for manager in self.single_type_managers:
for group_id, manager in enumerate(self.single_type_managers):
if not manager.enable_caching:
continue
# Only cache tokens with finalized KV. The last num_reprefillable_tokens
# tokens can be re-prefilled during multi-module MTP.
num_tokens_to_cache = max(
0, num_computed_tokens - self.num_reprefillable_tokens
)
manager.cache_blocks(
request,
num_tokens_to_cache,
self.get_num_cacheable_tokens(num_computed_tokens, group_id),
retention_interval=self.retention_interval,
replay_boundaries=boundaries,
)
@@ -809,39 +812,22 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
return num_tokens
return round_down(num_tokens, self.scheduler_block_size)
def cache_blocks(self, request: Request, num_computed_tokens: int) -> None:
def get_num_cacheable_tokens(
self, num_computed_tokens: int, kv_cache_group_id: int
) -> int:
cached_num_computed_tokens = self._align_cacheable(num_computed_tokens)
boundaries = self.get_replay_boundaries(request)
for manager in self.single_type_managers:
if not manager.enable_caching:
continue
num_tokens_to_cache = cached_num_computed_tokens
# EAGLE groups match one block past each aligned boundary and drop
# it, so make that lookahead block eligible to be cached.
if manager.use_eagle and cached_num_computed_tokens > 0:
# Only cache tokens with finalized KV. The last
# num_reprefillable_tokens tokens can be re-prefilled during
# multi-module MTP.
num_finalized_computed_tokens = max(
0, num_computed_tokens - self.num_reprefillable_tokens
)
cached_num_finalized_computed_tokens = self._align_cacheable(
num_finalized_computed_tokens
)
num_tokens_to_cache = min(
num_finalized_computed_tokens,
cached_num_finalized_computed_tokens + manager.block_size,
)
# The manager already knows the fine hit granularity
# (``scheduler_block_size``); retention is passed separately so it
# can keep both the coarse segment tails and the fine replay
# boundary (which needs the fine value).
manager.cache_blocks(
request,
num_tokens_to_cache,
retention_interval=self.retention_interval,
replay_boundaries=boundaries,
manager = self.single_type_managers[kv_cache_group_id]
# EAGLE matches one block past the aligned boundary, then drops it.
if manager.use_eagle and cached_num_computed_tokens > 0:
num_finalized_computed_tokens = max(
0, num_computed_tokens - self.num_reprefillable_tokens
)
return min(
num_finalized_computed_tokens,
self._align_cacheable(num_finalized_computed_tokens)
+ manager.block_size,
)
return cached_num_computed_tokens
def find_longest_cache_hit(
self,
+6 -14
View File
@@ -216,6 +216,7 @@ class SimpleCPUOffloadScheduler:
scheduler_block_size=self.block_size,
hash_block_size=self.hash_block_size,
allow_partial_hash_hits=not lazy_offload,
num_prefill_lookahead=vllm_config.num_prefill_lookahead_tokens,
)
self.group_block_sizes = self.cpu_coordinator.group_block_sizes
# FA group's own resolved block_size; divides scheduler_block_size (the
@@ -837,19 +838,6 @@ class SimpleCPUOffloadScheduler:
)
request = state.request
confirmed_tokens = request.num_computed_tokens - request.num_output_placeholders
# Truncate to the granularity a lookup can actually land on. With
# fine-grained hits that is hash_block_size; otherwise hits only land on
# the scheduler block (the group LCM). Using the LCM unconditionally
# would drop whole blocks that sit between the last LCM boundary and a
# reachable fine-grained boundary, so the other groups would hold that
# boundary and this one would not, and the joint hybrid lookup would
# reconcile to zero.
store_alignment = (
self.hash_block_size
if self.cpu_coordinator.enable_partial_hash_hits
else self.block_size
)
aligned_tokens = confirmed_tokens // store_alignment * store_alignment
num_free = self.cpu_block_pool.get_num_free_blocks()
for g, group_gpu_ids in enumerate(block_ids_by_group):
@@ -864,7 +852,11 @@ class SimpleCPUOffloadScheduler:
# num_stored_blocks can be stale and omit evicted blocks in
# the middle of the request.
group_size = self.group_block_sizes[g]
ready = min(len(group_gpu_ids), aligned_tokens // group_size)
# Mimic the GPU KV cache manager's cacheable prefix.
cacheable_tokens = self.cpu_coordinator.get_num_cacheable_tokens(
confirmed_tokens, g
)
ready = min(len(group_gpu_ids), cacheable_tokens // group_size)
resolved_hashes = resolve_block_hashes(
request.block_hashes, self.hash_block_size, group_size
)