Repository navigation
Fix TorchFT loss averaging across replicas - #4663
Merged
Merged
Conversation
jojoinfra
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 14, 2026 09:37
jojoinfra
force-pushed
the
fix-torchft-average-loss
branch
3 times, most recently
from
September 14, 2026 11:10
3d34cc0 to
85e0cf0
Compare
1. Average normalized loss across active replicas to avoid artificial log jumps when replicas leave or rejoin. 2. Preserve maximum loss and token sums while leaving training updates unchanged. 3. Add a focused regression test using the active replica count instead of the configured group size.
jojoinfra
force-pushed
the
fix-torchft-average-loss
branch
from
September 14, 2026 11:35
85e0cf0 to
8a68628
Compare
tianyu-l
approved these changes
Sep 14, 2026
Contributor
|
hmm not sure why it breaks non-ft numerics, could you help investigate? Thanks! should be due to #4535 (comment) |
Contributor
Author
Thanks for tracking this down and merging the fix! |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
During a training run with TorchFT replica exit and rejoin, the logged loss showed abrupt jumps when the active replica count changed. These discontinuities can be mistaken for training instability and make the loss curve difficult to interpret.
Root cause
Loss is already normalized within each replica, but the logging path sums these normalized losses across TorchFT replicas without averaging them.
Changes
ft_pg.size(), the number of replicas participating in the current step.