Diffusers 文档
DPMSolverMultistepScheduler
并获得增强的文档体验
开始使用
DPMSolverMultistepScheduler
DPMSolverMultistepScheduler
是一个多步调度器,来自 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,作者为 Cheng Lu, Yuhao Zhou, Fan Bao, Jianfei Chen, Chongxuan Li, 和 Jun Zhu。
DPMSolver(以及改进版本 DPMSolver++)是用于扩散 ODE 的快速专用高阶求解器,具有收敛阶数保证。 经验表明,仅需 20 步的 DPMSolver 采样即可生成高质量样本,即使在 10 步中也能生成相当不错的样本。
提示
建议将 solver_order
设置为 2 用于引导采样,将 solver_order=3
用于无条件采样。
支持来自 Imagen 的动态阈值,对于像素空间扩散模型,您可以同时设置 algorithm_type="dpmsolver++"
和 thresholding=True
以使用动态阈值。 此阈值方法不适用于潜在空间扩散模型,例如 Stable Diffusion。
也支持 DPMSolver 和 DPM-Solver++ 的 SDE 变体,但仅适用于一阶和二阶求解器。 这是用于反向扩散 SDE 的快速 SDE 求解器。 建议使用二阶 sde-dpmsolver++
。
DPMSolverMultistepScheduler
class diffusers.DPMSolverMultistepScheduler
< source >( 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 algorithm_type: str = 'dpmsolver++' solver_type: str = 'midpoint' lower_order_final: bool = True euler_at_final: bool = False use_karras_sigmas: typing.Optional[bool] = False use_exponential_sigmas: typing.Optional[bool] = False use_beta_sigmas: typing.Optional[bool] = False use_lu_lambdas: typing.Optional[bool] = False use_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 final_sigmas_type: typing.Optional[str] = 'zero' lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None timestep_spacing: str = 'linspace' steps_offset: int = 0 rescale_betas_zero_snr: bool = False )
参数
- num_train_timesteps (
int
, 默认为 1000) — 训练模型的扩散步数。 - beta_start (
float
, 默认为 0.0001) — 推理的起始beta
值。 - beta_end (
float
, 默认为 0.02) — 最终的beta
值。 - beta_schedule (
str
, 默认为"linear"
) — beta 时间表,从 beta 范围到用于步进模型的 beta 序列的映射。 从linear
、scaled_linear
或squaredcos_cap_v2
中选择。 - trained_betas (
np.ndarray
, 可选) — 直接将 beta 数组传递给构造函数以绕过beta_start
和beta_end
。 - solver_order (
int
, 默认为 2) — DPMSolver 阶数,可以是1
或2
或3
。 建议有引导采样使用solver_order=2
,无条件采样使用solver_order=3
。 - prediction_type (
str
, 默认为epsilon
, 可选) — 调度器函数的预测类型;可以是epsilon
(预测扩散过程的噪声)、sample
(直接预测噪声样本)或v_prediction
(参见 Imagen Video 论文的第 2.4 节)。 - 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
、dpmsolver++
、sde-dpmsolver
或sde-dpmsolver++
。dpmsolver
类型实现了 DPMSolver 论文中的算法,dpmsolver++
类型实现了 DPMSolver++ 论文中的算法。 建议对于像 Stable Diffusion 中的引导采样使用dpmsolver++
或sde-dpmsolver++
以及solver_order=2
。 - solver_type (
str
, 默认为midpoint
) — 二阶求解器的求解器类型;可以是midpoint
或heun
。 求解器类型略微影响样本质量,尤其是在步数较少的情况下。 建议使用midpoint
求解器。 - lower_order_final (
bool
, 默认为True
) — 是否在最后步骤中使用低阶求解器。 仅对 < 15 个推理步骤有效。 这可以稳定步数 < 15 的 DPMSolver 采样,特别是对于步数 <= 10 的情况。 - euler_at_final (
bool
, 默认为False
) — 是否在最后一步中使用欧拉方法。 这是数值稳定性和细节丰富度之间的权衡。 这可以稳定小步数推理步骤的 DPMSolver SDE 变体的采样,但有时可能导致模糊。 - use_karras_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用 Karras sigmas。 如果为True
,则根据噪声水平序列 {σi} 确定 sigmas。 - use_exponential_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用指数 sigmas。 - use_beta_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用 beta sigmas。 有关更多信息,请参阅 Beta Sampling is All You Need。 - use_lu_lambdas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中使用 Lu 的 DPM-Solver 提出的均匀 logSNR 作为步长。 如果为True
,则根据lambda(t)
序列确定 sigmas 和时间步长。 - final_sigmas_type (
str
, 默认为"zero"
) — 采样过程中噪声时间表的最终sigma
值。 如果为"sigma_min"
,则最终 sigma 与训练时间表中的最后一个 sigma 相同。 如果为zero
,则最终 sigma 设置为 0。 - lambda_min_clipped (
float
, 默认为-inf
) — 用于数值稳定性的lambda(t)
最小值的裁剪阈值。 这对于余弦 (squaredcos_cap_v2
) 噪声时间表至关重要。 - variance_type (
str
, 可选) — 对于预测方差的扩散模型,设置为“learned”或“learned_range”。 如果设置,则模型的输出包含预测的高斯方差。 - timestep_spacing (
str
, 默认为"linspace"
) — 时间步长应缩放的方式。 有关更多信息,请参阅 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。 - steps_offset (
int
, 默认为 0) — 添加到推理步骤的偏移量,某些模型族需要此偏移量。 - rescale_betas_zero_snr (
bool
, 默认为False
) — 是否重新缩放 betas 以使其具有零终端 SNR。 这使模型能够生成非常明亮和黑暗的样本,而不是将其限制为具有中等亮度的样本。 与--offset_noise
略有相关。
DPMSolverMultistepScheduler
是用于扩散 ODE 的快速专用高阶求解器。
此模型继承自 SchedulerMixin 和 ConfigMixin。 查看超类文档以获取库为所有调度器(如加载和保存)实现的通用方法。
convert_model_output
< source >( model_output: Tensor *args sample: Tensor = None **kwargs ) → torch.Tensor
将模型输出转换为 DPMSolver/DPMSolver++ 算法所需的相应类型。 DPMSolver 旨在离散化噪声预测模型的积分,而 DPMSolver++ 旨在离散化数据预测模型的积分。
算法和模型类型是解耦的。 您可以将 DPMSolver 或 DPMSolver++ 用于噪声预测和数据预测模型。
dpm_solver_first_order_update
< source >( model_output: Tensor *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
一阶 DPMSolver(等效于 DDIM)的一个步骤。
multistep_dpm_solver_second_order_update
< source >( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
二阶多步 DPMSolver 的一个步骤。
multistep_dpm_solver_third_order_update
< source >( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
三阶多步 DPMSolver 的一个步骤。
scale_model_input
< source >( sample: Tensor *args **kwargs ) → torch.Tensor
确保与需要根据当前时间步缩放去噪模型输入的调度器具有互换性。
设置调度器的起始索引。 此函数应在推理之前从管道运行。
set_timesteps
< source >( num_inference_steps: int = None device: typing.Union[str, torch.device] = None timesteps: typing.Optional[typing.List[int]] = None )
设置用于扩散链的离散时间步(在推理之前运行)。
step
< source >( model_output: Tensor timestep: typing.Union[int, torch.Tensor] sample: Tensor generator = None variance_noise: typing.Optional[torch.Tensor] = None return_dict: bool = True ) → SchedulerOutput 或 tuple
参数
- model_output (
torch.Tensor
) — 来自学习到的扩散模型的直接输出。 - timestep (
int
) — 扩散链中当前的离散时间步长。 - sample (
torch.Tensor
) — 由扩散过程创建的当前样本实例。 - generator (
torch.Generator
, 可选) — 随机数生成器。 - variance_noise (
torch.Tensor
) — 通过直接为方差本身提供噪声,来替代使用generator
生成噪声的方法。 对于诸如LEdits++
等方法很有用。 - return_dict (
bool
) — 是否返回 SchedulerOutput 或tuple
。
返回值
SchedulerOutput 或 tuple
如果 return_dict 为 True
,则返回 SchedulerOutput,否则返回一个元组,其中第一个元素是 sample tensor。
通过反转 SDE,从先前的 timestep 预测样本。此函数使用多步 DPMSolver 传播样本。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source >( prev_sample: Tensor )
调度器的 step
函数输出的基类。