[spmd_types] SPMD support for ChunkedCELoss - #3472
Conversation
Pull Request resolved: pytorch/torchtitan#3472 ghstack-source-id: 34283ce
| # The hidden activation type depends on the model's sharding config; | ||
| # we'll defer hidden_states typechecking to previous module boundaries. |
There was a problem hiding this comment.
what are the possibilities?
There was a problem hiding this comment.
hidden_states is directly from decoder, so we'd have to condition this on SP (I or S(1))
| 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), | ||
| ) |
There was a problem hiding this comment.
This is gradient, why would it need a type?
Also, why "tp" dim is spmd.R?
There was a problem hiding this comment.
fair point, removed typechecking for this
| # 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 |
There was a problem hiding this comment.
global_vocab_size should be a config of ChunkedCELoss, set in config_registry.py's not rather than patched in trainer
There was a problem hiding this comment.
hardcoded values in model registries
Pull Request resolved: pytorch/torchtitan#3472 ghstack-source-id: 98e6e13
| def qwen3_debugmodel() -> Trainer.Config: | ||
| return Trainer.Config( | ||
| loss=ChunkedCELoss.Config(), | ||
| loss=ChunkedCELoss.Config(global_vocab_size=2048), |
There was a problem hiding this comment.
should come from model_spec rather than hardcoding
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
left a comment
There was a problem hiding this comment.
see #3472 (comment)
let's not hardcode vocab_size
Pull Request resolved: pytorch/torchtitan#3472 ghstack-source-id: bd941ec
Pull Request resolved: pytorch/torchtitan#3472 ghstack-source-id: fa65d5a
| loss_config = getattr(config, "loss", None) | ||
| if isinstance(loss_config, ChunkedCELoss.Config): | ||
| loss_config.global_vocab_size = self.vocab_size |
There was a problem hiding this comment.
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.
Pull Request resolved: pytorch/torchtitan#3472 ghstack-source-id: 6ade7ab
Moves the seq-dim allgather onto the pre-lm-head norm, making it ShardingConfig-based. Annotates types in ChunkedCELoss impl.
Stack from ghstack (oldest at bottom):