Fix gradient accumulation under FSDP2: defer cross-replica all-reduce - #162
Open
Sullivan07043 wants to merge 6 commits into
Open
Fix gradient accumulation under FSDP2: defer cross-replica all-reduce#162Sullivan07043 wants to merge 6 commits into
Sullivan07043 wants to merge 6 commits into
Conversation
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.
Collaborator
|
Thanks for the fix — the overall approach makes sense to me. Just two small things I think we should address before merging:
Thanks! |
…finally - Capture each FSDP module's prior all_reduce_grads (mirroring the setter's write target) and restore exactly that value on exit, so nested or external sync contexts are preserved. - Record (module, prev) as mutations happen inside the protected block; if the setup loop raises midway, the finally block restores exactly the modules already changed. - Same treatment for the DDP branch via a mutation flag. Verified with a single-rank fully_shard test: flip/restore, nested contexts preserving outer state, exception-path restore, enabled=True no-op.
Contributor
Author
|
Thanks for the review — both points addressed in 799dbba:
Verified with a single-rank |
foreverlms
pushed a commit
that referenced
this pull request
Aug 7, 2026
The locked hf 1.25.1 in `cosmos_framework/utils/hf_cli` fails on repositories migrated to Hugging Face Xet storage. Reproduced with the framework's own download path (`checkpoint_db._hf_download`) against `nvidia/Cosmos-Guardrail1`: ``` RuntimeError: Task error: Unable to parse string as hex hash value ``` The action-policy inference server hard-requires that download at startup, so serving is currently broken on a fresh machine. **Change.** Bump the pin to `hf==1.26.0` and regenerate `uv.lock` with the documented procedure from the file header (`uv lock --upgrade-package hf`). Two files, no code change. **Validation.** On a fresh 8xA100 instance, cleared the model from the HF cache and re-ran the framework's exact download command with the new lock: full 3.5 GB snapshot (102 files) downloaded clean and returned the snapshot path. Found while validating #162 on rented hardware. Co-authored-by: lfengad <liangf@nvidia.com>
lfengad
reviewed
Aug 7, 2026
…or uninitialized Every non-DDP model takes the FSDP branch, including plain single-GPU runs. Guard it with dist.is_available()/is_initialized(): a build without distributed no longer hits the fsdp import on every micro-step, and initialized-but-plain runs skip the per-step modules() walk. Verified: with dist uninitialized the context yields without walking modules(), and exceptions propagate cleanly through the early exit.
Sullivan07043
force-pushed
the
fix-fsdp2-accum
branch
from
August 7, 2026 10:34
cf6ff5a to
a6642bf
Compare
lfengad
approved these changes
Aug 9, 2026
lfengad
self-requested a review
August 9, 2026 03:49
lfengad
requested changes
Aug 9, 2026
lfengad
left a comment
Collaborator
There was a problem hiding this comment.
Wait for the latest sync release of the framework code, then could merge. Thx!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #161 (
ddp_sync_gradis a silent no-op under FSDP2, sotrainer.grad_accum_itersaves no communication).Change. Add an FSDP2 branch to
ddp_sync_grad: on non-boundary micro-steps, defer only the cross-replica all-reduce viaFSDPModule.set_requires_all_reduce(enabled, recurse=False)on everyFSDPModule. Restore it in thefinallyblock. 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):
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.