模型概述
模型特點
模型能力
使用案例
🚀 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}
}

