Skip to content

[SimpleFSDP] Fix HSDP placement mismatch in _distribute_dtensor - #2329

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
sorenzzio:fix_simplefsdp
Feb 5, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
sorenzzio:fix_simplefsdp

Conversation

@sorenzzio

Copy link
Copy Markdown
Contributor

The current_spec and target_spec in _distribute_dtensor were only providing
one placement for HSDP's 2D outer_mesh, causing a dimension mismatch.

Fixed by using (Replicate(),) * len(dp_placements) to automatically generate
the correct number of placements matching the mesh dimensionality:

  • 1 placement for FSDP/DDP (1D mesh)
  • 2 placements for HSDP (2D mesh)

@meta-cla

meta-cla Bot commented Feb 5, 2026

Copy link
Copy Markdown

Hi @SongyuanZhao!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@sorenzzio
sorenzzio marked this pull request as draft February 5, 2026 10:09
@sorenzzio sorenzzio changed the title Fix HSDP placement mismatch in _distribute_dtensor [SimpleFSDP] Fix HSDP placement mismatch in _distribute_dtensor Feb 5, 2026
@sorenzzio
sorenzzio marked this pull request as ready for review February 5, 2026 10:15
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 5, 2026
)

# HSDP case needs 2 placements for 2D outer_mesh
current_placements = (Replicate(),) * len(dp_placements)

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.

This makes sense to me because the outer_mesh used below should be 2d mesh in HSDP case.

I wonder if why this test HSDP(2,2) TP2 didn't captures it?
https://github.com/pytorch/torchtitan/blob/main/torchtitan/experiments/simple_fsdp/tests/integration_tests.py#L183

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.

I believe this is primarily because the sizes of dp_replica and dp_shard are identical.

First, a 1D placement is passed in while the device_mesh is 2D. In this case, sharding is performed along mesh_dim 0 (dp_replica), so calling _distribute_dtensor does not trigger an error.

Second, although sharding is performed on the incorrect dimension, the resulting shape remains correct because dp_replica == dp_shard. Consequently, the subsequent execution proceeds without errors.

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.

that's very unfortunate -- I think we should add assertion in DTensorSpec creation that the number of placement elements should match the mesh ndim. cc @wconstab @pianpwk

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 it even make sense for a DTensorSpec to accept the case where mesh and placements lengths are different?

cc., @tianyu-l @wconstab

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.

It doesn't.

@tianyu-l
tianyu-l merged commit 71517cf into pytorch:main Feb 5, 2026
6 checks passed
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
…rch#2329)

The current_spec and target_spec in _distribute_dtensor were only
providing
one placement for HSDP's 2D outer_mesh, causing a dimension mismatch.

Fixed by using (Replicate(),) * len(dp_placements) to automatically
generate
the correct number of placements matching the mesh dimensionality:
- 1 placement for FSDP/DDP (1D mesh)
- 2 placements for HSDP (2D mesh)

---------

Co-authored-by: ZhaoSongyuan <[email protected]>
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
…rch#2329)

The current_spec and target_spec in _distribute_dtensor were only
providing
one placement for HSDP's 2D outer_mesh, causing a dimension mismatch.

Fixed by using (Replicate(),) * len(dp_placements) to automatically
generate
the correct number of placements matching the mesh dimensionality:
- 1 placement for FSDP/DDP (1D mesh)
- 2 placements for HSDP (2D mesh)

---------

Co-authored-by: ZhaoSongyuan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants