Skip to content

fix(dist_checkpointing): pass only dtype to torch.empty in load_tensors_metadata - #7620

Merged
ericharper merged 2 commits into
NVIDIA:mainfrom
michal2409:fix-load-tensors-metadata-strides
Sep 30, 2026
Merged

ericharper merged 2 commits into
NVIDIA:mainfrom
michal2409:fix-load-tensors-metadata-strides

Conversation

@michal2409

@michal2409 michal2409 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

Fix loading torch_dist checkpoints on PyTorch builds that include pytorch/pytorch#194251, which adds a strides field to torch.distributed.checkpoint.metadata.TensorProperties.

TorchDistLoadShardedStrategy.load_tensors_metadata builds each placeholder tensor with torch.empty(tp.size, **tp.properties.__dict__, device='meta'). torch.empty does not accept strides, so load_tensors_metadata, load_sharded_metadata and dist_checkpointing.load fail on every torch_dist checkpoint:

TypeError: empty() got an unexpected keyword argument 'strides'

Checkpoints written by older PyTorch versions fail too, because the new TensorProperties.__setstate__ sets strides = None when unpickling them.

The placeholder only supplies the shape and dtype of the ShardedTensor (.without_data() drops the tensor itself), so pass only the dtype, as PyTorch's _EmptyStateDictLoadPlanner does.

⚠️ 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 @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue: none (small bug fix).

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • If this PR adds or changes a GPU kernel (Triton, jit_fuser/torch.compile, CUDA extension, TE or external-library dispatch, or a scatter/index accumulation), I have added or updated its bit-exact determinism test and registered it in tests/unit_tests/determinism/kernels/manifest.py (guide)
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Validation

On a PyTorch build that includes pytorch/pytorch#194251 (2.15.0a0):

  • The new test_load_tensors_metadata_ignores_tensor_strides fails without the fix and passes with it.
  • load_tensors_metadata and load_sharded_metadata on a torch_dist checkpoint written by an older PyTorch (925 tensors) raise the TypeError without the fix and pass with it.
  • A dist_checkpointing.save, load_tensors_metadata, dist_checkpointing.load round trip raises the TypeError without the fix and passes with it; the loaded tensors match.
  • black 26.3.0, isort 5.13.2, ruff 0.9 and pylint 3.2.6 with the repository configs report nothing new on the changed files.

Code review

Feel free to message or comment @NVIDIA/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.

🤖 Generated with Claude Code

…rs_metadata

`TorchDistLoadShardedStrategy.load_tensors_metadata` built each placeholder
tensor with `torch.empty(tp.size, **tp.properties.__dict__, device='meta')`.
pytorch/pytorch#194251 adds a `strides` field to
`torch.distributed.checkpoint.metadata.TensorProperties`, which `torch.empty`
does not accept, so loading any torch_dist checkpoint fails with

    TypeError: empty() got an unexpected keyword argument 'strides'

Checkpoints written by older PyTorch versions are affected too, because the
new `TensorProperties.__setstate__` sets `strides = None` when unpickling them.

The placeholder only provides the shape and dtype of the ShardedTensor, and
`.without_data()` drops the tensor itself, so pass only the dtype, as
PyTorch's `_EmptyStateDictLoadPlanner` does.

Signed-off-by: Michal Futrega <mfutrega@nvidia.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@copy-pr-bot

copy-pr-bot Bot commented Sep 23, 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.

@michal2409
michal2409 marked this pull request as ready for review September 23, 2026 22:11
@michal2409
michal2409 requested review from a team as code owners September 23, 2026 22:11
@gautham-kollu gautham-kollu added the Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. label Sep 29, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Sep 29, 2026
@gautham-kollu

Copy link
Copy Markdown
Contributor

/ok to test 188b025

@gautham-kollu gautham-kollu removed the Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. label Sep 29, 2026
@gautham-kollu
gautham-kollu removed the request for review from dimapihtar September 29, 2026 18:13
@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 Sep 29, 2026
@gautham-kollu

Copy link
Copy Markdown
Contributor

/ok to test 188b025

@ericharper
ericharper added this pull request to the merge queue Sep 29, 2026
@nemo-automation-bot

Copy link
Copy Markdown

🔄 Merge queue validation started!

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

Merged via the queue into NVIDIA:main with commit a21f840 Sep 30, 2026
113 of 117 checks passed

This branch was previously deployed

2 inactive deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

26.10 Approved All necessary approvals have been made mlperf

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants