Diffusers documentation

ONNX Runtime

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ONNX Runtime

🤗 Optimum は、ONNX Runtime 互換の Stable Diffusion パイプラインを提供しています。ONNX Runtime 対応の 🤗 Optimum をインストールするには、以下のコマンドを実行してください。

pip install -q optimum["onnxruntime"]

このガイドでは、ONNX Runtime を使用して Stable Diffusion および Stable Diffusion XL (SDXL) パイプラインを扱う方法を紹介します。

Stable Diffusion

読み込みと推論の実行には、ORTStableDiffusionPipelineを使用します。PyTorchモデルを読み込んでONNX形式にオンザフライで変換したい場合は、export=Trueを設定します。

from optimum.onnxruntime import ORTStableDiffusionPipeline

model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
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")

バッチで複数のプロンプトを生成すると、メモリ使用量が多すぎるようです。調査中ですので、その間は一括処理ではなく、ループで処理する必要があるかもしれません。

ONNX形式でパイプラインをオフラインにエクスポートし、後で推論に使用するには、optimum-cli exportコマンドを使用します。

optimum-cli export onnx --model stable-diffusion-v1-5/stable-diffusion-v1-5 sd_v15_onnx/

次に、推論を実行します(export=Trueを再度指定する必要はありません):

from optimum.onnxruntime import ORTStableDiffusionPipeline

model_id = "sd_v15_onnx"
pipeline = ORTStableDiffusionPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Leonardo da Vinci"
image = pipeline(prompt).images[0]

🤗 Optimumのドキュメントにさらに多くの例があります。Stable Diffusionは、テキストから画像への変換、画像から画像への変換、インペインティングに対応しています。

Stable Diffusion XL

SDXLで読み込みと推論を実行するには、ORTStableDiffusionXLPipelineを使用します。

from optimum.onnxruntime import ORTStableDiffusionXLPipeline

model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = ORTStableDiffusionXLPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Leonardo da Vinci"
image = pipeline(prompt).images[0]

ONNX形式でパイプラインをエクスポートし、後で推論に使用するには、optimum-cli exportコマンドを使用します。

optimum-cli export onnx --model stabilityai/stable-diffusion-xl-base-1.0 --task stable-diffusion-xl sd_xl_onnx/

ONNX形式のSDXLは、テキストから画像への生成と画像から画像への生成に対応しています。

Update on GitHub