[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:
2026-10-09 20:01:26 +07:00
co-authored by Hinata
parent ad80cac06d
commit fba504d5bb
12 changed files with 463 additions and 16 deletions
+81 -12
View File
@@ -44,6 +44,42 @@ from vllm.v1.kv_cache_interface import (
MLAAttentionSpec,
)
# Ring-row tag contract. Every raw-key ring row is the key (bf16), padded to a
# whole int64, followed by a tail of four int64 words:
# (pos_t, pos_h, pos_w, tag), tag = logical_position + 1.
# The tail exists on every ring, with or without cached RoPE positions: the tag
# is what lets the reader prove the row it is about to pool was committed by
# this request for exactly that logical position. 0 is never a valid tag (so a
# cleared row can never validate) and the manager clears the tag word of every
# newly claimed ring block before its new owner's first step.
QSA_RING_TAIL_WORDS = 4
QSA_RING_TAG_WORD = 3
QSA_RING_BF16_PER_INT64 = 4
QSA_RING_TAIL_ELEMENTS = QSA_RING_TAIL_WORDS * QSA_RING_BF16_PER_INT64
def qsa_ring_row_tags(logical_positions: torch.Tensor) -> torch.Tensor:
"""The tag the writer stores for each committed row (int64)."""
return logical_positions.to(torch.int64) + 1
def qsa_ring_group_rows_valid(
found_tags: torch.Tensor,
source_positions: torch.Tensor,
historical: torch.Tensor,
) -> torch.Tensor:
"""Reference for the pre-indexer's per-group check (torch, any device).
``found_tags``/``source_positions``/``historical`` are ``[groups, ratio]``:
the tag word read from each source row's ring slot, the row's logical
position, and whether the row must come from the ring (it precedes the
chunk start). Returns a ``[groups]`` bool: True when EVERY historical row
carries ``source_position + 1``. In-chunk rows are never checked.
"""
expected = qsa_ring_row_tags(source_positions)
bad = historical & (found_tags.to(torch.int64) != expected)
return ~bad.any(dim=-1)
def canonical_qsa_rope_positions(positions: torch.Tensor) -> torch.Tensor:
"""Return exact per-token positions as ``[tokens, 1, 3]`` int64 rows."""
@@ -868,37 +904,65 @@ class _QSAStateCache(nn.Module, AttentionLayerBase):
class QSAKeyStateCache(_QSAStateCache):
"""Raw BF16 key, optionally followed by exact int64 MRoPE positions."""
"""Raw BF16 key followed by the int64 ring tail (pos_t, pos_h, pos_w, tag).
_BF16_PER_INT64 = 4
The position words are consumed only when ``cache_rope_positions``; the tag
word is always written and always validated."""
_BF16_PER_INT64 = QSA_RING_BF16_PER_INT64
_NUM_ROPE_AXES = 3
def __init__(self, *, cache_rope_positions: bool = False, **kwargs) -> None:
key_head_size = int(kwargs.pop("head_size"))
self.key_head_size = key_head_size
self.cache_rope_positions = bool(cache_rope_positions)
# Element offset of the int64 tail (the name predates the tag word).
self.rope_position_offset = (
(key_head_size + self._BF16_PER_INT64 - 1) // self._BF16_PER_INT64
) * self._BF16_PER_INT64
storage_head_size = key_head_size
if self.cache_rope_positions:
storage_head_size = self.rope_position_offset + (
self._NUM_ROPE_AXES * self._BF16_PER_INT64
)
storage_head_size = self.rope_position_offset + QSA_RING_TAIL_ELEMENTS
super().__init__(head_size=storage_head_size, **kwargs)
self._ring_tag_cache: torch.Tensor | None = None
self._ring_tail: torch.Tensor | None = None
def bind_kv_cache(self, kv_cache: torch.Tensor) -> None:
super().bind_kv_cache(kv_cache)
qsa_cache = self.kv_cache
tail_end = self.rope_position_offset + QSA_RING_TAIL_ELEMENTS
tail = qsa_cache[..., self.rope_position_offset : tail_end].view(torch.int64)
assert tail.shape[-1] == QSA_RING_TAIL_WORDS
# Derived on access so `kv_cache` stays the only reference to the bound
# storage: clearing it (e.g. after CUDA graph memory profiling) must free
# the cache, or the freed block stays pinned in the allocator. Only the
# tag word is read outside a bind, so it is the one cached attribute.
self._ring_tail = tail
self._ring_tag_cache = tail[..., QSA_RING_TAG_WORD : QSA_RING_TAG_WORD + 1]
# Derived on access so `kv_cache` stays the only reference to the bound
# storage: clearing it (e.g. after CUDA graph memory profiling) must free
# the cache, or the freed block stays pinned in the allocator.
@property
def key_cache(self) -> torch.Tensor:
return self.kv_cache[..., : self.key_head_size]
@property
def rope_position_cache(self) -> torch.Tensor | None:
if not self.cache_rope_positions:
if not self.cache_rope_positions or self._ring_tail is None:
return None
return self.kv_cache[..., self.rope_position_offset :].view(torch.int64)
return self._ring_tail[..., : self._NUM_ROPE_AXES]
@property
def ring_tag_cache(self) -> torch.Tensor | None:
return self._ring_tag_cache
def clear_ring_row_tags(self, block_ids: torch.Tensor) -> None:
"""Zero the tag word of every row of ``block_ids`` (ring-row tag claim clear).
Only the tag word: a claimed ring block's keys and positions stay as
the previous occupant left them, and the zero tag is what makes every
such row fail validation until this request commits it."""
if self._ring_tag_cache is None:
raise RuntimeError(
f"{self.prefix}: ring tag clear before the KV cache was bound"
)
self._ring_tag_cache.index_fill_(0, block_ids, 0)
def get_kv_cache_spec(self, vllm_config: VllmConfig) -> KVCacheSpec:
# Hold the open group's committed keys plus every row a speculative
@@ -936,6 +1000,9 @@ class QSACompressedKeyCache(_QSAStateCache):
__all__ = [
"QSA_RING_TAG_WORD",
"QSA_RING_TAIL_ELEMENTS",
"QSA_RING_TAIL_WORDS",
"QSACompressedKeyCache",
"QSAForwardMetadata",
"QSAKeyStateCache",
@@ -944,4 +1011,6 @@ __all__ = [
"canonical_qsa_rope_positions",
"circular_qsa_slot_mapping",
"compressed_qsa_slot_mapping",
"qsa_ring_group_rows_valid",
"qsa_ring_row_tags",
]