细粒度张量并行¶
特性介绍¶
Fine-Grained Tensor Parallelism (Fine-grained TP) extends standard tensor parallelism with independent tensor-parallel sizes for different model components — embedding, LM head, attention output projection (o_proj), MLP blocks, and QKV input projection (QKV IProj) — configured via the finegrained_tp_config parameter. Instead of a single global tensor_parallel_size, each component is sharded along the data-parallel (DP) axis with its own size, reducing per-device weight memory. The feature supports MoE models only.
在下方实测的 DeepSeek-R1-W8A8 部署中,四个开关共同节省了每卡 9.72 GB 内存,并带来了 TPOT 的净提升(参见实验结果)。在以 decode 为主的工作负载中,GEMM 受内存带宽限制,更小的单设备权重也降低了分片 GEMM 模块(LM head、o_proj 和 MLP)每步的权重读取量。
工作原理¶
All knobs shard weights over a process group built along the DP axis, and every rank of that group joins the exchange on every engine step.
- embedding / LM head(固定容量交换):无论每步实际的 token 数量是多少,交换始终以固定的、部署范围内统一的容量运行,随后裁剪掉填充部分。对于 embedding,该算子先将 token ID 填充到
max(potential_max_tokens, max_num_batched_tokens),然后执行all_gather,查找其词表分片,再通过reduce_scatter将 embedding 归还原属主——包括 prefill 规模的步。对于 LM head,runner 先将采样得到的 hidden-state 行填充到max_num_reqs * decode_query_len,然后执行all_gather → 词表分片 GEMM → all_to_all,空闲的 DP rank 通过相同容量的 dummycompute_logits参与。由于集合通信的形状从不依赖于具体的步,这些开关在 eager 模式和 graph 模式下、在 prefill 和 decode 中均可工作。 - o_proj / MLP(按步交换,要求 decode 形状的步):交换的大小由实际的步决定。MLP 执行普通的
all_gather(gate_up)/reduce_scatter(down),其形状跟随真实的 token 数量,因此进程组中的每个 rank 在每一步都必须前向相同数量的 token。o_proj 填充到静态容量——potential_max_tokens,即部署范围内的步上界(最大的 cudagraph 捕获大小,且至少为最大的 decode 形状步)——超出该容量则显式报错。这些开关仅在 graph 调度的 P/D decode 部署中受支持,此时步保持 decode 形状并在 DP 组内对齐——参见前置条件以及针对 eager 调度步的运行时保护。 - QKV IProj (per-step exchanges with padding) shards the GLM-5.3-Flash KDA Q/K/V and beta projection weights by head. Each rank pads its hidden states to a shared capacity from the DP token counts before
all_gather, including idle ranks. After projection,all_to_allrestores the full set of heads on each request's DP owner and overlaps with the local forget and gate up-projections. Each owner runs the recurrent core and keeps its convolution and recurrent state. Configure output projection TP independently withoproj_tensor_parallel_size.
使用场景¶
Fine-grained TP is designed for the decode instance of PD separation, where models are typically deployed in all-DP mode. Embedding, LM head, o_proj and MLP TP can be enabled together on a PD decode node, as measured in Experimental Results.
| 场景 | 可启用的组件 | 适用条件 |
|---|---|---|
| 使用 embedding / LM head TP 的 MoE 服务——独立实例(可选叠加标准 TP)或 PD decode 节点 | embedding / LM head | MoE 模型,各大小可整除 data_parallel_size |
| P/D-disaggregated decode (D) node | o_proj / MLP / embedding / LM head | The o_proj / MLP knobs require the full preconditions for o_proj / MLP TP |
| GLM-5.3-Flash P/D-disaggregated decode node | QKV IProj | See preconditions for QKV IProj TP |
组件与执行模式支持¶
| TP 配置 | Eager | Graph | Prefill | Decode |
|---|---|---|---|---|
| embedding | ✅ | ✅ | ✅ | ✅ |
| o_proj | ❌ | ✅ | ❌ | ✅ |
| mlp | ❌ | ✅ | ❌ | ✅ |
| LM head | ✅ | ✅ | ✅ | ✅ |
| QKV IProj | ✅ | ✅ | ❌ | ✅ |
- o_proj / MLP TP cannot be combined with standard TP and require `tensor_parallel_size == 1`; embedding / LM head TP can — see [Standard Tensor Parallelism Requirement](#standard-tensor-parallelism-requirement).
- LM head TP can be combined with speculative decoding (EAGLE, DFlash, and DSpark draft models); see [LM Head TP and Speculative Decoding](#lm-head-tp-and-speculative-decoding).
约束与限制¶
| 类别 | 描述 |
|---|---|
| 模型 | 仅支持 MoE 模型——关于如何检查 checkpoint,参见模型 |
| Deployment Scenario | Embedding and LM head TP support standalone MoE serving or PD decode nodes. o_proj, MLP and QKV IProj TP require a PD decode node. |
| Standard TP | o_proj, MLP and QKV IProj TP require tensor_parallel_size == 1. Embedding and LM head TP can be combined with standard TP. See Standard Tensor Parallelism Requirement. |
| Feature Mutual Exclusion | Pipeline parallelism (pipeline_parallel_size > 1) cannot be combined with fine-grained TP. o_proj, MLP and QKV IProj TP require prefill_context_parallel_size == 1. PCP embedding and LM head weight sharding (enable_pcp_embedding_lmhead_weight_sharding, on by default when PCP > 1) cannot be combined with embedding or LM head TP. |
| 硬件 | 无配置层面的硬件限制;本指南中的性能数据在 Atlas A2 上测得(参见实验结果) |
模型¶
细粒度 TP 目前仅支持 MoE 模型。该约束在配置加载时强制执行:非 MoE 模型会在启动时失败,并报错 The finegrained tp sizes can be enabled only for MOE models.
要检查某个 checkpoint 是否符合条件,请查看其 config.json:当配置通过以下任一字段暴露路由专家时,该模型即被视为 MoE 模型——n_routed_experts(DeepSeek 风格)、num_local_experts(Mixtral 风格)、num_experts、moe_num_experts,或 block_configs 下的 MoE 块。符合条件的 checkpoint 包括 DeepSeek-V3/R1、Qwen3 MoE 系列、GLM MoE 变体、Kimi-K2 和 MiniMax-M3;而 Llama 或稠密 Qwen 系列等稠密 checkpoint 则不符合条件。
该限制源于切分轴:细粒度 TP 在数据并行(DP)维度上切分权重,而只有 MoE 部署会保留跨 rank 的 DP 组——对于稠密模型,每个 DP rank 都作为独立的 DP=1 引擎运行,没有可供切分的组。
在符合条件的 MoE 模型中,mlp_tensor_parallel_size 会对稠密 MLP 层进行切分——例如 DeepSeek-R1 的前三个稠密层;MoE 层中的路由专家则改为通过专家并行进行切分,而共享专家保持复制。
o_proj / MLP TP 的前提条件¶
oproj_tensor_parallel_size > 1 和 mlp_tensor_parallel_size > 1 会在启动时进行校验,并且需要满足以下所有条件(无效配置会在配置加载时以明确的错误信息失败):
- 一个
tensor_parallel_size == 1的 MoE 模型(并且data_parallel_size能被 TP 大小整除); - 一个 P/D 分离部署,且仅在 decode(D)节点上(
kv_role = kv_consumer); - 重计算调度器:在
--additional-config中设置scheduler_config.recompute_scheduler_enable = true(它保持 decode 节点的 step 为 decode 形态); - KV connector 链中的
PreemptOffloadConnector——通过MultiConnector将 P/D 传输 connector(例如MooncakeConnectorV1)与PreemptOffloadConnector组合使用(被抢占的请求不得返回 prefill 节点:重计算的 KV 会丢失精度);参见 Preempt Offload 指南; - 图模式:不要使用
--enforce-eager启动实例; prefill_context_parallel_size == 1。
如果最大的 cudagraph 捕获尺寸无法覆盖最大的 decode 形态 step(min(max_num_batched_tokens, max_num_seqs * (1 + num_speculative_tokens))),则 oproj_tensor_parallel_size 和 mlp_tensor_parallel_size 都会在启动时自动禁用并给出警告,部署仍会在不启用它们的情况下启动;提高 max_cudagraph_capture_size 可重新启用。在运行时,若某个 step 被派发到已捕获图之外,会以明确的错误信息显式失败,而不是静默挂起跨 DP 集合通信。
Preconditions for QKV IProj TP¶
Set qkv_iproj_tensor_parallel_size above 1 when the deployment meets these conditions.
- GLM-5.3-Flash on a P/D-disaggregated decode node (
kv_role = kv_consumer). tensor_parallel_size == 1,prefill_context_parallel_size == 1andpipeline_parallel_size == 1.- A QKV IProj TP size that evenly divides both
data_parallel_sizeand the KDA head count.
LM Head TP 与投机解码¶
LM head TP 可与 EAGLE、DFlash 和 DSpark 草稿模型配合使用。有少数组合尚未与 group-row padding 对齐,会在构造时快速失败并给出说明原因的明确错误——这些是当前的范围决策,而非缺陷:
- DFlash2 草稿模型;
draft_sample_method = "probabilistic";use_local_argmax_reduction;enable_adaptive_verification。
LM head TP 也不支持 prompt logprobs:请求 prompt logprobs 会在采样时以明确的错误信息失败。
配置限制¶
任何组件的细粒度 TP 大小必须满足:
- ≤ 数据并行 (DP) 大小,并且
- 能整除 DP 大小(即
dp_size % tp_size == 0),以确保有效的设备分配和通信分组。
违反这些约束会在配置加载时以 finegrained tp sizes must divide by data_parallel_size. 失败。
标准张量并行要求¶
细粒度 TP 将所配置的层在数据并行(DP)维度上切分,而非标准张量并行(TP)维度。因此,某个开关能否与标准 TP 组合使用因组件而异:
| 组件 | 在 tensor_parallel_size > 1 时的行为 |
|---|---|
o_proj / mlp |
不能组合——在配置加载时被拒绝。 对于 o_proj,DSA attention 输出使用 n_local_groups = n_groups // tp_size(标准 TP)进行 reshape,而 wo_a/wo_b 权重由 OTP 组(DP 维度)切分;这两个轴不再对齐(对于 MLP,该组合只是未经测试)。这两个开关都要求 tensor_parallel_size == 1。 |
| QKV IProj | Requires tensor_parallel_size == 1. See preconditions for QKV IProj TP. |
embedding / LM head |
可以组合。 启用某个开关后,这些模块始终解析为细粒度方案:其权重由细粒度(DP 轴,固定 tp_idx)组切分——每个 rank 为 vocab / fine_grained_size——取代这些模块的标准 TP 切分,而不是与之叠加。场景 1 展示了一个叠加示例。 |
功能使用¶
使用示例¶
场景 1 — embedding + LM head TP,与标准 TP 叠加(独立 MoE 服务)¶
vllm serve vllm-ascend/DeepSeek-R1-W8A8 \
--quantization ascend \
--data-parallel-size 4 \
--tensor-parallel-size 4 \
--enable-expert-parallel \
--additional-config '{
"finegrained_tp_config": {
"lmhead_tensor_parallel_size": 4,
"embedding_tensor_parallel_size": 4
}
}'
embedding / LM head TP 是可以与标准 tensor_parallel_size 组合使用的两个开关(o_proj / MLP TP 则不能——参见标准张量并行要求);将它们与标准 TP 叠加会像往常一样在 TP rank 之间切分稠密权重,从而为独立实例提供每卡更多的 KV 余量。对于以 decode 吞吐为目标的生产部署,推荐的拓扑仍然是场景 2 所示的 P/D 分离 decode 节点。
Scenario 2 — o_proj, MLP, embedding and LM head TP on a P/D-disaggregated decode node¶
vllm serve vllm-ascend/DeepSeek-R1-W8A8 \
--quantization ascend \
--data-parallel-size 16 \
--tensor-parallel-size 1 \
--enable-expert-parallel \
--additional-config '{
"scheduler_config": {
"recompute_scheduler_enable": true
},
"finegrained_tp_config": {
"oproj_tensor_parallel_size": 8,
"mlp_tensor_parallel_size": 8,
"lmhead_tensor_parallel_size": 8,
"embedding_tensor_parallel_size": 8
}
}' \
--kv-transfer-config '{
"kv_connector": "MultiConnector",
"kv_role": "kv_consumer",
"kv_connector_extra_config": {
"connectors": [
{
"kv_connector": "MooncakeConnectorV1",
"kv_role": "kv_consumer",
"kv_port": "28000",
"kv_connector_extra_config": {
"prefill": {"dp_size": 1, "tp_size": 1},
"decode": {"dp_size": 16, "tp_size": 1}
}
},
{
"kv_connector": "PreemptOffloadConnector",
"kv_role": "kv_consumer",
"kv_connector_extra_config": {
"cpu_bytes_to_use_per_rank": 17179869184
}
}
]
}
}'
Scenario 2 must run in graph mode (do not pass `--enforce-eager`), and its prefill node keeps a plain producer configuration (e.g. `MooncakeConnectorV1` with `kv_role = kv_producer`). See the [Preempt Offload Guide](preempt_offload_connector.md) for the `PreemptOffloadConnector` parameters and the P/D disaggregation tutorials for the end-to-end deployment.
Scenario 3 — QKV IProj TP on a GLM-5.3-Flash decode node¶
On a GLM-5.3-Flash decode node with DP8, EP8, TP1, PCP1 and PP1, include the following settings in --additional-config.
--additional-config '{
"finegrained_tp_config": {
"qkv_iproj_tensor_parallel_size": 8,
"oproj_tensor_parallel_size": 1
}
}'
验证功能¶
实例启动后,检查哪些开关通过了启动校验:
- 检查启动日志中的已启用开关摘要:
预期输出(场景 1):
预期输出(场景 2):
finegrained_tp_config enabled: oproj_tensor_parallel_size=8, mlp_tensor_parallel_size=8, lmhead_tensor_parallel_size=8, embedding_tensor_parallel_size=8
Scenario 3 logs this entry.
仅列出通过启动检查的开关;格式为 knob=size(关于与 tensor_parallel_size 的相互作用,参见标准张量并行要求)。
- 如果该行中缺少
oproj_tensor_parallel_size/mlp_tensor_parallel_size,请检查是否存在捕获上限自动禁用警告:
当最大的 cudagraph 捕获尺寸无法覆盖最大的 decode 形态 step 时,该警告会触发;提高 max_cudagraph_capture_size 可重新启用这两个开关。
- 发送一个推理请求,以确认在开启这些开关的情况下服务能正常提供服务(任何标准客户端均可):
curl http://<ip>:<port>/v1/completions \
-H "Content-Type: application/json" \
-d '{"model": "vllm-ascend/DeepSeek-R1-W8A8", "prompt": "The future of AI is", "max_tokens": 32, "temperature": 0}'
预期结果:HTTP 200,且响应中包含 choices 字段。
配置参数¶
所有配置项均位于 --additional-config 中的 finegrained_tp_config 下。默认值 1 表示禁用该配置项。
| 参数 | 类型 | 默认值 | 是否必填 | 取值范围 | 说明 |
|---|---|---|---|---|---|
oproj_tensor_parallel_size |
int | 1 | 否 | data_parallel_size 的约数 |
注意力输出投影(o_proj,或 DSA 风格模型中的两阶段 wo_a/wo_b)的 TP 大小。取值 > 1 时需要满足 o_proj / MLP 前提条件。 |
mlp_tensor_parallel_size |
int | 1 | 否 | data_parallel_size 的约数 |
MLP(前馈)块的 TP 大小。取值 > 1 时需要满足 o_proj / MLP 前提条件。 |
lmhead_tensor_parallel_size |
int | 1 | 否 | data_parallel_size 的约数 |
LM head 的 TP 大小。 |
embedding_tensor_parallel_size |
int | 1 | 否 | data_parallel_size 的约数 |
token 嵌入表的 TP 大小。 |
qkv_iproj_tensor_parallel_size |
int | 1 | No | a divisor of data_parallel_size and the KDA head count |
TP size of the QKV input projection. Values > 1 require the QKV IProj preconditions. |
实验结果¶
为评估细粒度 TP 在大规模推理服务中的效果,我们在 32 张昇腾 Atlas A2 推理卡(每卡 64 GB)上部署了 DeepSeek-R1-W8A8 的 P/D 分离 decode 实例,采用 DP32+EP32 以及细粒度 TP 大小为 8。性能数据如下。
| 模块 | 内存节省 | TPOT 影响(批次=24) |
|---|---|---|
| o_proj TP = 8 | 5.8 GB | +1.5 毫秒(性能下降) |
| LM head TP = 8 | 1.51 GB | −1.2 毫秒(性能提升) |
| MLP TP = 8 | 0.9 GB | −1.0 ms(提升) |
| Embedding TP = 8 | 1.51 GB | −1.0 毫秒(性能提升) |
| 总计 | 9.72 GB | — |
- 每卡显存节省是主要收益;TPOT 也有小幅净提升。
常见问题¶
启动失败,报错 "The finegrained tp sizes can be enabled only for MOE models."¶
问题描述:设置了任意 finegrained_tp_config 大小后,实例在加载配置时因该错误而失败。
原因分析:该 checkpoint 不是 MoE 模型——细粒度 TP 会沿 DP 维度对权重进行切分,而该维度仅在 MoE 部署中作为跨 rank 组存在。
解决步骤:检查 checkpoint 的 config.json 中是否存在 模型 所列的专家字段。如果都不存在,则该模型为稠密模型,无法启用细粒度 TP;请改用标准的 tensor_parallel_size。
"finegrained_tp_config enabled" 日志行中缺少 o_proj / MLP TP¶
问题描述:服务正常启动,但尽管已配置 oproj_tensor_parallel_size / mlp_tensor_parallel_size,启动摘要中只列出了 embedding / LM head(或为空)。
原因分析:最大的 cudagraph 捕获大小未覆盖最大的 decode 形态 step,因此这两个开关被自动禁用并给出警告。
解决步骤:调高 max_cudagraph_capture_size(或调低 max_num_batched_tokens / max_num_seqs),使捕获上限覆盖 min(max_num_batched_tokens, max_num_seqs * (1 + num_speculative_tokens)),然后重启。
启动失败,报 o_proj / MLP 前置条件错误¶
问题描述:当 oproj_tensor_parallel_size > 1 或 mlp_tensor_parallel_size > 1 时,加载配置会抛出关于某一前置条件的错误(图模式、PD 场景、重计算调度器、PreemptOffloadConnector、tensor_parallel_size 或 PCP)。
原因分析:未满足 o_proj / MLP TP 的前置条件 中的一项或多项。
解决步骤:按照前置条件清单逐项检查;最常被遗漏的项是 scheduler_config.recompute_scheduler_enable = true 以及 MultiConnector 链中的 PreemptOffloadConnector 条目(参见上文场景 2)。