序列并行#

什么是序列并行#

序列并行最初由 Megatron 提出,其初衷是减少训练时的激活内存。核心修改是将 Allreduce->LayerNorm 改为 ReduceScatter->LayerNorm->Allgather。该技术后来被 vLLM 应用于推理。需要注意的是,将 Allreduce 拆分为 ReduceScatter 和 Allgather 本身并不会带来性能提升;它减少了 LayerNorm 的计算负载,但收益微乎其微。SP 的真正收益来自:

  1. LLM 推理部署常使用量化技术。以 NPU 上常用的 INT8 量化为例,在 LayerNorm 之后,Quant 算子会将隐藏状态从 BF16 量化为 INT8。Allgather 的通信量减半,耗时也几乎减半。

  2. 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。

Without modifying sp_min_token_num, the simplest way and recommended way to enable SP is:

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 开发的序列并行增强版本。增强内容包括:

  1. 对于使用 MLA 结构的模型,Allgather 被推迟到 QKV 投影之后,进一步减少了通信量。

  2. 对于 MoE 模型,Allgather 被推迟到 Gating+DynamicQuant 之后,同样旨在减少通信量。

FC1 是 vllm-ascend 中的特有优化,目前基于 Custom OP 实现,但难以支持 VL 类模型(原因详见 [RFC]: support sequence parallelism by pass)。

支持矩阵#

无量化#

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 需要 Qwen3VLMiddleAllReduceRMSNormPattern 的特殊处理

Qwen3-VL 的中间层在 all_reducelayernorm 之间插入了一个额外的加法:hidden_states=hidden_states + deepstack_input_embeds。在 SP 下,hidden_states(即 input)被 reduce-scatter 为每个 rank 的形状 [seq_len/tp, hidden],而 deepstack_input_embeds 来自视觉/deepstack 路径,保持完整序列形状 [seq_len, hidden](通常在 TP rank 间复制)。直接执行 reduce_scatter(input) + deepstack_input_embeds 会导致形状不匹配。解决方案是按 tp_sizedeepstack_input_embeds 进行分块,使每个 rank 使用 add(reduce_scatter, chunk(deepstack_input_embeds)[tp_rank]),从而在 layernormall_gather 之前保持形状一致。

SequenceParallelismMoePass#

应用 SequenceParallelismPass 后,MoE 模型的计算图如下所示:

AllGather EP 计算图

概述

  1. 推迟 allgather:在 SP 下,residual 按张量并行进行分块。这导致下一层的 layernorm 中隐藏状态与残差之间出现形状不匹配:隐藏状态被 gather(完整序列),而残差仍保持分块状态。解决方法是将 all_gather 移动到 layernorm 之后,以便 layernorm 在每个 rank 上对一致形状进行操作。MiddleLayerAllgatherAddRMSNormPatternLastLayerAllgatherRMSNormPatternQwen3VLMiddleLayerAllgatherAddRMSNormPattern 正是为此目的而设计的,每个模式处理不同的层和结构变体(见下表)。

  2. 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_spsp_min_token_num 默认为 None

  • NPUPlatform.apply_config_platform_defaults:如果 enable_spTruesp_min_token_num 为 None,

  • VllmConfig._apply_optimization_level_defaults:对于稠密模型,enable_sp 被设置为 True

  • VllmConfig.__post_init__:如果 sp_min_token_num 仍然为 None,则将 enable_sp 设置为 False