暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

企业级部署实操:SGLang 多节点集群部署Qwen系列大模型

技术源share 2025-04-18
4057

 

字数 1660,阅读大约需 9 分钟

往期推荐

目标

比起Ollama的方便,有些时候高并发更重要,因此这篇文章将实现在两台电脑(双节点)上部署 SGLang(当然如果你还有多余的也可以加进来当节点)
,运行 Qwen2.5-7B-Instruct
 模型,实现本地资源的充分利用。

硬件

  • • 节点 0:IP 192.168.0.12
    ,1 个 英伟达显卡
  • • 节点 1:IP 192.168.0.13
    ,1 个 英伟达显卡

模型

Qwen2.5-7B-Instruct
,FP16 下约需 14GB 显存,使用 --tp 2
 后每 GPU 约 7GB(权重)+ 2-3GB(KV 缓存)。

网络

两节点通过以太网(TCP)通信,网络接口为 eno1。

这里根据自己电脑 ip addr 查询即可

不量化

使用 FP16 精度以保留最大精度,显存占用较高,需优化配置。

2. 前置条件

在开始安装和部署之前,确保以下条件满足:

操作系统

  • • 推荐 Ubuntu 20.04/22.04 或其他 Linux 发行版(Windows 不推荐,需 WSL2)
  • • 两节点最好是一致的环境,当然os的环境不是必须,但是Python
    的环境需要一样

网络连通性

  • • 节点 0(192.168.0.12)和节点 1(192.168.0.13)可互相 ping 通:
ping 192.168.0.12  # 从节点 1
ping 192.168.0.13  # 从节点 0

  • • 端口 50000(分布式初始化)和 30000(HTTP 服务器)未被防火墙阻挡:

sudo ufw allow 50000
sudo ufw allow 30000

  • • 确认网络接口 eno1:
# 具体网卡根据实际调整
ip addr show eno1 

若 eno1
 不存在,替换为实际接口(如 eth0
 或 enp0s3
)。

GPU 驱动和 CUDA

  • • 安装 NVIDIA 驱动(版本 ≥ 470)和 CUDA Toolkit
    (推荐 12.x):
nvidia-smi  # 确认驱动和 CUDA 版本

输出应显示 英伟达和 CUDA 版本(如 12.4)。

若未安装,参考 NVIDIA 官网 自行安装即可:

Python 环境

  • • Python 3.9+(推荐 3.10)
  • • 两节点需一致的 Python 版本:
python3 --version

磁盘空间

  • • Qwen2.5-7B-Instruct
     模型约需 15GB 磁盘空间
  • • 确保 /opt/models/Qwen/Qwen2.5-7B-Instruct
     路径有足够空间

3. 安装 SGLang

在两节点上分别安装 SGLang 和依赖。以下步骤在每台电脑上执行。

3.1 创建虚拟环境(conda)

conda create -n sglang_env python=3.10
conda activate  sglang_env

3.2 安装 SGLang

备注: 安装过程会自动安装 对应显卡相关的依赖,如 torch
transformers
flashinfer

pip install --upgrade pip
pip install uv
uv pip install "sglang[all]>=0.4.5" --find-links https://flashinfer.ai/whl/cu124/torch2.5/flashinfer-python

验证安装:

python -m sglang.launch_server --help

应显示 SGLang 的命令行参数帮助信息。

3.3 下载 Qwen2.5-7B-Instruct 模型

国外使用 huggingface
,国内使用 modelscope

在两节点上下载模型到相同路径(如 /opt/models/Qwen/Qwen2.5-7B-Instruct
):

pip install modelscope
modelscope download Qwen/Qwen2.5-7B-Instruct --local-dir opt/models/Qwen/Qwen2.5-7B-Instruct

或手动从 Hugging Face
 或者 modelscope
 下载并解压到指定路径。确保两节点模型文件一致。

4. 配置双节点部署

使用张量并行(--tp 2)将模型分布到 2 个 GPU(每节点 1 个)。以下是详细的部署步骤和命令。

4.1 部署命令

  • • 节点 0(IP: 192.168.0.12):
NCCL_IB_DISABLE=1 NCCL_P2P_DISABLE=1 GLOO_SOCKET_IFNAME=eno1 NCCL_SOCKET_IFNAME=eno1 python3 -m sglang.launch_server \
  --model-path opt/models/Qwen/Qwen2.5-7B-Instruct \
  --tp 2 \
  --nnodes 2 \
  --node-rank 0 \
  --dist-init-addr 192.168.0.12:50000 \
  --disable-cuda-graph \
  --host 0.0.0.0 \
  --port 30000 \
  --mem-fraction-static 0.7

  • • 节点 1(IP: 192.168.0.13):
NCCL_IB_DISABLE=1 NCCL_P2P_DISABLE=1 GLOO_SOCKET_IFNAME=eno1 NCCL_SOCKET_IFNAME=eno1 python3 -m sglang.launch_server \
  --model-path opt/models/Qwen/Qwen2.5-7B-Instruct \
  --tp 2 \
  --nnodes 2 \
  --node-rank 1 \
  --dist-init-addr 192.168.0.12:50000 \
  --disable-cuda-graph \
  --host 0.0.0.0 \
  --port 30000 \
  --mem-fraction-static 0.7

注意: 如果出现 OOM的情况则调整 --mem-fraction-static
 参数,默认是 0.9,改为 0.7 即可。0.9 调整到0.7 时 当前7B模型 占用显存直接下降 2G左右。
CUDA Graph 会额外分配少量显存(通常几百 MB)来存储计算图。如果显存接近上限,启用 CUDA Graph 可能触发 OOM 错误。

4.2 参数说明

以下是命令中每个参数的详细解释,结合你的场景:

环境变量

  • • NCCL_IB_DISABLE=1
    :禁用 InfiniBand,因为你的节点通过以太网通信
  • • NCCL_P2P_DISABLE=1
    :禁用 GPU 间的 P2P 通信(如 NVLink),因为 GPU 在不同电脑上
  • • GLOO_SOCKET_IFNAME=eno1
    :指定 GLOO 分布式通信的网络接口(替换为实际接口,如 eth0)
  • • NCCL_SOCKET_IFNAME=eno1
    :指定 NCCL 通信的网络接口

SGLang 参数

  • • --model-path opt/models/Qwen/Qwen2.5-7B-Instruct
    :模型权重路径,两节点必须一致
  • • --tp 2
    :张量并行,使用 2 个 GPU(每节点 1 个)。模型权重和计算任务平分到 2 个 GPU
  • • --nnodes 2
    :指定 2 个节点。tp_size (2) ÷ nnodes (2) = 1 GPU 每节点,满足整除要求
  • • --node-rank 0 --node-rank 1
    :节点编号,节点 0 和 1 分别设置为 0 和 1
  • • --dist-init-addr 192.168.0.12:50000
    :分布式初始化地址,指向节点 0 的 IP 和端口,两节点一致
  • • --disable-cuda-graph
    :禁用 CUDA 图
  • • --trust-remote-code
    :允许加载 Qwen 模型的远程代码(Hugging Face 模型必需),模型下载好的直接不用这个参数即可
  • • --host 0.0.0.0
    :服务器监听所有网络接口,允许外部访问
  • • --port 30000
    :HTTP 服务器端口,可根据需要调整(若冲突,节点 1 可设为 --port 30001)
  • • --mem-fraction-static 0.7
    :KV 缓存池占用 70% 显存(默认 0.9),降低显存占用以适配 低显存显卡

如果选择量化

使用 --quantization
 或 --torchao-config
SGLang
 支持以下基于 torchao
 的量化方法。["int8dq", "int8wo", "fp8wo", "fp8dq-per_tensor", "fp8dq-per_row", "int4wo-32", "int4wo-64", "int4wo-128", "int4wo-256"]

请注意:使用--quantization fp8
量化,PyTorch 的 FP8(8 位浮点)量化功能(依赖 torch._scaled_mm)要求 GPU 的 CUDA 计算能力(Compute Capability)达到 8.9 或 9.0
 以上,或者使用 AMD ROCm MI300+ 
架构.因此如果你的显卡不咋地则不建议使用这个参数。

  • • 节点 0(IP: 192.168.0.12):
NCCL_IB_DISABLE=1 NCCL_P2P_DISABLE=1 GLOO_SOCKET_IFNAME=eno1 NCCL_SOCKET_IFNAME=eno1 python3 -m sglang.launch_server \
  --model-path opt/models/Qwen/Qwen2.5-7B-Instruct \
  --tp 2 \
  --nnodes 2 \
  --node-rank 0 \
  --dist-init-addr 192.168.0.12:50000 \
  --disable-cuda-graph \
  --torchao-config int4wo-32 \
  --host 0.0.0.0 \
  --port 30000 \
  --mem-fraction-static 0.7

  • • 节点 1(IP: 192.168.0.13):
NCCL_IB_DISABLE=1 NCCL_P2P_DISABLE=1 GLOO_SOCKET_IFNAME=eno1 NCCL_SOCKET_IFNAME=eno1 python3 -m sglang.launch_server \
  --model-path opt/models/Qwen/Qwen2.5-7B-Instruct \
  --tp 2 \
  --nnodes 2 \
  --node-rank 1 \
  --dist-init-addr 192.168.0.12:50000 \
  --disable-cuda-graph \
  --torchao-config int4wo-32 \
  --host 0.0.0.0 \
  --port 30000 \
  --mem-fraction-static 0.7

4.3 部署完成

如下在主节点出现类似下面日志则启动成功,如果有异常 则自己看日志排查

[2025-04-17 14:50:24 TP0] Attention backend not set. Use flashinfer backend by default.
[2025-04-17 14:50:24 TP0] Disable chunked prefix cache for non-MLA backend.
[2025-04-17 14:50:24 TP0] Init torch distributed begin.
[2025-04-17 14:50:32 TP0] sglang is using nccl==2.21.5
[2025-04-17 14:50:32 TP0] Custom allreduce is disabled because this process group spans across nodes.
[2025-04-17 14:50:32 TP0] Init torch distributed ends. mem usage=0.11 GB
[2025-04-17 14:50:32 TP0] Load weight begin. avail mem=11.44 GB
Loading safetensors checkpoint shards:   0% Completed | 0/4 [00:00<?, ?it/s]
Loading safetensors checkpoint shards:  25% Completed | 1/4 [00:00<00:01,  2.71it/s]
.....
[2025-04-17 14:50:34 TP0] Load weight end. type=Qwen2ForCausalLM, dtype=torch.bfloat16, avail mem=4.22 GB, mem usage=7.22 GB.
[2025-04-17 14:50:34 TP0] KV Cache is allocated. #tokens: 29357, K size: 0.39 GB, V size: 0.39 GB
[2025-04-17 14:50:34 TP0] Memory pool end. avail mem=3.09 GB
...
[2025-04-17 14:50:36] INFO:     Uvicorn running on http://0.0.0.0:30000 (Press CTRL+C to quit)
[2025-04-17 14:50:37] INFO:     127.0.0.1:32902 - "GET get_model_info HTTP/1.1" 200 OK
[2025-04-17 14:50:37 TP0] Prefill batch. #new-seq: 1, #new-token: 6, #cached-token: 0, token usage: 0.00, #running-req: 0, #queue-req: 0, 
[2025-04-17 14:50:39] INFO:     127.0.0.1:32908 - "POST generate HTTP/1.1" 200 OK
[2025-04-17 14:50:39] The server is fired up and ready to roll!

从主节点机器访问服务器, 查看模型信息,至此部署完成。

curl http://192.168.0.12:30000/v1/models

访问测试

curl http://192.168.0.12:30000/v1/chat/completions   -H "Content-Type: application/json"   -d '{
      "model": "Qwen2.5-7B-Instruct", 
      "messages": [{
          "role": "user", 
          "content": "你是谁?"
      }], 
      "temperature": 0.3
  }'


 


文章转载自技术源share,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论