算力平台:
OpenVINO
🤗 Optimum 提供了与 OpenVINO 兼容的 Stable Diffusion 管道,可在各种英特尔处理器上执行推理(请参阅 完整列表 中支持的设备)。
你需要使用 --upgrade-strategy eager
选项安装 🤗 Optimum Intel,以确保 optimum-intel
使用最新版本:
bash
pip install --upgrade-strategy eager optimum["openvino"]
本指南将向你展示如何使用 Stable Diffusion 和 Stable Diffusion XL (SDXL) 管道与 OpenVINO。
Stable Diffusion
要加载并运行推理,请使用 [~optimum.intel.OVStableDiffusionPipeline
]. 如果你想加载 PyTorch 模型并将其动态转换为 OpenVINO 格式,请设置 export=True
:
python
from optimum.intel import OVStableDiffusionPipeline
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]
# Don't forget to save the exported model
pipeline.save_pretrained("openvino-sd-v1-5")
为了进一步加速推理,请静态重塑模型。如果你更改了任何参数,例如输出高度或宽度,你需要再次静态重塑模型。
python
# Define the shapes related to the inputs and desired outputs
batch_size, num_images, height, width = 1, 1, 512, 512
# Statically reshape the model
pipeline.reshape(batch_size, height, width, num_images)
# Compile the model before inference
pipeline.compile()
image = pipeline(
prompt,
height=height,
width=width,
num_images_per_prompt=num_images,
).images[0]

你可以在 🤗 Optimum 文档 中找到更多示例,Stable Diffusion 支持文本到图像、图像到图像和修复。
Stable Diffusion XL
要加载并运行 SDXL 推理,请使用 [~optimum.intel.OVStableDiffusionXLPipeline
]:
python
from optimum.intel import OVStableDiffusionXLPipeline
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = OVStableDiffusionXLPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]
为了进一步加速推理,请参考 Stable Diffusion 部分中的说明 静态重塑 模型。
你可以在 🤗 Optimum 文档 中找到更多示例,在 OpenVINO 中运行 SDXL 支持文本到图像和图像到图像。