Prhub

#2169 Merging profiling info into router

原始 PR 作者 zhuzilin 合并时间 2026-07-02 21:18 文件变更 6 提交数 1 评论 0 代码增减 +185 / -641

执行摘要

将 Profiling 信息合并到 Router,重构 sglang 补丁

旨在将 profiling 信息的收集和聚合从 sglang 后端转移到 router 组件,降低对 sglang 版本的侵入性,并集中管理性能数据。从变更可见,原本在 decode 中通过 apply_prefill_timing_payloadis_slime_profiling_enabled 等函数执行的 profiling 逻辑被移除,转而依赖 router 侧的能力。

建议仔细 review router 侧对应的 profiling 实现是否已合入,并充分测试 PD 分离场景下的 timeout 和 memory 释放逻辑。

讨论亮点

由于该 PR 无 review 评论,无讨论值得提炼。

实现拆解

  1. 重构 sglang.patch:在 docker/patch/latest/sglang.patch 中移除了与 profiling 相关的导入和函数调用(如 apply_prefill_timing_payloadis_slime_profiling_enabled),并添加了 release_memory_occupationresume_memory_occupation 方法以支持显存释放。同时引入了 bootstrapping 超时处理机制。

  2. 增强 sglang-top_p.patch:在 MetadataBuffers 类中添加 output_top_p_token_ids_lenoutput_top_p_token_ids 缓冲区,并在 DecodeTransferQueue 中传输这些数据,以支持 top_p 采样的 token IDs 回传。

  3. 移除 rollout 中的 profiling 标志:在 slime/ray/rollout.py 中去掉 SLIME_ENABLE_PROFILING="true" 环境变量,因为 profiling 已由 router 接管。

  4. 更新测试用例:修改 tests/utils/test_trace_utils.py,验证 build_sglang_meta_trace_attrs 返回的 trace 属性包含 sglang_pd_prefillsglang_pd_decode 子 span。

  5. 配套基础设施:更新 docker/Dockerfile 中 sglang_router 包的版本和 docker/version.txt

文件 模块 状态 重要度
docker/patch/latest/sglang.patch sglang 补丁 modified 7.81
docker/patch/latest/sglang-top_p.patch top-p 补丁 modified 5.87
tests/utils/test_trace_utils.py trace 工具 modified 5.03
slime/ray/rollout.py rollout 引擎 modified 4.75
docker/Dockerfile Docker 镜像 modified 2.78
docker/version.txt 版本文件 modified 1.72

关键符号

build_sglang_meta_trace_attrs start_engines release_memory_occupation resume_memory_occupation get_buf get_buf_infos

关键源码片段

docker/patch/latest/sglang-top_p.patch test-coverage

新增 top_p_token_ids 缓冲区支持,增强 PD 传输能力

# docker/patch/latest/sglang-top_p.patch 修改后的 MetadataBuffers 新增部分
MAX_PD_TOP_P_TOKEN_IDS = 4096class MetadataBuffers:
    def __init__(self, size, max_top_logprobs_num, device):
        # ... 原有初始化 ...
        # 新增:为 top_p token ids 分配固定大小缓冲区
        self.output_top_p_token_ids_len = torch.zeros(
            (size, 16), dtype=torch.int32, device=device
        )
        self.output_top_p_token_ids = torch.zeros(
            (size, MAX_PD_TOP_P_TOKEN_IDS), dtype=torch.int32, device=device
        )
​
    def get_buf(self, idx):
        # 返回缓冲区切片时包含新增字段
        return (
            # ... 原有返回值 ...
            self.output_top_p_token_ids_len[idx].clone(),
            self.output_top_p_token_ids[idx].clone(),
        )
tests/utils/test_trace_utils.py test-coverage

验证新的 trace 子 span 结构,确保 build_sglang_meta_trace_attrs 行为正确

# tests/utils/test_trace_utils.py 修改后的测试函数
@pytest.mark.unit
def test_build_sglang_meta_trace_attrs_keeps_standard_and_pd_fields():
    meta = {
        "prompt_tokens": 12,
        "completion_tokens": 7,
        "cached_tokens": 3,
        "pd_prefill_forward_duration": 0.125,
        "pd_decode_transfer_duration": 0.05,
        "finish_reason": {"type": "stop"},
        "unused_field": "ignored",
    }
​
    attrs = build_sglang_meta_trace_attrs(meta)
    # 弹出子 span 字典
    trace_children = attrs.pop(TRACE_CHILDREN_KEY)
​
    # 验证标准字段被保留,finish_reason 被展平
    assert attrs == {
        "prompt_tokens": 12,
        "completion_tokens": 7,
        "cached_tokens": 3,
        "finish_reason": "stop",
    }
    # 验证 trace 子 span 包含 prefill 和 decode 信息
    assert trace_children[0]["name"] == "sglang_pd_prefill"
    assert trace_children[0]["children"][0]["attrs"] == {
        "pd_prefill_forward_duration": 0.125,
    }
    assert trace_children[1]["name"] == "sglang_pd_decode"
    assert trace_children[1]["children"][0]["attrs"] == {
        "pd_decode_transfer_duration": 0.05,
    }

评论区精华

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

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

风险与影响

主要风险在于 sglang.patch 的大范围重构(+126/-610),可能影响 PD 分离系统的稳定性。新引入的 bootstrapping 超时逻辑可能在某些场景下过早中断请求。移除 SLIME_ENABLE_PROFILING 环境变量后,若 router 尚未完整实现 profiling 聚合,会导致数据丢失。

对开发者而言,sglang 补丁维护复杂度降低;对系统而言,profiling 数据流发生变化,需要确保 router 版本兼容。对用户透明,但可能影响性能分析工具的输出。

补丁大范围重构 依赖 router 侧能力 环境变量移除影响 profiling

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论