Skip to content

[spmd_types] decoder sharding configs in spmd.* - #3471

Merged
pianpwk merged 45 commits into
gh/pianpwk/22/basefrom
gh/pianpwk/22/head
Jun 12, 2026
Merged

[spmd_types] decoder sharding configs in spmd.*#3471
pianpwk merged 45 commits into
gh/pianpwk/22/basefrom
gh/pianpwk/22/head

Conversation

@pianpwk

@pianpwk pianpwk commented May 31, 2026

Copy link
Copy Markdown
Contributor

switches decoder_sharding.py and llama3/sharding.py to spmd.* types.

  • Fill in src placements to be explicit, where previously we implicitly relied on DTensor
  • LocalMapConfig(in_grad_placements=...) carries info only used by default/full_dtensor backends; spmd_types backend just checks for presence of config, to switch to local SPMD
  • PartitionSpec used once for SP activation: CP/TP shard seq-dim
  • SP=off activations are I@TP
  • unsharded weights (e.g. norm) are R@TP when SP on, FSDP handles the gradient AR in DTensor: [spmd_types] fully_shard in DTensor and spmd_types save-restore pytorch#181519

Stack from ghstack (oldest at bottom):

[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 4 commits June 1, 2026 09:56
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 3 commits June 1, 2026 16:56
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk pianpwk changed the title Add SPMD decoder sharding config [spmd_types] decoder sharding configs in spmd.* Jun 3, 2026
@pianpwk
pianpwk marked this pull request as ready for review June 3, 2026 21:31
pianpwk added 2 commits June 3, 2026 15:21
[ghstack-poisoned]
[ghstack-poisoned]
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
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 9, 2026
Per-request, to avoid typechecking this

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3468
* #3471
* #3253
* #3587
* __->__ #3586
pianpwk added 3 commits June 9, 2026 10:57
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 9, 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
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
Per-request, to avoid typechecking this

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3468
* #3471
* #3253
* #3587
* __->__ #3586
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
[ghstack-poisoned]
"""
q_placements: SpmdLayout = dense_activation_placement(tp=spmd.S(2))
kv_src_placements: SpmdLayout = dense_activation_placement(tp=spmd.S(2))
kv_placements: SpmdLayout = dense_activation_placement(tp=spmd.S(2), cp=spmd.R)

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
kv_placements: SpmdLayout = dense_activation_placement(tp=spmd.S(2), cp=spmd.R)
kv_dst_placements: SpmdLayout = dense_activation_placement(tp=spmd.S(2), cp=spmd.R)

},
in_dst_shardings={
"q": q_placements,
"k": kv_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.

similar

@@ -19,8 +19,8 @@
def dense_param_placement(*, tp: spmd.PerMeshAxisSpmdType) -> SpmdLayout:

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.

all these should be renamed to _layout, can be its own PR

pianpwk added 6 commits June 10, 2026 22:22
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 11, 2026
Main infra PR for spmd_types + titan.

`spmd_types.py`:
- Adds a thread-local DeviceMesh stack, and current_mesh(),
set_current_mesh() helpers, to be set and accessed at model init (weight
parallelize) and runtime (PG access for collectives)
- Various helpers for spmd_types : module-boundary redistributions, spmd
-> DTensor placement translation (for full_dtensor backend),
`mesh_size(axis_name)` helper that returns > 1 when mesh is set & axis
is active. Some of this will be moved to spmd_types in near-term, see
comments.

`parallel_dims.py`:
- For spmd backend, we need 2 views over the world mesh: [pp, dp, cp,
tp] for typechecking, and [pp, dp_replicate, dp_shard, cp, tp] to pass
the unfolded fsdp axes to fully_shard, via DataParallelMeshDims. So we
hold both the full-DTensor-style dense mesh, as well as the
"typechecking" mesh.

`module.py`: module.parallelize() paths for spmd backend: weight init,
local SPMD drop in, input/output redistribution.

`trainer.py/utils.py`: typechecking context, input annotation, spmd
set_current_spmd_mesh, PP + typechecking raises a hard error.

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3632
* #3631
* #3468
* #3471
* __->__ #3253
@pianpwk pianpwk mentioned this pull request Jun 11, 2026
pianpwk added a commit that referenced this pull request Jun 11, 2026
Landed #3253 into wrong branch

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3278
* #3472
* #3632
* #3631
* #3468
* #3471
* __->__ #3641
pianpwk added 2 commits June 11, 2026 17:49
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk merged commit 40ff87e into gh/pianpwk/22/base Jun 12, 2026
9 of 11 checks passed
pianpwk added a commit that referenced this pull request Jun 12, 2026
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