From 7ebaa98f81c73f9564143dbbd680b39563e435b6 Mon Sep 17 00:00:00 2001 From: Liangsheng Yin Date: Tue, 18 Aug 2026 15:48:55 -0700 Subject: [PATCH] [Fix] Assert the page-aligned SWA evict floor on both PD decode prealloc paths (#35396) --- python/sglang/srt/disaggregation/decode.py | 7 ++++++- test/registered/unit/mem_cache/test_hisparse_allocator.py | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/disaggregation/decode.py b/python/sglang/srt/disaggregation/decode.py index b3638eed6..1085e3415 100644 --- a/python/sglang/srt/disaggregation/decode.py +++ b/python/sglang/srt/disaggregation/decode.py @@ -1766,7 +1766,12 @@ def alloc_for_decode_prealloc( swa_tail_len=swa_tail_len, **extra_kwargs, ) - req.kv.swa_evicted_seqlen = fill_len - swa_tail_len + swa_evicted_seqlen = fill_len - swa_tail_len + assert ( + swa_evicted_seqlen >= 0 + and swa_evicted_seqlen % allocator.page_size == 0 + ) + req.kv.swa_evicted_seqlen = swa_evicted_seqlen else: kv_loc = allocator.alloc_extend( prefix_lens=torch.tensor( diff --git a/test/registered/unit/mem_cache/test_hisparse_allocator.py b/test/registered/unit/mem_cache/test_hisparse_allocator.py index 923267af5..2071a685f 100644 --- a/test/registered/unit/mem_cache/test_hisparse_allocator.py +++ b/test/registered/unit/mem_cache/test_hisparse_allocator.py @@ -80,7 +80,10 @@ class TestDeepSeekV4HiSparseAllocator(CustomTestCase): from sglang.srt.disaggregation.decode import DecodePreallocQueue fill_len = 512 - swa_tail_len = 128 + sliding_window_size = 200 + # _swa_tail_len floors the window start to a page boundary: + # floor_align(512 - 200, 256) = 256, so the tail is one whole page. + swa_tail_len = 256 kv_loc = torch.arange(512, 512 + fill_len, dtype=torch.int64) host_indices = torch.arange(1000, 1128, dtype=torch.int64) @@ -134,9 +137,9 @@ class TestDeepSeekV4HiSparseAllocator(CustomTestCase): enable_hisparse=True, hisparse_coordinator=coordinator, server_args=SimpleNamespace(disaggregation_decode_enable_radix_cache=False), + sliding_window_size=sliding_window_size, ) queue._uses_swa_tail_prealloc = MagicMock(return_value=True) - queue._swa_tail_len = MagicMock(return_value=swa_tail_len) result = queue._pre_alloc(req)