Skip to content

Commit cd7071e

Browse files
authored
[docs] Add safetensors flag (huggingface#4245)
* add safetensors flag * apply review
1 parent e31f38b commit cd7071e

30 files changed

+147
-87
lines changed

Diff for: docs/source/en/optimization/fp16.md

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ from diffusers import DiffusionPipeline
5151
pipe = DiffusionPipeline.from_pretrained(
5252
"runwayml/stable-diffusion-v1-5",
5353
torch_dtype=torch.float16,
54+
use_safetensors=True,
5455
)
5556
pipe = pipe.to("cuda")
5657

@@ -80,6 +81,7 @@ from diffusers import StableDiffusionPipeline
8081
pipe = StableDiffusionPipeline.from_pretrained(
8182
"runwayml/stable-diffusion-v1-5",
8283
torch_dtype=torch.float16,
84+
use_safetensors=True,
8385
)
8486
pipe = pipe.to("cuda")
8587

@@ -106,6 +108,7 @@ from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler
106108
pipe = StableDiffusionPipeline.from_pretrained(
107109
"runwayml/stable-diffusion-v1-5",
108110
torch_dtype=torch.float16,
111+
use_safetensors=True,
109112
)
110113
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
111114
pipe = pipe.to("cuda")
@@ -133,6 +136,7 @@ from diffusers import StableDiffusionPipeline
133136
pipe = StableDiffusionPipeline.from_pretrained(
134137
"runwayml/stable-diffusion-v1-5",
135138
torch_dtype=torch.float16,
139+
use_safetensors=True,
136140
)
137141

138142
prompt = "a photo of an astronaut riding a horse on mars"
@@ -157,6 +161,7 @@ from diffusers import StableDiffusionPipeline
157161
pipe = StableDiffusionPipeline.from_pretrained(
158162
"runwayml/stable-diffusion-v1-5",
159163
torch_dtype=torch.float16,
164+
use_safetensors=True,
160165
)
161166

162167
prompt = "a photo of an astronaut riding a horse on mars"
@@ -189,6 +194,7 @@ from diffusers import StableDiffusionPipeline
189194
pipe = StableDiffusionPipeline.from_pretrained(
190195
"runwayml/stable-diffusion-v1-5",
191196
torch_dtype=torch.float16,
197+
use_safetensors=True,
192198
)
193199

194200
prompt = "a photo of an astronaut riding a horse on mars"
@@ -205,6 +211,7 @@ from diffusers import StableDiffusionPipeline
205211
pipe = StableDiffusionPipeline.from_pretrained(
206212
"runwayml/stable-diffusion-v1-5",
207213
torch_dtype=torch.float16,
214+
use_safetensors=True,
208215
)
209216

210217
prompt = "a photo of an astronaut riding a horse on mars"
@@ -267,6 +274,7 @@ def generate_inputs():
267274
pipe = StableDiffusionPipeline.from_pretrained(
268275
"runwayml/stable-diffusion-v1-5",
269276
torch_dtype=torch.float16,
277+
use_safetensors=True,
270278
).to("cuda")
271279
unet = pipe.unet
272280
unet.eval()
@@ -330,6 +338,7 @@ class UNet2DConditionOutput:
330338
pipe = StableDiffusionPipeline.from_pretrained(
331339
"runwayml/stable-diffusion-v1-5",
332340
torch_dtype=torch.float16,
341+
use_safetensors=True,
333342
).to("cuda")
334343

335344
# use jitted unet
@@ -389,6 +398,7 @@ import torch
389398
pipe = DiffusionPipeline.from_pretrained(
390399
"runwayml/stable-diffusion-v1-5",
391400
torch_dtype=torch.float16,
401+
use_safetensors=True,
392402
).to("cuda")
393403

394404
pipe.enable_xformers_memory_efficient_attention()

Diff for: docs/source/en/optimization/torch2.0.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pip install --upgrade torch diffusers
3939
import torch
4040
from diffusers import DiffusionPipeline
4141

42-
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
42+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True)
4343
pipe = pipe.to("cuda")
4444

4545
prompt = "a photo of an astronaut riding a horse on mars"
@@ -53,7 +53,7 @@ pip install --upgrade torch diffusers
5353
from diffusers import DiffusionPipeline
5454
+ from diffusers.models.attention_processor import AttnProcessor2_0
5555

56-
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
56+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
5757
+ pipe.unet.set_attn_processor(AttnProcessor2_0())
5858

5959
prompt = "a photo of an astronaut riding a horse on mars"
@@ -69,7 +69,7 @@ pip install --upgrade torch diffusers
6969
from diffusers import DiffusionPipeline
7070
from diffusers.models.attention_processor import AttnProcessor
7171

72-
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
72+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
7373
pipe.unet.set_default_attn_processor()
7474

7575
prompt = "a photo of an astronaut riding a horse on mars"
@@ -107,7 +107,7 @@ path = "runwayml/stable-diffusion-v1-5"
107107

108108
run_compile = True # Set True / False
109109

110-
pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
110+
pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16, use_safetensors=True)
111111
pipe = pipe.to("cuda")
112112
pipe.unet.to(memory_format=torch.channels_last)
113113

@@ -140,7 +140,7 @@ path = "runwayml/stable-diffusion-v1-5"
140140

141141
run_compile = True # Set True / False
142142

143-
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16)
143+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, use_safetensors=True)
144144
pipe = pipe.to("cuda")
145145
pipe.unet.to(memory_format=torch.channels_last)
146146

@@ -180,7 +180,7 @@ path = "runwayml/stable-diffusion-inpainting"
180180

181181
run_compile = True # Set True / False
182182

183-
pipe = StableDiffusionInpaintPipeline.from_pretrained(path, torch_dtype=torch.float16)
183+
pipe = StableDiffusionInpaintPipeline.from_pretrained(path, torch_dtype=torch.float16, use_safetensors=True)
184184
pipe = pipe.to("cuda")
185185
pipe.unet.to(memory_format=torch.channels_last)
186186

@@ -212,9 +212,9 @@ init_image = init_image.resize((512, 512))
212212
path = "runwayml/stable-diffusion-v1-5"
213213

214214
run_compile = True # Set True / False
215-
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
215+
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16, use_safetensors=True)
216216
pipe = StableDiffusionControlNetPipeline.from_pretrained(
217-
path, controlnet=controlnet, torch_dtype=torch.float16
217+
path, controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
218218
)
219219

220220
pipe = pipe.to("cuda")
@@ -240,11 +240,11 @@ import torch
240240

241241
run_compile = True # Set True / False
242242

243-
pipe = DiffusionPipeline.from_pretrained("DeepFloyd/IF-I-M-v1.0", variant="fp16", text_encoder=None, torch_dtype=torch.float16)
243+
pipe = DiffusionPipeline.from_pretrained("DeepFloyd/IF-I-M-v1.0", variant="fp16", text_encoder=None, torch_dtype=torch.float16, use_safetensors=True)
244244
pipe.to("cuda")
245-
pipe_2 = DiffusionPipeline.from_pretrained("DeepFloyd/IF-II-M-v1.0", variant="fp16", text_encoder=None, torch_dtype=torch.float16)
245+
pipe_2 = DiffusionPipeline.from_pretrained("DeepFloyd/IF-II-M-v1.0", variant="fp16", text_encoder=None, torch_dtype=torch.float16, use_safetensors=True)
246246
pipe_2.to("cuda")
247-
pipe_3 = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler", torch_dtype=torch.float16)
247+
pipe_3 = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler", torch_dtype=torch.float16, use_safetensors=True)
248248
pipe_3.to("cuda")
249249

250250

Diff for: docs/source/en/quicktour.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Load the model with the [`~DiffusionPipeline.from_pretrained`] method:
6767
```python
6868
>>> from diffusers import DiffusionPipeline
6969

70-
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
70+
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
7171
```
7272

7373
The [`DiffusionPipeline`] downloads and caches all modeling, tokenization, and scheduling components. You'll see that the Stable Diffusion pipeline is composed of the [`UNet2DConditionModel`] and [`PNDMScheduler`] among other things:
@@ -130,7 +130,7 @@ You can also use the pipeline locally. The only difference is you need to downlo
130130
Then load the saved weights into the pipeline:
131131

132132
```python
133-
>>> pipeline = DiffusionPipeline.from_pretrained("./stable-diffusion-v1-5")
133+
>>> pipeline = DiffusionPipeline.from_pretrained("./stable-diffusion-v1-5", use_safetensors=True)
134134
```
135135

136136
Now you can run the pipeline as you would in the section above.
@@ -142,7 +142,7 @@ Different schedulers come with different denoising speeds and quality trade-offs
142142
```py
143143
>>> from diffusers import EulerDiscreteScheduler
144144

145-
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
145+
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
146146
>>> pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
147147
```
148148

@@ -160,7 +160,7 @@ Models are initiated with the [`~ModelMixin.from_pretrained`] method which also
160160
>>> from diffusers import UNet2DModel
161161

162162
>>> repo_id = "google/ddpm-cat-256"
163-
>>> model = UNet2DModel.from_pretrained(repo_id)
163+
>>> model = UNet2DModel.from_pretrained(repo_id, use_safetensors=True)
164164
```
165165

166166
To access the model parameters, call `model.config`:

Diff for: docs/source/en/stable_diffusion.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Begin by loading the [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/r
2626
from diffusers import DiffusionPipeline
2727

2828
model_id = "runwayml/stable-diffusion-v1-5"
29-
pipeline = DiffusionPipeline.from_pretrained(model_id)
29+
pipeline = DiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
3030
```
3131

3232
The example prompt you'll use is a portrait of an old warrior chief, but feel free to use your own prompt:
@@ -75,7 +75,7 @@ Let's start by loading the model in `float16` and generate an image:
7575
```python
7676
import torch
7777

78-
pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
78+
pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True)
7979
pipeline = pipeline.to("cuda")
8080
generator = torch.Generator("cuda").manual_seed(0)
8181
image = pipeline(prompt, generator=generator).images[0]

Diff for: docs/source/en/training/adapt_a_model.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A [`UNet2DConditionModel`] by default accepts 4 channels in the [input sample](h
1111
```py
1212
from diffusers import StableDiffusionPipeline
1313

14-
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
14+
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
1515
pipeline.unet.config["in_channels"]
1616
4
1717
```
@@ -21,7 +21,7 @@ Inpainting requires 9 channels in the input sample. You can check this value in
2121
```py
2222
from diffusers import StableDiffusionPipeline
2323

24-
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting")
24+
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", use_safetensors=True)
2525
pipeline.unet.config["in_channels"]
2626
9
2727
```
@@ -35,7 +35,12 @@ from diffusers import UNet2DConditionModel
3535

3636
model_id = "runwayml/stable-diffusion-v1-5"
3737
unet = UNet2DConditionModel.from_pretrained(
38-
model_id, subfolder="unet", in_channels=9, low_cpu_mem_usage=False, ignore_mismatched_sizes=True
38+
model_id,
39+
subfolder="unet",
40+
in_channels=9,
41+
low_cpu_mem_usage=False,
42+
ignore_mismatched_sizes=True,
43+
use_safetensors=True,
3944
)
4045
```
4146

Diff for: docs/source/en/training/controlnet.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ import torch
306306
base_model_path = "path to model"
307307
controlnet_path = "path to controlnet"
308308
309-
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)
309+
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16, use_safetensors=True)
310310
pipe = StableDiffusionControlNetPipeline.from_pretrained(
311-
base_model_path, controlnet=controlnet, torch_dtype=torch.float16
311+
base_model_path, controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True
312312
)
313313
314314
# speed up diffusion process with faster scheduler and memory optimization

Diff for: docs/source/en/training/custom_diffusion.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ Once you have trained a model using the above command, you can run inference usi
222222
import torch
223223
from diffusers import DiffusionPipeline
224224

225-
pipe = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to("cuda")
225+
pipe = DiffusionPipeline.from_pretrained(
226+
"CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16, use_safetensors=True
227+
).to("cuda")
226228
pipe.unet.load_attn_procs("path-to-save-model", weight_name="pytorch_custom_diffusion_weights.bin")
227229
pipe.load_textual_inversion("path-to-save-model", weight_name="<new1>.bin")
228230

@@ -246,7 +248,7 @@ model_id = "sayakpaul/custom-diffusion-cat"
246248
card = RepoCard.load(model_id)
247249
base_model_id = card.data.to_dict()["base_model"]
248250

249-
pipe = DiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16).to("cuda")
251+
pipe = DiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, use_safetensors=True).to("cuda")
250252
pipe.unet.load_attn_procs(model_id, weight_name="pytorch_custom_diffusion_weights.bin")
251253
pipe.load_textual_inversion(model_id, weight_name="<new1>.bin")
252254

@@ -270,7 +272,7 @@ model_id = "sayakpaul/custom-diffusion-cat-wooden-pot"
270272
card = RepoCard.load(model_id)
271273
base_model_id = card.data.to_dict()["base_model"]
272274

273-
pipe = DiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16).to("cuda")
275+
pipe = DiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, use_safetensors=True).to("cuda")
274276
pipe.unet.load_attn_procs(model_id, weight_name="pytorch_custom_diffusion_weights.bin")
275277
pipe.load_textual_inversion(model_id, weight_name="<new1>.bin")
276278
pipe.load_textual_inversion(model_id, weight_name="<new2>.bin")

Diff for: docs/source/en/training/distributed_inference.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Now use the [`~accelerate.PartialState.split_between_processes`] utility as a co
1616
from accelerate import PartialState
1717
from diffusers import DiffusionPipeline
1818

19-
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
19+
pipeline = DiffusionPipeline.from_pretrained(
20+
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True
21+
)
2022
distributed_state = PartialState()
2123
pipeline.to(distributed_state.device)
2224

@@ -50,7 +52,9 @@ import torch.multiprocessing as mp
5052

5153
from diffusers import DiffusionPipeline
5254

53-
sd = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
55+
sd = DiffusionPipeline.from_pretrained(
56+
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True
57+
)
5458
```
5559

5660
You'll want to create a function to run inference; [`init_process_group`](https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group) handles creating a distributed environment with the type of backend to use, the `rank` of the current process, and the `world_size` or the number of processes participating. If you're running inference in parallel over 2 GPUs, then the `world_size` is 2.

Diff for: docs/source/en/training/dreambooth.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ unet = UNet2DConditionModel.from_pretrained("/sddata/dreambooth/daruma-v2-1/chec
303303
# if you have trained with `--args.train_text_encoder` make sure to also load the text encoder
304304
text_encoder = CLIPTextModel.from_pretrained("/sddata/dreambooth/daruma-v2-1/checkpoint-100/text_encoder")
305305

306-
pipeline = DiffusionPipeline.from_pretrained(model_id, unet=unet, text_encoder=text_encoder, dtype=torch.float16)
306+
pipeline = DiffusionPipeline.from_pretrained(
307+
model_id, unet=unet, text_encoder=text_encoder, dtype=torch.float16, use_safetensors=True
308+
)
307309
pipeline.to("cuda")
308310

309311
# Perform inference, or save, or push to the hub
@@ -318,7 +320,7 @@ from diffusers import DiffusionPipeline
318320

319321
# Load the pipeline with the same arguments (model, revision) that were used for training
320322
model_id = "CompVis/stable-diffusion-v1-4"
321-
pipeline = DiffusionPipeline.from_pretrained(model_id)
323+
pipeline = DiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
322324

323325
accelerator = Accelerator()
324326

@@ -333,6 +335,7 @@ pipeline = DiffusionPipeline.from_pretrained(
333335
model_id,
334336
unet=accelerator.unwrap_model(unet),
335337
text_encoder=accelerator.unwrap_model(text_encoder),
338+
use_safetensors=True,
336339
)
337340

338341
# Perform inference, or save, or push to the hub
@@ -488,7 +491,7 @@ from diffusers import DiffusionPipeline
488491
import torch
489492

490493
model_id = "path_to_saved_model"
491-
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
494+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True).to("cuda")
492495

493496
prompt = "A photo of sks dog in a bucket"
494497
image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
@@ -510,7 +513,7 @@ must also update the pipeline's scheduler config.
510513
```py
511514
from diffusers import DiffusionPipeline
512515

513-
pipe = DiffusionPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0")
516+
pipe = DiffusionPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0", use_safetensors=True)
514517

515518
pipe.load_lora_weights("<lora weights path>")
516519

Diff for: docs/source/en/training/instructpix2pix.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ import torch
165165
from diffusers import StableDiffusionInstructPix2PixPipeline
166166

167167
model_id = "your_model_id" # <- replace this
168-
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
168+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
169+
model_id, torch_dtype=torch.float16, use_safetensors=True
170+
).to("cuda")
169171
generator = torch.Generator("cuda").manual_seed(0)
170172

171173
url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/test_pix2pix_4.png"

0 commit comments

Comments
 (0)