[PD][Core] Fix Mamba prefix cache hit rate in PD disaggregation (#44243)

Co-authored-by: lHrHenry233 <2381623149@qq.com>
Co-authored-by: underfituu <hzhucong@163.com>
Signed-off-by: Zhanqiu Hu <zhu@redhat.com>
This commit is contained in:
zhanqiuhu
2026-06-11 14:10:25 +00:00
committed by GitHub
co-authored by lHrHenry233 underfituu
parent cc640ee8bc
commit 55911db580
5 changed files with 534 additions and 3 deletions
+39
View File
@@ -691,6 +691,45 @@ class HybridKVCacheCoordinator(KVCacheCoordinator):
blocks if blocks is not None else [] for blocks in hit_blocks_by_group
), hit_length
def find_longest_cache_hit_per_group(
self,
block_hashes: list[BlockHash],
max_cache_hit_length: int,
) -> tuple[tuple[list[KVCacheBlock], ...], tuple[int, ...]]:
"""Like find_longest_cache_hit but evaluates each group independently.
Returns:
(blocks_per_group, hit_lengths_per_group)
"""
def _get_block_hashes(kv_cache_spec: KVCacheSpec) -> BlockHashList:
if kv_cache_spec.block_size == self.hash_block_size:
return block_hashes
return BlockHashListWithBlockSize(
block_hashes, self.hash_block_size, kv_cache_spec.block_size
)
num_groups = len(self.kv_cache_config.kv_cache_groups)
hit_blocks: list[list[KVCacheBlock]] = [[] for _ in range(num_groups)]
hit_lengths: list[int] = [0] * num_groups
for spec, group_ids, manager_cls, use_eagle in self.attention_groups:
blocks = manager_cls.find_longest_cache_hit(
block_hashes=_get_block_hashes(spec),
max_length=max_cache_hit_length,
kv_cache_group_ids=group_ids,
block_pool=self.block_pool,
kv_cache_spec=spec,
drop_eagle_block=use_eagle,
alignment_tokens=self.scheduler_block_size,
)
group_hit = len(blocks[0]) * spec.block_size
for gid, blks in zip(group_ids, blocks):
hit_blocks[gid] = blks
hit_lengths[gid] = group_hit
return tuple(hit_blocks), tuple(hit_lengths)
def get_kv_cache_coordinator(
kv_cache_config: KVCacheConfig,
+39 -3
View File
@@ -34,6 +34,7 @@ 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.sched.interface import PauseState, SchedulerInterface
@@ -620,9 +621,44 @@ class Scheduler(SchedulerInterface):
# Get already-cached tokens.
if request.num_computed_tokens == 0:
# Get locally-cached tokens.
new_computed_blocks, num_new_local_computed_tokens = (
self.kv_cache_manager.get_computed_blocks(request)
)
if (
self.connector is not None
and self.has_mamba_layers
and isinstance(
self.kv_cache_manager.coordinator,
HybridKVCacheCoordinator,
)
):
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)
if self.kv_cache_manager.log_stats:
assert self.kv_cache_manager.prefix_cache_stats is not None
self.kv_cache_manager.prefix_cache_stats.record(
num_tokens=request.num_tokens,
num_hits=num_new_local_computed_tokens,
preempted=request.num_preemptions > 0,
)
else:
new_computed_blocks, num_new_local_computed_tokens = (
self.kv_cache_manager.get_computed_blocks(request)
)
# In case of hybrid models, obtain hint for Marconi-style APC logic
if self.has_mamba_layers: