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: