Diffusers documentation

EDMDPMSolverMultistepScheduler

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

EDMDPMSolverMultistepScheduler

EDMDPMSolverMultistepScheduler 是 DPMSolverMultistepScheduler 的 Karras 形式,后者是一种多步调度器,源自 Cheng Lu、Yuhao Zhou、Fan Bao、Jianfei Chen、Chongxuan Li 和 Jun Zhu 的论文 DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 Steps 和 DPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models。

DPMSolver(以及改进版本 DPMSolver++)是一种专为扩散 ODE 设计的高阶快速求解器,具有收敛阶数保证。从经验上看,DPMSolver 仅用 20 步采样即可生成高质量样本,甚至在 10 步内也能生成相当不错的样本。

EDMDPMSolverMultistepScheduler

class diffusers.EDMDPMSolverMultistepScheduler

< >

( sigma_min: float = 0.002sigma_max: float = 80.0sigma_data: float = 0.5sigma_schedule: typing.Literal['karras', 'exponential'] = 'karras'num_train_timesteps: int = 1000prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction'] = 'epsilon'rho: float = 7.0solver_order: int = 2thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995sample_max_value: float = 1.0algorithm_type: typing.Literal['dpmsolver++', 'sde-dpmsolver++'] = 'dpmsolver++'solver_type: typing.Literal['midpoint', 'heun'] = 'midpoint'lower_order_final: bool = Trueeuler_at_final: bool = Falsefinal_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero' )

参数

  • sigma_min (float,可选,默认为 0.002) — sigma 调度中的最小噪声幅度。EDM 论文 [1] 中将其设置为 0.002;合理的取值范围为 [0, 10]。
  • sigma_max (float,可选,默认值为 80.0) — sigma 调度中的最大噪声幅度。EDM 论文 [1] 中将其设置为 80.0;合理的取值范围为 [0.2, 80.0]。
  • sigma_data (float,可选,默认为 0.5) — 数据分布的标准差。在 EDM 论文 [1] 中,该值被设置为 0.5。
  • sigma_schedule (str,可选,默认为 karras) — 用于计算 sigmas 的 Sigma 调度。默认情况下,我们使用 EDM 论文中提出的调度 (https://huggingface.co/papers/2206.00364)。另一个可接受的值是 “exponential”。该指数 调度已在此模型中采用:https://huggingface.co/stabilityai/cosxl。
  • num_train_timesteps (int,默认为 1000) — 用于训练模型的扩散步数。
  • prediction_type (str, defaults to epsilon, optional) — Prediction type of the scheduler function; can be epsilon (predicts the noise of the diffusion process), sample (directly predicts the noisy sample) or v_prediction` (see section 2.4 of Imagen Video paper).
  • rho (float, 可选,默认值为 7.0) — Karras sigma 调度中的 rho 参数。在 EDM 论文 [1] 中,该值被设置为 7.0。
  • solver_order (int,默认为 2) — DPMSolver 的阶数,可以是 1、2 或 3。建议在有引导采样时使用 solver_order=2,在无引导采样时使用 solver_order=3。
  • thresholding (bool,默认为 False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。
  • dynamic_thresholding_ratio (float,默认为 0.995) — 动态阈值化方法的比例。仅在 thresholding=True 时有效。
  • sample_max_value (float,默认为 1.0) — 动态阈值化的阈值。仅在 thresholding=True 且 algorithm_type="dpmsolver++" 时有效。
  • algorithm_type (str,默认为 dpmsolver++) — 求解器的算法类型;可以是 dpmsolver++ 或 sde-dpmsolver++。dpmsolver++ 类型实现了 DPMSolver++ 论文中的算法。对于 Stable Diffusion 中的引导采样,建议使用 dpmsolver++ 或 sde-dpmsolver++ 并设置 solver_order=2。
  • solver_type (str,默认为 midpoint) — 二阶求解器的求解器类型;可以是 midpoint 或 heun。求解器类型会轻微影响 样本质量,尤其是在步数较少时。建议使用 midpoint 求解器。
  • lower_order_final (bool,默认为 True) — 是否在最后几步使用低阶求解器。仅适用于推理步数 < 15 的情况。这有助于稳定 DPMSolver 在步数 < 15 时的采样,尤其是步数 <= 10 时。
  • euler_at_final (bool,默认为 False) — 是否在最后一步使用欧拉法。这是在数值稳定性和细节丰富度之间的权衡。对于推理步数较少的 DPMSolver SDE 变体,这可以稳定采样过程,但有时可能会导致图像模糊。
  • final_sigmas_type (str,默认为 "zero") — 采样过程中噪声调度的最终 sigma 值。如果为 "sigma_min",则最终 sigma 与训练调度中的最后一个 sigma 相同。如果为 zero,则最终 sigma 设为 0。

按照 Karras 等人 2022 年 [1] 中提出的 EDM 公式,实现了 DPMSolverMultistepScheduler。EDMDPMSolverMultistepScheduler 是一个用于扩散 ODE 的快速专用高阶求解器。

[1] Karras, Tero, et al. “Elucidating the Design Space of Diffusion-Based Generative Models.” https://huggingface.co/papers/2206.00364

该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。

add_noise

< >

( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor

参数

  • original_samples (torch.Tensor) — 将添加噪声的原始样本。
  • noise (torch.Tensor) — 要添加到原始样本中的噪声张量。
  • timesteps (torch.Tensor) — 用于添加噪声的时间步,决定调度器中的噪声水平。

返回值

torch.Tensor

根据时间步调度对添加的噪声进行缩放后的含噪样本。

根据指定时间步的噪声调度,向原始样本添加噪声。

convert_model_output

< >

( model_output: Tensorsample: Tensor ) → torch.Tensor

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。

返回值

torch.Tensor

转换后的模型输出。

将模型输出转换为 DPMSolver/DPMSolver++ 算法所需的对应类型。DPM-Solver 旨在对噪声预测模型的积分进行离散化,而 DPM-Solver++ 则旨在对数据预测模型的积分进行离散化。

> 算法和模型类型是解耦的。对于噪声 > 预测模型和数据预测模型,都可以使用 DPMSolver 或 DPMSolver++。

dpm_solver_first_order_update

< >

( model_output: Tensorsample: Tensornoise: typing.Optional[torch.Tensor] = None ) → torch.Tensor

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • noise (torch.Tensor, 可选) — 要添加到原始样本上的噪声张量。

返回值

torch.Tensor

上一步时间步的样本张量。

一阶 DPMSolver 的一步(等效于 DDIM)。

index_for_timestep

< >

( timestep: typing.Union[int, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int

参数

  • timestep (int 或 torch.Tensor) — 用于查找索引的时间步。
  • schedule_timesteps (torch.Tensor,可选) — 要搜索的时间步调度。如果为 None,则使用 self.timesteps。

返回值

int

时间步在调度器中的索引。

查找给定时间步在调度器中的索引。

multistep_dpm_solver_second_order_update

< >

( model_output_list: typing.List[torch.Tensor]sample: Tensornoise: typing.Optional[torch.Tensor] = None ) → torch.Tensor

参数

  • model_output_list (list[torch.Tensor]) — 学习到的扩散模型在当前及后续时间步的直接输出。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • noise (torch.Tensor, 可选) — 要添加到原始样本上的噪声张量。

返回值

torch.Tensor

上一步时间步的样本张量。

二阶多步 DPMSolver 的单步。

multistep_dpm_solver_third_order_update

< >

( model_output_list: listsample: Tensor ) → torch.Tensor

参数

  • model_output_list (list[torch.Tensor]) — 学习到的扩散模型在当前及后续时间步的直接输出。
  • sample (torch.Tensor) — 扩散过程生成的当前样本实例。

返回值

torch.Tensor

上一步时间步的样本张量。

三阶多步 DPMSolver 的单步。

precondition_inputs

< >

( sample: Tensorsigma: typing.Union[float, torch.Tensor] ) → torch.Tensor

参数

  • sample (torch.Tensor) — 用于预处理的输入样本张量。
  • sigma(float 或 torch.Tensor)— 当前的 sigma(噪声水平)值。

返回值

torch.Tensor

缩放后的输入样本。

根据 EDM 公式对输入样本进行缩放,以对其进行预处理。

precondition_noise

< >

( sigma: typing.Union[float, torch.Tensor] ) → torch.Tensor

参数

  • sigma (float 或 torch.Tensor) — 用于预处理的 sigma(噪声水平)值。

返回值

torch.Tensor

计算得到的预条件噪声值,即 0.25 * log(sigma)。

通过对噪声水平应用对数变换来进行预处理。

precondition_outputs

< >

( sample: Tensormodel_output: Tensorsigma: typing.Union[float, torch.Tensor] ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本张量。
  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • sigma(float 或 torch.Tensor)— 当前的 sigma(噪声水平)值。

返回值

torch.Tensor

通过结合跳跃连接和输出缩放计算得到的去噪样本。

根据 EDM 公式对模型输出进行预处理。

scale_model_input

< >

( sample: Tensortimestep: typing.Union[float, torch.Tensor] ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本张量。
  • timestep (float 或 torch.Tensor) — 扩散链中的当前时间步。

返回值

torch.Tensor

经过缩放的输入样本。

将去噪模型输入进行缩放,以匹配 Euler 算法。确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器具有互换性。

set_begin_index

< >

( begin_index: int = 0 )

参数

  • begin_index (int,默认为 0) — 调度器的起始索引。

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

set_timesteps

< >

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

参数

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

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

步骤

< >

( model_output: Tensortimestep: typing.Union[int, torch.Tensor]sample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → SchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • timestep (int) — 扩散链中的当前离散时间步。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • generator (torch.Generator,可选) — 随机数生成器。
  • return_dict (bool) — 是否返回 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 来预测上一步时间步的样本。此函数使用多步 DPMSolver 传播样本。

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 函数输出的基类。

Update on GitHub