序列并行¶
什么是序列并行¶
序列并行最初由 Megatron 提出,其初衷是减少训练时的激活内存。核心修改是将 Allreduce->LayerNorm 改为 ReduceScatter->LayerNorm->Allgather。该技术后来被 vLLM 应用于推理。需要注意的是,将 Allreduce 拆分为 ReduceScatter 和 Allgather 本身并不会带来性能提升;它减少了 LayerNorm 的计算负载,但收益微乎其微。SP 的真正收益来自:
- LLM 推理部署常使用量化技术。以 NPU 上常用的 INT8 量化为例,在 LayerNorm 之后,Quant 算子会将隐藏状态从 BF16 量化为 INT8。Allgather 的通信量减半,耗时也几乎减半。
- ReduceScatter 和 Allgather 可以分别与前后的 Matmul 操作融合为通信-计算并行算子,从而减少延迟。
使用方法¶
目前,vllm-ascend 已基于 Inductor pass 为 VL 类模型实现了序列并行。可通过以下方式启用:
vllm serve Qwen/Qwen3-VL-2B-Instruct \
--tensor-parallel-size 2 \
--compilation-config '{"pass_config": {"enable_sp": true , "sp_min_token_num": 1000}}'
"enable_sp":SP 的开关。由于 SP 依赖图模式,因此在 eager 模式下不支持。sp_min_token_num(来自上游 vLLM 的pass_config):根据我们的实验,当 token 数量较小时(经验值小于 1000),SP 实际上会带来负面影响。这是因为当通信量较小时,通信算子的固定开销成为主导因素。SP 仅在num_tokens >= sp_min_token_num时生效。**在 Ascend 上默认值为 1000,通常无需修改。**如需自定义,请使用--compilation-config '{"pass_config": {"enable_sp": true, "sp_min_token_num": 512}}'。该值将被附加到compile_ranges_split_points中,用于划分图编译范围并检查每个范围是否适用该 pass。
不修改 sp_min_token_num 的情况下,启用 SP 的最简单且推荐的方式是:
vllm serve Qwen/Qwen3-VL-2B-Instruct \
--tensor-parallel-size 2 \
--compilation-config '{"pass_config": {"enable_sp": true}}'
SP 与 Flash Comm V1 的区别¶
Flash Comm V1 (FC1) 是基于 NPU 开发的序列并行增强版本。增强内容包括:
- 对于使用 MLA 结构的模型,Allgather 被推迟到 QKV 投影之后,进一步减少了通信量。
- 对于 MoE 模型,Allgather 被推迟到 Gating+DynamicQuant 之后,同样旨在减少通信量。
FC1 是 vllm-ascend 中的特有优化,目前基于 Custom OP 实现,但难以支持 VL 类模型(原因详见 [RFC]: support sequence parallelism by pass)。因此,目前 FC1 和 SP 是互补的关系。
支持矩阵¶
无量化¶
| VL + 稠密模型 | VL + MoE | 非 VL + 稠密模型 | 非 VL + MoE | |
|---|---|---|---|---|
| 序列并行 | 图模式 | 图模式 | x | x |
| Flash Comm V1 | x | x | eager/图模式 | eager/图模式 |
有量化¶
SP 目前不支持量化,正在适配中。
| VL + 稠密模型 | VL + MoE | 非 VL + 稠密模型 | 非 VL + MoE | |
|---|---|---|---|---|
| 序列并行 | x | x | x | x |
| Flash Comm V1 | x | x | eager/图模式 | eager/图模式 |
Pass 设计¶
启用 SP 后,将按顺序运行以下 pass:SequenceParallelismPass,然后是 SequenceParallelismMoePass。
SequenceParallelismPass¶
首先运行 NoOpEliminationPass 以消除冗余的 view-like 操作,然后应用基于 AllReduce 的模式:
| 模式 | 匹配 | 替换 |
|---|---|---|
MiddleAllReduceRMSNormPattern |
all_reduce + layernorm |
reduce_scatter + layernorm + all_gather |
LastAllReduceRMSNormPattern |
相同(最后一层,无残差) | 相同 |
Qwen3VLMiddleAllReduceRMSNormPattern |
all_reduce + add + layernorm |
reduce_scatter + chunk(deepstack_input_embeds) + add + layernorm + all_gather |
为什么 Qwen3 VL 需要 Qwen3VLMiddleAllReduceRMSNorm模式 的特殊处理
Qwen3-VL middle layers insert an extra add between all_reduce and layernorm: hidden_states=hidden_states + deepstack_input_embeds. Under SP, hidden_states (i.e., input) is reduced-scattered to shape [seq_len/tp, hidden] per rank, while deepstack_input_embeds comes from the vision/deepstack path and stays full-sequence [seq_len, hidden] (typically replicated across TP ranks). Simply doing reduce_scatter(input) + deepstack_input_embeds would cause a shape mismatch.
The fix is to chunk deepstack_input_embeds by tp_size so each rank uses add(reduce_scatter, chunk(deepstack_input_embeds)[tp_rank]), keeping shapes consistent before layernorm and all_gather.
SequenceParallelismMoePass¶
应用 SequenceParallelismPass 后,MoE 模型的计算图如下所示:

概述
-
推迟 allgather:在 SP 下,
residual按张量并行进行分块。这导致下一层的 layernorm 中隐藏状态与残差之间出现形状不匹配:隐藏状态被 gather(完整序列),而残差仍保持分块状态。解决方法是将all_gather移动到 layernorm 之后,以便 layernorm 在每个 rank 上对一致形状进行操作。MiddleLayerAllgatherAddRMSNormPattern、LastLayerAllgatherRMSNormPattern和Qwen3VLMiddleLayerAllgatherAddRMSNormPattern正是为此目的而设计的,每个模式处理不同的层和结构变体(见下表)。 -
AllGatherChunkNoOp 清理:当 MoE SP 启用时,vLLM 会引入一个
sequence_parallel_chunk操作(对应图中的sp_chunk)。该操作与前面的all_gather一起形成了一个冗余的无操作对(all_gather 收集,然后 chunk 重新分割)。AllGatherChunkNoOpPattern将此对替换为 identity,以消除冗余的通信和计算。
模式详情:
| 模式 | 匹配 | 替换 |
|---|---|---|
MiddleLayerAllgatherAddRMSNormPattern |
all_gather + slice + layernorm |
layernorm + all_gather |
LastLayerAllgatherRMSNormPattern |
相同(最后一层,无残差) | 相同 |
Qwen3VLMiddleLayerAllgatherAddRMSNormPattern |
all_gather + slice + add + layernorm |
add(chunk) + layernorm + all_gather |
AllGatherChunkNoOpPattern |
all_gather + sequence_parallel_chunk_impl |
identity(无操作) |
常见问题¶
问1:SP 默认启用吗?¶
不,SP 默认不启用。SP 目前处于实验阶段,未来将默认启用。
代码中 enable_sp 的处理流程如下:
- 在
pass_config中,enable_sp和sp_min_token_num默认为None NPUPlatform.apply_config_platform_defaults:如果enable_sp为True且sp_min_token_num为 None,则设置默认的sp_min_token_num(稠密模型为 1000,MoE 模型为 1)VllmConfig._apply_optimization_level_defaults:对于稠密模型,enable_sp被设置为True。VllmConfig.__post_init__:如果sp_min_token_num仍然为None,则将enable_sp设置为False