Promote prefix_cache_retention_interval to an argument and change the default to 0 (#52216)

Signed-off-by: Tyler Michael Smith <tlrmchlsmth@gmail.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Tyler Michael Smith
2026-08-17 13:20:50 +00:00
committed by GitHub
co-authored by OpenAI Codex
parent 1d3a8b9e22
commit 017e9f4448
17 changed files with 160 additions and 63 deletions
+22 -1
View File
@@ -7,7 +7,7 @@ from typing import Any, ClassVar, Literal
from pydantic import Field, field_validator, model_validator
from vllm.config.utils import config
from vllm.config.utils import config, get_from_deprecated_env_if_set
from vllm.logger import init_logger
from vllm.utils.torch_utils import (
is_quantized_kv_cache,
@@ -35,6 +35,17 @@ CacheDType = Literal[
"nvfp4",
"nvfp4_4over6",
]
def _get_prefix_cache_retention_interval() -> int | None:
env_value = get_from_deprecated_env_if_set(
"VLLM_PREFIX_CACHE_RETENTION_INTERVAL",
"v0.29",
"prefix_cache_retention_interval",
)
return 0 if env_value is None else int(env_value)
MambaDType = Literal["auto", "float32", "float16", "bfloat16"]
MambaCacheMode = Literal["all", "align", "none"]
PrefixCachingHashAlgo = Literal["sha256", "sha256_cbor", "xxhash", "xxhash_cbor"]
@@ -111,6 +122,15 @@ class CacheConfig:
security risk tolerance against the performance benefits before turning this on.
- "xxhash_cbor" combines canonical CBOR serialization with xxHash for
reproducible hashing. Requires the optional ``xxhash`` package."""
prefix_cache_retention_interval: int | None = Field(
default_factory=_get_prefix_cache_retention_interval, ge=0
)
"""Token interval between retained sliding-window and Mamba prefix-cache
checkpoints. ``0`` retains only semantic checkpoints, including the latest
replay boundary and shared-prefix junctions. Positive values additionally
retain periodic checkpoints at the specified interval, which must be a
multiple of the scheduler block size. ``None`` retains checkpoints densely.
Applies only to sliding-window and Mamba cache groups."""
kv_cache_dtype_skip_layers: list[str] = field(default_factory=list)
"""Layer patterns to skip KV cache quantization. Accepts layer indices
(e.g., '0', '2', '4') or attention type names (e.g., 'sliding_window')."""
@@ -218,6 +238,7 @@ class CacheConfig:
"num_gpu_blocks_override",
"enable_prefix_caching",
"prefix_caching_hash_algo",
"prefix_cache_retention_interval",
# Prefix-caching implementation detail (doesn't affect compiled graph).
"prefix_match_unit",
"mamba_page_size_padded",
@@ -1433,7 +1433,7 @@ class MooncakeStoreWorker:
scheduler_block_size=self.block_size,
hash_block_size=self.hash_block_size,
use_eagle=use_eagle,
retention_interval=envs.VLLM_PREFIX_CACHE_RETENTION_INTERVAL,
retention_interval=kv_cache_config.prefix_cache_retention_interval,
)
# One ChunkedTokenDatabase per group; addresses populated in
# register_kv_caches once the kv-cache layout is known. Each group's
+8
View File
@@ -522,6 +522,9 @@ class EngineArgs:
prefix_caching_hash_algo: PrefixCachingHashAlgo = (
CacheConfig.prefix_caching_hash_algo
)
prefix_cache_retention_interval: int | None = get_field(
CacheConfig, "prefix_cache_retention_interval"
)
disable_sliding_window: bool = ModelConfig.disable_sliding_window
disable_cascade_attn: bool = ModelConfig.disable_cascade_attn
offload_backend: str = OffloadConfig.offload_backend
@@ -1224,6 +1227,10 @@ class EngineArgs:
cache_group.add_argument(
"--prefix-caching-hash-algo", **cache_kwargs["prefix_caching_hash_algo"]
)
cache_group.add_argument(
"--prefix-cache-retention-interval",
**cache_kwargs["prefix_cache_retention_interval"],
)
cache_group.add_argument(
"--kv-cache-dtype-skip-layers", **cache_kwargs["kv_cache_dtype_skip_layers"]
)
@@ -2001,6 +2008,7 @@ class EngineArgs:
sliding_window=sliding_window,
enable_prefix_caching=self.enable_prefix_caching,
prefix_caching_hash_algo=self.prefix_caching_hash_algo,
prefix_cache_retention_interval=self.prefix_cache_retention_interval,
kv_cache_dtype_skip_layers=self.kv_cache_dtype_skip_layers,
kv_sharing_fast_prefill=self.kv_sharing_fast_prefill,
mamba_cache_dtype=self.mamba_cache_dtype,
-6
View File
@@ -1148,12 +1148,6 @@ environment_variables: dict[str, Callable[[], Any]] = {
if "VLLM_PLUGINS" not in os.environ
else os.environ["VLLM_PLUGINS"].split(",")
),
# Retain local sliding-window KV checkpoints for prefix caching.
# Unset (default) preserves the dense local checkpointing behavior. `0`
# retains only the latest completed prompt boundary. Positive values retain
# checkpoints at the specified interval boundaries (rounded up to the
# prefix-cache alignment).
# Applies to sliding-window attention for now but not yet Mamba/linear attention.
"VLLM_PREFIX_CACHE_RETENTION_INTERVAL": lambda: (
int(os.environ["VLLM_PREFIX_CACHE_RETENTION_INTERVAL"])
if "VLLM_PREFIX_CACHE_RETENTION_INTERVAL" in os.environ
+6 -5
View File
@@ -4,7 +4,6 @@ from abc import ABC, abstractmethod
from collections.abc import Sequence
from typing import NamedTuple
from vllm import envs
from vllm.logger import init_logger
from vllm.utils.math_utils import cdiv, round_down
from vllm.v1.core.block_pool import BlockPool
@@ -45,16 +44,18 @@ def _validate_prefix_cache_retention_interval(
isinstance(g.kv_cache_spec, (SlidingWindowSpec, MambaSpec))
for g in kv_cache_config.kv_cache_groups
):
if retention_interval == 0:
return
raise ValueError(
"VLLM_PREFIX_CACHE_RETENTION_INTERVAL is set but this model has "
"prefix_cache_retention_interval is set but this model has "
"no sliding-window or Mamba KV cache group, so retention has no "
"effect. Unset it (it only applies to sliding-window and Mamba "
"effect. Set it to 0 (it only applies to sliding-window and Mamba "
"attention)."
)
if retention_interval < 0 or retention_interval % scheduler_block_size != 0:
raise ValueError(
f"VLLM_PREFIX_CACHE_RETENTION_INTERVAL ({retention_interval}) "
f"prefix_cache_retention_interval ({retention_interval}) "
"must be non-negative and a multiple of scheduler_block_size "
f"({scheduler_block_size})."
)
@@ -151,7 +152,7 @@ class KVCacheCoordinator(ABC):
# A positive retention interval must be a multiple of the base hit granularity
# (``scheduler_block_size``) to land on real cache-hit boundaries.
# 0 = keep only the latest replay boundary; None = dense;
self.retention_interval = envs.VLLM_PREFIX_CACHE_RETENTION_INTERVAL
self.retention_interval = kv_cache_config.prefix_cache_retention_interval
_validate_prefix_cache_retention_interval(
self.retention_interval, self.scheduler_block_size, kv_cache_config
)
+1 -1
View File
@@ -243,7 +243,7 @@ class KVCacheManager:
- ``shared_prefix_boundary``: the block-aligned token position of
a shared prefix that a sparse-retention group (Mamba / sliding
window) has not cached yet (Marconi-style APC), or 0 if none.
Pinned so ``VLLM_PREFIX_CACHE_RETENTION_INTERVAL`` does not drop
Pinned so sparse prefix-cache retention does not drop
the junction and defeat cross-request reuse.
"""
# We skip finding the prefix cache hit when prefix caching is
+6
View File
@@ -1347,6 +1347,9 @@ def get_kv_cache_config_from_groups(
num_blocks=1,
kv_cache_tensors=[],
kv_cache_groups=kv_cache_groups,
prefix_cache_retention_interval=(
vllm_config.cache_config.prefix_cache_retention_interval
),
)
# Determine how model runners should initialize the KV cache tensors.
@@ -1406,6 +1409,9 @@ def get_kv_cache_config_from_groups(
num_blocks=num_blocks,
kv_cache_tensors=kv_cache_tensors,
kv_cache_groups=kv_cache_groups,
prefix_cache_retention_interval=(
vllm_config.cache_config.prefix_cache_retention_interval
),
)
+2
View File
@@ -970,6 +970,8 @@ class KVCacheConfig:
For models with multiple types of attention, there will be multiple groups,
see `_get_kv_cache_config_uniform_page_size` for more details.
"""
prefix_cache_retention_interval: int | None = None
"""Resolved retention policy for local prefix-cache checkpoints."""
@property
def has_mamba_layers(self) -> bool:
+3 -4
View File
@@ -4,7 +4,7 @@
import contextlib
from collections.abc import Iterable
from dataclasses import dataclass, field
from dataclasses import dataclass, field, replace
from typing import TYPE_CHECKING, Any
from vllm.config import VllmConfig
@@ -190,7 +190,6 @@ class SimpleCPUOffloadScheduler:
"""Derive a CPU KVCacheConfig from the GPU config.
Same kv_cache_groups, num_blocks scaled by CPU/GPU memory ratio."""
# Import here to avoid potential circular imports
from vllm.v1.kv_cache_interface import KVCacheConfig as KVCacheConfigCls
from vllm.v1.kv_cache_interface import KVCacheTensor
assert len(gpu_config.kv_cache_tensors) > 0
@@ -215,10 +214,10 @@ class SimpleCPUOffloadScheduler:
for t in gpu_config.kv_cache_tensors
]
return KVCacheConfigCls(
return replace(
gpu_config,
num_blocks=num_cpu_blocks,
kv_cache_tensors=cpu_tensors,
kv_cache_groups=gpu_config.kv_cache_groups,
)
@staticmethod