@@ -13,61 +13,53 @@ specific language governing permissions and limitations under the License.
13
13
14
14
# How to use the ONNX Runtime for inference
15
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.
16
+ 🤗 [Optimum](https://github.com/huggingface/optimum) provides a Stable Diffusion pipeline compatible with ONNX Runtime.
17
17
18
18
## Installation
19
19
20
- - TODO
20
+ Install 🤗 Optimum with the following command for ONNX Runtime support:
21
+
22
+ ```
23
+ pip install optimum["onnxruntime"]
24
+ ```
21
25
22
26
## Stable Diffusion Inference
23
27
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.
28
+ To load an ONNX model and run inference with the ONNX Runtime, you need to replace [`StableDiffusionPipeline`] with `ORTStableDiffusionPipeline`. In case you want to load
29
+ a PyTorch model and convert it to the ONNX format on-the-fly, you can set `export=True`.
25
30
26
31
```python
27
- # make sure you're logged in with `huggingface-cli login`
28
- from diffusers import OnnxStableDiffusionPipeline
29
-
30
- pipe = OnnxStableDiffusionPipeline.from_pretrained(
31
- " runwayml/stable-diffusion-v1-5" ,
32
- revision = " onnx" ,
33
- provider = " CUDAExecutionProvider" ,
34
- )
32
+ from optimum.onnxruntime import ORTStableDiffusionPipeline
35
33
34
+ model_id = " runwayml/stable-diffusion-v1-5"
35
+ pipe = ORTStableDiffusionPipeline.from_pretrained(model_id, export =True)
36
36
prompt = " a photo of an astronaut riding a horse on mars"
37
- image = pipe(prompt).images[0]
37
+ images = pipe(prompt).images[0]
38
+ pipe.save_pretrained("./onnx-stable-diffusion-v1-5")
38
39
```
39
40
40
- The snippet below demonstrates how to use the ONNX runtime with the Stable Diffusion upscaling pipeline.
41
+ If you want to export the pipeline in the ONNX format offline and later use it for inference,
42
+ you can 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:
41
43
42
- ```python
43
- from diffusers import OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline
44
+ ```bash
45
+ optimum-cli export onnx --model runwayml/stable-diffusion-v1-5 sd_v15_onnx/
46
+ ```
47
+
48
+ Then perform inference:
49
+
50
+ ```python
51
+ from optimum.onnxruntime import ORTStableDiffusionPipeline
44
52
53
+ model_id = " sd_v15_onnx"
54
+ pipe = ORTStableDiffusionPipeline.from_pretrained(model_id)
45
55
prompt = " a photo of an astronaut riding a horse on mars"
46
- steps = 50
47
-
48
- txt2img = OnnxStableDiffusionPipeline.from_pretrained(
49
- " runwayml/stable-diffusion-v1-5" ,
50
- revision = " onnx" ,
51
- provider = " CUDAExecutionProvider" ,
52
- )
53
- small_image = txt2img(
54
- prompt,
55
- num_inference_steps =steps,
56
- ).images[0]
57
-
58
- generator = torch.manual_seed(0)
59
- upscale = OnnxStableDiffusionUpscalePipeline.from_pretrained(
60
- " ssube/stable-diffusion-x4-upscaler-onnx" ,
61
- provider = " CUDAExecutionProvider" ,
62
- )
63
- large_image = upscale(
64
- prompt,
65
- small_image,
66
- generator =generator,
67
- num_inference_steps =steps,
68
- ).images[0]
56
+ images = pipe(prompt).images[0]
69
57
```
70
58
59
+ Notice that we didn't have to specify `export=True` above.
60
+
61
+ You can find more examples in [optimum documentation](https://huggingface.co/docs/optimum/).
62
+
71
63
## Known Issues
72
64
73
65
- 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.
0 commit comments