Skip to content

Fix MegatronFSDP root module hook dispatch - #5808

Merged
wujingyue merged 1 commit into
NVIDIA:mainfrom
wujingyue:agent/fix-mfsdp-root-forward-hooks
Jul 15, 2026
Merged

Fix MegatronFSDP root module hook dispatch#5808
wujingyue merged 1 commit into
NVIDIA:mainfrom
wujingyue:agent/fix-mfsdp-root-forward-hooks

Conversation

@wujingyue

Copy link
Copy Markdown
Contributor

Summary

  • Route MegatronFSDP.forward through nn.Module.__call__ so wrapped-module forward hooks run.
  • Add a regression test covering root-owned parameters under optim_grads_params sharding.

Root Cause

MegatronFSDP.forward called self.module.forward(...) directly, bypassing the wrapped module’s registered forward pre-hooks. For root-owned parameters, that skipped the FSDP gather hook and left the root forward reading ungathered/freed parameter storage.

Fixes #5789

Validation

  • python -m py_compile megatron/core/distributed/fsdp/src/megatron_fsdp/megatron_fsdp.py tests/unit_tests/distributed/mfsdp_v1/test_mfsdp_fully_shard.py
  • uv run --no-sync python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/mfsdp_v1/test_mfsdp_fully_shard.py::TestMegatronFsdpFullyShard::test_root_module_forward_uses_gathered_parameters

@copy-pr-bot

copy-pr-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@wujingyue

Copy link
Copy Markdown
Contributor Author

/ok to test 08332c4

Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
@wujingyue
wujingyue force-pushed the agent/fix-mfsdp-root-forward-hooks branch from 08332c4 to 0b5f9db Compare July 14, 2026 17:48
@wujingyue

Copy link
Copy Markdown
Contributor Author

/ok to test 0b5f9db

@cspades cspades left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is indeed a bug. #5789 (comment)

Originally I tested TorchTitan and BioNeMo (HuggingFace) models with fully_shard. I wonder if every single root module I tested never owned any direct parameters, similar to MCore models.

cc @shjwudp

@wujingyue
wujingyue marked this pull request as ready for review July 14, 2026 19:05
@wujingyue
wujingyue requested review from a team as code owners July 14, 2026 19:05
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jul 14, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the Final Review PR is in the "final review" stage label Jul 15, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Approved All necessary approvals have been made label Jul 15, 2026
@wujingyue
wujingyue added this pull request to the merge queue Jul 15, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29389041233

Merged via the queue into NVIDIA:main with commit 4bf7fca Jul 15, 2026
188 of 190 checks passed
@wujingyue
wujingyue deleted the agent/fix-mfsdp-root-forward-hooks branch July 15, 2026 05:21
chochowski pushed a commit to chochowski/Megatron-LM that referenced this pull request Jul 20, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: mchochowski <mchochowski@nvidia.com>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made module: megatron-fsdp Run functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MegatronFSDP.forward calls self.module.forward(...) directly, bypassing the wrapped module's registered forward hooks

5 participants