[CI] radix-cache: Build unified radix cache test states through production paths (#45334)
This commit is contained in:
@@ -13,7 +13,6 @@ from sglang.test.test_utils import CustomTestCase
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CacheConfig:
|
||||
# Tree
|
||||
page_size: int = 1
|
||||
components: tuple[ComponentType, ...] = (ComponentType.FULL,)
|
||||
|
||||
@@ -21,12 +20,10 @@ class CacheConfig:
|
||||
num_layers: int = 24
|
||||
full_attention_layer_ids: tuple[int, ...] = (3, 7, 11, 15, 19, 23)
|
||||
|
||||
# SWA
|
||||
sliding_window_size: Optional[int] = None
|
||||
draft_swa_window: int = 0
|
||||
decoder_swa_bounded_replay: bool = False
|
||||
|
||||
# Mamba
|
||||
enable_mamba_extra_buffer: bool = False
|
||||
mamba_cache_size: int = 20
|
||||
mamba_intermediate_size: int = 256
|
||||
@@ -37,7 +34,6 @@ class CacheConfig:
|
||||
mamba_conv_kernel: int = 4
|
||||
enable_int8_mamba_checkpoint: bool = False
|
||||
|
||||
# Model / pool
|
||||
kv_size: int = 256
|
||||
max_num_reqs: int = 10
|
||||
max_context_len: int = 512
|
||||
@@ -193,11 +189,8 @@ CONFIGS: list[CacheConfig] = [
|
||||
def requires(predicate, reason):
|
||||
"""Keep a test only under the configs where ``predicate`` holds.
|
||||
|
||||
Under any other config the test is removed from the bound class, not
|
||||
skipped. ``reason`` documents the requirement and is not read at runtime.
|
||||
|
||||
Stacked requirements are checked top to bottom and stop at the first that
|
||||
fails, so a lower one may assume the ones above it hold.
|
||||
Elsewhere the test is removed, not skipped. Stacked requirements are checked
|
||||
top to bottom, so a lower one may assume the ones above it hold.
|
||||
"""
|
||||
|
||||
def decorate(test):
|
||||
|
||||
@@ -86,9 +86,7 @@ class FakeFullComponent(TreeComponent):
|
||||
|
||||
|
||||
class PagedFullComponent(FullComponent):
|
||||
"""FULL component whose device values are not backed one-to-one by pool
|
||||
rows: a value lists two entries per row, and the component can hand rows
|
||||
of a node back to the allocator while the value stays on device."""
|
||||
"""FULL component whose device values are not backed one-to-one by pool rows."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -99,7 +97,6 @@ class PagedFullComponent(FullComponent):
|
||||
|
||||
|
||||
def component_with_cache(component_type, cache):
|
||||
"""A registry component instance bound to a (mock) cache and its tree_core."""
|
||||
component = object.__new__(COMPONENT_REGISTRY[component_type])
|
||||
component.cache = cache
|
||||
component.tree_core = cache.tree_core
|
||||
|
||||
@@ -52,9 +52,7 @@ from sglang.test.kits.unified_radix_cache.unified_tree_core_inspector import (
|
||||
UnifiedTreeCoreInspector,
|
||||
)
|
||||
|
||||
# The Rust re-run entry point overrides this without changing the process-wide
|
||||
# production backend selection. Every cache built by build_fixture follows it;
|
||||
# tests that construct a Python core directly stay Python-only.
|
||||
# The Rust re-run overrides this; every cache built by build_fixture follows it.
|
||||
TREE_CORE_TEST_BACKEND: Optional[str] = "python"
|
||||
|
||||
|
||||
@@ -63,9 +61,8 @@ def selected_tree_core_test_backend() -> str:
|
||||
|
||||
|
||||
def drop_hicache_atexit_pin(cache):
|
||||
"""init_hicache registers ``cache.shutdown`` with atexit, which pins the
|
||||
fixture's device and host pools until process exit; hundreds of HiCache
|
||||
fixtures in one test process would otherwise accumulate on the GPU."""
|
||||
"""init_hicache registers cache.shutdown with atexit, which pins the fixture's GPU
|
||||
memory until exit."""
|
||||
atexit.unregister(cache.shutdown)
|
||||
|
||||
|
||||
@@ -128,19 +125,17 @@ def build_fixture(
|
||||
):
|
||||
"""Create (tree, allocator, req_to_token_pool) from a CacheConfig.
|
||||
|
||||
``tree_page_size`` stands in for DCP, which widens the tree page past the
|
||||
``page_size`` the rest of the config still sees. It only reaches values
|
||||
derived from the tree page: the allocator keeps ``cfg.page_size``, whereas
|
||||
DCP sets the two equal, so do not read insert or match behaviour off it.
|
||||
``tree_page_size`` stands in for DCP but only reaches values derived from the
|
||||
tree page; the allocator keeps ``cfg.page_size``, so do not read insert or
|
||||
match behaviour off it.
|
||||
"""
|
||||
server_args = ServerArgs(
|
||||
model_path="dummy",
|
||||
page_size=cfg.page_size,
|
||||
enable_int8_mamba_checkpoint=cfg.enable_int8_mamba_checkpoint,
|
||||
)
|
||||
# The mamba component reads mamba_cache_chunk_size, whose property otherwise
|
||||
# loads the HF config for self.model_path — impossible for the dummy model.
|
||||
# Mirror the property's default for a dummy HF config: FLA_CHUNK_SIZE.
|
||||
# The dummy HF config cannot supply mamba_cache_chunk_size, so preset
|
||||
# FLA_CHUNK_SIZE.
|
||||
server_args._mamba_cache_chunk_size = (
|
||||
max(FLA_CHUNK_SIZE, cfg.page_size)
|
||||
if mamba_cache_chunk_size is None
|
||||
|
||||
@@ -33,7 +33,6 @@ class ResumableInsertWalkSWAFixtures(UnifiedRadixCacheFixtures):
|
||||
seq = list(range(1, self.cfg.sliding_window_size + 1))
|
||||
key = RadixKey(array("q", seq))
|
||||
evicted = self._alloc(allocator, len(seq))
|
||||
# Window eviction already released the peers below the floor.
|
||||
allocator.free_swa(evicted)
|
||||
cache.insert(
|
||||
InsertParams(
|
||||
@@ -229,7 +228,6 @@ class SegmentLockFuzzFixtures(UnifiedRadixCacheFixtures):
|
||||
raise
|
||||
cache.sanity_check()
|
||||
|
||||
# Drain remaining locks; the tree must come back exactly balanced.
|
||||
for node_id, receipt, released in held:
|
||||
cache.dec_lock_ref(node_id, receipt.to_dec_params(), skip_swa=released)
|
||||
cache.sanity_check()
|
||||
|
||||
@@ -81,11 +81,10 @@ class UnifiedRadixCacheKVEventsFixtures:
|
||||
return leaf
|
||||
|
||||
def _init_hicache(self, cache, *, write_policy: str = "write_through"):
|
||||
# Production config: kernel IO backend + page_first layout with
|
||||
# PINNED host pools (kernel GPU DMA requires them). Pools track their
|
||||
# cudaHostRegister'd pointers and unregister on destroy()/GC, so the
|
||||
# many fixtures sharing this process cannot collide on recycled
|
||||
# address ranges (rc=712).
|
||||
# Production config: kernel IO backend + page_first layout.
|
||||
# Pinned host pools (kernel GPU DMA requires them) track their registered
|
||||
# pointers and unregister on destroy/GC, so fixtures sharing a process
|
||||
# cannot collide on recycled address ranges.
|
||||
server_args = ServerArgs(
|
||||
model_path="dummy",
|
||||
page_size=self.cfg.page_size,
|
||||
|
||||
@@ -11,11 +11,14 @@ from sglang.srt.managers.schedule_batch import (
|
||||
Req,
|
||||
)
|
||||
from sglang.srt.mem_cache.base_prefix_cache import (
|
||||
InitLoadBackParams,
|
||||
InsertParams,
|
||||
MatchPrefixParams,
|
||||
)
|
||||
from sglang.srt.mem_cache.radix_cache import RadixKey
|
||||
from sglang.srt.mem_cache.unified_cache.components.base import (
|
||||
ComponentType,
|
||||
EvictLayer,
|
||||
)
|
||||
from sglang.srt.sampling.sampling_params import SamplingParams
|
||||
from sglang.srt.server_args import (
|
||||
@@ -73,9 +76,8 @@ class UnifiedRadixCacheFixtures:
|
||||
if not (self.cfg.has_swa and self.cfg.page_size > 1):
|
||||
return allocator.alloc(need_size)
|
||||
|
||||
# SWATokenToKVPoolAllocator.alloc() asserts page_size == 1, and
|
||||
# alloc_extend() requires batch tensors unsuitable for unit tests.
|
||||
# Replicate alloc_extend's core logic here.
|
||||
# The SWA allocator's alloc asserts page_size == 1 and alloc_extend needs
|
||||
# batch tensors, so replicate alloc_extend's core logic.
|
||||
ps = self.cfg.page_size
|
||||
aligned = ((need_size + ps - 1) // ps) * ps
|
||||
if aligned > allocator.full_attn_allocator.available_size():
|
||||
@@ -195,9 +197,8 @@ class UnifiedRadixCacheFixtures:
|
||||
if storage_backend == "file":
|
||||
from sglang.srt.runtime_context import get_parallel
|
||||
|
||||
# The file-backend storage config records TP/PP rank/size. These unit
|
||||
# fixtures run without initializing distributed parallel state, so
|
||||
# force the local single-rank topology the fixture represents.
|
||||
# Distributed state is not initialized in unit fixtures; force the
|
||||
# local single-rank topology.
|
||||
parallel_override = get_parallel().override(
|
||||
tp_rank=0, tp_size=1, pp_rank=0, pp_size=1
|
||||
)
|
||||
@@ -216,7 +217,6 @@ class UnifiedRadixCacheFixtures:
|
||||
extra.update(storage_extra)
|
||||
storage_extra_config = json.dumps(extra) if extra else None
|
||||
|
||||
# See _init_hicache: production kernel IO backend, pinned pools.
|
||||
server_args = ServerArgs(
|
||||
model_path="dummy",
|
||||
page_size=self.cfg.page_size,
|
||||
@@ -228,7 +228,6 @@ class UnifiedRadixCacheFixtures:
|
||||
hicache_host_memory_mode=host_memory_mode,
|
||||
context_length=context_length,
|
||||
)
|
||||
# See build_fixture for why _mamba_cache_chunk_size is preset.
|
||||
server_args._mamba_cache_chunk_size = max(FLA_CHUNK_SIZE, self.cfg.page_size)
|
||||
set_global_server_args_for_scheduler(server_args)
|
||||
cache.init_hicache(server_args, cache.cache_init_params)
|
||||
@@ -237,15 +236,12 @@ class UnifiedRadixCacheFixtures:
|
||||
cache.write_through_threshold = 1 << 30
|
||||
cache.load_back_threshold = 0
|
||||
if storage_backend is not None:
|
||||
# Unit fixtures size host/device pools equally, which makes the
|
||||
# production prefetch capacity limit (host - device) zero. Keep the
|
||||
# L3 tests focused on storage round trips by allowing one fixture
|
||||
# worth of prefetch tokens.
|
||||
# Fixtures size host and device pools equally, which zeroes the
|
||||
# prefetch capacity limit; allow one fixture of prefetch tokens.
|
||||
cache.cache_controller.prefetch_capacity_limit = max(
|
||||
cache.cache_controller.prefetch_capacity_limit,
|
||||
cache.cache_controller.mem_pool_host.size,
|
||||
)
|
||||
# Background prefetch/backup threads are daemon; stop them per-test.
|
||||
self.addCleanup(cache.cache_controller._stop_storage_threads)
|
||||
|
||||
def _build_hicache_fixture(self, tree_core_backend: Optional[str] = None):
|
||||
@@ -307,10 +303,9 @@ class UnifiedRadixCacheFixtures:
|
||||
)
|
||||
|
||||
def _build_chain_pages(self, cache, allocator, req_to_token_pool, num_pages):
|
||||
"""Insert an incremental chain of single-page extensions.
|
||||
"""Insert a chain of single-page extensions; return it root-to-leaf.
|
||||
|
||||
Returns the chain root-to-leaf. Length may differ from num_pages
|
||||
when the radix tree merges or splits nodes.
|
||||
The length may differ from num_pages when the tree merges or splits nodes.
|
||||
"""
|
||||
seq: list[int] = []
|
||||
for i in range(num_pages):
|
||||
@@ -340,6 +335,31 @@ class UnifiedRadixCacheFixtures:
|
||||
ack.finish_event.synchronize()
|
||||
cache.loading_check()
|
||||
|
||||
def _evict_device_component(self, cache, node, component_type):
|
||||
"""Evict one node's device value of one component, as the eviction
|
||||
walk does for the victim it picks."""
|
||||
result = cache.tree_core.evict_component(
|
||||
node, component_type, EvictLayer.DEVICE
|
||||
)
|
||||
cache._free_values(result.device_frees, result.host_frees)
|
||||
|
||||
def _load_back_through_admission(self, cache, req_to_token_pool, tokens):
|
||||
"""Match ``tokens`` and start the scheduler's load-back of the host
|
||||
hit. The load-back holds its path lock until it is released."""
|
||||
req = self._make_req(req_to_token_pool)
|
||||
match = cache.match_prefix(
|
||||
MatchPrefixParams(key=RadixKey(array("q", tokens)), req=req)
|
||||
)
|
||||
self._apply_match_to_req(req, match)
|
||||
cache.init_load_back(
|
||||
InitLoadBackParams(
|
||||
best_match_node=req.best_match_node,
|
||||
host_hit_length=req.host_hit_length,
|
||||
req=req,
|
||||
)
|
||||
)
|
||||
return req
|
||||
|
||||
def _match_tokens_for_chain(self, cache, chain):
|
||||
tokens: list[int] = []
|
||||
for node in chain:
|
||||
@@ -363,9 +383,7 @@ class UnifiedRadixCacheFixtures:
|
||||
)
|
||||
|
||||
def _swa_finalize_setup(self):
|
||||
"""Build a SWA chain long enough to fill at least the window
|
||||
plus one extra page, and host-back every node so we can flip
|
||||
SWA tombstones at will."""
|
||||
"""Build a SWA chain longer than the window, host-backed at every node."""
|
||||
ps = self.cfg.page_size
|
||||
sw = self.cfg.sliding_window_size
|
||||
window_pages = (sw + ps - 1) // ps
|
||||
|
||||
@@ -134,10 +134,9 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
def _run_prefetch_to_completion(self, cache, req_id, timeout: float = 10.0):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
# Host memory is reserved (and IO started) by the scheduler-thread
|
||||
# drain once the L3 hit count is known, and the buffer-mode fill
|
||||
# commits at its H2D ack in loading_check — pump the full event
|
||||
# round like the real scheduler loop does.
|
||||
# Pump the full event round like the scheduler loop: host reservation
|
||||
# happens at the scheduler-thread drain, and the buffer-mode fill
|
||||
# commits at its H2D ack.
|
||||
cache.check_hicache_events()
|
||||
if cache.check_prefetch_progress(req_id):
|
||||
# Buffer mode parks a completed fetch as a staged prefetch;
|
||||
@@ -175,7 +174,7 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
f = cache.buffer_pipeline.staged_prefetches[req_id]
|
||||
if prefix_len is None:
|
||||
prefix_len = f.matched_len
|
||||
req = mock.Mock()
|
||||
req = self._make_req(cache.req_to_token_pool)
|
||||
req.rid = req_id.rid
|
||||
req.cache_request_handle = req_id
|
||||
req.extra_key = extra_key
|
||||
@@ -391,9 +390,8 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
cons.enable_storage_metrics = True
|
||||
cons.storage_metrics_collector = mock.Mock()
|
||||
|
||||
# Masked state: nodes born with live FULL under SWA tombstones (a
|
||||
# sibling insert whose SWA ring had slid past the span). live_tail is
|
||||
# how far short of the span end the ring stopped.
|
||||
# Nodes born with live FULL under SWA tombstones; live_tail is how far short
|
||||
# of the span end the ring stopped.
|
||||
live_from = len(seq) - live_tail
|
||||
value = self._alloc(cons_alloc, len(seq))
|
||||
cons.insert(
|
||||
@@ -407,8 +405,7 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
self.assertEqual(masked.device_prefix_len, 0, "unified match not masked")
|
||||
self.assertEqual(masked.full_kv_hit_length, len(seq), "live FULL not resident")
|
||||
masked_full = value[: len(seq)].clone()
|
||||
# Model the real SWA-eviction path: tombstoned positions have their
|
||||
# allocator mapping cleared, while the live trailing portion remains.
|
||||
# Tombstoned positions have their allocator mapping cleared; the tail stays.
|
||||
cons_alloc.free_swa(masked_full[:live_from])
|
||||
self._fill_full_kv(cons_alloc, masked_full, marker=7)
|
||||
masked_kv = self._snapshot_full_kv(cons_alloc, masked_full)
|
||||
@@ -421,7 +418,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
cons.prefetch_from_storage(
|
||||
req_id, cons.root_node_handle(), array("q", seq), None, None
|
||||
)
|
||||
# A parked SWA-only retry has no FULL suffix, but must re-pin its prefix.
|
||||
with mock.patch.object(cons.swa_kv_pool_host, "alloc", return_value=None):
|
||||
self._pump_hicache_until(
|
||||
cons,
|
||||
@@ -500,7 +496,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
after = match_prefix(cons, seq)
|
||||
self.assertEqual(after.full_kv_hit_length, len(seq))
|
||||
self.assertEqual(after.device_prefix_len, len(seq))
|
||||
# Only the fetched SWA tail made the resident FULL reusable.
|
||||
self.assertEqual((req.storage_hit_start, req.storage_hit_length), (0, len(seq)))
|
||||
cons.storage_metrics_collector.log_storage_prefetch_unfulfilled_tokens.assert_not_called()
|
||||
cons.sanity_check()
|
||||
@@ -552,7 +547,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
)
|
||||
self.assertGreater(aux_max, len(seq) - len(head))
|
||||
|
||||
# The surfaced host hit is the splice-able tail, not the full span.
|
||||
req = SimpleNamespace(
|
||||
rid=req_id.rid,
|
||||
cache_request_handle=req_id,
|
||||
@@ -585,7 +579,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
self.assertTrue(torch.equal(hk, head_k))
|
||||
self.assertTrue(torch.equal(hv, head_v))
|
||||
|
||||
# The whole bounce (trimmed head included) frees at the ack.
|
||||
self.assertEqual(self._host_avail_sizes(cons), avail0)
|
||||
self.assertEqual(cons.cache_controller.prefetch_tokens_occupied, 0)
|
||||
cons.sanity_check()
|
||||
@@ -596,7 +589,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
|
||||
cc = cache.cache_controller
|
||||
|
||||
# Fake _reduce_prefetch_ack to drop SWA pool_hits when drop_swa is True.
|
||||
def fake_reduce(ack):
|
||||
if drop_swa and ack.pool_hits is not None:
|
||||
if PoolName.SWA.value in ack.pool_hits:
|
||||
@@ -608,7 +600,6 @@ class UnifiedRadixCacheStorageFixtures(UnifiedRadixCacheFixtures):
|
||||
p_reduce.start()
|
||||
self.addCleanup(p_reduce.stop)
|
||||
|
||||
# Make all_reduce no-op. The real all_reduce fails in unit tests with tp_world_size=2.
|
||||
p_dist = mock.patch.object(dist, "all_reduce", return_value=None)
|
||||
p_dist.start()
|
||||
self.addCleanup(p_dist.stop)
|
||||
@@ -774,25 +765,26 @@ class UnifiedRadixCacheStorageAttachBackfillFixtures:
|
||||
return hashes
|
||||
|
||||
|
||||
class AnchorLockOutcomePolicyFixtures:
|
||||
def _make_pipeline(self, cache, cap_tokens=10_000):
|
||||
from sglang.srt.mem_cache.buffer_mode.pipeline import BufferModePipeline
|
||||
class AnchorLockOutcomePolicyFixtures(UnifiedRadixCacheStorageFixtures):
|
||||
def _insert_tokens(self, cache, tokens):
|
||||
value = cache.token_to_kv_pool_allocator.alloc(len(tokens))
|
||||
self.assertIsNotNone(value)
|
||||
cache.insert(InsertParams(key=RadixKey(array("q", tokens)), value=value))
|
||||
|
||||
pipeline = BufferModePipeline.__new__(BufferModePipeline)
|
||||
pipeline.anchor_locks = {}
|
||||
pipeline.anchor_locked_tokens_ = 0
|
||||
pipeline.anchor_lock_cap_tokens = cap_tokens
|
||||
pipeline._anchor_lock_cap_skips = 0
|
||||
pipeline._prefetch_prefix_ctx = {self._REQ: (list(self._PREFIX), None, None)}
|
||||
pipeline._cache = cache
|
||||
return pipeline
|
||||
|
||||
def _make_cache(self, live_match_len, pinned_len=None):
|
||||
cache = mock.MagicMock()
|
||||
cache.tree_core.is_eagle = False
|
||||
cache.tree_core.match_full_device_prefix.return_value = (
|
||||
live_match_len,
|
||||
99,
|
||||
live_match_len if pinned_len is None else pinned_len,
|
||||
def _make_buffer_cache(
|
||||
self, resident=(), cap_tokens=None, prefix=(), storage_dir=None
|
||||
):
|
||||
"""A buffer-mode cache holding ``resident`` on device, with the
|
||||
request's matched prefix recorded as prefetch enqueue records it."""
|
||||
cache, _, _ = self._build_buffer_hicache_fixture(
|
||||
storage_dir or self._make_storage_dir()
|
||||
)
|
||||
return cache
|
||||
if resident:
|
||||
self._insert_tokens(cache, list(resident))
|
||||
pipeline = cache.buffer_pipeline
|
||||
if cap_tokens is not None:
|
||||
pipeline.anchor_lock_cap_tokens = cap_tokens
|
||||
pipeline.set_prefix_ctx(
|
||||
self._REQ, self._PREFIX if prefix == () else (prefix or [])
|
||||
)
|
||||
return cache, pipeline
|
||||
|
||||
@@ -27,8 +27,6 @@ if TYPE_CHECKING:
|
||||
class UnifiedTreeCoreInspectionInterface(UnifiedTreeCoreInterface):
|
||||
"""Test-only inspection and control contract for shared backend tests."""
|
||||
|
||||
# ==== Read-only inspection ====
|
||||
|
||||
@abstractmethod
|
||||
def contains_node(self, node_id: NodeId) -> bool: ...
|
||||
|
||||
@@ -125,8 +123,6 @@ class UnifiedTreeCoreInspectionInterface(UnifiedTreeCoreInterface):
|
||||
"""Protected token count for one component (0 if the component is absent)."""
|
||||
...
|
||||
|
||||
# ==== White-box state controls ====
|
||||
|
||||
@abstractmethod
|
||||
def set_node_hash_values(
|
||||
self, node_id: NodeId, hash_values: Optional[list[str]]
|
||||
@@ -194,8 +190,6 @@ class UnifiedTreeCoreInspectionInterface(UnifiedTreeCoreInterface):
|
||||
"""Refresh duplicate-host tracking for the node."""
|
||||
...
|
||||
|
||||
# ==== Targeted white-box operations ====
|
||||
|
||||
@abstractmethod
|
||||
def advance_insert_walk_once(self) -> None:
|
||||
"""Advance one suspended insert walk step without flushing its actions."""
|
||||
|
||||
@@ -30,7 +30,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
return node_id in self._node_arena
|
||||
|
||||
def get_parent_node_id(self, node_id: NodeId) -> Optional[NodeId]:
|
||||
"""The parent node id, or None for the root."""
|
||||
parent = self.node_by_id(node_id).parent
|
||||
return None if parent is None else parent.id
|
||||
|
||||
@@ -45,13 +44,11 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
return len(key)
|
||||
|
||||
def get_node_token_ids(self, node_id: NodeId) -> list[int]:
|
||||
"""The raw token ids spanned by the node's radix key."""
|
||||
key = self.node_by_id(node_id).key
|
||||
assert key is not None
|
||||
return list(key.raw_token_ids())
|
||||
|
||||
def is_node_key_bigram(self, node_id: NodeId) -> bool:
|
||||
"""Whether the node's radix key uses bigram atoms."""
|
||||
key = self.node_by_id(node_id).key
|
||||
assert key is not None
|
||||
return key.is_bigram
|
||||
@@ -59,19 +56,16 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
def get_component_host_value(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> Optional[torch.Tensor]:
|
||||
"""The component's host value on the node, or None if absent."""
|
||||
return self.node_by_id(node_id).component_data[component_type].host_value
|
||||
|
||||
def get_component_device_lock_ref(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> int:
|
||||
"""The component's device lock count on the node."""
|
||||
return self.node_by_id(node_id).component_data[component_type].lock_ref
|
||||
|
||||
def get_component_host_lock_ref(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> int:
|
||||
"""The component's host lock count on the node."""
|
||||
return self.node_by_id(node_id).component_data[component_type].host_lock_ref
|
||||
|
||||
def get_node_hit_count(self, node_id: NodeId) -> int:
|
||||
@@ -83,20 +77,17 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
return self.node_by_id(node_id).write_through_pending_id
|
||||
|
||||
def is_external_cache_stored(self, node_id: NodeId) -> bool:
|
||||
"""Whether the node is known to be stored in the external cache."""
|
||||
return self.node_by_id(node_id).external_cache_stored
|
||||
|
||||
def is_node_in_device_lru(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> bool:
|
||||
"""Whether the node belongs to the component's device LRU."""
|
||||
lru = self.lru_lists.get(component_type)
|
||||
return lru is not None and lru.in_list(self.node_by_id(node_id))
|
||||
|
||||
def is_node_in_host_lru(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> bool:
|
||||
"""Whether the node belongs to the component's host LRU."""
|
||||
lru = self.host_lru_lists.get(component_type)
|
||||
return lru is not None and lru.in_list(self.node_by_id(node_id))
|
||||
|
||||
@@ -114,7 +105,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
def get_component_device_lru_node_ids(
|
||||
self, component_type: ComponentType
|
||||
) -> list[NodeId]:
|
||||
"""The component's device LRU members from most to least recent."""
|
||||
lru = self.lru_lists.get(component_type)
|
||||
return [] if lru is None else self._lru_node_ids(lru)
|
||||
|
||||
@@ -129,7 +119,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
return node is not None and node in self.evictable_host_leaves
|
||||
|
||||
def is_device_leaf(self, node_id: NodeId) -> bool:
|
||||
"""Whether the node has no device-resident descendants."""
|
||||
return self._is_device_leaf(self.node_by_id(node_id))
|
||||
|
||||
def get_all_node_ids(self) -> list[NodeId]:
|
||||
@@ -137,13 +126,11 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
return [node.id for node in self._collect_all_nodes()]
|
||||
|
||||
def component_protected_size(self, component_type: ComponentType) -> int:
|
||||
"""Protected token count for one component (0 if the component is absent)."""
|
||||
return self.component_protected_size_.get(component_type, 0)
|
||||
|
||||
def set_node_hash_values(
|
||||
self, node_id: NodeId, hash_values: Optional[list[str]]
|
||||
) -> None:
|
||||
"""Replace the node's page-hash field."""
|
||||
self.node_by_id(node_id).hash_value = hash_values
|
||||
|
||||
def set_component_device_value_raw(
|
||||
@@ -152,7 +139,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
component_type: ComponentType,
|
||||
value: Optional[torch.Tensor],
|
||||
) -> None:
|
||||
"""Replace the device-value field without updating tree bookkeeping."""
|
||||
self.node_by_id(node_id).component_data[component_type].value = value
|
||||
|
||||
def set_component_host_value_raw(
|
||||
@@ -161,48 +147,40 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
component_type: ComponentType,
|
||||
value: Optional[torch.Tensor],
|
||||
) -> None:
|
||||
"""Replace the host-value field without updating tree bookkeeping."""
|
||||
self.node_by_id(node_id).component_data[component_type].host_value = value
|
||||
|
||||
def set_component_device_lock_ref(
|
||||
self, node_id: NodeId, component_type: ComponentType, lock_ref: int
|
||||
) -> None:
|
||||
"""Replace the component's device lock count."""
|
||||
assert lock_ref >= 0
|
||||
self.node_by_id(node_id).component_data[component_type].lock_ref = lock_ref
|
||||
|
||||
def remove_node_from_device_lru(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> None:
|
||||
"""Remove the node from the component's device LRU."""
|
||||
self.lru_lists[component_type].remove_node(self.node_by_id(node_id))
|
||||
|
||||
def insert_node_into_host_lru(
|
||||
self, node_id: NodeId, component_type: ComponentType
|
||||
) -> None:
|
||||
"""Insert the node as the component's most-recent host entry."""
|
||||
self.host_lru_lists[component_type].insert_mru(self.node_by_id(node_id))
|
||||
|
||||
def set_component_evictable_size(
|
||||
self, component_type: ComponentType, value: int
|
||||
) -> None:
|
||||
"""Replace the component's evictable device-token count."""
|
||||
assert value >= 0
|
||||
self.component_evictable_size_[component_type] = value
|
||||
|
||||
def set_component_protected_size(
|
||||
self, component_type: ComponentType, value: int
|
||||
) -> None:
|
||||
"""Replace the component's protected device-token count."""
|
||||
assert value >= 0
|
||||
self.component_protected_size_[component_type] = value
|
||||
|
||||
def update_duplicate_tracking(self, node_id: NodeId) -> None:
|
||||
"""Refresh duplicate-host tracking for the node."""
|
||||
self._update_duplicate_tracking(self.node_by_id(node_id))
|
||||
|
||||
def advance_insert_walk_once(self) -> None:
|
||||
"""Advance one suspended insert walk step without flushing its actions."""
|
||||
state = self._ongoing_insert_walk_state
|
||||
if state is None:
|
||||
raise RuntimeError("no in-flight insert")
|
||||
@@ -216,7 +194,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
component_type: ComponentType,
|
||||
target: EvictLayer,
|
||||
) -> BaseEvictionResult:
|
||||
"""Evict one component layer from a node and detach its LRU entry."""
|
||||
result = BaseEvictionResult()
|
||||
self._evict_component_and_detach_lru(
|
||||
self.node_by_id(node_id),
|
||||
@@ -235,7 +212,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
component_type: ComponentType,
|
||||
target: EvictLayer,
|
||||
) -> None:
|
||||
"""Validate the locks for a component-triggered cascade eviction."""
|
||||
node = self.node_by_id(node_id)
|
||||
trigger = self.components_by_type[component_type]
|
||||
is_leaf = self._is_cascade_evict_leaf(node, target)
|
||||
@@ -243,7 +219,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
self._should_cascade_evict_component(node, trigger, comp, target, is_leaf)
|
||||
|
||||
def cleanup_tombstone_ancestors(self, node_id: NodeId) -> BaseEvictionResult:
|
||||
"""Delete childless tombstone ancestors until a live or locked node is reached."""
|
||||
result = BaseEvictionResult()
|
||||
self._iteratively_delete_tombstone_ancestors(
|
||||
self.node_by_id(node_id),
|
||||
@@ -262,7 +237,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
value_chunks: list[torch.Tensor],
|
||||
best_value_len: int,
|
||||
) -> MatchResult:
|
||||
"""Run one component's match finalizer with NodeId boundaries."""
|
||||
node_result = result._replace(
|
||||
last_device_node=self.node_by_id(result.last_device_node),
|
||||
last_host_node=self.node_by_id(result.last_host_node),
|
||||
@@ -285,7 +259,6 @@ class UnifiedTreeCoreInspector(UnifiedTreeCore, UnifiedTreeCoreInspectionInterfa
|
||||
def build_backup_node_ids(
|
||||
self, node_id: NodeId, write_back: bool = False
|
||||
) -> list[NodeId]:
|
||||
"""Build the ordered node list for a device-to-host backup."""
|
||||
return self._build_backup_kv_action(
|
||||
self.node_by_id(node_id), write_back
|
||||
).node_ids
|
||||
|
||||
Reference in New Issue
Block a user