From 99c317b1314c275ca0f2efc79fee2791a2a4eb9e Mon Sep 17 00:00:00 2001 From: "minke.yu" Date: Sat, 10 Oct 2026 01:02:33 +0800 Subject: [PATCH] =?UTF-8?q?cherry-pick=20=E4=B8=8A=E6=B8=B8=20#42716?= =?UTF-8?q?=EF=BC=88=E8=BF=90=E8=A1=8C=E6=97=B6=E9=83=A8=E5=88=86=EF=BC=89?= =?UTF-8?q?=EF=BC=9Aencoder=20SWA=20bounded=20replay=20=E4=B8=8B=E6=94=BE?= =?UTF-8?q?=E8=A1=8C=20host=20=E5=B1=82=E5=89=8D=E7=BC=80=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E2=80=94=E2=80=94request=5Fwindow=20=E5=BD=A2?= =?UTF-8?q?=E6=80=81=E7=9A=84=20SWA=20=E4=B8=8D=E8=BF=9B=E6=A0=91=EF=BC=8C?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=97=A8=E4=B8=8D=E5=BA=94=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=20host-only=20=E8=8A=82=E7=82=B9=EF=BC=9Brust=20adapter/test?= =?UTF-8?q?=20=E4=B8=A4=E5=A4=84=20hunk=20=E4=B8=8E=E6=9C=AC=E6=A0=91?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=B8=8D=E7=AC=A6=E6=9C=AA=E5=90=88=EF=BC=88?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=20python=20=E6=A0=91=E8=8A=AF=E7=94=A8?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/sglang/srt/mem_cache/allocator/swa.py | 12 ++++++++++++ .../srt/mem_cache/unified_cache/components/swa.py | 10 +++++----- 2 files changed, 17 insertions(+), 5 deletions(-) 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)