Repository navigation
[graph_trainer] Coalesce lm_head chunked-loss collectives to match eager (bitwise) - #3636
Closed
SherlockNoMad wants to merge 1 commit into
Closed
SherlockNoMad wants to merge 1 commit into
SherlockNoMad wants to merge 1 commit into
Conversation
SherlockNoMad
requested review from
IvanKobzarev,
aditvenk,
sanketpurandare,
tianyu-l and
xmfan
as code owners
June 11, 2026 17:56
SherlockNoMad
marked this pull request as draft
June 11, 2026 17:57
Contributor
Author
|
Superseded by #3637: the lm_head chunked-loss coalescing fix is now part of the graph_trainer/dsv3_scaling ghstack stack. Closing this standalone PR to avoid a duplicate of the chunked_loss.py change. |
IvanKobzarev
pushed a commit
to IvanKobzarev/torchtitan
that referenced
this pull request
Jun 17, 2026
…twise vs eager) Make graph_trainer (simple_fsdp) bitwise-identical to the eager Trainer (FSDP2) for the chunked-loss path, and record the verification + benchmark as Run 7. - chunked_loss.py: ChunkedCELossWithParamGrads reads the all-gathered lm_head weight once, runs the per-chunk forward+backward against a detached leaf (no per-chunk collective; chunking/memory preserved), accumulates the per-chunk grads unsharded in fp32, and does a single Partial->Shard reduce-scatter -- reproducing eager's chunks-then-ranks summation order. Previously simple_fsdp reduce-scattered once per chunk (ranks-then-chunks), an f32-ulp drift that compounded (loss diverged from eager ~step 7 at 16B). Upstream: PR pytorch#3636. - run_graph_trainer_dsv3.sh: switch the benchmark to non-MinimalAsyncEP (graph_trainer_deepseek_v3_16b) + cudagraph disabled -- the eager-comparable path the fix makes exact. - dsv3_numerics_verification.md: root-cause, ablations (tying / NCCL / fused-vs-decomposed CE / num_chunks=1), and fix writeup. - dsv3_scaling_experiment_results.md: Run 7 -- bitwise verification (loss + grad_norm 0/20 at 16B over 20 steps) + benchmark + artifacts; resolves the UNVERIFIED caveat on Runs 2-6 for the chunked-loss path. ghstack-source-id: 1b11892 Pull-Request: pytorch#3637
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.
Stack from ghstack (oldest at bottom):
graph_trainer's
ChunkedCELossWithParamGradsdiverged from the eagerTrainerin
lm_head.weight.grad(and hence loss/grad_norm), causing dsv3-16B trainingto drift from eager at the last bit (loss diverged ~step 7).
Root cause: graph_trainer shards lm_head with
simple_fsdp(a DTensorparametrization), not FSDP2. The base
ChunkedCELosscoalesces the per-chunklm_head grad reduce-scatters via FSDP2's
set_requires_gradient_sync, which isinert under simple_fsdp -- so each
lm_head(h_chunk)access fired onereduce-scatter per chunk and summed the sharded grads ranks-then-chunks, while
eager FSDP2 accumulates the per-chunk grads unsharded in fp32 and
reduce-scatters once (chunks-then-ranks). Both fp32 -- only the summation order
differed, an fp32-ulp gap that compounds over steps.
Fix:
ChunkedCELossWithParamGradsnow reads the all-gathered lm_head weightonce, runs the per-chunk forward+backward against a detached leaf (no per-chunk
collective -- chunking/memory preserved), accumulates the per-chunk grads
unsharded in fp32, and performs a single
Partial->Shardreduce-scatter(
_reduce_scatter_grad_like;Partialon the FSDP/DP axes, keptShardon TP),reproducing eager's summation order.
Validation (bitwise vs eager
Trainer,--debug.deterministic --debug.seed=42):max|diff|=0(0/20).lm_head.weight.gradbitwise.tests/test_chunked_loss.py: 3/3 (bitwise-equal to baseChunkedCELoss).The fix also yields ~13% lower peak memory and ~3.5% higher MFU than eager at
matched batch (B=4, seq 4096).
Depends on #3248 (which enables ChunkedCELoss / ChunkedCELossWithParamGrads for
graph_trainer).