Safetensors checkpoint loading fails with a torch.device map_location

Offen Anfängerfreundlich
#15,459 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python, pytorch

Rechercherichtung

Beginne in src/diffusers/models/model_loading_utils.py. Dort verarbeitet load_state_dict safetensors-Checkpoints. Vergleiche die Behandlung von map_location mit dem .bin-Pfad. Verwende die Reproduktion mit einem Tensor aus dem Issue, um den Fehler zu bestätigen, und prüfe die relevanten Tests zum Laden von Modellen. Das Ziel ist erreicht, wenn ein gültiger Checkpoint mit übergebenem map_location=torch.device("cpu") geladen wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Describe the bug

I get an OSError when I pass torch.device("cpu") to load_state_dict for a valid safetensors checkpoint. The helper accepts torch.device in its type annotation, and the same argument works for .bin checkpoints. I have a small fix for the safetensors call and would like to submit it.

Reproduction
from pathlib import Path
from tempfile import TemporaryDirectory

import torch
from safetensors.torch import save_file

from diffusers.models.model_loading_utils import load_state_dict

with TemporaryDirectory() as directory:
    path = Path(directory) / "model.safetensors"
    save_file({"weight": torch.ones(1)}, path)
    load_state_dict(path, map_location=torch.device("cpu"))
Context

I found this while reading checkpoint loading in model_loading_utils.py, then reproduced it with the one-tensor checkpoint above.

Logs
SafetensorError: device device(type='cpu') is invalid
OSError: Unable to load weights from checkpoint file
System Info

Diffusers 0.44.0.dev0 at 08d5de3, Linux x86_64, Python 3.14.5, PyTorch 2.15.0+cpu, safetensors 0.8.0. CPU only, single process.

Vorherrschende Sprache
Python
Sterne
35k
Forks
7.5k
Ø Merge
3 T. 2 Std.
Gemergte PRs (30 T.)
58

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus huggingface/diffusers

Alle Issues in huggingface/diffusers

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.