[BugFix] Resolve multiple async kv load deadlock (#44560)

Signed-off-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
Nick Hill
2026-06-06 23:05:47 +00:00
committed by GitHub
parent 062b05ff3a
commit 3b3d5287fa
6 changed files with 214 additions and 1 deletions
+8 -1
View File
@@ -246,6 +246,7 @@ class KVCacheManager:
delay_cache_blocks: bool = False,
num_encoder_tokens: int = 0,
full_sequence_must_fit: bool = False,
reserved_blocks: int = 0,
) -> KVCacheBlocks | None:
"""Add slots for a request with new tokens to append.
@@ -271,6 +272,11 @@ class KVCacheManager:
free blocks to hold the full sequence, accounting for prefix cache hits
and sliding window. Used as an admission gate to prevent over-admitting
requests when chunked prefill would otherwise only check the first chunk
reserved_blocks: Number of free blocks that must be left available for
other in-flight sequences to complete. The actual allocation is only
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.
Blocks layout:
```
@@ -386,7 +392,8 @@ class KVCacheManager:
num_tokens_main_model=num_tokens_main_model,
)
if num_blocks_to_allocate > self.block_pool.get_num_free_blocks():
available_blocks = self.block_pool.get_num_free_blocks() - reserved_blocks
if num_blocks_to_allocate > available_blocks:
# Cannot allocate new blocks
return None
+46
View File
@@ -286,6 +286,10 @@ class Scheduler(SchedulerInterface):
self._pause_state: PauseState = PauseState.UNPAUSED
# In-flight requests still prefilling (prefill chunks + in-progress
# async KV loads). Their remaining-block reservation gates async loads.
self._inflight_prefills: set[Request] = set()
def _mamba_block_aligned_split(
self,
request: Request,
@@ -746,6 +750,14 @@ class Scheduler(SchedulerInterface):
for i in encoder_inputs_to_schedule
)
reserved_blocks = 0
if load_kv_async:
# An async load holds its blocks for the whole transfer with
# no forward progress and isn't preemptible here. Admit it
# only if it fits in (free - other in-flight reservations), to
# avoid deadlock and predictable preemptions.
reserved_blocks = self._inflight_prefill_reserved_blocks()
new_blocks = self.kv_cache_manager.allocate_slots(
request,
num_new_tokens,
@@ -756,6 +768,7 @@ class Scheduler(SchedulerInterface):
delay_cache_blocks=load_kv_async,
num_encoder_tokens=num_encoder_tokens,
full_sequence_must_fit=self.scheduler_reserve_full_isl,
reserved_blocks=reserved_blocks,
)
if new_blocks is None:
@@ -807,6 +820,7 @@ class Scheduler(SchedulerInterface):
# _update_waiting_for_remote_kv will then cache
# only the successfully loaded tokens.
request.num_computed_tokens = num_computed_tokens
self._inflight_prefills.add(request)
continue
self.running.append(request)
@@ -830,6 +844,9 @@ class Scheduler(SchedulerInterface):
token_budget -= num_new_tokens
request.status = RequestStatus.RUNNING
request.num_computed_tokens = num_computed_tokens
# Only track requests that will still be prefilling after this chunk.
if num_computed_tokens + num_new_tokens < request.num_tokens:
self._inflight_prefills.add(request)
# Encoder-related.
if encoder_inputs_to_schedule:
scheduled_encoder_inputs[request_id] = encoder_inputs_to_schedule
@@ -965,6 +982,7 @@ class Scheduler(SchedulerInterface):
)
self.kv_cache_manager.free(request)
self.encoder_cache_manager.free(request)
self._inflight_prefills.discard(request)
request.status = RequestStatus.PREEMPTED
request.num_computed_tokens = 0
if request.spec_token_ids:
@@ -996,6 +1014,9 @@ class Scheduler(SchedulerInterface):
scheduler_output.has_structured_output_requests |= (
request.use_structured_output and not request.is_prefill_chunk
)
# Drop from the in-flight-prefill set once it's no longer prefilling.
if not request.is_prefill_chunk:
self._inflight_prefills.discard(request)
# Snapshot block IDs for routed experts before forward starts.
# A concurrent schedule() may preempt requests and free blocks
@@ -1869,6 +1890,7 @@ class Scheduler(SchedulerInterface):
) -> dict[str, Any] | None:
assert request.is_finished()
self._inflight_prefills.discard(request)
connector_delay_free_blocks, kv_xfer_params = self._connector_finished(request)
self.encoder_cache_manager.free(request)
request_id = request.request_id
@@ -2105,6 +2127,30 @@ class Scheduler(SchedulerInterface):
return self.connector.request_finished_all_groups(request, block_ids)
def _request_remaining_blocks(self, request: Request) -> int:
"""Blocks `request` still needs to allocate to hold its full sequence."""
full_num_tokens = min(request.num_tokens, self.max_model_len)
return self.kv_cache_manager.coordinator.get_num_blocks_to_allocate(
request_id=request.request_id,
num_tokens=full_num_tokens,
new_computed_blocks=self.kv_cache_manager.empty_kv_cache_blocks.blocks,
num_encoder_tokens=0,
total_computed_tokens=request.num_computed_tokens,
num_tokens_main_model=full_num_tokens,
apply_admission_cap=True,
)
def _inflight_prefill_reserved_blocks(self) -> int:
"""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
)
def _update_waiting_for_remote_kv(self, request: Request) -> None:
"""
KV Connector: update request state after async recv is finished.