Open
支持 NovaSwap optimizer state 的 checkpoint 恢复 #1
没有杰Add on Sep 10
支持 NovaSwap optimizer state 的 checkpoint 恢复 #1
Open
没有杰Add on Sep 10
没有杰
没有杰Collaborators
Sep 10

背景

本 PR 基于 PR 758 的 optimizer state swap 实现,补充 NovaSwap 场景下的 DCP checkpoint 保存和恢复支持。

optimizer state 完成 D2H 后,实际数据位于 NovaSwap 的 CPU buffer 中,原来的 NPU Tensor storage 不再适合作为 checkpoint 数据源。同时,NovaSwap 使用连续 bucket 保存多个 optimizer state,DCP 需要知道每个状态在 bucket 中的位置以及对应的全局 shard 信息。

因此,本实现将 NovaSwap CPU buffer 按照原状态的 dtype、shape、stride 和 byte offset 构造零拷贝 CheckpointableTensor view,并补充 DTensor shard metadata,继续复用 TorchTitan 和 PyTorch DCP 的标准保存、加载及重分片流程。

加载时,DCP 直接将数据写入当前 NovaSwap CPU buffer,同时保留 optimizer 中原有的 NPU/DTensor 对象,避免破坏 NovaSwap 注册的 Tensor identity 和后续 H2D 流程。

主要改动

  • 支持 AdamW 的 exp_avg、exp_avg_sq 和 max_exp_avg_sq。
  • 支持 DistMuon 的 momentum_buffer。
  • 支持 NovaSwap bucket 中不同 optimizer state 的零拷贝 checkpoint view。
  • 保留原 optimizer Tensor identity、DTensor placement 和 param groups。
  • 支持同步 checkpoint 和基于 CPU snapshot 的异步 checkpoint。
  • 支持普通 state_dict()/load_state_dict() 直接恢复。
  • 未启用 swap_optimizer 时,继续使用 TorchTitan 原生 checkpoint 路径。

使用方式

启用以下 override(启动Swap优化器的override这个checkpoint功能会自动启动):

torchtitan_npu.override.common.optimizer.swap_optimizer

当前支持 disabled 和 async 模式,暂不支持 async_with_pinned_mem。

验证结果

CPU UT 共 13 项通过,覆盖:

  • CheckpointableTensor metadata 和零拷贝关系;
  • DCP 同步、异步保存及恢复;
  • 普通 state_dict/load_state_dict round-trip;
  • optimizer Tensor identity;
  • AdamW bucket pipeline;
  • 恢复后的继续训练一致性。

另外完成了以下 NPU 集成验证:

  • DeepSeek-V4 AdamW:连续训练 10 step,与第 5 step 保存、重新创建模型和 optimizer 后恢复训练至第 10 step,loss 和 grad_norm 完全一致。
  • 4 NPU DistMuon + AdamW 混合优化器:连续训练与 checkpoint 恢复训练的最终模型参数和 optimizer state 完全一致。

下图展示 DeepSeek-V4 AdamW 在 step 5 保存并恢复后,step 6–10 与连续训练结果的对齐情况。

adamw_novaswap_async_resume_alignment_pr_under_200kb.png

Checkpoint 性能数据

测试环境:

  • DeepSeek-V4 debug model,AdamW
  • 2 × Ascend 910B3
  • FSDP2 / EP2
  • seq_len=128,global_batch_size=2
  • step 5 保存 checkpoint,随后启动新进程加载并继续执行 step 6
  • checkpoint 大小约 974.51 MiB
  • 每种模式独立运行 3 次,表中为中位数,方括号为 [min, max]
模式 保存阻塞时间 异步完整时间 加载时间 Checkpoint 大小 恢复精度
TorchTitan 原生同步 1.885 s [1.883, 1.911] — 2.036 s [1.987, 2.099] 974.51 MiB exact
NovaSwap 同步 1.965 s [1.949, 2.031] — 2.261 s [2.239, 2.331] 974.51 MiB exact
NovaSwap 异步 0.748 s [0.730, 0.795] 2.694 s [2.680, 2.820] 2.237 s [2.225, 2.342] 974.51 MiB exact

NovaSwap 同步保存相对原生路径增加约 4.3% 的保存阻塞时间,加载时间增加约 11.1%。异步模式通过 CPU snapshot 将训练线程的 checkpoint 阻塞时间降低至约 0.75 秒,相比原生同步路径减少约 60.3%;完整文件写入在后台继续进行。

三轮测试中,三种模式恢复后的 step 6 均与连续训练完全一致:

loss      = 15.748899459838867
grad_norm = 4.500015735626221

异步完整落盘时间通过 checkpoint 开始时间与 step-5 最后一个文件更新时间计算,用于反映后台写入完成时间;它不等于训练线程被阻塞的时间。

likedislike
Merge blocked
没有杰没有杰Collaborators
Sep 10 Force-pushed 1 commits:1d0dadf6-test(checkpoint): cover AdamW and mixed Muon resume
没有杰没有杰Collaborators
Sep 10 Pushed 1 commits:65ceaac1-refactor(checkpoint): minimize AdamW bucket changes
没有杰没有杰Collaborators
Sep 11 Pushed 2 commits:f02390e4-feat(checkpoint): support async NovaSwap snapshots,28db7c0e-test(checkpoint): add CPU NovaSwap checkpoint coverage
没有杰没有杰Collaborators
Sep 11 Force-pushed 2 commits:fa903c2b-test(checkpoint): add CPU NovaSwap checkpoint coverage,3fcaf616-docs(checkpoint): document NovaSwap checkpoint usage
没有杰没有杰Collaborators
Sep 11 Modify description of pull request
没有杰没有杰Collaborators
Sep 11 Modify description of pull request
zzyyjj012
zzyyjj012CollaboratorsSep 11Start code review3
torchtitan_npu/override/common/novaswap_checkpoint.py
Outdated
@@ -0,0 +118,4 @@
118+ super().__init__(config=config, **kwargs)
119+ 
120+ 
121+@override(
zzyyjj012
zzyyjj012Sep 11Comments:

确认一下能否和swap同时打开不单独配。

likedislike
System
System MessageSystem
Sep 11 Comments:

changed this line on bb4a8bcd view diff detail

likedislike
没有杰
没有杰Collaborators
Sep 11 Comments:

已调整为只配置 swap_optimizer,不再单独配置 swap_checkpoint。启用 swap_optimizer 后,optimizer container 会直接向 TorchTitan 原有 checkpoint/DCP 流程提供 NovaSwap CPU view,因此 swap 与 checkpoint 可以同时开启。

likedislike
zzyyjj012
zzyyjj012CollaboratorsSep 11Start code review3
torchtitan_npu/override/common/novaswap_checkpoint.py
Outdated
@@ -0,0 +123,4 @@
123+ description="Checkpoint NovaSwap optimizer state directly from pinned CPU buffers",
124+ exact=True,
125+)
126+def swap_checkpoint(
zzyyjj012
zzyyjj012Sep 11Comments:

确认一下CheckpointExtensions:是否会冲突

likedislike
System
System MessageSystem
Sep 11 Comments:

changed this line on bb4a8bcd view diff detail

likedislike
没有杰
没有杰Collaborators
Sep 11 Comments:

已移除单独的 checkpoint override,不再注册额外的 CheckpointExtensions。当前 checkpoint 仍使用 TorchTitan 原生 CheckpointManager/DCP,仅由 optimizer container 的 state_dict()/load_state_dict() 适配 NovaSwap,因此不存在两套 extension 冲突。

likedislike
zzyyjj012
zzyyjj012CollaboratorsSep 11Start code review3
torchtitan_npu/override/common/optimizer.py
Outdated
@@ -68,0 +75,4 @@
75+ parameter
76+ for group in self.optimizer.param_groups
77+ for parameter in group["params"]
78+ if parameter.requires_grad and not self.optimizer.state.get(parameter)
zzyyjj012
zzyyjj012Sep 11Comments:

确认是否和Adamw原来的lazy mode逻辑一致吗?必要性问题?

likedislike
没有杰
没有杰Collaborators
Sep 11 Comments:

这里在 checkpoint 前创建 optimizer state 是 TorchTitan 上游 OptimizersContainer.state_dict()/load_state_dict() 的标准行为。上游会先通过 init_optim_state() 建立完整状态,再生成或加载 flat optimizer state,因为首次 optimizer.step() 前这些状态可能还不存在。当前 override 只是沿用该流程,并为 NovaSwap 同时建立对应 bucket 和 CPU buffer,不改变正常训练路径的 lazy init 行为。

likedislike
System
System MessageSystem
Sep 14 Comments:

changed this line on ad390a49 view diff detail

likedislike
zzyyjj012
zzyyjj012CollaboratorsSep 11Start code review3
torchtitan_npu/override/common/novaswap_checkpoint.py
Outdated
@@ -0,0 +56,4 @@
56+ return view
57+ 
58+ 
59+def get_checkpoint_view(
zzyyjj012
zzyyjj012Sep 11Comments:

考虑是否可以放在novaswap中,使其对外表现是dcp view?

likedislike
没有杰
没有杰Collaborators
Sep 11 Comments:

暂时不把完整 DCP view 放入 NovaSwap。NovaSwap 只负责管理 CPU buffer 和 D2H/H2D 生命周期,而构造 DCP view 还需要 optimizer bucket 的 byte_offset、参数 FQN,以及 DTensor 的 global shape/offset 等 checkpoint 语义。这些信息属于 optimizer checkpoint adapter,因此保留在 novaswap_checkpoint.py,可以避免 NovaSwap 与 TorchTitan/DCP 强耦合。

likedislike
System
System MessageSystem
Sep 11 Comments:

changed this line on fbdf5ad2 view diff detail

likedislike
没有杰没有杰Collaborators
Sep 11 Pushed 1 commits:bb4a8bcd-refactor(checkpoint): use swap optimizer override directly
9 event messages are collapsed here。 See more
没有杰没有杰Collaborators
Sep 14 Force-pushed 1 commits:6d214ff5-feat: support NovaSwap optimizer checkpoints
没有杰
没有杰Collaborators
Sep 14 Comments:

/compile

likedislike
没有杰没有杰Collaborators
Sep 14 Pushed 1 commits:62ca07d6-fix: satisfy optimizer checkpoint lint checks
14 event messages are collapsed here。 See more
zzyyjj012zzyyjj012Collaborators
Sep 19 Force-pushed 18 commits:3500b075-action上线适配:checkout改allow-unsafe-pr-checkout,revise-img改comment+TESTTOKEN,9071f097-调整DeepSeek-V4.1 indexer部分实现,413a7a2c-feat: integrate KPBot via pinned submodule,215c3c5e-fix: preserve empty MoE unpermute backward under compile,22dab1ed-feat(deepseek_v4_1): 接入多模态数据加载与 A3/A5 融合训练,e22bfe2c-feat: add DeepSeek-V4 swiglu_group override,02bcde5f-fix(deepseek_v4_1): pass expert count through model config,6685d203-DeepSeek-V4.1代码调整,2c003445-[feat] add best performance config in a5 launcher,dad8ab0e-fix: 修复 CP 接收行梯度在选择性重计算下的动态 shape 错误,ad5aa510-refactor(deepseek_v4_1): 统一启动方式并清理冗余配置,584a627d-[feat] Qwen3.5-VL NPU Context Parallel 适配迁移至 master,265a8c83-fix(ops): materialize the split-K layout for degenerate R=0/1 inputs,967ba483-feat: support NovaSwap optimizer checkpoint with bucket states,4f7010e9-test: make NovaSwap checkpoint UT environment-safe,653baaae-refactor: address checkpoint code quality findings,da521971-test: shorten DeepSeek integration golden runs,c8e1d285-test: cover Muon swap checkpoint resume