[Bugfix][GLM-5.3-Flash] kpool corruption with speculative decoding (#58454)
Signed-off-by: Matt Mastracci <matthew@mastracci.com> Signed-off-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Yifan Qiao
Claude Opus 5.5
Robert Shaw
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
f83ea61cfe
commit
2617fe9383
@@ -327,37 +327,38 @@ def _kpool_tail_seed_kernel(
|
||||
KPOOL_HEAD: tl.constexpr,
|
||||
HEAD_DIM: tl.constexpr,
|
||||
KPOOL: tl.constexpr,
|
||||
RING: tl.constexpr,
|
||||
BLOCK_D: tl.constexpr,
|
||||
):
|
||||
"""Copy token ``i``'s raw K + gate into its request's tail block.
|
||||
"""Copy token ``i``'s raw K + gate into its request's tail ring.
|
||||
|
||||
Token ``i`` is among its request's last KPOOL tokens iff the token KPOOL
|
||||
ahead belongs to a different tail block (or is past the batch / padding,
|
||||
slot < 0). ``tslot = block * KPOOL + pos % KPOOL``; the destination is
|
||||
``tail[block, {0:K, 1:score}, pos % KPOOL, :]``.
|
||||
slot < 0). ``tslot = block * RING + pos % RING``; the destination is
|
||||
``tail[block, {0:K, 1:score}, pos % RING, :]``.
|
||||
"""
|
||||
i = tl.program_id(0)
|
||||
t = tl.load(tslot_ptr + i).to(tl.int64)
|
||||
if t < 0:
|
||||
return
|
||||
blk = t // KPOOL # t >= 0 here, so trunc == floor
|
||||
blk = t // RING # t >= 0 here, so trunc == floor
|
||||
ahead = tl.load(tslot_ptr + i + KPOOL, mask=i + KPOOL < n_tokens, other=-1).to(
|
||||
tl.int64
|
||||
)
|
||||
# Match the torch semantics exactly: a negative ahead slot floors to a
|
||||
# block id that differs from every real block -> token is in the tail.
|
||||
# Only divide non-negative slots (Triton int div truncates, torch floors).
|
||||
if ahead >= 0 and ahead // KPOOL == blk:
|
||||
if ahead >= 0 and ahead // RING == blk:
|
||||
return
|
||||
offs = tl.arange(0, BLOCK_D)
|
||||
m = offs < HEAD_DIM
|
||||
block_base = blk * TAIL_BLOCK_ELEMS
|
||||
base = block_base + (t % KPOOL) * HEAD_DIM
|
||||
base = block_base + (t % RING) * HEAD_DIM
|
||||
k = tl.load(key_ptr + i * HEAD_DIM + offs, mask=m)
|
||||
s = tl.load(score_ptr + i * HEAD_DIM + offs, mask=m)
|
||||
tl.store(tail_ptr + base + offs, k, mask=m)
|
||||
tl.store(
|
||||
tail_ptr + block_base + KPOOL_HEAD + (t % KPOOL) * HEAD_DIM + offs, s, mask=m
|
||||
tail_ptr + block_base + KPOOL_HEAD + (t % RING) * HEAD_DIM + offs, s, mask=m
|
||||
)
|
||||
|
||||
|
||||
@@ -385,6 +386,7 @@ def kpool_seed_tail_cache(
|
||||
KPOOL_HEAD=tail_kv_cache.stride(1),
|
||||
HEAD_DIM=head_dim,
|
||||
KPOOL=kpool,
|
||||
RING=tail_kv_cache.shape[2],
|
||||
BLOCK_D=triton.next_power_of_2(head_dim),
|
||||
)
|
||||
|
||||
@@ -412,6 +414,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
PAGE_SIZE: tl.constexpr,
|
||||
BUF_NUMEL_PER_PAGE: tl.constexpr,
|
||||
POOL_SIZE: tl.constexpr,
|
||||
RING: tl.constexpr,
|
||||
TAIL_BLOCK_ELEMS: tl.constexpr,
|
||||
KPOOL_HEAD: tl.constexpr,
|
||||
HEAD_DIM: tl.constexpr,
|
||||
@@ -428,10 +431,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
the tail-ring slots that tokens t < t* (same request) just stashed in this
|
||||
same invocation. ``tl.range`` iterates sequentially within the program, so
|
||||
those stashes are visible to the later completion read. Cross-request
|
||||
programs are independent (distinct tail blocks). With NEXT_N < POOL_SIZE
|
||||
(the spec-verify case: NEXT_N ~= num_spec+1, POOL_SIZE=16) at most one
|
||||
completion can occur per request per call, but the ordered loop is correct
|
||||
for any NEXT_N.
|
||||
programs are independent (distinct tail blocks). RING >= POOL_SIZE.
|
||||
"""
|
||||
req = tl.program_id(0)
|
||||
offs = tl.arange(0, BLOCK_D)
|
||||
@@ -445,7 +445,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
pos_valid = (cache_loc >= 0) & (pos >= 0)
|
||||
|
||||
slot = safe_pos % POOL_SIZE
|
||||
phys_slot = safe_pos % POOL_SIZE
|
||||
phys_slot = safe_pos % RING
|
||||
|
||||
# Derive the tail block from THIS token's tail_slot (the request's block
|
||||
# is constant across a pool, but a padded / invalid entry carries a
|
||||
@@ -453,7 +453,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
# token's base address). Clamp so an invalid entry can never form an
|
||||
# out-of-bounds base; the accesses below are gated on pos_valid anyway.
|
||||
tail_slot = tl.load(tail_slot_mapping_ptr + idx)
|
||||
block = tl.maximum(tail_slot, 0).to(tl.int64) // POOL_SIZE
|
||||
block = tl.maximum(tail_slot, 0).to(tl.int64) // RING
|
||||
block_base = block * TAIL_BLOCK_ELEMS
|
||||
|
||||
# The tail-ring stash must run for EVERY real token, so it is gated on
|
||||
@@ -481,7 +481,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
max_score = tl.full((BLOCK_D,), -float("inf"), tl.float32)
|
||||
for pool_slot in tl.static_range(0, POOL_SIZE):
|
||||
is_current = pool_slot == slot
|
||||
phys = (pool_logical_start + pool_slot) % POOL_SIZE
|
||||
phys = (pool_logical_start + pool_slot) % RING
|
||||
score_buf = tl.load(
|
||||
tail_kv_ptr + block_base + KPOOL_HEAD + phys * HEAD_DIM + offs,
|
||||
mask=dim_mask,
|
||||
@@ -499,7 +499,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
denom = tl.full((BLOCK_D,), 0.0, tl.float32)
|
||||
for pool_slot in tl.static_range(0, POOL_SIZE):
|
||||
is_current = pool_slot == slot
|
||||
phys = (pool_logical_start + pool_slot) % POOL_SIZE
|
||||
phys = (pool_logical_start + pool_slot) % RING
|
||||
score_buf = tl.load(
|
||||
tail_kv_ptr + block_base + KPOOL_HEAD + phys * HEAD_DIM + offs,
|
||||
mask=dim_mask,
|
||||
@@ -609,7 +609,8 @@ def kpool_decode_update_and_maybe_write_cache_batched(
|
||||
return
|
||||
assert tail_kv_cache.ndim == 4
|
||||
assert tail_kv_cache.shape[1] == 2
|
||||
assert tail_kv_cache.shape[2] == pool_size
|
||||
ring = tail_kv_cache.shape[2]
|
||||
assert ring >= pool_size and ring % pool_size == 0, (ring, pool_size)
|
||||
assert tail_kv_cache.shape[3] == head_dim
|
||||
assert tail_kv_cache.dtype == torch.bfloat16
|
||||
assert key.ndim == 3 and key.shape[2] == head_dim
|
||||
@@ -658,6 +659,7 @@ def kpool_decode_update_and_maybe_write_cache_batched(
|
||||
PAGE_SIZE=page_size,
|
||||
BUF_NUMEL_PER_PAGE=buf.stride(0),
|
||||
POOL_SIZE=pool_size,
|
||||
RING=ring,
|
||||
TAIL_BLOCK_ELEMS=tail_kv_cache.stride(0),
|
||||
KPOOL_HEAD=tail_kv_cache.stride(1),
|
||||
HEAD_DIM=head_dim,
|
||||
|
||||
@@ -34,6 +34,7 @@ from vllm.models.glm5next.sparse_indexer import SparseAttnIndexerKpool
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.transformers_utils.configs.glm5_next import Glm5NextConfig
|
||||
from vllm.utils.deep_gemm import PAGED_MQA_PAGE_SIZES
|
||||
from vllm.utils.math_utils import cdiv, next_power_of_2
|
||||
from vllm.v1.kv_cache_interface import KpoolTailSpec, MLAAttentionSpec
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -160,7 +161,7 @@ class Glm5NextTailCache(DeepseekV32IndexerCache):
|
||||
"""Paged circular buffer for the kpool indexer's in-progress (tail) pool.
|
||||
|
||||
Holds the trailing incomplete pool's raw K + gate score: one block of
|
||||
``index_kpool`` slots per request, overwritten in place by ``pos % kpool``
|
||||
``ring`` slots per request, overwritten in place by ``pos % ring``
|
||||
as decode/spec-decode advances. Prefill seeds it (instead of discarding the
|
||||
tail raw K+gate); the connector transfers it across PD; decode reads it to
|
||||
compress the boundary pool correctly. ``KpoolTailSpec`` /
|
||||
@@ -190,13 +191,24 @@ class Glm5NextTailCache(DeepseekV32IndexerCache):
|
||||
def get_kv_cache_spec(self, vllm_config: VllmConfig):
|
||||
# The two head slots form [K, gate score] in the generic
|
||||
# [block, head, state, content] cache view.
|
||||
# Drafts are stashed before acceptance. With a one-pool ring, the
|
||||
# drafts behind a rejected pool-completing draft overwrite the keys
|
||||
# read by its redo.
|
||||
span = self._index_kpool + vllm_config.num_speculative_tokens
|
||||
ring = self._index_kpool * next_power_of_2(cdiv(span, self._index_kpool))
|
||||
# ring must divide the attention block size (a multiple of 128).
|
||||
assert self.cache_config.block_size % ring == 0, (
|
||||
f"Glm5NextTailCache: cache_config.block_size "
|
||||
f"({self.cache_config.block_size}) must be a multiple of the "
|
||||
f"tail ring ({ring})"
|
||||
)
|
||||
return KpoolTailSpec(
|
||||
block_size=self._index_kpool,
|
||||
block_size=ring,
|
||||
num_kv_heads=2,
|
||||
head_size=self.head_dim,
|
||||
head_size_v=0,
|
||||
dtype=torch.bfloat16,
|
||||
sliding_window=self._index_kpool,
|
||||
sliding_window=ring,
|
||||
)
|
||||
|
||||
def get_attn_backend(self):
|
||||
|
||||
@@ -387,14 +387,15 @@ def _kpool_tail_seed_kernel(
|
||||
KPOOL_HEAD: tl.constexpr,
|
||||
HEAD_DIM: tl.constexpr,
|
||||
KPOOL: tl.constexpr,
|
||||
RING: tl.constexpr,
|
||||
BLOCK_D: tl.constexpr,
|
||||
):
|
||||
"""Copy token ``i``'s raw K + gate into its request's tail block.
|
||||
"""Copy token ``i``'s raw K + gate into its request's tail ring.
|
||||
|
||||
Token ``i`` is among its request's last KPOOL tokens iff the token KPOOL
|
||||
ahead belongs to a different tail block (or is past the batch / padding,
|
||||
slot < 0). ``tslot = block * KPOOL + pos % KPOOL``; the destination is
|
||||
``tail[block, {0:K, 1:score}, pos % KPOOL, :]``.
|
||||
slot < 0). ``tslot = block * RING + pos % RING``; the destination is
|
||||
``tail[block, {0:K, 1:score}, pos % RING, :]``.
|
||||
|
||||
The tail cache aliases the indexer cache with the indexer's (padded) block
|
||||
stride, so blocks are addressed through ``TAIL_BLOCK_ELEMS`` /
|
||||
@@ -405,18 +406,18 @@ def _kpool_tail_seed_kernel(
|
||||
t = tl.load(tslot_ptr + i).to(tl.int64)
|
||||
if t < 0:
|
||||
return
|
||||
blk = t // KPOOL # t >= 0 here, so trunc == floor
|
||||
blk = t // RING # t >= 0 here, so trunc == floor
|
||||
ahead = tl.load(tslot_ptr + i + KPOOL, mask=i + KPOOL < n_tokens, other=-1).to(
|
||||
tl.int64
|
||||
)
|
||||
# Match the torch semantics exactly: a negative ahead slot floors to a
|
||||
# block id that differs from every real block -> token is in the tail.
|
||||
# Only divide non-negative slots (Triton int div truncates, torch floors).
|
||||
if ahead >= 0 and ahead // KPOOL == blk:
|
||||
if ahead >= 0 and ahead // RING == blk:
|
||||
return
|
||||
offs = tl.arange(0, BLOCK_D)
|
||||
m = offs < HEAD_DIM
|
||||
base = blk * TAIL_BLOCK_ELEMS + (t % KPOOL) * HEAD_DIM
|
||||
base = blk * TAIL_BLOCK_ELEMS + (t % RING) * HEAD_DIM
|
||||
k = tl.load(key_ptr + i * HEAD_DIM + offs, mask=m)
|
||||
s = tl.load(score_ptr + i * HEAD_DIM + offs, mask=m)
|
||||
tl.store(tail_ptr + base + offs, k, mask=m)
|
||||
@@ -449,6 +450,7 @@ def kpool_seed_tail_cache(
|
||||
KPOOL_HEAD=tail_kv_cache.stride(1),
|
||||
HEAD_DIM=head_dim,
|
||||
KPOOL=kpool,
|
||||
RING=tail_kv_cache.shape[2],
|
||||
BLOCK_D=triton.next_power_of_2(head_dim),
|
||||
)
|
||||
|
||||
@@ -476,6 +478,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
PAGE_SIZE: tl.constexpr,
|
||||
BUF_NUMEL_PER_PAGE: tl.constexpr,
|
||||
POOL_SIZE: tl.constexpr,
|
||||
RING: tl.constexpr,
|
||||
TAIL_BLOCK_ELEMS: tl.constexpr,
|
||||
KPOOL_HEAD: tl.constexpr,
|
||||
HEAD_DIM: tl.constexpr,
|
||||
@@ -490,10 +493,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
the tail-ring slots that tokens t < t* (same request) just stashed in this
|
||||
same invocation. ``tl.range`` iterates sequentially within the program, so
|
||||
those stashes are visible to the later completion read. Cross-request
|
||||
programs are independent (distinct tail blocks). With NEXT_N < POOL_SIZE
|
||||
(the spec-verify case: NEXT_N ~= num_spec+1, POOL_SIZE=16) at most one
|
||||
completion can occur per request per call, but the ordered loop is correct
|
||||
for any NEXT_N.
|
||||
programs are independent (distinct tail blocks). RING >= POOL_SIZE.
|
||||
"""
|
||||
req = tl.program_id(0)
|
||||
offs = tl.arange(0, BLOCK_D)
|
||||
@@ -507,7 +507,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
pos_valid = (cache_loc >= 0) & (pos >= 0)
|
||||
|
||||
slot = safe_pos % POOL_SIZE
|
||||
phys_slot = safe_pos % POOL_SIZE
|
||||
phys_slot = safe_pos % RING
|
||||
|
||||
# Derive the tail block from THIS token's tail_slot (the request's block
|
||||
# is constant across a pool, but a padded / invalid entry carries a
|
||||
@@ -515,7 +515,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
# token's base address). Clamp so an invalid entry can never form an
|
||||
# out-of-bounds base; the accesses below are gated on pos_valid anyway.
|
||||
tail_slot = tl.load(tail_slot_mapping_ptr + idx)
|
||||
block = tl.maximum(tail_slot, 0).to(tl.int64) // POOL_SIZE
|
||||
block = tl.maximum(tail_slot, 0).to(tl.int64) // RING
|
||||
block_base = block * TAIL_BLOCK_ELEMS
|
||||
|
||||
# The tail-ring stash must run for EVERY real token, so it is gated on
|
||||
@@ -543,7 +543,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
max_score = tl.full((BLOCK_D,), -float("inf"), tl.float32)
|
||||
for pool_slot in tl.static_range(0, POOL_SIZE):
|
||||
is_current = pool_slot == slot
|
||||
phys = (pool_logical_start + pool_slot) % POOL_SIZE
|
||||
phys = (pool_logical_start + pool_slot) % RING
|
||||
score_buf = tl.load(
|
||||
tail_kv_ptr + block_base + KPOOL_HEAD + phys * HEAD_DIM + offs,
|
||||
mask=dim_mask,
|
||||
@@ -561,7 +561,7 @@ def _kpool_decode_update_batched_kernel(
|
||||
denom = tl.full((BLOCK_D,), 0.0, tl.float32)
|
||||
for pool_slot in tl.static_range(0, POOL_SIZE):
|
||||
is_current = pool_slot == slot
|
||||
phys = (pool_logical_start + pool_slot) % POOL_SIZE
|
||||
phys = (pool_logical_start + pool_slot) % RING
|
||||
score_buf = tl.load(
|
||||
tail_kv_ptr + block_base + KPOOL_HEAD + phys * HEAD_DIM + offs,
|
||||
mask=dim_mask,
|
||||
@@ -671,7 +671,8 @@ def kpool_decode_update_and_maybe_write_cache_batched(
|
||||
return
|
||||
assert tail_kv_cache.ndim == 4
|
||||
assert tail_kv_cache.shape[1] == 2
|
||||
assert tail_kv_cache.shape[2] == pool_size
|
||||
ring = tail_kv_cache.shape[2]
|
||||
assert ring >= pool_size and ring % pool_size == 0, (ring, pool_size)
|
||||
assert tail_kv_cache.shape[3] == head_dim
|
||||
assert tail_kv_cache.dtype == torch.bfloat16
|
||||
assert key.ndim == 3 and key.shape[2] == head_dim
|
||||
@@ -717,6 +718,7 @@ def kpool_decode_update_and_maybe_write_cache_batched(
|
||||
PAGE_SIZE=page_size,
|
||||
BUF_NUMEL_PER_PAGE=buf.stride(0),
|
||||
POOL_SIZE=pool_size,
|
||||
RING=ring,
|
||||
TAIL_BLOCK_ELEMS=tail_kv_cache.stride(0),
|
||||
KPOOL_HEAD=tail_kv_cache.stride(1),
|
||||
HEAD_DIM=head_dim,
|
||||
|
||||
Reference in New Issue
Block a user