From dc62c3effd20f922fce9d54f976cf1d152517aa7 Mon Sep 17 00:00:00 2001 From: metamergebot Date: Tue, 20 Oct 2026 13:23:59 -0700 Subject: [PATCH] [Feat] radix-tree: Report each path node's child count in collect_path_info (#45241) Co-authored-by: metamergebot Co-authored-by: Jialin --- .../unified_cache/unified_tree_core.py | 1 + .../unified_tree_core_interface.py | 1 + rust/sglang-radix-tree/src/python_bindings.rs | 3 ++- .../src/tests/unified_tree_core.rs | 21 ++++++++++++++--- .../src/unified_tree_core.rs | 5 +++- .../test_rust_tree_core_integration.py | 23 +++++++++++++------ 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py index 0714b191ae..15307a0d19 100644 --- a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py +++ b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core.py @@ -690,6 +690,7 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface): start, end, None if value is None else len(value), + len(node.children), ) ) start = end diff --git a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core_interface.py b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core_interface.py index a2163b1c92..ed83ca91ca 100644 --- a/python/sglang/srt/mem_cache/unified_cache/unified_tree_core_interface.py +++ b/python/sglang/srt/mem_cache/unified_cache/unified_tree_core_interface.py @@ -116,6 +116,7 @@ class TreePathNodeInfo(msgspec.Struct, frozen=True): path_end: int # Logical FULL device indices; None when the value is absent. full_value_len: Optional[int] + num_children: int class InsertStepResult(msgspec.Struct, frozen=True): diff --git a/rust/sglang-radix-tree/src/python_bindings.rs b/rust/sglang-radix-tree/src/python_bindings.rs index 879dc7ee6c..4f93548460 100644 --- a/rust/sglang-radix-tree/src/python_bindings.rs +++ b/rust/sglang-radix-tree/src/python_bindings.rs @@ -1013,7 +1013,7 @@ impl HostEvictionResultBinding { } } -type TreePathNodeInfoTuple = (NodeId, NodeId, usize, usize, Option); +type TreePathNodeInfoTuple = (NodeId, NodeId, usize, usize, Option, usize); /// The generic UnifiedTreeCore adapter the per-key-type pyclasses delegate to; /// the Mutex makes the Send-only core satisfy pyclass's Sync bound. @@ -1419,6 +1419,7 @@ impl TreeCoreBinding { node.path_start, node.path_end, node.full_value_len, + node.num_children, ) }) .collect() diff --git a/rust/sglang-radix-tree/src/tests/unified_tree_core.rs b/rust/sglang-radix-tree/src/tests/unified_tree_core.rs index 600809dd37..6b11485fb9 100644 --- a/rust/sglang-radix-tree/src/tests/unified_tree_core.rs +++ b/rust/sglang-radix-tree/src/tests/unified_tree_core.rs @@ -161,9 +161,9 @@ fn assert_path_info() { let root_id = core.arena.node(root).id; let key = K::key_from(std::borrow::Cow::Owned(vec![1, 2, 3, 4, 5, 6, 7])).into_owned(); let total = key.atom_len(); - let (head_key, tail_key) = key.split_at(2); - let head = add_leaf(&mut core, root, head_key); - let tail = add_leaf(&mut core, head, tail_key); + let tail = add_leaf(&mut core, root, key); + let (head, action) = core.split_node_(tail, 2); + assert!(action.is_none()); let other_key = K::key_from(std::borrow::Cow::Owned(vec![20, 21, 22])).into_owned(); let other_len = other_key.atom_len(); let other = add_leaf(&mut core, root, other_key); @@ -177,6 +177,7 @@ fn assert_path_info() { path_start: 0, path_end: 2, full_value_len: Some(2), + num_children: 1, }, TreePathNodeInfo { node_id: tail_id, @@ -184,6 +185,7 @@ fn assert_path_info() { path_start: 2, path_end: total, full_value_len: Some(total - 2), + num_children: 0, }, ]; let snapshot = core @@ -200,12 +202,25 @@ fn assert_path_info() { path_start: 0, path_end: other_len, full_value_len: Some(other_len), + num_children: 0, }], full_path.clone(), ] ); assert!(core.collect_path_info(&[]).unwrap().is_empty()); + let branch_key = K::key_from(std::borrow::Cow::Owned(vec![40, 41, 42])).into_owned(); + let branch = add_leaf(&mut core, head, branch_key); + let branch_id = core.arena.node(branch).id; + let branched = core.collect_path_info(&[tail_id, branch_id]).unwrap(); + for path in &branched { + assert_eq!(path[0].num_children, 2); + assert_eq!(path[1].num_children, 0); + } + assert_eq!(snapshot[0], full_path); // Child counts are snapshots too. + core.evict_device_leaf(branch_id, false).unwrap(); + assert_eq!(core.collect_path_info(&[tail_id]).unwrap()[0], full_path); + core.commit_backup(head_id, Tensor::from_slice(&[30i64, 31]), HashMap::new()) .unwrap(); core.commit_backup( diff --git a/rust/sglang-radix-tree/src/unified_tree_core.rs b/rust/sglang-radix-tree/src/unified_tree_core.rs index e6d0777962..2729528968 100644 --- a/rust/sglang-radix-tree/src/unified_tree_core.rs +++ b/rust/sglang-radix-tree/src/unified_tree_core.rs @@ -53,7 +53,7 @@ fn next_coexist_reclaim_digest(current: i64, node_id: NodeId, component_idx: usi // ---- interface types ---- -/// A node's position and FULL value length on a root-to-node path. +/// A node's position, topology, and FULL value length on a root-to-node path. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TreePathNodeInfo { pub node_id: NodeId, @@ -63,6 +63,8 @@ pub struct TreePathNodeInfo { pub path_end: usize, /// Logical FULL device indices; None when absent. pub full_value_len: Option, + /// Number of immediate children at the time of the snapshot. + pub num_children: usize, } /// Receipt returned by `inc_lock_ref` and replayed by `dec_lock_ref`. @@ -742,6 +744,7 @@ impl UnifiedTreeCore { full_value_len: node .has_device_value(FULL) .then(|| node.device_value_len(FULL)), + num_children: node.children.len(), }); current = parent; } diff --git a/test/registered/unit/mem_cache/test_rust_tree_core_integration.py b/test/registered/unit/mem_cache/test_rust_tree_core_integration.py index b60864a5aa..512915e9e7 100644 --- a/test/registered/unit/mem_cache/test_rust_tree_core_integration.py +++ b/test/registered/unit/mem_cache/test_rust_tree_core_integration.py @@ -1054,23 +1054,32 @@ def test_path_order_namespaces_and_detached_snapshots(backend, is_eagle): other = insert(tokens, "second") before = core.collect_path_info((other, root, leaf, other)) assert before == ( - (TreePathNodeInfo(other, root, 0, length, length),), + (TreePathNodeInfo(other, root, 0, length, length, 0),), (), - (TreePathNodeInfo(leaf, root, 0, length, length),), - (TreePathNodeInfo(other, root, 0, length, length),), + (TreePathNodeInfo(leaf, root, 0, length, length, 0),), + (TreePathNodeInfo(other, root, 0, length, length, 0),), ) with pytest.raises(AttributeError): before[0][0].path_end = 99 + core.match_prefix( + MatchPrefixParams( + key=RadixKey(array("q", tokens[:3]), extra_key="first", is_bigram=is_eagle) + ) + ) + chain = core.collect_path_info([leaf])[0] + assert [node.num_children for node in chain] == [1, 0] + insert([1, 2, 3, 7, 8, 9], "first") path = core.collect_path_info([leaf])[0] split = 3 - int(is_eagle) assert len(path) == 2 - assert path[0] == TreePathNodeInfo(path[0].node_id, root, 0, split, split) + assert path[0] == TreePathNodeInfo(path[0].node_id, root, 0, split, split, 2) assert path[1] == TreePathNodeInfo( - leaf, path[0].node_id, split, length, length - split + leaf, path[0].node_id, split, length, length - split, 0 ) - assert before[2] == (TreePathNodeInfo(leaf, root, 0, length, length),) + assert [node.num_children for node in chain] == [1, 0] + assert before[2] == (TreePathNodeInfo(leaf, root, 0, length, length, 0),) core.reset() with pytest.raises(KeyError): core.collect_path_info([leaf]) @@ -1091,7 +1100,7 @@ def test_host_only_and_root_adjustments_are_noops(backend, is_eagle): root, key, torch.arange(100, 100 + len(key)), ["hash"] * len(key) ).inserted_host_node assert core.collect_path_info([host]) == ( - (TreePathNodeInfo(host, root, 0, len(key), None),), + (TreePathNodeInfo(host, root, 0, len(key), None, 0),), ) for node in (root, host): for delta in (-10, 0, 10):