十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

SGLang 部署 DeepSeek V3/V3.1/R1 实战指南:FP8 推理、DP Attention 优化与多节点集群配置

SGLang 部署 DeepSeek V3/V3.1/R1 实战指南:FP8 推理、DP Attention 优化与多节点集群配置 SGLang 部署 DeepSeek V3/V3.1/R1 实战指南FP8 推理、DP Attention 优化与多节点集群配置【免费下载链接】sglangSGLang is a high-performance serving framework for large language models and multimodal models.项目地址: https://gitcode.com/GitHub_Trending/sg/sglang本篇指南以 benchmark/deepseek_v3/README.md 为核心系统讲解如何使用 SGLang 从零启动 DeepSeek V3 / V3.1 / R1 系列模型的服务端覆盖 Docker 与 pip 两种安装方式、MLA 与 DP Attention 等性能优化开关、OpenAI 兼容接口的调用含 V3.1 的 thinking / non-thinking 逐请求切换以及 2×H20、B200、2×H200、4×A100、多节点 int8/AWQ 量化部署等真实场景的完整命令行。读完本文你将能够根据手头 GPU 资源与精度需求直接复制出一套可运行的 DeepSeek 推理服务并对底层模型实现与参数生效位置有源码级的认识。DeepSeek 系列在 SGLang 中的支持概况SGLang 与 DeepSeek 团队合作在FP8 权重NVIDIA 与 AMD GPU上做到了发布首日即可运行DeepSeek V3。在此基础上SGLang 还内置了两项针对 DeepSeek 模型的关键优化MLAMulti-head Latent Attention优化DeepSeek 系列采用 MLA 注意力机制以压缩 KV 缓存SGLang 对其做了针对性优化默认开启DP AttentionData Parallelism Attention将注意力部分做数据并行、FFN 部分做张量并行用于高 QPS 场景下提升吞吐可选开启。官方 DeepSeek 团队也将 SGLang 列为推荐的推理引擎。该模型的实现与优化工作由美团搜索与推荐平台团队、Baseten 模型性能团队共同完成DataCrunch 提供了 GPU 资源支持。关于 DeepSeek 系列模型在 SGLang 中的详细优化清单如 MLA、CUDA graph、DeepGEMM、EP 等请参阅官方文档中 “DeepSeek V3/V3.1/R1 Model Optimizations in SGLang” 一节仓库侧的实现与参数定义可对照 python/sglang/srt/models/deepseek_v2.py 等源码阅读详见本文最后一节。安装与启动在开始之前有一个重要提醒如果在启动服务器时遇到报错请先确认权重已经完整下载。建议提前下载权重或在启动失败后多次重启直到所有权重就绪。使用 Docker推荐# 拉取最新镜像 docker pull lmsysorg/sglang:latest # 启动服务 docker run --gpus all --shm-size 32g -p 30000:30000 -v ~/.cache/huggingface:/root/.cache/huggingface --ipchost --networkhost --privileged lmsysorg/sglang:latest \ python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3 --tp 8 --trust-remote-code --port 30000几个关键参数说明--gpus all暴露全部 GPU 给容器--shm-size 32gDeepSeek 这类超大模型的 KV cache 与张量并行的共享内存需求较高需要调大/dev/shm-v ~/.cache/huggingface:/root/.cache/huggingface挂载本机 Hugging Face 权重缓存避免重复下载--ipchost --networkhost --privilegedRDMAInfiniBand/RoCE场景必需不使用 RDMA 时可移除--networkhost与--privileged--tp 88 卡张量并行DeepSeek V3 671B 的 FP8 版本需要 8×80GB 级别显存--port 30000服务端口客户端将访问http://127.0.0.1:30000/v1。如果使用 RoCE 网络可能还需要设置export NCCL_IB_GID_INDEX3。按需追加性能优化选项中的参数。使用 pip# 安装 pip install sglang # 启动 python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V3 --tp 8 --trust-remote-code按需追加性能优化选项中的参数。从源码看python/sglang/launch_server.py 是python -m sglang.launch_server的入口它解析sys.argv[1:]得到ServerArgs随后根据encoder_only、gRPC/Ray 等标志分发到对应的launch_server默认走 python/sglang/srt/entrypoints/http_server.py 的 HTTP 模式。该文件在运行时还会提示python -m sglang.launch_server仍然受支持但更推荐使用sglang serve --model-path model [options]作为统一入口。性能优化选项Performance Optimization OptionsMLA 优化默认开启无需额外参数。以下是两个可选的优化开关优化项适用场景参数说明Data Parallelism Attention高 QPS 场景--enable-dp-attention注意力走数据并行、FFN 走张量并行提升吞吐Torch.compile 优化追求更低的 kernel 延迟--enable-torch-compile启动时编译模型会消耗一定启动时间Torch.compile 批次上限配合 torch.compile 使用--torch-compile-max-bs建议取值 18例如--torch-compile-max-bs 8从源码确认参数行为--enable-dp-attention的定义位于 python/sglang/srt/arg_groups/fields/parallel.py其帮助文本明确指出该开关为注意力启用数据并行、为 FFN 启用张量并行DP 尺寸需等于 TP 尺寸dp_size tp_size当前支持 DeepSeek-V2 与 Qwen 2/3 的 MoE 模型DeepSeek V3/V3.1/R1 的 MLA 架构继承自 V2同样适用。--enable-torch-compile与--torch-compile-max-bs定义于 python/sglang/srt/arg_groups/fields/exec_.py其中torch_compile_max_bs的默认值为32文档建议在 18 之间取值以获得更稳定的编译行为。用法与 DeepSeek 对话DeepSeek V3 / R1服务启动后使用 OpenAI 兼容接口即可调用model参数可填defaultimport openai client openai.Client( base_urlhttp://127.0.0.1:30000/v1, api_keyEMPTY) # Chat completion response client.chat.completions.create( modeldefault, messages[ {role: system, content: You are a helpful AI assistant}, {role: user, content: List 3 countries and their capitals.}, ], temperature0, max_tokens64, ) print(response)DeepSeek V3.1请求级思考开关DeepSeek V3.1 在基础用法之上支持逐请求request-level切换 thinking / non-thinking 模式。通过extra_body{chat_template_kwargs: {thinking: True/False}}即可控制。非思考模式Non Thinkingimport openai client openai.Client( base_urlhttp://127.0.0.1:30000/v1, api_keyEMPTY) # Chat completion response client.chat.completions.create( modeldefault, messages[ {role: system, content: You are a helpful AI assistant}, {role: user, content: Answer the following with the second letter of the correct answer only: What is the capital of France?}, ], temperature0, max_tokens1024, extra_body {chat_template_kwargs: {thinking: False}} ) print(response.choices[0].message.content)输出示例h说明该问题的正确答案是 “Paris”正确做法是输出 “Paris” 的第二个字母 “A”但 non-thinking 模式下模型直接按字面要求给出了错误答案h恰好演示了思考模式的必要性。思考模式Thinkingimport openai client openai.Client( base_urlhttp://127.0.0.1:30000/v1, api_keyEMPTY) # Chat completion response client.chat.completions.create( modeldefault, messages[ {role: system, content: You are a helpful AI assistant}, {role: user, content: Answer the following with the second letter of the correct answer only: What is the capital of France?}, ], temperature0, max_tokens1024, extra_body {chat_template_kwargs: {thinking: True}} ) print(response)输出示例截取关键部分First, the question is: What is the capital of France? I know that the capital of France is Paris. ... So, the second letter is A. ... Finally, I need to make sure that this is the correct answer. Yes, Paris is indeed the capital of France./thinkA可见思考模式下响应中包含完整的/think推理轨迹模型能够据此推导出正确答案 “A”——这正是 reasoning 模型的核心价值把推理过程显式展开后给出结果。多节点 / 多卡部署实战以下示例均来自仓库的 benchmark/deepseek_v3/README.md覆盖从 4 卡到 32 卡、从 FP8/BF16 到 AWQ/int8 量化的多种部署形态。示例2 个 H20×8 节点假设有两个 H20 节点各含 8 张 GPU。节点 1 的 IP 为10.0.0.1节点 2 的 IP 为10.0.0.2。两个节点都要使用节点 1 的 IP作为--dist-init-addr。若命令执行失败尝试设置GLOO_SOCKET_IFNAME环境变量参见 PyTorch 分布式常用环境变量说明若多节点使用 NVIDIA InfiniBand 且启动时挂起可考虑增加export NCCL_IB_GID_INDEX3。# node 1 python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 10.0.0.1:5000 --nnodes 2 --node-rank 0 --trust-remote-code # node 2 python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 10.0.0.1:5000 --nnodes 2 --node-rank 1 --trust-remote-code参数速查--tp 16为跨 2 节点的 16 路张量并行--dist-init-addr IP:PORT指定分布式初始化的主节点地址--nnodes 2与--node-rank 0/1分别声明节点总数与当前节点序号。两个 H100 节点的用法与上述 H20 一致。注意上面的启动命令没有开启 DP Attention 与 torch.compile 优化。追求最优性能时请参考性能优化选项中的参数组合。示例1 个 B200 节点FP4 / FP8单个 B200 节点可以配置 4 张仅 FP4或 8 张FP4/FP8 均可GPU。DeepSeek R1 同时支持 FP4 与 FP8 模型两者的最优参数略有差异。FP44 卡配置python3 -m sglang.launch_server --model-path nvidia/DeepSeek-R1-0528-FP4-V2 --host 0.0.0.0 --port 8000 --tensor-parallel-size4 --cuda-graph-max-bs-decode 256 --max-running-requests 256 --mem-fraction-static 0.85 --ep-size 4 --scheduler-recv-interval 30 --enable-symm-mem --stream-interval 108 卡配置python3 -m sglang.launch_server --model-path nvidia/DeepSeek-R1-0528-FP4-V2 --host 0.0.0.0 --port 8000 --tensor-parallel-size8 --cuda-graph-max-bs-decode 256 --max-running-requests 256 --mem-fraction-static 0.85 --ep-size 8 --scheduler-recv-interval 30 --enable-symm-mem --stream-interval 10FP8SGLANG_ENABLE_JIT_DEEPGEMMfalse python3 -m sglang.launch_server --model-pathdeepseek-ai/DeepSeek-R1-0528 --host0.0.0.0 --port8000 --tensor-parallel-size8 --cuda-graph-max-bs-decode 128 --max-running-requests 128 --mem-fraction-static 0.82 --kv-cache-dtype fp8_e4m3 --chunked-prefill-size 32768 --max-prefill-tokens 32768 --scheduler-recv-interval 30 --stream-interval 30 --fp8-gemm-backend flashinfer_trtllm关键参数解读对应源码中的参数定义见 python/sglang/srt/arg_groups/fields/exec_.py 与模型/调度参数组--cuda-graph-max-bs-decodedecode 阶段 CUDA graph 捕获的最大 batch 大小需与--max-running-requests匹配避免频繁回退到 eager 模式--max-running-requests并发运行请求数上限含等待中的请求--mem-fraction-static为模型权重与 KV cache 预留的静态显存比例0.820.85 表示预留 82%85%--ep-sizeMoE 专家并行Expert Parallelism的专家分组大小FP4 下与 TP 一致--kv-cache-dtype fp8_e4m3KV cache 使用 FP8e4m3存储显著降低显存占用--chunked-prefill-size/--max-prefill-tokenschunked prefill 的块大小与单次 prefill token 上限--scheduler-recv-interval/--stream-interval调度器接收间隔与流式输出间隔毫秒级控制--enable-symm-mem启用对称内存分配面向 B200 这类设备的内存特性--fp8-gemm-backend flashinfer_trtllm指定 FP8 GEMM 后端实现SGLANG_ENABLE_JIT_DEEPGEMMfalse关闭 DeepGEMM 的 JIT 编译路径改用上述显式指定的 GEMM 后端。示例2 个 H200×8 节点 Docker两个 H200 节点各含 8 张 GPUIP 分别为192.168.114.10与192.168.114.11。使用--host 0.0.0.0与--port 40000将服务暴露给其他容器并用--dist-init-addr 192.168.114.10:20000建立节点间通信。单个 H200 的 8 卡即可运行 DeepSeek V3双 H200 配置只是为了演示多节点用法。# node 1 docker run --gpus all \ --shm-size 32g \ --networkhost \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --name sglang_multinode1 \ -it \ --rm \ --env HF_TOKEN$HF_TOKEN \ --ipchost \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 192.168.114.10:20000 --nnodes 2 --node-rank 0 --trust-remote-code --host 0.0.0.0 --port 40000# node 2 docker run --gpus all \ --shm-size 32g \ --networkhost \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --name sglang_multinode2 \ -it \ --rm \ --env HF_TOKEN$HF_TOKEN \ --ipchost \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server --model-path deepseek-ai/DeepSeek-V3 --tp 16 --dist-init-addr 192.168.114.10:20000 --nnodes 2 --node-rank 1 --trust-remote-code --host 0.0.0.0 --port 40000为验证服务可用从第三个客户端容器发起一次基准请求调用仓库自带的sglang.bench_serving构造 1 个 prompt、输入 1 token、输出 512 tokendocker run --gpus all \ --shm-size 32g \ --networkhost \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --name sglang_multinode_client \ -it \ --rm \ --env HF_TOKEN$HF_TOKEN \ --ipchost \ lmsysorg/sglang:latest \ python3 -m sglang.bench_serving --backend sglang --dataset-name random --random-input 1 --random-output 512 --random-range-ratio 1 --num-prompts 1 --host 0.0.0.0 --port 40000 --output-file deepseekv3_multinode.jsonl注意上面的启动命令没有开启 DP Attention 与 torch.compile 优化。追求最优性能时请参考性能优化选项中的参数组合。示例4 个 A100×8 节点BF16A100 不支持原生 FP8 计算因此需要先将 FP8 权重转换为 BF16。原文档推荐使用 DeepSeek 官方仓库提供的fp8_cast_bf16.py脚本完成转换。由于 BF16 模型体积超过 1.3 TB需要准备 4 个节点、每个节点 8 张 80GB A100。假设节点 1 的 IP 为10.0.0.1转换后的模型路径为/path/to/DeepSeek-V3-BF16# node 1 python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 0 --trust-remote-code --host 0.0.0.0 --port 30000 # node 2 python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 1 --trust-remote-code # node 3 python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 2 --trust-remote-code # node 4 python3 -m sglang.launch_server --model-path /path/to/DeepSeek-V3-BF16 --tp 32 --dist-init-addr 10.0.0.1:5000 --nnodes 4 --node-rank 3 --trust-remote-code注意上面的启动命令没有开启 DP Attention 与 torch.compile 优化。追求最优性能时请参考性能优化选项中的参数组合。随后访问节点 1 暴露的端口对精度与延迟进行基准测试# 精度基准GSM8K1319 题 python3 benchmark/gsm8k/bench_sglang.py --num-questions 1319 --host 10.0.0.1 --port 30000 # 延迟基准单请求输入输出各 128 token python3 -m sglang.bench_one_batch_server --model None --base-url http://10.0.0.1:30000 --batch-size 1 --input-len 128 --output-len 128上述两个基准脚本都位于仓库内GSM8K 精度测试见 benchmark/gsm8k/bench_sglang.py单 batch 延迟测试对应sglang.bench_one_batch_server模块python/sglang/bench_one_batch_server.py。示例8/16 卡 A100/A800 的 AWQ 量化部署推荐用法添加--quantization moe_wna16启用 MoE WNA16 kernel可获得更好的性能python3 -m sglang.launch_server --model cognitivecomputations/DeepSeek-R1-AWQ --tp 8 --trust-remote-code --quantization moe_wna16备选用法使用awq_marlin后端python3 -m sglang.launch_server --model cognitivecomputations/DeepSeek-R1-AWQ --tp 8 --trust-remote-code --quantization awq_marlin --dtype float16注意awq_marlin目前仅支持float16可能带来一定精度损失。示例16 卡 A100/A800 的 int8 量化部署int8 量化分为block-wise块级与per-channel逐通道两种方法量化参数已上传至 Hugging Face例如meituan/DeepSeek-R1-Block-INT8与meituan/DeepSeek-R1-Channel-INT8等社区模型仓库。假设主节点 IP 为MASTER_IP权重路径为/path/to/DeepSeek-R1-INT8端口为 5000# master python3 -m sglang.launch_server \ --model meituan/DeepSeek-R1-Block-INT8 --tp 16 --dist-init-addr \ MASTER_IP:5000 --nnodes 2 --node-rank 0 --trust-remote-code --enable-torch-compile --torch-compile-max-bs 8 # cluster python3 -m sglang.launch_server \ --model meituan/DeepSeek-R1-Block-INT8 --tp 16 --dist-init-addr \ MASTER_IP:5000 --nnodes 2 --node-rank 1 --trust-remote-code --enable-torch-compile --torch-compile-max-bs 8注意上面的启动命令开启了 torch.compile 优化。追求最优性能时请参考性能优化选项中的参数组合。然后在master 节点上假设 ShareGPT 数据集位于/path/to/ShareGPT_V3_unfiltered_cleaned_split.json执行基准# 精度基准GSM8K1319 题 python3 benchmark/gsm8k/bench_sglang.py --num-questions 1319 # 服务吞吐基准1000 个 prompt请求速率 128 QPS python3 -m sglang.bench_serving --dataset-path /path/to/ShareGPT_V3_unfiltered_cleaned_split.json --dataset-name random --random-input 128 --random-output 128 --num-prompts 1000 --request-rate 128 --random-range-ratio 1.0提示在精度基准命令中增加--parallel 200可显著加速评测过程。示例32 卡 L40S 的 int8 量化部署使用 per-channel 量化模型例如meituan/DeepSeek-R1-Channel-INT8等社区模型仓库主节点 IP 为MASTER_IP端口为 5000# master python3 -m sglang.launch_server --model meituan/DeepSeek-R1-Channel-INT8 --tp 32 --quantization w8a8_int8 \ --dist-init-addr MASTER_IP:5000 --nnodes 4 --node-rank 0 --trust-remote \ --enable-torch-compile --torch-compile-max-bs 32 # cluster python3 -m sglang.launch_server --model meituan/DeepSeek-R1-Channel-INT8 --tp 32 --quantization w8a8_int8 \ --dist-init-addr MASTER_IP:5000 --nnodes 4 --node-rank 1 --trust-remote \ --enable-torch-compile --torch-compile-max-bs 32 python3 -m sglang.launch_server --model meituan/DeepSeek-R1-Channel-INT8 --tp 32 --quantization w8a8_int8 \ --dist-init-addr MASTER_IP:5000 --nnodes 4 --node-rank 2 --trust-remote \ --enable-torch-compile --torch-compile-max-bs 32 python3 -m sglang.launch_server --model meituan/DeepSeek-R1-Channel-INT8 --tp 32 --quantization w8a8_int8 \ --dist-init-addr MASTER_IP:5000 --nnodes 4 --node-rank 3 --trust-remote \ --enable-torch-compile --torch-compile-max-bs 32基准测试方法与上文“16 卡 A100/A800 int8”一节完全相同--quantization w8a8_int8表示 8-bit 权重 × 8-bit 激活。示例任意云 / Kubernetes 上的 SkyPilot 部署SkyPilot 可以在任意云厂商或现有 Kubernetes 集群中寻找性价比最高的 GPU并用单条命令拉起分布式推理服务git clone skypilot 仓库 # 2 个 H100/H200×8 节点 sky launch -c r1 llm/deepseek-r1/deepseek-r1-671B.yaml --retry-until-up # 4 个 A100×8 节点 sky launch -c r1 llm/deepseek-r1/deepseek-r1-671B-A100.yaml --retry-until-up上述 YAML 位于 SkyPilot 仓库的llm/deepseek-r1/目录下可自行查阅。示例4 卡 H200 的 W4A-FP8 混合精度部署W4A-FP8 是一种混合精度量化方案MoE 层使用 W4(int)A(FP)8 计算dense 层保持 FP8 精度。预量化权重已在社区模型仓库中发布例如novita/Deepseek-V3-0324-W4AFP84×H200或潜在的 8×H100即可高效运行python -m sglang.launch_server --model novita/Deepseek-V3-0324-W4AFP8 --mem-fraction-static 0.85 --disable-shared-experts-fusion --tp-size 4其中--disable-shared-experts-fusion用于关闭共享专家shared experts的融合优化——在 W4A-FP8 这类混合精度量化下关闭融合往往能获得更稳定的行为。其他可用的预量化变体还包括社区模型仓库名novita/Deepseek-V3.1-W4AFP8、novita/Deepseek-R1-0528-W4AFP8、novita/Deepseek-R1-W4AFP8、novita/Deepseek-V3-0324-W4AFP8。常见问题排查Troubleshooting如果在 fp16/bf16 checkpoint 上遇到如下报错ValueError: Weight output_partition_size 576 is not divisible by weight quantization block_n 128.请编辑模型的config.json删除其中的quantization_config块例如移除如下内容quantization_config: { activation_scheme: dynamic, fmt: e4m3, quant_method: fp8, weight_block_size: [128, 128] },删除该块通常即可解决报错。其原因是当 checkpoint 实际为 fp16/bf16 权重、但config.json中残留 FP8 量化描述时张量并行切分output_partition_size与量化块大小block_n128无法整除导致权重加载阶段校验失败。附源码速览与 DeepSeek V3 优化计划为了便于深入阅读这里汇总本文涉及的关键源码位置服务入口python/sglang/launch_server.py ——python -m sglang.launch_server的参数解析与分发逻辑官方推荐sglang serve作为统一命令并行参数组DP Attention / EPpython/sglang/srt/arg_groups/fields/parallel.py ——--enable-dp-attention、--ep-size、--dp-size等定义其中 DP Attention 要求dp_size tp_size执行与编译参数组CUDA graph / torch.compilepython/sglang/srt/arg_groups/fields/exec_.py ——--cuda-graph-max-bs-decode、--enable-torch-compile、--torch-compile-max-bs默认 32等模型实现python/sglang/srt/models/deepseek_v2.py —— 包含DeepseekV2MoE第 543 行附近、DeepseekV2AttentionMLA第 1707 行附近、DeepseekV2Model第 2575 行附近与DeepseekV2ForCausalLM第 2883 行附近等核心类仓库中另有 python/sglang/srt/models/deepseek.py、deepseek_v4.py、deepseek_nextn.py等模型文件基准脚本benchmark/gsm8k/bench_sglang.py、python/sglang/bench_one_batch_server.py、python/sglang/bench_serving.py优化路线图DeepSeek V3 在 SGLang 中的优化计划以公开 issue编号 2591形式持续跟踪涵盖上述 MLA、DP Attention、CUDA graph、量化等方向的落地进展感兴趣的读者可在仓库历史与讨论中查看。结语从单机 Docker 到 4 节点 32 卡从 FP8 到 AWQ/int8/W4A-FP8 混合精度SGLang 为 DeepSeek V3 / V3.1 / R1 提供了覆盖绝大多数生产形态的部署路径。结合 benchmark/deepseek_v3/README.md 中的现成命令与仓库源码中的参数定义你可以按“先跑通 → 再加优化开关 → 再按显存与精度预算选量化方案”的顺序快速搭建出满足自身吞吐与精度要求的 DeepSeek 推理服务。【免费下载链接】sglangSGLang is a high-performance serving framework for large language models and multimodal models.项目地址: https://gitcode.com/GitHub_Trending/sg/sglang创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表