Fix: serialize low_cpu_mem_usage LoRA injection to prevent thread-safety leak - #14418
Open
lorenzozanee wants to merge 2 commits into
Open
Fix: serialize low_cpu_mem_usage LoRA injection to prevent thread-safety leak#14418lorenzozanee wants to merge 2 commits into
lorenzozanee wants to merge 2 commits into
Conversation
…ter patch leak PEFT's init_empty_weights() context monkey-patches torch.nn.Module.register_parameter process-wide and restores the value captured at entry on exit, which is not thread-safe. Concurrent adapter injection with low_cpu_mem_usage=True can interleave these capture/restore operations and leak the patch, leaving newly created modules on the meta device. Wrap the low-memory injection path in PeftAdapterMixin.load_lora_adapter and _load_lora_into_text_encoder in a process-wide threading.Lock, and add a regression test that asserts the global patch is not leaked under concurrent injection. Closes huggingface#14347
lorenzozanee
marked this pull request as ready for review
August 8, 2026 08:02
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?
Makes
low_cpu_mem_usage=TrueLoRA injection thread-safe by serializing the low-memory path with a process-widethreading.Lock, so PEFT'sinit_empty_weights()globalregister_parameterpatch can no longer leak across concurrent adapter loads.Fixes #14347
Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.
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.
PEFT-related: @sayakpaul @BenjaminBossan
General functionalities: @sayakpaul @yiyixuxu @DN6
Note: this contribution was prepared with automated tooling and AI assistance for code search, drafting, and initial implementation. The account owner reviewed the reproduction, root cause, diff, and tests before submission.