onnx.md 3.54 KB
Newer Older
1
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
Nathan Lambert's avatar
Nathan Lambert committed
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
# ONNX Runtime
Patrick von Platen's avatar
Patrick von Platen committed
14

15
🤗 [Optimum](https://github.com/huggingface/optimum) provides a Stable Diffusion pipeline compatible with ONNX Runtime. You'll need to install 🤗 Optimum with the following command for ONNX Runtime support:
Patrick von Platen's avatar
Patrick von Platen committed
16

17
```bash
18
pip install -q optimum["onnxruntime"]
19
```
Patrick von Platen's avatar
Patrick von Platen committed
20

21
This guide will show you how to use the Stable Diffusion and Stable Diffusion XL (SDXL) pipelines with ONNX Runtime.
Patrick von Platen's avatar
Patrick von Platen committed
22

23
## Stable Diffusion
24

25
To load and run inference, use the [`~optimum.onnxruntime.ORTStableDiffusionPipeline`]. If you want to load a PyTorch model and convert it to the ONNX format on-the-fly, set `export=True`:
Patrick von Platen's avatar
Patrick von Platen committed
26

27
```python
28
from optimum.onnxruntime import ORTStableDiffusionPipeline
29

30
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
31
32
33
34
pipeline = ORTStableDiffusionPipeline.from_pretrained(model_id, export=True)
prompt = "sailing ship in storm by Leonardo da Vinci"
image = pipeline(prompt).images[0]
pipeline.save_pretrained("./onnx-stable-diffusion-v1-5")
35
```
Patrick von Platen's avatar
Patrick von Platen committed
36

37
38
39
40
41
42
43
44
<Tip warning={true}>

Generating multiple prompts in a batch seems to take too much memory. While we look into it, you may need to iterate instead of batching.

</Tip>

To export the pipeline in the ONNX format offline and use it later for inference,
use the [`optimum-cli export`](https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli) command:
45

46
```bash
47
optimum-cli export onnx --model stable-diffusion-v1-5/stable-diffusion-v1-5 sd_v15_onnx/
48
49
```

50
Then to perform inference (you don't have to specify `export=True` again):
51

52
```python
53
from optimum.onnxruntime import ORTStableDiffusionPipeline
54

55
model_id = "sd_v15_onnx"
56
57
58
pipeline = ORTStableDiffusionPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Leonardo da Vinci"
image = pipeline(prompt).images[0]
59
60
```

61
62
63
64
<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/optimum/documentation-images/resolve/main/onnxruntime/stable_diffusion_v1_5_ort_sail_boat.png">
</div>

65
You can find more examples in 🤗 Optimum [documentation](https://huggingface.co/docs/optimum/), and Stable Diffusion is supported for text-to-image, image-to-image, and inpainting.
66
67
68

## Stable Diffusion XL

69
To load and run inference with SDXL, use the [`~optimum.onnxruntime.ORTStableDiffusionXLPipeline`]:
70
71
72
73

```python
from optimum.onnxruntime import ORTStableDiffusionXLPipeline

Ella Charlaix's avatar
Ella Charlaix committed
74
75
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = ORTStableDiffusionXLPipeline.from_pretrained(model_id)
76
77
78
79
prompt = "sailing ship in storm by Leonardo da Vinci"
image = pipeline(prompt).images[0]
```

80
To export the pipeline in the ONNX format and use it later for inference, use the [`optimum-cli export`](https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli) command:
81

82
83
84
```bash
optimum-cli export onnx --model stabilityai/stable-diffusion-xl-base-1.0 --task stable-diffusion-xl sd_xl_onnx/
```
Patrick von Platen's avatar
Patrick von Platen committed
85

86
SDXL in the ONNX format is supported for text-to-image and image-to-image.