Skip to content

Commit

Permalink
add ort + stable diffusion documentation (huggingface#1205)
Browse files Browse the repository at this point in the history
Co-authored-by: Prathik Rao <[email protected]>
  • Loading branch information
2 people authored and baskrahmer committed Jul 22, 2023
1 parent 0ec7afa commit a4dbf8b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/source/onnxruntime/usage_guides/trainer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,47 @@ in the Optimum repository.

</Tip>

## ORTModule+StableDiffusion

Optimum supports accelerating Hugging Face Diffusers with ONNX Runtime in [this example](https://github.com/huggingface/optimum/tree/main/examples/onnxruntime/training/stable-diffusion/text-to-image).
The core changes required to enable ONNX Runtime Training are summarized below:

```diff
import torch
from diffusers import AutoencoderKL, UNet2DConditionModel
from transformers import CLIPTextModel

+from onnxruntime.training.ortmodule import ORTModule
+from onnxruntime.training.optim.fp16_optimizer import FP16_Optimizer as ORT_FP16_Optimizer

unet = UNet2DConditionModel.from_pretrained(
"CompVis/stable-diffusion-v1-4",
subfolder="unet",
...
)
text_encoder = CLIPTextModel.from_pretrained(
"CompVis/stable-diffusion-v1-4",
subfolder="text_encoder",
...
)
vae = AutoencoderKL.from_pretrained(
"CompVis/stable-diffusion-v1-4",
subfolder="vae",
...
)

optimizer = torch.optim.AdamW(
unet.parameters(),
...
)

+vae = ORTModule(vae)
+text_encoder = ORTModule(text_encoder)
+unet = ORTModule(unet)

+optimizer = ORT_FP16_Optimizer(optimizer)
```

## Other Resources

* Blog posts
Expand Down

0 comments on commit a4dbf8b

Please sign in to comment.