Skip to content

Fix gradient accumulation under FSDP2: defer cross-replica all-reduce - #162

Open
Sullivan07043 wants to merge 1 commit into
NVIDIA:mainfrom
Sullivan07043:fix-fsdp2-accum
Open

Fix gradient accumulation under FSDP2: defer cross-replica all-reduce#162
Sullivan07043 wants to merge 1 commit into
NVIDIA:mainfrom
Sullivan07043:fix-fsdp2-accum

Conversation

@Sullivan07043

Copy link
Copy Markdown

Fixes #161 (ddp_sync_grad is a silent no-op under FSDP2, so trainer.grad_accum_iter saves no communication).

Change. Add an FSDP2 branch to ddp_sync_grad: on non-boundary micro-steps, defer only the cross-replica all-reduce via FSDPModule.set_requires_all_reduce(enabled, recurse=False) on every FSDPModule. Restore it in the finally block. The intra-node reduce-scatter still runs each micro-step, so gradients stay sharded and per-rank memory does not grow. DDP behavior is unchanged.

Validation (2x 8xA100-80GB, Ethernet, HSDP shard=8 replicate=2, batch 64/rank, fleet whose accum=1 step is 22.2 s):

Config s/step
accum=1 22.2
accum=4, without this fix (measured behavior = 4x) 88.8
accum=4, with this fix 54.3 / 54.3 / 54.9

Decomposition from the two measured points: compute 10.8 s + sync 11.4 s per micro-batch. Throughput 1.63x at accum=4. Loss stayed in the expected band.

One commit, 18 added lines, no API change.

ddp_sync_grad only handled DistributedDataParallel. Under FSDP2
(fully_shard) it was a silent no-op, so every micro-step of an
accumulation window ran the full cross-node gradient reduction and
grad_accum_iter saved no communication (measured: accum=4 step =
4x the accum=1 step on 2x8 A100 over Ethernet).

Defer only the cross-replica all-reduce via
FSDPModule.set_requires_all_reduce on non-boundary micro-steps. The
intra-node reduce-scatter still runs every micro-step, so gradients
stay sharded and per-rank memory does not grow.

Validated on 2x 8xA100-80GB (HSDP shard=8 replicate=2): accum=4 step
54.5 s vs 88.8 s unamortized on a fleet whose accum=1 step is 22.2 s.
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.

trainer.grad_accum_iter saves no communication under FSDP2: ddp_sync_grad is a no-op for fully_shard modules

1 participant