Prhub

#40264 [ROCm] Profiler api support for ROCm MORI toy proxy server in PD Disaggregation

原始 PR 作者 itej89 合并时间 2026-05-07 16:58 文件变更 1 提交数 4 评论 12 代码增减 +73 / -1

执行摘要

为 MORI 代理服务器添加 profiler API 支持

在ROCm MORI KV Connector的PD分离部署中,用户需要通过代理服务器统一管理prefill和decode实例。现有代理服务器缺乏性能分析接口,无法远程控制torch profiler。此PR通过在代理服务器上暴露/start_profile和/stop_profile端点,使得用户可以通过统一的API启动和停止所有decode服务器的profiler,并收集trace文件用于性能分析。

该PR变更清晰,适合想了解PD分离和KV Connector profiling的开发者快速阅读。设计上使用asyncio并发发送请求,并正确使用锁机制处理竞态条件,值得借鉴。

讨论亮点

在代码审查中,gemini-code-assist[bot] 指出了 send_profile_cmd 函数中的竞态条件:直接访问全局 prefill_instances 和 decode_instances 列表而未加锁,可能导致迭代时列表被后台线程修改而抛出 RuntimeError。建议使用已有的 _list_lock 创建本地副本。该建议被采纳,提交者在后续版本中修复了该问题。此次讨论体现了对并发安全性的重视,即使是在示例脚本中。

实现拆解

  1. 新增依赖与命令行参数:在文件头部添加 argparse 和 urlparse 导入,并在 __main__ 块中使用 argparse 解析 --port 参数,替换原先硬编码的端口号。
  2. 实现 send_profile_cmd 核心函数:异步函数接收 req_data 和 profiler_cmd(仅允许 "start" 或 "stop")。通过 _list_lock 获取 prefill_instances 和 decode_instances 的本地副本,避免多线程竞态条件。若没有已注册的实例,则直接抛出 RuntimeError。随后使用 aiohttp 并发地向所有实例发送POST请求,URL格式为 http://hostname:port/{cmd}_profile。所有请求通过 asyncio.gather 并发执行,并统一处理异常和HTTP错误状态码。最后返回第一个成功响应的JSON。
  3. 添加 /start_profile 和 /stop_profile 端点:使用Quart的 @app.post 装饰器注册两个路由,分别解析请求JSON并调用 send_profile_cmd,遇到异常时记录日志并返回500错误。
  4. 主入口配置调整:将 app.run(port=10001) 替换为 app.run(port=args.port),使端口可通过 --port 参数指定。
    本次变更为纯新增功能,未引入测试文件,但PR body中提供了详细的部署和验证步骤,证明变更可正常工作。
文件 模块 状态 重要度
examples/disaggregated/disaggregated_serving/moriio_toy_proxy_server.py 代理服务器 modified 7.43

关键符号

send_profile_cmd start_profile stop_profile

关键源码片段

examples/disaggregated/disaggregated_serving/moriio_toy_proxy_server.py dependency-wiring

添加了 profiler API 支持,包括 send_profile_cmd 函数和 /start_profile、/stop_profile 端点,以及端口参数配置。

async def send_profile_cmd(req_data: dict, profiler_cmd: str):
    # profiler_cmd 必须是 "start" 或 "stop"
    assert profiler_cmd in {"start", "stop"}
​
    # 加锁获取实例列表的本地副本,避免竞态条件
    with _list_lock:
        p_instances = list(prefill_instances)
        d_instances = list(decode_instances)
​
    if not p_instances and not d_instances:
        raise RuntimeError(
            "Service Unavailable: No prefill or decode instances are registered."
        )
​
    headers = {
        "Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
    }
​
    tasks = []
​
    # 使用 aiohttp 并发地向所有 prefill 和 decode 实例发送 profiler 请求
    async with aiohttp.ClientSession(
        timeout=aiohttp.ClientTimeout(total=60)
    ) as session:
        for instances in (p_instances, d_instances):
            for inst in instances:
                _p = urlparse(inst["request_address"])
                url = f"http://{_p.hostname}:{_p.port}/{profiler_cmd}_profile"
​
                tasks.append(
                    session.post(
                        url,
                        json=req_data,
                        headers=headers,
                    )
                )
​
        responses = await asyncio.gather(*tasks, return_exceptions=True)
​
        for r in responses:
            if isinstance(r, Exception):
                raise r
            if r.status >= 400:
                msg = await r.text()
                raise RuntimeError(f"{profiler_cmd}_profile failed: {r.status}, {msg}")
​
        return await responses[0].json()
​
​
@app.post("/start_profile")
async def start_profile():
    try:
        req_data = await request.get_json()
        return await send_profile_cmd(req_data, "start")
    except Exception as e:
        logger.exception("start_profile failed: %s", e)
        return await make_response((str(e), 500))
​
​
@app.post("/stop_profile")
async def stop_profile():
    try:
        req_data = await request.get_json()
        return await send_profile_cmd(req_data, "stop")
    except Exception as e:
        logger.exception("stop_profile failed: %s", e)
        return await make_response((str(e), 500))

评论区精华

send_profile_cmd 中访问全局列表的竞态条件 正确性

gemini-code-assist[bot] 指出,send_profile_cmd 直接访问全局 prefill_instances 和 decode_instances 列表,而这些列表由后台线程修改,存在竞态条件。建议使用 _list_lock 创建本地副本。

结论:采纳建议,在函数开头用 with _list_lock 创建本地副本 p_instances 和 d_instances。 · 已解决

风险与影响

变更为示例脚本,不涉及任何生产代码,风险极低。唯一的风险是如果代理服务器与实例之间网络不通,profiler请求会超时并返回错误,但属于预期行为。此外,send_profile_cmd中已使用_list_lock保护全局列表,竞态条件已修复。

对用户:为使用MORI KV Connector的PD分离部署用户提供了便捷的性能分析接口。对系统:仅影响MORI代理服务器,不影响vLLM核心组件。对团队:降低了在ROCm平台上调试和性能分析的门槛。

无显著风险,仅示例变更

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论