
SDXL-Lightning 是一款极速文本生成图像模型。它仅需几步即可生成高质量的 1024px 图像。更多信息,请参考我们的研究论文:SDXL-Lightning: Progressive Adversarial Diffusion Distillation。我们将该模型作为研究的一部分开源。
我们的模型源自 stabilityai/stable-diffusion-xl-base-1.0 的蒸馏。本仓库包含 1 步、2 步、4 步和 8 步蒸馏模型的检查点。我们的 2 步、4 步和 8 步模型生成质量令人惊叹。1 步模型则更具实验性。
我们提供完整的 UNet 和 LoRA 检查点。完整的 UNet 模型质量最佳,而 LoRA 模型可应用于其他基础模型。
sdxl_lightning_Nstep.safetensors:一体化检查点,适用于 ComfyUI。sdxl_lightning_Nstep_unet.safetensors:仅包含 UNet 检查点,适用于 Diffusers。sdxl_lightning_Nstep_lora.safetensors:LoRA 检查点,适用于 Diffusers 和 ComfyUI。请始终为相应的推理步骤使用正确的检查点。
import torch
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from safetensors.torch import load_file
from openmind import is_torch_npu_available
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
base = "./PyTorch-NPU/stable-diffusion-xl-base-1_0"
ckpt = "./PyTorch-NPU/SDXL-Lightning/sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
# Load model.
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to(device, torch.float16)
unet.load_state_dict(load_file(ckpt))
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to(device)
# Ensure sampler uses "trailing" timesteps.
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
# Ensure using the same inference steps as the loaded model and CFG set to 0.
pipe("A girl smiling", num_inference_steps=4, guidance_scale=0).images[0].save("output.png")仅在使用非 SDXL 基础模型时使用 LoRA。否则,请使用我们的 UNet 检查点以获得更好的质量。
import torch
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
from openmind import is_torch_npu_available
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
base = "./PyTorch-NPU/stable-diffusion-xl-base-1_0"
ckpt = "./PyTorch-NPU/SDXL-Lightning/sdxl_lightning_4step_lora.safetensors" # Use the correct ckpt for your step setting!
# Load model.
pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to(device)
pipe.load_lora_weights(ckpt)
pipe.fuse_lora()
# Ensure sampler uses "trailing" timesteps.
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
# Ensure using the same inference steps as the loaded model and CFG set to 0.
pipe("A girl smiling", num_inference_steps=4, guidance_scale=0).images[0].save("output.png")1-step 模型仅为实验性模型,质量稳定性较差。建议使用 2-step 模型以获得更优质量。
1-step 模型采用“sample”预测而非“epsilon”预测!需要正确配置调度器。
import torch
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from safetensors.torch import load_file
from openmind import is_torch_npu_available
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
base = "./PyTorch-NPU/stable-diffusion-xl-base-1_0"
ckpt = "./PyTorch-NPU/SDXL-Lightning/sdxl_lightning_1step_unet_x0.safetensors" # Use the correct ckpt for your step setting!
# Load model.
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to(device, torch.float16)
unet.load_state_dict(load_file(ckpt))
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to(device)
# Ensure sampler uses "trailing" timesteps and "sample" prediction type.
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample")
# Ensure using the same inference steps as the loaded model and CFG set to 0.
pipe("A girl smiling", num_inference_steps=1, guidance_scale=0).images[0].save("output.png")请务必为相应的推理步数使用正确的检查点。 请使用 Euler 采样器并搭配 sgm_uniform 调度器。
sdxl_lightning_Nstep.safetensors)下载至 /ComfyUI/models/checkpoints。
仅在使用非 SDXL 基础模型时才使用 LoRA。否则,请使用我们的完整检查点以获得更佳质量。
sdxl_lightning_Nstep_lora.safetensors)下载至 /ComfyUI/models/loras。
1 步模型仅为实验性版本,质量稳定性较差。建议使用 2 步模型以获得显著更优的质量。
sdxl_lightning_1step_x0.safetensors)下载至 /ComfyUI/models/checkpoints。
@misc{lin2024sdxllightning,
title={SDXL-Lightning: Progressive Adversarial Diffusion Distillation},
author={Shanchuan Lin and Anran Wang and Xiao Yang},
year={2024},
eprint={2402.13929},
archivePrefix={arXiv},
primaryClass={cs.CV}
}