Skip to content

Allow preparing a model without an optimizer under FSDP2 - #4519

Merged
SunMarc merged 2 commits into
mainfrom
fsdp2-model-only-prepare
Nov 13, 2026
Merged

SunMarc merged 2 commits into
mainfrom
fsdp2-model-only-prepare

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Resolves huggingface/transformers#48841.

Trainer.evaluate() before train() prepares the model alone, and under FSDP2 prepare raises without an optimizer. The optimizer is only required so its parameters can be swapped for the sharded ones after fully_shard, so this keeps the error for an optimizer passed without its model and lets a model through alone. It comes out sharded, as with an optimizer. When train() then prepares it again with its optimizer, activation checkpointing is not applied a second time.

  • 2x B200, tiny Qwen3, Trainer + FSDP2: evaluate() on a fresh Trainer now runs, with the same eval loss as without FSDP (6.957570). With activation checkpointing, evaluate then train ends at the same eval loss as train alone (6.773301).

@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.

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Relaxing the check to "optimizer requires its model" matches what _prepare_fsdp2 actually needs, and the new test covers the model-only prepare. My question is about what happens next in the Trainer flow from the linked issue: evaluate() (model alone), then train() (model + optimizer). The test doesn't exercise that sequence, and I think it may not be idempotent. This is reasoned from the code, not run.

Comment thread src/accelerate/accelerator.py Resolved
@qgallouedec
qgallouedec requested a review from SunMarc November 10, 2026 15:52

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks ! cc @winglian I think you wanted something similar ? maybe relax the condition on passing an optimizer together with the model

@SunMarc
SunMarc merged commit 979c7c7 into main Nov 13, 2026
29 checks passed
@SunMarc
SunMarc deleted the fsdp2-model-only-prepare branch November 13, 2026 16:57
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.

FSDP2: evaluate/predict on a fresh Trainer raises in Accelerator.prepare

4 participants