Merged
fix: 修复 Engram SparseAdam checkpoint 跨 EP 拓扑恢复 #1092
fix: 修复 Engram SparseAdam checkpoint 跨 EP 拓扑恢复 #1092
Merged
没有杰Add on Oct 26
没有杰
没有杰
Oct 26

描述

修复 Engram CPU 权重及 SparseAdam 动量在改变 EP 拓扑后无法通过 DCP 恢复的问题。

原实现把 EP 分片信息写进权重和 SparseAdam 动量的 checkpoint key。例如,同一张表在 EP1 下叫 engram.table.weight,EP2 下则带有 .ep_shard_* 后缀。改变 EP 后,加载端生成的名字与保存时不同,DCP 就找不到对应状态。另外,普通 CPU Tensor 本身无法告诉 DCP“这是全局表的哪几行”。修改后,权重和动量始终使用固定名称,通过 CheckpointableTensor metadata 单独记录全局形状、分片起点和大小,让 DCP 根据当前 EP 布局读取对应数据,完成跨 EP 恢复。

跨 EP 恢复需要 PyTorch 支持 CheckpointableTensor 协议。

  • 权重与 exp_avg、exp_avg_sq 使用一致的行分片信息。
  • HF 导出根据 metadata 识别分片位置并排除 padding。
  • 删除旧 EP 后缀的兼容逻辑,离线导出直接使用标准 dcp.load(),移除重复的 _engram_native_key 字段和相关传参。
  • 精度对比工具统一使用单进程 CPU seed,移除 !1014 中不再需要的 --seed-mode、分布式辅助入口及对应测试。

类型

Checklist

如何测试

扩展现有 CPU UT,检查 Engram 权重与 SparseAdam 动量的稳定 key、全局分片 metadata 和零拷贝引用。

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -v --tb=short \
  tests/unit_tests/models/deepseek_v4_1/test_engram_host.py

HF 导入导出、量化读取、Host/SparseAdam 和 LoRA 相关 UT 共 76 项通过、1 项跳过。本次修改的生产文件类型检查通过。精度对比工具的 44 项 UT 通过。在 A2 上额外验证 V4.1 两层 debug 模型:默认单进程 CPU 生成 seed checkpoint,再由 8 卡 FSDP8/EP8 加载,两次独立训练各运行 2 step,TensorBoard 原始 loss 和 grad_norm 均完全一致。

已在 A5 单卡 V4.1 两层模型上验证 mix 低精度 checkpoint 的量化 HF 导出,新旧加载入口的导出结果完全一致。

训练对比

在 A2 两卡上使用 V4.1 debug 模型训练 20 step,第 10 step 保存 checkpoint,使用新进程加载后继续训练第 11–20 step,与连续训练比较。

EP2 → EP2: 恢复后的 loss 和 grad_norm 逐步完全一致。

EP2 到 EP2 断点续训对比

EP1 → EP2: loss 最大绝对误差约为 2.90e-4。grad_norm 最大相对误差约为 0.99%。

EP1 到 EP2 断点续训对比

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for 没有杰's contribution.)
没有杰没有杰
Oct 26 Created pull request, commit 13bbeed6
CANN-robotCANN-robotCollaborators
Oct 26 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Oct 26 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 ✅ zzyyjj012, xuyujun (2/2) ✅ zzyyjj012 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Oct 26 Comments:

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Oct 26 Set hwcaifubi,lrwei0709,zhaowei1936,zhong_lin,zhanghz1,depeng1994,shengnan666,XDaoHong,xuyujun,zhuofanshen,wanghui0,panchao-gitcode,zzyyjj012 as approver
137 messages are collapsed here。 See more
1Fire41Fire4Collaborators
Oct 30 Resolved final issue
xuyujun
xuyujunCollaborators
Oct 30 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Oct 30 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 30 Merged pull request
CANN-robot
CANN-robotCollaborators
Oct 30 Comments:

Pull Request 已合并或已关闭。

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

likedislike