Skip to content

Commit 3a5fbf8

Browse files
author
yiyixuxu
committed
Merge remote-tracking branch 'origin/main' into kandinsky
2 parents 0272da9 + 909742d commit 3a5fbf8

File tree

113 files changed

+2680
-1061
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+2680
-1061
lines changed

.github/workflows/pr_tests.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ jobs:
6969
run: |
7070
apt-get update && apt-get install libsndfile1-dev -y
7171
python -m pip install -e .[quality,test]
72-
python -m pip install -U git+https://github.com/huggingface/transformers
73-
python -m pip install git+https://github.com/huggingface/accelerate
7472
7573
- name: Environment
7674
run: |
@@ -152,8 +150,8 @@ jobs:
152150
${CONDA_RUN} python -m pip install --upgrade pip
153151
${CONDA_RUN} python -m pip install -e .[quality,test]
154152
${CONDA_RUN} python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
155-
${CONDA_RUN} python -m pip install git+https://github.com/huggingface/accelerate
156-
${CONDA_RUN} python -m pip install -U git+https://github.com/huggingface/transformers
153+
${CONDA_RUN} python -m pip install accelerate --upgrade
154+
${CONDA_RUN} python -m pip install transformers --upgrade
157155
158156
- name: Environment
159157
shell: arch -arch arm64 bash {0}

.github/workflows/push_tests.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ jobs:
6161
- name: Install dependencies
6262
run: |
6363
python -m pip install -e .[quality,test]
64-
python -m pip install -U git+https://github.com/huggingface/transformers
65-
python -m pip install git+https://github.com/huggingface/accelerate
6664
6765
- name: Environment
6866
run: |
@@ -72,6 +70,9 @@ jobs:
7270
if: ${{ matrix.config.framework == 'pytorch' }}
7371
env:
7472
HUGGING_FACE_HUB_TOKEN: ${{ secrets.HUGGING_FACE_HUB_TOKEN }}
73+
# https://pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms
74+
CUBLAS_WORKSPACE_CONFIG: :16:8
75+
7576
run: |
7677
python -m pytest -n 1 --max-worker-restart=0 --dist=loadfile \
7778
-s -v -k "not Flax and not Onnx" \
@@ -131,8 +132,6 @@ jobs:
131132
- name: Install dependencies
132133
run: |
133134
python -m pip install -e .[quality,test,training]
134-
python -m pip install git+https://github.com/huggingface/accelerate
135-
python -m pip install -U git+https://github.com/huggingface/transformers
136135
137136
- name: Environment
138137
run: |

.github/workflows/push_tests_fast.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ jobs:
6262
run: |
6363
apt-get update && apt-get install libsndfile1-dev -y
6464
python -m pip install -e .[quality,test]
65-
python -m pip install -U git+https://github.com/huggingface/transformers
66-
python -m pip install git+https://github.com/huggingface/accelerate
6765
6866
- name: Environment
6967
run: |
@@ -137,8 +135,8 @@ jobs:
137135
${CONDA_RUN} python -m pip install --upgrade pip
138136
${CONDA_RUN} python -m pip install -e .[quality,test]
139137
${CONDA_RUN} python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
140-
${CONDA_RUN} python -m pip install git+https://github.com/huggingface/accelerate
141-
${CONDA_RUN} python -m pip install -U git+https://github.com/huggingface/transformers
138+
${CONDA_RUN} python -m pip install accelerate --upgrade
139+
${CONDA_RUN} python -m pip install transformers --upgrade
142140
143141
- name: Environment
144142
shell: arch -arch arm64 bash {0}

docker/diffusers-pytorch-cuda/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
2626
python3 -m pip install --no-cache-dir \
2727
torch \
2828
torchvision \
29-
torchaudio \
29+
torchaudio && \
3030
python3 -m pip install --no-cache-dir \
3131
accelerate \
3232
datasets \
@@ -37,6 +37,7 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip && \
3737
numpy \
3838
scipy \
3939
tensorboard \
40-
transformers
40+
transformers \
41+
omegaconf
4142

4243
CMD ["/bin/bash"]

docs/source/en/_toctree.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
title: Load and compare different schedulers
2727
- local: using-diffusers/custom_pipeline_overview
2828
title: Load community pipelines
29+
- local: using-diffusers/using_safetensors
30+
title: Load safetensors
2931
- local: using-diffusers/kerascv
3032
title: Load KerasCV Stable Diffusion checkpoints
3133
title: Loading & Hub
@@ -50,8 +52,6 @@
5052
title: Community pipelines
5153
- local: using-diffusers/contribute_pipeline
5254
title: How to contribute a community pipeline
53-
- local: using-diffusers/using_safetensors
54-
title: Using safetensors
5555
- local: using-diffusers/stable_diffusion_jax_how_to
5656
title: Stable Diffusion in JAX/Flax
5757
- local: using-diffusers/weighted_prompts
@@ -62,6 +62,8 @@
6262
title: Overview
6363
- local: training/create_dataset
6464
title: Create a dataset for training
65+
- local: training/adapt_a_model
66+
title: Adapt a model to a new task
6567
- local: training/unconditional_training
6668
title: Unconditional image generation
6769
- local: training/text_inversion

docs/source/en/installation.mdx

+24-22
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ specific language governing permissions and limitations under the License.
1212

1313
# Installation
1414

15-
Install 🤗 Diffusers for whichever deep learning library youre working with.
15+
Install 🤗 Diffusers for whichever deep learning library you're working with.
1616

17-
🤗 Diffusers is tested on Python 3.7+, PyTorch 1.7.0+ and flax. Follow the installation instructions below for the deep learning library you are using:
17+
🤗 Diffusers is tested on Python 3.7+, PyTorch 1.7.0+ and Flax. Follow the installation instructions below for the deep learning library you are using:
1818

1919
- [PyTorch](https://pytorch.org/get-started/locally/) installation instructions.
2020
- [Flax](https://flax.readthedocs.io/en/latest/) installation instructions.
@@ -37,27 +37,28 @@ Activate the virtual environment:
3737
source .env/bin/activate
3838
```
3939

40-
Now you're ready to install 🤗 Diffusers with the following command:
41-
42-
**For PyTorch**
40+
🤗 Diffusers also relies on the 🤗 Transformers library, and you can install both with the following command:
4341

42+
<frameworkcontent>
43+
<pt>
4444
```bash
45-
pip install diffusers["torch"]
45+
pip install diffusers["torch"] transformers
4646
```
47-
48-
**For Flax**
49-
47+
</pt>
48+
<jax>
5049
```bash
51-
pip install diffusers["flax"]
50+
pip install diffusers["flax"] transformers
5251
```
52+
</jax>
53+
</frameworkcontent>
5354

5455
## Install from source
5556

56-
Before intsalling `diffusers` from source, make sure you have `torch` and `accelerate` installed.
57+
Before installing 🤗 Diffusers from source, make sure you have `torch` and 🤗 Accelerate installed.
5758

58-
For `torch` installation refer to the `torch` [docs](https://pytorch.org/get-started/locally/#start-locally).
59+
For `torch` installation, refer to the `torch` [installation](https://pytorch.org/get-started/locally/#start-locally) guide.
5960

60-
To install `accelerate`
61+
To install 🤗 Accelerate:
6162

6263
```bash
6364
pip install accelerate
@@ -74,7 +75,7 @@ The `main` version is useful for staying up-to-date with the latest developments
7475
For instance, if a bug has been fixed since the last official release but a new release hasn't been rolled out yet.
7576
However, this means the `main` version may not always be stable.
7677
We strive to keep the `main` version operational, and most issues are usually resolved within a few hours or a day.
77-
If you run into a problem, please open an [Issue](https://github.com/huggingface/transformers/issues), so we can fix it even sooner!
78+
If you run into a problem, please open an [Issue](https://github.com/huggingface/diffusers/issues/new/choose), so we can fix it even sooner!
7879

7980
## Editable install
8081

@@ -90,21 +91,22 @@ git clone https://github.com/huggingface/diffusers.git
9091
cd diffusers
9192
```
9293

93-
**For PyTorch**
94-
95-
```
94+
<frameworkcontent>
95+
<pt>
96+
```bash
9697
pip install -e ".[torch]"
9798
```
98-
99-
**For Flax**
100-
101-
```
99+
</pt>
100+
<jax>
101+
```bash
102102
pip install -e ".[flax]"
103103
```
104+
</jax>
105+
</frameworkcontent>
104106

105107
These commands will link the folder you cloned the repository to and your Python library paths.
106108
Python will now look inside the folder you cloned to in addition to the normal library paths.
107-
For example, if your Python packages are typically installed in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python will also search the folder you cloned to: `~/diffusers/`.
109+
For example, if your Python packages are typically installed in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python will also search the `~/diffusers/` folder you cloned to.
108110

109111
<Tip warning={true}>
110112

docs/source/en/stable_diffusion.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ image_grid(images, rows=2, cols=4)
246246
Pretty impressive! Let's tweak the second image - corresponding to the `Generator` with a seed of `1` - a bit more by adding some text about the age of the subject:
247247

248248
```python
249-
prommpts = [
249+
prompts = [
250250
"portrait photo of the oldest warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3 --beta --upbeta",
251251
"portrait photo of a old warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3 --beta --upbeta",
252252
"portrait photo of a warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3 --beta --upbeta",
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Adapt a model to a new task
2+
3+
Many diffusion systems share the same components, allowing you to adapt a pretrained model for one task to an entirely different task.
4+
5+
This guide will show you how to adapt a pretrained text-to-image model for inpainting by initializing and modifying the architecture of a pretrained [`UNet2DConditionModel`].
6+
7+
## Configure UNet2DConditionModel parameters
8+
9+
A [`UNet2DConditionModel`] by default accepts 4 channels in the [input sample](https://huggingface.co/docs/diffusers/v0.16.0/en/api/models#diffusers.UNet2DConditionModel.in_channels). For example, load a pretrained text-to-image model like [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5) and take a look at the number of `in_channels`:
10+
11+
```py
12+
from diffusers import StableDiffusionPipeline
13+
14+
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
15+
pipeline.unet.config["in_channels"]
16+
4
17+
```
18+
19+
Inpainting requires 9 channels in the input sample. You can check this value in a pretrained inpainting model like [`runwayml/stable-diffusion-inpainting`](https://huggingface.co/runwayml/stable-diffusion-inpainting):
20+
21+
```py
22+
from diffusers import StableDiffusionPipeline
23+
24+
pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting")
25+
pipeline.unet.config["in_channels"]
26+
9
27+
```
28+
29+
To adapt your text-to-image model for inpainting, you'll need to change the number of `in_channels` from 4 to 9.
30+
31+
Initialize a [`UNet2DConditionModel`] with the pretrained text-to-image model weights, and change `in_channels` to 9. Changing the number of `in_channels` means you need to set `ignore_mismatched_sizes=True` and `low_cpu_mem_usage=False` to avoid a size mismatch error because the shape is different now.
32+
33+
```py
34+
from diffusers import UNet2DConditionModel
35+
36+
model_id = "runwayml/stable-diffusion-v1-5"
37+
unet = UNet2DConditionModel.from_pretrained(
38+
model_id, subfolder="unet", in_channels=9, low_cpu_mem_usage=False, ignore_mismatched_sizes=True
39+
)
40+
```
41+
42+
The pretrained weights of the other components from the text-to-image model are initialized from their checkpoints, but the input channel weights (`conv_in.weight`) of the `unet` are randomly initialized. It is important to finetune the model for inpainting because otherwise the model returns noise.

docs/source/en/using-diffusers/inpaint.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Now you can create a prompt to replace the mask with something else:
5252

5353
```python
5454
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
55-
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
55+
image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
5656
```
5757

5858
`image` | `mask_image` | `prompt` | output |
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,74 @@
1-
# What is safetensors ?
1+
# Load safetensors
22

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).
3+
[safetensors](https://github.com/huggingface/safetensors) is a safe and fast file format for storing and loading tensors. Typically, PyTorch model weights are saved or *pickled* into a `.bin` file with Python's [`pickle`](https://docs.python.org/3/library/pickle.html) utility. However, `pickle` is not secure and pickled files may contain malicious code that can be executed. safetensors is a secure alternative to `pickle`, making it ideal for sharing model weights.
64

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.
5+
This guide will show you how you load `.safetensor` files, and how to convert Stable Diffusion model weights stored in other formats to `.safetensor`. Before you start, make sure you have safetensors installed:
96

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
7+
```bash
8+
!pip install safetensors
259
```
26-
from diffusers import StableDiffusionPipeline
2710

28-
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
11+
If you look at the [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main) repository, you'll see weights inside the `text_encoder`, `unet` and `vae` subfolders are stored in the `.safetensors` format. By default, 🤗 Diffusers automatically loads these `.safetensors` files from their subfolders if they're available in the model repository.
2912

30-
# Loaded in safetensors 0:00:02.033658
31-
# Loaded in Pytorch 0:00:02.663379
32-
```
13+
For more explicit control, you can optionally set `use_safetensors=True` (if `safetensors` is not installed, you'll get an error message asking you to install it):
3314

34-
This is for the entire loading time, the actual weights loading time to load 500MB:
15+
```py
16+
from diffusers import DiffusionPipeline
3517

36-
```
37-
Safetensors: 3.4873ms
38-
PyTorch: 172.7537ms
18+
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_safetensors=True)
3919
```
4020

41-
Performance in general is a tricky business, and there are a few things to understand:
21+
However, model weights are not necessarily stored in separate subfolders like in the example above. Sometimes, all the weights are stored in a single `.safetensors` file. In this case, if the weights are Stable Diffusion weights, you can load the file directly with the [`~diffusers.loaders.FromCkptMixin.from_ckpt`] method:
4222

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.
23+
```py
24+
from diffusers import StableDiffusionPipeline
4825

49-
# How to use safetensors ?
26+
pipeline = StableDiffusionPipeline.from_ckpt(
27+
"https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
28+
)
29+
```
5030

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.
31+
## Convert to safetensors
5332

54-
If you are really paranoid about this, the ultimate weapon would be disabling `torch.load`:
55-
```python
56-
import torch
33+
Not all weights on the Hub are available in the `.safetensors` format, and you may encounter weights stored as `.bin`. In this case, use the Space below to convert the weights to `.safetensors`. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted `.safetensors` file on the Hub. This way, if there is any malicious code contained in the pickled files, they're uploaded to the Hub - which has a [security scanner](https://huggingface.co/docs/hub/security-pickle#hubs-security-scanner) to detect unsafe files and suspicious pickle imports - instead of your computer.
5734

35+
<iframe
36+
src="https://safetensors-convert.hf.space"
37+
frameborder="0"
38+
width="850"
39+
height="450"
40+
></iframe>
5841

59-
def _raise():
60-
raise RuntimeError("I don't want to use pickle")
42+
You can use the model with the new `.safetensors` weights by specifying the reference to the Pull Request in the `revision` parameter (you can also test it in this [Check PR](https://huggingface.co/spaces/diffusers/check_pr) Space on the Hub), for example `refs/pr/22`:
6143

44+
```py
45+
from diffusers import DiffusionPipeline
6246

63-
torch.load = lambda *args, **kwargs: _raise()
47+
pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", revision="refs/pr/22")
6448
```
6549

66-
# I want to use model X but it doesn't have safetensors weights.
50+
## Why use safetensors?
6751

68-
Just go to this [space](https://huggingface.co/spaces/diffusers/convert).
69-
This will create a new PR with the weights, let's say `refs/pr/22`.
52+
There are several reasons for using safetensors:
7053

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.
54+
- Safety is the number one reason for using safetensors. As open-source and model distribution grows, it is important to be able to trust the model weights you downloaded don't contain any malicious code. The current size of the header in safetensors prevents parsing extremely large JSON files.
55+
- Loading speed between switching models is another reason to use safetensors, which performs zero-copy of the tensors. It is especially fast compared to `pickle` if you're loading the weights to CPU (the default case), and just as fast if not faster when directly loading the weights to GPU. You'll only notice the performance difference if the model is already loaded, and not if you're downloading the weights or loading the model for the first time.
7456

75-
Then in order to use the model, even before the branch gets accepted by the original author you can do:
57+
The time it takes to load the entire pipeline:
7658

77-
```python
78-
from diffusers import DiffusionPipeline
59+
```py
60+
from diffusers import StableDiffusionPipeline
7961

80-
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", revision="refs/pr/22")
81-
```
62+
pipeline = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
63+
"Loaded in safetensors 0:00:02.033658"
64+
"Loaded in PyTorch 0:00:02.663379"
65+
```
8266

83-
or you can test it directly online with this [space](https://huggingface.co/spaces/diffusers/check_pr).
67+
But the actual time it takes to load 500MB of the model weights is only:
8468

85-
And that's it !
69+
```bash
70+
safetensors: 3.4873ms
71+
PyTorch: 172.7537ms
72+
```
8673

87-
Anything unclear, concerns, or found a bugs ? [Open an issue](https://github.com/huggingface/diffusers/issues/new/choose)
74+
- Lazy loading is also supported in safetensors, which is useful in distributed settings to only load some of the tensors. This format allowed the [BLOOM](https://huggingface.co/bigscience/bloom) model to be loaded in 45 seconds on 8 GPUs instead of 10 minutes with regular PyTorch weights.

0 commit comments

Comments
 (0)