Add meta-parameter support - #5369
Merged
Merged
Conversation
This was referenced Jun 16, 2026
Signed-off-by: Jingyue Wu <wujingyue@gmail.com>
|
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. |
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
Signed-off-by: Jingyue Wu <jingyuew@nvidia.com>
shjwudp
approved these changes
Jul 31, 2026
jaredcasper
approved these changes
Jul 31, 2026
Phlip79
approved these changes
Jul 31, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30674465640 |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30675527927 |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 1, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30710998401 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds meta-parameter support to MFSDP v2.
fully_shard()can now accept meta parameters, allocate their sharded storage directly on the device mesh, and leave their values for the caller to initialize or checkpoint-load.This helps with #6139
Why is this useful?
MFSDP v2 already supports models whose weights are initialized on CPU, but that requires the full unsharded model weights to occupy host memory before sharding. For large models, CPU memory can be precious and may become a startup bottleneck. Constructing the model on the meta device retains only parameter metadata such as shapes and dtypes, allowing MFSDP v2 to create the real sharded storage without first materializing a full CPU copy.
Summary
DBufferstorage on the mesh device from tensor shapes and dtypes.Parameteronto its real unsharded buffer withtorch.utils.swap_tensors()because.datacannot rebind a meta TensorImpl to a real device TensorImpl.to_empty()orreset_parameters()/_reset_parameters();fully_shard()materializes storage without invoking user initialization hooks.Initialization semantics
I chose not to rely on
reset_parameters()because it is not guaranteed to be available for everynn.Module. Automatically initializing only modules that happen to provide a reset hook would also be confusing:fully_shard()would initialize some modules while leaving others uninitialized. Instead,fully_shard()consistently materializes storage without choosing parameter values, and the caller explicitly initializes or checkpoint-loads them.Checkpoint loading
The test explicitly synchronizes every parameter group's main weights to model weights after filling them. This simulates the post-load behavior being added by #6024; once that PR lands, its checkpoint loader will perform the synchronization.
Testing
python -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/mfsdp_v2/test_fully_shard.py::test_meta_parameters_shard_to_mesh_devicepython -m torch.distributed.run --nproc-per-node 2 -m pytest -q tests/unit_tests/distributed/mfsdp_v2git diff --check