diff --git a/python/sglang/srt/arg_groups/fields/disagg.py b/python/sglang/srt/arg_groups/fields/disagg.py index 04b187d5ac..9f4eb0dea4 100644 --- a/python/sglang/srt/arg_groups/fields/disagg.py +++ b/python/sglang/srt/arg_groups/fields/disagg.py @@ -84,7 +84,7 @@ class Disagg(msgspec.Struct): ] = None disaggregation_decode_enable_radix_cache: A[ bool, - "Enable radix cache on decode server (PD mode). Caches KV prefixes to avoid redundant transfers. Incompatible with --enable-hisparse, speculative decoding, and --disaggregation-transfer-backend fake.", + "Enable radix cache on decode server (PD mode). Caches KV prefixes to avoid redundant transfers. Incompatible with --enable-hisparse, --disaggregation-transfer-backend fake, and speculative algorithms other than EAGLE, EAGLE3, and DSPARK.", ] = False disaggregation_decode_enable_offload_kvcache: A[ bool, "Enable async KV cache offloading on decode server (PD mode)." diff --git a/python/sglang/srt/arg_groups/pd_disaggregation_hook.py b/python/sglang/srt/arg_groups/pd_disaggregation_hook.py index 32d2d8c978..1e4083b92c 100644 --- a/python/sglang/srt/arg_groups/pd_disaggregation_hook.py +++ b/python/sglang/srt/arg_groups/pd_disaggregation_hook.py @@ -107,13 +107,6 @@ def handle_pd_disaggregation(server_args: ServerArgs) -> None: "--disaggregation-decode-enable-radix-cache is incompatible " "with --disaggregation-transfer-backend fake" ) - if cfg.speculative_algorithm not in (None, "DSPARK"): - raise ValueError( - "--disaggregation-decode-enable-radix-cache is incompatible " - "with speculative decoding " - f"(--speculative-algorithm {cfg.speculative_algorithm})" - ) - if attn_dp_enabled_of(resolved_view(server_args)): logger.warning( "EXPERIMENTAL: Decode radix cache with DP attention. " diff --git a/python/sglang/srt/arg_groups/speculative_hook.py b/python/sglang/srt/arg_groups/speculative_hook.py index 3b78d2f969..0c975873c1 100644 --- a/python/sglang/srt/arg_groups/speculative_hook.py +++ b/python/sglang/srt/arg_groups/speculative_hook.py @@ -158,6 +158,19 @@ def handle_speculative_decoding(server_args: ServerArgs) -> None: ), ) + # Check the final algorithm: NEXTN aliases EAGLE, while Gemma4 drafts promote + # EAGLE/NEXTN to FROZEN_KV_MTP. + if ( + cfg.disaggregation_mode == "decode" + and cfg.disaggregation_decode_enable_radix_cache + and cfg.speculative_algorithm not in (None, "DSPARK", "EAGLE", "EAGLE3") + ): + raise ValueError( + "--disaggregation-decode-enable-radix-cache is incompatible " + "with speculative decoding " + f"(--speculative-algorithm {cfg.speculative_algorithm})" + ) + if cfg.speculative_use_block_verification: if cfg.speculative_algorithm not in ("EAGLE", "EAGLE3", "DFLASH", "DSPARK"): raise ValueError( diff --git a/python/sglang/srt/mem_cache/unified_radix_cache.py b/python/sglang/srt/mem_cache/unified_radix_cache.py index d1399f088d..d9db734a1c 100644 --- a/python/sglang/srt/mem_cache/unified_radix_cache.py +++ b/python/sglang/srt/mem_cache/unified_radix_cache.py @@ -1197,19 +1197,17 @@ class UnifiedRadixCache(BasePrefixCache): ) ) - # Match prefix. SWA insertion retains one extra window before the - # page-aligned boundary, so the normal match remains safe to repoint. - # The tree's own walk: a session slot must not answer for the insert. - match_result = self._match_tree(MatchPrefixParams(key=radix_key, req=req)) - new_last_node = match_result.last_device_node + # With PD decode radix caching, SWA matching can stop short of the inserted path. + # Repoint from that path to protect all tree-owned KV. + new_last_node = result.last_device_node new_indices = self.path_device_indices(new_last_node) new_prefix_len = result.prefix_len assert req.kv.cache_protected_len <= len(new_indices) + self.page_size - 1, ( f"{req.kv.cache_protected_len=}, {len(new_indices)=}, {page_aligned_len=}" ) - assert new_prefix_len <= len(new_indices), ( - f"{new_prefix_len=}, {len(new_indices)=}" - ) + assert ( + new_prefix_len <= len(new_indices) and len(new_indices) == page_aligned_len + ), f"{new_prefix_len=}, {len(new_indices)=}, {page_aligned_len=}" self.req_to_token_pool.write( (req.kv.req_pool_idx, slice(req.kv.cache_protected_len, len(new_indices))), new_indices[req.kv.cache_protected_len :], diff --git a/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py b/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py index 435b141679..e3d3684627 100644 --- a/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py +++ b/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py @@ -2279,6 +2279,67 @@ class UnifiedRadixCacheSuite: cache.unlock(req.lock) cache.sanity_check() + def test_swa_unfinished_req_repoints_whole_insert_below_window(self): + # A request whose live SWA tail inside the inserted range is shorter + # than the window must still end up protected for the whole insert: + # the tree owns those slots now, and a shorter protected length lets + # the request free tree-owned SWA pages and re-insert them at finish. + if not self.cfg.has_swa or self.cfg.has_mamba: + self.skipTest("requires SWA without Mamba") + if self.cfg.page_size != 1 or self.cfg.sliding_window_size != 4: + self.skipTest("requires page_size=1, sliding_window_size=4") + cache, allocator, req_to_token_pool = build_fixture(self.cfg) + full_avail = allocator.full_attn_allocator.available_size() + swa_avail = allocator.swa_attn_allocator.available_size() + + req = self._make_req(req_to_token_pool) + tokens = self._make_seq(1, 8) + # Live SWA is [6, 8): two tokens, below the window of four. + evicted_len = 6 + req.origin_input_ids = array("q", tokens) + req.output_ids = array("q") + req.full_untruncated_fill_ids = array("q", tokens) + req.extend_end = len(req.full_untruncated_fill_ids) + kv_indices = self._alloc(allocator, len(tokens)) + req_to_token_pool.write( + (req.kv.req_pool_idx, slice(0, len(tokens))), kv_indices + ) + req.kv.kv_committed_len = len(tokens) + req.last_node = cache.root_node_handle() + req.kv.cache_protected_len = 0 + req.lock = None + req.extra_key = None + # The window already slid past [0, 6): those SWA slots are freed. + allocator.free_swa(kv_indices[:evicted_len]) + req.kv.set_evicted_seqlen(ComponentType.SWA, evicted_len) + + cache.checkpoint(req, up_to=req.extend_end) + + inserted = len(tokens) - (1 if self.cfg.is_eagle else 0) + self.assertEqual(req.kv.cache_protected_len, inserted) + torch.testing.assert_close( + req_to_token_pool.req_to_token[req.kv.req_pool_idx, : len(tokens)], + kv_indices, + check_dtype=False, + ) + # A fresh matcher still needs a full live window and gets nothing. + m = cache.match_prefix(MatchPrefixParams(key=RadixKey(array("q", tokens)))) + self.assertEqual(m.device_prefix_len, 0) + + finish_req(cache, req, len(tokens)) + req.kv.mark_kv_released() + cache.sanity_check() + # Everything the request held is now tree-owned exactly once. + self.assertEqual( + allocator.full_attn_allocator.available_size() + + cache.full_evictable_size(), + full_avail, + ) + self.assertEqual( + allocator.swa_attn_allocator.available_size() + cache.swa_evictable_size(), + swa_avail, + ) + def test_swa_unfinished_req_preserves_existing_eviction_boundary(self): if not self.cfg.has_swa or self.cfg.has_mamba: self.skipTest("requires SWA without Mamba") diff --git a/test/registered/unit/spec/test_spec_registry.py b/test/registered/unit/spec/test_spec_registry.py index 6d0c00a855..1fd2996bd2 100644 --- a/test/registered/unit/spec/test_spec_registry.py +++ b/test/registered/unit/spec/test_spec_registry.py @@ -209,6 +209,8 @@ class TestServerArgsHook(_RegistryIsolated): return MagicMock server_args = SimpleNamespace( + disaggregation_mode="null", + disaggregation_decode_enable_radix_cache=False, speculative_draft_model_path=None, speculative_draft_model_revision=None, speculative_moe_runner_backend=None,