Diffusers 文档

UniPCMultistepScheduler

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

UniPCMultistepScheduler

UniPCMultistepScheduler 是一个免训练框架,专为扩散模型的快速采样而设计。它在 UniPC: A Unified Predictor-Corrector Framework for Fast Sampling of Diffusion Models 中被提出,作者是 Wenliang Zhao, Lujia Bai, Yongming Rao, Jie Zhou, Jiwen Lu。

它由一个校正器 (UniC) 和一个预测器 (UniP) 组成,它们共享统一的解析形式并支持任意阶数。UniPC 在设计上是模型无关的,支持在无条件/有条件采样下对像素空间/潜在空间 DPM 进行操作。它也可以应用于噪声预测和数据预测模型。校正器 UniC 也可以在任何现成的求解器之后应用,以提高精度阶数。

论文摘要如下:

扩散概率模型 (DPM) 在高分辨率图像合成方面展示了非常有希望的能力。然而,从预训练的 DPM 中采样是耗时的,因为需要多次评估去噪网络,这使得加速 DPM 的采样变得越来越重要。尽管在设计快速采样器方面取得了最新进展,但在许多需要较少步骤(例如,<10 步)的应用中,现有方法仍然无法生成令人满意的图像。在本文中,我们开发了一个统一的校正器 (UniC),它可以应用于任何现有的 DPM 采样器之后,以在不进行额外模型评估的情况下提高精度阶数,并导出一个支持任意阶数的统一预测器 (UniP) 作为副产品。结合 UniP 和 UniC,我们提出了一个名为 UniPC 的统一预测器-校正器框架,用于 DPM 的快速采样,它对于任何阶数都具有统一的解析形式,并且可以显着提高采样质量,尤其是在极少的步骤中。我们通过广泛的实验评估了我们的方法,包括使用像素空间和潜在空间 DPM 进行的无条件和有条件采样。我们的 UniPC 在 CIFAR10(无条件)上仅用 10 次函数评估即可达到 3.87 FID,在 ImageNet 256×256(有条件)上达到 7.51 FID。代码可在 此 HTTPS URL 获取。

提示

建议为引导采样将 solver_order 设置为 2,为无条件采样将 solver_order=3

支持来自 Imagen 的动态阈值,对于像素空间扩散模型,您可以同时设置 predict_x0=Truethresholding=True 以使用动态阈值。此阈值方法不适用于潜在空间扩散模型,例如 Stable Diffusion。

UniPCMultistepScheduler

class diffusers.UniPCMultistepScheduler

< >

( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: typing.Union[numpy.ndarray, typing.List[float], NoneType] = None solver_order: int = 2 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 predict_x0: bool = True solver_type: str = 'bh2' lower_order_final: bool = True disable_corrector: typing.List[int] = [] solver_p: SchedulerMixin = None use_karras_sigmas: typing.Optional[bool] = False use_exponential_sigmas: typing.Optional[bool] = False use_beta_sigmas: typing.Optional[bool] = False use_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 timestep_spacing: str = 'linspace' steps_offset: int = 0 final_sigmas_type: typing.Optional[str] = 'zero' rescale_betas_zero_snr: bool = False )

参数

  • num_train_timesteps (int, 默认为 1000) — 训练模型的扩散步数。
  • beta_start (float, 默认为 0.0001) — 推理的起始 beta 值。
  • beta_end (float, defaults to 0.02) — 最终的 beta 值。
  • beta_schedule (str, defaults to "linear") — beta 计划,将 beta 范围映射到模型步进的 beta 序列。可从 linearscaled_linearsquaredcos_cap_v2 中选择。
  • trained_betas (np.ndarray, 可选) — 直接将 beta 数组传递给构造函数以绕过 beta_startbeta_end
  • solver_order (int, default 2) — UniPC 阶数,可以是任何正整数。由于 UniC,有效精度阶数为 solver_order + 1。建议在引导采样中使用 solver_order=2,在无条件采样中使用 solver_order=3
  • prediction_type (str, defaults to epsilon, 可选) — 调度器函数的预测类型;可以是 epsilon (预测扩散过程的噪声), sample (直接预测噪声样本) 或 v_prediction (参见 Imagen Video 论文的第 2.4 节).
  • thresholding (bool, defaults to False) — 是否使用 “动态阈值” 方法。这不适用于潜在空间扩散模型,例如 Stable Diffusion。
  • dynamic_thresholding_ratio (float, defaults to 0.995) — 动态阈值方法的比率。仅当 thresholding=True 时有效。
  • sample_max_value (float, defaults to 1.0) — 动态阈值的阈值。仅当 thresholding=Truepredict_x0=True 时有效。
  • predict_x0 (bool, defaults to True) — 是否在预测的 x0 上使用更新算法。
  • solver_type (str, default bh2) — UniPC 的求解器类型。建议当步数 < 10 时在无条件采样中使用 bh1,否则使用 bh2
  • lower_order_final (bool, default True) — 是否在最后步骤中使用低阶求解器。仅对 < 15 推理步骤有效。这可以稳定 DPMSolver 在步数 < 15 时的采样,特别是对于步数 <= 10 的情况。
  • disable_corrector (list, default []) — 决定在哪个步骤禁用校正器,以减轻 epsilon_theta(x_t, c)epsilon_theta(x_t^c, c) 之间的错位,这可能会影响大 guidance scale 的收敛性。校正器通常在最初的几个步骤中被禁用。
  • solver_p (SchedulerMixin, default None) — 任何其他调度器,如果指定,则算法变为 solver_p + UniC
  • use_karras_sigmas (bool, 可选, defaults to False) — 是否在采样过程中对噪声计划中的步长使用 Karras sigmas。如果为 True,则根据噪声级别序列 {σi} 确定 sigma。
  • use_exponential_sigmas (bool, 可选, defaults to False) — 是否在采样过程中对噪声计划中的步长使用指数 sigmas。
  • use_beta_sigmas (bool, 可选, defaults to False) — 是否在采样过程中对噪声计划中的步长使用 beta sigmas。有关更多信息,请参阅 Beta Sampling is All You Need
  • timestep_spacing (str, defaults to "linspace") — 应该如何缩放时间步长。有关更多信息,请参阅 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。
  • steps_offset (int, defaults to 0) — 添加到推理步骤的偏移量,某些模型系列需要这样做。
  • final_sigmas_type (str, defaults to "zero") — 采样过程中噪声计划的最终 sigma 值。如果为 "sigma_min",则最终 sigma 与训练计划中的最后一个 sigma 相同。如果为 zero,则最终 sigma 设置为 0。
  • rescale_betas_zero_snr (bool, defaults to False) — 是否重新缩放 beta 以使其具有零终端 SNR。这使模型能够生成非常亮和非常暗的样本,而不是将其限制为中等亮度的样本。与 --offset_noise 松散相关。

UniPCMultistepScheduler 是一个免训练框架,专为扩散模型的快速采样而设计。

此模型继承自 SchedulerMixinConfigMixin。有关库为所有调度器(例如加载和保存)实现的通用方法,请查看超类文档。

convert_model_output

< >

( model_output: Tensor *args sample: Tensor = None **kwargs ) torch.Tensor

参数

  • model_output (torch.Tensor) — 来自学习的扩散模型的直接输出。
  • timestep (int) — 扩散链中当前的离散时间步长。
  • sample (torch.Tensor) — 由扩散过程创建的样本的当前实例。

Returns

torch.Tensor

转换后的模型输出。

将模型输出转换为 UniPC 算法所需的相应类型。

multistep_uni_c_bh_update

< >

( this_model_output: Tensor *args last_sample: Tensor = None this_sample: Tensor = None order: int = None **kwargs ) torch.Tensor

参数

  • this_model_output (torch.Tensor) — x_t 时的模型输出。
  • this_timestep (int) — 当前时间步 t
  • last_sample (torch.Tensor) — 上一个预测器 x_{t-1} 之前的生成样本。
  • this_sample (torch.Tensor) — 上一个预测器 x_{t} 之后的生成样本。
  • order (int) — 此步骤中 UniC-p 的 p 值。有效精度阶数应为 order + 1

Returns

torch.Tensor

当前时间步校正后的样本张量。

UniC(B(h) 版本)的单步更新。

multistep_uni_p_bh_update

< >

( model_output: Tensor *args sample: Tensor = None order: int = None **kwargs ) torch.Tensor

参数

  • model_output (torch.Tensor) — 来自学习的扩散模型在当前时间步的直接输出。
  • prev_timestep (int) — 扩散链中的上一个离散时间步。
  • sample (torch.Tensor) — 由扩散过程创建的样本的当前实例。
  • order (int) — 此时间步中 UniP 的阶数(对应于 UniPC-p 中的 p)。

Returns

torch.Tensor

前一个时间步的样本张量。

UniP(B(h) 版本)的单步更新。或者,如果指定了 self.solver_p,则使用它。

scale_model_input

< >

( sample: Tensor *args **kwargs ) torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本。

Returns

torch.Tensor

缩放后的输入样本。

确保与需要根据当前时间步缩放去噪模型输入的调度器具有互换性。

set_begin_index

< >

( begin_index: int = 0 )

参数

  • begin_index (int) — 调度器的起始索引。

设置调度器的起始索引。此函数应在推理之前从 pipeline 运行。

set_timesteps

< >

( num_inference_steps: int device: typing.Union[str, torch.device] = None )

参数

  • num_inference_steps (int) — 使用预训练模型生成样本时使用的扩散步骤数。
  • device (strtorch.device, 可选) — 时间步应移动到的设备。如果为 None,则时间步不会移动。

设置用于扩散链的离散时间步(在推理之前运行)。

step

< >

( model_output: Tensor timestep: typing.Union[int, torch.Tensor] sample: Tensor return_dict: bool = True ) SchedulerOutputtuple

参数

  • model_output (torch.Tensor) — 来自学习的扩散模型的直接输出。
  • timestep (int) — 扩散链中当前的离散时间步。
  • sample (torch.Tensor) — 由扩散过程创建的样本的当前实例。
  • return_dict (bool) — 是否返回 SchedulerOutputtuple

Returns

SchedulerOutputtuple

如果 return_dict 为 True,则返回 SchedulerOutput,否则返回一个元组,其中第一个元素是样本张量。

通过反转 SDE 来预测上一个时间步的样本。此函数使用多步 UniPC 传播样本。

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< >

( prev_sample: Tensor )

参数

  • prev_sample (形状为 (batch_size, num_channels, height, width)torch.Tensor,适用于图像) — 计算出的前一个时间步的样本 (x_{t-1})prev_sample 应在去噪循环中用作下一个模型输入。

调度器的 step 函数输出的基类。

< > 在 GitHub 上更新