diff --git a/python/sglang/srt/mem_cache/allocator/swa.py b/python/sglang/srt/mem_cache/allocator/swa.py index cee82b1236..b57fb2694f 100644 --- a/python/sglang/srt/mem_cache/allocator/swa.py +++ b/python/sglang/srt/mem_cache/allocator/swa.py @@ -866,3 +866,15 @@ class PureSWATokenToKVPoolAllocator(SWATokenToKVPoolAllocator): def is_swa_req_ring(allocator) -> bool: return isinstance(allocator, SWATokenToKVPoolAllocator) and allocator.swa_req_ring + + +def swa_kept_out_of_tree(allocator) -> bool: + """Whether radix-tree nodes do not own the SWA state: a per-request SWA ring, + or the DeepSeek-V4.1 encoder-replay request window, which replay rebuilds on a + prefix hit. SWA bookkeeping must not gate prefix matching for either.""" + if not isinstance(allocator, SWATokenToKVPoolAllocator): + return False + if allocator.swa_req_ring: + return True + kvcache = getattr(allocator, "_kvcache", None) + return getattr(kvcache, "request_window", None) is not None diff --git a/python/sglang/srt/mem_cache/unified_cache/components/swa.py b/python/sglang/srt/mem_cache/unified_cache/components/swa.py index 0fdac250ab..f8645a1035 100644 --- a/python/sglang/srt/mem_cache/unified_cache/components/swa.py +++ b/python/sglang/srt/mem_cache/unified_cache/components/swa.py @@ -5,7 +5,7 @@ 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.allocator.swa import swa_kept_out_of_tree from sglang.srt.mem_cache.base_prefix_cache import ( DecLockRefParams, EvictParams, @@ -323,9 +323,9 @@ class SWAComponent(TreeComponent): 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) + # SWA state that tree nodes do not own (a per-request ring, or a request + # window rebuilt by replay) must not gate prefix matching. + swa_out_of_tree = swa_kept_out_of_tree(self.cache.token_to_kv_pool_allocator) def validator(node: UnifiedTreeNode) -> bool: cd = node.component_data[ct] @@ -333,7 +333,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 swa_out_of_tree and (node.backuped or not node.evicted): return True return False state["len"] += len(node.key)