Skip to content

[FSDP] Add reduce-sum gradient reduction helper#1961

Draft
HAOCHENYE wants to merge 1 commit into
xtuner-yehc-devfrom
reduce-sum-01-fsdp-helper
Draft

[FSDP] Add reduce-sum gradient reduction helper#1961
HAOCHENYE wants to merge 1 commit into
xtuner-yehc-devfrom
reduce-sum-01-fsdp-helper

Conversation

@HAOCHENYE

Copy link
Copy Markdown
Collaborator

This PR (1/5) — behavior-neutral. Adds BaseModel.set_gradient_reduce_sum(), which pairs set_gradient_divide_factor(1.0) with set_force_sum_reduction_for_comms(True) over every FSDPModule (torch>=2.10 guarded, avoids the bf16 NCCL PreMulSum zeroing trap). Not wired into any fully_shard yet.

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

Add BaseModel.set_gradient_reduce_sum(), which switches every sharded
FSDPModule under the model to pure SUM gradient reduction by pairing
set_gradient_divide_factor(1.0) with set_force_sum_reduction_for_comms(True).
The pairing is required: setting only the divide factor routes FSDP through
NCCL PreMulSum, which silently zeros bf16 gradients on torch 2.10, whereas
forcing plain ReduceOp.SUM is exact in bf16 without upcasting.

The helper is not yet wired into any fully_shard path, so training behavior
is unchanged. Includes a 2-rank bf16 regression test asserting the reduced
gradient equals the exact SUM of per-rank local gradients (non-zero, non-mean).
@HAOCHENYE
HAOCHENYE force-pushed the reduce-sum-01-fsdp-helper branch from c216448 to 184b6ca 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