feat: session radix cache (#27058)
Signed-off-by: Ishan Dhanani <ishandhanani@gmail.com>
This commit is contained in:
@@ -123,6 +123,7 @@ from sglang.srt.managers.io_struct import (
|
||||
LoadLoRAAdapterReqInput,
|
||||
LoadLoRAAdapterReqOutput,
|
||||
OpenSessionReqInput,
|
||||
OpenSessionReqOutput,
|
||||
PauseGenerationReqInput,
|
||||
ProfileReq,
|
||||
ReleaseMemoryOccupationReqInput,
|
||||
@@ -583,7 +584,7 @@ class Scheduler(
|
||||
from sglang.srt.hardware_backend.npu.utils import init_zbal
|
||||
|
||||
if self.ps.pp_size > 1:
|
||||
logger.error(f"only zbal mix mode support pp_size > 1!")
|
||||
logger.error("only zbal mix mode support pp_size > 1!")
|
||||
init_zbal(
|
||||
self.ps.tp_size, self.ps.gpu_id, self.ps.tp_rank
|
||||
) # only switch allocator if is mix mode
|
||||
@@ -2003,9 +2004,39 @@ class Scheduler(
|
||||
session_id = (
|
||||
recv_req.session_params.id if recv_req.session_params is not None else None
|
||||
)
|
||||
# Radix-native session: session_id is just a tag; KV bulk-freed on close.
|
||||
radix_native_session = (
|
||||
session_id is not None and self.server_args.enable_session_radix_cache
|
||||
)
|
||||
if radix_native_session:
|
||||
sp = recv_req.session_params
|
||||
if (
|
||||
sp.rid is not None
|
||||
or sp.offset is not None
|
||||
or sp.replace is not None
|
||||
or sp.drop_previous_output is not None
|
||||
):
|
||||
error_msg = (
|
||||
"Invalid request: radix-native sessions do not support "
|
||||
"session_params rid/offset/replace/drop_previous_output; "
|
||||
"send full context each turn."
|
||||
)
|
||||
req = Req(
|
||||
recv_req.rid,
|
||||
recv_req.input_text,
|
||||
recv_req.input_ids,
|
||||
recv_req.sampling_params,
|
||||
vocab_size=self.model_config.vocab_size,
|
||||
http_worker_ipc=recv_req.http_worker_ipc,
|
||||
)
|
||||
req.tokenizer = self.tokenizer
|
||||
req.set_finish_with_abort(error_msg)
|
||||
self.init_req_max_new_tokens(req)
|
||||
self._add_request_to_queue(req)
|
||||
return
|
||||
|
||||
if session_id is None:
|
||||
# Normal non-session request
|
||||
if session_id is None or radix_native_session:
|
||||
# Normal non-session request, or a radix-native session request
|
||||
if recv_req.input_embeds is not None:
|
||||
# Generate fake input_ids based on the length of input_embeds
|
||||
seq_length = len(recv_req.input_embeds)
|
||||
@@ -2056,6 +2087,8 @@ class Scheduler(
|
||||
multi_item_delimiter_indices=recv_req.multi_item_delimiter_indices,
|
||||
)
|
||||
req.tokenizer = self.tokenizer
|
||||
if radix_native_session:
|
||||
req.session_id = session_id
|
||||
|
||||
if self.disaggregation_mode != DisaggregationMode.NULL:
|
||||
# Invalid request for disaggregated mode
|
||||
@@ -4020,13 +4053,23 @@ class Scheduler(
|
||||
return ExpertDistributionReqOutput()
|
||||
|
||||
def open_session(self, recv_req: OpenSessionReqInput):
|
||||
output = self.session_controller.open(recv_req)
|
||||
if self.server_args.enable_session_radix_cache:
|
||||
# Radix-native: open is implicit; explicit open only permits id reuse.
|
||||
session_id = recv_req.session_id
|
||||
self.tree_cache.register_session(session_id)
|
||||
output = OpenSessionReqOutput(session_id, session_id is not None)
|
||||
else:
|
||||
output = self.session_controller.open(recv_req)
|
||||
if self.ps.pp_rank == 0 and self.ps.tp_rank == 0 and self.ps.attn_cp_rank == 0:
|
||||
return output
|
||||
return None
|
||||
|
||||
def close_session(self, recv_req: CloseSessionReqInput):
|
||||
self.session_controller.close(recv_req)
|
||||
if self.server_args.enable_session_radix_cache:
|
||||
# "Close" just triggers eviction of the session's tagged KV.
|
||||
self.tree_cache.release_session(recv_req.session_id)
|
||||
else:
|
||||
self.session_controller.close(recv_req)
|
||||
|
||||
def maybe_sleep_on_idle(self):
|
||||
if self.idle_sleeper is not None:
|
||||
|
||||
@@ -75,6 +75,7 @@ class InsertResult:
|
||||
|
||||
prefix_len: int
|
||||
total_len: int = 0
|
||||
last_device_node: Any = None
|
||||
mamba_exist: bool = False
|
||||
inserted_host_node: Any = None
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ from sglang.srt.mem_cache.base_prefix_cache import (
|
||||
MatchResult,
|
||||
)
|
||||
from sglang.srt.mem_cache.events import KVCacheEventMixin
|
||||
from sglang.srt.mem_cache.session_radix_cache import SessionRadixCacheMixin
|
||||
from sglang.srt.mem_cache.utils import get_eviction_strategy, split_node_hash_value
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -282,7 +283,7 @@ class TreeNode:
|
||||
return self.last_access_time < other.last_access_time
|
||||
|
||||
|
||||
class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
class RadixCache(SessionRadixCacheMixin, KVCacheEventMixin, BasePrefixCache):
|
||||
def __init__(self, params: CacheInitParams):
|
||||
self.disable = params.disable
|
||||
self.req_to_token_pool = params.req_to_token_pool
|
||||
@@ -343,6 +344,7 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
self.evictable_size_ = 0
|
||||
self.protected_size_ = 0
|
||||
self.evictable_leaves.clear()
|
||||
self._reset_session_radix_state()
|
||||
self._empty_match_result = MatchResult(
|
||||
device_indices=torch.empty(
|
||||
(0,),
|
||||
@@ -432,8 +434,10 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
# Debug/test fallback: use token ids themselves as values.
|
||||
value = torch.tensor(key.token_ids[: len(key)], dtype=torch.int64)
|
||||
|
||||
prefix_len = self._insert_helper(self.root_node, key, value, priority, chunked)
|
||||
return InsertResult(prefix_len=prefix_len)
|
||||
prefix_len, last_node = self._insert_helper(
|
||||
self.root_node, key, value, priority, chunked
|
||||
)
|
||||
return InsertResult(prefix_len=prefix_len, last_device_node=last_node)
|
||||
|
||||
def cache_finished_req(self, req: Req, is_insert: bool = True):
|
||||
"""Cache request when it finishes."""
|
||||
@@ -466,11 +470,13 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
result = self.insert(
|
||||
InsertParams(key=radix_key, value=values, priority=priority)
|
||||
)
|
||||
session_leaf = result.last_device_node
|
||||
# Free the duplicates that were already in the tree
|
||||
self.token_to_kv_pool_allocator.free(
|
||||
kv_indices[req.cache_protected_len : result.prefix_len]
|
||||
)
|
||||
else:
|
||||
session_leaf = None
|
||||
self.token_to_kv_pool_allocator.free(
|
||||
kv_indices[req.cache_protected_len : key_len]
|
||||
)
|
||||
@@ -478,6 +484,8 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
# free the unaligned tail
|
||||
self.token_to_kv_pool_allocator.free(kv_indices[key_len:])
|
||||
|
||||
self._tag_session_leaf(req, radix_key, node=session_leaf)
|
||||
|
||||
# Remove req slot release the cache lock
|
||||
if req.last_node is not None:
|
||||
self.dec_lock_ref(req.last_node)
|
||||
@@ -548,6 +556,8 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
|
||||
req.last_node = new_last_node
|
||||
|
||||
self._tag_session_leaf(req, radix_key, node=new_last_node)
|
||||
|
||||
def pretty_print(self):
|
||||
self._print_helper(self.root_node, 0)
|
||||
print(f"#tokens: {self.total_size()}")
|
||||
@@ -616,7 +626,7 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
if node.parent is None:
|
||||
assert (
|
||||
node is self.root_node
|
||||
), f"This request holds the node from another tree"
|
||||
), "This request holds the node from another tree"
|
||||
node = node.parent
|
||||
return DecLockRefResult(delta=delta)
|
||||
|
||||
@@ -712,7 +722,7 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
# Update priority along the path (take max to propagate higher priority)
|
||||
node.priority = max(node.priority, priority)
|
||||
if len(key) == 0:
|
||||
return 0
|
||||
return 0, node
|
||||
|
||||
child_key = key.child_key(self.page_size)
|
||||
|
||||
@@ -748,7 +758,8 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
self._update_leaf_status(new_node)
|
||||
# Hash will be computed lazily during event emission
|
||||
self._record_store_event(new_node)
|
||||
return total_prefix_length
|
||||
node = new_node
|
||||
return total_prefix_length, node
|
||||
|
||||
def _print_helper(self, node: TreeNode, indent: int):
|
||||
"""Prints the radix tree in a human-readable format."""
|
||||
@@ -773,6 +784,7 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
v = node.parent.children.pop(key, None)
|
||||
assert v == node, f"parent does not have child key, {key}"
|
||||
|
||||
self._discard_session_leaf(node)
|
||||
self.evictable_size_ -= len(node.key)
|
||||
if node in self.evictable_leaves:
|
||||
self.evictable_leaves.remove(node)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Session radix cache (``--enable-session-radix-cache``): tag each request's KV
|
||||
by session_id; ``release_session`` (close) frees a session's tagged KV."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections import OrderedDict, defaultdict
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sglang.srt.mem_cache.base_prefix_cache import MatchPrefixParams
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sglang.srt.managers.schedule_batch import Req
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Bounded guard against a request finishing after close. If a session id falls
|
||||
# out of this LRU after 8192 later closes, an extremely late finish can tag
|
||||
# again; explicit register_session clears the tombstone for intentional id reuse.
|
||||
_CLOSED_SESSION_TOMBSTONE_LIMIT = 8192
|
||||
|
||||
|
||||
class SessionRadixCacheMixin:
|
||||
"""Tags radix KV by session id; ``release_session`` (close) frees a session's
|
||||
tagged chains. A node holds the set of sessions on it, so a node shared by
|
||||
several sessions is freed only when its last holder closes. Tagged KV is
|
||||
ordinary LRU radix -- no pinning, no open. Mixed into RadixCache."""
|
||||
|
||||
def _reset_session_radix_state(self) -> None:
|
||||
self._session_leaves = defaultdict(set)
|
||||
self._closed_session_ids = OrderedDict()
|
||||
|
||||
def _ensure_session_radix_state(self) -> None:
|
||||
if not hasattr(self, "_session_leaves"):
|
||||
self._reset_session_radix_state()
|
||||
|
||||
def register_session(self, session_id: str) -> None:
|
||||
self._ensure_session_radix_state()
|
||||
if session_id is None:
|
||||
return
|
||||
self._closed_session_ids.pop(session_id, None)
|
||||
self._session_leaves.setdefault(session_id, set())
|
||||
|
||||
def _remember_closed_session(self, session_id: str) -> None:
|
||||
self._closed_session_ids[session_id] = None
|
||||
self._closed_session_ids.move_to_end(session_id)
|
||||
while len(self._closed_session_ids) > _CLOSED_SESSION_TOMBSTONE_LIMIT:
|
||||
self._closed_session_ids.popitem(last=False)
|
||||
|
||||
def _discard_session_leaf(self, node) -> None:
|
||||
session_ids = getattr(node, "session_ids", None)
|
||||
if not session_ids or not hasattr(self, "_session_leaves"):
|
||||
return
|
||||
for sid in tuple(session_ids):
|
||||
leaves = self._session_leaves.get(sid)
|
||||
if leaves is not None:
|
||||
leaves.discard(node)
|
||||
if not leaves and sid not in self._closed_session_ids:
|
||||
self._session_leaves.pop(sid, None)
|
||||
if hasattr(node, "session_ids"):
|
||||
delattr(node, "session_ids")
|
||||
|
||||
def _tag_session_leaf(self, req: Req, radix_key, node=None) -> None:
|
||||
"""Add this request's session id to its leaf's holder set; no-op for non-session reqs."""
|
||||
self._ensure_session_radix_state()
|
||||
sid = getattr(req, "session_id", None)
|
||||
if sid is None or sid in self._closed_session_ids:
|
||||
return
|
||||
if node is None:
|
||||
logger.warning(
|
||||
"_tag_session_leaf called without node; falling back to match_prefix"
|
||||
)
|
||||
node = self.match_prefix(MatchPrefixParams(key=radix_key)).last_device_node
|
||||
if node is not None and node is not self.root_node:
|
||||
session_ids = getattr(node, "session_ids", None)
|
||||
if session_ids is None:
|
||||
session_ids = set()
|
||||
node.session_ids = session_ids
|
||||
session_ids.add(sid)
|
||||
self._session_leaves[sid].add(node)
|
||||
logger.debug(
|
||||
"tag session %s: node=%d holders=%d indexed=%d",
|
||||
sid,
|
||||
node.id,
|
||||
len(session_ids),
|
||||
len(self._session_leaves[sid]),
|
||||
)
|
||||
|
||||
def release_session(self, session_id: str) -> int:
|
||||
"""Close: drop this session from each of its tagged leaves, freeing a node
|
||||
only once no other session still holds it (last holder). Shared
|
||||
prefixes/leaves kept."""
|
||||
self._ensure_session_radix_state()
|
||||
self._remember_closed_session(session_id)
|
||||
indexed = self._session_leaves.pop(session_id, set())
|
||||
freed = 0
|
||||
for leaf in indexed:
|
||||
if session_id not in getattr(leaf, "session_ids", set()):
|
||||
continue
|
||||
node = leaf
|
||||
while True:
|
||||
session_ids = getattr(node, "session_ids", None)
|
||||
if session_ids is not None:
|
||||
session_ids.discard(session_id)
|
||||
if not session_ids:
|
||||
delattr(node, "session_ids")
|
||||
if (
|
||||
node is self.root_node
|
||||
or node.lock_ref != 0
|
||||
or len(node.children) != 0
|
||||
or node not in self.evictable_leaves
|
||||
or getattr(node, "session_ids", None)
|
||||
):
|
||||
break
|
||||
parent = node.parent
|
||||
self.token_to_kv_pool_allocator.free(node.value)
|
||||
self._delete_leaf(node)
|
||||
freed += 1
|
||||
node = parent
|
||||
logger.info(
|
||||
"release_session %s: indexed %d leaves, freed %d nodes",
|
||||
session_id,
|
||||
len(indexed),
|
||||
freed,
|
||||
)
|
||||
return freed
|
||||
@@ -970,6 +970,10 @@ class ServerArgs:
|
||||
bool,
|
||||
"Enable streaming session mode and StreamingSession wrapper.",
|
||||
] = False
|
||||
enable_session_radix_cache: A[
|
||||
bool,
|
||||
"Hold per-session KV as ordinary evictable radix entries, tagged by session id and bulk-evicted on close. Requires --radix-eviction-policy priority.",
|
||||
] = False
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Constrained decoding
|
||||
@@ -2476,6 +2480,10 @@ class ServerArgs:
|
||||
)
|
||||
|
||||
handle_pd_disaggregation(self)
|
||||
if self.enable_session_radix_cache and self.radix_eviction_policy != "priority":
|
||||
raise ValueError(
|
||||
"--enable-session-radix-cache requires --radix-eviction-policy priority"
|
||||
)
|
||||
|
||||
# Normalize deprecated CP aliases before validations or model-specific
|
||||
# defaults inspect enable_prefill_cp/cp_strategy.
|
||||
|
||||
Reference in New Issue
Block a user