Diffusers documentation
DPMSolverMultistepScheduler
DPMSolverMultistepScheduler
DPMSolverMultistepScheduler 是一个多步调度器,源自 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 步内也能生成相当不错的样本。
提示
建议将 solver_order 设置为 2 用于引导采样,将 solver_order=3 用于无条件采样。
支持来自 Imagen 的动态阈值处理。对于像素空间扩散模型,您可以同时设置 algorithm_type="dpmsolver++" 和 thresholding=True 来使用动态阈值处理。此阈值处理方法不适用于 Stable Diffusion 等潜在空间扩散模型。
DPMSolver 和 DPM-Solver++ 的 SDE 变体也得到支持,但仅适用于一阶和二阶求解器。这是一种用于逆向扩散 SDE 的快速 SDE 求解器。建议使用二阶的 sde-dpmsolver++。
DPMSolverMultistepScheduler
类 diffusers.DPMSolverMultistepScheduler
< 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.0algorithm_type: typing.Literal['dpmsolver', 'dpmsolver++', 'sde-dpmsolver', 'sde-dpmsolver++'] = 'dpmsolver++'solver_type: typing.Literal['midpoint', 'heun'] = 'midpoint'lower_order_final: bool = Trueeuler_at_final: bool = Falseuse_karras_sigmas: bool = Falseuse_exponential_sigmas: bool = Falseuse_beta_sigmas: bool = Falseuse_lu_lambdas: bool = Falseuse_flow_sigmas: bool = Falseflow_shift: float = 1.0final_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero'lambda_min_clipped: float = -infvariance_type: typing.Optional[typing.Literal['learned', 'learned_range']] = Nonetimestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'linspace'steps_offset: int = 0rescale_betas_zero_snr: bool = Falseuse_dynamic_shifting: bool = Falsetime_shift_type: typing.Literal['exponential'] = 'exponential' )
参数
- 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 值。 - trained_betas (
np.ndarray, 可选) — 直接传入 betas 数组到构造函数中,以绕过beta_start和beta_end。 - solver_order (
int,默认为 2) — DPMSolver 的阶数,可以是1、2或3。建议在有引导采样时使用solver_order=2,在无引导采样时使用solver_order=3。 - prediction_type(
"epsilon"、"sample"、"v_prediction"或"flow_prediction",默认为"epsilon")— 调度器函数的预测类型。epsilon预测扩散过程的噪声,sample直接预测含噪样本,v_prediction预测速度(参见 Imagen Video 论文的 2.4 节),flow_prediction预测流。 - 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(
"dpmsolver"、"dpmsolver++"、"sde-dpmsolver"或"sde-dpmsolver++",默认为"dpmsolver++")— 求解器的算法类型。dpmsolver类型实现了 DPMSolver 论文中的算法,而dpmsolver++类型实现了 DPMSolver++ 论文中的算法。对于 Stable Diffusion 中的引导采样,建议使用dpmsolver++或sde-dpmsolver++并设置solver_order=2。 - solver_type(
"midpoint"或"heun",默认为"midpoint") — 二阶求解器的求解器类型。求解器类型会轻微影响样本质量,尤其是在步数较少时。建议使用midpoint求解器。 - lower_order_final (
bool,默认为True) — 是否在最后几步使用低阶求解器。仅适用于推理步数 < 15 的情况。这有助于稳定 DPMSolver 在步数 < 15 时的采样,尤其是步数 <= 10 时。 - euler_at_final (
bool,默认为False) — 是否在最后一步使用欧拉法。这是在数值稳定性和细节丰富度之间的权衡。对于推理步数较少的 DPMSolver SDE 变体,这可以稳定采样过程,但有时可能会导致图像模糊。 - 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_lu_lambdas (
bool,可选,默认为False) — 在采样过程中,是否使用 Lu 的 DPM-Solver 提出的 uniform-logSNR 步长来确定噪声调度。如果为True,则 sigmas 和时间步将根据lambda(t)序列确定。 - use_flow_sigmas (
bool,可选,默认为False) — 在采样过程中,是否在噪声调度中使用 flow sigmas 作为步长。 - flow_shift (
float,可选,默认为 1.0) — 用于 flow matching 的时间步调度的偏移值。 - final_sigmas_type(
"zero"或"sigma_min",可选,默认为"zero") — 采样过程中噪声调度的最终sigma值。如果为"sigma_min",则最终 sigma 与训练调度中的最后一个 sigma 相同。如果为"zero",则最终 sigma 设为 0。 - lambda_min_clipped (
float,默认为-inf) —lambda(t)最小值的裁剪阈值,用于保证数值稳定性。这对于余弦(squaredcos_cap_v2)噪声调度至关重要。 - variance_type(
"learned"或"learned_range",可选)— 对于预测方差的扩散模型,将其设置为"learned"或"learned_range"。如果已设置,模型的输出将包含预测的高斯方差。 - timestep_spacing(
"linspace"、"leading"或"trailing",默认为"linspace") — 时间步的缩放方式。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。 - steps_offset (
int,默认为 0) — 添加到推理步数中的偏移量,某些模型系列需要此参数。 - rescale_betas_zero_snr (
bool,默认为False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与--offset_noise大致相关。 - use_dynamic_shifting(
bool,默认为False)—— 是否对时间步调度使用动态偏移。 - time_shift_type (
"exponential",默认为"exponential") — 在使用动态移位时应用的时间移位类型。
DPMSolverMultistepScheduler 是一种专为扩散 ODE 设计的高效高阶求解器。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。
convert_model_output
< source >( model_output: Tensor*argssample: typing.Optional[torch.Tensor] = None**kwargs ) → torch.Tensor
将模型输出转换为 DPMSolver/DPMSolver++ 算法所需的对应类型。DPM-Solver 旨在对噪声预测模型的积分进行离散化,而 DPM-Solver++ 则旨在对数据预测模型的积分进行离散化。
> 算法和模型类型是解耦的。对于噪声 > 预测模型和数据预测模型,都可以使用 DPMSolver 或 DPMSolver++。
dpm_solver_first_order_update
< source >( model_output: Tensor*argssample: typing.Optional[torch.Tensor] = Nonenoise: typing.Optional[torch.Tensor] = None**kwargs ) → torch.Tensor
一阶 DPMSolver 的一步(等效于 DDIM)。
index_for_timestep
< source >( timestep: typing.Union[int, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int
查找给定时间步在调度器中的索引。
multistep_dpm_solver_second_order_update
< source >( model_output_list: list*argssample: typing.Optional[torch.Tensor] = Nonenoise: typing.Optional[torch.Tensor] = None**kwargs ) → torch.Tensor
二阶多步 DPMSolver 的单步。
multistep_dpm_solver_third_order_update
< source >( model_output_list: list*argssample: typing.Optional[torch.Tensor] = Nonenoise: 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 = Nonedevice: typing.Union[str, torch.device] = Nonemu: float | None = Nonetimesteps: list[int] | None = None )
参数
- num_inference_steps (
int, 可选) — 使用预训练模型生成样本时所用的扩散步数。 - device (
str或torch.device,可选) — 时间步应移动到的设备。如果为None,则不移动时间步。 - mu (
float, 可选) — 动态时间偏移的指数。需要use_dynamic_shifting且time_shift_type为"exponential";传入时,flow_shift将被设置为exp(mu)。 - timesteps (
list[int],可选) — 用于支持任意时间步调度的自定义时间步。如果为None,则会根据timestep_spacing属性生成时间步。如果传入了timesteps,则num_inference_steps和sigmas必须为None,并且timestep_spacing属性将被忽略。
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( model_output: Tensortimestep: typing.Union[int, torch.Tensor]sample: Tensorgenerator: typing.Optional[torch.Generator] = Nonevariance_noise: typing.Optional[torch.Tensor] = Nonereturn_dict: bool = True ) → SchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int或torch.Tensor) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - generator (
torch.Generator,可选) — 随机数生成器。 - variance_noise (
torch.Tensor, 可选) — 直接提供方差本身的噪声,作为使用generator生成噪声的替代方案。适用于LEdits++等方法。 - return_dict (
bool,默认为True) — 是否返回 SchedulerOutput 或tuple。
返回值
SchedulerOutput 或 tuple
如果 return_dict 为 True,则返回 SchedulerOutput,否则返回一个元组,其中第一个元素是样本张量。
通过反转 SDE 来预测上一步时间步的样本。此函数使用多步 DPMSolver 传播样本。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source >( prev_sample: Tensor )
调度器 step 函数输出的基类。