Prhub

#26883 [PP][Bugfix] Handle input_ids assignment in prepare_for_extend

原始 PR 作者 ShangmingCai 合并时间 2026-06-01 14:43 文件变更 1 提交数 3 评论 4 代码增减 +7 / -0

执行摘要

修复 PP profiler 中 deferred H2D 后 input_ids 未赋值

PR body 指出这是一个“CPP bug (missing change of PR: #25945)”,即 #25945 重构了 prepare_for_extend 以支持 deferred H2D(将 input_ids 延迟到 GPU 上分配),但 PP profiler 路径(profile_and_init_predictor)未同步更新,导致 batch.input_ids 仍为 None 时被后续代码使用,引发错误。

值得合入,修复明确,逻辑可读且无副作用。建议验证 PP profiling 端到端测试通过。

讨论亮点

无 reviewer 评论,作者自行合并。CI 中仅作者触发 rerun 了相关 disaggregation PP 测试并通过。

实现拆解

  1. 定位问题:在 python/sglang/srt/managers/scheduler_pp_mixin.pyprofile_and_init_predictor 方法中,batch.prepare_for_extend() 调用后,由于 #25945 的变更,batch.input_ids 可能为 None(数据尚在 CPU 上)。
  2. 添加回退逻辑:在 batch.prepare_for_extend() 之后、ForwardBatch.init_new 之前,插入一个条件判断:如果 batch.input_idsNonebatch.prefill_input_ids_cpu 不为 None,则将 prefill_input_ids_cpu 异步拷贝到 GPU 设备(non_blocking=True),并赋值给 batch.input_ids,同时将 batch.prefill_input_ids_cpu 置为 None 以释放内存。
  3. 影响范围:仅影响 PP(Pipeline Parallelism)profiling 路径,不改变常规推理流程。
文件 模块 状态 重要度
python/sglang/srt/managers/scheduler_pp_mixin.py 调度器 modified 6.02

关键源码片段

python/sglang/srt/managers/scheduler_pp_mixin.py core-logic

唯一修改的文件,在 `profile_and_init_predictor` 方法中增加了 deferred H2D 回退逻辑,修复了关键路径上的数据竞争问题。

# 位于 profile_and_init_predictor 方法中
batch.prepare_for_extend()# Resolve deferred H2D: prepare_for_extend 现在将 input_ids 延迟到 GPU
# 因此在之后需要检查 input_ids 是否为 None,并从 CPU 拷贝
if batch.input_ids is None and batch.prefill_input_ids_cpu is not None:
    # 异步拷贝到 GPU,避免阻塞
    batch.input_ids = batch.prefill_input_ids_cpu.to(
        self.device, non_blocking=True
    )
    batch.prefill_input_ids_cpu = None # 释放 CPU 内存

评论区精华

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

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

风险与影响

风险较低,修改仅影响 PP profiler 路径,且逻辑简单(从 CPU 拷贝到 GPU)。但需确认 prefill_input_ids_cpu 在 profiler 上下文中确实一直有效;若该属性未正确设置,则条件不会触发,但也不会引入新 crash。

  • 用户/系统:修复了启用 PP dynamic chunk profiling 时可能遇到的崩溃或错误,确保 PP profiler 正常工作。
  • 影响范围:仅影响使用 PP 且启用 profiling(profile_and_init_predictor)的用户,常规推理无影响。
  • 影响程度:中等,因为修复的是一个功能性回归。
PP 专属路径

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论