• 文件 >
  • 使用 torch.compile 後端編譯 Stable Diffusion 模型
快捷方式

使用 torch.compile 後端編譯 Stable Diffusion 模型

本互動式指令碼旨在作為使用 torch.compile 後端在 Stable Diffusion 模型上執行 Torch-TensorRT 工作流程的示例。下面展示了一個示例輸出

../../../_images/majestic_castle.png

匯入與模型定義

import torch
import torch_tensorrt
from diffusers import DiffusionPipeline

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda:0"

# Instantiate Stable Diffusion Pipeline with FP16 weights
pipe = DiffusionPipeline.from_pretrained(
    model_id, revision="fp16", torch_dtype=torch.float16
)
pipe = pipe.to(device)

backend = "torch_tensorrt"

# Optimize the UNet portion with Torch-TensorRT
pipe.unet = torch.compile(
    pipe.unet,
    backend=backend,
    options={
        "truncate_long_and_double": True,
        "enabled_precisions": {torch.float32, torch.float16},
    },
    dynamic=False,
)

推理

prompt = "a majestic castle in the clouds"
image = pipe(prompt).images[0]

image.save("images/majestic_castle.png")
image.show()

指令碼總執行時間:( 0 分鐘 0.000 秒)

由 Sphinx-Gallery 生成的相簿

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源