Diffusers documentation
스케줄러
스케줄러
스케줄러는 디노이징 루프가 각 단계에서 얼마나 많은 노이즈를 제거할지 알려줍니다. 스케줄러에 따라 속도와 품질을 서로 트레이드오프합니다.
이 가이드에서는 스케줄러를 로드하고 그 timestep schedule, spacing, sigmas를 사용자 정의하는 방법을 보여줍니다.
스케줄러 선택
checkpoint 기본값에서 시작하세요. 속도 또는 품질 트레이드오프가 다른 경우에만 교체하세요.
- DPM++ 2M SDE Karras는 많은 latent diffusion 체크포인트에 대해 강력한 범용 옵션입니다.
- TCDScheduler는 증류된 모델에 잘 작동합니다.
- FlowMatch 모델(Qwen-Image, Flux 및 유사 모델)에는 FlowMatchEulerDiscreteScheduler 또는 FlowMatchHeunDiscreteScheduler를 사용하세요.
- 애니메이션 스타일 이미지에는 EulerDiscreteScheduler 또는 EulerAncestralDiscreteScheduler를 사용하는 것이 효과적입니다.
- 체크포인트가 지원하는 경우, 몇 단계 생성을 위해 LCM UNet 또는 LoRA와 함께 LCMScheduler를 사용합니다.
스케줄러 로드
Qwen-Image, Flux와 같은 Flow-matching 모델은 기본적으로 FlowMatchEulerDiscreteScheduler를 사용합니다. 의도적으로 실험하는 경우가 아니라면 해당 스케줄러를 유지하세요. 대체 스케줄러가 체크포인트와 호환되는 경우에만 from_config()를 사용하여 교체하세요.
Schedulers는 설정만 포함하며 가중치 텐서를 포함하지 않습니다. 파이프라인의 .scheduler 속성에 액세스하여 로드된 설정을 확인하세요.
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.float16, device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.scheduler로드된 파이프라인에서 스케줄러를 교체하려면 기존 스케줄러 설정을 from_config()에 전달하여 num_train_timesteps 및 관련 필드가 정렬되도록 유지하세요. FlowMatch 체크포인트(Qwen-Image, Flux 등)의 경우, 호환되는 대체 스케줄러를 의도적으로 실험하는 것이 아니라면 FlowMatchEulerDiscreteScheduler를 유지하세요.
from diffusers import DPMSolverMultistepScheduler
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)from_pretrained()를 사용하여 Hub에서 스케줄러 설정을 로드한 후, scheduler를 통해 from_pretrained()에 전달할 수도 있습니다.
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
dpm = DPMSolverMultistepScheduler.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler"
)
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
scheduler=dpm,
dtype=torch.float16,
device_map="cuda", # or "mps", "xpu", "cpu"
)
pipeline.schedulerTimestep 스케줄
Timestep 또는 noise schedule는 denoising 과정에서 노이즈가 어떻게 분포되는지를 결정합니다. 스케줄은 선형일 수도 있고, 시작 부분이나 끝 부분에 더 집중되어 있을 수도 있습니다. 이는 스케줄러의 기본 구성에서 생성된 노이즈 레벨의 사전 계산된 시퀀스이지만, 다른 스케줄을 사용하도록 사용자 정의할 수 있습니다.
linear (even steps) AYS (denser where it matters) noise noise ^ ^ | * | * | * | * | * | ** | * | *** | * | ** | * | * | * | * +-----------------> step +-----------------> step
커스텀
timesteps는 해당 스케줄러의set_timesteps가 해당 인수를 지원하는 경우에만 작동합니다(파이프라인은retrieve_timesteps를 통해 시그니처를 확인하고, 그렇지 않으면ValueError를 발생시킵니다). 전달하기 전에 스케줄러의 API 페이지 또는set_timesteps시그니처를 확인하세요.
아래 예제는 Align Your Steps (AYS) 스케줄을 사용하여 10단계 만에 고품질 이미지를 생성할 수 있으며, 이를 통해 생성 속도를 크게 높이고 계산 시간을 줄입니다.
schedule를 가져와서 파이프라인의 timesteps 인자에 전달합니다.
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.schedulers import AysSchedules
sampling_schedule = AysSchedules["StableDiffusionXLTimesteps"]
print(sampling_schedule)
# [999, 845, 730, 587, 443, 310, 193, 116, 53, 13]
pipeline = DiffusionPipeline.from_pretrained(
"SG161222/RealVisXL_V4.0",
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config, algorithm_type="sde-dpmsolver++"
)
prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
prompt=prompt,
negative_prompt="",
timesteps=sampling_schedule,
).images[0]
구버전 체크포인트의 v-prediction/zero-SNR rescaling에 대해서는 Legacy checkpoints를 참조하세요.
Timestep 간격
Timestep spacing은 스케줄에서 샘플링할 특정 단계 t를 의미합니다. Diffusers는 아래에 표시된 세 가지 spacing 유형을 제공합니다.
| spacing 전략 | spacing 계산 | 예시 timesteps |
|---|---|---|
leading | 균등 간격의 단계 | [900, 800, 700, ..., 100, 0] |
linspace | 첫 단계와 마지막 단계를 포함하고 나머지 중간 단계를 균등하게 나눕니다 | [1000, 888.89, 777.78, ..., 111.11, 0] |
trailing | 마지막 단계와 끝에서부터 시작하여 나머지 중간 단계를 균등하게 나눕니다 | [999, 899, 799, 699, 599, 499, 399, 299, 199, 99] |
스케줄러의 timestep_spacing 인자에 간격 전략을 전달합니다.
trailing전략은 일반적으로 더 적은 단계 수로 더 높은 품질과 더 많은 디테일을 가진 이미지를 생성하지만, 보다 표준적인 단계 값에서는 품질 차이가 그렇게 뚜렷하지 않습니다.
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
"SG161222/RealVisXL_V4.0",
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config, timestep_spacing="trailing"
)
prompt = "A cinematic shot of a cute little black cat sitting on a pumpkin at night"
image = pipeline(
prompt=prompt,
negative_prompt="",
num_inference_steps=5,
).images[0]
image
Sigmas
Sigmas는 스케줄에 따라 특정 단계에서 샘플의 노이즈 수준을 나타내는 지표입니다. 사용자 정의 sigmas를 사용하는 경우, timesteps은 기본 스케줄러 구성 대신 이 값들로부터 계산됩니다.
step: 0 1 2 3 4
sigma: σ0 > σ1 > σ2 > σ3 > σ4 ≈ 0
high noise ---> clean sample
sigmas커스텀 값은 해당 스케줄러의set_timesteps가 해당 인수를 지원하는 경우에만 작동합니다. 파이프라인은retrieve_timesteps를 통해 시그니처를 확인하며, 지원하지 않으면ValueError를 발생시킵니다. 전달하기 전에 스케줄러의 API 페이지 또는set_timesteps시그니처를 확인하세요.
커스텀 sigmas를 파이프라인의 sigmas 인자에 전달합니다. 아래 예제는 10단계 AYS 스케줄의 sigmas를 사용합니다.
import torch
from diffusers import DiffusionPipeline, EulerDiscreteScheduler
from diffusers.schedulers import AysSchedules
pipeline = DiffusionPipeline.from_pretrained(
"SG161222/RealVisXL_V4.0",
dtype=torch.float16,
device_map="cuda", # or "mps", "xpu", "cpu"
)
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
sigmas = AysSchedules["StableDiffusionXLSigmas"]
prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
prompt=prompt,
negative_prompt="",
sigmas=sigmas,
).images[0]Karras 시그마
Karras sigmas는 시퀀스 중간, 즉 구조 재건이 중요한 부분에 시그마를 더 밀집하게 클러스터링하고, 노이즈 변화의 영향이 적은 시작과 끝 부분에는 시그마를 적게 사용하여 노이즈 스케줄을 재샘플링함으로써 더 효율적인 샘플링을 수행합니다. 이를 통해 생성된 이미지의 디테일 수준을 높일 수 있습니다.
default σ: * * * * * * * * even-ish
Karras σ: * * * * * * * denser mid, sparser ends
|---structure---|use_karras_sigmas=True를 스케줄러에 설정하여 활성화합니다.
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
"SG161222/RealVisXL_V4.0",
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
pipeline.scheduler.config,
algorithm_type="sde-dpmsolver++",
use_karras_sigmas=True,
)
prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
prompt=prompt,
negative_prompt="",
num_inference_steps=20,
).images[0]
Karras sigmas를 지원하는 스케줄러 목록은 스케줄러 API 개요를 참조하세요. Karras sigmas로 학습된 모델에만 사용해야 합니다.
다음 단계
- Read the Common Diffusion Noise Schedules and Sample Steps are Flawed paper for more details about rescaling the noise schedule to enforce zero SNR.