[BugFix][KV Cache] Fix hybrid prefix caching with hidden-state extraction (#51108)

Signed-off-by: Canlin Guo <canlinguosdu@gmail.com>
This commit is contained in:
Canlin Guo
2026-08-06 06:42:37 +00:00
committed by GitHub
parent 8060260e1d
commit 5370461649
2 changed files with 82 additions and 1 deletions
+60
View File
@@ -45,6 +45,7 @@ from vllm.v1.core.kv_cache_utils import (
from vllm.v1.kv_cache_interface import (
ChunkedLocalAttentionSpec,
FullAttentionSpec,
HiddenStateCacheSpec,
KVCacheConfig,
KVCacheGroupSpec,
KVCacheSpec,
@@ -2196,6 +2197,65 @@ def _grouping_config():
)
def test_hidden_state_group_preserves_hybrid_prefix_cache_granularity():
block_size = 544
full_spec = FullAttentionSpec(
block_size=block_size,
num_kv_heads=1,
# FullAttentionSpec stores both K and V, so this produces a
# 544 * 1 * (512 + 512) * 2 = 1,114,112-byte page.
head_size=512,
dtype=torch.bfloat16,
)
mamba_spec = MambaSpec(
block_size=block_size,
shapes=((557056,),),
dtypes=(torch.bfloat16,),
mamba_cache_mode="align",
)
hidden_spec = HiddenStateCacheSpec(
block_size=block_size,
num_kv_heads=3,
head_size=1024,
dtype=torch.bfloat16,
)
assert full_spec.page_size_bytes == mamba_spec.page_size_bytes
groups = get_kv_cache_groups(
_grouping_config(),
{
"model.full_attn": full_spec,
"model.mamba": mamba_spec,
"cache_only_layers.0": hidden_spec,
},
)
hidden_group = next(
group
for group in groups
if isinstance(group.kv_cache_spec, HiddenStateCacheSpec)
)
assert hidden_group.kv_cache_spec.block_size == 136
kv_cache_config = KVCacheConfig(
num_blocks=1,
kv_cache_tensors=[],
kv_cache_groups=groups,
)
vllm_config = SimpleNamespace(
cache_config=SimpleNamespace(
block_size=16,
enable_prefix_caching=True,
prefix_match_unit=None,
),
parallel_config=SimpleNamespace(decode_context_parallel_size=1),
kv_transfer_config=object(),
)
assert kv_cache_utils.resolve_kv_cache_block_sizes(
kv_cache_config, vllm_config
) == (544, 136)
def test_mla_draft_prefers_standard_layout_when_pages_can_be_unified():
specs = {
"target.0.attn": new_mla_spec(),