模型简介
模型特点
模型能力
使用案例
🚀 SDXL-Lightning
SDXL-Lightning 是一款极速的文本到图像生成模型,能够在几步内生成高质量的 1024px 图像。本项目将模型开源以供研究使用,更多信息请参考我们的研究论文:SDXL-Lightning: Progressive Adversarial Diffusion Distillation。
📦 模型信息
属性 | 详情 |
---|---|
模型类型 | 文本到图像生成模型 |
训练数据 | 从 stabilityai/stable-diffusion-xl-base-1.0 蒸馏而来 |
许可证 | OpenRail++ |
✨ 主要特性
- 快速生成:能够在几步内生成高质量的 1024px 图像。
- 多种步数模型:提供 1 步、2 步、4 步和 8 步蒸馏模型的检查点。
- 多种检查点类型:提供完整的 UNet 和 LoRA 检查点,完整 UNet 模型质量最佳,LoRA 模型可应用于其他基础模型。
🚀 快速开始
本模型是从 stabilityai/stable-diffusion-xl-base-1.0 蒸馏而来。此仓库包含 1 步、2 步、4 步和 8 步蒸馏模型的检查点。其中,2 步、4 步和 8 步模型的生成质量惊人,1 步模型则更具实验性。
📦 安装指南
本项目未提供具体安装命令,可根据使用的框架(如 Diffusers、ComfyUI)进行相应的安装和配置。
💻 使用示例
基础用法
以下是使用 Diffusers 库的基础代码示例,展示了如何加载和使用 4 步 UNet 模型:
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")
# 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")
高级用法
以下是使用 LoRA 检查点的高级代码示例:
import torch
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
from huggingface_hub import hf_hub_download
base = "stabilityai/stable-diffusion-xl-base-1.0"
repo = "ByteDance/SDXL-Lightning"
ckpt = "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("cuda")
pipe.load_lora_weights(hf_hub_download(repo, 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")
🔧 不同框架使用说明
Diffusers 框架使用说明
请始终使用与推理步数相对应的正确检查点。
2 步、4 步、8 步 UNet
代码示例见上文基础用法。
2 步、4 步、8 步 LoRA
仅在使用非 SDXL 基础模型时使用 LoRA,否则使用完整的 UNet 检查点以获得更好的质量。代码示例见上文高级用法。
1 步 UNet
1 步模型仅用于实验,质量不太稳定,建议使用 2 步模型以获得更好的质量。该模型使用 "sample" 预测而非 "epsilon" 预测,需要正确配置调度器。
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_1step_unet_x0.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 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")
ComfyUI 框架使用说明
请始终使用与推理步数相对应的正确检查点,并使用带有 sgm_uniform 调度器的 Euler 采样器。
2 步、4 步、8 步完整模型
- 将完整检查点 (
sdxl_lightning_Nstep.safetensors
) 下载到/ComfyUI/models/checkpoints
。 - 下载我们的 ComfyUI 完整工作流。
2 步、4 步、8 步 LoRA
仅在使用非 SDXL 基础模型时使用 LoRA,否则使用完整检查点以获得更好的质量。
- 准备好自己的基础模型。
- 将 LoRA 检查点 (
sdxl_lightning_Nstep_lora.safetensors
) 下载到/ComfyUI/models/loras
。 - 下载我们的 ComfyUI LoRA 工作流。
1 步模型
1 步模型仅用于实验,质量不太稳定,建议使用 2 步模型以获得更好的质量。
- 将 ComfyUI 更新到最新版本。
- 将完整检查点 (
sdxl_lightning_1step_x0.safetensors
) 下载到/ComfyUI/models/checkpoints
。 - 下载我们的 ComfyUI 完整 1 步工作流。
📚 详细文档
如需更多详细信息,请参考我们的研究论文:SDXL-Lightning: Progressive Adversarial Diffusion Distillation。
📄 许可证
本项目采用 OpenRail++ 许可证。
📖 引用我们的工作
如果您使用了本项目的模型或代码,请引用以下论文:
@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}
}

