Fix: avoid late CUDA OOM in load_best_model_at_end with PEFT models#44660
Open
DogWala wants to merge 2 commits intohuggingface:mainfrom
Open
Fix: avoid late CUDA OOM in load_best_model_at_end with PEFT models#44660DogWala wants to merge 2 commits intohuggingface:mainfrom
DogWala wants to merge 2 commits intohuggingface:mainfrom
Conversation
Member
|
Comment to reviewers: Let's resolve the discussion in #44637 first before proceeding with this PR. |
This was referenced Apr 29, 2026
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.
What does this PR do?
Fixes #44637
This PR makes the PEFT
load_best_model_at_endpath inTraineruse a CPU-first adapter reload path during best-model loading.Previously, when training a PEFT model,
Trainercould reload the best adapter through a path that materialized adapter weights on CUDA during the final best-model load. Under low remaining GPU memory, this could trigger a late OOM even though the training loop had already completed.To be specific:
The OOM happens because
PeftModel.load_adapter()does not load weights directly into the existing adapter parameters in place. Instead, it first callsload_peft_weights(), which materializes a full temporary adapterstate_dicton the target device, and only then passes thatstate_dictintoset_peft_model_state_dict()/model.load_state_dict(...)to copy the values into the actual model parameters.When
torch_deviceis not specified, the current PEFT path inferscuda, so the checkpoint tensors are first loaded as a separate set of CUDA tensors. Under low remaining GPU memory, this extra device-side materialization can OOM before the weights are fully copied into the model, even though training itself has already finished.A CPU-first load path is more memory-safe here: load the adapter checkpoint onto CPU first, then copy the weights into the model parameters. That avoids creating a full temporary CUDA
state_dictat the most memory-constrained point ofload_best_model_at_end.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@SunMarc @BenjaminBossan