Skip to content

Docs: fp16 page #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
title: "Using Diffusers"
- sections:
- local: optimization/fp16
title: "Torch Float16"
title: "Memory and Speed"
- local: optimization/onnx
title: "ONNX"
- local: optimization/open_vino
Expand Down
2 changes: 1 addition & 1 deletion docs/source/_toctree_new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
title: "Pipelines for Inference"
- sections:
- local: optimization/fp16
title: "Torch Float16"
title: "Memory and Speed"
- local: optimization/onnx
title: "ONNX"
- local: optimization/open_vino
Expand Down
52 changes: 44 additions & 8 deletions docs/source/optimization/fp16.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,59 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
specific language governing permissions and limitations under the License.
-->

# Memory and speed

We present some techniques and ideas to optimize 🤗 Diffusers _inference_ for memory or speed.

# Quicktour
## CUDA `autocast`

Start using Diffusers🧨 quickly!
To start, use the [`DiffusionPipeline`] for quick inference and sample generations!
If you use a CUDA GPU, you can take advantage of `torch.autocast` to perform much faster inference. All you need to do is put your inference call inside an `autocast` context manager. The following example shows how to do it using Stable Diffusion text-to-image generation as an example:

```Python
from torch import autocast
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
image = pipe(prompt).images[0]
```
pip install diffusers

## Half precision weights

In order to save a big chunk of GPU memory, you can load the model weights in half precision. This involves loading the float16 version of the weights, which was saved to a branch named `fp16`, and telling PyTorch to use the `float16` type when loading them:

```Python
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True
)
```

## Main classes
## Sliced attention for additional memory savings

### Models
For even additional memory savings, you can use a sliced version of attention that performs the computation in steps instead of all at once. You only need to invoke `enable_attention_slicing()` in your pipeline before inference, like here:

### Schedulers
```Python
import torch
from diffusers import StableDiffusionPipeline

### Pipeliens
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
pipe.enable_attention_slicing()
with torch.autocast("cuda"):
image = pipe(prompt).images[0]
```

There's a small performance penalty of about 10% slower inference times, but this method allows you to use Stable Diffusion in as little as 3.2 GB of VRAM!
2 changes: 1 addition & 1 deletion examples/textual_inversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ from torch import autocast
from diffusers import StableDiffusionPipeline

model_id = "path-to-your-trained-model"
pipe = pipe = StableDiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.float16).to("cuda")
pipe = StableDiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.float16).to("cuda")

prompt = "A <cat-toy> backpack"

Expand Down