Skip to content

[fully_shard][DTensor] Support fully_shard with DTensor on full SPMD mesh - #176334

Closed
fegin wants to merge 13 commits into
gh/fegin/342/basefrom
gh/fegin/342/head
Closed

fegin wants to merge 13 commits into
gh/fegin/342/basefrom
gh/fegin/342/head

Conversation

@fegin

@fegin fegin commented Mar 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary
Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were not part of an existing DTensor mesh (which is TP or EP). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry Replicate placements in the original DTensor spec before fully_shard replaces them with Shard.

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

Verification
We verify this PR through unittests and TorchTitan integration: pytorch/torchtitan#2616.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Mar 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/176334

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 3 Pending, 1 Unrelated Failure

As of commit 82db5c8 with merge base 4f4276b (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

fegin added a commit that referenced this pull request Mar 3, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were not part of an existing DTensor mesh (TP). This PR introduces DataParallelMeshDimNames, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry Replicate placements in the original DTensor spec before fully_shard replaces them with Shard

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.


ghstack-source-id: 7a69142
Pull-Request: #176334
@fegin
fegin requested a review from weifengpy March 3, 2026 20:06
@fegin
fegin marked this pull request as draft March 3, 2026 20:35
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 4, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were not part of an existing DTensor mesh (TP). This PR introduces DataParallelMeshDimNames, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry Replicate placements in the original DTensor spec before fully_shard replaces them with Shard

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 1e2f340
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 4, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were not part of an existing DTensor mesh (TP). This PR introduces DataParallelMeshDimNames, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry Replicate placements in the original DTensor spec before fully_shard replaces them with Shard

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 575a8f5
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 5, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 3d915a6
Pull-Request: #176334
optim.zero_grad(set_to_none=(i % 2 == 0))
if mesh is not None:
inp = DTensor.from_local(
inp, mesh, [Replicate()] * mesh.ndim, run_check=False

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.

noob: why it's Replicate() instead of Shard(0) on the DP (batch) dim?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Think about it is DDP, if no fully_shard is applied. fully_shard is just an optimization that perform the allgather and reduce_scatter. Without fully_shard, it will still work correctly due to DTensor.

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

done with the review - totally understand it's in draft yet. just want to have a complete review first

fegin added a commit that referenced this pull request Mar 5, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 3d915a6
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 10, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: ea1790b
Pull-Request: #176334
@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Mar 10, 2026
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 10, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 5fe7824
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 12, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: a2c76ec
Pull-Request: #176334
fegin added a commit that referenced this pull request Mar 17, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 20a2568
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 17, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: ea4dff5
Pull-Request: #176334
fegin added a commit to pytorch/torchtitan that referenced this pull request Mar 17, 2026
**NOT READY YET**

This PR is still a WIP, so code quality and design are not finalized.

We publish this PR to get early signals of CIs and also verify pytorch/pytorch#176334.


ghstack-source-id: fc1597e
Pull-Request: #2616
@fegin
fegin requested a review from weifengpy March 17, 2026 21:39
@fegin
fegin marked this pull request as ready for review March 17, 2026 21:39
@fegin
fegin requested a review from wconstab March 17, 2026 21:40
fegin added a commit to pytorch/torchtitan that referenced this pull request Mar 17, 2026
**NOT READY YET**

This PR is still a WIP, so code quality and design are not finalized.

We publish this PR to get early signals of CIs and also verify pytorch/pytorch#176334.

ghstack-source-id: 184da4a
Pull-Request: #2616

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

mostly lgtm. @weifengpy do you want to do the final review?

shard (Optional[Union[str, tuple[str, ...]]]): Mesh dimension name(s)
that FSDP shards parameters on. If a tuple of names, those dims
are flattened into a single shard dimension. At least one of
``shard`` and ``replicate`` must be set.

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.

add a post__init to validate this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sgtm

def _get_submesh(names: tuple[str, ...]) -> "DeviceMesh":
if len(names) == 1:
return mesh[names[0]]
return mesh[names]._flatten("_".join(names))

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.

does fsdp already have the convention of creating new flattened meshes (with PGs, which is what i assume this line does), or is this new behavior?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a new behavior. The existing behavior is that users need to "flatten" CP and FSDP by themselves.

replicate_mesh_dim: int | None = None
dp_mesh_dims: DataParallelMeshDims | None = None
spmd_mesh: DeviceMesh | None = field(default=None, repr=False)
is_spmd_mesh: bool = field(default=False, init=False, repr=False)

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.

i wonder if we could validate that the mesh is actually an spmd mesh. i guess not. we can not compare it to the world since we don't know if PP is involved and we can't assume the whole world is part of SPMD. it is important that users don't accidentally pass a submesh that excludes any other spmd dims here though right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could do some validations, but it is not possible to the accurate validation as you mentioned, if PP exists.

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

thanks for resolving my long list of comments!

[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 19, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 26553d4
Pull-Request: #176334
[ghstack-poisoned]
fegin added a commit that referenced this pull request Mar 19, 2026
…mesh

Enable fully_shard to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, fully_shard only handled the case where DP dimensions were separate from the DTensor mesh (e.g., DP + TP on separate submeshes). This PR introduces DataParallelMeshDims, which lets users specify which mesh dimension names correspond to data parallelism (FSDP sharding and/or HSDP replication). The DP dimensions must carry Replicate() placements in the original DTensor spec before fully_shard replaces them with Shard().  Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

ghstack-source-id: 7d19071
Pull-Request: #176334
@fegin fegin added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 19, 2026
@fegin

fegin commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "The failing tests are not related"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

fegin added a commit to pytorch/torchtitan that referenced this pull request Mar 23, 2026
**NOT READY YET**

This PR is still a WIP, so code quality and design are not finalized.

We publish this PR to get early signals of CIs and also verify pytorch/pytorch#176334.

ghstack-source-id: 97bf135
Pull-Request: #2616
fegin added a commit to pytorch/torchtitan that referenced this pull request Mar 24, 2026
**NOT READY YET**

This PR is still a WIP, so code quality and design are not finalized.

We publish this PR to get early signals of CIs and also verify pytorch/pytorch#176334.

ghstack-source-id: b888637
Pull-Request: #2616
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
…mesh (pytorch#176334)

**Summary**
Enable `fully_shard` to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, `fully_shard` only handled the case where DP dimensions were not part of an existing DTensor mesh (which is TP or EP). This PR introduces `DataParallelMeshDims`, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry `Replicate` placements in the original DTensor spec before `fully_shard` replaces them with `Shard`.

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

**Verification**
We verify this PR through unittests and TorchTitan integration: pytorch/torchtitan#2616.
Pull Request resolved: pytorch#176334
Approved by: https://github.com/weifengpy
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…mesh (pytorch#176334)

**Summary**
Enable `fully_shard` to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, `fully_shard` only handled the case where DP dimensions were not part of an existing DTensor mesh (which is TP or EP). This PR introduces `DataParallelMeshDims`, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry `Replicate` placements in the original DTensor spec before `fully_shard` replaces them with `Shard`.

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

**Verification**
We verify this PR through unittests and TorchTitan integration: pytorch/torchtitan#2616.
Pull Request resolved: pytorch#176334
Approved by: https://github.com/weifengpy
@github-actions
github-actions Bot deleted the gh/fegin/342/head branch April 19, 2026 02:25
elfiegg added a commit to elfiegg/torchtitan that referenced this pull request May 20, 2026
Upstream 52a292d introduced torchtitan/distributed/full_dtensor.py with an
unconditional `from torch.distributed.fsdp import DataParallelMeshDims`.
That symbol was only added to PyTorch on 2026-03-19 (pytorch/pytorch#176334);
sqsh containers baked against an older torch nightly (i.e. anything before
the recipe was bumped to torch 2.13.0.dev20260505) crash at module-load:

  ImportError: cannot import name 'DataParallelMeshDims' from
  'torch.distributed.fsdp'

The crash is unconditional because trainer.py and components/validate.py
both do `from torchtitan.distributed import full_dtensor` at module scope,
which executes the offending import. DeepSeek-V3 already raises
NotImplementedError on `parallelism.full_dtensor=True`, so the runtime
callsites are dead code for the LLMB DeepSeek-V3 recipe -- the module just
needs to *import*.

Wrap the DataParallelMeshDims import in a try/except in both
torchtitan/distributed/full_dtensor.py and torchtitan/models/llama3/parallelize.py.
Fallback is `None`; the resulting function annotations (`-> None`,
`tuple[DeviceMesh, None]`) are valid in Python 3.9+ and never executed
under DeepSeek-V3.

This lets users running containers built against an older torch nightly
pick up the latest LLMB recipe without rebuilding the sqsh.
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…mesh (pytorch#176334)

**Summary**
Enable `fully_shard` to operate on models whose parameters are already DTensors distributed across a full SPMD mesh, including the data-parallel dimensions.

Previously, `fully_shard` only handled the case where DP dimensions were not part of an existing DTensor mesh (which is TP or EP). This PR introduces `DataParallelMeshDims`, which lets users specify which mesh dimensions correspond to data parallelism (sharding and/or replication). The DP dimensions must carry `Replicate` placements in the original DTensor spec before `fully_shard` replaces them with `Shard`.

Note that, the input/activation will also be DTensors distributed across the full SPMD mesh.

**Verification**
We verify this PR through unittests and TorchTitan integration: pytorch/torchtitan#2616.
Pull Request resolved: pytorch#176334
Approved by: https://github.com/weifengpy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (fsdp) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants