Skip to content

Fix: serialize low_cpu_mem_usage LoRA injection to prevent thread-safety leak - #14418

Open
lorenzozanee wants to merge 2 commits into
huggingface:mainfrom
lorenzozanee:fix/low-cpu-mem-usage-thread-safety
Open

Fix: serialize low_cpu_mem_usage LoRA injection to prevent thread-safety leak#14418
lorenzozanee wants to merge 2 commits into
huggingface:mainfrom
lorenzozanee:fix/low-cpu-mem-usage-thread-safety

Conversation

@lorenzozanee

@lorenzozanee lorenzozanee commented Aug 7, 2026

Copy link
Copy Markdown

What does this PR do?

Makes low_cpu_mem_usage=True LoRA injection thread-safe by serializing the low-memory path with a process-wide threading.Lock, so PEFT's init_empty_weights() global register_parameter patch can no longer leak across concurrent adapter loads.

Fixes #14347

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • 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?
  • Are you the author (or part of the team) of the model/pipeline (only applicable for model/pipeline related PRs)?

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.

…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
@github-actions github-actions Bot added size/M PR with diff < 200 LOC fixes-issue lora tests and removed size/M PR with diff < 200 LOC fixes-issue labels Aug 7, 2026
@lorenzozanee
lorenzozanee marked this pull request as ready for review August 8, 2026 08:02
@github-actions github-actions Bot added size/M PR with diff < 200 LOC fixes-issue labels Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] low_cpu_mem_usage=True is not thread-safe and can leak the global nn.Module.register_parameter patch

1 participant