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

Aperta Adatta ai principianti
#15,463 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Leggi PeftAdapterMixin.load_lora_adapter e _fetch_state_dict, quindi esamina il test condiviso test_save_load_lora_adapter. Riproduci il problema salvando e caricando un adattatore con gli argomenti predefiniti; aggiorna il loader e il test in modo che una cartella salvata con save_lora_adapter venga caricata per impostazione predefinita. Il lavoro è concluso quando il test di regressione passa senza impostare use_safetensors=True.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

save_lora_adapter on a model writes pytorch_lora_weights.safetensors by default. Calling load_lora_adapter on that same folder with default arguments fails because it only looks for pytorch_lora_weights.bin. Passing use_safetensors=True works.

The cause is that PeftAdapterMixin.load_lora_adapter passes use_safetensors=None to _fetch_state_dict, and the safetensors branch there only runs when the flag is truthy or weight_name ends in .safetensors. The pipeline loaders in lora_pipeline.py, load_attn_procs and textual inversion all turn None into use_safetensors=True with pickle fallback. load_lora_adapter is the only one that doesn't.

I found this while reading the loader code around _fetch_state_dict and then reproduced it with the script below. The shared test_save_load_lora_adapter test passes use_safetensors=True on load, so it doesn't catch it.

Reproduction
import tempfile
from torch import nn
from diffusers.configuration_utils import ConfigMixin
from diffusers.loaders.peft import PeftAdapterMixin
from diffusers.models.modeling_utils import ModelMixin
from peft import LoraConfig


class TinyModel(ModelMixin, ConfigMixin, PeftAdapterMixin):
    config_name = "config.json"

    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(2, 2)


model = TinyModel()
model.add_adapter(LoraConfig(r=1, lora_alpha=1, target_modules=["linear"]))
with tempfile.TemporaryDirectory() as tmp:
    model.save_lora_adapter(tmp)
    model.unload_lora()
    model.load_lora_adapter(tmp, prefix=None)  # OSError

Real models such as FluxTransformer2DModel and UNet2DConditionModel go through the same PeftAdapterMixin.load_lora_adapter path.

Logs
OSError: Error no file named pytorch_lora_weights.bin found in directory /tmp/tmp07lmtgut.
System Info

diffusers main (facc812), peft 0.22.0, torch 2.15.0 (CPU), Python 3.12, Linux.

Who can help?

@sayakpaul @BenjaminBossan

I have a fix ready (four lines in load_lora_adapter plus the shared test change) and can open a PR once a maintainer is fine with the approach.

Lingua principale
Python
Stelle
35k
Fork
7.5k
Merge medio
3g 2h
PR unite (30g)
58

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di huggingface/diffusers

Tutte le issue di huggingface/diffusers

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.