diff --git a/python/sglang/srt/disaggregation/decode_hicache_mixin.py b/python/sglang/srt/disaggregation/decode_hicache_mixin.py index acfddcecdb..2ccef12ec0 100644 --- a/python/sglang/srt/disaggregation/decode_hicache_mixin.py +++ b/python/sglang/srt/disaggregation/decode_hicache_mixin.py @@ -266,9 +266,6 @@ class DecodeHiCacheTransferMixin: return True def _process_hicache_local_restores(self, decode_reqs: List[DecodeRequest]) -> None: - if not hasattr(self.tree_cache, "is_load_back_event_done"): - return - # Filter once: keep only PENDING reqs that still need restore work; # trivially-done reqs (no prefix_match / nothing to restore) flip to READY. active: List[DecodeRequest] = [] diff --git a/python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py b/python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py index dd0d456968..eb11ae5253 100644 --- a/python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py +++ b/python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py @@ -86,30 +86,3 @@ def make_row_source( read_bytes=clipped, ), ) - - -def make_packed_source( - *, - page_buffer: torch.Tensor, - page_size: int, - bytes_per_token: int, - read_bytes: int, -) -> Tuple[RealKvSource, ...]: - if read_bytes == 0 or page_buffer.numel() == 0: - return () - flat = page_buffer.contiguous().view(torch.uint8) - if flat.ndim == 1: - flat = flat.reshape(1, -1) - clipped = _clip_read_bytes_aligned( - requested=read_bytes, num_bytes_per_token=bytes_per_token - ) - if clipped == 0: - return () - return ( - RealKvSource( - tensor=flat, - page_size=page_size, - num_bytes_per_token=bytes_per_token, - read_bytes=clipped, - ), - ) diff --git a/python/sglang/srt/kv_canary/radix_cache_walker.py b/python/sglang/srt/kv_canary/radix_cache_walker.py index 9935eb756f..3b06e70882 100644 --- a/python/sglang/srt/kv_canary/radix_cache_walker.py +++ b/python/sglang/srt/kv_canary/radix_cache_walker.py @@ -1,10 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING -import torch - -from sglang.srt.mem_cache.radix_cache import RadixCache from sglang.srt.mem_cache.unified_cache.unified_tree_core_interface import ( RadixCacheWalkResult, ) @@ -12,7 +9,6 @@ from sglang.srt.mem_cache.unified_radix_cache import UnifiedRadixCache if TYPE_CHECKING: from sglang.srt.mem_cache.base_prefix_cache import BasePrefixCache - from sglang.srt.mem_cache.radix_cache import TreeNode def walk_radix_cache_for_canary( @@ -28,83 +24,10 @@ def walk_radix_cache_for_canary( per-forward HEAD/TAIL path). ``unlocked_only=True`` skips nodes still locked by a running req. ``swa_resident_only=True`` skips SWA-tombstoned nodes (slots evicted from the SWA window).""" - cache_type = type(radix_cache) - if cache_type is UnifiedRadixCache: - return radix_cache.tree_core.walk_for_kv_canary( - unlocked_only=unlocked_only, swa_resident_only=swa_resident_only - ) - if cache_type is not RadixCache: + if type(radix_cache) is not UnifiedRadixCache: raise NotImplementedError( - f"walk_radix_cache_for_canary does not support {cache_type.__name__}" + f"walk_radix_cache_for_canary does not support {type(radix_cache).__name__}" ) - - # RadixCache has no SWA tier, so every node it holds is SWA-resident and - # swa_resident_only is a no-op here. - slot_buf: list[int] = [] - position_buf: list[int] = [] - prev_slot_buf: list[int] = [] - - _walk_radix_subtree( - node=radix_cache.root_node, - depth=0, - parent_last_slot=-1, - slot_buf=slot_buf, - position_buf=position_buf, - prev_slot_buf=prev_slot_buf, - is_root=True, - unlocked_only=unlocked_only, + return radix_cache.tree_core.walk_for_kv_canary( + unlocked_only=unlocked_only, swa_resident_only=swa_resident_only ) - - slot_tensor = torch.tensor(slot_buf, dtype=torch.int64) - position_tensor = torch.tensor(position_buf, dtype=torch.int64) - prev_slot_tensor = torch.tensor(prev_slot_buf, dtype=torch.int64) - return RadixCacheWalkResult( - slot_indices=slot_tensor, - positions=position_tensor, - prev_slot_indices=prev_slot_tensor, - ) - - -def _walk_radix_subtree( - *, - node: TreeNode, - depth: int, - parent_last_slot: int, - slot_buf: list[int], - position_buf: list[int], - prev_slot_buf: list[int], - is_root: bool, - unlocked_only: bool, -) -> None: - node_slots = _node_slots_for_canary(node=node) - - emit_slots = not is_root and (not unlocked_only or node.lock_ref == 0) - - chain_last_slot = parent_last_slot - for j, slot in enumerate(node_slots): - prev = parent_last_slot if j == 0 else node_slots[j - 1] - if emit_slots: - slot_buf.append(slot) - position_buf.append(depth + j) - prev_slot_buf.append(prev) - chain_last_slot = slot - - child_depth = depth + len(node_slots) - for child in node.children.values(): - _walk_radix_subtree( - node=child, - depth=child_depth, - parent_last_slot=chain_last_slot, - slot_buf=slot_buf, - position_buf=position_buf, - prev_slot_buf=prev_slot_buf, - is_root=False, - unlocked_only=unlocked_only, - ) - - -def _node_slots_for_canary(*, node: TreeNode) -> list[int]: - value: Any = node.value - if isinstance(value, torch.Tensor): - return [int(s) for s in value.tolist()] - return [] diff --git a/python/sglang/srt/kv_canary/runner/swa_divergence.py b/python/sglang/srt/kv_canary/runner/swa_divergence.py index 7dda13930f..74fa6057af 100644 --- a/python/sglang/srt/kv_canary/runner/swa_divergence.py +++ b/python/sglang/srt/kv_canary/runner/swa_divergence.py @@ -140,15 +140,6 @@ class SwaDivergenceLog: return None return cls(**json.loads(match.group(1))) - @classmethod - def find_last(cls, text: str) -> Optional[tuple[SwaDivergenceLog, str]]: - last_match: Optional[re.Match] = None - for match in _SWA_DIVERGENCE_LINE_RE.finditer(text): - last_match = match - if last_match is None: - return None - return cls(**json.loads(last_match.group(1))), last_match.group(0) - @classmethod def find_all(cls, text: str) -> list[tuple[SwaDivergenceLog, str]]: return [ diff --git a/python/sglang/srt/mem_cache/base_prefix_cache.py b/python/sglang/srt/mem_cache/base_prefix_cache.py index a5950ad4f6..0d50b61711 100644 --- a/python/sglang/srt/mem_cache/base_prefix_cache.py +++ b/python/sglang/srt/mem_cache/base_prefix_cache.py @@ -406,18 +406,6 @@ class BasePrefixCache(ABC, PrefixCacheTrait): implementations that shard trees per cache namespace.""" return self.root_node - def is_root(self, node: Any) -> bool: - """Whether the node is a tree root.""" - return node is self.root_node - - def get_last_hash_value(self, node: Any) -> Optional[str]: - """The node's last page hash, or None when it was never hashed.""" - return node.get_last_hash_value() - - def get_prefix_hash_values(self, node: Any) -> list[str]: - """The hash chain of the node's ancestors, in root-to-parent order.""" - return node.get_prefix_hash_values(node.parent) - def rotation_base_of(self, node: Any) -> Optional[int]: """Logical-page KV sharding: the rotation base stamped on ``node``. diff --git a/python/sglang/srt/mem_cache/radix_cache.py b/python/sglang/srt/mem_cache/radix_cache.py index 5719c61498..d069b199b3 100644 --- a/python/sglang/srt/mem_cache/radix_cache.py +++ b/python/sglang/srt/mem_cache/radix_cache.py @@ -48,7 +48,6 @@ from sglang.srt.mem_cache.base_prefix_cache import ( from sglang.srt.mem_cache.events import KVCacheEventRecorder from sglang.srt.mem_cache.utils import ( get_eviction_strategy, - get_hash_str, split_node_hash_value, ) @@ -246,12 +245,6 @@ class RadixKey: return ((self.extra_key, self.cache_salt), plain) return plain if self.extra_key is None else (self.extra_key, plain) - def hash_page(self, start: int, end: int, prior_hash: Optional[str] = None) -> str: - """SHA256 for logical units [start, end); bigram mode feeds overlapping (t_i, t_{i+1}) byte pairs.""" - hash_value = get_hash_str(self[start:end], prior_hash) - assert isinstance(hash_value, str) - return hash_value - class TreeNode: counter = 0 @@ -280,19 +273,6 @@ class TreeNode: def evicted(self): return self.value is None - def get_last_hash_value(self) -> Optional[str]: - """Returns the hash value of the last page in this node.""" - if self.hash_value is None or len(self.hash_value) == 0: - return None - return self.hash_value[-1] - - def get_prefix_hash_values(self, node: TreeNode) -> List[str]: - chunks = [] - while node is not None and node.hash_value is not None: - chunks.append(node.hash_value) - node = node.parent - return [value for chunk in reversed(chunks) for value in chunk] - def __lt__(self, other: TreeNode): return self.last_access_time < other.last_access_time diff --git a/python/sglang/test/kv_canary/fixtures.py b/python/sglang/test/kv_canary/fixtures.py index 524a1ffe16..f932436805 100644 --- a/python/sglang/test/kv_canary/fixtures.py +++ b/python/sglang/test/kv_canary/fixtures.py @@ -2,7 +2,8 @@ from __future__ import annotations from dataclasses import dataclass from types import SimpleNamespace -from typing import List, Optional +from typing import List, Optional, Tuple +from unittest.mock import MagicMock import torch @@ -13,7 +14,13 @@ from sglang.srt.kv_canary.config import CanaryConfig, CanaryMode from sglang.srt.kv_canary.pool_patcher.adapters.mha import attach_mha from sglang.srt.kv_canary.pool_patcher.adapters.swa import attach_swa from sglang.srt.kv_canary.pool_patcher.api import register_pool_attacher -from sglang.srt.mem_cache.radix_cache import RadixCache, TreeNode +from sglang.srt.mem_cache.cache_init_params import CacheInitParams +from sglang.srt.mem_cache.unified_cache.components import ( + BASE_COMPONENT_TYPE, + ComponentType, +) +from sglang.srt.mem_cache.unified_cache.unified_tree_core import UnifiedTreeCore +from sglang.srt.mem_cache.unified_radix_cache import UnifiedRadixCache, UnifiedTreeNode from sglang.srt.model_executor.forward_batch_info import ForwardMode from sglang.srt.utils import get_device @@ -268,28 +275,55 @@ def make_buffer_group( ) -def make_radix_cache( - slot_lists: List[List[int]], device: torch.device = DEFAULT_DEVICE -): - cache = RadixCache.__new__(RadixCache) - cache.device = device - cache.page_size = 1 - cache.disable = False - - root = TreeNode() - root.value = torch.tensor( - slot_lists[0] if slot_lists else [], dtype=torch.int32, device=device +def make_unified_radix_cache( + tree_components: Tuple[ComponentType, ...] = (ComponentType.FULL,), +) -> UnifiedRadixCache: + cache = UnifiedRadixCache.__new__(UnifiedRadixCache) + cache.tree_components = tree_components + cache.components = {ct: None for ct in tree_components} + cache.is_swa_enabled = ComponentType.SWA in tree_components + cache.tree_core = UnifiedTreeCore( + CacheInitParams( + disable=False, + req_to_token_pool=None, + token_to_kv_pool_allocator=None, + page_size=1, + ), + {ct: MagicMock() for ct in tree_components}, ) - cache.root_node = root + return cache - current = root - for child_slots in slot_lists[1:]: - child = TreeNode() - child.value = torch.tensor(child_slots, dtype=torch.int32, device=device) - child.parent = current - current.children[child.id] = child - current = child +def add_unified_child( + cache: UnifiedRadixCache, + slots: List[int], + *, + parent: Optional[UnifiedTreeNode] = None, + lock_ref: int = 0, + swa_value: Optional[List[int]] = None, + device: torch.device = DEFAULT_DEVICE, +) -> UnifiedTreeNode: + parent = cache.root_node if parent is None else parent + child = UnifiedTreeNode(cache.tree_components) + child.parent = parent + base = child.component_data[BASE_COMPONENT_TYPE] + base.value = torch.tensor(slots, dtype=torch.int32, device=device) + base.lock_ref = lock_ref + if swa_value is not None: + child.component_data[ComponentType.SWA].value = torch.tensor( + swa_value, dtype=torch.int32, device=device + ) + parent.children[child.id] = child + return child + + +def make_unified_radix_chain( + chain: List[List[int]], device: torch.device = DEFAULT_DEVICE +) -> UnifiedRadixCache: + cache = make_unified_radix_cache() + parent = None + for slots in chain: + parent = add_unified_child(cache, slots, parent=parent, device=device) return cache diff --git a/test/registered/kv_canary/test_self_unit_buffer_alloc.py b/test/registered/kv_canary/test_self_unit_buffer_alloc.py index 91d21c8b00..1212126f2e 100644 --- a/test/registered/kv_canary/test_self_unit_buffer_alloc.py +++ b/test/registered/kv_canary/test_self_unit_buffer_alloc.py @@ -8,7 +8,6 @@ import torch from sglang.kernels.ops.kv_canary.consts import RealKvHashMode from sglang.srt.kv_canary.config import CanaryConfig, CanaryMode from sglang.srt.kv_canary.pool_patcher.buffer_alloc import ( - make_packed_source, make_row_source, resolve_real_kv_read_bytes, ) @@ -76,69 +75,5 @@ class TestMakeRowSource(CustomTestCase): make_row_source(layer_buffer=layer_buf, read_bytes=read_bytes) -class TestMakePackedSource(CustomTestCase): - def test_make_packed_source_large_stride(self) -> None: - """Verify packed sources with 128-byte stride return the requested clip / full stride.""" - bytes_per_token = 128 - page_size = 2 - page_buffer = torch.zeros(4, bytes_per_token * page_size, dtype=torch.uint8) - cases = [ - ("partial", 32, 32), - ("all", sys.maxsize, bytes_per_token), - ] - for label, read_bytes, expected_read in cases: - with self.subTest(label=label): - sources = make_packed_source( - page_buffer=page_buffer, - page_size=page_size, - bytes_per_token=bytes_per_token, - read_bytes=read_bytes, - ) - self.assertEqual(len(sources), 1) - self.assertEqual(sources[0].read_bytes, expected_read) - self.assertEqual(sources[0].num_bytes_per_token, bytes_per_token) - - def test_make_packed_source_small_stride_raises(self) -> None: - """Verify packed sources reject 8-byte strides (cannot satisfy 16-byte aligned loads).""" - bytes_per_token = 8 - page_size = 1 - page_buffer = torch.zeros(4, bytes_per_token, dtype=torch.uint8) - for label, read_bytes in [("partial", 32), ("all", sys.maxsize)]: - with self.subTest(label=label): - with self.assertRaisesRegex(ValueError, "num_bytes_per_token"): - make_packed_source( - page_buffer=page_buffer, - page_size=page_size, - bytes_per_token=bytes_per_token, - read_bytes=read_bytes, - ) - - def test_make_packed_source_unaligned_read_bytes_raises(self) -> None: - """Verify packed sources reject unaligned explicit reads.""" - bytes_per_token = 128 - page_size = 1 - page_buffer = torch.zeros(4, bytes_per_token, dtype=torch.uint8) - with self.assertRaisesRegex(ValueError, "multiple of 16"): - make_packed_source( - page_buffer=page_buffer, - page_size=page_size, - bytes_per_token=bytes_per_token, - read_bytes=24, - ) - - def test_make_packed_source_oversized_read_bytes_raises(self) -> None: - """Verify packed sources reject oversized explicit reads.""" - bytes_per_token = 128 - page_size = 1 - page_buffer = torch.zeros(4, bytes_per_token, dtype=torch.uint8) - with self.assertRaisesRegex(ValueError, "<= num_bytes_per_token"): - make_packed_source( - page_buffer=page_buffer, - page_size=page_size, - bytes_per_token=bytes_per_token, - read_bytes=256, - ) - - if __name__ == "__main__": unittest.main() diff --git a/test/registered/kv_canary/test_self_unit_perturb.py b/test/registered/kv_canary/test_self_unit_perturb.py index 10a21dc547..d89d8a763a 100644 --- a/test/registered/kv_canary/test_self_unit_perturb.py +++ b/test/registered/kv_canary/test_self_unit_perturb.py @@ -27,13 +27,16 @@ from sglang.srt.kv_canary.perturb.utils import ( flip_first_byte_in_source, pick_target_group, ) +from sglang.srt.mem_cache.unified_cache.components import ComponentType from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci from sglang.test.kv_canary.fixtures import ( DEFAULT_DEVICE, + add_unified_child, make_buffer_group, make_forward_batch, - make_radix_cache, make_req_to_token_pool, + make_unified_radix_cache, + make_unified_radix_chain, ) from sglang.test.test_utils import CustomTestCase @@ -441,7 +444,7 @@ class TestRealKvUnusedCachePerturb(CustomTestCase): outer_step_counter_getter=lambda: 10, sweep_interval=1, ) - manager.attach_radix_cache(make_radix_cache([[], [3]], device=device)) + manager.attach_radix_cache(make_unified_radix_chain([[3]], device=device)) snapshot = source.tensor.clone() with ( @@ -462,9 +465,9 @@ class TestRealKvUnusedCachePerturb(CustomTestCase): """Verify unused-cache perturbation chooses only unlocked radix-cache slots.""" device = DEFAULT_DEVICE group = make_buffer_group(kind=PoolKind.FULL, has_real_kv=True) - cache = make_radix_cache([[], [1, 2], [3]], device=device) - locked_node = next(iter(cache.root_node.children.values())) - locked_node.lock_ref = 1 + cache = make_unified_radix_cache() + locked_node = add_unified_child(cache, [1, 2], lock_ref=1, device=device) + add_unified_child(cache, [3], parent=locked_node, device=device) with patch.object(torch, "randint", return_value=torch.tensor(0)): slot = real_kv_unused_cache_module._pick_sweep_slot_for_group( @@ -482,7 +485,8 @@ class TestRealKvUnusedCachePerturb(CustomTestCase): group = make_buffer_group( kind=PoolKind.SWA, has_real_kv=True, swa_index_lut=lut ) - cache = make_radix_cache([[], [1]], device=device) + cache = make_unified_radix_cache((ComponentType.FULL, ComponentType.SWA)) + add_unified_child(cache, [1], swa_value=[1], device=device) with patch.object(torch, "randint", return_value=torch.tensor(0)): slot = real_kv_unused_cache_module._pick_sweep_slot_for_group( diff --git a/test/registered/kv_canary/test_self_unit_radix_walker.py b/test/registered/kv_canary/test_self_unit_radix_walker.py index 3d94ca4aed..6ca88e7dbb 100644 --- a/test/registered/kv_canary/test_self_unit_radix_walker.py +++ b/test/registered/kv_canary/test_self_unit_radix_walker.py @@ -2,21 +2,18 @@ from __future__ import annotations import unittest from array import array -from unittest import mock - -import torch from sglang.srt.kv_canary.radix_cache_walker import walk_radix_cache_for_canary -from sglang.srt.mem_cache.cache_init_params import CacheInitParams from sglang.srt.mem_cache.radix_cache import RadixKey from sglang.srt.mem_cache.unified_cache.components import ( - BASE_COMPONENT_TYPE, ComponentType, ) -from sglang.srt.mem_cache.unified_cache.unified_tree_core import UnifiedTreeCore -from sglang.srt.mem_cache.unified_radix_cache import UnifiedRadixCache, UnifiedTreeNode +from sglang.srt.mem_cache.unified_radix_cache import UnifiedTreeNode from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci -from sglang.test.kv_canary.fixtures import DEFAULT_DEVICE, make_radix_cache +from sglang.test.kv_canary.fixtures import ( + add_unified_child, + make_unified_radix_cache, +) from sglang.test.test_utils import CustomTestCase register_cuda_ci(est_time=10, stage="extra-a", runner_config="1-gpu-small") @@ -24,64 +21,14 @@ register_amd_ci(est_time=30, suite="extra-a-test-1-gpu-small-amd") class TestSelfUnitRadixWalker(CustomTestCase): - def setUp(self): - self.device = DEFAULT_DEVICE - - def test_single_node_chain_positions_increase(self): - """Verify a single radix chain emits increasing positions.""" - chain = [10, 20, 30, 40] - cache = make_radix_cache([[], chain], device=self.device) - result = walk_radix_cache_for_canary(radix_cache=cache) - self.assertEqual(result.slot_indices.tolist(), chain) - self.assertEqual(result.positions.tolist(), [0, 1, 2, 3]) - self.assertEqual(result.prev_slot_indices.tolist(), [-1, 10, 20, 30]) - - def test_child_node_first_slot_prev_is_parent_last(self): - """Verify child chains link their first slot to the parent tail.""" - parent = [7, 8] - child = [9, 10] - cache = make_radix_cache([[], parent, child], device=self.device) - result = walk_radix_cache_for_canary(radix_cache=cache) - self.assertEqual(result.slot_indices.tolist(), parent + child) - self.assertEqual(result.prev_slot_indices.tolist()[len(parent)], parent[-1]) - - def test_root_child_first_slot_prev_minus_one(self): - """Verify root child chains use -1 as the initial previous slot.""" - cache = make_radix_cache([[], [42, 43]], device=self.device) - result = walk_radix_cache_for_canary(radix_cache=cache) - self.assertEqual(int(result.prev_slot_indices[0]), -1) - - def test_position_equals_depth_from_root(self): - """Verify emitted positions match depth from the radix root.""" - cache = make_radix_cache([[], [1, 2], [3], [4, 5]], device=self.device) - result = walk_radix_cache_for_canary(radix_cache=cache) - self.assertEqual(result.positions.tolist(), [0, 1, 2, 3, 4]) - self.assertEqual(result.slot_indices.tolist(), [1, 2, 3, 4, 5]) - - def test_walk_includes_locked_nodes_by_default(self): - """Verify radix walking includes locked nodes by default.""" - cache = make_radix_cache([[], [1, 2], [3, 4]], device=self.device) - locked_node = next(iter(cache.root_node.children.values())) - locked_node.lock_ref = 1 - result = walk_radix_cache_for_canary(radix_cache=cache) - self.assertEqual(result.slot_indices.tolist(), [1, 2, 3, 4]) - - def test_walk_unlocked_only_skips_locked(self): - """Verify unlocked-only radix walking skips locked nodes.""" - cache = make_radix_cache([[], [1, 2], [3, 4]], device=self.device) - locked_node = next(iter(cache.root_node.children.values())) - locked_node.lock_ref = 1 - result = walk_radix_cache_for_canary(radix_cache=cache, unlocked_only=True) - self.assertEqual(result.slot_indices.tolist(), [3, 4]) - def test_unified_swa_sweep_gates_on_swa_lock_not_full_lock(self): """With unlocked_only + swa_resident_only, the sweep filters on the SWA component lock: a FULL-locked node whose SWA lock was already released (early dec_swa_lock_only) must still be swept, and a node whose SWA lock is still held must not.""" - cache = self._make_unified_cache((ComponentType.FULL, ComponentType.SWA)) - self._add_unified_child(cache, [1, 2], lock_ref=1, swa_value=[1, 2]) - held = self._add_unified_child(cache, [3, 4], swa_value=[3, 4]) + cache = make_unified_radix_cache((ComponentType.FULL, ComponentType.SWA)) + add_unified_child(cache, [1, 2], lock_ref=1, swa_value=[1, 2]) + held = add_unified_child(cache, [3, 4], swa_value=[3, 4]) held.component_data[ComponentType.SWA].lock_ref = 1 result = walk_radix_cache_for_canary( @@ -89,48 +36,10 @@ class TestSelfUnitRadixWalker(CustomTestCase): ) self.assertEqual(result.slot_indices.tolist(), [1, 2]) - def _make_unified_cache( - self, tree_components: tuple[ComponentType, ...] - ) -> UnifiedRadixCache: - cache = UnifiedRadixCache.__new__(UnifiedRadixCache) - cache.tree_components = tree_components - cache.components = {ct: None for ct in tree_components} - cache.is_swa_enabled = ComponentType.SWA in tree_components - cache.tree_core = UnifiedTreeCore( - CacheInitParams( - disable=False, - req_to_token_pool=None, - token_to_kv_pool_allocator=None, - page_size=1, - ), - {ct: mock.MagicMock() for ct in tree_components}, - ) - return cache - - def _add_unified_child( - self, - cache: UnifiedRadixCache, - slots: list[int], - *, - lock_ref: int = 0, - swa_value: list[int] | None = None, - ) -> UnifiedTreeNode: - child = UnifiedTreeNode(cache.tree_components) - child.parent = cache.root_node - base = child.component_data[BASE_COMPONENT_TYPE] - base.value = torch.tensor(slots, dtype=torch.int32, device=self.device) - base.lock_ref = lock_ref - if swa_value is not None: - child.component_data[ComponentType.SWA].value = torch.tensor( - swa_value, dtype=torch.int32, device=self.device - ) - cache.root_node.children[child.id] = child - return child - def test_unified_walk_emits_full_component_slots(self): """Verify unified radix walking emits the base (full) component slots.""" - cache = self._make_unified_cache((ComponentType.FULL,)) - self._add_unified_child(cache, [10, 20, 30]) + cache = make_unified_radix_cache((ComponentType.FULL,)) + add_unified_child(cache, [10, 20, 30]) result = walk_radix_cache_for_canary(radix_cache=cache) self.assertEqual(result.slot_indices.tolist(), [10, 20, 30]) self.assertEqual(result.positions.tolist(), [0, 1, 2]) @@ -138,17 +47,17 @@ class TestSelfUnitRadixWalker(CustomTestCase): def test_unified_walk_unlocked_only_uses_full_lock_ref(self): """Verify unified radix walking honors the base component lock reference.""" - cache = self._make_unified_cache((ComponentType.FULL,)) - self._add_unified_child(cache, [1, 2], lock_ref=1) - self._add_unified_child(cache, [3, 4]) + cache = make_unified_radix_cache((ComponentType.FULL,)) + add_unified_child(cache, [1, 2], lock_ref=1) + add_unified_child(cache, [3, 4]) result = walk_radix_cache_for_canary(radix_cache=cache, unlocked_only=True) self.assertEqual(result.slot_indices.tolist(), [3, 4]) def test_unified_swa_resident_only_skips_evicted_swa_nodes(self): """Verify unified radix walking skips nodes whose SWA storage was evicted.""" - cache = self._make_unified_cache((ComponentType.FULL, ComponentType.SWA)) - self._add_unified_child(cache, [1, 2], swa_value=None) - self._add_unified_child(cache, [3, 4], swa_value=[3, 4]) + cache = make_unified_radix_cache((ComponentType.FULL, ComponentType.SWA)) + add_unified_child(cache, [1, 2], swa_value=None) + add_unified_child(cache, [3, 4], swa_value=[3, 4]) result = walk_radix_cache_for_canary( radix_cache=cache, swa_resident_only=True, @@ -158,12 +67,12 @@ class TestSelfUnitRadixWalker(CustomTestCase): def test_unified_walk_spans_device_evicted_nodes_without_emitting_them(self): """Verify device-evicted (host-only) nodes emit no slots but still advance positions by their key length and pass the prev-slot chain through.""" - cache = self._make_unified_cache((ComponentType.FULL,)) + cache = make_unified_radix_cache((ComponentType.FULL,)) evicted = UnifiedTreeNode(cache.tree_components) evicted.parent = cache.root_node evicted.key = RadixKey(array("q", [7, 8]), None) cache.root_node.children[evicted.id] = evicted - grandchild = self._add_unified_child(cache, [5, 6]) + grandchild = add_unified_child(cache, [5, 6]) cache.root_node.children.pop(grandchild.id) grandchild.parent = evicted evicted.children[grandchild.id] = grandchild @@ -174,9 +83,9 @@ class TestSelfUnitRadixWalker(CustomTestCase): def test_unified_swa_resident_only_noop_without_swa_component(self): """Verify swa_resident_only is a no-op when SWA is not enabled.""" - cache = self._make_unified_cache((ComponentType.FULL,)) - self._add_unified_child(cache, [1, 2]) - self._add_unified_child(cache, [3, 4]) + cache = make_unified_radix_cache((ComponentType.FULL,)) + add_unified_child(cache, [1, 2]) + add_unified_child(cache, [3, 4]) result = walk_radix_cache_for_canary( radix_cache=cache, swa_resident_only=True, diff --git a/test/registered/kv_canary/test_self_unit_runner_sweep.py b/test/registered/kv_canary/test_self_unit_runner_sweep.py index ee59aee511..a36f6f1377 100644 --- a/test/registered/kv_canary/test_self_unit_runner_sweep.py +++ b/test/registered/kv_canary/test_self_unit_runner_sweep.py @@ -7,8 +7,8 @@ from sglang.srt.kv_canary import endpoint as endpoint_module from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci from sglang.test.kv_canary.fixtures import ( make_forward_batch, - make_radix_cache, make_req_to_token_pool, + make_unified_radix_chain, ) from sglang.test.kv_canary.runner_test_base import ( CanaryManagerTestCase, @@ -62,7 +62,7 @@ class TestSelfUnitManagerSweep(CanaryManagerTestCase): with manager.with_active_single_forward_manager(0): manager.pre_ops_maybe_inside_graph(forward_batch) - cache = make_radix_cache([[], [10, 11, 12]], device=self.device) + cache = make_unified_radix_chain([[10, 11, 12]], device=self.device) cache.req_to_token_pool = make_req_to_token_pool(self.device) manager.attach_radix_cache(cache) @@ -80,7 +80,7 @@ class TestSelfUnitManagerSweep(CanaryManagerTestCase): def test_sweep_allocates_verify_plan_from_walker_output(self) -> None: """Verify sweep planning sizes the verify plan from walker output.""" manager = make_manager(device=self.device) - cache = make_radix_cache([[], [10, 11], [12, 13, 14]], device=self.device) + cache = make_unified_radix_chain([[10, 11], [12, 13, 14]], device=self.device) cache.req_to_token_pool = make_req_to_token_pool(self.device) manager.attach_radix_cache(cache) diff --git a/test/registered/kv_canary/test_self_unit_sweep_plan_builder.py b/test/registered/kv_canary/test_self_unit_sweep_plan_builder.py index c4d973c833..b3fa2dba87 100644 --- a/test/registered/kv_canary/test_self_unit_sweep_plan_builder.py +++ b/test/registered/kv_canary/test_self_unit_sweep_plan_builder.py @@ -8,8 +8,8 @@ from sglang.srt.kv_canary.sweep_plan_builder import build_verify_plan_radix_swee from sglang.test.ci.ci_register import register_amd_ci, register_cuda_ci from sglang.test.kv_canary.fixtures import ( DEFAULT_DEVICE, - make_radix_cache, make_req_to_token_pool, + make_unified_radix_chain, ) from sglang.test.test_utils import CustomTestCase @@ -23,7 +23,7 @@ class TestSelfUnitSweepPlanBuilder(CustomTestCase): def test_build_verify_plan_radix_sweep(self) -> None: """Verify radix sweep verify plans include cached slot chains.""" - empty_cache = make_radix_cache([[]], device=self.device) + empty_cache = make_unified_radix_chain([], device=self.device) empty_cache.req_to_token_pool = make_req_to_token_pool(self.device) empty_out = build_verify_plan_radix_sweep( radix_cache=empty_cache, @@ -32,7 +32,7 @@ class TestSelfUnitSweepPlanBuilder(CustomTestCase): ) self.assertEqual(int(empty_out.verify_num_valid.item()), 0) - cache = make_radix_cache([[], [100, 101, 102]], device=self.device) + cache = make_unified_radix_chain([[100, 101, 102]], device=self.device) cache.req_to_token_pool = make_req_to_token_pool(self.device) out = build_verify_plan_radix_sweep( radix_cache=cache, @@ -49,7 +49,7 @@ class TestSelfUnitSweepPlanBuilder(CustomTestCase): def test_radix_held_slot_still_swept(self) -> None: """Verify held radix slots are still included in sweep plans.""" - cache = make_radix_cache([[], [42, 43, 44]], device=self.device) + cache = make_unified_radix_chain([[42, 43, 44]], device=self.device) cache.req_to_token_pool = make_req_to_token_pool(self.device) out = build_verify_plan_radix_sweep( radix_cache=cache, @@ -64,7 +64,7 @@ class TestSelfUnitSweepPlanBuilder(CustomTestCase): def test_truly_free_slot_not_swept(self) -> None: """Verify free radix slots are excluded from sweep plans.""" - empty_cache = make_radix_cache([[]], device=self.device) + empty_cache = make_unified_radix_chain([], device=self.device) empty_cache.req_to_token_pool = make_req_to_token_pool(self.device) out = build_verify_plan_radix_sweep( radix_cache=empty_cache, @@ -75,7 +75,7 @@ class TestSelfUnitSweepPlanBuilder(CustomTestCase): def test_swa_translate_preserves_evicted_as_padding_sentinel(self) -> None: """Evicted (LUT=0) slots stay in the plan as the padding sentinel; the kernel does the skipping.""" - cache = make_radix_cache([[], [100, 101, 102]], device=self.device) + cache = make_unified_radix_chain([[100, 101, 102]], device=self.device) cache.req_to_token_pool = make_req_to_token_pool(self.device) lut = torch.zeros(200, dtype=torch.int64, device=self.device) diff --git a/test/registered/unit/mem_cache/test_mem_cache_utils.py b/test/registered/unit/mem_cache/test_mem_cache_utils.py index 0385a25dc3..c0f0760dc7 100644 --- a/test/registered/unit/mem_cache/test_mem_cache_utils.py +++ b/test/registered/unit/mem_cache/test_mem_cache_utils.py @@ -90,9 +90,6 @@ class _HashKey: def raw_token_ids(self): return self.token_ids - def hash_page(self, start, end, prior_hash=None): - return _legacy_get_hash_str(self[start:end], prior_hash) - def _single_hash_compatibility_cases(): prior_hash = _legacy_get_hash_str([7, 8, 9]) @@ -263,15 +260,6 @@ class TestGetHashStr(unittest.TestCase): with self.subTest(tokens=tokens): self.assertRegex(get_hash_str(tokens), r"^[0-9a-f]{64}$") - def test_hash_key_hash_page_matches_get_hash_str(self): - key = _HashKey(array("q", [1, 2, 3, 4, 5, 6]), is_bigram=True) - prior_hash = get_hash_str([(9, 10)]) - - self.assertEqual( - key.hash_page(1, 4, prior_hash), - get_hash_str(key[1:4], prior_hash), - ) - class TestStorageHashNamespace(unittest.TestCase): def test_node_hashes_isolate_namespaces_and_continue_the_chain(self): 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 349cfebe1d..a11faacad0 100644 --- a/test/registered/unit/mem_cache/test_radix_cache_unit.py +++ b/test/registered/unit/mem_cache/test_radix_cache_unit.py @@ -291,46 +291,6 @@ class TestTreeNode(unittest.TestCase): node.value = torch.tensor([1, 2, 3]) self.assertFalse(node.evicted) - def test_get_last_hash_value(self): - """Test get_last_hash_value method.""" - node = TreeNode() - self.assertIsNone(node.get_last_hash_value()) - - node.hash_value = ["hash1", "hash2", "hash3"] - self.assertEqual(node.get_last_hash_value(), "hash3") - - def test_get_prefix_hash_values_not_shared_across_calls(self): - """Regression guard for cached mutable prefix hash lists.""" - for node_cls in (TreeNode,): - with self.subTest(node_cls=node_cls.__module__): - root = node_cls() - n1 = node_cls() - n1.parent = root - n1.hash_value = ["h1"] - n2 = node_cls() - n2.parent = n1 - n2.hash_value = ["h2"] - n3 = node_cls() - n3.parent = n2 - n3.hash_value = ["h3"] - - first = n3.get_prefix_hash_values(n2) - self.assertEqual(first, ["h1", "h2"]) - - # Downstream storage code extends prefix_keys in place while - # processing pages. A cached list must not be observable by a - # later call. - first += ["h3"] - - second = n3.get_prefix_hash_values(n2) - self.assertEqual(second, ["h1", "h2"]) - self.assertIsNot(second, first) - - n4 = node_cls() - n4.parent = n3 - n4.hash_value = ["h4"] - self.assertEqual(n4.get_prefix_hash_values(n3), ["h1", "h2", "h3"]) - class TestRadixCache(CustomTestCase): """Test cases for RadixCache class.""" diff --git a/test/registered/unit/mem_cache/test_session_unified_radix_cache.py b/test/registered/unit/mem_cache/test_session_unified_radix_cache.py index e50475985d..2678b61976 100644 --- a/test/registered/unit/mem_cache/test_session_unified_radix_cache.py +++ b/test/registered/unit/mem_cache/test_session_unified_radix_cache.py @@ -18,7 +18,7 @@ from sglang.srt.mem_cache.base_prefix_cache import ( ) from sglang.srt.mem_cache.cache_init_params import CacheInitParams from sglang.srt.mem_cache.memory_pool import MHATokenToKVPool, ReqToTokenPool -from sglang.srt.mem_cache.radix_cache import RadixCache, RadixKey +from sglang.srt.mem_cache.radix_cache import RadixKey from sglang.srt.mem_cache.unified_cache.components import ComponentType from sglang.srt.mem_cache.unified_radix_cache import UnifiedRadixCache from sglang.test.test_utils import CustomTestCase @@ -98,15 +98,6 @@ def register(cache, token_ids, session_id, generation=None): ) -class TestRadixCacheSessionRemoval(CustomTestCase): - def test_plain_radix_cache_does_not_enable_session_references(self): - cache = RadixCache(make_params(enable_session=True)) - - self.assertFalse(hasattr(cache, "enable_session_radix_cache")) - self.assertFalse(hasattr(cache, "register_session_ref")) - self.assertFalse(hasattr(cache, "open_radix_session")) - - class TestSessionUnifiedRadixCache(CustomTestCase): def setUp(self): self.cache = UnifiedRadixCache(make_params(enable_session=True))