Skip to content

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

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

[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
loss = loss * self.loss_cfg.z_loss_alpha / self._batch_size
self._update_running(loss.detach())
self._update_local_running(local_loss.detach())
self._update_local_running(loss.detach())

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the changes in PR 1962 are necessary, it is recommended to merge PR 1962 and PR 1963 into a single one, as it makes no sense for a function defined in PR 1962 to be called in PR 1963 — it feels fragmented

Comment thread xtuner/v1/model/base.py
# Safety net: every `*loss` tensor field an output exposes must have produced a display curve
# from a registered local component. Otherwise a newly added loss term would silently vanish
# from the logged curves while still driving backward, hiding the regression.
for output in batch_outputs:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encapsulate it into an internal function; subclasses can override it if needed

@HAOCHENYE

Copy link
Copy Markdown
Collaborator Author

Superseded by the consolidated 2-PR stack #1968 (core) + #1969 (drop non-global). Closing this fragmented stack.

@HAOCHENYE HAOCHENYE closed this Jul 20, 2026
@HAOCHENYE
HAOCHENYE deleted the reduce-sum-03-switch-sum branch July 20, 2026 16:35
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