Skip to content

[graph_trainer] Coalesce lm_head chunked-loss collectives to match eager (bitwise) - #3636

Closed
SherlockNoMad wants to merge 1 commit into
gh/SherlockNoMad/55/basefrom
gh/SherlockNoMad/55/head
Closed

SherlockNoMad wants to merge 1 commit into
gh/SherlockNoMad/55/basefrom
gh/SherlockNoMad/55/head

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

graph_trainer's ChunkedCELossWithParamGrads diverged from the eager Trainer
in lm_head.weight.grad (and hence loss/grad_norm), causing dsv3-16B training
to drift from eager at the last bit (loss diverged ~step 7).

Root cause: graph_trainer shards lm_head with simple_fsdp (a DTensor
parametrization), not FSDP2. The base ChunkedCELoss coalesces the per-chunk
lm_head grad reduce-scatters via FSDP2's set_requires_gradient_sync, which is
inert under simple_fsdp -- so each lm_head(h_chunk) access fired one
reduce-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: ChunkedCELossWithParamGrads now 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 performs a single Partial->Shard reduce-scatter
(_reduce_scatter_grad_like; Partial on the FSDP/DP axes, kept Shard on TP),
reproducing eager's summation order.

Validation (bitwise vs eager Trainer, --debug.deterministic --debug.seed=42):

  • dsv3-16B, dp_shard=8 tp=1 ep=4, 20 steps: loss and grad_norm max|diff|=0 (0/20).
  • debugmodel no-TP (dp8/tp1/ep4) and TP (dp4/tp2/ep2): lm_head.weight.grad bitwise.
  • CPU tests/test_chunked_loss.py: 3/3 (bitwise-equal to base ChunkedCELoss).
    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).

[ghstack-poisoned]
@SherlockNoMad

Copy link
Copy Markdown
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.

@SherlockNoMad
SherlockNoMad deleted the gh/SherlockNoMad/55/head branch June 11, 2026 18:39
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant