Skip to content

[FSDP][Loss] Switch gradient reduction to SUM#1963

Draft
HAOCHENYE wants to merge 1 commit into
reduce-sum-02-split-loggingfrom
reduce-sum-03-switch-sum
Draft

[FSDP][Loss] Switch gradient reduction to SUM#1963
HAOCHENYE wants to merge 1 commit into
reduce-sum-02-split-loggingfrom
reduce-sum-03-switch-sum

Conversation

@HAOCHENYE

Copy link
Copy Markdown
Collaborator

This PR (3/5) — the single atomic semantic flip. FSDP reduce-scatters with pure SUM; scale_and_reduce_grad drops the expert div_(ep_size) and replicated div_(flat_mesh.size()); CE drops the WORLD autograd all_reduce; balancing uses local_gating_sum with global detached stats; z-loss drops x world_size; compose/VLM fully_shard also wired to SUM. Carries all regression tests (bf16 mechanism, token-mean parity EP1/EP2, balancing & z-loss new-vs-old A/B, compose hook).

Full stack (merge bottom-up, under #1959):

  1. reduce-sum-01-fsdp-helper — [FSDP] Add reduce-sum gradient reduction helper
  2. reduce-sum-02-split-logging — [Loss] Split logging loss from backward loss
  3. reduce-sum-03-switch-sum — [FSDP][Loss] Switch gradient reduction to SUM
  4. reduce-sum-04-remove-world-size — [Refactor] Remove unused world_size plumbing
  5. reduce-sum-05-drop-nonglobal — [Refactor] Drop non-global aux-loss averaging mode

Flip gradient reduction from mean to pure SUM, so each parameter receives the
sum of per-rank local-component gradients, i.e. the global loss gradient, with
no compensating divides. This is the single atomic semantic change; the three
cancelling x world_size factors and the reduce divides are removed together.

- BaseModel/MoE/Dense.fully_shard call set_gradient_reduce_sum() at the end.
- MoE.scale_and_reduce_grad drops the expert div_(ep_size) and the replicated
  div_(flat_mesh.size()); only the coalesced SUM all_reduce remains.
- CE: drop the WORLD autograd all_reduce; the loss stays this rank's local
  component (display global value restored by the C2 detached pipeline).
- Balancing: use local_gating_sum directly instead of all_reduce_autograd;
  the global detached statistics (tokens_global/seqlen_global/scale_global) are
  kept unchanged.
- Z-loss: drop the x world_size in the global-average branch.

Verified on torch 2.10 (bf16 force-sum): distributed full gradient reproduces a
single-process full-batch token-mean CE reference at EP=1 and EP=2 (norm-ratio
median 0.9998, EP-invariant); global display loss unchanged through the flip;
balancing+z backward stays finite. Regression tests cover the bf16 reduce-sum
mechanism, token-mean parity with grad-acc=2, aux-loss finite gradients, and an
isolated fp32 balancing-only gate-gradient A/B (new local+SUM vs old
all_reduce_autograd+AVG) asserting element-wise equality.
@HAOCHENYE
HAOCHENYE force-pushed the reduce-sum-02-split-logging branch from a13bca8 to 316b1cc Compare July 17, 2026 08:11
@HAOCHENYE
HAOCHENYE force-pushed the reduce-sum-03-switch-sum branch from 51c5491 to 590c9e7 Compare July 17, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant