[Mooncake] Skip KV lookup for non-reachable SWA blocks (#45444)

Signed-off-by: wzhao18 <wzhao18.sz@gmail.com>
This commit is contained in:
Wei Zhao
2026-06-18 02:23:20 -07:00
committed by GitHub
parent 1e9f04da14
commit 5fd3b276f8
4 changed files with 120 additions and 18 deletions
@@ -172,19 +172,50 @@ class MooncakeStoreCoordinator:
self,
aligned_token_len: int,
num_prompt_tokens: int | None = None,
) -> tuple[list[bool], ...]:
"""Per-group store masks: ``mask[g][i]`` is True iff chunk ``i`` of
group ``g`` should be written to the store so a future cache hit can
consume it.
) -> tuple[list[bool] | None, ...]:
"""Per-group store masks.
``mask[g][i]`` is True iff chunk ``i`` of group ``g`` should be
written to the store so a future cache hit can consume it. ``None`` is
the all-True sentinel.
Reuses the engine's ``SingleTypeKVCacheManager.reachable_block_mask``
so the store retains exactly the blocks the local prefix cache would.
"""
return self._reachable_masks(
aligned_token_len,
retention_interval=self.retention_interval,
num_prompt_tokens=num_prompt_tokens,
)
def lookup_mask(
self,
aligned_token_len: int,
) -> tuple[list[bool] | None, ...]:
"""Per-group lookup masks.
``mask[g][i]`` is True iff chunk ``i`` of group ``g`` should be
looked up as an aligned hit boundary. ``None`` is the all-True
sentinel.
"""
return self._reachable_masks(
aligned_token_len,
retention_interval=None,
num_prompt_tokens=None,
)
def _reachable_masks(
self,
aligned_token_len: int,
*,
retention_interval: int | None,
num_prompt_tokens: int | None,
) -> tuple[list[bool] | None, ...]:
assert aligned_token_len % self.lcm_block_size == 0, (
f"aligned_token_len ({aligned_token_len}) must be a multiple of "
f"lcm_block_size ({self.lcm_block_size})"
)
masks: list[list[bool]] = []
masks: list[list[bool] | None] = []
for g_idx, g in enumerate(self.kv_cache_groups):
spec = _unwrap_spec(g.kv_cache_spec)
num_chunks = aligned_token_len // spec.block_size
@@ -196,10 +227,12 @@ class MooncakeStoreCoordinator:
alignment_tokens=self.lcm_block_size,
kv_cache_spec=spec,
use_eagle=g_idx in self.eagle_group_ids,
retention_interval=self.retention_interval,
retention_interval=retention_interval,
num_prompt_tokens=num_prompt_tokens,
)
masks.append([True] * num_chunks if mask is None else mask)
if mask is not None:
assert len(mask) == num_chunks
masks.append(mask)
return tuple(masks)
def block_hashes_for_spec(
@@ -548,7 +548,9 @@ class KVCacheStoreSendingThread(KVTransferThread):
for chunk_idx, (start, end, key) in enumerate(
db.process_tokens(token_len, req_meta.block_hashes)
):
if chunk_idx >= len(mask) or not mask[chunk_idx]:
if mask is not None and (
chunk_idx >= len(mask) or not mask[chunk_idx]
):
continue
starts.append(start)
ends.append(end)
@@ -1375,9 +1377,11 @@ class MooncakeStoreWorker:
# candidate_meta[i] is the (group_id, hash_bytes) for candidate_keys[i].
candidate_keys: list[str] = []
candidate_meta: list[tuple[int, bytes]] = []
lookup_masks = self.coord.lookup_mask(token_len)
tp_count = min(self.tp_size, self.num_kv_head)
for g_idx, db in enumerate(self.token_dbs):
spec_block_size = db.block_size
lookup_mask = lookup_masks[g_idx]
group_hashes = self.coord.block_hashes_for_spec(
block_hashes, self._kv_cache_groups[g_idx].kv_cache_spec
)
@@ -1385,6 +1389,10 @@ class MooncakeStoreWorker:
start_idx = chunk_id * spec_block_size
if start_idx >= token_len:
break
if lookup_mask is not None and (
chunk_id >= len(lookup_mask) or not lookup_mask[chunk_id]
):
continue
for tp in range(tp_count):
for pp in range(self.pp_size):
md = dataclasses.replace(db.metadata, tp_rank=tp, pp_rank=pp)