Repository navigation
feat: Session-reference-aware Unified Radix Cache for agentic multi-turn workloads - #29173
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements a ref-aware KV cache eviction mechanism with two-tier priority, introducing a reusable RefAwareCacheMixin along with RefAwareHiRadixCache and RefAwareRadixCache implementations, new API endpoints, and scheduling integration. The code review identified several critical issues: a runtime NameError in prepare_for_extend due to an undefined reqs variable, a potential bug when modifying the running batch in-place during forward iteration, reference count collisions when req.rid is empty, and bypassed eviction metrics caused by calling the private _evict_tiered method directly instead of using the public interface.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
This is great work! Wanted to flag some similar work that is being done with SGLang session-radix cache. I think there's some easy parallels and wins here. IIUC the distinction seems to be:
I wonder if we can share the existing |
Hi Ishan, thanks for your review! I have seen your message on Slack. Happy to discuss it there. |
e96abee to
90980f1
Compare
There was a problem hiding this comment.
Narrow review at 67801c4135. I reproduced two blocking correctness issues: (1) current head cannot import/start on supported Python 3.12 because the new stdlib dataclasses subclass msgspec.Struct; (2) after an import-only throwaway patch, a valid low-priority multi-chunk request crashes the scheduler. The added 40 manual RefAware tests and 85 server-argument tests pass but do not exercise either path. I intentionally excluded SessionRadixCache removal, /close_session, and broader conditional concerns from this review.
Note - generated with codex but reviewed by Ishan
ishandhanani
left a comment
There was a problem hiding this comment.
Follow-up review at b404e169a4. The two previously posted findings are resolved: the server imports and starts on Python 3.12.12, and the exact 200-token low-priority chunked-prefill reproduction now completes with HTTP 200. The refreshed 125-test targeted suite and a 64/64-request AIPerf smoke also pass, but they do not exercise the HTTP control routes or HiCache host-pressure lifecycle below. This review is intentionally limited to four independently reproduced P1 issues; SessionRadixCache removal, /close_session, conditional priority contracts, and broader deep-review notes are excluded.
1b8a065 to
92a209a
Compare
|
Hey wanted to give some of my thoughts on this PR! Main correctness issue I found: mixed-priority refs can leak within the same Repro shape:
After release, the suffix can still have I think the issue is that Possible fixes:
Bigger-picture thoughts: I like the direction. Agentic workloads are not well served by plain LRU, because useful prefixes can look temporarily cold while the agent is in a tool call, a branch, or a SWE-bench-style edit loop. Treating That said, I think the hard part is lifecycle semantics, not only eviction priority. Long term, this probably wants to behave like a session/trajectory lease:
Things I’d consider next:
So my take is: the PR is a promising first step toward agent-aware KV management, and the GPU smoke path works, but I’d fix the mixed-priority accounting and clarify the session/ref lifecycle before landing. |
|
@krishs0404 Hi, thanks for review. In fact, the low-priority session should not send high-priority generation request unless calls update_ref. It may need a log here to manage priority mismatch instead of take this case as normal one. |
06c0fe7 to
6090342
Compare
ishandhanani
left a comment
There was a problem hiding this comment.
Some mechanical reviews. Have some questions about some mechanics of the ref aware but i need to deeply understand for a bit longer. Will add that review in very very soon
Track per-session references on UnifiedRadixCache KV so that eviction consumes unreferenced entries before referenced ones, and closing a session only dereferences its KV instead of freeing it. Enabled by --enable-session-radix-cache on top of SGLANG_ENABLE_UNIFIED_RADIX_TREE=1. Each tree component keeps its own session frontier leaves and per-node session_ref, over the span it actually owns: Full covers the whole leaf->root path, SWA one sliding window, Mamba only the leaf. The per-component LRUs are partitioned by session_ref around a locked `mid` sentinel, so an eviction walk drains the unreferenced side first, and SWA/Mamba walk behind a locked cursor sentinel that the list relinks on its own. Full keeps its heap and orders by (referenced, ref, base priority) instead. Registration happens in cache_finished_req after a successful insert (FINISH_ABORT excluded), guarded by a monotonic session generation so a request that finishes after a close/reopen of the same session id cannot tag the new incarnation. Drops the previous RadixCache-based SessionRadixCacheMixin.
ab3f0e5 to
daf70ab
Compare
It should be fixed. And i have moved the session-radix-cache test to the registered. |
|
/rerun-failed-ci |
|
/rerun-test test/registered/unit/mem_cache/test_session_unified_radix_cache.py test/registered/unit/mem_cache/test_tree_core_registry.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py |
|
Results for 🚀 🚀 |
| @@ -0,0 +1,222 @@ | |||
| """Tests for session references on UnifiedRadixCache.""" | |||
There was a problem hiding this comment.
we need to add an e2e test in next pr for session radix tree @Zhangmj0621
refer to test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_swa.py
…urn workloads (sgl-project#29173) Co-authored-by: Ishan Dhanani <[email protected]> Co-authored-by: hzh0425 <[email protected]> Co-authored-by: ispobock <[email protected]>
…urn workloads (sgl-project#29173) Co-authored-by: Ishan Dhanani <[email protected]> Co-authored-by: hzh0425 <[email protected]> Co-authored-by: ispobock <[email protected]>

Motivation
In multi-turn inference workloads such as agentic workflows and RL rollouts, active sessions accumulate reusable KV prefixes across turns. Under memory pressure, the standard radix cache evicts entries using only the configured cache policy, without considering whether a prefix is still referenced by an active session.
This may evict an active session's prefix while unrelated, unreferenced KV remains cached, causing unnecessary recomputation in later turns and reducing cache hit rate and throughput.
Design
This PR adds session-aware eviction to
UnifiedRadixCache. Applications pass a stable top-levelsession_idwith each request. When a request finishes, SGLang registers its reusable KV under that session. When the session ends, the application calls/close_sessionto release those references.Each UnifiedRadixCache component tracks the KV region reusable by a session:
(is_referenced, session_ref_count, base_eviction_priority).Referenced KV is soft-protected, not pinned. If unreferenced KV is insufficient, referenced entries remain evictable. Closing a session removes its references without immediately freeing its KV, allowing the KV to remain reusable until normal eviction reclaims it.
Session generations and bounded closed-session tombstones prevent stale in-flight requests from reattaching references after close or close/reopen.
This feature is implemented only by
UnifiedRadixCache. OrdinaryRadixCacheandHiRadixCachedo not maintain session references.Storage-backed L3 cache is not included in this PR.
Modifications
SessionUnifiedRadixCacheMixinfor session lifecycle and stale-request protection./close_session.session_idwithout changing or reconstructing request prompts.--radix-eviction-policy priority.SessionAwareEvictionStrategyintegration from ordinaryRadixCacheandHiRadixCache.--enable-session-radix-cache;UnifiedRadixCachemust also be enabled.Speed Tests and Profiling
We evaluate session radix cache on a SWE-agent agentic workload using GLM-5.2 (UnifiedRadixCache) under different batch sizes. The cache hit rate breakdown between the original HiCache and session radix cache is shown below.
The results show that session-reference-aware radix cache substantially improves both cache hit rate and end-to-end throughput. By preserving KV cache entries that are still referenced by active multi-turn requests, session radix cache reduces unnecessary prefix recomputation and improves cache residency under memory pressure.
However, we also observe that the cache hit rate still drops at larger batch sizes. This suggests that large batches can still cause KV cache thrashing, even with reference-aware eviction. Addressing this issue may require additional mechanisms such as cache-aware admission control, batch throttling, or concurrency control, similar to the approaches explored in ThunderAgent and Concur.
CI States
Latest PR Test (Base): ✅ Run #30707949357
Latest PR Test (Extra): ✅ Run #30707949287