huggingface / huggingface/diffusers

Loading pipeline in precision it was saved in

Offen
#9,797 10 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Python
Sterne
34.5k
Forks
7.3k
Ø Merge
3 T. 3 Std.
Gemergte PRs (30 T.)
91

Beschreibung

**Is your feature request related to a problem? Please describe.**
Currently, if `torch_dtype` is not specified, the pipeline defaults to loading in `float32`. This behavior causes `float16` or `bfloat16` weights to be upcast to `float32` when the model is saved in lower precision, leading to increased memory usage. In scenarios where memory efficiency is critical (e.g., when exporting the model to another format), it’s important to load the model in the original precision specified in the safetensors file. Additionally, there’s currently no way to determine the dtype the model was saved in.

**Describe the solution you'd like.**
A feature similar to `torch_dtype="auto"` in the transformers library would be helpful. This option allows models to be loaded with the dtype defined in their configuration. However, diffuser pipeline models generally lack a dtype specification in their configs. It is sometimes possible to use `torch_dtype` from `text_encoder` config, but not all pipelines have it and it is not clear if this is a reliable place to check the precision of the model.

**Describe alternatives you've considered.**
A possible solution could be implementing a method to identify the model’s precision prior to calling `from_pretrained`, as the weights are accessible only after the model is downloaded inside `from_pretrained` and remain hidden from external access. This approach would allow users to set the appropriate `torch_dtype` for loading the model.

**Additional context.**
This feature is relevant to `optimum-cli` use cases where model conversion or export to other formats must work within memory constraints. If there’s already a way to achieve this, guidance would be appreciated.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, das Laden der Pipeline über from_pretrained nachzuverfolgen und zu untersuchen, wie safetensors-Gewichte während des Ladens verfügbar werden. Vergleiche das angeforderte Verhalten mit transformers' torch_dtype="auto" und dem genannten optimum-cli-Konvertierungsanwendungsfall. Die Aufgabe ist abgeschlossen, wenn es eine zuverlässige Möglichkeit gibt, die gespeicherte Präzision zu erkennen oder beizubehalten, ohne dass Benutzer torch_dtype manuell angeben müssen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python, pytorch
Bereich
machine-learning, performance
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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