PD 分离#

什么是 PD 分离,为什么要使用它?#

大语言模型 (LLM) 推理包含两个不同的阶段:Prefill (预填充)Decode (解码)。Prefill 阶段是计算密集型的,处理整个输入序列;而 Decode 阶段是内存密集型的,管理用于逐个生成 token 的 Key-Value (KV) 缓存。传统上,这两个阶段在统一的引擎中处理,混合调度 prefill 和 decode 批次会导致效率低下。为了解决这些挑战,我们在 SGLang 中引入了 Prefill 和 Decoding (PD) 分离

统一调度存在的问题#

传统的统一引擎同时处理 prefill 和 decode 批次,这会导致两个显著问题:

  1. Prefill 中断:新进入的 prefill 批次经常中断正在进行的 decode 批次,导致 token 生成出现大幅延迟。

  2. DP Attention 不平衡:在数据并行 (DP) 注意力机制中,一个 DP 工作进程可能正在处理 prefill 批次,而另一个同时处理 decode 批次,导致解码延迟增加。

PD 分离通过将这两个阶段分开来解决这些问题,从而能够为每个阶段进行针对性优化。

有关设计详情,请参阅 链接

目前,我们支持 Mooncake 和 NIXL 作为传输引擎。

在 PD 分离模式下进行性能分析 (Profiling)#

当您需要在 PD 分离模式下对 prefill 或 decode 工作进程进行性能分析时,请参阅“基准测试与性能分析”指南中的 在 PD 分离模式下进行性能分析 章节。由于 torch profiler 的限制,必须使用专用的命令行选项分别对 prefill 和 decode 工作进程进行分析。

Router 集成#

为了在大规模部署 PD 分离并实现负载均衡和容错,SGLang 提供了一个 Router (路由器)。Router 可以使用各种路由策略在 prefill 和 decode 实例之间分配请求。有关使用 PD 分离设置路由的详细信息(包括配置选项和部署模式),请参阅 SGLang Router 文档

Mooncake#

环境要求#

uv pip install mooncake-transfer-engine

用法#

Llama 单节点#

python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode prefill \
  --port 30000 \
  --disaggregation-ib-device mlx5_roce0
python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode decode \
  --port 30001 \
  --base-gpu-id 1 \
  --disaggregation-ib-device mlx5_roce0
python -m sglang_router.launch_router --pd-disaggregation --prefill http://127.0.0.1:30000 --decode http://127.0.0.1:30001 --host 0.0.0.0 --port 8000

DeepSeek 多节点#

# prefill 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-ib-device ${device_name} \
  --disaggregation-mode prefill \
  --host ${local_ip} \
  --port 30000 \
  --trust-remote-code \
  --dist-init-addr ${prefill_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 0 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8
# prefill 1
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-ib-device ${device_name} \
  --disaggregation-mode prefill \
  --host ${local_ip} \
  --port 30000 \
  --trust-remote-code \
  --dist-init-addr ${prefill_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 1 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8
# decode 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-ib-device ${device_name} \
  --disaggregation-mode decode \
  --host ${local_ip} \
  --port 30001 \
  --trust-remote-code \
  --dist-init-addr ${decode_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 0 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8 \
  --max-running-requests 128
# decode 1
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-ib-device ${device_name} \
  --disaggregation-mode decode \
  --host ${local_ip} \
  --port 30001 \
  --trust-remote-code \
  --dist-init-addr ${decode_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 1 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8 \
  --max-running-requests 128

高级配置#

带有 Mooncake 的 PD 分离支持以下环境变量,用于对系统行为进行精细控制。

Prefill 服务端配置#

变量

描述

默认值

SGLANG_DISAGGREGATION_THREAD_POOL_SIZE

控制每个 TP 秩 (rank) 用于 KV Cache 传输操作的工作线程总数

int(0.75 * os.cpu_count()) // 8) 计算得出的动态值,限制在 4 到 12 之间,以确保效率并防止线程竞态条件

SGLANG_DISAGGREGATION_QUEUE_SIZE

设置并行传输队列的数量。来自多个 decode 实例的 KV Cache 传输请求将分片到这些队列中,以便它们可以同时共享线程和传输带宽。如果设置为 1,则根据 FCFS(先来先服务)策略逐个传输请求

4

SGLANG_DISAGGREGATION_BOOTSTRAP_TIMEOUT

请求初始化期间接收目标 KV 索引的超时时间(秒)

300

如果可以接受较大的平均 TTFT,可以执行 export SGLANG_DISAGGREGATION_BOOTSTRAP_TIMEOUT=600(10 分钟)以放宽超时条件。请注意,此设置会导致当运行中的 decode 节点断开连接时,prefill 实例需要更长时间来清理受影响的内存资源。

Decode 服务端配置#

变量

描述

默认值

SGLANG_DISAGGREGATION_HEARTBEAT_INTERVAL

向 prefill 引导服务器进行健康检查的间隔时间(秒)

5.0

SGLANG_DISAGGREGATION_HEARTBEAT_MAX_FAILURE

在标记 prefill 服务器离线之前的连续心跳失败次数

2

SGLANG_DISAGGREGATION_WAITING_TIMEOUT

请求初始化后接收 KV Cache 的超时时间(秒)

300

如果可以接受较大的平均 TTFT,可以执行 export SGLANG_DISAGGREGATION_WAITING_TIMEOUT=600(10 分钟)以放宽超时条件。

NIXL#

环境要求#

通过 pip 安装。

pip install nixl

或从源码构建 - 如果您已经安装了 UCX,可能需要这样做。

git clone https://github.com/ai-dynamo/nixl.git
cd nixl
pip install . --config-settings=setup-args="-Ducx_path=/path/to/ucx"

使用方法#

Llama 单节点#

python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode prefill \
  --port 30000 \
  --disaggregation-transfer-backend nixl
python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode decode \
  --port 30001 \
  --base-gpu-id 1 \
  --disaggregation-transfer-backend nixl
python -m sglang_router.launch_router --pd-disaggregation --prefill http://127.0.0.1:30000 --decode http://127.0.0.1:30001 --host 0.0.0.0 --port 8000

DeepSeek 多节点#

# prefill 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend nixl \
  --disaggregation-mode prefill \
  --host ${local_ip} \
  --port 30000 \
  --trust-remote-code \
  --dist-init-addr ${prefill_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 0 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8
# prefill 1
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend nixl \
  --disaggregation-mode prefill \
  --host ${local_ip} \
  --port 30000 \
  --trust-remote-code \
  --dist-init-addr ${prefill_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 1 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8
# decode 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend nixl \
  --disaggregation-mode decode \
  --host ${local_ip} \
  --port 30001 \
  --trust-remote-code \
  --dist-init-addr ${decode_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 0 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8 \
  --max-running-requests 128
# decode 1
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend nixl \
  --disaggregation-mode decode \
  --host ${local_ip} \
  --port 30001 \
  --trust-remote-code \
  --dist-init-addr ${decode_master_ip}:5000 \
  --nnodes 2 \
  --node-rank 1 \
  --tp-size 16 \
  --dp-size 8 \
  --enable-dp-attention \
  --moe-a2a-backend deepep \
  --mem-fraction-static 0.8 \
  --max-running-requests 128

昇腾 (ASCEND)#

使用方法#

通过设置 ASCEND_MF_STORE_URL 并使用 mf_adapter (下载链接) 来配合 ascend 后端使用

pip install mf_adapter-1.0.0-cp311-cp311-linux_aarch64.whl --force-reinstall
export ASCEND_MF_STORE_URL="tcp://xxx.xx.xxx.xxx:xxxx"

使用 mooncake 后端,更多详情可以在 mooncake 章节中找到。

export ENABLE_ASCEND_TRANSFER_WITH_MOONCAKE=true

需要在容器环境变量中设置 ASCEND_NPU_PHY_ID

export ASCEND_NPU_PHY_ID=xxx

Llama 单节点#

python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode prefill \
  --port 30000 \
  --disaggregation-transfer-backend ascend
python -m sglang.launch_server \
  --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode decode \
  --port 30001 \
  --base-gpu-id 1 \
  --disaggregation-transfer-backend ascend
python -m sglang_router.launch_router --pd-disaggregation --prefill http://127.0.0.1:30000 --decode http://127.0.0.1:30001 --host 0.0.0.0 --port 8000

DeepSeek 多节点#

# prefill 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend ascend \
  --disaggregation-mode prefill \
  --host ${local_ip} \
  --port 30000 \
  --trust-remote-code \
  --dist-init-addr ${prefill_master_ip}:5000 \
  --nnodes 1 \
  --node-rank 0 \
  --tp-size 16
# decode 0
python -m sglang.launch_server \
  --model-path deepseek-ai/DeepSeek-V3-0324 \
  --disaggregation-transfer-backend ascend \
  --disaggregation-mode decode \
  --host ${local_ip} \
  --port 30001 \
  --trust-remote-code \
  --dist-init-addr ${decode_master_ip}:5000 \
  --nnodes 1 \
  --node-rank 0 \
  --tp-size 16