Repository navigation
ChunkedCELoss - #2937
Merged
Merged
ChunkedCELoss#2937
Conversation
wwwjn
commented
Apr 11, 2026
tianyu-l
reviewed
Apr 18, 2026
tianyu-l
reviewed
Apr 20, 2026
weifengpy
reviewed
Apr 21, 2026
tianyu-l
reviewed
Apr 22, 2026
wwwjn
commented
Apr 22, 2026
| **extra_kwargs, | ||
| target=targets, | ||
| losses=losses, | ||
| loss_kwargs=loss_kwargs, |
Contributor
Author
There was a problem hiding this comment.
Here's the callsite of loss_kwargs cc @tugsbayasgalan
pytorchmergebot
pushed a commit
to pytorch/pytorch
that referenced
this pull request
Apr 24, 2026
…0428) torchtitan wraps `fully_shard([norm, lm_head])`. Two execution patterns: - **Full forward** (no chunked loss): `norm` and `lm_head` run together - **Chunked loss**: the model forward runs `norm`-only. Then `lm_head(chunk)` is called standalone per chunk with per-chunk backward Depending on `_modules_to_run_forward` to drive unshard/reshard is wrong for the chunked-loss pattern ## Changes 1. **Unshard gated on `is_unsharded`, not `_modules_to_run_forward`.** The pre-hook unshards whenever the group isn't already unsharded, independent of `_modules_to_run_forward` 2. **Root post-forward cleanup for incomplete groups.** `_force_complete_incomplete_states` runs at the root's `post_forward` 3. **Clear `iter_forward_root` in post-backward unconditionally.** When `model.lm_head(chunk)` claims `iter_forward_root` via its pre_forward, the group's `_post_forward` never fires to clear it (partial group) ## Why this design Any call pattern that runs some submodule of the wrapped tree leaves correctly reduced sharded grads after `.backward()` — across: - Normal `model(x).backward()`. - Unused-param cases (some modules don't run). - Chunked-loss (standalone per-chunk calls). - Grad accumulation (`set_requires_gradient_sync(False)`). - Multiple forward passes before one backward. chunked-loss fires N+1 reduce-scatters (one per chunk + one for `h.backward`) instead of 1. Users who know their pattern can reclaim the single-RS behavior explicitly via `set_requires_gradient_sync(False)`: see torchtitan pytorch/torchtitan#2937 Pull Request resolved: #180428 Approved by: https://github.com/tianyu-l
wwwjn
marked this pull request as ready for review
April 24, 2026 15:59
wwwjn
requested review from
SherlockNoMad,
aditvenk,
daniellepintz,
fegin,
felipemello1,
joecummings,
wconstab,
xmfan and
yiming0416
as code owners
April 24, 2026 15:59
2 tasks done
saforem2
added a commit
to saforem2/torchtitan
that referenced
this pull request
Apr 28, 2026
The 21st upstream sync (b6c0469) brought in three breaking changes that broke the ezpz models at import / config-build time: - pytorch#2937 (ChunkedCELoss) removed `build_cross_entropy_loss` and replaced `ModelSpec.build_loss_fn` with the new `JobConfig.loss` field. Drop the `from torchtitan.components.loss import build_cross_entropy_loss` imports and the `build_loss_fn=...` kwargs. Update the ezpz trainer to call `config.loss.build(compile_config=config.compile)` instead. - `Decoder.Config` renamed `output: Linear.Config` to `lm_head: Linear.Config`. Rename `output=Linear.Config(...)` -> `lm_head=Linear.Config(...)` in all agpt and moe configs. This is the minimum mechanical fix; the sharding-API refactor (parallelize_module -> Module.parallelize) is replayed in the next commit.
saforem2
added a commit
to saforem2/torchtitan
that referenced
this pull request
Apr 28, 2026
50-step moe smoke test using the smallest non-debug flavor (500M) on 2 nodes, AdamW, no checkpoint, fineweb-edu HF stream. Mirrors smoke_2b_50steps for moe to verify the post-pytorch#2963/pytorch#2937 replay end-to-end: imports, model build, sharding-config population on MLA attention + dense FFN, Module.parallelize, apply_moe_ep_tp, per-block compile, FSDP wrap, optimizer step, loss decreasing. scripts/submit_moe_smoke.sh mirrors competition/submit_run.sh but for the moe module path.
aperson30
pushed a commit
to aperson30/pytorch
that referenced
this pull request
Jun 20, 2026
…orch#180428) torchtitan wraps `fully_shard([norm, lm_head])`. Two execution patterns: - **Full forward** (no chunked loss): `norm` and `lm_head` run together - **Chunked loss**: the model forward runs `norm`-only. Then `lm_head(chunk)` is called standalone per chunk with per-chunk backward Depending on `_modules_to_run_forward` to drive unshard/reshard is wrong for the chunked-loss pattern ## Changes 1. **Unshard gated on `is_unsharded`, not `_modules_to_run_forward`.** The pre-hook unshards whenever the group isn't already unsharded, independent of `_modules_to_run_forward` 2. **Root post-forward cleanup for incomplete groups.** `_force_complete_incomplete_states` runs at the root's `post_forward` 3. **Clear `iter_forward_root` in post-backward unconditionally.** When `model.lm_head(chunk)` claims `iter_forward_root` via its pre_forward, the group's `_post_forward` never fires to clear it (partial group) ## Why this design Any call pattern that runs some submodule of the wrapped tree leaves correctly reduced sharded grads after `.backward()` — across: - Normal `model(x).backward()`. - Unused-param cases (some modules don't run). - Chunked-loss (standalone per-chunk calls). - Grad accumulation (`set_requires_gradient_sync(False)`). - Multiple forward passes before one backward. chunked-loss fires N+1 reduce-scatters (one per chunk + one for `h.backward`) instead of 1. Users who know their pattern can reclaim the single-RS behavior explicitly via `set_requires_gradient_sync(False)`: see torchtitan pytorch/torchtitan#2937 Pull Request resolved: pytorch#180428 Approved by: https://github.com/tianyu-l
gplutop7
pushed a commit
to gplutop7/pytorch
that referenced
this pull request
Jul 15, 2026
…orch#180428) torchtitan wraps `fully_shard([norm, lm_head])`. Two execution patterns: - **Full forward** (no chunked loss): `norm` and `lm_head` run together - **Chunked loss**: the model forward runs `norm`-only. Then `lm_head(chunk)` is called standalone per chunk with per-chunk backward Depending on `_modules_to_run_forward` to drive unshard/reshard is wrong for the chunked-loss pattern ## Changes 1. **Unshard gated on `is_unsharded`, not `_modules_to_run_forward`.** The pre-hook unshards whenever the group isn't already unsharded, independent of `_modules_to_run_forward` 2. **Root post-forward cleanup for incomplete groups.** `_force_complete_incomplete_states` runs at the root's `post_forward` 3. **Clear `iter_forward_root` in post-backward unconditionally.** When `model.lm_head(chunk)` claims `iter_forward_root` via its pre_forward, the group's `_post_forward` never fires to clear it (partial group) ## Why this design Any call pattern that runs some submodule of the wrapped tree leaves correctly reduced sharded grads after `.backward()` — across: - Normal `model(x).backward()`. - Unused-param cases (some modules don't run). - Chunked-loss (standalone per-chunk calls). - Grad accumulation (`set_requires_gradient_sync(False)`). - Multiple forward passes before one backward. chunked-loss fires N+1 reduce-scatters (one per chunk + one for `h.backward`) instead of 1. Users who know their pattern can reclaim the single-RS behavior explicitly via `set_requires_gradient_sync(False)`: see torchtitan pytorch/torchtitan#2937 Pull Request resolved: pytorch#180428 Approved by: https://github.com/tianyu-l
aws-kingrj
pushed a commit
to amazon-contributing/upstream-to-pytorch
that referenced
this pull request
Jul 29, 2026
…orch#180428) torchtitan wraps `fully_shard([norm, lm_head])`. Two execution patterns: - **Full forward** (no chunked loss): `norm` and `lm_head` run together - **Chunked loss**: the model forward runs `norm`-only. Then `lm_head(chunk)` is called standalone per chunk with per-chunk backward Depending on `_modules_to_run_forward` to drive unshard/reshard is wrong for the chunked-loss pattern ## Changes 1. **Unshard gated on `is_unsharded`, not `_modules_to_run_forward`.** The pre-hook unshards whenever the group isn't already unsharded, independent of `_modules_to_run_forward` 2. **Root post-forward cleanup for incomplete groups.** `_force_complete_incomplete_states` runs at the root's `post_forward` 3. **Clear `iter_forward_root` in post-backward unconditionally.** When `model.lm_head(chunk)` claims `iter_forward_root` via its pre_forward, the group's `_post_forward` never fires to clear it (partial group) ## Why this design Any call pattern that runs some submodule of the wrapped tree leaves correctly reduced sharded grads after `.backward()` — across: - Normal `model(x).backward()`. - Unused-param cases (some modules don't run). - Chunked-loss (standalone per-chunk calls). - Grad accumulation (`set_requires_gradient_sync(False)`). - Multiple forward passes before one backward. chunked-loss fires N+1 reduce-scatters (one per chunk + one for `h.backward`) instead of 1. Users who know their pattern can reclaim the single-RS behavior explicitly via `set_requires_gradient_sync(False)`: see torchtitan pytorch/torchtitan#2937 Pull Request resolved: pytorch#180428 Approved by: https://github.com/tianyu-l
This was referenced Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ChunkedCELoss implementation in eager.
Implementation
When FSDP enabled:
- separate [norm, lm_head] FSDP group: separate backward (lm_head, decoder) will double fire reduce-scatters on the same FSDP moduleNeed pytorch/pytorch#180428 to be landedWhen TP is enabled:
Memory Profiling
Qwen3-0.6B model FSDP=4
w/o ChunkedLoss:

With ChunkedCELoss:

Compatibility Test
(llama3 debug model)
TP + no SP
TP + SP
CP

PP
Compile
