Prhub

#23202 [core] Always-on `StreamingSession` in `UnifiedRadixCache`

原始 PR 作者 hnyls2002 合并时间 2026-04-20 12:19 文件变更 7 提交数 6 评论 2 代码增减 +34 / -54

执行摘要

无条件嵌入 StreamingSession 到 UnifiedRadixCache,移除配置开关实现零开销。

PR body 明确说明:将 StreamingSession 嵌入 UnifiedRadixCache 无条件化,因为 try_* 方法在非流式请求或真实 TreeNode 上会短路,从而在无实际流式会话时实现零开销。移除每个缓存的 enable_streaming_session 配置管道,以减少复杂性和维护成本。

值得精读,尤其关注如何通过设计实现零开销嵌入(如 try_* 短路机制)以及配置简化的最佳实践,适合缓存和会话管理模块的开发者参考。

讨论亮点

无实质性 review 讨论,仅有的评论为 bot 消息和作者触发 CI 的命令(/tag-and-rerun-ci),表明变更较为直接且已基于前序 PR #23145 的共识,通过 CI 验证后合并。

实现拆解

  1. 重命名与文件结构调整:将 session/session_aware_cache.py 重命名为 session/streaming_session.py,类名从 SessionAwareCache 改为 StreamingSession,以更准确反映其作为流式会话组件的身份,而非缓存变体。
  2. 核心缓存模块改造:在 python/sglang/srt/mem_cache/unified_radix_cache.py 中,移除 params.enable_streaming_session 判断,无条件初始化 self.session = StreamingSession(inner=self);并删除所有 if self.session is not None 守卫,直接调用 self.session.try_match_prefixself.session.try_inc_lock_ref 等方法,确保零开销短路逻辑生效。
  3. 配置参数清理:移除 python/sglang/srt/mem_cache/cache_init_params.py 中的 enable_streaming_session 字段,在 python/sglang/srt/managers/scheduler.py 中去除 params.enable_streaming_session = server_args.enable_streaming_session 设置代码,并更新 python/sglang/srt/server_args.py 中的帮助文本以反映 StreamingSession 重命名。
  4. 调度器与调用方适配:在 scheduler.py 中更新导入从 SessionAwareCacheStreamingSession,并调整条件检查逻辑,当 tree_cache.supports_streaming_session() 返回 True 时使用 StreamingSession 包装。
  5. 测试配套更新:修改 test/registered/unit/mem_cache/test_streaming_session_unit.py 中的导入和类名引用,从 SessionAwareCacheSessionSlot 改为 StreamingSessionSessionSlot,保持测试覆盖不变。
文件 模块 状态 重要度
python/sglang/srt/mem_cache/unified_radix_cache.py 缓存管理 modified 7.24
python/sglang/srt/session/streaming_session.py 会话层 renamed 6.45
python/sglang/srt/managers/scheduler.py 调度器 modified 5.51
test/registered/unit/mem_cache/test_streaming_session_unit.py 测试单元 modified 4.5

关键符号

UnifiedRadixCache.__init__ UnifiedRadixCache.match_prefix UnifiedRadixCache.cache_finished_req StreamingSession.try_match_prefix Scheduler.init_cache_with_memory_pool

关键源码片段

python/sglang/srt/mem_cache/unified_radix_cache.py core-logic

核心缓存模块,实现了无条件嵌入 StreamingSession 和移除条件判断,直接影响缓存行为。

    def __init__(self, params: CacheInitParams):
        # ... 其他初始化代码 ...
        # Streaming session: embedded StreamingSession with self as inner.
        # Always on -- zero overhead when no streaming session is open (the
        # try_* entries short-circuit on non-streaming reqs / real TreeNodes).
        # Dispatch methods below pre-check conditions so the session's
        # internal fall-through to self.inner.xxx never fires -- no recursion.
        self.session = StreamingSession(inner=self) # 无条件初始化,移除条件判断
        self.reset()
        logger.info(f"Init Unified RadixTree with components {self.tree_components}")
​
    def match_prefix(self, params: MatchPrefixParams) -> MatchResult:
        result = self.session.try_match_prefix(params) # 直接调用,无需检查 None
        if result is not None:
            return result
        # ... 后续非流式请求处理逻辑 ...
python/sglang/srt/managers/scheduler.py dependency-wiring

调度器模块更新导入和配置设置,移除 enable_streaming_session 的管道,确保缓存初始化正确。

        # 在初始化缓存时,移除 enable_streaming_session 设置
        params.tree_components = tuple(tree_components) # 移除了 params.enable_streaming_session = server_args.enable_streaming_session
        self.tree_cache = UnifiedRadixCache(params)
​
        # 后续条件检查更新为使用 StreamingSession
        if (
            server_args.enable_streaming_session
            and not self.tree_cache.supports_streaming_session()
        ):
            self.tree_cache = StreamingSession(self.tree_cache) # 替换 SessionAwareCache

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险较低:核心逻辑依赖 try_* 方法在非流式请求时正确短路以避免递归或性能下降,若短路机制失效可能导致额外开销或逻辑错误;配置文件移除 enable_streaming_session 可能影响向后兼容性,但该参数已被废弃且行为不变;需确保所有调用方(如调度器)适配新接口。

对用户透明,因为流式会话行为不变且无额外开销;系统层面简化了缓存管理,减少配置复杂性,提升代码可维护性;团队受益于更清晰的接口和减少的冗余代码,但需更新相关文档或配置示例。

配置移除 核心路径变更

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论