onnx.mdx 2.48 KB
Newer Older
Patrick von Platen's avatar
Patrick von Platen committed
1
<!--Copyright 2023 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.
-->

Patrick von Platen's avatar
Patrick von Platen committed
13

14
# How to use the ONNX Runtime for inference
Patrick von Platen's avatar
Patrick von Platen committed
15

16
🤗 Diffusers provides a Stable Diffusion pipeline compatible with the ONNX Runtime. This allows you to run Stable Diffusion on any hardware that supports ONNX (including CPUs), and where an accelerated version of PyTorch is not available.
Patrick von Platen's avatar
Patrick von Platen committed
17

18
## Installation
Patrick von Platen's avatar
Patrick von Platen committed
19

20
- TODO
Patrick von Platen's avatar
Patrick von Platen committed
21

22
## Stable Diffusion Inference
Patrick von Platen's avatar
Patrick von Platen committed
23

24
The snippet below demonstrates how to use the ONNX runtime. You need to use `OnnxStableDiffusionPipeline` instead of `StableDiffusionPipeline`. You also need to download the weights from the `onnx` branch of the repository, and indicate the runtime provider you want to use.
Patrick von Platen's avatar
Patrick von Platen committed
25

26
27
```python
# make sure you're logged in with `huggingface-cli login`
28
from diffusers import OnnxStableDiffusionPipeline
Patrick von Platen's avatar
Patrick von Platen committed
29

30
pipe = OnnxStableDiffusionPipeline.from_pretrained(
apolinario's avatar
apolinario committed
31
    "runwayml/stable-diffusion-v1-5",
32
33
34
35
36
37
38
    revision="onnx",
    provider="CUDAExecutionProvider",
)

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
```
Patrick von Platen's avatar
Patrick von Platen committed
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
The snippet below demonstrates how to use the ONNX runtime with the Stable Diffusion upscaling pipeline.

```python
from diffusers import OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline

prompt = "a photo of an astronaut riding a horse on mars"
steps = 50

txt2img = OnnxStableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    revision="onnx",
    provider="CUDAExecutionProvider",
)
small_image = txt2img(
    prompt,
    num_inference_steps=steps,
).images[0]

generator = torch.manual_seed(0)
upscale = OnnxStableDiffusionUpscalePipeline.from_pretrained(
    "ssube/stable-diffusion-x4-upscaler-onnx",
    provider="CUDAExecutionProvider",
)
large_image = upscale(
    prompt,
    small_image,
    generator=generator,
    num_inference_steps=steps,
).images[0]
```

71
## Known Issues
Patrick von Platen's avatar
Patrick von Platen committed
72

73
- 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.