High-speed image generation SDXL-Lightning

MLBoy
2 min readFeb 21, 2024

1 second image generation

Fast and high quality.
When I tried it on A100GPU, it was generated in about 1 second.
It seems to use a progressive adversarial diffusion distillation method.

person disassembling a computer

How to use

install

pip install diffusers
import torch
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file

base = "stabilityai/stable-diffusion-xl-base-1.0"
repo = "ByteDance/SDXL-Lightning"
ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!

# Load model.
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")

# Ensure sampler uses "trailing" timesteps.
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")

Run

pipe("person disassembling a computer",negative_prompt="anime, illustration", num_inference_steps=4, guidance_scale=0).images[0].save("output.png")

example

RPG characters
a person drinking coffe

It’s fun because you can see the images right away.

🐣

I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.
rockyshikoku@gmail.com

I am creating applications using machine learning and AR technology.

I send machine learning / AR related information.

GitHub

Twitter
Medium

--

--