Keep CP HiCache host visibility behind per-layer target and draft backup
CP shared KV needs HiCache backup to overlap with layer execution without exposing partially copied host state. Split CP backup into reservation, pending radix state, per-layer target/draft D2H submission, and one final ack-driven visibility commit. The all-layer path remains available only as an explicit fallback and now logs a warning when used. Constraint: CP shared KV owner-lane metadata and draft/MTP KV must stay strongly synchronized with target KV. Constraint: Local CUDA tests are disallowed; CUDA verification was run only in the g0034 container. Rejected: Let target layer hooks copy draft KV too | draft may not have stored that layer yet, which can corrupt MTP accept behavior. Rejected: Silent all-layer fallback | it hides performance regressions and makes ETE logs ambiguous. Confidence: medium Scope-risk: broad Directive: Reserved or partially copied host payloads must remain invisible until final ack commits pending_host_backups. Tested: g0034 docker /sgl-workspace/sglang-tai PYTHONPATH=/mnt/beegfs/cjy/tai-kernel/python:python python -m pytest test/registered/unit/managers/test_hicache_controller_cp.py -q -> 49 passed. Tested: g0034 docker /sgl-workspace/sglang-tai PYTHONPATH=/mnt/beegfs/cjy/tai-kernel/python:python python -m pytest test/registered/unit/mem_cache/test_cp_hicache_metadata.py -q -> 58 passed. Tested: g0034 docker /mnt/beegfs/cjy/tai-kernel PYTHONPATH=python python -m pytest tests/nsa_prefill/test_kvcacheio_lf_pf.py -q -> 7 passed. Not-tested: Long-running GLM5 CP+HiCache+MTP ETE throughput and host-pressure soak.
This commit is contained in:
@@ -90,10 +90,15 @@ for _schema in (
|
||||
if "already" not in str(exc).lower() and "duplicate" not in str(exc).lower():
|
||||
raise
|
||||
|
||||
from sglang.srt.managers.cache_controller import (
|
||||
HiCacheWriteFailure,
|
||||
HiCacheWriteReservation,
|
||||
)
|
||||
from sglang.srt.mem_cache.base_prefix_cache import EvictParams, MatchPrefixParams
|
||||
from sglang.srt.mem_cache.hiradix_cache import (
|
||||
CpHiCacheNodeMetadata,
|
||||
HiRadixCache,
|
||||
PendingHiCacheBackup,
|
||||
_compute_shared_hicache_token_capacities,
|
||||
)
|
||||
from sglang.srt.mem_cache.radix_cache import RadixKey, TreeNode
|
||||
@@ -531,6 +536,49 @@ class FakeZeroOwnedWriteController:
|
||||
)
|
||||
|
||||
|
||||
class FakeReserveWriteController:
|
||||
write_policy = "write_through"
|
||||
has_draft_hicache = False
|
||||
|
||||
def __init__(self, results):
|
||||
self.results = list(results)
|
||||
self.reservations = []
|
||||
self.submitted = []
|
||||
self.evicted_host_indices = []
|
||||
|
||||
def reserve_write_cp(self, device_indices, priority=None, node_id=-1):
|
||||
self.reservations.append((device_indices.clone(), node_id))
|
||||
result = self.results.pop(0)
|
||||
return result(device_indices) if callable(result) else result
|
||||
|
||||
def submit_write_cp_all_layer(self, reservation):
|
||||
self.submitted.append(reservation)
|
||||
|
||||
def submit_write_cp_per_layer(self, reservation):
|
||||
self.submitted.append(reservation)
|
||||
|
||||
def evict_cp_host(self, metadata):
|
||||
self.evicted_host_indices.append(metadata.host_indices.clone())
|
||||
return len(metadata.host_indices)
|
||||
|
||||
|
||||
def make_write_reservation(device_indices, node_id=0, host_start=90):
|
||||
host_indices = torch.arange(host_start, host_start + len(device_indices))
|
||||
metadata = CpHiCacheNodeMetadata(
|
||||
logical_len=len(device_indices),
|
||||
owned_positions=torch.arange(len(device_indices), dtype=torch.int64),
|
||||
host_indices=host_indices,
|
||||
page_owners=torch.zeros(max(len(device_indices), 0), dtype=torch.int8),
|
||||
page_size=1,
|
||||
)
|
||||
return HiCacheWriteReservation(
|
||||
metadata=metadata,
|
||||
host_indices=host_indices,
|
||||
physical_device_indices=device_indices.clone(),
|
||||
node_id=node_id,
|
||||
)
|
||||
|
||||
|
||||
class FakeEvictionStrategy:
|
||||
def get_priority(self, node):
|
||||
return 0
|
||||
@@ -643,6 +691,65 @@ class TestHiRadixCacheCPBackup(CustomTestCase):
|
||||
cache.ongoing_write_through.clear()
|
||||
self.assertTrue(cache._node_backuped(node))
|
||||
|
||||
def test_node_backuped_excludes_explicit_pending_cp_backup_until_commit(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.cache_controller = types.SimpleNamespace(has_draft_hicache=False)
|
||||
cache.ongoing_write_through = {}
|
||||
cache.pending_host_backups = {}
|
||||
dec_locked = []
|
||||
cache.dec_node_lock_ref = lambda node: dec_locked.append(node)
|
||||
node = TreeNode()
|
||||
node.id = 128
|
||||
node.host_len = 64
|
||||
metadata = CpHiCacheNodeMetadata(
|
||||
logical_len=64,
|
||||
owned_positions=torch.empty((0,), dtype=torch.int64),
|
||||
host_indices=torch.empty((0,), dtype=torch.int64),
|
||||
page_owners=torch.tensor([0], dtype=torch.int8),
|
||||
page_size=64,
|
||||
)
|
||||
node.cp_hicache = metadata
|
||||
cache.pending_host_backups[node.id] = PendingHiCacheBackup(
|
||||
node=node, metadata=metadata, logical_len=64
|
||||
)
|
||||
|
||||
self.assertFalse(cache._node_backuped(node))
|
||||
|
||||
cache._commit_pending_backup(node.id)
|
||||
|
||||
self.assertTrue(cache._node_backuped(node))
|
||||
self.assertEqual(dec_locked, [node])
|
||||
|
||||
def test_rollback_pending_cp_backup_frees_reserved_host_slots(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
evicted = []
|
||||
cache.cache_controller = types.SimpleNamespace(
|
||||
evict_cp_host=lambda metadata: evicted.append(metadata) or 2
|
||||
)
|
||||
dec_locked = []
|
||||
cache.dec_node_lock_ref = lambda node: dec_locked.append(node)
|
||||
cache.pending_host_backups = {}
|
||||
node = TreeNode()
|
||||
node.id = 129
|
||||
metadata = CpHiCacheNodeMetadata(
|
||||
logical_len=64,
|
||||
owned_positions=torch.tensor([0, 1], dtype=torch.int64),
|
||||
host_indices=torch.tensor([10, 11], dtype=torch.int64),
|
||||
page_owners=torch.tensor([0], dtype=torch.int8),
|
||||
page_size=64,
|
||||
)
|
||||
cache.pending_host_backups[node.id] = PendingHiCacheBackup(
|
||||
node=node, metadata=metadata, logical_len=64
|
||||
)
|
||||
|
||||
cache._rollback_pending_backup(node.id)
|
||||
|
||||
self.assertEqual(evicted, [metadata])
|
||||
self.assertEqual(dec_locked, [node])
|
||||
self.assertNotIn(node.id, cache.pending_host_backups)
|
||||
|
||||
def test_single_node_write_lock_updates_device_evictable_leaf_set(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache.disable = False
|
||||
@@ -726,12 +833,18 @@ class TestHiRadixCacheCPBackup(CustomTestCase):
|
||||
def test_write_backup_retries_by_required_physical_slots(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.cache_controller = FakeWriteController(required_host_slots=1)
|
||||
reservation_factory = lambda device_indices: make_write_reservation(
|
||||
device_indices, node_id=122
|
||||
)
|
||||
cache.cache_controller = FakeReserveWriteController(
|
||||
[HiCacheWriteFailure(required_host_slots=1), reservation_factory]
|
||||
)
|
||||
cache.evictable_host_leaves = set()
|
||||
cache.eviction_strategy = FakeEvictionStrategy()
|
||||
cache.get_child_key_fn = lambda key: key.token_ids[0]
|
||||
cache._record_remove_event = lambda node: None
|
||||
cache.ongoing_write_through = {}
|
||||
cache.pending_host_backups = {}
|
||||
cache.inc_node_lock_ref = lambda node: None
|
||||
|
||||
root = TreeNode()
|
||||
@@ -754,6 +867,7 @@ class TestHiRadixCacheCPBackup(CustomTestCase):
|
||||
cache.evictable_host_leaves.add(evictable_node)
|
||||
|
||||
node = TreeNode()
|
||||
node.id = 122
|
||||
node.value = torch.arange(16, dtype=torch.int64)
|
||||
|
||||
cache.write_backup(node)
|
||||
@@ -764,13 +878,57 @@ class TestHiRadixCacheCPBackup(CustomTestCase):
|
||||
self.assertEqual(evictable_node.host_len, 0)
|
||||
self.assertIsNone(evictable_node.cp_hicache)
|
||||
self.assertNotIn(1, root.children)
|
||||
self.assertEqual(node.host_len, 16)
|
||||
self.assertEqual(node.host_len, 0)
|
||||
self.assertIsNone(node.cp_hicache)
|
||||
self.assertIn(node.id, cache.pending_host_backups)
|
||||
self.assertEqual(len(cache.cache_controller.submitted), 1)
|
||||
|
||||
def test_write_backup_cp_retry_failure_leaves_node_device_only(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.cache_controller = FakeReserveWriteController(
|
||||
[
|
||||
HiCacheWriteFailure(required_host_slots=2),
|
||||
HiCacheWriteFailure(required_host_slots=2),
|
||||
]
|
||||
)
|
||||
cache.evictable_host_leaves = set()
|
||||
cache.eviction_strategy = FakeEvictionStrategy()
|
||||
cache._record_remove_event = lambda node: None
|
||||
cache.ongoing_write_through = {}
|
||||
cache.pending_host_backups = {}
|
||||
|
||||
node = TreeNode()
|
||||
node.id = 124
|
||||
node.value = torch.arange(16, dtype=torch.int64)
|
||||
|
||||
backed_len = cache.write_backup(node)
|
||||
|
||||
self.assertEqual(backed_len, 0)
|
||||
self.assertEqual(node.host_len, 0)
|
||||
self.assertIsNone(node.cp_hicache)
|
||||
self.assertEqual(cache.pending_host_backups, {})
|
||||
self.assertEqual(cache.cache_controller.submitted, [])
|
||||
|
||||
def test_write_backup_cp_success_returns_logical_length_for_zero_owned_rank(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.cache_controller = FakeZeroOwnedWriteController()
|
||||
cache.cache_controller = FakeReserveWriteController(
|
||||
[lambda device_indices: HiCacheWriteReservation(
|
||||
metadata=CpHiCacheNodeMetadata(
|
||||
logical_len=len(device_indices),
|
||||
owned_positions=torch.empty((0,), dtype=torch.int64),
|
||||
host_indices=torch.empty((0,), dtype=torch.int64),
|
||||
page_owners=torch.zeros(max(len(device_indices), 0), dtype=torch.int8),
|
||||
page_size=1,
|
||||
),
|
||||
host_indices=torch.empty((0,), dtype=torch.int64),
|
||||
physical_device_indices=torch.empty((0,), dtype=torch.int64),
|
||||
node_id=123,
|
||||
)]
|
||||
)
|
||||
cache.ongoing_write_through = {}
|
||||
cache.pending_host_backups = {}
|
||||
cache.inc_node_lock_ref = lambda node: None
|
||||
node = TreeNode()
|
||||
node.id = 123
|
||||
@@ -779,8 +937,10 @@ class TestHiRadixCacheCPBackup(CustomTestCase):
|
||||
backed_len = cache.write_backup(node)
|
||||
|
||||
self.assertEqual(backed_len, 16)
|
||||
self.assertEqual(node.host_len, 16)
|
||||
self.assertEqual(node.cp_hicache.host_indices.tolist(), [])
|
||||
self.assertEqual(node.host_len, 0)
|
||||
self.assertIsNone(node.cp_hicache)
|
||||
self.assertIn(node.id, cache.pending_host_backups)
|
||||
self.assertEqual(len(cache.cache_controller.submitted), 1)
|
||||
|
||||
def test_attach_storage_backend_rejects_cp_hicache_without_controller_call(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
@@ -875,6 +1035,26 @@ class TestHiRadixCacheCPSplitEvict(CustomTestCase):
|
||||
self.assertEqual(child.cp_hicache.owned_positions.tolist(), [0, 4])
|
||||
self.assertEqual(child.cp_hicache.host_indices.tolist(), [22, 23])
|
||||
|
||||
def test_cp_host_leaf_status_skips_device_valid_host_backed_node(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.evictable_host_leaves = set()
|
||||
node = TreeNode()
|
||||
node.value = torch.arange(4, dtype=torch.int64)
|
||||
node.host_len = 4
|
||||
node.cp_hicache = CpHiCacheNodeMetadata(
|
||||
logical_len=4,
|
||||
owned_positions=torch.tensor([0], dtype=torch.int64),
|
||||
host_indices=torch.tensor([55], dtype=torch.int64),
|
||||
page_owners=torch.zeros(max(4, 0), dtype=torch.int8),
|
||||
page_size=1,
|
||||
)
|
||||
cache.evictable_host_leaves.add(node)
|
||||
|
||||
cache._update_host_leaf_status(node)
|
||||
|
||||
self.assertNotIn(node, cache.evictable_host_leaves)
|
||||
|
||||
def test_cp_host_eviction_uses_physical_freed_slots_for_progress(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
@@ -1268,6 +1448,38 @@ class TestHiRadixCacheCPLoadBack(CustomTestCase):
|
||||
self.assertIs(result.last_host_node, node)
|
||||
self.assertIs(result.last_device_node, root)
|
||||
|
||||
def test_cp_match_prefix_device_valid_host_missing_remains_device_hit(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.device = "cpu"
|
||||
cache.disable = False
|
||||
cache.page_size = 1
|
||||
cache.get_child_key_fn = lambda key: key.token_ids[0]
|
||||
cache.key_match_fn = lambda child_key, key: sum(
|
||||
1 for lhs, rhs in zip(child_key.token_ids, key.token_ids) if lhs == rhs
|
||||
)
|
||||
cache.maybe_bigram_convert = lambda key: (key, None)
|
||||
root = TreeNode()
|
||||
root.key = RadixKey([])
|
||||
root.value = torch.empty((0,), dtype=torch.int64)
|
||||
root.host_len = 0
|
||||
cache.root_node = root
|
||||
node = TreeNode()
|
||||
node.parent = root
|
||||
node.key = RadixKey(list(range(8)))
|
||||
node.value = torch.arange(8, dtype=torch.int64)
|
||||
node.host_value = None
|
||||
node.host_len = 0
|
||||
node.cp_hicache = None
|
||||
root.children[0] = node
|
||||
|
||||
result = cache.match_prefix(MatchPrefixParams(key=RadixKey(list(range(8)))))
|
||||
|
||||
self.assertEqual(result.device_indices.tolist(), list(range(8)))
|
||||
self.assertEqual(result.host_hit_length, 0)
|
||||
self.assertIs(result.last_device_node, node)
|
||||
self.assertIs(result.last_host_node, root)
|
||||
|
||||
def test_cp_match_prefix_does_not_admit_inflight_write_as_host_hit(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
@@ -1311,6 +1523,51 @@ class TestHiRadixCacheCPLoadBack(CustomTestCase):
|
||||
self.assertIs(result.last_device_node, node)
|
||||
self.assertIs(result.last_host_node, root)
|
||||
|
||||
def test_cp_match_prefix_defers_split_when_child_backup_pending(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
cache.device = "cpu"
|
||||
cache.disable = False
|
||||
cache.page_size = 1
|
||||
cache.ongoing_write_through = {}
|
||||
cache.pending_host_backups = {}
|
||||
cache.cache_controller = types.SimpleNamespace(has_draft_hicache=False)
|
||||
cache.get_child_key_fn = lambda key: key.token_ids[0]
|
||||
cache.key_match_fn = lambda child_key, key: sum(
|
||||
1 for lhs, rhs in zip(child_key.token_ids, key.token_ids) if lhs == rhs
|
||||
)
|
||||
cache.maybe_bigram_convert = lambda key: (key, None)
|
||||
root = TreeNode()
|
||||
root.key = RadixKey([])
|
||||
root.value = torch.empty((0,), dtype=torch.int64)
|
||||
root.host_len = 0
|
||||
cache.root_node = root
|
||||
child = TreeNode()
|
||||
child.id = 130
|
||||
child.parent = root
|
||||
child.key = RadixKey(list(range(8)))
|
||||
child.value = torch.arange(8, dtype=torch.int64)
|
||||
child.host_len = 0
|
||||
metadata = CpHiCacheNodeMetadata(
|
||||
logical_len=8,
|
||||
owned_positions=torch.arange(8, dtype=torch.int64),
|
||||
host_indices=torch.arange(50, 58, dtype=torch.int64),
|
||||
page_owners=torch.zeros(8, dtype=torch.int8),
|
||||
page_size=1,
|
||||
)
|
||||
cache.pending_host_backups[child.id] = PendingHiCacheBackup(
|
||||
node=child, metadata=metadata, logical_len=8
|
||||
)
|
||||
root.children[0] = child
|
||||
|
||||
result = cache.match_prefix(MatchPrefixParams(key=RadixKey([0, 1, 2, 9])))
|
||||
|
||||
self.assertIs(result.pending_backup_deferred_node, child)
|
||||
self.assertIs(root.children[0], child)
|
||||
self.assertEqual(child.key.token_ids, list(range(8)))
|
||||
self.assertEqual(result.device_indices.tolist(), [])
|
||||
self.assertIs(result.last_device_node, root)
|
||||
|
||||
def test_cp_match_prefix_shorter_than_page_returns_empty_root_match(self):
|
||||
cache = HiRadixCache.__new__(HiRadixCache)
|
||||
cache._uses_cp_hicache = True
|
||||
|
||||
Reference in New Issue
Block a user