[KV Connector][Bugfix] MooncakeStore: don't double-apply Eagle prune in load_mask (#43516)

Signed-off-by: Dao Le <daole@inferact.ai>
Signed-off-by: Dao Le <Dao007forever@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Dao007forever
2026-05-25 22:11:57 -07:00
committed by GitHub
co-authored by Claude
parent aa2b56ffb0
commit 7966fc7233
2 changed files with 112 additions and 7 deletions
@@ -300,3 +300,85 @@ def test_store_mask_fast_path_single_attention_group():
assert len(coord.attention_groups) == 1
masks = coord.store_mask(64)
assert masks == ([True] * 4, [True] * 4)
# ----- Eagle / MTP interaction with load_mask -----
def test_lookup_with_eagle_pops_last_full_attention_block():
"""Sanity: with use_eagle, find_longest_cache_hit drops the last block.
Pairs with the load_mask test below to lock the round-trip contract."""
groups = [KVCacheGroupSpec(["L0"], _full(16))]
coord = _make_coord(groups, hash_block_size=16, use_eagle=True)
hs = _hashes(4)
cmap = ExternalCachedBlockPool({(0, bytes(h)) for h in hs})
_masks, hit = coord.find_longest_cache_hit(
hs, max_length=64, cached_block_pool=cmap
)
# 4 blocks present, eagle pops 1 → 3 blocks = 48 tokens.
assert hit == 48
def test_load_mask_with_eagle_does_not_double_prune_full_attention():
"""Regression for silent KV corruption with MTP/EAGLE-3.
The recv side calls ``load_mask(block_hashes, token_len)`` where
``token_len`` is already the eagle-pruned hit length from ``lookup``.
A second eagle pop here used to shorten the mask by one extra block;
``process_tokens`` then yielded a chunk past the mask, which the worker
silently skipped — leaving the trailing block of the loaded prefix
uninitialized in local KV.
"""
groups = [KVCacheGroupSpec(["L0"], _full(16))]
coord = _make_coord(groups, hash_block_size=16, use_eagle=True)
hs = _hashes(4)
cmap = ExternalCachedBlockPool({(0, bytes(h)) for h in hs})
_masks, hit = coord.find_longest_cache_hit(
hs, max_length=64, cached_block_pool=cmap
)
assert hit == 48 # eagle popped 1 block
masks = coord.load_mask(hs, token_len=hit)
# Every chunk that process_tokens(token_len=48, ...) would yield must
# have a corresponding mask slot. process_tokens emits chunk_id 0..2
# (start=0, 16, 32), so the mask must be length 3, all True.
assert masks[0] == [True, True, True]
def test_load_mask_with_eagle_hybrid_full_plus_swa():
"""Hybrid (FullAttn + SWA) with eagle: load_mask must cover every chunk
in [0, token_len) for the FullAttn group; SWA group keeps its
tail-window mask."""
groups = [
KVCacheGroupSpec(["L0"], _full(16)),
KVCacheGroupSpec(["L1"], _swa(16, 32)),
]
coord = _make_coord(groups, hash_block_size=16, use_eagle=True)
hs = _hashes(4)
exists = {(g, bytes(h)) for g in (0, 1) for h in hs}
cmap = ExternalCachedBlockPool(exists)
_masks, hit = coord.find_longest_cache_hit(
hs, max_length=64, cached_block_pool=cmap
)
# FullAttn dictates the convergence; eagle pops one block off it.
assert hit == 48
masks = coord.load_mask(hs, token_len=hit)
# FullAttn: all chunks populated locally.
assert masks[0] == [True, True, True]
# SWA: tail-window only (ceil((32-1)/16) = 2 trailing blocks).
assert masks[1][-2:] == [True, True]
def test_load_mask_without_eagle_unchanged():
"""Sanity: when eagle is off, load_mask is identical to the pre-fix path."""
groups = [KVCacheGroupSpec(["L0"], _full(16))]
coord = _make_coord(groups, hash_block_size=16, use_eagle=False)
hs = _hashes(4)
cmap = ExternalCachedBlockPool({(0, bytes(h)) for h in hs})
_masks, hit = coord.find_longest_cache_hit(
hs, max_length=64, cached_block_pool=cmap
)
assert hit == 64
masks = coord.load_mask(hs, token_len=hit)
assert masks[0] == [True, True, True, True]