[QSA] Tag every raw-key ring row and validate it before pooling
The fused pre-indexer could read a stale or uncommitted raw-key ring row and pass its int64 tail to RoPE as a position (illegal address). The writer now tags each row with logical_position + 1; the reader checks the tag of every source row and, on a mismatch, masks the pooled load, the RoPE-tail read and the compressed-slot store. New guard site 11 (always fatal under warn). The KV pool size is unchanged. Co-Authored-By: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
@@ -867,6 +867,21 @@ class KVCacheManager:
|
||||
ids.extend(mgr.take_new_block_ids())
|
||||
return ids
|
||||
|
||||
def take_new_ring_block_ids(self) -> list[int]:
|
||||
"""Drain ring blocks claimed since the last call (ring-row tag clear).
|
||||
Independent of KV zeroing: every claim is returned."""
|
||||
ids: list[int] = []
|
||||
for mgr in self.coordinator.single_type_managers:
|
||||
ids.extend(mgr.take_new_ring_block_ids())
|
||||
return ids
|
||||
|
||||
def get_ring_block_ids(self, request_id: str) -> list[int]:
|
||||
"""The request's ring (CircularBufferSpec) block ids, all groups."""
|
||||
ids: list[int] = []
|
||||
for mgr in self.coordinator.single_type_managers:
|
||||
ids.extend(mgr.get_ring_block_ids(request_id))
|
||||
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.
|
||||
|
||||
@@ -301,6 +301,13 @@ class SchedulerOutput:
|
||||
# preventing stale NaN/data from corrupting attention or SSM computation.
|
||||
new_block_ids_to_zero: list[int] | None = None
|
||||
|
||||
# v2.2 ring-row tag fix: ring (CircularBufferSpec) blocks claimed during this
|
||||
# scheduling step. Independent of new_block_ids_to_zero and of
|
||||
# needs_kv_cache_zeroing: the worker clears only the rows' tag words, after
|
||||
# the zeroer and before any copy or forward of this step, i.e. before the
|
||||
# new owner's first read or write of the ring.
|
||||
new_ring_block_ids_to_clear: list[int] | None = None
|
||||
|
||||
# CoW copies to apply after zeroing new blocks and before forward.
|
||||
kv_cache_block_copies: list[KVCacheBlockCopy] | None = None
|
||||
|
||||
|
||||
@@ -366,6 +366,11 @@ class Scheduler(SchedulerInterface):
|
||||
# Bind after construction so connectors can access the cache manager.
|
||||
if self.connector is not None:
|
||||
self.connector.bind_kv_cache_manager(self.kv_cache_manager)
|
||||
# Ring blocks an async KV load is about to fill. Their tag clear
|
||||
# is skipped for the same reason as zeroing (it could land after the
|
||||
# out-of-band write and wipe the producer's committed tags). Tracked
|
||||
# whatever needs_kv_cache_zeroing says, like the clear itself.
|
||||
self._skip_ring_clear_block_ids: set[int] = set()
|
||||
|
||||
self.use_pp = self.parallel_config.pipeline_parallel_size > 1
|
||||
self.use_v2_model_runner = vllm_config.use_v2_model_runner
|
||||
@@ -1333,6 +1338,12 @@ class Scheduler(SchedulerInterface):
|
||||
request.num_computed_tokens = num_computed_tokens
|
||||
self._inflight_prefills.add(request)
|
||||
self._set_kv_fetch_stage(request, KV_FETCH_IN_PROGRESS)
|
||||
# Same race for the ring's tag clear; the load
|
||||
# brings the producer's rows and tags (or none, and the
|
||||
# reader then fails closed on the missing history).
|
||||
self._skip_ring_clear_block_ids.update(
|
||||
self.kv_cache_manager.get_ring_block_ids(request.request_id)
|
||||
)
|
||||
skip_request(request_queue)
|
||||
continue
|
||||
|
||||
@@ -1525,6 +1536,8 @@ class Scheduler(SchedulerInterface):
|
||||
finished_req_ids=self.finished_req_ids,
|
||||
free_encoder_mm_hashes=self.encoder_cache_manager.get_freed_mm_hashes(),
|
||||
new_block_ids_to_zero=self._get_new_block_ids_to_zero(),
|
||||
# Drained every step, never gated on KV zeroing.
|
||||
new_ring_block_ids_to_clear=self._get_new_ring_block_ids_to_clear(),
|
||||
has_sync_kv_loads=has_sync_kv_loads,
|
||||
kv_cache_block_copies=pending_kv_cache_block_copies,
|
||||
kv_connector_block_state=kv_connector_block_state,
|
||||
@@ -1572,6 +1585,17 @@ class Scheduler(SchedulerInterface):
|
||||
) -> KVConnectorMetadata:
|
||||
return connector.build_connector_meta(scheduler_output)
|
||||
|
||||
def _get_new_ring_block_ids_to_clear(self) -> list[int] | None:
|
||||
"""Ring claims of this step whose tag words the worker clears.
|
||||
Unconditional (not under needs_kv_cache_zeroing); only blocks an async
|
||||
KV load is about to write are left out."""
|
||||
ids = self.kv_cache_manager.take_new_ring_block_ids()
|
||||
if self._skip_ring_clear_block_ids:
|
||||
skip = self._skip_ring_clear_block_ids
|
||||
ids = [b for b in ids if b not in skip]
|
||||
skip.clear()
|
||||
return ids or None
|
||||
|
||||
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.
|
||||
|
||||
@@ -121,6 +121,12 @@ class SingleTypeKVCacheManager(ABC):
|
||||
and not isinstance(kv_cache_spec, CircularBufferSpec)
|
||||
)
|
||||
self.new_block_ids: list[int] = []
|
||||
# v2.2 ring-row tag fix: ring blocks claimed since the last drain. The ring stays
|
||||
# OUT of whole-block zeroing above; instead every claim is recorded
|
||||
# UNCONDITIONALLY (never under needs_kv_cache_zeroing) so the worker
|
||||
# clears the rows' tag words before the new owner's first step. Only
|
||||
# CircularBufferManager appends here.
|
||||
self.new_ring_block_ids: list[int] = []
|
||||
|
||||
# Mapping from request ID to blocks to track the blocks allocated
|
||||
# for each request, so that we can free the blocks when the request
|
||||
@@ -422,6 +428,17 @@ class SingleTypeKVCacheManager(ABC):
|
||||
self.new_block_ids = []
|
||||
return ids
|
||||
|
||||
def take_new_ring_block_ids(self) -> list[int]:
|
||||
"""Drain ring blocks claimed since the last call; empty for
|
||||
every manager but CircularBufferManager."""
|
||||
ids = self.new_ring_block_ids
|
||||
self.new_ring_block_ids = []
|
||||
return ids
|
||||
|
||||
def get_ring_block_ids(self, request_id: str) -> list[int]:
|
||||
"""The request's ring block ids; empty for non-ring managers."""
|
||||
return []
|
||||
|
||||
def take_pending_cow_copies(
|
||||
self,
|
||||
) -> list[tuple[KVCacheBlock, KVCacheBlock]]:
|
||||
@@ -1214,8 +1231,37 @@ class CircularBufferManager(FullAttentionManager):
|
||||
req_blocks.extend(new_blocks)
|
||||
if self._record_new_block_ids and record_for_zeroing:
|
||||
self.new_block_ids.extend(block.block_id for block in new_blocks)
|
||||
# Unconditional -- not gated on needs_kv_cache_zeroing.
|
||||
self.new_ring_block_ids.extend(block.block_id for block in new_blocks)
|
||||
return new_blocks
|
||||
|
||||
def take_new_ring_block_ids(self) -> list[int]:
|
||||
"""Drain the ring blocks claimed since the last call, keeping only
|
||||
blocks a request of this manager still holds, in claim order, once.
|
||||
|
||||
A block claimed and then freed within the same scheduling pass (the
|
||||
request was preempted after its allocation) may already belong to
|
||||
another group by the time the worker acts; clearing its tag words
|
||||
there would write into that group's page, so it is dropped here.
|
||||
"""
|
||||
claimed = self.new_ring_block_ids
|
||||
self.new_ring_block_ids = []
|
||||
if not claimed:
|
||||
return []
|
||||
held = {
|
||||
block.block_id for blocks in self.req_to_blocks.values() for block in blocks
|
||||
}
|
||||
out: list[int] = []
|
||||
seen: set[int] = set()
|
||||
for block_id in claimed:
|
||||
if block_id in held and block_id not in seen:
|
||||
seen.add(block_id)
|
||||
out.append(block_id)
|
||||
return out
|
||||
|
||||
def get_ring_block_ids(self, request_id: str) -> list[int]:
|
||||
return [block.block_id for block in self.req_to_blocks.get(request_id, ())]
|
||||
|
||||
def get_num_blocks_to_allocate(
|
||||
self,
|
||||
request_id: str,
|
||||
|
||||
Reference in New Issue
Block a user