Merged
[fix] 迁移 DeepEP MXFP8 通信量化到 TorchTitan 0.4.0 #1465
[fix] 迁移 DeepEP MXFP8 通信量化到 TorchTitan 0.4.0 #1465
Merged
zx1029Add on 2 days ago
zx1029
zx1029
2 days ago

描述

将已有的 MoE 专家激活 MXFP8 通信迁移到 TorchTitan 0.4.0 的类型化配置和 override 流程,使采用 CANN DeepEP 的 eager 训练可以按需降低 EP token 通信的传输字节数。

  • 新增 extension.quantization.all_to_all_quant_format。在启用低精训练的 DeepEP recipe 中设置为 "mxfp8" 即可开启,默认值为 None。支持 all_block_fp8 和 mix,要求 routed experts 的 w13、w2 使用相同的 E4M3 激活量化配置,block size 为 32。
  • 通信 converter 在专家计算量化之后运行,复用专家的激活量化参数,并选择 MXFP8 CANN DeepEP dispatcher。兼容 recipe 中显式配置的 asc_deepep override。
  • Dispatch 前向传输 E4M3 数据和 E8M0 scale,在接收端按专家顺序恢复成 grouped matmul 可直接消费的 MXTensor。Dispatch 反向通过 CANN combine 归并高精度激活梯度和 router score 梯度。
  • Combine 前向使用高精度归约,反向复用前向路由 handle 执行 cached MXFP8 dispatch。分数在 W2 前应用时,W2 反向可直接消费 MXFP8 梯度。分数在 W2 后应用时,梯度在进入分数乘法的反向前解量化。
  • 修正量化张量的 view 形状推导,使零元素输入可以变形为 (0, H),并对包含 0 且无法唯一推断 -1 的形状报错。

类型

Checklist:

如何测试

  • 变更文件的 ruff check、ruff format --check、Python 3.12 语法检查、python scripts/check_architecture_boundaries.py 和 git diff --check 1b32d27d ddaf0cda 均通过。完整 pre-commit 待在配套 Linux 环境执行。
  • 已将 resolve_view_shape 的 16,824 组输入与 CPU PyTorch 2.14 的 Tensor.view 对照,结果一致。新增 UT 覆盖 dispatcher 选择、override 兼容、scale 布局及空 shape,待在具备 torch_npu 和 TorchAO-NPU 的环境执行。
  • 开发侧已试跑训练,实际命令、设备与 CANN 版本、完成步数和日志待补录。复现时可基于 DeepSeek-V4 A5 的 deepseek_v4_flash_cpt_4k_a5() recipe 打开通信量化,先确认日志中的 MXFP8 dispatcher 数量,再验证普通和 cached dispatch 的前后向及梯度。

精度和性能验证结果

开启 mxfp8 通信有大约有 10 tps 提升

deepep 高精结果
image.png

deepep mxfp8 结果
image.png

其他信息

  • 当前启用范围为 eager CANN DeepEP,要求 expert_parallel_degree > 1,并使用同时支持普通与 cached (qdata, scale) dispatch 的配套 CANN 算子和 wheel。启用示例见低精度训练指南。
  • 零 token rank 遗留。 已核对的 CANN 实现要求每个 EP rank 至少发送一行,高精和 MXFP8 DeepEP 共用此约束。后续在共享 dispatcher 处理零发送,并分别验证 EP=2 下的零发送、零接收及 NPU Standard AllToAll 的空行前后向。
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zx1029's contribution.)
zx1029zx1029
2 days ago Created pull request, commit 02fb3a00
zx1029zx1029
2 days ago Close pull request
zx1029zx1029
2 days ago Reopen pull request
zx1029zx1029
2 days ago update merge request[project id:9028822, iid:1465, commit_id:66199e9dafcd1767099d4f00901b7b04a44b7cc1], virtual merging success
CANN-robotCANN-robotCollaborators
2 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
2 days ago 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 ✅ wanghui0, xuyujun (2/2) ✅ wanghui0 (1/1)

💡 Tip:

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

CLA Signature Pass

zhaoxi1029, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
zx1029
zx1029
2 days ago Comments:

compile

likedislike
zx1029zx1029
2 days ago Pre merge succeeded (commit_id: b7906aa85e47e41e159fe279ecca66a3873469a5)
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-failed
CANN-robot
CANN-robotCollaborators
2 days ago Comments:

流水线分析报告 — torchtitan-npu run#3523

流水线: https://gitcode.com/CANN/torchtitan-npu/actions/runs/0d33dfe6ac3c4f42839b18551bb44a73 | PR#1465 | 2026-12-03 16:12:48 ~ 2026-12-03 16:27:10 | 失败 Job 2 个

定界结论: 代码问题x2(共 2 个根因)

根因 #1(代码问题 / 代码检查错误)

  • 根因: Codecheck_dt 的 openlibing codecheck 返回 no pass
  • 处置: 通知 PR 提交者打开 codecheck 看板确认命中规则并修复后重触发

根因 #2(代码问题 / 代码检查错误)

  • 根因: pre-commit 的 Pyrefly 类型检查失败,命中 3 处类型错误(recipes.py 2 处、token_dispatcher.py 1 处)

  • 处置: 通知 PR 提交者修复下列类型错误后重触发

  • 排除项: 本次两个失败 Job 均为代码静态检查类失败,日志中未出现平台内部错误(如 DEV-CODECI-35002、查询 step status 失败)、依赖下载失败、镜像拉取失败或网络异常,故不属于环境问题/工程问题。

失败根因分析

根因 #1: 代码问题 / 代码检查错误 — Codecheck_dt: openlibing codecheck result: no pass
  • 判据: codecheck result: no pass
  • 定位步骤: 3_Codecheck_dt.log
  • 影响 Job: Codecheck_dt
  • 根因说明: 日志显示 codecheck 任务创建成功、轮询约 13 分钟后返回 state=no pass,任务退出码 255。该 Codecheck_dt Job 的日志只给出 codecheck 平台的汇总结果与看板地址,规则级命中明细(具体文件/行)需打开看板查看。
  • codecheck 看板地址(日志原文返回): https://www.openlibing.com/apps/entryCheckDashCode/MR_272971723f744384bb837a59464b8b64/9c492b499c24e1b2ac9ef8d861dad8ac?projectId=300033&codeHostingPlatformFlag=gitcode
  • 根因证据(日志原文):
[2026-12-03 16:26:47.253+08:00] 响应: {"code":"200","msg":"success","data":"https://www.openlibing.com/apps/entryCheckDashCode/MR_272971723f744384bb837a59464b8b64/9c492b499c24e1b2ac9ef8d861dad8ac?projectId=300033&codeHostingPlatformFlag=gitcode","uuid":null,"state":"no pass","failData":null,"taskCount":0,"task_id":null}
[2026-12-03 16:26:47.272+08:00] 2026-12-03 16:26:47,253 - INFO [codecheck 2026-12-03 16:26:47] Task failed
[2026-12-03 16:26:47.272+08:00] 2026-12-03 16:26:47,256 - INFO [codecheck 2026-12-03 16:26:47] codecheck result: no pass.
[2026-12-03 16:26:47.277+08:00] ::error::Process exited with code 255
根因 #2: 代码问题 / 代码检查错误 — Pyrefly 类型检查失败(3 errors) — recipes.py / token_dispatcher.py
  • 判据: Pyrefly (type checking)...Failed + ERROR ... [missing-attribute] / [bad-argument-type]
  • 定位步骤: 5_precommit.log
  • 影响 Job: CodeCheck_precommit
  • 定位到的文件/行(日志原文注解):
    • torchtitan_npu/extensions/adaptor/torchao/recipes.py:393:44 — Object of class 'type' has no attribute 'Config' [missing-attribute]
    • torchtitan_npu/extensions/adaptor/torchao/recipes.py:398:36 — Object of class 'type' has no attribute 'Config' [missing-attribute]
    • torchtitan_npu/extensions/adaptor/torchao/token_dispatcher.py:47:61 — Argument 'None' is not assignable to parameter 'class_or_tuple' with type 'type' in function 'issubclass' [bad-argument-type]
  • 与 PR 变更的关联: 上述两个文件均在 PR#1465 的变更文件列表中(recipes.py +107/-1、token_dispatcher.py +213/-0),属于本次 PR 引入/修改代码。
  • 根因证据(日志原文):
[2026-12-03 16:14:16.021+08:00] Pyrefly (type checking)..................................................Failed
[2026-12-03 16:14:16.021+08:00] - hook id: pyrefly
[2026-12-03 16:14:16.021+08:00] - exit code: 1
[2026-12-03 16:14:16.021+08:00]  INFO Checking project configured at `/runner/workers/***.version/worker_dir/cann/torchtitan-npu/pyproject.toml`
[2026-12-03 16:14:16.021+08:00] ERROR Object of class `type` has no attribute `Config` [missing-attribute]
[2026-12-03 16:14:16.021+08:00]    --> torchtitan_npu/extensions/adaptor/torchao/recipes.py:393:44
[2026-12-03 16:14:16.021+08:00] 393 |                 type(dispatcher_config) is quantized_dispatcher.Config
[2026-12-03 16:14:16.021+08:00] ERROR Object of class `type` has no attribute `Config` [missing-attribute]
[2026-12-03 16:14:16.021+08:00]    --> torchtitan_npu/extensions/adaptor/torchao/recipes.py:398:36
[2026-12-03 16:14:16.021+08:00] 398 |                 dispatcher_config, quantized_dispatcher.Config, _activation_quant_config=activation_config
[2026-12-03 16:14:16.021+08:00] ERROR Argument `None` is not assignable to parameter `class_or_tuple` with type `type` in function `issubclass` [bad-argument-type]
[2026-12-03 16:14:16.021+08:00]    --> torchtitan_npu/extensions/adaptor/torchao/token_dispatcher.py:47:61
[2026-12-03 16:14:16.021+08:00] 47 |         if parent_cls is None or not issubclass(parent_cls, cann_cls):
[2026-12-03 16:14:16.021+08:00]  INFO 3 errors (176 suppressed)
[2026-12-03 16:14:16.021+08:00] ::error::precommit.py 异常退出: exit code=1, signal=null
  • 说明: 同一次 pre-commit 中 trailing-whitespace / ruff / codespell / OAT 等其余检查均 Passed,仅 Pyrefly 检查 Failed,故本次失败点是类型检查而非格式化。
结构化定界摘要
[
  {
    "repo": "torchtitan-npu",
    "run_number": 3523,
    "Failed_Type": "代码问题",
    "Failed_Sub_Type": "代码检查错误",
    "Failed_Reason": "Codecheck_dt: openlibing codecheck 返回 no pass",
    "evidence": "[2026-12-03 16:26:47.256+08:00] [codecheck 2026-12-03 16:26:47] codecheck result: no pass.",
    "suggestion": "通知 PR 提交者按 codecheck 看板命中规则修复后重触发",
    "jobs": "Codecheck_dt"
  },
  {
    "repo": "torchtitan-npu",
    "run_number": 3523,
    "Failed_Type": "代码问题",
    "Failed_Sub_Type": "代码检查错误",
    "Failed_Reason": "CodeCheck_precommit: Pyrefly 类型检查失败 3 处(recipes.py:393/398, token_dispatcher.py:47)",
    "evidence": "[2026-12-03 16:14:16.021+08:00] ERROR Argument `None` is not assignable to parameter `class_or_tuple` with type `type` in function `issubclass` [bad-argument-type] --> torchtitan_npu/extensions/adaptor/torchao/token_dispatcher.py:47:61",
    "suggestion": "通知 PR 提交者修复 Pyrefly 类型错误(recipes.py:393/398 missing-attribute, token_dispatcher.py:47 bad-argument-type)后重触发",
    "jobs": "CodeCheck_precommit"
  }
]

由门禁失败定界分析自动生成 | repo: torchtitan-npu | run: 0d33dfe6ac3c4f42839b18551bb44a73 | PR#1465

likedislike
zx1029zx1029
2 days ago Modify description of pull request
7 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
2 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
zx1029
zx1029
2 days ago Comments:

compile

likedislike
zx1029zx1029
2 days ago Pre merge succeeded (commit_id: abd2ceebaf25b3cce2939cffed40f79a7e0480b7)
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-failed
CANN-robot
CANN-robotCollaborators
2 days ago Comments:

流水线分析报告 — torchtitan-npu run#3528

流水线: https://gitcode.com/CANN/torchtitan-npu/actions/runs/f82b74c5c4b24ce18f0a17aa70e5cdaf | PR#1465 | 2026-12-03 17:28:35 ~ 2026-12-03 17:41:14 | 失败 Job 1 个

定界结论: 代码问题 / LLT测试失败 — FAILED tests/unit_tests/test_sdc.py::tes
  • 失败类型: 代码问题
  • 根因: pytest 用例失败: tests/unit_tests/test_sdc.py::test_trainer_initializes_sdc_once_after_model_setup
  • 处置建议: 通知 PR 提交者修复(见各根因的具体建议)
    • 检查失败用例(FAILED tests/unit_tests/test_sdc.py::tes)的断言与实现: 若 PR 行为变更符合预期则同步更新用例断言, 否则修正实现逻辑(断言详情见根因折叠块)

失败根因分析

根因 #1: 代码问题 / LLT测试失败 — FAILED tests/unit_tests/test_sdc.py::tes
  • 判据: pytest 用例失败: tests/unit_tests/test_sdc.py::test_trainer_initializes_sdc_once_after_model_setup
  • 定位步骤: 5_UT_Test.log
  • 影响文件: tests/unit_tests/test_sdc.py
  • 影响 Job: UT_Test
  • 根因证据(日志原文):
[2026-12-03 17:40:39.917+08:00] FAILED tests/unit_tests/test_sdc.py::test_trainer_initializes_sdc_once_after_model_setup - AttributeError: 'types.SimpleNamespace' object has no attribute 'all_to_all_quant_format'
结构化定界摘要
[
  {
    "repo": "torchtitan-npu",
    "run_number": 3528,
    "Failed_Type": "代码问题",
    "Failed_Sub_Type": "LLT测试失败",
    "Failed_Reason": "pytest 用例失败: tests/unit_tests/test_sdc.py::test_trainer_initializes_sdc_once_after_model_setup",
    "evidence": "[2026-12-03 17:40:39.917+08:00] FAILED tests/unit_tests/test_sdc.py::test_trainer_initializes_sdc_once_after_model_setup - AttributeError: 'types.SimpleNamespace' object has no attribute 'all_to_all_q",
    "suggestion": "通知 PR 提交者修复(见各根因的具体建议)",
    "jobs": "UT_Test"
  }
]

由门禁失败定界分析自动生成 | repo: torchtitan-npu | run: f82b74c5c4b24ce18f0a17aa70e5cdaf | PR#1465

likedislike
zx1029
zx1029
2 days ago Comments:

compile

likedislike
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-failed
5 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
2 days ago Added label: cann-cla/yes
zx1029
zx1029
2 days ago Comments:

compile

likedislike
zx1029zx1029
2 days ago Pre merge succeeded (commit_id: d76b7f8844f10e1f7188c83b1250492cf5ff1e4a)
CANN-robot
CANN-robotCollaborators
2 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
2 days ago Added label: ci-pipeline-passed
zx1029zx1029
2 days ago Modify description of pull request
xuyujun
xuyujunCollaborators
1 day ago Comments:

/lgtm

likedislike
wanghui0
wanghui0Collaborators
1 day ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
1 day ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
1 day ago Merged pull request