Prhub

#25711 Expose can-run-cuda-graph as a regular property on the embedding result

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

执行摘要

EmbeddingBatchResult 显式暴露 can_run_cuda_graph 属性

EmbeddingBatchResult 没有声明 can_run_cuda_graph 字段,导致 process_batch_result_prefill 等函数需要通过 getattr(result, "can_run_cuda_graph", False) 来读取,这种隐式回退对类型系统不友好。由于 Embedding 前向从不运行 CUDA Graph,其值恒为 False,应通过显式属性暴露该事实。

可快速合并,作为重构链中的一环,为未来统一类型处理打下基础。

讨论亮点

该 PR 仅有 1 条来自 gemini-code-assist 的评论,提示已到达每日配额限制,未讨论技术细节。

实现拆解

  1. python/sglang/srt/managers/utils.py: 在 EmbeddingBatchResult 数据类中添加 can_run_cuda_graph 属性,使用 @property 装饰器,始终返回 False
  2. python/sglang/srt/dllm/mixin/scheduler.py: 将 getattr(result, "can_run_cuda_graph", False) 替换为 result.can_run_cuda_graph 直接访问。
  3. python/sglang/srt/managers/scheduler_components/batch_result_processor.py: 同样将 getattr 调用替换为直接属性访问。
  4. python/sglang/srt/disaggregation/prefill.py: 同样将 getattr 调用替换为直接属性访问。

所有改动均为机械性替换,不引入新依赖或运行时状态。

文件 模块 状态 重要度
python/sglang/srt/managers/utils.py 结果处理器 modified 5.69
python/sglang/srt/dllm/mixin/scheduler.py 调度器 modified 5.1
python/sglang/srt/managers/scheduler_components/batch_result_processor.py 结果处理器 modified 5.1
python/sglang/srt/disaggregation/prefill.py 拆分配置 modified 4.49

关键符号

EmbeddingBatchResult.can_run_cuda_graph

关键源码片段

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

在 EmbeddingBatchResult 数据类中新增 can_run_cuda_graph property,是核心变更点。

@dataclass
class EmbeddingBatchResult:
    """Embeding / classification forward 的结果。"""
    embeddings: torch.Tensor
    pooled_hidden_states: Optional[torch.Tensor] = None
    copy_done: Optional[torch.cuda.Event] = None
​
    @property
    def can_run_cuda_graph(self) -> bool:
        # Embedding 模型从不运行 CUDA Graph,因此始终返回 False
        return False
​
    def copy_to_cpu(self):
        # ...

评论区精华

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

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

风险与影响

风险极低:改动为纯机械替换,逻辑等价。所有调用处原来的 getattr(result, "can_run_cuda_graph", False) 在 EmbeddingBatchResult 实例上始终返回 False,现在通过 property 显式返回 False,行为无变化。但需确保所有 EmbeddingBatchResult 类型的结果变量都被正确捕获,不存在残留的 getattr 调用。

影响范围小:仅涉及 Embedding 相关的 prefill 结果处理路径,对 GenerationBatchResult 无影响。代码可读性和类型检查友好度提升。

缺少测试覆盖

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论