Skip to content

fix FSDP loading with meta devices#44473

Merged
Cyrilvallez merged 9 commits intohuggingface:mainfrom
winglian:fix-meta
Mar 9, 2026
Merged

fix FSDP loading with meta devices#44473
Cyrilvallez merged 9 commits intohuggingface:mainfrom
winglian:fix-meta

Conversation

@winglian
Copy link
Copy Markdown
Collaborator

@winglian winglian commented Mar 5, 2026

What does this PR do?

supersedes #44446

on main, when loading to cpu and using meta devices for non-rank0 processes, it now re-initializes weights on those processes as well as uses more CPU memory. In testing with loading llama3-8b.

main; both on CPU, uses 16GB system RAM, slow to load, re-inits weights on rank1
#44446: rank0 on CPU, rank1 on meta, uses 1.5GB system RAM
v4.57.6, both on CPU, uses 1.5GB system RAM
this PR, both on CPU, uses 1.5GB system RAM, same behavior and training loss as main and v4.57.6

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@winglian
Copy link
Copy Markdown
Collaborator Author

winglian commented Mar 5, 2026

Comment thread src/transformers/modeling_utils.py Outdated
Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

I believe we should just completely skip the init in this case rather than mark everything as initialized, then try to initialize??

Comment on lines +4552 to +4560
# Handle FSDP edge case when using cpu ram efficient loading to ensure it is marked as initialized
# since it will get its weights broadcasted from rank0
for key in self.state_dict():
try:
param_or_buffer = self.get_parameter_or_buffer(key)
param_or_buffer._is_hf_initialized = True
except AttributeError:
pass # may happen when handling pre-quantized weights
self._is_hf_initialized = True
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.

Should we simply return here instead, to completely avoid calling initialize_weights later in the function? Would be easier than setting all weights as initialized before calling initialize which will be skipped anyway as params are marked as already initialized

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Screenshot 2026-03-09 at 10 09 47 AM the early return leads to unstable training with NaN grad_norm on first step and 0.0 loss on second step.

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

Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks a lot for this!
For posterity, the issue is only the non-persistent buffers, as they are NOT gathered from rank0 (only the state_dict), so we need to go through all inits for them (but skipping everything in the state_dict)!

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.

4 participants