Skip to content

Fix fsdp_dtensor conversion for pretrained-only checkpoints - #3912

Merged
cspades merged 2 commits into
NVIDIA:mainfrom
DAISY-gh:fix/fsdp-dtensor-pretrained-ckpt
Apr 1, 2026
Merged

cspades merged 2 commits into
NVIDIA:mainfrom
DAISY-gh:fix/fsdp-dtensor-pretrained-ckpt

Conversation

@DAISY-gh

Copy link
Copy Markdown
Contributor

What does this PR do ?

Fix fsdp_dtensor conversion for pretrained-only checkpoints

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact the @mcore-oncall.

Fix two bugs in convert-torch-dist-to-fsdp-dtensor that cause crashes when converting pretrained-only checkpoints (no optimizer state, e.g. HuggingFace-converted weights) to fsdp_dtensor format.

Changes

1. tools/checkpoint/checkpoint_inspector.py — AttributeError: 'Metadata' object has no attribute 'mcore_data'

convert_checkpoint() directly accesses metadata.mcore_data without checking if the attribute exists. mcore_data is a Megatron-Core extension that only appears in checkpoints containing distributed optimizer state. Pretrained-only checkpoints (e.g. converted from HuggingFace) have no optimizer state, so their metadata lacks this attribute.

Fix: Add hasattr(metadata, "mcore_data") guards before all unprotected accesses. This is consistent with the existing pattern already used elsewhere in the same file (lines 791, 804).

2. megatron/core/distributed/fsdp/src/megatron_fsdp/uneven_dtensor.py — RuntimeError: spec.tensor_meta.shape must be a tuple

split_dtensor() builds out_shape as a list (to allow element mutation at line 475), then passes it directly to DTensor.from_local(shape=out_shape). Newer PyTorch versions (e.g. PyTorch 2.6 in NeMo 26.02 container) enforce that shape must be a tuple.

Fix: Wrap with tuple(out_shape) before passing to DTensor.from_local().

Reproduction

Convert a HuggingFace-originated mcore torch_dist checkpoint (no optimizer state) to fsdp_dtensor

torchrun --nproc_per_node=4 --nnodes=1
tools/checkpoint/checkpoint_inspector.py
convert-torch-dist-to-fsdp-dtensor --swiglu
/path/to/mcore-torch-dist-ckpt/iter_0000000
/path/to/output-fsdp-dtensor
--param-to-param-group-map-json /path/to/empty-map.json

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.

@DAISY-gh
DAISY-gh requested review from a team as code owners March 18, 2026 03:31
@copy-pr-bot

copy-pr-bot Bot commented Mar 18, 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 March 18, 2026 03:31
@github-actions

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.

@DAISY-gh
DAISY-gh marked this pull request as ready for review March 18, 2026 03:31
@svcnvidia-nemo-ci
svcnvidia-nemo-ci requested a review from a team March 18, 2026 03:31

@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, thanks!

@shjwudp
shjwudp requested a review from xuwchen March 18, 2026 03:33
@shjwudp shjwudp added module: megatron-fsdp Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. and removed Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. labels Mar 18, 2026
@shjwudp
shjwudp requested a review from a team March 18, 2026 03:34
@shjwudp

shjwudp commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

/ok to test d8c4cc9

@chtruong814 chtruong814 added the needs-follow-up Issue needs follow-up label Mar 20, 2026

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

Approving to unblock.

@cspades

cspades commented Apr 1, 2026

Copy link
Copy Markdown
Member

/ok to test 0af5a7d

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Approved All necessary approvals have been made label Apr 1, 2026
@chtruong814 chtruong814 removed the needs-follow-up Issue needs follow-up label Apr 1, 2026
@cspades
cspades added this pull request to the merge queue Apr 1, 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/23862729458

Merged via the queue into NVIDIA:main with commit 1284d25 Apr 1, 2026
61 checks passed
yangbofun pushed a commit to xlm-research/Megatron-LM that referenced this pull request May 22, 2026
)

Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
yhgalaxy pushed a commit to yhgalaxy/Megatron-LM that referenced this pull request Jun 17, 2026
)

Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
Signed-off-by: yhgalaxy <yhgalaxy@outlook.com>
jon-barker pushed a commit to jon-barker/Megatron-LM that referenced this pull request Jul 10, 2026
)

Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
)

Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
test — 0af5a7dd Deployed Apr 1, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #10678
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 community-request module: megatron-fsdp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants