Merged
feat(dsv4): support context parallelism in fused compressor #911
feat(dsv4): support context parallelism in fused compressor #911
Merged
xuyujunAdd on Sep 23
xuyujun
xuyujunCollaborators
Sep 23

描述

DeepSeek-V4 的 CANN 融合 Compressor 原先拒绝 context parallel(CP)。现复用已有 CPTokenDispatcher,只交换压缩块需要的边界原始 token,再按 plan 的分段边界调用融合算子;无需全量 all-gather。C4 的前驱块仍用于 overlap,输出继续由原有 compressed_rows 筛选,norm 与 RoPE 保持原有路径。

空压缩块的 rank 仍参加一次 CP 通信,然后以本地空 plan 走参考路径,避免把空输入传给不支持它的 CompressorGrad,同时保留零梯度。非 CP 路径与参数、checkpoint 格式不变。

兼容两种 CANN Compressor 入口:已注册组合 op 时继续调用 torch.ops.cann_ops_transformer.compressor;仅提供 Python 公共函数的版本调用 cann_ops_transformer.ops.compressor.compressor。两种入口均在组件构造时确定并绑定,前向只调用已选入口,避免热路径反复探测和 compile 在前向图中追踪 importlib。

类型

Checklist:

如何测试

基于 master 的 45bdc50,在 153 使用 CANN cann-9.2.0-w0920 运行:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -q \
  tests/unit_tests/override/deepseek_v4/test_compressor.py \
  tests/unit_tests/models/deepseek_v4/test_cp_dispatch.py --tb=short

结果:66 passed。UT 使用真实 override 配置和 CP plan,覆盖 C4 attention / C4 indexer / C128、连续与 head-tail 切分、短文档和空 rank;以未切分文档为独立参考,比较输出及输入、权重、APE、norm 梯度。新增用例分别模拟组合 op 与 Python 函数入口,并在 CPU 上验证两种入口的 aot_eager fullgraph/dynamic 前后向。

153 / A3 手动整网验证:共用同一个 HF seed checkpoint,DeepSeek-V4 Flash 43 层、EP8、4K 序列、global batch 8,关闭 compile,分别运行 FSDP8(8 卡)和 FSDP8CP2(16 卡)各 10 步。两组读取相同数据;保留 SMLA。CANN Compressor 使用真实融合前向;由于 A3 不支持原生反向,实验运行在本地测试入口中使用参考反向桩,该桩未提交到 PR。

对比 loss 最大绝对差 grad_norm 最大绝对差
参考 Compressor:CP1 vs CP2 1.5e-4 3.64e-2
融合 Compressor:CP1 vs CP2 4.3e-4 3.49e-2
CP1:参考 vs 融合 3.4e-4 2.0e-3
CP2:参考 vs 融合 4.0e-4 2.1e-3

完整执行 python -m pre_commit run --all-files --show-diff-on-failure:基础检查、Ruff、格式、codespell、OAT 通过;完整检查仍被未修改的 torchtitan_npu/models/deepseek_v4_1/attention.py:182 中 selected_attention 与当前 attn_gym 的类型签名不匹配挡住。

A5的验证结果:
cp1fsdp4_vs_cp2fsdp4_20260924_221545_pr_under_200kb.png

其他信息

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyujun's contribution.)
xuyujunxuyujunCollaborators
Sep 23 Created pull request, commit 61a00413
atomgit-bot
atomgit-botCollaborators
Sep 23 Comments:

变更摘要

本 PR 为 DeepSeek-V4 的 CANN 融合 Compressor 增加了 context parallel(CP)支持:AscCompressor.forward 不再在存在 CP 或 window 时直接报错,而是复用 CPTokenDispatcher 仅交换压缩块所需的边界原始 token,再按 plan 的分段边界调用融合算子,避免全量 all-gather。同时调整了 _state_inputs 的入参形式,并为 CP 场景补充了单元测试。

主要改动

  • 移除 CP 拒绝逻辑并接入边界 token 交换:AscCompressor.forward 删除原先 window 非空或 plan.exchange 非空即抛出 ValueError 的分支,改为在 plan.exchange is not None 时调用 compressor.token_dispatcher.gather(x, plan) 只交换缺失的边界 token,并按 plan.first_indices 与 plan.gather_indices.numel() 重建分段 cu_seqlens。
  • 空压缩块 rank 走本地参考路径:当 plan.gather_indices.numel() == 0 时,仍执行一次 CP 通信,然后用 replace(plan, exchange=None) 构造 CompressedVarlenMetadata 并走 compressor._forward,以规避不支持空输入的 CompressorGrad,同时保留零梯度。
  • _state_inputs 接口调整:函数签名由接收 metadata 改为接收 cu_seqlens 与 max_length,state_block_table 的列数改由 max_length 计算,CP 路径下 max_length 取 min(plan.gather_indices.numel(), x.shape[1] + 2 * compress_ratio),非 CP 路径取 attention_masks.varlen.max_k。
  • 拆分并新增 CP 单元测试:将原 test_compressor_empty_plan_and_cp 拆为 test_compressor_empty_plan,并新增 test_compressor_cp_outputs_and_gradients,覆盖 C4 attention/C4 indexer/C128、连续与 head-tail 切分、空 rank 及 eager/aot_eager,通过伪造 _all_to_all 与 cann_ops_transformer.compressor 校验输出与输入、权重、APE、norm 梯度。
  • 测试夹具清理:compressor_factory 在 finally 中调用 torch._dynamo.reset(),避免编译状态在用例间残留。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Sep 23 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 23 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 23 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ✅ lrwei0709, panchao-gitcode (2/2) ✅ lrwei0709 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Guide

@xuyujun , thanks for your pull request.

The following commit(s) have not signed Contributor License Agreement (CLA).

Commit Reason
ba5c29c9 test(dsv4): allow CPU rounding i... the email is not signed for CLA! please check if it is correct and the same as your signed email.

If you need to sign CLA, you can click here to sign the CLA.

If you need to check if the email is set up correctly, you can click here to do it in the FAQs.

After signing the CLA or updating the email, you must comment /check-cla to check the CLA status again.

likedislike
85 messages are collapsed here。 See more
weijinyi
weijinyiCollaborators
Sep 24 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 24 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Sep 24 Merged pull request
xuyujunxuyujunCollaborators
Sep 24 Modify description of pull request
xuyujunxuyujunCollaborators
Oct 24 Associated issue: [RFC] Compressor融合算子支持CP