Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyujun's contribution.)变更摘要
本 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(),避免编译状态在用例间残留。


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
/approveor/lgtm- Commenting
/approveimplies 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.


/lgtm
/approve


描述
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 使用 CANNcann-9.2.0-w0920运行:结果:66 passed。UT 使用真实 override 配置和 CP plan,覆盖 C4 attention / C4 indexer / C128、连续与 head-tail 切分、短文档和空 rank;以未切分文档为独立参考,比较输出及输入、权重、APE、norm 梯度。新增用例分别模拟组合 op 与 Python 函数入口,并在 CPU 上验证两种入口的
aot_eagerfullgraph/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。
完整执行
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的验证结果:

其他信息