[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
+9
View File
@@ -119,6 +119,15 @@ class QSAIndexer(nn.Module):
eps=float(getattr(config, "rms_norm_eps", 1e-6)),
)
# QSAKeyStateCache rows now carry
# a tag word the NVIDIA pre-indexer writes and validates; this path
# neither writes nor checks it, so it would run the widened layout
# with no ring validation at all. Refuse it rather than share it.
raise NotImplementedError(
"Qwen4Exp QSA on the AMD path is not supported on this fork: the "
"ring-row tag contract is implemented for the NVIDIA "
"pre-indexer only (vllm/models/qwen4_exp/nvidia/ops/qsa_pre_indexer.py)."
)
cache_config = vllm_config.cache_config
cache_prefix = f"{prefix}." if prefix else ""
self.raw_key_cache = QSAKeyStateCache(
+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",
]
@@ -20,6 +20,7 @@ from ..common.qsa_cache import (
QSAForwardMetadata,
QSAKeyStateCache,
canonical_qsa_rope_positions,
qsa_ring_row_tags,
)
from .ops.qsa_prepare import qsa_prepare
@@ -339,6 +340,7 @@ class QSAIndexer(nn.Module):
main_slot_mapping=slot_mapping[:num_tokens],
main_k_scale=attn._k_scale_float,
main_v_scale=attn._v_scale_float,
layer_id=self.layer_id,
)
else:
# Unfused reference path
@@ -405,6 +407,18 @@ class QSAIndexer(nn.Module):
raw_metadata.slot_mapping,
position_rows,
)
# The unfused writer commits the same tag word as the fused
# one, so both paths leave byte-identical rings. (This reference
# reader does not validate tags; the fused kernel does.)
ring_tag_cache = getattr(raw_key_state_cache, "ring_tag_cache", None)
if ring_tag_cache is not None:
qsa_store_cache_rows(
ring_tag_cache,
raw_metadata.slot_mapping,
qsa_ring_row_tags(raw_metadata.logical_positions[:num_tokens]).view(
-1, 1, 1
),
)
if self.skip_topk:
if out is None:
+113 -3
View File
@@ -4,6 +4,10 @@
import torch
from vllm.models.qwen4_exp.common.qsa_cache import (
QSA_RING_TAG_WORD,
QSA_RING_TAIL_ELEMENTS,
)
from vllm.triton_utils import tl, triton
from vllm.v1.worker import guard_telemetry
@@ -12,6 +16,14 @@ from vllm.v1.worker import guard_telemetry
# from a @triton.jit body -- an earlier build shipped one and every guarded kernel died
# with "NameError: Cannot access global variable _GUARD_BIAS" on the smoke.
_GUARD_BIAS = tl.constexpr(guard_telemetry.GUARD_BIAS)
# Int64 word of the ring tail that carries `logical_position + 1`.
_RING_TAG_WORD = tl.constexpr(QSA_RING_TAG_WORD)
# Ring-row tag record packing and the encodable range of the found-tag slot.
_REC_POS_BITS = tl.constexpr(guard_telemetry.RING_RECORD_POS_BITS)
_REC_REQ_BITS = tl.constexpr(guard_telemetry.RING_RECORD_REQ_BITS)
_REC_LAYER_BITS = tl.constexpr(guard_telemetry.RING_RECORD_LAYER_BITS)
_GUARD_MAX_ENC = tl.constexpr(guard_telemetry.GUARD_MAX_ENCODABLE)
_GUARD_MIN_ENC = tl.constexpr(guard_telemetry.GUARD_MIN_ENCODABLE)
@@ -184,6 +196,7 @@ def _store_rotated(dst, y, o1, o2, scale):
"num_compressed_blocks",
"num_k_work",
"num_requests",
"layer_id",
]
)
def _qsa_prepare_kernel(
@@ -223,6 +236,8 @@ def _qsa_prepare_kernel(
num_requests, # bounds-guard instrumentation
guard_ctr, # bounds-guard instrumentation: persistent int64[3]
ring_skip_ctr, # ring-skip telemetry, persistent int64[3], SITE_QSA_RING_SKIP
ring_uncommitted_ctr, # ring-row tag fix, persistent int64[3], SITE_QSA_RING_UNCOMMITTED
layer_id, # ring-row tag fix: named in the uncommitted-row record only
HQ: tl.constexpr,
D: tl.constexpr,
TILE_T_Q: tl.constexpr,
@@ -446,6 +461,64 @@ def _qsa_prepare_kernel(
+ safe_state_block * state_cache_stride_block
+ (source_positions % STATE_SIZE)[:, None] * state_cache_stride_token
)
# Ring-row tag fix: EVERY historical source row must carry the tag the
# writer stores for exactly this logical position, before anything
# reads its key or its position tail. A missing ring block (state
# table entry out of range) reads tag 0 and fails the same way --
# before the ring-row tag fix it pooled zeros silently. In-chunk rows come from
# this launch's own k and are never checked.
historical = valid & ~source_in_chunk
source_tags = tl.load(
(
state_cache_ptr
+ safe_state_block * state_cache_stride_block
+ (source_positions % STATE_SIZE) * state_cache_stride_token
+ D
).to(tl.pointer_type(tl.int64))
+ _RING_TAG_WORD,
mask=historical & state_block_valid,
other=0,
)
row_bad = historical & (source_tags != source_positions.to(tl.int64) + 1)
num_bad_rows = tl.sum(row_bad.to(tl.int32), axis=0)
group_ok = num_bad_rows == 0
if num_bad_rows > 0:
# One coherent record per site, first offender wins: the CAS
# claims slot 1 and only its winner writes slot 2, so both
# fields always describe the same row.
bad_position = tl.min(
tl.where(row_bad, source_positions.to(tl.int64), 1 << 62),
axis=0,
)
bad_tag = tl.sum(
tl.where(
row_bad & (source_positions.to(tl.int64) == bad_position),
source_tags,
0,
),
axis=0,
)
record = (
(
((layer_id + 1).to(tl.int64) & ((1 << _REC_LAYER_BITS) - 1))
<< (_REC_REQ_BITS + _REC_POS_BITS)
)
| (
(request.to(tl.int64) & ((1 << _REC_REQ_BITS) - 1))
<< _REC_POS_BITS
)
| (bad_position & ((1 << _REC_POS_BITS) - 1))
)
tl.atomic_add(ring_uncommitted_ctr, num_bad_rows.to(tl.int64))
zero64 = bad_position * 0
prev = tl.atomic_cas(
ring_uncommitted_ctr + 1, zero64, record + _GUARD_BIAS
)
if prev == 0:
clamped_tag = tl.minimum(
tl.maximum(bad_tag, _GUARD_MIN_ENC), _GUARD_MAX_ENC
)
tl.atomic_xchg(ring_uncommitted_ctr + 2, clamped_tag + _GUARD_BIAS)
# Only the first completed group can cross the chunk boundary. Select
# historical rows from the ring without issuing two masked loads.
source_base = tl.where(source_in_chunk[:, None], current_base, cached_base)
@@ -454,9 +527,11 @@ def _qsa_prepare_kernel(
source_valid = tl.where(
source_in_chunk, source_tokens_valid, state_block_valid
)
# A failed group loads nothing (the mask, not a tl.where on the
# result, is what suppresses the memory operation).
source = tl.load(
source_base + dims[None, :],
mask=valid & source_valid[:, None],
mask=valid & group_ok & source_valid[:, None],
other=0.0,
).to(tl.float32)
# Match the unfused path's BF16 pooled tensor before RMSNorm.
@@ -504,7 +579,9 @@ def _qsa_prepare_kernel(
+ (first_position % STATE_SIZE) * state_cache_stride_token
+ D
).to(tl.pointer_type(tl.int64))
load_cached_position = ~first_in_chunk & state_block_valid
# The first row's tag was validated above with the rest;
# a failed group never reads its tail (pos stays 0, in range).
load_cached_position = ~first_in_chunk & state_block_valid & group_ok
cached_pos_t = tl.load(tail, mask=load_cached_position, other=0)
cached_pos_h = tl.load(tail + 1, mask=load_cached_position, other=0)
cached_pos_w = tl.load(tail + 2, mask=load_cached_position, other=0)
@@ -532,13 +609,16 @@ def _qsa_prepare_kernel(
)
compressed_block = (compressed_slot // COMP_PAGE_SIZE).to(tl.int64)
compressed_row = compressed_slot % COMP_PAGE_SIZE
# A group built from an uncommitted row must never become
# usable compressed state. The slot stays unwritten; the site is
# fatal because the selection kernels will still read it.
tl.store(
compressed_cache_ptr
+ compressed_block * compressed_cache_stride_block
+ compressed_row * compressed_cache_stride_token
+ dims,
tl.reshape(y, (D,)),
mask=valid,
mask=valid & group_ok,
)
if work_in_request == 0:
@@ -609,6 +689,20 @@ def _qsa_prepare_kernel(
tl.store(tail, pos_t.to(tl.int64), mask=valid_slot)
tl.store(tail + 1, pos_h.to(tl.int64), mask=valid_slot)
tl.store(tail + 2, pos_w.to(tl.int64), mask=valid_slot)
# Ring-row tag fix: the tag names the logical position this row now
# holds, taken from the authoritative per-token metadata (not
# re-derived from chunk arithmetic), stored after key + tail.
# Every committed row gets one, with or without RoPE positions.
token_logical = tl.load(
logical_positions_ptr + tl.maximum(token, 0),
mask=valid_slot,
other=-1,
)
tl.store(
(state_row + D).to(tl.pointer_type(tl.int64)) + _RING_TAG_WORD,
token_logical.to(tl.int64) + 1,
mask=valid_slot,
)
def qsa_prepare(
@@ -640,11 +734,16 @@ def qsa_prepare(
main_slot_mapping: torch.Tensor,
main_k_scale: float,
main_v_scale: float,
layer_id: int = -1,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Normalize Q, compress K, then update the circular raw state.
Also prepares the main attention (QK-norm/RoPE, gate copy, K/V cache write)
and returns its Q and gate.
``state_cache`` rows are ``[key D bf16 | 4 int64: pos_t, pos_h, pos_w, tag]``
(``QSAKeyStateCache``, ring-row tag fix); the tail is present with or without
``rope_pos_offset``. ``layer_id`` only labels the uncommitted-row record.
"""
num_tokens = q.shape[0]
main_head_dim = main_kv_cache.shape[-1] // 2
@@ -669,6 +768,15 @@ def qsa_prepare(
is_k_mrope = bool(mrope_section)
cache_has_rope_pos = rope_pos_offset is not None
assert rope_pos_offset is None or rope_pos_offset == head_dim
# The kernel addresses the tail at +head_dim and the tag at word 3 of
# it. A ring without the four-word tail (the older D or D+12 layouts)
# would put the tag store into the NEXT row, so refuse it outright.
assert state_cache.shape[-1] == head_dim + QSA_RING_TAIL_ELEMENTS, (
f"QSA ring row width {state_cache.shape[-1]} != head_dim + "
f"{QSA_RING_TAIL_ELEMENTS} (key + pos_t/h/w + tag)"
)
assert state_cache.dtype == torch.bfloat16
assert state_cache.stride(1) % 4 == 0 and state_cache.stride(0) % 4 == 0
if is_2d_positions:
assert positions.shape == (3, num_tokens)
assert is_k_mrope
@@ -727,6 +835,8 @@ def qsa_prepare(
min(query_start_loc.numel() - 1, state_block_table.shape[0]),
guard_telemetry.counter(guard_telemetry.SITE_QSA_PRE_INDEXER, q.device),
guard_telemetry.counter(guard_telemetry.SITE_QSA_RING_SKIP, q.device),
guard_telemetry.counter(guard_telemetry.SITE_QSA_RING_UNCOMMITTED, q.device),
int(layer_id),
HQ=num_q_heads,
D=head_dim,
TILE_T_Q=TILE_T_Q,