Skip to content

[LoRA] load safetensors by default in load_lora_adapter - #15468

Merged
sayakpaul merged 1 commit into
huggingface:mainfrom
mertkayacs:fix/peft-load-lora-adapter-safetensors-default
Dec 4, 2026
Merged

sayakpaul merged 1 commit into
huggingface:mainfrom
mertkayacs:fix/peft-load-lora-adapter-safetensors-default

Conversation

@mertkayacs

Copy link
Copy Markdown
Contributor

What does this PR do?

load_lora_adapter couldn't load a folder written by save_lora_adapter with default arguments. The save writes pytorch_lora_weights.safetensors, but the load passed use_safetensors=None through to _fetch_state_dict, which then only looked for pytorch_lora_weights.bin.

The fix applies the same default load_lora_weights and the other loaders already use: when use_safetensors is not set, try safetensors first and allow falling back to a .bin file.

The shared LoraTesterMixin.test_save_load_lora_adapter round trip passed use_safetensors=True on load, which hid this. I removed that argument so the test covers a default save and default load. Explicit use_safetensors=True stays covered by the other tests in the mixin.

Fixes #15463

Tests

# without the fix, with the test change
pytest tests/models/transformers/test_models_transformer_flux.py::TestFluxTransformerLoRA::test_save_load_lora_adapter
OSError: Error no file named pytorch_lora_weights.bin found in directory ...
2 failed

# with the fix
same command: 2 passed
pytest tests/models/transformers/test_models_transformer_flux.py::TestFluxTransformerLoRA tests/models/unets/test_models_unet_2d_condition.py -k "LoRA or lora"
27 passed, 23 skipped, 129 deselected, 2 xpassed
pytest tests/lora: 15 passed
make quality: clean

Self-review notes

I ran the self-review rubric over the diff. No blocking issues and no dead code. The change adds no comments or docstrings, and load_lora_adapter's docstring doesn't document use_safetensors, so no docs needed updating. Nothing was left unfixed.

Before submitting

Who can review?

@sayakpaul


if use_safetensors is None:
use_safetensors = True
allow_pickle = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it have to do with allow_pickle?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once use_safetensors defaults to True, _fetch_state_dict tries the .safetensors file first, and on an IOError it only falls back to the .bin file when allow_pickle is set. Without it, adapters that only ship pytorch_lora_weights.bin would stop loading through load_lora_adapter, while they load today. This mirrors what load_lora_weights already does in lora_pipeline.py (allow_pickle = True when use_safetensors is not passed). An explicit use_safetensors=True still keeps the strict behaviour.

@mertkayacs

Copy link
Copy Markdown
Contributor Author

Thanks for the review! The two red checks don't come from this change. The LoRA CUDA job fails on one Flux 3 test that gets a 401 from a gated repo (flux-3-image-klein-base), and the doc build fails inside kernels (use_kernelized_func), which other open PRs hit too.

@sayakpaul
sayakpaul merged commit 938d2b9 into huggingface:main Dec 4, 2026
35 of 37 checks passed
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.

load_lora_adapter can't load a folder written by save_lora_adapter unless use_safetensors=True is passed

2 participants