Skip to content

Prevent double-wrapping models in prepare_model()#3977

Open
joshuaswanson wants to merge 2 commits intohuggingface:mainfrom
joshuaswanson:prepare-model-double-wrap-guard
Open

Prevent double-wrapping models in prepare_model()#3977
joshuaswanson wants to merge 2 commits intohuggingface:mainfrom
joshuaswanson:prepare-model-double-wrap-guard

Conversation

@joshuaswanson
Copy link
Copy Markdown
Contributor

prepare_model() doesn't check if a model has already been prepared, so calling prepare() twice on the same model wraps it in a nested DDP, corrupts the _models registry, and breaks checkpoint save/load with key mismatches like module.module.weight.

Dataloaders, optimizers, and schedulers already have this guard via _is_accelerate_prepared. This adds the same check to prepare_model().

Closes #3967

Copy link
Copy Markdown
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks !

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

@SunMarc
Copy link
Copy Markdown
Member

SunMarc commented Mar 23, 2026

The CI is not passing, can you check @joshuaswanson ?

@joshuaswanson
Copy link
Copy Markdown
Contributor Author

Fixed, should be green now. The flag wasn't being set in prepare_model() directly, only in prepare().

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.

[Bug] prepare() silently double-wraps models and double-serializes checkpoints when called twice on the same object

3 participants