other-formats.md 13.5 KB
Newer Older
Aryan's avatar
Aryan committed
1
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

13
14
[[open-in-colab]]

Steven Liu's avatar
Steven Liu committed
15
# Model formats
16

Steven Liu's avatar
Steven Liu committed
17
Diffusion models are typically stored in the Diffusers format or single-file format. Model files can be stored in various file types such as safetensors, dduf, or ckpt.
18

Steven Liu's avatar
Steven Liu committed
19
> [!TIP]
Steven Liu's avatar
Steven Liu committed
20
> Format refers to whether the weights are stored in a directory structure and file refers to the file type.
21

Steven Liu's avatar
Steven Liu committed
22
This guide will show you how to load pipelines and models from these formats and files.
23

Steven Liu's avatar
Steven Liu committed
24
## Diffusers format
25

Steven Liu's avatar
Steven Liu committed
26
The Diffusers format stores each model (UNet, transformer, text encoder) in a separate subfolder. There are several benefits to storing models separately.
27

Steven Liu's avatar
Steven Liu committed
28
29
30
31
- Faster overall pipeline initialization because you can load the individual model you need or load them all in parallel.
- Reduced memory usage because you don't need to load all the pipeline components if you only need one model. [Reuse](./loading#reusing-models-in-multiple-pipelines) a model that is shared between multiple pipelines.
- Lower storage requirements because common models shared between multiple pipelines are only downloaded once.
- Flexibility to use new or improved models in a pipeline.
32

Steven Liu's avatar
Steven Liu committed
33
## Single file format
34

Steven Liu's avatar
Steven Liu committed
35
A single-file format stores *all* the model (UNet, transformer, text encoder) weights in a single file. Benefits of single-file formats include the following.
36

Steven Liu's avatar
Steven Liu committed
37
38
- Greater compatibility with [ComfyUI](https://github.com/comfyanonymous/ComfyUI) or [Automatic1111](https://github.com/AUTOMATIC1111/stable-diffusion-webui).
- Easier to download and share a single file.
39

Steven Liu's avatar
Steven Liu committed
40
Use [`~loaders.FromSingleFileMixin.from_single_file`] to load a single file.
41

Steven Liu's avatar
Steven Liu committed
42
```py
Steven Liu's avatar
Steven Liu committed
43
44
import torch
from diffusers import StableDiffusionXLPipeline
Steven Liu's avatar
Steven Liu committed
45

Steven Liu's avatar
Steven Liu committed
46
47
48
49
pipeline = StableDiffusionXLPipeline.from_single_file(
    "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0.safetensors",
    torch_dtype=torch.float16,
    device_map="cuda"
Steven Liu's avatar
Steven Liu committed
50
)
51
52
```

Steven Liu's avatar
Steven Liu committed
53
The [`~loaders.FromSingleFileMixin.from_single_file`] method also supports passing new models or schedulers.
54

Steven Liu's avatar
Steven Liu committed
55
56
```py
import torch
Steven Liu's avatar
Steven Liu committed
57
from diffusers import FluxPipeline, FluxTransformer2DModel
58

Steven Liu's avatar
Steven Liu committed
59
60
61
transformer = FluxTransformer2DModel.from_single_file(
    "https://huggingface.co/Kijai/flux-fp8/blob/main/flux1-dev-fp8.safetensors", torch_dtype=torch.bfloat16
)
Steven Liu's avatar
Steven Liu committed
62
pipeline = FluxPipeline.from_pretrained(
Steven Liu's avatar
Steven Liu committed
63
64
65
66
    "black-forest-labs/FLUX.1-dev",
    transformer=transformer,
    torch_dtype=torch.bfloat16,
    device_map="cuda"
Steven Liu's avatar
Steven Liu committed
67
)
68
69
```

Steven Liu's avatar
Steven Liu committed
70
### Configuration options
71

Steven Liu's avatar
Steven Liu committed
72
Diffusers format models have a `config.json` file in their repositories with important attributes such as the number of layers and attention heads. The [`~loaders.FromSingleFileMixin.from_single_file`] method automatically determines the appropriate config to use from `config.json`. This may fail in a few rare instances though, in which case, you should use the `config` argument.
73

Steven Liu's avatar
Steven Liu committed
74
You should also use the `config` argument if the models in a pipeline are different from the original implementation or if it doesn't have the necessary metadata to determine the correct config.
75

Steven Liu's avatar
Steven Liu committed
76
```py
Steven Liu's avatar
Steven Liu committed
77
from diffusers import StableDiffusionXLPipeline
78

Steven Liu's avatar
Steven Liu committed
79
ckpt_path = "https://huggingface.co/segmind/SSD-1B/blob/main/SSD-1B.safetensors"
80

Steven Liu's avatar
Steven Liu committed
81
82
pipeline = StableDiffusionXLPipeline.from_single_file(ckpt_path, config="segmind/SSD-1B")
```
83

Steven Liu's avatar
Steven Liu committed
84
Diffusers attempts to infer the pipeline components based on the signature types of the pipeline class when using `original_config` with `local_files_only=True`. It won't download the config files from a Hub repository to avoid backward breaking changes when you can't connect to the internet. This method isn't as reliable as providing a path to a local model with the `config` argument and may lead to errors. You should run the pipeline with `local_files_only=False` to download the config files to the local cache to avoid errors.
85

Steven Liu's avatar
Steven Liu committed
86
Override default configs by passing the arguments directly to [`~loaders.FromSingleFileMixin.from_single_file`]. The examples below demonstrate how to override the configs in a pipeline or model.
87
88

```py
Steven Liu's avatar
Steven Liu committed
89
from diffusers import StableDiffusionXLInstructPix2PixPipeline
90

Steven Liu's avatar
Steven Liu committed
91
92
93
94
ckpt_path = "https://huggingface.co/stabilityai/cosxl/blob/main/cosxl_edit.safetensors"
pipeline = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
    ckpt_path, config="diffusers/sdxl-instructpix2pix-768", is_cosxl_edit=True
)
Steven Liu's avatar
Steven Liu committed
95
96
97
```

```py
Steven Liu's avatar
Steven Liu committed
98
from diffusers import UNet2DConditionModel
Steven Liu's avatar
Steven Liu committed
99

Steven Liu's avatar
Steven Liu committed
100
101
ckpt_path = "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0_0.9vae.safetensors"
model = UNet2DConditionModel.from_single_file(ckpt_path, upcast_attention=True)
Steven Liu's avatar
Steven Liu committed
102
103
```

Steven Liu's avatar
Steven Liu committed
104
### Local files
Marc Sun's avatar
Marc Sun committed
105

Steven Liu's avatar
Steven Liu committed
106
The [`~loaders.FromSingleFileMixin.from_single_file`] method attempts to configure a pipeline or model by inferring the model type from the keys in the checkpoint file. For example, any single file checkpoint based on the Stable Diffusion XL base model is configured from [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0).
Marc Sun's avatar
Marc Sun committed
107

Steven Liu's avatar
Steven Liu committed
108
If you're working with local files, download the config files with the [`~huggingface_hub.snapshot_download`] method and the model checkpoint with [`~huggingface_hub.hf_hub_download`]. These files are downloaded to your [cache directory](https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache), but you can download them to a specific directory with the `local_dir` argument.
Marc Sun's avatar
Marc Sun committed
109
110

```py
Steven Liu's avatar
Steven Liu committed
111
112
from huggingface_hub import hf_hub_download, snapshot_download
from diffusers import StableDiffusionXLPipeline
Marc Sun's avatar
Marc Sun committed
113

Steven Liu's avatar
Steven Liu committed
114
115
116
117
my_local_checkpoint_path = hf_hub_download(
    repo_id="segmind/SSD-1B",
    filename="SSD-1B.safetensors"
)
Marc Sun's avatar
Marc Sun committed
118

Steven Liu's avatar
Steven Liu committed
119
120
121
122
my_local_config_path = snapshot_download(
    repo_id="segmind/SSD-1B",
    allow_patterns=["*.json", "**/*.json", "*.txt", "**/*.txt"]
)
Marc Sun's avatar
Marc Sun committed
123

Steven Liu's avatar
Steven Liu committed
124
125
126
pipeline = StableDiffusionXLPipeline.from_single_file(
    my_local_checkpoint_path, config=my_local_config_path, local_files_only=True
)
127
```
Marc Sun's avatar
Marc Sun committed
128

Steven Liu's avatar
Steven Liu committed
129
### Symlink
Steven Liu's avatar
Steven Liu committed
130

Steven Liu's avatar
Steven Liu committed
131
If you're working with a file system that does not support symlinking, download the checkpoint file to a local directory first with the `local_dir` parameter. Using the `local_dir` parameter automatically disables symlinks.
Steven Liu's avatar
Steven Liu committed
132

Steven Liu's avatar
Steven Liu committed
133
134
135
```py
from huggingface_hub import hf_hub_download, snapshot_download
from diffusers import StableDiffusionXLPipeline
Steven Liu's avatar
Steven Liu committed
136

Steven Liu's avatar
Steven Liu committed
137
138
139
140
141
142
my_local_checkpoint_path = hf_hub_download(
    repo_id="segmind/SSD-1B",
    filename="SSD-1B.safetensors"
    local_dir="my_local_checkpoints",
)
print("My local checkpoint: ", my_local_checkpoint_path)
Steven Liu's avatar
Steven Liu committed
143

Steven Liu's avatar
Steven Liu committed
144
145
146
147
148
my_local_config_path = snapshot_download(
    repo_id="segmind/SSD-1B",
    allow_patterns=["*.json", "**/*.json", "*.txt", "**/*.txt"]
)
print("My local config: ", my_local_config_path)
Steven Liu's avatar
Steven Liu committed
149
150
```

Steven Liu's avatar
Steven Liu committed
151
Pass these paths to [`~loaders.FromSingleFileMixin.from_single_file`].
Steven Liu's avatar
Steven Liu committed
152
153

```py
Steven Liu's avatar
Steven Liu committed
154
pipeline = StableDiffusionXLPipeline.from_single_file(
Steven Liu's avatar
Steven Liu committed
155
    my_local_checkpoint_path, config=my_local_config_path, local_files_only=True
Steven Liu's avatar
Steven Liu committed
156
)
Steven Liu's avatar
Steven Liu committed
157
158
```

Steven Liu's avatar
Steven Liu committed
159
## File types
Steven Liu's avatar
Steven Liu committed
160

Steven Liu's avatar
Steven Liu committed
161
Models can be stored in several file types. Safetensors is the most common file type but you may encounter other file types on the Hub or diffusion community.
Steven Liu's avatar
Steven Liu committed
162

Steven Liu's avatar
Steven Liu committed
163
### safetensors
Steven Liu's avatar
Steven Liu committed
164

Steven Liu's avatar
Steven Liu committed
165
[Safetensors](https://hf.co/docs/safetensors) is a safe and fast file type for securely storing and loading tensors. It restricts the header size to limit certain types of attacks, supports lazy loading (useful for distributed setups), and generally loads faster.
Steven Liu's avatar
Steven Liu committed
166

Steven Liu's avatar
Steven Liu committed
167
Diffusers loads safetensors file by default (a required dependency) if they are available and the Safetensors library is installed.
Steven Liu's avatar
Steven Liu committed
168

Steven Liu's avatar
Steven Liu committed
169
Use [`~DiffusionPipeline.from_pretrained`] or [`~loaders.FromSingleFileMixin.from_single_file`] to load safetensor files.
Steven Liu's avatar
Steven Liu committed
170
171

```py
Steven Liu's avatar
Steven Liu committed
172
173
import torch
from diffusers import DiffusionPipeline
Steven Liu's avatar
Steven Liu committed
174

Steven Liu's avatar
Steven Liu committed
175
176
177
178
179
pipeline = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch.dtype=torch.float16,
    device_map="cuda"
)
Steven Liu's avatar
Steven Liu committed
180

Steven Liu's avatar
Steven Liu committed
181
182
183
184
pipeline = DiffusionPipeline.from_single_file(
    "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0.safetensors",
    torch_dtype=torch.float16,
)
Steven Liu's avatar
Steven Liu committed
185
186
```

Steven Liu's avatar
Steven Liu committed
187
If you're using a checkpoint trained with a Diffusers training script, metadata such as the LoRA configuration, is automatically saved. When the file is loaded, the metadata is parsed to correctly configure the LoRA and avoid missing or incorrect LoRA configs. Inspect the metadata of a safetensors file by clicking on the <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/safetensors/logo.png" alt="safetensors logo" style="vertical-align: middle; display: inline-block; max-height: 0.8em; max-width: 0.8em; margin: 0; padding: 0; line-height: 1;"> logo next to the file on the Hub.
Steven Liu's avatar
Steven Liu committed
188

Steven Liu's avatar
Steven Liu committed
189
Save the metadata for LoRAs that aren't trained with Diffusers with either `transformer_lora_adapter_metadata` or `unet_lora_adapter_metadata` depending on your model. For the text encoder, use the `text_encoder_lora_adapter_metadata` and `text_encoder_2_lora_adapter_metadata` arguments in [`~loaders.FluxLoraLoaderMixin.save_lora_weights`]. This is only supported for safetensors files.
Steven Liu's avatar
Steven Liu committed
190
191

```py
Steven Liu's avatar
Steven Liu committed
192
193
import torch
from diffusers import FluxPipeline
Steven Liu's avatar
Steven Liu committed
194

Steven Liu's avatar
Steven Liu committed
195
196
197
198
199
200
201
202
pipeline = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16
).to("cuda")
pipeline.load_lora_weights("linoyts/yarn_art_Flux_LoRA")
pipeline.save_lora_weights(
    text_encoder_lora_adapter_metadata={"r": 8, "lora_alpha": 8},
    text_encoder_2_lora_adapter_metadata={"r": 8, "lora_alpha": 8}
)
Steven Liu's avatar
Steven Liu committed
203
204
```

Steven Liu's avatar
Steven Liu committed
205
### ckpt
Steven Liu's avatar
Steven Liu committed
206

Steven Liu's avatar
Steven Liu committed
207
Older model weights are commonly saved with Python's [pickle](https://docs.python.org/3/library/pickle.html) utility in a ckpt file.
Steven Liu's avatar
Steven Liu committed
208

Steven Liu's avatar
Steven Liu committed
209
Pickled files may be unsafe because they can be exploited to execute malicious code. It is recommended to use safetensors files or convert the weights to safetensors files.
Steven Liu's avatar
Steven Liu committed
210

Steven Liu's avatar
Steven Liu committed
211
Use [`~loaders.FromSingleFileMixin.from_single_file`] to load a ckpt file.
Steven Liu's avatar
Steven Liu committed
212
213

```py
Steven Liu's avatar
Steven Liu committed
214
from diffusers import DiffusionPipeline
Steven Liu's avatar
Steven Liu committed
215

Steven Liu's avatar
Steven Liu committed
216
217
218
pipeline = DiffusionPipeline.from_single_file(
    "https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned.ckpt"
)
Steven Liu's avatar
Steven Liu committed
219
220
```

Steven Liu's avatar
Steven Liu committed
221
### dduf
Steven Liu's avatar
Steven Liu committed
222
223

> [!TIP]
Steven Liu's avatar
Steven Liu committed
224
> DDUF is an experimental file type and the API may change. Refer to the DDUF [docs](https://huggingface.co/docs/hub/dduf) to learn more.
Steven Liu's avatar
Steven Liu committed
225

Steven Liu's avatar
Steven Liu committed
226
DDUF is a file type designed to unify different diffusion model distribution methods and weight-saving formats. It is a standardized and flexible method to package all components of a diffusion model into a single file, providing a balance between the Diffusers and single-file formats.
Steven Liu's avatar
Steven Liu committed
227

Steven Liu's avatar
Steven Liu committed
228
Use the `dduf_file` argument in [`~DiffusionPipeline.from_pretrained`] to load a DDUF file. You can also load quantized dduf files as long as they are stored in the Diffusers format.
Steven Liu's avatar
Steven Liu committed
229

Steven Liu's avatar
Steven Liu committed
230
231
232
```py
import torch
from diffusers import DiffusionPipeline
Steven Liu's avatar
Steven Liu committed
233

Steven Liu's avatar
Steven Liu committed
234
235
236
237
238
239
pipeline = DiffusionPipeline.from_pretrained(
    "DDUF/FLUX.1-dev-DDUF",
    dduf_file="FLUX.1-dev.dduf",
    torch_dtype=torch.bfloat16,
    device_map="cuda"
)
Steven Liu's avatar
Steven Liu committed
240
241
```

Steven Liu's avatar
Steven Liu committed
242
To save a pipeline as a dduf file, use the [`~huggingface_hub.export_folder_as_dduf`] utility.
Steven Liu's avatar
Steven Liu committed
243

Steven Liu's avatar
Steven Liu committed
244
245
246
247
```py
import torch
from diffusers import DiffusionPipeline
from huggingface_hub import export_folder_as_dduf
Steven Liu's avatar
Steven Liu committed
248

Steven Liu's avatar
Steven Liu committed
249
pipeline = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
Steven Liu's avatar
Steven Liu committed
250

Steven Liu's avatar
Steven Liu committed
251
252
253
save_folder = "flux-dev"
pipeline.save_pretrained("flux-dev")
export_folder_as_dduf("flux-dev.dduf", folder_path=save_folder)
Steven Liu's avatar
Steven Liu committed
254
255
```

Steven Liu's avatar
Steven Liu committed
256
## Converting formats and files
Steven Liu's avatar
Steven Liu committed
257

Steven Liu's avatar
Steven Liu committed
258
Diffusers provides scripts and methods to convert format and files to enable broader support across the diffusion ecosystem.
Steven Liu's avatar
Steven Liu committed
259

Steven Liu's avatar
Steven Liu committed
260
Take a look at the [diffusers/scripts](https://github.com/huggingface/diffusers/tree/main/scripts) folder to find a conversion script. Scripts with `"to_diffusers` appended at the end converts a model to the Diffusers format. Each script has a specific set of arguments for configuring the conversion. Make sure you check what arguments are available.
Steven Liu's avatar
Steven Liu committed
261

Steven Liu's avatar
Steven Liu committed
262
The example below converts a model stored in Diffusers format to a single-file format. Provide the path to the model to convert and where to save the converted model. You can optionally specify what file type and data type to save the model as.
Steven Liu's avatar
Steven Liu committed
263

Steven Liu's avatar
Steven Liu committed
264
265
```bash
python convert_diffusers_to_original_sdxl.py --model_path path/to/model/to/convert --checkpoint_path path/to/save/model/to --use_safetensors
Steven Liu's avatar
Steven Liu committed
266
267
```

Steven Liu's avatar
Steven Liu committed
268
The [`~DiffusionPipeline.save_pretrained`] method also saves a model in Diffusers format and takes care of creating subfolders for each model. It saves the files as safetensor files by default.
Steven Liu's avatar
Steven Liu committed
269

Steven Liu's avatar
Steven Liu committed
270
271
```py
from diffusers import DiffusionPipeline
Steven Liu's avatar
Steven Liu committed
272

Steven Liu's avatar
Steven Liu committed
273
274
pipeline = DiffusionPipeline.from_single_file(
    "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0.safetensors",
Steven Liu's avatar
Steven Liu committed
275
)
Steven Liu's avatar
Steven Liu committed
276
pipeline.save_pretrained()
Steven Liu's avatar
Steven Liu committed
277
278
```

Steven Liu's avatar
Steven Liu committed
279
Finally, you can use a Space like [SD To Diffusers](https://hf.co/spaces/diffusers/sd-to-diffusers) or [SD-XL To Diffusers](https://hf.co/spaces/diffusers/sdxl-to-diffusers) to convert models to the Diffusers format. It'll open a PR on your model repository with the converted files. This is the easiest way to convert a model, but it may fail for more complicated models. Using a conversion script is more reliable.
Steven Liu's avatar
Steven Liu committed
280

Steven Liu's avatar
Steven Liu committed
281
## Resources
Steven Liu's avatar
Steven Liu committed
282

Steven Liu's avatar
Steven Liu committed
283
- Learn more about the design decisions and why safetensor files are preferred for saving and loading model weights in the [Safetensors audited as really safe and becoming the default](https://blog.eleuther.ai/safetensors-security-audit/) blog post.
Steven Liu's avatar
Steven Liu committed
284