Skip to content

[loss] Fix ChunkedCELoss + TP gradient placement mismatch - #3412

Merged
fegin merged 4 commits into
mainfrom
gh/fegin/129/head
May 22, 2026
Merged

fegin merged 4 commits into
mainfrom
gh/fegin/129/head

Conversation

@fegin

@fegin fegin commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

TLDR
When verifying CP8 v.s TP8, TP posts a significant higher loss than CP. After comparing with single GPU run, it is clear that TP + ChunkedCELoss doesn't work correctly. This PR fixes the issue.

Summary
GradAccumulator was wrapping the buffer with the reference activation's placement (Replicate), but the buffer actually held the local values of chunk_grad, whose placements can be different than activations' placement. With TP and a loss-parallel ColwiseParallel lm_head, chunk_grad comes back as Partial(sum) on the TP axis, and to_local() returns each rank's partial slice without an all-reduce. The Replicate label then made downstream autograd treat the partial values as already summed, poisoning the decoder backward and pushing the loss curve well above the single-GPU baseline.

This PR fixes the issue by capturing _placements from the first added chunk_grad instead of from the reference activation.

Validated via scripts/loss_compare.py on llama3 debugmodel + global batch size 8 + 500 steps + seed checkpoint + --debug.seed=42 --debug.deterministic.

Config Step 1 loss Step 500 loss
Single GPU 8.054873 2.342949
TP8 before this fix 8.054862 2.727362
TP8 after this fix 8.054862 2.353253

We can also infer the placement if doing this in backward causes tracing issues

fegin added 2 commits May 20, 2026 20:23
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin requested review from tianyu-l, wconstab and wwwjn as code owners May 21, 2026 03:23
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 21, 2026
@ezyang

ezyang commented May 21, 2026

Copy link
Copy Markdown
Contributor

@pianpwk I'm pretty sure that spmd types would have caught this

@acisseJZhong

Copy link
Copy Markdown
Contributor

GradAccumulator was wrapping the buffer with the reference activation's placement (Replicate), but the buffer actually held the local values of chunk_grad.

maybe I didn't have much context, but wondering why we don't wrap the buffer with Partial if this is the case?

@fegin

fegin commented May 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@acisseJZhong

maybe I didn't have much context, but wondering why we don't wrap the buffer with Partial if this is the case?

That's what this PR does. The statement you refer to just states the status quo.

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

Thanks for fixing! Agree that spmd_types could help, as spmd.R means backward should be spmd.P.

Some nit comments.

Comment thread torchtitan/components/loss.py Outdated
DTensor, only its device mesh is reused; the placement of the
returned DTensor is taken from the first added chunk (see add()),
not from this reference, so the buffer is labeled with the actual
gradient placement (e.g. Partial(sum) for a loss-parallel

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.

I think it has nothing to do with loss parallel.
It happens as long as input_dst is Replicate, weight is Shard, per
https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/decoder_sharding.py#L250-L255

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.

ye, you are right, it is just we enable loss parallel and Claude thinks this is the root cause. Sharded weight will cause this problem.

label_chunks = list(_chunk(labels))

grad_accumulator = GradAccumulator(
hidden_states,

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 PR implicitly assumes that
the forward placement of h_chunk is the same as forward placement of hidden_states

This is true, e.g. on TP they are both Replicate, but it adds one layer of indirection. It is still possible to write buggy code.

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

Thanks for fixing! Agree that spmd_types could help, as spmd.R means backward should be spmd.P.

Some nit comments.

fegin added 2 commits May 21, 2026 19:33
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request May 22, 2026
**TLDR**
When verifying CP8 v.s TP8, TP posts a significant higher loss than CP. After comparing with single GPU run, it is clear that TP + ChunkedCELoss doesn't work correctly. This PR fixes the issue.

**Summary**
GradAccumulator was wrapping the buffer with the reference activation's placement (Replicate on the TP axis after ChunkedCELoss's upstream redistribute), but the buffer actually held the local values of chunk_grad. With a Replicate input and a Shard(0) weight (the ColwiseParallel pattern, independent of whether loss_parallel is enabled), the lm_head backward matmul contracts along the sharded vocab dim and emits a per-rank partial sum, so chunk_grad comes back as Partial(sum) on the TP axis; to_local() then returns each rank's partial slice without an all-reduce. The Replicate label made downstream autograd treat that partial slice as the already-summed value, poisoning the decoder backward and pushing the loss curve well above the single-GPU baseline.

**This PR fixes the issue by capturing _placements from the first added chunk_grad instead of from the reference activation.**

Validated via scripts/loss_compare.py on llama3 debugmodel + global batch size 8 + 500 steps + seed checkpoint + --debug.seed=42 --debug.deterministic.

| Config              | Step 1 loss | Step 500 loss |
|---------------------|-------------|---------------|
| Single GPU          |    8.054873 |      2.342949 |
| TP8 before this fix |    8.054862 |      2.727362 |
| TP8 after this fix  |    8.054862 |      2.353253 |

**We can also infer the placement if doing this in backward causes tracing issues**

ghstack-source-id: e7528b3
Pull-Request: #3412
@fegin fegin added the ciflow/h100.8 Trigger H100.8 CI label May 22, 2026
@fegin
fegin changed the base branch from gh/fegin/129/base to main May 22, 2026 04:11
@fegin
fegin merged commit c2a3771 into main May 22, 2026
9 of 11 checks passed
@wwwjn

wwwjn commented May 22, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix! The fix looks good to me, and the gradient's placements were previously wrongly labeled as the activation's placement

saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 22, 2026
…eport

Documents the 38th upstream sync (4 commits, cfe97c6..c2a3771):

- PR pytorch#3389 (MoE [6/n] dispatcher split) — replayed in d87729a;
  smoke report at docs/experiments/moe/sunspot/20260522-smoke-n2-pr3389-replay.md
- PR pytorch#3412 (ChunkedCELoss + TP gradient fix) — internal to upstream
  loss.py, no ezpz replay; flagged interaction with ezpz's separate
  TP-loss-reporting workaround
- PR pytorch#3414 (DeepEP imports) — XPU-irrelevant no-op
- PR pytorch#3416 (graph_trainer fsdp_passes) — Meta-internal experiment, no-op

Smoke results (2N Sunspot, jobs 12467277 + 12467288):

- agpt_2b clean, byte-comparable baseline (140s, peak 24.34 GiB).
- moe_2b_ep REGRESSED at LBS=16: 62.53 GiB OOM vs yesterday's
  15.03 GiB peak with identical config. Almost certainly PR pytorch#3389's
  token-dispatcher restructure responsibility.
- moe_2b_ep at LBS=2 workaround clean (427s, peak 27.08 GiB, 9.4%
  MFU, loss 12.93 → 6.15).

Adds the 38th-sync entry to docs/upstream-sync.md, the smoke report
to docs/experiments/moe/sunspot/, links it from the moe experiments
README, and updates today's journal entry with the sync details.

Action items captured in upstream-sync.md: file upstream issue for
the LBS=16 OOM regression, pin moe_2b_ep LBS=2 in registry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI 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.

5 participants