[Fix] [AMD] hicache: Serve encoder-replay prefix hits with a paged DSpark draft SWA (#42716)

Co-authored-by: hnyls2002 <lsyincs@gmail.com>
This commit is contained in:
ormandj
2026-10-16 10:45:16 -07:00
committed by GitHub
co-authored by hnyls2002
parent 553068af25
commit 80c54e6ac5
6 changed files with 85 additions and 11 deletions
@@ -54,6 +54,9 @@ class CacheInitParams:
chunked_prefill_size: Optional[int] = None
sliding_window_size: Optional[int] = None
# False when tree nodes do not own the SWA state (a per-request SWA ring, or a
# paged SWA pool only a draft uses), so it must not gate prefix matching.
swa_gates_prefix_match: bool = True
decoder_swa_bounded_replay: bool = False
# Time-to-live for cache entries in seconds. If None, TTL is disabled.
@@ -37,6 +37,7 @@ from sglang.srt.configs.hybrid_arch import (
from sglang.srt.configs.model_config import ModelImpl, is_deepseek_dsa
from sglang.srt.environ import envs
from sglang.srt.managers.mm_schedule import init_mm_embedding_cache
from sglang.srt.mem_cache.allocator.swa import is_swa_req_ring
from sglang.srt.mem_cache.base_swa_memory_pool import BaseSWAKVPool
from sglang.srt.mem_cache.cache_init_params import CacheInitParams
from sglang.srt.mem_cache.deepseek_v4_memory_pool import DeepSeekV4TokenToKVPool
@@ -326,6 +327,16 @@ def build_kv_cache(
req_to_token_pool, token_to_kv_pool_allocator = tp_worker.get_memory_pool()
mtp_draft_device_pools = tp_worker.model_runner.mtp_draft_device_pools
# SWA the tree does not own must not gate prefix matching: a per-request ring,
# or paged SWA beside the target's encoder-replay request window, which then
# only the draft uses.
swa_gates_prefix_match = not (
is_swa_req_ring(token_to_kv_pool_allocator)
or (
isinstance(token_to_kv_pool, DeepSeekV4TokenToKVPool)
and token_to_kv_pool.request_window is not None
)
)
retraction_backup = resolve_decode_retraction_backup(tp_worker=tp_worker)
@@ -386,6 +397,7 @@ def build_kv_cache(
attn_cp_size=parallel.attn_cp_size,
chunked_prefill_size=effective_chunked_prefill_size,
sliding_window_size=sliding_window_size,
swa_gates_prefix_match=swa_gates_prefix_match,
mtp_draft_device_pools=mtp_draft_device_pools,
mtp_draft_mamba_pools=tp_worker.model_runner.mtp_draft_mamba_pools,
)
@@ -18,7 +18,6 @@ from sglang.srt.disaggregation.kv_events import (
namespaced_block_hash,
)
from sglang.srt.environ import envs
from sglang.srt.mem_cache.allocator.swa import is_swa_req_ring
from sglang.srt.mem_cache.base_prefix_cache import (
DecLockRefParams,
DecLockRefResult,
@@ -470,7 +469,7 @@ class RustUnifiedTreeCore(UnifiedTreeCoreInterface):
write_through_threshold=256,
device=str(self.device),
swa_sliding_window_size=params.sliding_window_size,
swa_req_ring=is_swa_req_ring(self._allocator),
swa_req_ring=not params.swa_gates_prefix_match,
enable_kv_cache_events=params.enable_kv_cache_events,
dynamo_kv_event_format=params.dynamo_kv_event_format,
mamba_cache_chunk_size=(
@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Callable, Optional, Sequence
import torch
from sglang.srt.environ import envs
from sglang.srt.mem_cache.allocator.swa import is_swa_req_ring
from sglang.srt.mem_cache.base_prefix_cache import (
DecLockRefParams,
EvictParams,
@@ -90,6 +89,7 @@ class SWAComponent(TreeComponent):
super().__init__(cache, params)
self._session_leaf_covered_len: dict[str, dict[UnifiedTreeNode, int]] = {}
self.sliding_window_size = params.sliding_window_size
self.gates_prefix_match = params.swa_gates_prefix_match
self.full_window_pages = (
self.sliding_window_size + params.page_size - 1
) // params.page_size
@@ -358,10 +358,7 @@ class SWAComponent(TreeComponent):
sliding_window_size = self.sliding_window_size
ct = self.component_type
state = {"len": float("inf")}
# A per-request SWA ring is not stored in tree nodes, so its bookkeeping
# must not gate prefix matching.
swa_req_ring = is_swa_req_ring(self.cache.token_to_kv_pool_allocator)
gates_prefix_match = self.gates_prefix_match
def validator(node: UnifiedTreeNode) -> bool:
cd = node.component_data[ct]
@@ -369,7 +366,7 @@ class SWAComponent(TreeComponent):
# — load_back will restore SWA from host before use.
if cd.value is None and (match_device_only or cd.host_value is None):
state["len"] = 0
if swa_req_ring and (node.backuped or not node.evicted):
if not gates_prefix_match and (node.backuped or not node.evicted):
return True
return False
state["len"] += len(node.key)