|
| 1 | +# Distilled Stable Diffusion inference |
| 2 | + |
| 3 | +[[open-in-colab]] |
| 4 | + |
| 5 | +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 AI](https://huggingface.co/nota-ai). 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%. |
| 6 | + |
| 7 | +<Tip> |
| 8 | + |
| 9 | +Read this [blog post](https://huggingface.co/blog/sd_distillation) to learn more about how knowledge distillation training works to produce a faster, smaller, and cheaper generative model. |
| 10 | + |
| 11 | +</Tip> |
| 12 | + |
| 13 | +Let's load the distilled Stable Diffusion model and compare it against the original Stable Diffusion model: |
| 14 | + |
| 15 | +```py |
| 16 | +from diffusers import StableDiffusionPipeline |
| 17 | +import torch |
| 18 | + |
| 19 | +distilled = StableDiffusionPipeline.from_pretrained( |
| 20 | + "nota-ai/bk-sdm-small", torch_dtype=torch.float16, use_safetensors=True, |
| 21 | +).to("cuda") |
| 22 | + |
| 23 | +original = StableDiffusionPipeline.from_pretrained( |
| 24 | + "CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16, use_safetensors=True, |
| 25 | +).to("cuda") |
| 26 | +``` |
| 27 | + |
| 28 | +Given a prompt, get the inference time for the original model: |
| 29 | + |
| 30 | +```py |
| 31 | +import time |
| 32 | + |
| 33 | +seed = 2023 |
| 34 | +generator = torch.manual_seed(seed) |
| 35 | + |
| 36 | +NUM_ITERS_TO_RUN = 3 |
| 37 | +NUM_INFERENCE_STEPS = 25 |
| 38 | +NUM_IMAGES_PER_PROMPT = 4 |
| 39 | + |
| 40 | +prompt = "a golden vase with different flowers" |
| 41 | + |
| 42 | +start = time.time_ns() |
| 43 | +for _ in range(NUM_ITERS_TO_RUN): |
| 44 | + images = original( |
| 45 | + prompt, |
| 46 | + num_inference_steps=NUM_INFERENCE_STEPS, |
| 47 | + generator=generator, |
| 48 | + num_images_per_prompt=NUM_IMAGES_PER_PROMPT |
| 49 | + ).images |
| 50 | +end = time.time_ns() |
| 51 | +original_sd = f"{(end - start) / 1e6:.1f}" |
| 52 | + |
| 53 | +print(f"Execution time -- {original_sd} ms\n") |
| 54 | +"Execution time -- 45781.5 ms" |
| 55 | +``` |
| 56 | + |
| 57 | +Time the distilled model inference: |
| 58 | + |
| 59 | +```py |
| 60 | +start = time.time_ns() |
| 61 | +for _ in range(NUM_ITERS_TO_RUN): |
| 62 | + images = distilled( |
| 63 | + prompt, |
| 64 | + num_inference_steps=NUM_INFERENCE_STEPS, |
| 65 | + generator=generator, |
| 66 | + num_images_per_prompt=NUM_IMAGES_PER_PROMPT |
| 67 | + ).images |
| 68 | +end = time.time_ns() |
| 69 | + |
| 70 | +distilled_sd = f"{(end - start) / 1e6:.1f}" |
| 71 | +print(f"Execution time -- {distilled_sd} ms\n") |
| 72 | +"Execution time -- 29884.2 ms" |
| 73 | +``` |
| 74 | + |
| 75 | +<div class="flex gap-4"> |
| 76 | + <div> |
| 77 | + <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/original_sd.png"/> |
| 78 | + <figcaption class="mt-2 text-center text-sm text-gray-500">original Stable Diffusion (45781.5 ms)</figcaption> |
| 79 | + </div> |
| 80 | + <div> |
| 81 | + <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/distilled_sd.png"/> |
| 82 | + <figcaption class="mt-2 text-center text-sm text-gray-500">distilled Stable Diffusion (29884.2 ms)</figcaption> |
| 83 | + </div> |
| 84 | +</div> |
| 85 | + |
| 86 | +## Tiny AutoEncoder |
| 87 | + |
| 88 | +To speed inference up even more, use a tiny distilled version of the [Stable Diffusion VAE](https://huggingface.co/sayakpaul/taesdxl-diffusers) to denoise the latents into images. Replace the VAE in the distilled Stable Diffusion model with the tiny VAE: |
| 89 | + |
| 90 | +```py |
| 91 | +from diffusers import AutoencoderTiny |
| 92 | + |
| 93 | +distilled.vae = AutoencoderTiny.from_pretrained( |
| 94 | + "sayakpaul/taesd-diffusers", torch_dtype=torch.float16, use_safetensors=True, |
| 95 | +).to("cuda") |
| 96 | +``` |
| 97 | + |
| 98 | +Time the distilled model and distilled VAE inference: |
| 99 | + |
| 100 | +```py |
| 101 | +start = time.time_ns() |
| 102 | +for _ in range(NUM_ITERS_TO_RUN): |
| 103 | + images = distilled( |
| 104 | + prompt, |
| 105 | + num_inference_steps=NUM_INFERENCE_STEPS, |
| 106 | + generator=generator, |
| 107 | + num_images_per_prompt=NUM_IMAGES_PER_PROMPT |
| 108 | + ).images |
| 109 | +end = time.time_ns() |
| 110 | + |
| 111 | +distilled_tiny_sd = f"{(end - start) / 1e6:.1f}" |
| 112 | +print(f"Execution time -- {distilled_tiny_sd} ms\n") |
| 113 | +"Execution time -- 27165.7 ms" |
| 114 | +``` |
| 115 | + |
| 116 | +<div class="flex justify-center"> |
| 117 | + <div> |
| 118 | + <img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/distilled_sd_vae.png" /> |
| 119 | + <figcaption class="mt-2 text-center text-sm text-gray-500">distilled Stable Diffusion + Tiny AutoEncoder (27165.7 ms)</figcaption> |
| 120 | + </div> |
| 121 | +</div> |
0 commit comments