diff --git a/python/sglang/srt/mem_cache/radix_cache.py b/python/sglang/srt/mem_cache/radix_cache.py index 70ddffc5b8..71ba4a4357 100644 --- a/python/sglang/srt/mem_cache/radix_cache.py +++ b/python/sglang/srt/mem_cache/radix_cache.py @@ -287,8 +287,11 @@ class RadixCache(BasePrefixCache): self.is_eagle = params.is_eagle self.eviction_policy = params.eviction_policy.lower() + # A disabled tree holds nothing, so it reports no KV events and never + # evicts; its eviction policy config is not built or validated. self.kv_events = KVCacheEventRecorder( - enabled=params.enable_kv_cache_events, page_size=self.page_size + enabled=params.enable_kv_cache_events and not self.disable, + page_size=self.page_size, ) if params.enable_metrics: @@ -304,7 +307,8 @@ class RadixCache(BasePrefixCache): self.device = torch.device("cpu") self.eviction_strategy = get_eviction_strategy( - self.eviction_policy, params.eviction_policy_config + "lru" if self.disable else self.eviction_policy, + None if self.disable else params.eviction_policy_config, ) self.evictable_leaves = set() diff --git a/python/sglang/srt/mem_cache/unified_radix_cache.py b/python/sglang/srt/mem_cache/unified_radix_cache.py index 7e1eaa82fd..37a06b11c1 100644 --- a/python/sglang/srt/mem_cache/unified_radix_cache.py +++ b/python/sglang/srt/mem_cache/unified_radix_cache.py @@ -216,9 +216,21 @@ class UnifiedRadixCache(BasePrefixCache): # The TreeCore owns the tree member-var state (structure, LRUs, sizes, # evictable leaves) and drives the components' tree-level hooks. self._tree_core_backend = select_tree_core_backend(params) + # A disabled tree holds nothing, so it reports no KV events and never + # evicts; its eviction policy config is not built or validated. + tree_params = ( + replace( + params, + enable_kv_cache_events=False, + eviction_policy="lru", + eviction_policy_config=None, + ) + if self.disable + else params + ) self.tree_core = create_tree_core( name=self._tree_core_backend, - params=params, + params=tree_params, components=self.components, ) # Components execute boundary actions through the tree core. diff --git a/test/registered/unit/mem_cache/test_radix_cache_unit.py b/test/registered/unit/mem_cache/test_radix_cache_unit.py index cbe39a6a5d..7b8528201f 100644 --- a/test/registered/unit/mem_cache/test_radix_cache_unit.py +++ b/test/registered/unit/mem_cache/test_radix_cache_unit.py @@ -44,6 +44,7 @@ from sglang.srt.mem_cache.base_prefix_cache import ( InsertParams, MatchPrefixParams, ) +from sglang.srt.mem_cache.cache_init_params import CacheInitParams from sglang.srt.mem_cache.events import KVCacheEventRecorder from sglang.srt.mem_cache.radix_cache import RadixCache, RadixKey, TreeNode from sglang.srt.utils import get_device @@ -325,6 +326,27 @@ class TestRadixCache(CustomTestCase): self.assertIsNotNone(cache.root_node) self.assertEqual(len(cache.root_node.key), 0) + def test_disabled_cache_skips_events_and_eviction_config(self): + def make(disable): + return RadixCache( + CacheInitParams( + disable=disable, + req_to_token_pool=None, + token_to_kv_pool_allocator=None, + page_size=1, + enable_kv_cache_events=True, + eviction_policy="lru", + eviction_policy_config={"not_an_lru_option": 1}, + ) + ) + + cache = make(disable=True) + cache.reset() + self.assertEqual(cache.take_events(), []) + + with self.assertRaises(TypeError): + make(disable=False) + def test_reset(self): """Test reset method.""" cache = RadixCache.create_simulated() 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 906947371e..07c61d6815 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 @@ -379,6 +379,34 @@ class TestUnifiedRadixComponentLedgerUnits(CustomTestCase): ) +class TestDisabledUnifiedRadixCache(CustomTestCase): + def _params(self, disable): + return CacheInitParams( + req_to_token_pool=ReqToTokenPool( + size=2, + max_context_len=8, + device="cpu", + enable_memory_saver=False, + ), + token_to_kv_pool_allocator=None, + page_size=1, + disable=disable, + tree_components=(ComponentType.FULL,), + component_registry_override={ComponentType.FULL: _FakeFullComponent}, + enable_kv_cache_events=True, + eviction_policy="lru", + eviction_policy_config={"not_an_lru_option": 1}, + ) + + def test_disabled_cache_skips_events_and_eviction_config(self): + cache = UnifiedRadixCache(params=self._params(disable=True)) + cache.reset() + self.assertEqual(cache.take_events(), []) + + with self.assertRaises(TypeError): + UnifiedRadixCache(params=self._params(disable=False)) + + class TestUnifiedTreeNodeGetPrefixHashValues(CustomTestCase): def test_get_prefix_hash_values_not_shared_across_calls(self): """Regression guard for cached mutable prefix hash lists (#26177)."""