Prhub

#23389 [Chore] Remove deadcode in prefill delayer

原始 PR 作者 jonahbernard 合并时间 2026-04-28 00:59 文件变更 1 提交数 2 评论 3 代码增减 +28 / -19

执行摘要

清除 PrefillDelayer 中不可达代码并替换 **kwargs 为显式参数

PR 说明此变更不改变代码库行为,旨在移除不可达代码,使代码库更易读。引入的 **kwargs 是糟糕的编码风格,因其隐藏了实际传递的参数。

值得快速合入,是良好的代码清理实践。可推荐给团队作为 **kwargs 重构的范例。

讨论亮点

无 review 评论。维护者 ch-wan 直接批准了该 PR。

实现拆解

  1. _negotiate_should_allow_prefill 方法中:将 **kwargs 替换为 running_batch: int = 0max_prefill_bs: int = 0max_running_requests: int = 0 三个显式参数,并在调用 _negotiate_should_allow_prefill_pure 时直接传递这些参数。
  2. _negotiate_should_allow_prefill_pure 方法中:同样将 **kwargs 替换为显式参数,并移除 if kwargs is None 的不可达分支代码。原本 kwargs.get('max_running_requests', 0) 改为直接使用 max_running_requests 参数。
  3. _gather_info 方法中:将 **kwargs 替换为 running_batch: int = 0max_prefill_bs: int = 0 两个显式参数,移除 kwargs.get('running_batch', 0)kwargs.get('max_prefill_bs', 0)
  4. PrefillDelayerSinglePassExecutor.negotiate_should_allow_prefill 方法中:同样将 **kwargs 替换为显式参数,并传递这些参数给 _negotiate_should_allow_prefill
    所有改动仅涉及 python/sglang/srt/managers/prefill_delayer.py,未添加测试文件。
文件 模块 状态 重要度
python/sglang/srt/managers/prefill_delayer.py 调度器 modified 7.11

关键符号

negotiate_should_allow_prefill _negotiate_should_allow_prefill _negotiate_should_allow_prefill_pure _gather_info PrefillDelayerSinglePassExecutor.negotiate_should_allow_prefill

关键源码片段

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

唯一修改的文件,移除了不可达代码并将 `**kwargs` 替换为显式参数,涉及调度器核心类 `PrefillDelayer` 及其执行器。

def _negotiate_should_allow_prefill(
    self,
    local_prefillable: bool,
    token_usage: float,
    running_batch: int = 0, # 显式参数,替代 **kwargs
    max_prefill_bs: int = 0,
    max_running_requests: int = 0,
) -> _NegotiateOutput:
    out = self._negotiate_should_allow_prefill_pure(
        prev_state=self._curr_state,
        local_prefillable=local_prefillable,
        token_usage=token_usage,
        running_batch=running_batch,
        max_prefill_bs=max_prefill_bs,
        max_running_requests=max_running_requests,
    )
    self._curr_state = out.next_state
    return outdef _negotiate_should_allow_prefill_pure(
    self,
    prev_state: Optional[_State],
    local_prefillable: bool,
    token_usage: float,
    running_batch: int = 0, # 显式参数,替代 **kwargs
    max_prefill_bs: int = 0,
    max_running_requests: int = 0,
) -> _NegotiateOutput:
    # ... 本地状态计算省略 ...
    tp0_info = self._gather_info(
        local_prefillable=local_prefillable,
        local_token_watermark_force_allow=local_token_watermark_force_allow,
        running_batch=running_batch, # 显式传递
        max_prefill_bs=max_prefill_bs, # 显式传递
    )
    # ... 全局状态聚合省略 ...
​
    # 核心变更:移除了 `if kwargs is None` 不可达分支
    if prefillable_status == "all":
        # 原本 `kwargs.get('max_running_requests', 0)` 现在直接使用参数
        if not self.enable_dp_attention:
            max_running_requests = (
                max_running_requests + self.dp_size - 1
            ) // self.dp_size
        if (
            max_running_requests - global_running_batch.max().item()
            < global_max_prefill_bs.max().item()
        ):
            # ... 延迟处理 ...
        exist_previous_wait = prev_state is not None
        return _NegotiateOutput(
            next_state=None,
            output_allow=True,
            output_reason="wait_success" if exist_previous_wait else "no_wait",
            **debug_info,
        )
    # ... 其余分支不变 ...

评论区精华

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

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

风险与影响

风险极低。变更为纯重构,不改变逻辑行为:移除不可达代码(kwargs is None 分支在 Python 中永不触发),替换 **kwargs 为显式参数时保持了默认值 0,与原行为一致。测试未改动,但原有测试应覆盖路径。

影响范围限于 PrefillDelayer 类及其单次执行包装器。对用户无感知,降低了未来维护者的认知负担,使参数传递链路清晰。

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论