hicache: align prefetch and host restore boundaries

Use the request radix-key cap for L3 prefetch so bounded replay cannot
split a queried trailing-state checkpoint at the later rematch. Reject
Host-only SWA-gap candidates before they mask a complete ancestor, while
preserving device matching and the final load-back completeness guard.

Add CPU regressions that import the real scheduler, cache and TreeCore
methods. All 13 test methods pass; the same tests against the unmodified
scheduler/SWA code fail six methods (ten assertions including subtests).
The checks use synthetic cache state and controller boundaries, not GPU
or live Store execution.
This commit is contained in:
2026-10-30 11:42:19 +08:00
parent 81f7398855
commit 17505e6cf6
3 changed files with 370 additions and 4 deletions
+11 -3
View File
@@ -294,6 +294,7 @@ from sglang.srt.mem_cache import kv_cache_builder
from sglang.srt.mem_cache.allocator.page_interleave import page_interleave_shard_size
from sglang.srt.mem_cache.base_prefix_cache import CacheRequestOutcome
from sglang.srt.mem_cache.common import (
_req_radix_key,
checkpoint_kv_cache,
discard_kv_cache_backup,
release_kv_cache,
@@ -3323,9 +3324,16 @@ class Scheduler(
and tree_cache.get_last_hash_value(last_host_node) is not None
)
):
match_end = req._compute_max_prefix_len(
len(req.full_untruncated_fill_ids)
)
# L3 candidates must use the same raw-token cap as the
# subsequent rematch, including SWA bounded replay/logprobs.
# Otherwise a published trailing-state checkpoint can be split
# away by rematch, leaving a FULL-only parent with no SWA state.
match_end = _req_radix_key(
tree_cache,
req,
req.full_untruncated_fill_ids,
req._compute_max_prefix_len(len(req.full_untruncated_fill_ids)),
)._raw_len()
new_input_tokens = req.full_untruncated_fill_ids[matched_len:match_end]
prefix_keys = (
tree_cache.get_prefix_hash_values(last_host_node)
@@ -369,7 +369,15 @@ class SWAComponent(TreeComponent):
if cd.value is None and (match_device_only or cd.host_value is None):
state["len"] = 0
if not gates_prefix_match and (node.backuped or not node.evicted):
return True
# Gate-off preserves device bounded replay. A Host-only
# candidate must still be loadable: otherwise it masks a
# complete ancestor/L3 opportunity and later produces an
# empty load-back spec. Keep zero-window/root no-gap cases.
return (
match_device_only
or not node.evicted
or not self.has_load_back_gap(node)
)
return False
state["len"] += len(node.key)
return state["len"] >= sliding_window_size