MetaX-Tech Developer Forum 论坛首页
  • 沐曦开发者
search
Sign in

zapapi

  • Members
  • Joined 2026年11月10日
  • message 帖子
  • forum 主题
  • favorite 关注者
  • favorite_border Follows
  • person_outline 详细信息

zapapi has started 1 thread.

  • See post chevron_right
    zapapi
    Members
    【镜像问题】sglang 0.5.14 镜像 --enable-torch-compile 启动报 NameError: monkey_patch_torch_compile 已解决 2026年11月10日 09:10

    一、软硬件信息

    1. 服务器厂家:浪潮 Inspur CS5998H3(255 核 / 1007 GB 内存)
    2. 沐曦 GPU 型号:曦云 C600-A ×16(单卡 73728 MiB)
    3. 操作系统内核版本:Ubuntu 22.04.5 LTS,5.15.0-119-generic
    4. 是否开启 CPU 虚拟化:未开启
    5. mx-smi 回显:
    mx-smi  version: 2.3.4
    Kernel Mode Driver Version: 3.9.6
    MACA Version: 3.8.1.3
    BIOS Version: 2.3.5.0
    Attached GPUs: 16
    | 0   MetaX C600-A | 0           Off | 0000:06:00.0  | 0%          Disabled |
    | 143W / 850W      | 35C          P0 | 178/73728 MiB | Available            |
    
    1. docker info 回显:本机是 k3s 单机集群(k3s v1.34.3+k3s1,containerd v1.34.0-k3s2),镜像由 k8s 节点直接拉取,不走 docker daemon。

    2. 镜像版本:cr.metax-tech.com/public-ai-release/maca/sglang:0.5.14-maca.ai3.8.2.122-torch2.10-py312-ubuntu22.04-amd64(linux/amd64)

    3. 启动容器命令:实际是用 k8s Deployment 起单卡实例,关键片段如下

    image: cr.metax-tech.com/public-ai-release/maca/sglang:0.5.14-maca.ai3.8.2.122-torch2.10-py312-ubuntu22.04-amd64
    resources:
      limits:
        metax-tech.com/gpu: "1"
    env:
      - REPO_ID=Qwen/Qwen3-0.6B
      - ENGINE_VERSION=v0.5.14
      - ENGINE_ARGS=--context-length 8192 --tensor-parallel-size 1 --chunked-prefill-size 1024 --cpu-offload-gb 1 --dp-size 1 --enable-dp-attention --tool-call-parser auto --mem-fraction-static 0.8
    

    镜像自带的启动脚本在 ENGINE_VERSION >= 0.4.4 时会自动追加 --enable-torch-compile,等价于下面这条直接启动命令:

    docker run --rm -it --network host --ipc host \
      --device /dev/mxcd \
      -v /models/Qwen3-0.6B:/models/Qwen3-0.6B \
      cr.metax-tech.com/public-ai-release/maca/sglang:0.5.14-maca.ai3.8.2.122-torch2.10-py312-ubuntu22.04-amd64 \
      python3 -m sglang.launch_server \
        --model-path /models/Qwen3-0.6B \
        --host 0.0.0.0 --port 8000 \
        --tp 1 --mem-fraction-static 0.8 \
        --enable-torch-compile
    

    同一镜像去掉 --enable-torch-compile 可以正常起服务、正常出结果,所以问题只跟 torch compile 相关。

    1. 容器内执行命令与关键日志:
    File "/opt/conda/lib/python3.12/site-packages/sglang/srt/model_executor/runner/decode_cuda_graph_runner.py", line 410, in set_torch_compile_config
        monkey_patch_torch_compile()
    NameError: name 'monkey_patch_torch_compile' is not defined
    ...
    Received sigquit from a child process. ...
    

    之后进程退出、容器重启,服务起不来。

    二、问题现象与定位

    1. 现象:只要带 --enable-torch-compile 启动,在 decode CUDA graph 初始化阶段就直接抛 NameError 退出;不带该参数一切正常。

    2. 定位:镜像内 decode_cuda_graph_runner.py 有两处异常:

    $ grep -n "monkey_patch_torch_compile\|def set_torch_compile_config\|patch_torch" \
        /opt/conda/lib/python3.12/site-packages/sglang/srt/model_executor/runner/decode_cuda_graph_runner.py
    39:from sglang.srt.compilation import torch_compile_decoration
    40:from sglang.srt.compilation.torch_compile_decoration import set_torch_compile_config
    397:def set_torch_compile_config():
    410:    monkey_patch_torch_compile()
    

    第 40 行已经从 torch_compile_decoration 导入了 set_torch_compile_config,但第 397 行又在本文件里重新定义了一份同名函数(覆盖了导入的版本),而这份本地实现第 410 行调用了 monkey_patch_torch_compile(),文件里却没有对应的 import。

    monkey_patch_torch_compile 本身是存在的,定义在 sglang/srt/utils/patch_torch.py:106:

    $ grep -n "def monkey_patch_torch_compile" /opt/conda/lib/python3.12/site-packages/sglang/srt/utils/patch_torch.py
    106:def monkey_patch_torch_compile():
    
    1. 与上游对比:上游 v0.5.14 的 decode_cuda_graph_runner.py 没有这段本地重复定义,直接用第 40 行导入的 set_torch_compile_config,因此不会触发该问题。看起来是沐曦分支合入本地改动时,把一份旧版函数体带进来了,漏掉了对应的 import。

    2. 临时规避(我们已经在自己的 Dockerfile 里加了,只是想知道官方是否会在后续版本修复):

    sed -i '/^from sglang.srt.compilation.torch_compile_decoration import set_torch_compile_config$/a from sglang.srt.utils.patch_torch import monkey_patch_torch_compile' \
        /opt/conda/lib/python3.12/site-packages/sglang/srt/model_executor/runner/decode_cuda_graph_runner.py
    

    补上 import 后,同一镜像带 --enable-torch-compile 冷启动约 4 分钟(compile + CUDA graph 捕获约 3 分钟),The server is fired up and ready to roll!,/v1/models 与 /v1/chat/completions 均正常。

    1. 希望确认:
    2. 这是已知问题吗?后续 sglang 镜像版本(0.5.15 及以后)是否会修复?
    3. 0.5.14 镜像的 mcoplib 版本信息里打印的是 SGlang Op Version = 0.5.13,这是否会影响该版本的其他功能?

    附件:无

  • 沐曦开发者论坛
powered by misago