Skip to content

Commit 20396e2

Browse files
authored
Adding some safetensors docs. (#2122)
* Tmp. * Adding more docs. * Doc style. * Remove the argument `use_safetensors=True`. * doc-builder
1 parent 2cf34e6 commit 20396e2

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
title: Community Pipelines
3939
- local: using-diffusers/contribute_pipeline
4040
title: How to contribute a Pipeline
41+
- local: using-diffusers/using_safetensors
42+
title: Using safetensors
4143
title: Pipelines for Inference
4244
- sections:
4345
- local: using-diffusers/rl
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# What is safetensors ?
2+
3+
[safetensors](https://github.com/huggingface/safetensors) is a different format
4+
from the classic `.bin` which uses Pytorch which uses pickle.
5+
6+
Pickle is notoriously unsafe which allow any malicious file to execute arbitrary code.
7+
The hub itself tries to prevent issues from it, but it's not a silver bullet.
8+
9+
`safetensors` first and foremost goal is to make loading machine learning models *safe*
10+
in the sense that no takeover of your computer can be done.
11+
12+
# Why use safetensors ?
13+
14+
**Safety** can be one reason, if you're attempting to use a not well known model and
15+
you're not sure about the source of the file.
16+
17+
And a secondary reason, is **the speed of loading**. Safetensors can load models much faster
18+
than regular pickle files. If you spend a lot of times switching models, this can be
19+
a huge timesave.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# What is safetensors ?
2+
3+
[safetensors](https://github.com/huggingface/safetensors) is a different format
4+
from the classic `.bin` which uses Pytorch which uses pickle. It contains the
5+
exact same data, which is just the model weights (or tensors).
6+
7+
Pickle is notoriously unsafe which allow any malicious file to execute arbitrary code.
8+
The hub itself tries to prevent issues from it, but it's not a silver bullet.
9+
10+
`safetensors` first and foremost goal is to make loading machine learning models *safe*
11+
in the sense that no takeover of your computer can be done.
12+
13+
Hence the name.
14+
15+
# Why use safetensors ?
16+
17+
**Safety** can be one reason, if you're attempting to use a not well known model and
18+
you're not sure about the source of the file.
19+
20+
And a secondary reason, is **the speed of loading**. Safetensors can load models much faster
21+
than regular pickle files. If you spend a lot of times switching models, this can be
22+
a huge timesave.
23+
24+
Numbers taken AMD EPYC 7742 64-Core Processor
25+
```
26+
from diffusers import StableDiffusionPipeline
27+
28+
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
29+
30+
# Loaded in safetensors 0:00:02.033658
31+
# Loaded in Pytorch 0:00:02.663379
32+
```
33+
34+
This is for the entire loading time, the actual weights loading time to load 500MB:
35+
36+
```
37+
Safetensors: 3.4873ms
38+
PyTorch: 172.7537ms
39+
```
40+
41+
Performance in general is a tricky business, and there are a few things to understand:
42+
43+
- If you're using the model for the first time from the hub, you will have to download the weights.
44+
That's extremely likely to be much slower than any loading method, therefore you will not see any difference
45+
- If you're loading the model for the first time (let's say after a reboot) then your machine will have to
46+
actually read the disk. It's likely to be as slow in both cases. Again the speed difference may not be as visible (this depends on hardware and the actual model).
47+
- The best performance benefit is when the model was already loaded previously on your computer and you're switching from one model to another. Your OS, is trying really hard not to read from disk, since this is slow, so it will keep the files around in RAM, making it loading again much faster. Since safetensors is doing zero-copy of the tensors, reloading will be faster than pytorch since it has at least once extra copy to do.
48+
49+
# How to use safetensors ?
50+
51+
If you have `safetensors` installed, and all the weights are available in `safetensors` format, \
52+
then by default it will use that instead of the pytorch weights.
53+
54+
If you are really paranoid about this, the ultimate weapon would be disabling `torch.load`:
55+
```python
56+
import torch
57+
58+
59+
def _raise():
60+
raise RuntimeError("I don't want to use pickle")
61+
62+
63+
torch.load = lambda *args, **kwargs: _raise()
64+
```
65+
66+
# I want to use model X but it doesn't have safetensors weights.
67+
68+
Just go to this [space](https://huggingface.co/spaces/safetensors/convert).
69+
This will create a new PR with the weights, let's say `refs/pr/22`.
70+
71+
This space will download the pickled version, convert it, and upload it on the hub as a PR.
72+
If anything bad is contained in the file, it's Huggingface hub that will get issues, not your own computer.
73+
And we're equipped with dealing with it.
74+
75+
Then in order to use the model, even before the branch gets accepted by the original author you can do:
76+
77+
```python
78+
from diffusers import StableDiffusionPipeline
79+
80+
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", revision="refs/pr/22")
81+
```
82+
83+
And that's it !
84+
85+
Anything unclear, concerns, or found a bugs ? [Open an issue](https://github.com/huggingface/diffusers/issues/new/choose)
86+
87+

0 commit comments

Comments
 (0)