Fix CPU-offload tensor placement in the FSDP2 full-state loader - #2901
Merged
Conversation
Current Accelerate calls the FSDP2 full-state loader with a cpu_offload argument, while older callers use the previous three-argument form. The local override handled neither combination safely under an active offload policy. - Extend the replacement loader signature with an optional cpu_offload value so both current and legacy call forms remain valid. - When the argument is omitted, infer placement from the active CPUOffloadPolicy; explicit true and false continue to override that policy. - Apply the resolved placement to sharded and unsharded tensors on main and non-main ranks before load_state_dict(assign=True). This keeps parameter placement consistent across the complete FSDP2 model. - Keep CPU placement independent of RAM-efficient loading because offload is an FSDP2 runtime policy rather than a loader memory-mode side effect.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates SimpleTuner’s override of Accelerate’s FSDP2 full-state-dict loader to accept Accelerate 1.14.0’s cpu_offload keyword (while keeping legacy call compatibility) and to move loaded shards to CPU when CPU offload is in effect.
Changes:
- Extends the loader override signature with
cpu_offload: Optional[bool] = Noneto support both keyword and legacy call forms. - Infers
cpu_offloadfrom the active FSDP CPU offload policy when the argument is omitted. - Ensures loaded shards are moved to CPU when
cpu_offloadresolves toTruein both main and non-main rank paths.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix CPU-offload tensor placement in the FSDP2 full-state loader
Summary
1.14.0's explicit
cpu_offloadcall nor legacy three-argument calls safelyunder an active offload policy.
Accelerator.prepare()could reject the keyword, or loaded tensorscould be placed on the wrong device.
loaded tensor.
Changes
patch_fsdp2_state_dict_loader().replacement(): addcpu_offload: Optional[bool] = Noneso current and legacy call forms work.Trueselects CPU, explicitFalseselects theaccelerator device, and
Noneinfers placement from the activeCPUOffloadPolicy.non-main ranks before
load_state_dict(assign=True).FSDP2 CPU-offload policy.
Result
CPUOffloadPolicy, an omitted value now infers CPU;explicit
Trueselects CPU and explicitFalsekeeps tensors on theaccelerator. Before the fix, the explicit keyword could raise during
Accelerator.prepare().RAM-efficient on/off, CPU offload on/off, and mixed DTensor/unsharded state;
all enabled cases placed tensors on CPU and completed a finite optimizer step.
Environment
3.13.142.13.0+cu1301.14.00.39.05.13.1590.48.01