[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:
Matt Mastracci
2026-09-25 12:51:34 -04:00
committed by GitHub
co-authored by Yifan Qiao Claude Opus 5.5 Robert Shaw mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent f83ea61cfe
commit 2617fe9383
5 changed files with 208 additions and 49 deletions
@@ -24,6 +24,8 @@ from types import SimpleNamespace
import pytest
import torch
from vllm.config import VllmConfig, set_current_vllm_config
from vllm.models.glm5next.common.attention import Glm5NextTailCache
from vllm.v1.attention.backend import CommonAttentionMetadata
from vllm.v1.attention.backends.mla.indexer import (
KpoolTailBackend,
@@ -56,6 +58,30 @@ def test_tail_backend_layout_matches_kernel_pointer_arithmetic():
assert content_stride == 1
@pytest.mark.parametrize(
"num_speculative_tokens, ring", [(0, 4), (1, 8), (4, 8), (7, 16), (13, 32)]
)
def test_tail_ring_divides_the_attention_block(num_speculative_tokens, ring):
"""With 7 draft tokens the ring was 12 slots. 12 does not divide the
640-token KDA attention block, so the scheduler block grew to their lcm
and prefix-cache hits were cut down to multiples of 1920."""
with set_current_vllm_config(VllmConfig()):
cache = Glm5NextTailCache(
head_dim=128,
dtype=torch.bfloat16,
prefix="tail",
cache_config=SimpleNamespace(block_size=640),
index_kpool=KPOOL,
)
spec = cache.get_kv_cache_spec(
SimpleNamespace(num_speculative_tokens=num_speculative_tokens)
)
assert spec.block_size == spec.sliding_window == ring
assert ring >= KPOOL + num_speculative_tokens
assert 640 % ring == 0
def test_tail_spec_opts_out_of_generic_slot_mapping():
"""The tail row is one block wide (padded to the block-table alignment), so
the generic kernel's ``pos // kpool`` column index runs off the end of the
@@ -296,31 +322,30 @@ def test_builder_reuses_slot_mapping_storage():
class TailRingMirror:
"""Mirror of _kpool_tail_seed_kernel / _kpool_decode_update_batched_kernel
addressing: block = tail_slot // kpool, ring offset = pos % kpool; a pool
completing at pos reads ring slots (pool_start + s) % kpool and uses the
current token's own K/score for the last member."""
"""Mirror of the tail-ring addressing in _kpool_tail_seed_kernel and
_kpool_decode_update_batched_kernel."""
def __init__(self, num_blocks, kpool=KPOOL):
def __init__(self, num_blocks, kpool=KPOOL, ring=None):
self.kpool = kpool
self.k = torch.full((num_blocks, kpool, 3), float("nan"))
self.s = torch.full((num_blocks, kpool, 3), float("nan"))
self.ring = ring or kpool
self.k = torch.full((num_blocks, self.ring, 3), float("nan"))
self.s = torch.full((num_blocks, self.ring, 3), float("nan"))
def stash(self, tail_slot, pos, k, s):
blk, off = tail_slot // self.kpool, pos % self.kpool
blk, off = tail_slot // self.ring, pos % self.ring
self.k[blk, off] = k
self.s[blk, off] = s
seed = stash # the seed kernel writes with the same addressing
def complete(self, tail_slot, pos, k, s):
blk = tail_slot // self.kpool
blk = tail_slot // self.ring
start = pos - (self.kpool - 1)
kk = torch.stack(
[self.k[blk, (start + i) % self.kpool] for i in range(self.kpool)]
[self.k[blk, (start + i) % self.ring] for i in range(self.kpool)]
)
ss = torch.stack(
[self.s[blk, (start + i) % self.kpool] for i in range(self.kpool)]
[self.s[blk, (start + i) % self.ring] for i in range(self.kpool)]
)
kk[-1], ss[-1] = k, s # is_current for the completing token
w = torch.softmax(ss, dim=0)
@@ -454,3 +479,35 @@ def test_triton_mapping_reads_strided_block_table():
for req, blk in enumerate(own_blocks):
start, end = int(qsl[req]), int(qsl[req + 1])
assert (got[start:end] // KPOOL == blk).all()
@pytest.mark.parametrize("ring_pools", [1, 2])
def test_rejected_completing_draft_needs_ring_slots(ring_pools):
"""With a one-pool ring, the drafts behind a rejected pool-completing draft
overwrote the pool's earlier keys, so its redo compressed wrong keys."""
ring_size = ring_pools * KPOOL
truth = TailRingMirror(num_blocks=2, ring=ring_size)
ring = TailRingMirror(num_blocks=2, ring=ring_size)
block = 1
def slot(pos):
return block * ring_size + pos % ring_size
for pos in range(4, 7):
truth.stash(slot(pos), pos, *token_kv(0, pos))
ring.stash(slot(pos), pos, *token_kv(0, pos))
expected = truth.complete(slot(7), 7, *token_kv(0, 7))
# Draft 7 completes the pool and is rejected. With a one-pool ring, drafts
# 8..10 overwrite the slots of positions 4..6, which are read by the redo
# of 7.
for pos in range(7, 11):
k, s = token_kv(9, pos) # draft values
if pos % KPOOL == KPOOL - 1:
ring.complete(slot(pos), pos, k, s)
ring.stash(slot(pos), pos, k, s)
redo = ring.complete(slot(7), 7, *token_kv(0, 7))
if ring_pools == 1:
assert not torch.allclose(redo, expected)
else:
torch.testing.assert_close(redo, expected)