Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/v1/core/test_kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,39 @@ def test_k3_hybrid_group_size_is_model_and_cache_specific(
assert len(groups) == expected_groups


@pytest.mark.parametrize("dcp_world_size", [8, 16])
def test_k3_parallel_draft_groups_are_annotated_by_layer_contract(
monkeypatch: pytest.MonkeyPatch,
dcp_world_size: int,
):
config = SimpleNamespace(
scheduler_config=SimpleNamespace(disable_hybrid_kv_cache_manager=False),
parallel_config=SimpleNamespace(
decode_context_parallel_size=dcp_world_size,
prefill_context_parallel_size=1,
),
model_config=SimpleNamespace(
hf_config=SimpleNamespace(model_type="kimi_k3"),
),
speculative_config=SimpleNamespace(use_eagle=lambda: True),
)
monkeypatch.setenv("VLLM_K3_KV_GROUP_SIZE", "2")
monkeypatch.setattr(
kv_cache_utils,
"group_and_unify_kv_cache_specs",
lambda *args, **kwargs: None,
)

groups = get_kv_cache_groups(config, _k3_hybrid_cache_specs())
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

assert any(group.is_eagle_group for group in groups)
for group in groups:
contains_parallel_draft = any(
layer_name.startswith("draft.") for layer_name in group.layer_names
)
assert group.is_eagle_group is contains_parallel_draft
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved


def test_group_and_unify_kv_cache_specs_mixed_page_size_groups():
# DeepseekV4-style: differing page sizes across MLA and sliding-window MLA
# layers do require tuple packing, so grouping must still be produced.
Expand Down
36 changes: 31 additions & 5 deletions vllm/v1/core/kv_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,7 @@ def promoted_page_size_padded(spec: AttentionSpec, block_size: int) -> int | Non
compress_ratio=spec.compress_ratio,
model_version=spec.model_version,
dcp_replicated=spec.dcp_replicated,
non_causal_multi_token_decode=spec.non_causal_multi_token_decode,
)
elif isinstance(spec, SlidingWindowSpec):
block_size = full_attention_block_size or spec.block_size
Expand Down Expand Up @@ -1990,14 +1991,33 @@ def _get_kv_cache_groups_uniform_groups(
return [full_mla_group, *swa_mla_groups]


def _annotate_eagle_groups_deepseek_v4(
def _annotate_eagle_groups(
vllm_config: VllmConfig,
kv_cache_spec: dict[str, KVCacheSpec],
kv_cache_groups: list[KVCacheGroupSpec],
) -> None:
spec_config = vllm_config.speculative_config
"""Identify cache groups that contain speculative-draft attention.

Kimi parallel drafters expose their execution contract directly on each
MLA cache specification. This gives the cache planner an exact,
topology-independent layer set. DeepSeek-V4 does not expose equivalent
per-layer metadata, so its established final-layer rule remains the
compatibility fallback.
"""
spec_config = getattr(vllm_config, "speculative_config", None)
if spec_config is None or not spec_config.use_eagle():
return

draft_layer_names = {
layer_name
for layer_name, spec in kv_cache_spec.items()
if getattr(spec, "non_causal_multi_token_decode", False)
}
if draft_layer_names:
for group in kv_cache_groups:
if draft_layer_names.intersection(group.layer_names):
group.is_eagle_group = True

# Detection uses the merged MLA spec's model_version.
if not any(
getattr(spec, "model_version", None) == "deepseek_v4"
Expand Down Expand Up @@ -2046,12 +2066,16 @@ def get_kv_cache_groups(
# KV cache of all layers are the same, which is true for
# most models. Allocate the same amount of memory for
# each layer.
return _get_kv_cache_groups_uniform_spec(kv_cache_spec)
kv_cache_groups = _get_kv_cache_groups_uniform_spec(kv_cache_spec)
_annotate_eagle_groups(vllm_config, kv_cache_spec, kv_cache_groups)
return kv_cache_groups
elif uniform_spec := UniformTypeKVCacheSpecs.from_specs(kv_cache_spec):
# All layers need the same number of token slots (e.g., all layers are
# full attention, or all layers are sliding window attention with the
# same window size). Put all layers into one group.
return _get_kv_cache_groups_uniform_type(uniform_spec)
kv_cache_groups = _get_kv_cache_groups_uniform_type(uniform_spec)
_annotate_eagle_groups(vllm_config, kv_cache_spec, kv_cache_groups)
return kv_cache_groups
elif grouped_specs := group_and_unify_kv_cache_specs(
kv_cache_spec,
vllm_config.parallel_config.decode_context_parallel_size,
Expand All @@ -2062,7 +2086,7 @@ def get_kv_cache_groups(
# attention in different sizes. Need to group layers into multiple
# UniformTypeKVCacheSpecs.
kv_cache_groups = _get_kv_cache_groups_uniform_groups(grouped_specs)
_annotate_eagle_groups_deepseek_v4(vllm_config, kv_cache_spec, kv_cache_groups)
_annotate_eagle_groups(vllm_config, kv_cache_spec, kv_cache_groups)
return kv_cache_groups

# Pull HiddenStateCacheSpec layers out before the general multi-group
Expand All @@ -2084,6 +2108,7 @@ def get_kv_cache_groups(
fallback_groups = _try_get_full_allocation_fallback_groups(kv_cache_spec)
if fallback_groups is None:
raise
_annotate_eagle_groups(vllm_config, kv_cache_spec, fallback_groups)
return fallback_groups
model_config = getattr(vllm_config, "model_config", None)
hf_config = getattr(model_config, "hf_config", None)
Expand Down Expand Up @@ -2132,6 +2157,7 @@ def get_kv_cache_groups(
aligned = replace(spec, block_size=new_bs, page_size_padded=common_page)
groups.append(KVCacheGroupSpec([name], aligned))

_annotate_eagle_groups(vllm_config, kv_cache_spec, groups)
return groups


Expand Down
Loading