[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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user