[Core] Add kvcache watermark to reduce preemptions (#44594)
Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
23eb7c8fbb
commit
4085ff7cb4
@@ -143,6 +143,13 @@ class SchedulerConfig:
|
||||
checking the first chunk. Prevents over-admission and KV cache thrashing
|
||||
with chunked prefill."""
|
||||
|
||||
watermark: float = Field(default=0.0, ge=0.0, lt=1.0)
|
||||
"""Fraction of total KV cache blocks to keep free (the watermark) when
|
||||
admitting waiting or preempted requests into the running queue. This headroom
|
||||
helps avoid frequent KV cache eviction and the resulting repeated preemption
|
||||
of requests when GPU memory is scarce. Must be in the range [0.0, 1.0); 0.0
|
||||
(the default) disables the watermark."""
|
||||
|
||||
async_scheduling: bool | None = None
|
||||
"""If set to False, disable async scheduling. Async scheduling helps to
|
||||
avoid gaps in GPU utilization, leading to better latency and throughput.
|
||||
|
||||
@@ -600,6 +600,8 @@ class EngineArgs:
|
||||
|
||||
scheduler_reserve_full_isl: bool = SchedulerConfig.scheduler_reserve_full_isl
|
||||
|
||||
watermark: float = SchedulerConfig.watermark
|
||||
|
||||
disable_hybrid_kv_cache_manager: bool | None = (
|
||||
SchedulerConfig.disable_hybrid_kv_cache_manager
|
||||
)
|
||||
@@ -1408,6 +1410,7 @@ class EngineArgs:
|
||||
"--scheduler-reserve-full-isl",
|
||||
**scheduler_kwargs["scheduler_reserve_full_isl"],
|
||||
)
|
||||
scheduler_group.add_argument("--watermark", **scheduler_kwargs["watermark"])
|
||||
scheduler_group.add_argument(
|
||||
"--disable-hybrid-kv-cache-manager",
|
||||
**scheduler_kwargs["disable_hybrid_kv_cache_manager"],
|
||||
@@ -2045,6 +2048,7 @@ class EngineArgs:
|
||||
max_long_partial_prefills=self.max_long_partial_prefills,
|
||||
long_prefill_token_threshold=self.long_prefill_token_threshold,
|
||||
scheduler_reserve_full_isl=self.scheduler_reserve_full_isl,
|
||||
watermark=self.watermark,
|
||||
disable_hybrid_kv_cache_manager=self.disable_hybrid_kv_cache_manager,
|
||||
async_scheduling=self.async_scheduling,
|
||||
stream_interval=self.stream_interval,
|
||||
|
||||
@@ -17,7 +17,7 @@ from vllm.v1.kv_cache_interface import (
|
||||
get_kv_cache_spec_sliding_window,
|
||||
)
|
||||
from vllm.v1.metrics.stats import PrefixCacheStats
|
||||
from vllm.v1.request import Request
|
||||
from vllm.v1.request import Request, RequestStatus
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -122,6 +122,7 @@ class KVCacheManager:
|
||||
dcp_world_size: int = 1,
|
||||
pcp_world_size: int = 1,
|
||||
metrics_collector: KVCacheMetricsCollector | None = None,
|
||||
watermark: float = 0.0,
|
||||
) -> None:
|
||||
self.max_model_len = max_model_len
|
||||
# When unset, fall back to `max_model_len` so the recycling-aware cap
|
||||
@@ -155,6 +156,11 @@ class KVCacheManager:
|
||||
self.num_kv_cache_groups = len(kv_cache_config.kv_cache_groups)
|
||||
self.block_pool = self.coordinator.block_pool
|
||||
self.kv_cache_config = kv_cache_config
|
||||
|
||||
# Watermark: minimum number of KV cache blocks to keep free when
|
||||
# admitting waiting/preempted requests, to avoid frequent preemptions.
|
||||
assert watermark >= 0.0, "watermark must be non-negative"
|
||||
self.watermark_blocks = int(watermark * kv_cache_config.num_blocks)
|
||||
self.kv_cache_event_metadata = tuple(
|
||||
(
|
||||
get_kv_cache_spec_kind(group.kv_cache_spec).value,
|
||||
@@ -247,6 +253,7 @@ class KVCacheManager:
|
||||
num_encoder_tokens: int = 0,
|
||||
full_sequence_must_fit: bool = False,
|
||||
reserved_blocks: int = 0,
|
||||
has_scheduled_reqs: bool = True,
|
||||
) -> KVCacheBlocks | None:
|
||||
"""Add slots for a request with new tokens to append.
|
||||
|
||||
@@ -277,6 +284,8 @@ class KVCacheManager:
|
||||
made if it fits within (free blocks - reserved_blocks). Used to gate
|
||||
async KV-connector loads so their initial allocation cannot consume
|
||||
blocks an already in-flight (prefilling) sequence is relying on.
|
||||
has_scheduled_reqs: Whether any requests are already scheduled to run
|
||||
this step, controls whether watermark is applied.
|
||||
|
||||
Blocks layout:
|
||||
```
|
||||
@@ -351,6 +360,15 @@ class KVCacheManager:
|
||||
self.max_model_len,
|
||||
)
|
||||
|
||||
watermark_blocks = 0
|
||||
# The watermark is applied to waiting/preempted requests only, and only
|
||||
# when there's at least one request already scheduled.
|
||||
if has_scheduled_reqs and request.status in (
|
||||
RequestStatus.WAITING,
|
||||
RequestStatus.PREEMPTED,
|
||||
):
|
||||
watermark_blocks = self.watermark_blocks
|
||||
|
||||
if full_sequence_must_fit:
|
||||
# First check and fail if the full request sequence won't fit.
|
||||
full_num_tokens = min(request.num_tokens, self.max_model_len)
|
||||
@@ -364,7 +382,8 @@ class KVCacheManager:
|
||||
num_tokens_main_model=full_num_tokens,
|
||||
apply_admission_cap=True,
|
||||
)
|
||||
if num_blocks_to_allocate > self.block_pool.get_num_free_blocks():
|
||||
required_blocks = num_blocks_to_allocate + watermark_blocks
|
||||
if required_blocks > self.block_pool.get_num_free_blocks():
|
||||
return None
|
||||
|
||||
num_tokens_main_model = total_computed_tokens + num_new_tokens
|
||||
@@ -392,8 +411,11 @@ class KVCacheManager:
|
||||
num_tokens_main_model=num_tokens_main_model,
|
||||
)
|
||||
|
||||
# Keep `reserved_blocks` free for other in-flight sequences, and an
|
||||
# additional watermark of headroom for waiting/preempted admissions.
|
||||
available_blocks = self.block_pool.get_num_free_blocks() - reserved_blocks
|
||||
if num_blocks_to_allocate > available_blocks:
|
||||
required_blocks = num_blocks_to_allocate + watermark_blocks
|
||||
if required_blocks > available_blocks:
|
||||
# Cannot allocate new blocks
|
||||
return None
|
||||
|
||||
|
||||
@@ -242,6 +242,7 @@ class Scheduler(SchedulerInterface):
|
||||
scheduler_block_size=self.block_size,
|
||||
hash_block_size=hash_block_size,
|
||||
metrics_collector=self.kv_metrics_collector,
|
||||
watermark=self.scheduler_config.watermark,
|
||||
)
|
||||
# Bind GPU block pool to the KV connector. This must happen after
|
||||
# kv_cache_manager is constructed so block_pool is available.
|
||||
@@ -826,6 +827,7 @@ class Scheduler(SchedulerInterface):
|
||||
num_encoder_tokens=num_encoder_tokens,
|
||||
full_sequence_must_fit=self.scheduler_reserve_full_isl,
|
||||
reserved_blocks=reserved_blocks,
|
||||
has_scheduled_reqs=bool(self.running),
|
||||
)
|
||||
|
||||
if new_blocks is None:
|
||||
@@ -2198,12 +2200,8 @@ class Scheduler(SchedulerInterface):
|
||||
)
|
||||
|
||||
def _inflight_prefill_reserved_blocks(self) -> int:
|
||||
"""Blocks in-flight prefills still need to finish (their reservation).
|
||||
"""Num blocks in-flight prefills still need to finish (their reservation)."""
|
||||
|
||||
Sums remaining full-ISL blocks over `self._inflight_prefills` (running
|
||||
prefills + in-progress async loads). The candidate async load isn't yet
|
||||
in the set, so it's naturally excluded.
|
||||
"""
|
||||
return sum(
|
||||
self._request_remaining_blocks(req) for req in self._inflight_prefills
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user