Diffusers documentation
UniPCMultistepScheduler
UniPCMultistepScheduler
UniPCMultistepScheduler 是一个专为扩散模型快速采样而设计的免训练框架。它由 Wenliang Zhao、Lujia Bai、Yongming Rao、Jie Zhou 和 Jiwen Lu 在 UniPC: A Unified Predictor-Corrector Framework for Fast Sampling of Diffusion Models 中提出。
它由校正器(UniC)和预测器(UniP)组成,二者共享统一的解析形式并支持任意阶数。 UniPC 在设计上是模型无关的,支持像素空间/潜在空间 DPM 的无条件/条件采样。它同样适用于噪声预测模型和数据预测模型。校正器 UniC 也可以应用于任何现成的求解器之后,以提高精度阶数。
该论文的摘要如下:
扩散概率模型(DPMs)在高分辨率图像合成方面展现了极具前景的能力。然而,由于需要对去噪网络进行多次评估,从预训练 DPM 中采样非常耗时,因此加速 DPM 的采样变得越来越重要。尽管在快速采样器设计方面取得了最新进展,但在许多偏好更少步数(例如 <10)的应用场景中,现有方法仍无法生成令人满意的图像。在本文中,我们开发了一种统一校正器(UniC),它可以应用于任何现有的 DPM 采样器之后,在不进行额外模型评估的情况下提高精度阶数,并作为副产品推导出了一个支持任意阶数的统一预测器(UniP)。结合 UniP 和 UniC,我们提出了一个用于 DPM 快速采样的统一预测器-校正器框架,称为 UniPC,它对任意阶数都具有统一的解析形式,并能显著地提高采样质量,尤其是在极少步数的情况下。我们通过大量实验评估了我们的方法,包括使用像素空间和潜在空间 DPM 的无条件采样和有条件采样。我们的 UniPC 仅需 10 次函数评估,即可在 CIFAR10(无条件)上实现 3.87 FID,在 ImageNet 256×256(有条件)上实现 7.51 FID。代码可在 this https URL 获取。
提示
建议将 solver_order 设置为 2 用于引导采样,将 solver_order=3 用于无条件采样。
支持来自 Imagen 的动态阈值处理。对于像素空间扩散模型,您可以同时设置 predict_x0=True 和 thresholding=True 以使用动态阈值处理。此阈值处理方法不适用于 Stable Diffusion 等潜在空间扩散模型。
UniPCMultistepScheduler
class diffusers.UniPCMultistepScheduler
< source >( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: str = 'linear'trained_betas: numpy.ndarray | list[float] | None = Nonesolver_order: int = 2prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction', 'flow_prediction'] = 'epsilon'thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995sample_max_value: float = 1.0predict_x0: bool = Truesolver_type: typing.Literal['bh1', 'bh2'] = 'bh2'lower_order_final: bool = Truedisable_corrector: list[int] = []solver_p: SchedulerMixin = Noneuse_karras_sigmas: bool = Falseuse_exponential_sigmas: bool = Falseuse_beta_sigmas: bool = Falseuse_flow_sigmas: bool = Falseflow_shift: float = 1.0timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'linspace'steps_offset: int = 0final_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero'rescale_betas_zero_snr: bool = Falseuse_dynamic_shifting: bool = Falsetime_shift_type: typing.Literal['exponential'] = 'exponential'sigma_min: float | None = Nonesigma_max: bool | None = Noneshift_terminal: bool | None = None )
参数
- num_train_timesteps (
int,默认为 1000) — 用于训练模型的扩散步数。 - beta_start (
float,默认为 0.0001) — 推理的起始beta值。 - beta_end (
float,默认为 0.02) — 最终的beta值。 - beta_schedule(
"linear"、"scaled_linear"或"squaredcos_cap_v2",默认为"linear")—— beta 调度,即将 beta 范围映射为用于模型步进的一系列 beta 值。可选linear、scaled_linear或squaredcos_cap_v2。 - trained_betas (
np.ndarray, 可选) — 直接传入 betas 数组到构造函数中,以绕过beta_start和beta_end。 - solver_order (
int,默认为2) — UniPC 的阶数,可以是任意正整数。由于 UniC 的存在,有效精度阶数为solver_order + 1。 建议在有引导采样时使用solver_order=2,在无引导采样时使用solver_order=3。 - prediction_type (
"epsilon","sample","v_prediction", or"flow_prediction", defaults to"epsilon", optional) — Prediction type of the scheduler function; can beepsilon(predicts the noise of the diffusion process),sample(directly predicts the noisy sample),v_prediction(see section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper), orflow_prediction`. - thresholding (
bool,默认为False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。 - dynamic_thresholding_ratio (
float,默认为 0.995) — 动态阈值化方法的比例。仅在thresholding=True时有效。 - sample_max_value (
float,默认为 1.0) — 动态阈值化的阈值。仅在thresholding=True且predict_x0=True时有效。 - predict_x0 (
bool,默认为True) — 是否对预测的 x0 使用更新算法。 - solver_type(
"bh1"或"bh2",默认为"bh2")— UniPC 的求解器类型。当步数 < 10 时,建议对无条件采样使用bh1,否则使用bh2。 - lower_order_final (
bool,默认值为True) — 是否在最后几步使用低阶求解器。仅适用于少于 15 个推理步数的情况。这可以稳定 DPMSolver 在少于 15 步时的采样,尤其是在步数小于等于 10 时。 - disable_corrector (
list,默认值为[]) — 决定在哪些步骤禁用校正器,以缓解epsilon_theta(x_t, c)与epsilon_theta(x_t^c, c)之间的错位,这种错位可能会影响较大 guidance scale 下的收敛。通常在前几步中禁用校正器。 - solver_p (
SchedulerMixin,默认值为None) — 如果指定了其他调度器,算法将变为solver_p + UniC。 - use_karras_sigmas (
bool,可选,默认为False) — 是否在采样过程的噪声调度中使用 Karras sigmas 来确定步长。如果为True,则 sigmas 将根据噪声水平序列 {σi} 确定。 - use_exponential_sigmas (
bool,可选,默认为False) — 在采样过程中,噪声调度的步长是否使用指数 sigmas。 - use_beta_sigmas (
bool, optional, defaults toFalse) — Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to Beta Sampling is All You Need for more information. - use_flow_sigmas (
bool,可选,默认为False) — 在采样过程中,是否在噪声调度中使用 flow sigmas 作为步长。 - timestep_spacing(
"linspace"、"leading"或"trailing",默认为"linspace") — 时间步的缩放方式。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。 - steps_offset (
int,默认为 0) — 添加到推理步数中的偏移量,某些模型系列需要此参数。 - final_sigmas_type(
"zero"或"sigma_min",默认为"zero")— 采样过程中噪声调度的最终sigma值。如果为"sigma_min",则最终 sigma 与训练调度中的最后一个 sigma 相同。如果为zero,则最终 sigma 设为 0。 - rescale_betas_zero_snr (
bool,默认为False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与--offset_noise大致相关。
UniPCMultistepScheduler 是一个无需训练的框架,专为扩散模型的高效采样而设计。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。
convert_model_output
< source >( model_output: Tensor*argssample: Tensor = None**kwargs ) → torch.Tensor
将模型输出转换为 UniPC 算法所需的对应类型。
index_for_timestep
< source >( timestep: typing.Union[int, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int
查找给定时间步在调度器中的索引。
multistep_uni_c_bh_update
< source >( this_model_output: Tensor*argslast_sample: Tensor = Nonethis_sample: Tensor = Noneorder: int = None**kwargs ) → torch.Tensor
UniC (B(h) 版本) 的一步。
multistep_uni_p_bh_update
< source >( model_output: Tensor*argssample: Tensor = Noneorder: int = None**kwargs ) → torch.Tensor
UniP(B(h) 版本)的单步。或者,如果指定了,则使用 self.solver_p。
scale_model_input
< source >( sample: Tensor*args**kwargs ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
设置调度器的起始索引。此函数应在推理之前从流水线中运行。
set_timesteps
< source >( num_inference_steps: int | None = Nonedevice: typing.Union[str, torch.device] = Nonesigmas: list[float] | None = Nonemu: bool | None = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( model_output: Tensortimestep: typing.Union[int, torch.Tensor]sample: Tensorreturn_dict: bool = True ) → SchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int或torch.Tensor) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - return_dict (
bool,默认为True) — 是否返回 SchedulerOutput 或tuple。
返回值
SchedulerOutput 或 tuple
If return_dict is True, SchedulerOutput is returned, otherwise a
tuple is returned where the first element is the sample tensor.
通过逆转 SDE 来预测上一步时间步的样本。此函数使用多步 UniPC 传播样本。
stretch_shift_to_terminal
< source >( t: Tensor ) → torch.Tensor
拉伸并平移时间步调度,以确保其在配置的 shift_terminal 配置值处终止。
time_shift
< source >( mu: floatsigma: floatt: Tensor ) → torch.Tensor
对 sigmas 应用时间偏移。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source >( prev_sample: Tensor )
调度器 step 函数输出的基类。