Diffusers 文档
DPMSolverMultistepInverse
并获取增强的文档体验
开始使用
DPMSolverMultistepInverse
DPMSolverMultistepInverse
是来自 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 by Cheng Lu, Yuhao Zhou, Fan Bao, Jianfei Chen, Chongxuan Li, and Jun Zhu. 的反向调度器。
该实现主要基于 Null-text Inversion for Editing Real Images using Guided Diffusion Models 的 DDIM 反演定义,以及来自 Xiang-cd/DiffEdit-stable-diffusion 的 DiffEdit
潜在反演的 notebook 实现。
提示
支持来自 Imagen 的动态阈值处理,对于像素空间扩散模型,您可以设置 algorithm_type="dpmsolver++"
和 thresholding=True
来使用动态阈值处理。此阈值处理方法不适用于潜在空间扩散模型,例如 Stable Diffusion。
DPMSolverMultistepInverseScheduler
class diffusers.DPMSolverMultistepInverseScheduler
< 源代码 >( 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_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None timestep_spacing: str = 'linspace' steps_offset: int = 0 )
参数
- 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++ 论文中的算法。 建议将dpmsolver++
或sde-dpmsolver++
与solver_order=2
一起用于引导采样,例如在 Stable Diffusion 中。 - solver_type (
str
, 默认为midpoint
) — 二阶求解器的求解器类型; 可以是midpoint
或heun
。 求解器类型略微影响样本质量,特别是对于少量步骤。 建议使用midpoint
求解器。 - lower_order_final (
bool
, 默认为True
) — 是否在最后步骤中使用低阶求解器。 仅对 < 15 个推理步骤有效。 这可以稳定 DPMSolver 在 < 15 步时的采样,特别是对于 <= 10 步。 - euler_at_final (
bool
, 默认为False
) — 是否在最后一步中使用欧拉方法。 这是数值稳定性和细节丰富度之间的权衡。 这可以稳定小步数推理的 DPMSolver 的 SDE 变体的采样,但有时可能导致模糊。 - use_karras_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用 Karras sigma 值。 如果为True
,则根据噪声水平序列 {σi} 确定 sigma 值。 - use_exponential_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用指数 sigma 值。 - use_beta_sigmas (
bool
, 可选, 默认为False
) — 是否在采样过程中对噪声时间表中的步长使用 beta sigma 值。 有关更多信息,请参阅 Beta Sampling is All You Need。 - 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) — 推理步骤的偏移量,某些模型系列需要此偏移量。
DPMSolverMultistepInverseScheduler
是 DPMSolverMultistepScheduler 的反向调度器。
此模型继承自 SchedulerMixin 和 ConfigMixin。 查看超类文档,了解库为所有调度器实现的通用方法,例如加载和保存。
convert_model_output
< source >( model_output: Tensor *args sample: Tensor = None **kwargs ) → torch.Tensor
将模型输出转换为 DPMSolver/DPMSolver++ 算法所需的相应类型。 DPM-Solver 旨在离散化噪声预测模型的积分,而 DPM-Solver++ 旨在离散化数据预测模型的积分。
算法和模型类型是解耦的。 您可以将 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
二阶多步 DPM 求解器的单步更新。
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
三阶多步 DPM 求解器的单步更新。
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 )
设置用于扩散链的离散时间步(在推理之前运行)。
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
生成噪声的方法。对于诸如CycleDiffusion
等方法很有用。 - return_dict (
bool
) — 是否返回 SchedulerOutput 或tuple
。
返回
SchedulerOutput 或 tuple
如果 return_dict 为 True
,则返回 SchedulerOutput,否则返回一个元组,其中第一个元素是样本张量。
通过反转 SDE 来预测前一个时间步的样本。此函数使用多步 DPMSolver 传播样本。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source >( prev_sample: Tensor )
调度器的 step
函数输出的基类。