Repository navigation
[loss] Fix ChunkedCELoss + TP gradient placement mismatch - #3412
Conversation
[ghstack-poisoned]
|
@pianpwk I'm pretty sure that spmd types would have caught this |
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
left a comment
There was a problem hiding this comment.
Thanks for fixing! Agree that spmd_types could help, as spmd.R means backward should be spmd.P.
Some nit comments.
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thanks for fixing! Agree that spmd_types could help, as spmd.R means backward should be spmd.P.
Some nit comments.
[ghstack-poisoned]
**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
|
Thanks for the fix! The fix looks good to me, and the gradient's placements were previously wrongly labeled as the activation's placement |
…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.
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.
We can also infer the placement if doing this in backward causes tracing issues