Prhub

#25637 Move batch-result processing to SchedulerBatchResultProcessor and retire output_processor mixin

原始 PR 作者 fzyzcjy 合并时间 2026-05-18 18:45 文件变更 4 提交数 1 评论 1 代码增减 +667 / -735

执行摘要

将 batch 结果处理逻辑从 mixin 迁移至独立组件

PR body 明确指出这是 'Mechanical cut + paste for the introduce-batch-result-processor mech move (final extract from SchedulerOutputProcessorMixin)',目的是将调度器的输出处理逻辑从混入类彻底迁移到独立的 batch 结果处理器组件中,进一步解耦 Scheduler 类。

本 PR 是调度器重构链中的一环,建议相关开发者阅读以了解解耦模式。由于变更机械,无需深入审查逻辑,但可关注如何通过逐步提取实现大型 mixin 的拆解。

讨论亮点

该 PR 无 review 讨论。

实现拆解

  1. batch_result_processor.py 中新增方法:从 scheduler_output_processor_mixin.py 复制所有 process_batch_result_* 及辅助方法(如 _maybe_collect_routed_experts),去掉 @staticmethod 装饰器,并将 self: "SchedulerBatchResultProcessor" 简化为普通 self
  2. 补充导入:在 batch_result_processor.py 中添加 torchReqScheduleBatchLogitsProcessorOutput 等依赖项。
  3. 删除 scheduler_output_processor_mixin.py 文件(722 行)。
  4. 更新 scheduler.py:移除对 SchedulerOutputProcessorMixin 的导入和多重继承,将所有调用方式从 self.<method>(self.batch_result_processor, ...) 改为 self.batch_result_processor.<method>(...)
  5. 更新 decode.py:同样将 process_batch_result_prebuilt 的调用方式改为 self.batch_result_processor.process_batch_result_prebuilt(...)
    注:未涉及测试、配置或部署配套变更。
文件 模块 状态 重要度
python/sglang/srt/managers/scheduler_output_processor_mixin.py 调度器 removed 9.28
python/sglang/srt/managers/scheduler_components/batch_result_processor.py 结果处理器 modified 9.05
python/sglang/srt/managers/scheduler.py 调度器 modified 5.66
python/sglang/srt/disaggregation/decode.py 分离解码 modified 4.1

关键符号

process_batch_result_prebuilt _maybe_collect_routed_experts _maybe_collect_indexer_topk _maybe_collect_customized_info process_batch_result_prefill _resolve_spec_overlap_tokens process_batch_result_idle process_batch_result_decode

关键源码片段

python/sglang/srt/managers/scheduler_components/batch_result_processor.py dependency-wiring

该文件是 batch 结果处理的新家,新增了全部处理方法,是重构后的核心。

# 以下展示 process_batch_result_prebuilt 方法,它从 SchedulerOutputProcessorMixin
# 迁移过来,去掉了 @staticmethod 并将 self 类型注解简化为普通实例方法。
def process_batch_result_prebuilt(self, batch: ScheduleBatch):
    # 断言当前为分离 DECODE 模式
    assert self.disaggregation_mode == DisaggregationMode.DECODE
    use_free_group = self.server_args.disaggregation_decode_enable_radix_cache
    if use_free_group:
        self.token_to_kv_pool_allocator.free_group_begin()
    for req in batch.reqs:
        req.time_stats.set_decode_prebuilt_finish_time()
        req.check_finished()
        if req.finished():
            req.time_stats.set_quick_finish_time()
            if self.server_args.enable_hisparse:
                self.hisparse_coordinator.request_finished(req)
            release_kv_cache(req, self.tree_cache)
    # Logprobs 由 prefill 引擎处理,此处只做流式输出
    self.output_streamer.stream_output(batch.reqs, batch.return_logprob)
    if use_free_group:
        self.token_to_kv_pool_allocator.free_group_end()

评论区精华

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

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

风险与影响

该 PR 为纯机械移动,方法体无任何逻辑变化,回归风险极低。但需要注意以下两点:

  • 若存在外部脚本或未包含在本次变更中的文件也调用了 SchedulerOutputProcessorMixin 的方法,可能导致运行时错误(经查,仅 scheduler.pydecode.py 引用了相关方法,已全部更新)。
  • 缺少针对 SchedulerBatchResultProcessor 的独立单元测试,后续重构中可考虑补充。

对用户透明,功能完全一致。对开发团队而言,Scheduler 的多重继承减少,职责边界更清晰;结果处理逻辑集中到 SchedulerBatchResultProcessor 后更易于单元测试和后续优化。影响范围仅限调度器核心模块,其他模块无感知。

核心路径变更 无变更测试

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论