Skip to content

Fix restored optimizer learning rates in TorchFT - #4598

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
jojoinfra:fix-torchft-restored-optimizer-lr
Sep 12, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
jojoinfra:fix-torchft-restored-optimizer-lr

Conversation

@jojoinfra

@jojoinfra jojoinfra commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

While performing zero-deviation numerical alignment validation with TorchTitan + TorchFT, I found that a joining replica could resume with an incorrect learning rate. Although gradient all-reduce produced identical gradients across replicas, different learning rates caused their weights to diverge after the optimizer update.

This breaks a fundamental invariant of synchronous data-parallel training: replicas starting from identical parameters and optimizer state must apply identical updates after gradient synchronization.

Root cause

Two issues contribute to the incorrect learning rate:

  • The optimizer state dictionary is cached. Tensor state remains current through shared references, but scalar learning rates become stale when the scheduler updates the optimizer.
  • Restoring scheduler state updates scheduler metadata without updating the actual optimizer parameter groups. Consequently, the scheduler can report the expected learning rate while the optimizer uses a different value.
torchft_lr_bug_root_cause

An example:

torchft_lr_example

Changes

  • Refresh learning rates in the cached optimizer state dictionary before exporting it.
  • Restore actual optimizer learning rates from the scheduler after both optimizer and scheduler states have finished loading, covering online replica recovery and DCP checkpoint loading without advancing the scheduler.
  • Add one focused regression test asserting that a joining replica restores the same actual optimizer learning rate as a healthy replica.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 11, 2026
@jojoinfra
jojoinfra force-pushed the fix-torchft-restored-optimizer-lr branch from df19e9e to cc44057 Compare September 11, 2026 16:04
1. Refresh cached optimizer state before peer transfer so joining replicas receive current learning rates.
2. Reuse the flat state exporter to update the cache in place and keep state_dict() read-only.
3. Verify metadata refresh, cache and tensor references, restored rates, and the first recovered update.
@jojoinfra
jojoinfra force-pushed the fix-torchft-restored-optimizer-lr branch from cc44057 to 0562571 Compare September 11, 2026 17:02
optimizers.init_cache_state_dict()

def state_dict():
if isinstance(optimizers, TorchFTOptimizersContainer):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this if -- this checkpoint component is not supposed to be used without the ft optimizer, so maybe assert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use an assert

@tianyu-l
tianyu-l merged commit d771abb into pytorch:main Sep 12, 2026
7 checks passed
submartingales pushed a commit to botcanlearn/torchtitan-npu-upstream that referenced this pull request Sep 22, 2026
Co-authored-by: hwcaifubi<[email protected]>



# message auto-generated for no-merge-commit merge:
!893 merge master-add-patches into master

fix: backport merged TorchFT fixes for torchtitan v0.3.0

Created-by: hwcaifubi
Commit-by: hwcaifubi
Merged-by: cann-robot
Description: ## 描述
Torchft相关的bugfix已合入上游社区,但是当前torchtitan-npu使用的是v0.3.0,还未包含这部分代码。当前先通过打patch的方式支持,等后续配套版本升级后可以完全删除。

| [#4534](pytorch/torchtitan#4534) | 在 TorchFT 模型构造前应用配置 override 并重新校验配置,对应 `experiments/torchft/trainer.py` |
| [#4575](pytorch/torchtitan#4575) | 为 TorchFT 的 chunked loss 绑定 `lm_head`,覆盖普通训练及 PP 最后阶段,对应 `experiments/torchft/trainer.py` |
| [#4578](pytorch/torchtitan#4578) | 消除 TorchFT optimizer 递归分派,保留一次容器 hook 调用,对应 `experiments/torchft/optimizer.py` |
| [#4598](pytorch/torchtitan#4598) | 导出副本恢复状态前刷新缓存中的学习率等元数据,对应 `experiments/torchft/optimizer.py` 和 `checkpoint.py` |
| [#4663](pytorch/torchtitan#4663) | 按当前参与副本数平均 TorchFT 日志 loss,对应 `experiments/torchft/trainer.py` |
| [#4664](pytorch/torchtitan#4664) | 为每个 FSDP 参数组注册 TorchFT 梯度平均 hook,对应 `experiments/torchft/manager.py` |

## 类型
- [x] Bug 修复
- [ ] 新功能
- [ ] 重构(即不是新增功能,也不是修改bug的代码变动)
- [ ] 构建过程或辅助工具的变动
- [ ] 文档内容更新

## Checklist:
- [x] 我的代码遵循这个项目的代码风格
- [x] 我已经自己测试过我的代码
- [x] 我已经更新了相应的文档
- [x] 我已经在标题中正确使用了类型标签(例如:`feat`, `fix`, `refactor`, `docs`, `test`)

## 如何测试
简要描述测试方案,并附上自验证记录。

## 其他信息
在这里可以添加任何与这个 Pull Request 相关的其他说明。


See merge request: cann/torchtitan-npu!893
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants