Skip to content

[spmd_types] SPMD support for ChunkedCELoss - #3472

Open
pianpwk wants to merge 71 commits into
gh/pianpwk/23/basefrom
gh/pianpwk/23/head
Open

[spmd_types] SPMD support for ChunkedCELoss#3472
pianpwk wants to merge 71 commits into
gh/pianpwk/23/basefrom
gh/pianpwk/23/head

Conversation

@pianpwk

@pianpwk pianpwk commented May 31, 2026

Copy link
Copy Markdown
Contributor

[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 3 commits June 1, 2026 09:56
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 3 commits June 1, 2026 16:56
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added 2 commits June 3, 2026 14:07
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk pianpwk changed the title Add SPMD support for chunked CE loss [spmd_types] SPMD chunked loss Jun 4, 2026
@pianpwk
pianpwk marked this pull request as ready for review June 4, 2026 23:34
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 18, 2026
Pull Request resolved: pytorch/torchtitan#3472
ghstack-source-id: 34283ce
Comment thread torchtitan/components/loss.py Outdated
Comment on lines +721 to +722
# The hidden activation type depends on the model's sharding config;
# we'll defer hidden_states typechecking to previous module boundaries.

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 possibilities?

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.

hidden_states is directly from decoder, so we'd have to condition this on SP (I or S(1))

Comment thread torchtitan/components/loss.py Outdated
Comment on lines +645 to +650
if get_spmd_backend() == "spmd_types":
spmd.assert_type(
accumulated_grad,
{"dp": spmd.V, "cp": spmd.V, "tp": spmd.R},
spmd.PartitionSpec("dp", "cp", None),
)

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 is gradient, why would it need a type?
Also, why "tp" dim is spmd.R?

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.

fair point, removed typechecking for this

Comment thread torchtitan/trainer.py Outdated
# PP: only the last stage has lm_head; non-last stages skip this.
if isinstance(self.loss_fn, ChunkedCELoss):
assert isinstance(self.model_config, Decoder.Config)
self.loss_fn.global_vocab_size = self.model_config.vocab_size

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.

global_vocab_size should be a config of ChunkedCELoss, set in config_registry.py's not rather than patched in trainer

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.

hardcoded values in model registries

pianpwk added 2 commits June 18, 2026 01:51
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 18, 2026
Pull Request resolved: pytorch/torchtitan#3472
ghstack-source-id: 98e6e13
pianpwk added 2 commits June 18, 2026 10:09
[ghstack-poisoned]
[ghstack-poisoned]
def qwen3_debugmodel() -> Trainer.Config:
return Trainer.Config(
loss=ChunkedCELoss.Config(),
loss=ChunkedCELoss.Config(global_vocab_size=2048),

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.

should come from model_spec rather than hardcoding

@pianpwk
pianpwk requested a review from tianyu-l June 18, 2026 17:33
pianpwk added a commit that referenced this pull request Jun 18, 2026
loss migration: loss-parallel custom autograd handling, DTensor
local_map / unwrapping, now lives under cross_entropy_loss fn

loss-parallel flag deletion: 
- for trainers, TP on = LP on, uses custom autograd impl instead of
pytorch's implicit `loss_parallel()` context. Sets model sharding
configs to return vocab-sharded logits: `tp_gather_logits=False`
- RL usecases require full logits from lm_head, `tp_gather_logits=True`
- GraphTrainer TP on = LP on

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0)
(oldest at bottom):
* #3690
* #3673
* #3655
* #3664
* #3654
* #3278
* #3472
* __->__ #3694

@tianyu-l tianyu-l 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.

see #3472 (comment)
let's not hardcode vocab_size

submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 18, 2026
Pull Request resolved: pytorch/torchtitan#3472
ghstack-source-id: bd941ec
pianpwk added 2 commits June 18, 2026 23:09
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 19, 2026
Pull Request resolved: pytorch/torchtitan#3472
ghstack-source-id: fa65d5a
@pianpwk
pianpwk changed the base branch from gh/pianpwk/23/base to main June 19, 2026 06:36
@pianpwk
pianpwk requested a review from ezyang June 19, 2026 06:39
@pianpwk
pianpwk changed the base branch from main to gh/pianpwk/23/base June 19, 2026 06:40
@pianpwk
pianpwk changed the base branch from gh/pianpwk/23/base to main June 19, 2026 06:40
@pianpwk
pianpwk changed the base branch from main to gh/pianpwk/23/base June 19, 2026 07:05
@pianpwk
pianpwk changed the base branch from gh/pianpwk/23/base to main June 19, 2026 23:56
Comment on lines +146 to +148
loss_config = getattr(config, "loss", None)
if isinstance(loss_config, ChunkedCELoss.Config):
loss_config.global_vocab_size = self.vocab_size

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 function is to give CLI config a chance to override python file config. The vocab size shouldn't be adjusted by CLI, so we could've set it entirely in config_registry files.

If you think the radius is too large, I'm fine with this for now but please add a NOTE / TODO.

@pianpwk
pianpwk changed the base branch from main to gh/pianpwk/23/base June 20, 2026 07:22
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 20, 2026
Pull Request resolved: pytorch/torchtitan#3472
ghstack-source-id: 6ade7ab
@ezyang
ezyang requested review from ezyang and removed request for ezyang July 9, 2026 15:43
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