From 18c16f8660c3120297808c4c91887e32b8cf8c54 Mon Sep 17 00:00:00 2001 From: Yuhao Yang <47235274+yhyang201@users.noreply.github.com> Date: Sat, 16 May 2026 08:39:13 +0800 Subject: [PATCH] Port SGLANG_OPT_SWA_EVICT_DROP_PAGE_MARGIN from deepseek_v4_dev (#25419) --- python/sglang/srt/environ.py | 1 + python/sglang/srt/managers/schedule_batch.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index a194ac05e..5380d528b 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -596,6 +596,7 @@ class Envs: SGLANG_OPT_SWA_RADIX_CACHE_COMPACT = EnvBool(False) SGLANG_OPT_SWA_SPLIT_LEAF_ON_INSERT = EnvBool(False) SGLANG_OPT_SWA_RELEASE_LEAF_LOCK_AFTER_WINDOW = EnvBool(False) + SGLANG_OPT_SWA_EVICT_DROP_PAGE_MARGIN = EnvBool(False) # DeepGemm Mega MoE SGLANG_OPT_USE_DEEPGEMM_MEGA_MOE = EnvBool(False) diff --git a/python/sglang/srt/managers/schedule_batch.py b/python/sglang/srt/managers/schedule_batch.py index ab8cbc78f..091a52c45 100755 --- a/python/sglang/srt/managers/schedule_batch.py +++ b/python/sglang/srt/managers/schedule_batch.py @@ -2724,9 +2724,13 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin): # preserving cache reuse in multi-turn scenarios. Without this, leaf nodes # may become tombstoned, causing SWA memory leak. # See also: _insert_helper case 3 in swa_radix_cache.py (defensive counterpart). + if envs.SGLANG_OPT_SWA_EVICT_DROP_PAGE_MARGIN.get(): + evict_threshold = pre_len - sliding_window_size + else: + evict_threshold = pre_len - sliding_window_size - self.tree_cache.page_size new_swa_evicted_seqlen = max( req.swa_evicted_seqlen, - pre_len - sliding_window_size - self.tree_cache.page_size, + evict_threshold, ) if self.tree_cache.page_size > 1: