Skip to content

Fsdp2 smart root reshard#4015

Open
SunMarc wants to merge 2 commits intomainfrom
fsdp2-smart-root-reshard
Open

Fsdp2 smart root reshard#4015
SunMarc wants to merge 2 commits intomainfrom
fsdp2-smart-root-reshard

Conversation

@SunMarc
Copy link
Copy Markdown
Member

@SunMarc SunMarc commented Apr 22, 2026

What does this PR do?

This PR fixes the reshard_after_forward default when sharding the root modules of the model. In practice, we should not reshard after the forward for the norm + lm_head as we have the backward right after. If the emb are tied, it's best to not too but we have a bit more mem consumption. If the emb are not tied, then we should definitely reshard after.

For now, I'm defaulting all root modules to not reshard -> reshard_after_forward=None.

SunMarc added 2 commits April 22, 2026 14:56
…rward

The FSDP2 path in Accelerate currently passes the plugin's
`reshard_after_forward` (default `True`) uniformly to every
`fully_shard()` call, including the final call on the whole model that
creates the root unit. PyTorch's default (`None`) is smarter: it
resolves to `True` for non-root units and `False` for the root, which
avoids an unhideable pre-backward all-gather of the root's leftover
params (embeddings, final norm, lm_head) that would otherwise be
resharded and immediately re-gathered.

Stripping the kwarg from the root call lets PyTorch apply its heuristic.
Peak memory is unchanged on typical workloads (dominated by backward,
where the root is gathered in either case); we save one all-gather per
step on the root unit. This also matches torchtitan's default wrapping,
which similarly lets the root call fall back to `None`.
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@qgallouedec
Copy link
Copy Markdown
Member

lgtm! for context, it was flagged in huggingface/trl#5575 where we measured a significant slowdown because of reshard_after_forward=True being true by default

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.

3 participants