Skip to content

Fix recursive TorchFT optimizer dispatch - #4578

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

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

Conversation

@jojoinfra

@jojoinfra jojoinfra commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

TorchFTOptimizersContainer wraps itself with torchft.Optimizer. On accepted steps, the wrapper calls back into the same container, repeatedly triggering PyTorch's pre/post hooks. When the base method is already wrapped, super().step() adds a third invocation, causing redundant blocking MoE load-balancing collectives.

Both diagrams show an accepted step. All hooks shown belong to the same FT container.

Before, with the base step() already wrapped:

Trainer.train_step()
`-- TorchFTOptimizersContainer.step()
    |-- pre_hook                              #1
    |-- OptimizerWrapper.step()
    |   |-- manager.should_commit() -> True
    |   `-- TorchFTOptimizersContainer.step()  (same container)
    |       |-- pre_hook                      #2
    |       |-- super().step()
    |       |   |-- pre_hook                  #3
    |       |   |-- optimizer update loop     (once)
    |       |   `-- post_hook                 #1
    |       `-- post_hook                     #2
    `-- post_hook                             #3
screenshot_before

Keep a single public step() hook boundary, move optimizer updates into _step_optimizers(), and call the FT Manager directly. This also removes the temporary dispatch flag, which could remain disabled after an exception.

After:

Trainer.train_step()
`-- OptimizersContainer.step()                (inherited public entry)
    |-- pre_hook                              #1
    |-- TorchFTOptimizersContainer._step_optimizers()
    |   |-- manager.should_commit() -> True
    |   `-- OptimizersContainer._step_optimizers()
    |       `-- optimizer update loop         (once)
    `-- post_hook                             #1

Validation:

  • Two-rank Gloo regression with real MoE hooks and collectives: three load synchronizations per rank before the change, one afterward; Manager decisions are mocked.
  • CPU validation: 48 passed, 1 failed
    torchtitan-test.log

The single failure is a pre-existing LR restoration issue: restored optimizers use [0.05, 0.1] instead of [0.0875, 0.175], producing a different next update. The same failure reproduces before and after this change.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 10, 2026
1. Keep container step hooks on one public entry point with an internal update method.
2. Manage quorum directly without recursive wrappers or temporary dispatch flags.
3. Add a two-rank Gloo regression for one MoE load synchronization to the existing CPU tests.
@jojoinfra
jojoinfra force-pushed the fix-torchft-optimizer branch from 949c702 to 2f1c7b4 Compare September 10, 2026 13:05
self._step_optimizers()
return None

def _step_optimizers(self) -> None:

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.

no need to change core, just duplicate code in torchft/, not much code

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.

Agreed. I've reverted the core change and kept the update loop directly in TorchFTOptimizersContainer.step() after the quorum check.

1. Restore the core optimizer step implementation as requested in review.
2. Update inner optimizers directly in TorchFT to avoid repeated container hooks.
@tianyu-l
tianyu-l merged commit 59bbedf into pytorch:main Sep 11, 2026
17 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