Skip to content

fix: skip redundant CPU materialization for FSDP2 during from_pretrained - #49298

Closed
Subodh-17 wants to merge 1 commit into
huggingface:mainfrom
Subodh-17:fix/fsdp2-meta-loading-oom
Closed

Subodh-17 wants to merge 1 commit into
huggingface:mainfrom
Subodh-17:fix/fsdp2-meta-loading-oom

Conversation

@Subodh-17

@Subodh-17 Subodh-17 commented Oct 4, 2026 •

Copy link
Copy Markdown

CPU CI GPU run-slow

Summary

Fixes #49297 by preventing FSDP2 non-rank-0 processes from materializing the entire model onto the host CPU during from_pretrained.

Root Cause

FSDP1 and FSDP2 both export identical distributed environment variables (ACCELERATE_USE_FSDP=1). When loading missing keys from meta devices in _move_missing_keys_from_meta_to_device(), transformers assumes this implies FSDP1 and iterates over all parameters to allocate torch.zeros_like(param, device="cpu"). For FSDP2—which natively broadcasts meta-tensors from rank-0—this is entirely redundant and causes immediate Host OOMs for large models.

Changes

  • Localized an explicit FSDP2 lifecycle check within the CPU-materialization block in src/transformers/modeling_utils.py using accelerate.state.is_initialized().
  • For FSDP2, the loop now materializes ONLY non-persistent buffers (preserving existing fallback behavior) and safely returns early to leave parameters and persistent buffers on meta.
  • Added a regression test specifically mocking PartialState to ensure FSDP2 paths leave weights on the meta device.
  • The global is_fsdp_enabled() helper remains strictly generic to avoid breaking upstream accelerate dispatch paths.

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 37201087984:2
Result: success | Jobs: 16 | Tests: 196,923 | Failures: 0 | Duration: 16h 11m

@Rocketknight1

Copy link
Copy Markdown
Member

Existing PR at #45649 so I'm going to try to wake that one up instead

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] cpu_ram_efficient_loading materializes full model on CPU for every non-rank-0, defeating rank-0 broadcast (host OOM)

2 participants