Skip to content

[spmd_types] state sharding and redistribution infra - #3587

Merged
pianpwk merged 8 commits into
mainfrom
gh/pianpwk/30/head
Jun 9, 2026
Merged

pianpwk merged 8 commits into
mainfrom
gh/pianpwk/30/head

Conversation

@pianpwk

@pianpwk pianpwk commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Splitting up infra PR. This adds:

  • _shard_spmd_state: util for distributing param/buffer onto mesh in SpmdLayout format, supporting PartitionSpec shard order
  • redistribute_spmd_per_axis: module boundary, SpmdLayout-based redistribution handler
  • _validate_spmd_redistributions: helper for validating it resolves to just one spmd.redistribute call - that's all titan will support

Stack from ghstack (oldest at bottom):

pianpwk added 2 commits June 8, 2026 23:19
[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 9, 2026
@pianpwk pianpwk changed the title Add local SPMD state sharding and redistribution helpers [spmd_types] state sharding and redistribution Jun 9, 2026
@pianpwk pianpwk changed the title [spmd_types] state sharding and redistribution [spmd_types] state sharding and redistribution infra Jun 9, 2026
pianpwk added a commit that referenced this pull request Jun 9, 2026
Adding this to allow both spmd types specs, and DTensor-style
placements. We're adding something like D107576760 in spmd_types, but in
the meantime, because non-llama3 models are still in DTensor placement
form, I think we have 2 options:

1) keep this as a hybrid spmd/DTensor container (e.g. moe_sharding.py
calls `dense_activation_placement(tp=Replicate())`, and migrate to
SpmdLayout once all models are off full_dtensor.
2) since we're adding a spmd->DTensor translation shim anyways, migrate
all models from `Replicate/Shard/Partial` to `spmd.R/S/P` immediately,
use SpmdLayout, and go deal with I/R distinctions later. This should be
ok because non-llama3 models won't support spmd backend yet.

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3468
* #3471
* #3253
* #3587
* #3586
* __->__ #3501
pianpwk added 2 commits June 9, 2026 00:59
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 9, 2026
pianpwk added 2 commits June 9, 2026 10:56
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk marked this pull request as ready for review June 9, 2026 18:00
Comment thread torchtitan/distributed/spmd_types.py Outdated
return x


def _shard_spmd_state(

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.

Suggested change
def _shard_spmd_state(
def spmd_distribute_tensor(
  • it's a public util function
  • "shard" may confuse with spmd.S
  • not necessarily for model state
  • distribute_tensor "matching" DTensor api name

Comment thread torchtitan/distributed/spmd_types.py Outdated
_validate_redistribute_spmd_pair(out_src, out_dst, name="output")


def redistribute_spmd_per_axis(

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.

Suggested change
def redistribute_spmd_per_axis(
def spmd_redistribute_per_axis(

for consistency

Comment on lines +58 to +59
or we should write collective-based (not placement-based) redistributions
after full_dtensor backend is removed.

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.

  • What's the motivation for this?
  • How do you map (in_src, in_dist) config to collectives?

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 isn't a clear-cut or strong proposal, but src/dst is more of a full-DTensor-first thing, and once that's done ShardingConfig design should be less constrained

And if we had thoughts to enhance redistribution info to be fine-grained (e.g. per-collective dtype: {"src": NamedPlacement, "dst": NamedPlacement, "op_dtype": ..., "out_dtype": ..., "backward_options": ...})

it doesn't seem too far-fetched to me to replace src/dst with the actual collective

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.

Sounds reasonable. I feel having both static metadata (types) and collectives is also appealing. E.g. one could even swap the collective backend while keeping the same metadata.

entries[dim] = (axis_name,)
return tuple(entries)

def _validate_redistribute_spmd_pair(

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.

didn't check internal, assuming it's correct

pianpwk added 2 commits June 9, 2026 14:09
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk changed the base branch from gh/pianpwk/30/base to main June 9, 2026 21:19
@pianpwk
pianpwk merged commit 51f107f into main Jun 9, 2026
7 of 11 checks passed
wwwjn pushed a commit that referenced this pull request Jun 10, 2026
Adding this to allow both spmd types specs, and DTensor-style
placements. We're adding something like D107576760 in spmd_types, but in
the meantime, because non-llama3 models are still in DTensor placement
form, I think we have 2 options:

1) keep this as a hybrid spmd/DTensor container (e.g. moe_sharding.py
calls `dense_activation_placement(tp=Replicate())`, and migrate to
SpmdLayout once all models are off full_dtensor.
2) since we're adding a spmd->DTensor translation shim anyways, migrate
all models from `Replicate/Shard/Partial` to `spmd.R/S/P` immediately,
use SpmdLayout, and go deal with I/R distinctions later. This should be
ok because non-llama3 models won't support spmd backend yet.

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3468
* #3471
* #3253
* #3587
* #3586
* __->__ #3501
wwwjn pushed a commit that referenced this pull request Jun 10, 2026
wwwjn pushed a commit that referenced this pull request Jun 10, 2026
Splitting up infra PR. This adds:
- _shard_spmd_state: util for distributing param/buffer onto mesh in
SpmdLayout format, supporting PartitionSpec shard order
- redistribute_spmd_per_axis: module boundary, SpmdLayout-based
redistribution handler
- _validate_spmd_redistributions: helper for validating it resolves to
just one spmd.redistribute call - that's all titan will support

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3468
* #3471
* #3253
* __->__ #3587
saforem2 pushed a commit to saforem2/torchtitan that referenced this pull request Jun 10, 2026
Splitting up infra PR. This adds:
- _shard_spmd_state: util for distributing param/buffer onto mesh in
SpmdLayout format, supporting PartitionSpec shard order
- redistribute_spmd_per_axis: module boundary, SpmdLayout-based
redistribution handler
- _validate_spmd_redistributions: helper for validating it resolves to
just one spmd.redistribute call - that's all titan will support

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* pytorch#3278
* pytorch#3472
* pytorch#3468
* pytorch#3471
* pytorch#3253
* __->__ pytorch#3587
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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.

2 participants