Skip to content

Fix chunked loss initialization in TorchFT trainer - #4575

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

tianyu-l merged 2 commits into
pytorch:mainfrom
jojoinfra:fix-chunk-loss

Conversation

@jojoinfra

Copy link
Copy Markdown
Contributor

While testing DeepSeek-V4 Flash training with TorchTitan and TorchFT across 128 dies, I ran into several issues and fixed them in my downstream setup. I'd like to contribute these fixes upstream as small, focused PRs. This PR addresses one of them.

With chunked loss enabled, FaultTolerantTrainer does not bind the model's lm_head to ChunkedLossWrapper or set _skip_lm_head. These steps already exist in Trainer, but are missing from TorchFT's separate initialization path.

This PR extracts the existing setup into Trainer._configure_chunked_loss() and calls it from both trainers after model construction. The helper preserves the existing handling of non-pipeline training and pipeline stages, while keeping this fix limited to chunked-loss initialization.

Validation:

  • Added one CPU unit test verifying lm_head binding and _skip_lm_head in the non-pipeline case: passed.

1. Share Trainer setup so TorchFT binds lm_head and skips the model projection.
2. Add one focused CPU regression test for non-pipeline training.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 10, 2026
Comment thread torchtitan/trainer.py Outdated
self.model_parts[
0
]._skip_lm_head = True # pyrefly: ignore[bad-argument-type]
self._configure_chunked_loss()

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.

I probably will refactor trainer soon. Before that, prefer not introducing new methods just because downstream needs it. You could duplicate the code in ft for now. Thanks!

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.

Understood, I've moved the helper into FaultTolerantTrainer and restored the original base Trainer initialization.

1. Move the chunked-loss helper into FaultTolerantTrainer.
2. Restore base Trainer initialization.
@tianyu-l
tianyu-l merged commit 91abd23 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