Skip to content

[spmd_types] spmd.local region in ChunkedCELoss - #3632

Merged
pianpwk merged 44 commits into
mainfrom
gh/pianpwk/32/head
Jun 17, 2026
Merged

pianpwk merged 44 commits into
mainfrom
gh/pianpwk/32/head

Conversation

@pianpwk

@pianpwk pianpwk commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

No immediate effect, just prepping for next commit.

I decided not to use spmd.local_map mainly because it needs refactoring out the chunked loss body as standalone function, and the assert_type/assert_type_like following the region input is more convenient, than specifying the full placements for both loss & hidden states..

Stack from ghstack (oldest at bottom):

pianpwk added 2 commits June 10, 2026 21:46
[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 11, 2026
@pianpwk pianpwk changed the title Refactor chunked CE loss structure [spmd_types] Refactor chunked CE loss structure Jun 11, 2026
@pianpwk pianpwk changed the title [spmd_types] Refactor chunked CE loss structure [spmd_types] refactor chunked CE loss structure Jun 11, 2026
pianpwk added 12 commits June 10, 2026 22:22
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[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
chunk_loss = self.fn(logits, label_chunk)
if global_valid_tokens is not None:
chunk_loss = chunk_loss / global_valid_tokens
total_loss = total_loss + chunk_loss.detach()

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 are the local spmd_types for each operands?

It is a bit surprising to see this region working with local type checking.

@pianpwk pianpwk Jun 16, 2026 •

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.

it needs the next commit changes, this PR is a no-op, just prep for reviewability

total_loss needs mutate_type to be init as P@DP/CP, I@TP

chunk_loss exits local SPMD region as V@DP/CP, I@TP, but gets reinterpreted to P@DP/CP (lose partial inference ability in local)

global_valid_tokens we changed to float

pianpwk added 8 commits June 15, 2026 22:51
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 16, 2026
Pull Request resolved: pytorch/torchtitan#3632
ghstack-source-id: ff7b918
pianpwk added a commit that referenced this pull request Jun 16, 2026
Moves full_dtensor backend off pytorch's loss_parallel -> our custom
autograd fn

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3673
* #3655
* #3654
* #3653
* #3278
* #3672
* #3472
* #3632
* __->__ #3671
pianpwk added 2 commits June 16, 2026 16:22
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 2 commits June 16, 2026 20:37
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 17, 2026
SAC + Flex, or SAC + typecheck + non-flex, or typecheck + Flex + full/no AC is fine, but this is not (likely SAC + regional inductor issue). Raise error asking user to use one of the other cominbations

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3690
* #3673
* #3655
* #3664
* #3654
* #3278
* #3694
* #3472
* #3632
* #3653
* __->__ #3672
pianpwk added 2 commits June 16, 2026 20:45
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Jun 17, 2026
@pianpwk
pianpwk changed the base branch from gh/pianpwk/32/base to main June 17, 2026 06:19
@pianpwk
pianpwk merged commit feb8cdd into main Jun 17, 2026
8 of 11 checks passed
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 17, 2026
Pull Request resolved: pytorch/torchtitan#3632
ghstack-source-id: c8efc0d
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.

3 participants