Skip to content

Fix TorchFT hooks for multi-group FSDP modules - #4664

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
jojoinfra:fix-fsdp-param-group
Sep 15, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
jojoinfra:fix-fsdp-param-group

Conversation

@jojoinfra

Copy link
Copy Markdown
Contributor

Summary

Fix TorchFT initialization with FSDP+EP when one FSDP module has multiple parameter groups.

This process: replica=0, local_rank=0
Scope: initialization only, before forward/backward or optimizer.step
layers.0: one FSDPModule, 2 FSDP parameter groups
  group 0: shard_ranks=[0, 1]
    parameters=['layers.0.attention.weight', 'layers.0.moe.router.weight']
  group 1: shard_ranks=[0]
    parameters=['layers.0.moe.routed_experts.inner_experts.w1_EFD', 'layers.0.moe.routed_experts.inner_experts.w2_EDF', 'layers.0.moe.routed_experts.inner_experts.w3_EFD']
TorchFT manager: enabled=True, use_async_quorum=True
Calling TorchFTManager.maybe_set_all_reduce_hook([model])
[rank0]: Traceback (most recent call last):
[rank0]:   File "/tmp/ft-fsdp-ep-repro.FVcvhL/reproduce.py", line 172, in <module>
[rank0]:     run_worker(args)
[rank0]:   File "/tmp/ft-fsdp-ep-repro.FVcvhL/reproduce.py", line 110, in run_worker
[rank0]:     ft_manager.maybe_set_all_reduce_hook([model])
[rank0]:   File "/tmp/ft-fsdp-ep-repro.FVcvhL/before/torchtitan/experiments/torchft/manager.py", line 157, in maybe_set_all_reduce_hook
[rank0]:     model_part.apply(apply_set_all_reduce_hook)
[rank0]:   File "/usr/local/python/python312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1077, in apply
[rank0]:     module.apply(fn)
[rank0]:   File "/usr/local/python/python312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1077, in apply
[rank0]:     module.apply(fn)
[rank0]:   File "/usr/local/python/python312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1078, in apply
[rank0]:     fn(self)
[rank0]:   File "/tmp/ft-fsdp-ep-repro.FVcvhL/before/torchtitan/experiments/torchft/manager.py", line 154, in apply_set_all_reduce_hook
[rank0]:     m.set_all_reduce_hook(all_reduce_hook)
[rank0]:   File "/usr/local/python/python312/lib/python3.12/site-packages/torch/distributed/fsdp/_fully_shard/_fully_shard.py", line 610, in set_all_reduce_hook
[rank0]:     raise ValueError(
[rank0]: ValueError: set_all_reduce_hook is not supported with multiple param groups (from per-param mesh via shard_placement_fn). The hook would be ambiguous across groups with different meshes.

Root cause

EP assigns ordinary and expert parameters to different FSDP meshes, creating separate groups within one module. TorchTitan calls FSDPModule.set_all_reduce_hook(), which rejects multiple groups because a generic hook may be ambiguous across their meshes. This raises ValueError before training starts.

Changes

Register TorchFT's averaging hook on each group's internal _all_reduce_hook. The hook averages corresponding shards across replicas, so both groups can use it while retaining FSDP's existing communication path.

Test plan

  • Two replicas with two ranks each, FSDP=2 and EP=2: initialization fails before the fix and succeeds afterward on all four ranks.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 14, 2026
def apply_set_all_reduce_hook(m):
if isinstance(m, FSDPModule):
m.set_all_reduce_hook(all_reduce_hook)
_set_fsdp_all_reduce_hook(m, all_reduce_hook)

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.

just inline this function? It's already inside embedded functions

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.

Inlined the hook registration into apply_set_all_reduce_hook, Thanks for the suggestion

1. Handle FSDP modules with multiple parameter groups, which the public hook API rejects.
2. Register the TorchFT hook on every group and add a regression to the existing CPU test entry point.
@tianyu-l
tianyu-l merged commit f5c97dc into pytorch:main Sep 15, 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