Distilled Stable Diffusion inference

Distilled Stable Diffusion inference

Open In ColabOpen In Studio Lab

Stable Diffusion inference can be a computationally intensive process because it must iteratively denoise the latents to generate an image. To reduce the computational burden, you can use a distilled version of the Stable Diffusion model from Nota AIarrow-up-right. The distilled version of their Stable Diffusion model eliminates some of the residual and attention blocks from the UNet, reducing the model size by 51% and improving latency on CPU/GPU by 43%.

Read this blog postarrow-up-right to learn more about how knowledge distillation training works to produce a faster, smaller, and cheaper generative model.

Let’s load the distilled Stable Diffusion model and compare it against the original Stable Diffusion model:

Copied

from diffusers import StableDiffusionPipeline
import torch

distilled = StableDiffusionPipeline.from_pretrained(
    "nota-ai/bk-sdm-small", torch_dtype=torch.float16, use_safetensors=True,
).to("cuda")

original = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16, use_safetensors=True,
).to("cuda")

Given a prompt, get the inference time for the original model:

Copied

Time the distilled model inference:

Copied

original Stable Diffusion (45781.5 ms)
distilled Stable Diffusion (29884.2 ms)

Tiny AutoEncoder

To speed inference up even more, use a tiny distilled version of the Stable Diffusion VAEarrow-up-right to denoise the latents into images. Replace the VAE in the distilled Stable Diffusion model with the tiny VAE:

Copied

Time the distilled model and distilled VAE inference:

Copied

distilled Stable Diffusion + Tiny AutoEncoder (27165.7 ms)

Last updated