Skip to content

fix(megatron-fsdp): preserve non-meta tensors during meta device materialization - #4154

Open
xuwchen wants to merge 4 commits into
NVIDIA:mainfrom
xuwchen:fix_mfsdp_meta_device_init
Open

fix(megatron-fsdp): preserve non-meta tensors during meta device materialization#4154
xuwchen wants to merge 4 commits into
NVIDIA:mainfrom
xuwchen:fix_mfsdp_meta_device_init

Conversation

@xuwchen

@xuwchen xuwchen commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

During meta device initialization, the materialization path calls m.to_empty(device, recurse=False) before reset_parameters(). to_empty() unconditionally replaces all tensors (both parameters and buffers) in the module with uninitialized memory, regardless of whether they are actually on the meta device.

This becomes a problem for MoE when --moe-router-enable-expert-bias is enabled: expert_bias is registered with an explicit device=torch.cuda.current_device() [link], so it bypasses the meta context and is correctly initialized to zeros on GPU. But to_empty() clobbers it with uninitialized memory (NaN), and reset_parameters() only reinitializes parameters, not buffers, so expert_bias stays NaN for the entire training run.

The fix here replaces m.to_empty() with a targeted helper that only materializes is_meta=True tensors, leaving non-meta buffers (like expert_bias) untouched.

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

@xuwchen
xuwchen requested review from a team as code owners April 6, 2026 10:35
@copy-pr-bot

copy-pr-bot Bot commented Apr 6, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft April 6, 2026 10:35
@github-actions

github-actions Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@xuwchen
xuwchen marked this pull request as ready for review April 20, 2026 15:08
@svcnvidia-nemo-ci
svcnvidia-nemo-ci requested a review from a team April 20, 2026 15:08

@shjwudp shjwudp left a comment

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.

LGTM

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Apr 21, 2026
@xuwchen
xuwchen requested a review from a team April 21, 2026 03:13
@xuwchen
xuwchen enabled auto-merge April 21, 2026 16:37
@xuwchen
xuwchen disabled auto-merge April 21, 2026 16:48
@xuwchen
xuwchen enabled auto-merge April 21, 2026 16:50

@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.

Can someone give me a short backstory for why this is even needed (for MoE)? Why do we care about values prior to module.reset_parameters()? 👀

@xuwchen

xuwchen commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

Can someone give me a short backstory for why this is even needed (for MoE)? Why do we care about values prior to module.reset_parameters()? 👀

During meta device initialization, the materialization path calls m.to_empty(device, recurse=False) before reset_parameters(). to_empty() unconditionally replaces all tensors (both parameters and buffers) in the module with uninitialized memory, regardless of whether they are actually on the meta device.

This becomes a problem for MoE when --moe-router-enable-expert-bias is enabled: expert_bias is registered with an explicit device=torch.cuda.current_device() [link], so it bypasses the meta context and is correctly initialized to zeros on GPU. But to_empty() clobbers it with uninitialized memory (NaN), and reset_parameters() only reinitializes parameters, not buffers, so expert_bias stays NaN for the entire training run.

The fix here replaces m.to_empty() with a targeted helper that only materializes is_meta=True tensors, leaving non-meta buffers (like expert_bias) untouched.

@cspades

cspades commented May 11, 2026

Copy link
Copy Markdown
Member

/ok to test f13f04b

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels May 14, 2026
@Phlip79

Phlip79 commented Jul 7, 2026

Copy link
Copy Markdown
Member

/ok to test 626235a

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 complexity: low module: megatron-fsdp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants