从 SGLang 的 CP 策略实现出发,拆开 TP8 prefill 里被重复计算的那部分,以及 CP8 用哪次通信把它换掉
hc_combine 的 kernel 网格是 (m, …),m 就是行数:实测 mHC 136.77 → 13.06 ms,与行数 8 倍差同比例。deepseek_v4.py 里的 dsa_cp_gather_hidden_states),每卡仍然把全部 8192 行喂进自己那份专家。CP 重排的是 per-token 工作,动不了 per-weight 工作。cp_decode_attn_tp.py 现场切权重 + 重算 MXFP8 swizzled scale 换回 TP8 行为。TP 切的是头和隐藏维,切不掉 token 维。于是每一层里「逐 token 但不必分片」的算子,在 8 张卡上各算了一遍同一批 8192 行;只有真正的矩阵乘、以及按头切分的注意力,才享受到了分片。
两种布局的总 query×head 都是 8192 × 64 = 524,288。差别在于:TP8 每卡把 20480 宽的超连接输入(4 路 × 5120)读 8192 遍,CP8 只读 1024 遍。
这也是本轮优化清单里那条判断的由来:
--cp-strategy interleave 对应 InterleaveCPStrategy。它做的是取模取行:第 r 张卡拿 r, r+8, r+16… 这些行,注意力头一个不切。
# srt/layers/cp/interleave.py · _interleave_shard
if tokens % cp_size != 0:
indices = torch.arange(cp_rank, tokens, cp_size, device=input_.device)
return input_[indices]
return input_.view(-1, cp_size, *input_.shape[1:])[:, cp_rank].contiguous()
分片发生在模型入口,由 split_before_forward 统一处理 —— input_ids、positions、input_embeds 都按同一套索引切:
# srt/layers/cp/base.py · ContextParallelStrategy.split_before_forward
if input_ids is not None:
forward_batch.cp_v2_input_ids = self.shard_hidden_states(input_ids, forward_batch)
forward_batch.positions = self.shard_position_ids(positions, forward_batch)
shard_position_ids 切的是「每个 token 的 KV-cache 槽位」:所以每张卡只为自己那 1024 行写 KV,KV 池没有重复写。
TP 版和 CP 版的 communicator 在同一个位置做同一件事,但一个要 AllReduce、一个不要:
# TP:srt/layers/communicator.py · _gather_hidden_states_and_residual
if context.attn_tp_size > 1:
hidden_states = attention_tensor_model_parallel_all_reduce(hidden_states)
hidden_states, residual = layernorm(hidden_states, residual)
# CP:srt/layers/communicator_dsa_cp.py · 同名方法
if hidden_states.shape[0] != 0:
hidden_states, residual = layernorm(hidden_states, residual)
if dsa_use_prefill_cp(forward_batch) or mla_use_prefill_cp(forward_batch):
hidden_states = dsa_cp_gather_hidden_states(hidden_states) # 没有 all_reduce
原因很直白:TP8 里每个 token 只有 1/8 个头在本卡算出来,输出必须跨卡求和才完整;CP8 里每个 token 的 64 个头都在持有该 token 的卡上算完,不需要求和。层内这样收口之后,MoE 前把行收齐、MoE 后把结果散射回去即可:
# srt/layers/communicator_dsa_cp.py
# After using cp, the communication mode of this part changes.
# The three parts of prepare_attn, prepare_mlp, and postprocess_layer
# no longer require additional communication for reduce, scatter, etc.
def dsa_cp_reduce_scatter_hidden_states(hidden_states):
hidden_states = hidden_states.tensor_split(cp_size)[cp_rank] # 先切好目标槽位
attn_cp_reduce_scatter_tensor(hidden_states, input_hidden_states)
return hidden_states
ReduceScatter 的目标切片提前 tensor_split 好了,所以 ReduceScatter 的结果直接落在本卡那一行区间里,同时把残差加也一并省掉。
# srt/models/deepseek_v4.py · forward(CP 分支)
_use_cp = self.dsa_enable_prefill_cp and dsa_use_prefill_cp(forward_batch)
...
if _use_cp:
moe_a2a_backend = get_moe_a2a_backend()
if moe_a2a_backend.is_none():
hidden_states = dsa_cp_gather_hidden_states(hidden_states)
专家是「按权重切、不按 token 切」的:本卡负责的那份专家权重,必须见过请求里的所有 token 才有意义。所以 MoE 之前一定要回到全量 8192 行,每卡的专家 GEMM 工作量与 TP8 时一模一样 —— 实测 55.14 → 54.98 ms,差 0.3%,也就是没变。CP 只重排 per-token 工作,动不了 per-weight 工作。
Sparse MLA 要先用 indexer 在全上下文里选 top-k 候选。如果每张卡只在自己那 1024 行上打分,选出来的候选就和 TP8 不是同一批了。所以 indexer 的 key 必须收齐再用:
# srt/layers/attention/dsa/dsa_indexer.py · _maybe_rotate 之后
# Gather the full key on alt_stream so the CP all-gather overlaps
# with the query rotate above on the current stream.
if is_cp_v2_active(forward_batch):
key = get_cp_strategy().materialize_full_indexer_k_cache(key, forward_batch)
注意这个 gather 是挂在另一条 stream 上、和 query 的 rope 旋转重叠跑的 —— 通信被算力盖住,而不是排在关键路径上。注意力本体需要的 KV 同理,按 MLA 的 latent 收(materialize_full_mla_kv,每 token 只有 kv_lora_rank + qk_rope = 512 + 64 = 576 个元素),不是按头收 64 × head_dim。
CP 模式下 tp_size=1 —— 注意力权重不切了,每卡一份完整副本。decode 不需要行分片,于是要现场把这副本切回 TP8 的样子用:
# srt/layers/cp/cp_decode_attn_tp.py
"""CP Decode Attention TP context.
When CP sets tp_size=1 (repeat weights), decode can partition attention
weights across CP ranks matching normal TP behavior."""
self.use_decode_attn_tp = not is_cp_v2_active(forward_batch) and not dsa_use_prefill_cp(forward_batch)
...
radix_attn.tp_q_head_num = orig_tp_q_head_num // self.decode_tp_size # 64 → 8 头
切权重的同时还得重算量化 scale:CUTLASS 用的 weight_scale_inv_swizzled 布局依赖矩阵形状,切片后必须重新生成并按 rank 缓存(cp_mxfp8_scale.py:block_fp8_scale_to_mxfp8_e8m0 → block_scale_interleave)。这段是 CP 的纯开销,TP8 下不存在。
同一全局 8192-token prefill chunk、TP0 的 trace。GPU 窗口从 811.55 ms 降到 533.70 ms,少掉的 278 ms 分布如下 —— 颜色只编码大小,灰阶即数值(Mono)。
下表把每一步的收益摊开,避免把整轮优化的功劳都算到 CP 头上。「相对上一配置」才是 CP8 自己那一步的增量。
| 配置(C8) | 输入 tok/s | 输出 tok/s | TTFT ms | TPOT ms | 相对上一配置 |
|---|---|---|---|---|---|
| 切换前一步:Prefill mHC 与 AllReduce 重叠 | 7,321.77 | 228.81 | 7,284.18 | 20.11 | — |
| 切换为 Prefill CP8 / Decode TP8 | 9,304.25 | 290.76 | 4,772.54 | 16.94 | +27.1% / −34.5% / −15.8% |
| 再叠加:Prefill chunk 16384 | 9,418.09 | 294.32 | 4,934.79 | 16.10 | +1.2% |
同一张表在 C32(并发 32)下的同一行:8,832.34 → 12,181.19 输入 tok/s(+37.9%)、TTFT 24,954.81 → 16,089.37 ms(−35.5%)、TPOT 63.59 → 48.15 ms(−24.3%)。相对本轮起点,CP8 的贡献是输入吞吐 +56.85%(C8)/ +70.03%(C32)。并发越高,CP 那一步的增量越大 —— 因为被重复计算的那部分随 chunk 线性增长,而它正是 CP 消掉的东西。
tp_q_head_num、必要时重算 swizzled scale(有缓存,但首次要生成),退出时再恢复。一句话判据:如果一层的耗时里,按 token 重复的算子占大头,CP 有收益;如果按权重算的 GEMM 占大头,CP 没收益。这个模型里 per-token 那部分(mHC 124 ms + 注意力形状 46 ms)贡献了 278 ms 中的 170 ms,通信重构又贡献 74 ms,MoE 一分未动 —— 余下约 34 ms 落在 trace 未单列的算子上。
cp_size = 8 时每卡 query×head 与 TP8 完全相同 —— 注意力算力持平,CP 的收益全部来自 per-token 算子和通信形态。cp_size 小于 8 时,每卡注意力反而比 TP8 更重(倍数 = 8 / cp_size)。
图型按 lieflat 的流程先审计主力模板,≥3 个候选:
| 候选 | 结论 | 理由 |
|---|---|---|
| F6 Paired Rungs | 采用 | 契约正是「每类 2 系列、如今昔对照」;4 个类目 × 2 系列,标签装得下。 |
| F12 Dumbbell Queue | 淘汰 | 同为前后对比,但契约是「串珠 = 真单位」,349.51 ms 要画 349 颗珠,密度失真。 |
| F1 Rung Bars / F5 Tick Rows | 淘汰 | 单系列模板;要点是对照,两张单图会把「MoE 没动」这个结论拆散。 |
| G3 Chunky Bars(Glance) | 淘汰 | Glance 是降级方案,仅在 Lupi 两组都不适配时启用;本例 F6 已能诚实承载,无需降级。 |
单位取 5 ms/格并在底注写明取整,是因为原始值是两次运行的平均(136.77 ms 这类小数);用 10 ms/格会把 MoE 的 55.14 → 54.98 显示成 6 格 → 5 格,凭空造出一个不存在的下降。
| 路径 | 作用 |
|---|---|
srt/layers/cp/interleave.py | 按行交错分片;_interleave_shard、materialize_full_mla_kv、materialize_full_indexer_k_cache |
srt/layers/cp/base.py | 策略抽象;split_before_forward、shard_hidden_states、shard_position_ids、init_cp_strategy |
srt/layers/cp/zigzag.py | 另一套分片策略(zigzag),本部署未选 |
srt/layers/communicator_dsa_cp.py | CP 版层间通信:dsa_cp_gather_hidden_states / dsa_cp_reduce_scatter_hidden_states,替代 TP 的 AllReduce + LayerNorm 组合 |
srt/layers/communicator.py | TP 版对照:_gather_hidden_states_and_residual 里的 attention_tensor_model_parallel_all_reduce |
srt/models/deepseek_v4.py | MoE 前的 token 收齐(dsa_cp_gather_hidden_states)、mHC 的 hc_pre / hc_post |
sglang/kernels/ops/layernorm/mhc.py | hc_combine:网格 (m, cdiv(h,1024)),代价随行数 m 线性 |
sglang/kernels/ops/layernorm/hc_combine_norm_candidate.py | 四路超连接合并 + RMSNorm 融合(宽度 5120、4 路共 20480) |
srt/layers/attention/dsa/dsa_indexer.py | indexer key 的全量收集,与 query rope 在双 stream 上重叠 |
srt/layers/cp/cp_decode_attn_tp.py | decode 侧把复制权重现场切成 TP8 形状,覆盖 tp_q_head_num |
srt/layers/cp/cp_mxfp8_scale.py | 为切片后的权重重算 CUTLASS MXFP8 swizzled scale 并缓存 |
srt/configs/deepseek_v4.py | 注意力几何:64 头、kv_lora_rank 512、qk_rope_head_dim 64 |