Merged
[fix] Fix DeepSeek V4.1 Inductor fullgraph compilation with CP and forced MoE load balancing #1456
[fix] Fix DeepSeek V4.1 Inductor fullgraph compilation with CP and forced MoE load balancing #1456
Merged
1Fire4Add on 3 days ago
1Fire4
1Fire4Collaborators
3 days ago

描述

问题背景

DeepSeek V4.1 同时启用 CP(doc_chunk)、Inductor 和强制 MoE 负载均衡时,模型以 fullgraph=True 编译。CP all-to-all 的 split 随 microbatch 的文档边界变化,Dynamo 为具体的 Python 整数值建立 guard,例如 recv_split[0] == 2584。不同结构的 TransformerBlock 又共用 checkpoint wrapper 的 Dynamo 缓存,编译变体叠加至 recompile_limit=32,训练在首步完成前抛出 FailOnRecompileLimitHit。

窗口 KV 交换还有一种边界情况。如果某个 rank 无需接收远端 key,Inductor 生成的激活检查点重算代码可能省略该 rank 的窗口 all-to-all。同组其他 rank 仍执行这次通信,导致反向卡住。

解决方案

  • 用 CPU 张量保存窗口 KV 交换与 compressor 输入恢复的 split count,在原生 functional all-to-all 的调用处保留符号长度,避免对每个 split 的具体值建立 guard。
  • 在 Inductor 和强制 MoE 负载均衡同时启用时,为每个 TransformerBlock 开启 isolate_recompiles=True,隔离不同层的 Dynamo 编译缓存。
  • 让每个 rank 经窗口 all-to-all 接收一个已有的本地 key,确保同组 rank 在激活检查点重算中都参与通信。窗口索引仍取原有的 K/V 内容。

类型

Checklist:

如何测试

  • 在 8 张 Ascend 950DT 上运行仓库的 DeepSeek V4.1 Flash 8 卡 A5 脚本,模型为 deepseek_v4_1_flash_40layers_16experts_text,数据为 tests/assets/c4_test。启用 FP8、CP=2、DP shard=4、DP replicate=1、EP=8、doc_chunk、local batch=1、global batch=32、序列长度 4096,以及 --debug.moe-force-load-balance --compile.enable --compile.components model --compile.backend inductor。
  • 5 个训练步完成,8 个 rank 各执行 40 次前反向,Dynamo 重编译计数均为 0。逐步 loss 为 12.29646、12.27092、12.19830、12.13146、12.08062。
  • 定向 CPU 测试通过 14 项,另有 2 项未选入。Gloo 对照覆盖 doc_chunk 和 doc_zigzag 的前向与反向。
  • 全仓 pre-commit 在基线和当前 PR 上均报告相同的 Ruff 和 Pyrefly 问题。

精度和性能验证结果

8 卡 FP8、Inductor fullgraph、强制 MoE 负载均衡,CP=1(DP shard=8)和 CP=2(DP shard=4)均完成 20 步。按仓库 loss_compare.py 的默认 close 阈值,本次 20 步对比 PASS。

指标 第 20 步 CP=1 第 20 步 CP=2 平均绝对误差 最大相对误差 平均相对误差 判定
训练 loss 11.460317 11.461547 0.001357 0.0276% 0.0116% PASS
梯度范数 8.367977 8.387766 0.007476 0.2744% 0.0872% PASS

DeepSeek V4.1 FP8 CP=1 与 CP=2 的 20 步 loss、梯度范数和误差曲线

误差曲线以 CP=1 为基线。底部红色虚线标出最大相对误差上限 ±0.5%。

阈值为平均绝对误差 ≤ 0.02、最大和平均相对误差各 ≤ 0.5%。两组使用相同初值(8 个 rank 的参数和缓冲区逐字节一致)。先打包再按 DP 切分,逐步核对了 640 条样本的输入、位置和标签。

强制均衡路由使用本地 token 序号。按两种 CP 布局重放路由规则,72.85% 的 token 位置(含 padding)对应的专家列表不同,因此指标反映两种训练配置的整体差异。两组使用同一套 CANN 9.3.0 运行库和编译环境,其中 3 处 ops-nn 头文件目录取自同版本的较早构建。

仓库数值对齐指南中的完整验证及性能对比待补充。

其他信息

本 PR 涉及 3 个产品文件和 1 个测试文件,沿用现有的 fullgraph 判定和公开编译开关语义。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for 1Fire4's contribution.)
1Fire41Fire4Collaborators
3 days ago Created pull request, commit 4338d486
CANN-robotCANN-robotCollaborators
3 days ago Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
3 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
3 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 ✅ zhaowei1936, zzyyjj012 (2/2) ✅ zhaowei1936 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
3 days ago Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
118 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
2 days ago Added label: approved
zzyyjj012
zzyyjj012Collaborators
2 days ago Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
2 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
2 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike