[KV Connector] Fix PD async scheduling race condition for hybrid attn models (#48481)
Signed-off-by: Artem Perevedentsev <aperevedents@nvidia.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: llx-08 <2596671364@qq.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
llx-08
Nick Hill
Claude Fable 5
parent
a317bc5739
commit
530852f959
@@ -681,6 +681,34 @@ class KVCacheManager:
|
||||
ids.extend(mgr.take_new_block_ids())
|
||||
return ids
|
||||
|
||||
def get_zeroing_block_ids_in_range(
|
||||
self, request_id: str, start_token: int, end_token: int
|
||||
) -> list[int]:
|
||||
"""The request's block ids covering [start_token, end_token), from
|
||||
the groups whose new blocks are zeroed by the worker."""
|
||||
ids: list[int] = []
|
||||
for mgr in self.coordinator.single_type_managers:
|
||||
if mgr.records_new_block_ids:
|
||||
start_idx = start_token // mgr.block_size
|
||||
end_idx = cdiv(end_token, mgr.block_size)
|
||||
blocks = mgr.req_to_blocks[request_id]
|
||||
ids.extend(blk.block_id for blk in blocks[start_idx:end_idx])
|
||||
return ids
|
||||
|
||||
def record_blocks_for_zeroing(self, request_id: str, start_token: int) -> None:
|
||||
"""Re-record the request's blocks from start_token onwards for
|
||||
zeroing, e.g. blocks a failed async KV load left unwritten.
|
||||
|
||||
start_token must be block-aligned: zeroing a partially-valid block
|
||||
would wipe its valid prefix.
|
||||
"""
|
||||
for mgr in self.coordinator.single_type_managers:
|
||||
if mgr.records_new_block_ids:
|
||||
assert start_token % mgr.block_size == 0
|
||||
start_idx = start_token // mgr.block_size
|
||||
blocks = mgr.req_to_blocks[request_id]
|
||||
mgr.new_block_ids.extend(blk.block_id for blk in blocks[start_idx:])
|
||||
|
||||
def take_kv_cache_block_copies(
|
||||
self,
|
||||
) -> tuple[list[KVCacheBlockCopy], list[KVCacheBlock]]:
|
||||
|
||||
@@ -297,6 +297,9 @@ class Scheduler(SchedulerInterface):
|
||||
|
||||
self.has_mamba_layers = kv_cache_config.has_mamba_layers
|
||||
self.needs_kv_cache_zeroing = kv_cache_config.needs_kv_cache_zeroing
|
||||
# Blocks that async KV loads will overwrite this step, skipped from
|
||||
# zeroing since the zeroing could race the out-of-band write.
|
||||
self._skip_zero_block_ids: set[int] = set()
|
||||
self.need_mamba_block_aligned_split = (
|
||||
self.has_mamba_layers and self.cache_config.mamba_cache_mode == "align"
|
||||
)
|
||||
@@ -986,6 +989,16 @@ class Scheduler(SchedulerInterface):
|
||||
# only the successfully loaded tokens.
|
||||
request.num_computed_tokens = num_computed_tokens
|
||||
self._inflight_prefills.add(request)
|
||||
if self.needs_kv_cache_zeroing:
|
||||
# Skip zeroing of the blocks the async load will
|
||||
# overwrite; the zeroing could race the write.
|
||||
self._skip_zero_block_ids.update(
|
||||
self.kv_cache_manager.get_zeroing_block_ids_in_range(
|
||||
request.request_id,
|
||||
num_new_local_computed_tokens,
|
||||
num_computed_tokens,
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
||||
self.running.append(request)
|
||||
@@ -1098,12 +1111,6 @@ class Scheduler(SchedulerInterface):
|
||||
self.prev_step_scheduled_req_ids.clear()
|
||||
self.prev_step_scheduled_req_ids.update(num_scheduled_tokens.keys())
|
||||
|
||||
# Drain new attention block ids every step so the manager-side list
|
||||
# does not grow unbounded; only kv-cache zeroing consumes them.
|
||||
new_attn_block_ids = self.kv_cache_manager.take_new_block_ids()
|
||||
new_block_ids_to_zero = (
|
||||
(new_attn_block_ids or None) if self.needs_kv_cache_zeroing else None
|
||||
)
|
||||
kv_cache_block_copies, cow_retained_blocks = (
|
||||
self.kv_cache_manager.take_kv_cache_block_copies()
|
||||
)
|
||||
@@ -1147,7 +1154,7 @@ class Scheduler(SchedulerInterface):
|
||||
# the previous and the current steps.
|
||||
finished_req_ids=self.finished_req_ids,
|
||||
free_encoder_mm_hashes=self.encoder_cache_manager.get_freed_mm_hashes(),
|
||||
new_block_ids_to_zero=new_block_ids_to_zero,
|
||||
new_block_ids_to_zero=self._get_new_block_ids_to_zero(),
|
||||
kv_cache_block_copies=pending_kv_cache_block_copies,
|
||||
num_spec_tokens_to_schedule=num_spec_tokens_to_schedule,
|
||||
)
|
||||
@@ -1181,6 +1188,20 @@ class Scheduler(SchedulerInterface):
|
||||
) -> KVConnectorMetadata:
|
||||
return connector.build_connector_meta(scheduler_output)
|
||||
|
||||
def _get_new_block_ids_to_zero(self) -> list[int] | None:
|
||||
# Drain new attention block ids every step so the manager-side list
|
||||
# does not grow unbounded; only kv-cache zeroing consumes them.
|
||||
new_block_ids_to_zero = self.kv_cache_manager.take_new_block_ids()
|
||||
if not self.needs_kv_cache_zeroing:
|
||||
return None
|
||||
|
||||
if self._skip_zero_block_ids:
|
||||
skip = self._skip_zero_block_ids
|
||||
new_block_ids_to_zero = [b for b in new_block_ids_to_zero if b not in skip]
|
||||
skip.clear()
|
||||
|
||||
return new_block_ids_to_zero or None
|
||||
|
||||
def _preempt_request(self, request: Request, timestamp: float) -> None:
|
||||
"""Preempt a request and put it back to the waiting queue.
|
||||
|
||||
@@ -2518,9 +2539,18 @@ class Scheduler(SchedulerInterface):
|
||||
if request.num_computed_tokens:
|
||||
# Cache any valid computed tokens.
|
||||
self.kv_cache_manager.cache_blocks(request, request.num_computed_tokens)
|
||||
if self.needs_kv_cache_zeroing:
|
||||
# The failed load left the blocks beyond the valid
|
||||
# prefix unwritten and their zeroing was skipped; zero
|
||||
# them before they are recomputed locally.
|
||||
self.kv_cache_manager.record_blocks_for_zeroing(
|
||||
request.request_id, request.num_computed_tokens
|
||||
)
|
||||
else:
|
||||
# No valid computed tokens, release allocated blocks.
|
||||
# There may be a local cache hit on retry.
|
||||
# (Freed blocks are re-recorded for zeroing when
|
||||
# reallocated, so the skipped blocks need no handling.)
|
||||
self.kv_cache_manager.free(request)
|
||||
|
||||
self.failed_recving_kv_req_ids.remove(request.request_id)
|
||||
|
||||
@@ -359,6 +359,11 @@ class SingleTypeKVCacheManager(ABC):
|
||||
self.new_block_ids.extend(b.block_id for b in new_blocks)
|
||||
return cow_blocks + new_blocks
|
||||
|
||||
@property
|
||||
def records_new_block_ids(self) -> bool:
|
||||
"""Whether this manager's new blocks are zeroed by the worker."""
|
||||
return self._record_new_block_ids
|
||||
|
||||
def take_new_block_ids(self) -> list[int]:
|
||||
"""Drain and return block IDs allocated since the last call."""
|
||||
ids = self.new_block_ids
|
||||
|
||||
Reference in New Issue
Block a user