Diffusers documentation
DEISMultistepScheduler
DEISMultistepScheduler
扩散指数积分器采样器(DEIS)由 Qinsheng Zhang 和 Yongxin Chen 在 Fast Sampling of Diffusion Models with Exponential Integrator 中提出。DEISMultistepScheduler 是用于求解扩散常微分方程(ODE)的快速高阶求解器。
该实现修改了 DEIS 论文中原始线性 t 空间下的多项式拟合公式,改为在 log-rho 空间中进行拟合。这一修改使得指数多步更新能够采用闭式系数,而无需依赖数值求解器。
该论文的摘要如下:
过去几年,扩散模型~(DMs) 在生成建模任务中生成高保真样本方面取得了巨大成功。DM 的一个主要局限在于其众所周知的缓慢采样过程,通常需要数百到数千个时间离散化步骤才能达到所需的精度。我们的目标是开发一种快速采样方法,在大幅减少步数的同时保持高样本质量。为此,我们系统地分析了 DM 中的采样过程,并识别出影响样本质量的关键因素,其中离散化方法最为关键。通过仔细研究学习到的扩散过程,我们提出了 Diffusion Exponential Integrator Sampler~(DEIS)。它基于为离散化常微分方程 (ODEs) 而设计的指数积分器,并利用学习到的扩散过程的半线性结构来降低离散化误差。所提出的方法可应用于任何 DM,并能在少至 10 步内生成高保真样本。在我们的实验中,在单张 A6000 GPU 上生成 50k 张 CIFAR10 图像大约需要 3 分钟。此外,通过直接使用预训练的 DM,我们在评分函数评估次数~(NFE) 受限时(例如 4.17 FID 对应 10 NFEs,3.37 FID,以及仅 15 NFEs 时 9.74 IS)实现了最先进的采样性能。代码可在 this https URL 获取。
提示
建议将 solver_order 设置为 2 或 3,而 solver_order=1 则等同于 DDIMScheduler。
支持来自 Imagen 的动态阈值处理,对于像素空间扩散模型,您可以设置 thresholding=True 来使用动态阈值处理。
DEISMultistepScheduler
class diffusers.DEISMultistepScheduler
< source >( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: typing.Literal['linear', 'scaled_linear', 'squaredcos_cap_v2'] = '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['deis'] = 'deis'solver_type: typing.Literal['logrho'] = 'logrho'lower_order_final: bool = Trueuse_karras_sigmas: bool = Falseuse_exponential_sigmas: bool = Falseuse_beta_sigmas: bool = Falseuse_flow_sigmas: bool = Falseflow_shift: float = 1.0timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'linspace'steps_offset: int = 0use_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 值。可选linear、scaled_linear或squaredcos_cap_v2。 - trained_betas(
np.ndarray或list[float],可选)— 直接传入 betas 数组到构造函数中,以绕过beta_start和beta_end。 - solver_order (
int,默认为2) — DEIS 阶数,可以是1、2或3。建议在有引导采样时使用solver_order=2,在无引导采样时使用solver_order=3。 - prediction_type (
"epsilon","sample","v_prediction", or"flow_prediction", defaults to"epsilon") — Prediction type of the scheduler function; can beepsilon(predicts the noise of the diffusion process),sample(directly predicts the noisy sample),v_prediction(see section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper), orflow_prediction`. - thresholding (
bool,默认为False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。 - dynamic_thresholding_ratio (
float, 默认值为0.995) — 动态阈值化方法的比例。仅在thresholding=True时有效。 - sample_max_value (
float,默认为1.0) — 动态阈值化的阈值。仅在thresholding=True时有效。 - algorithm_type (
"deis",默认为"deis") — 求解器的算法类型。 - solver_type (
"logrho",默认为"logrho") — DEIS 的求解器类型。 - lower_order_final (
bool,默认为True) — 是否在最后几步使用低阶求解器。仅在推理步数小于 15 时有效。 - 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_flow_sigmas (
bool,可选,默认为False) — 在采样过程中,是否在噪声调度中使用 flow sigmas 作为步长。 - flow_shift (
float,可选,默认为1.0) — 基于流的模型的 flow shift 参数。 - timestep_spacing(
"linspace"、"leading"或"trailing",默认为"linspace") — 时间步的缩放方式。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。 - steps_offset(
int,默认为0)— 添加到推理步数中的偏移量,某些模型系列需要此参数。 - use_dynamic_shifting (
bool,默认为False) — 是否对噪声调度使用动态偏移。 - time_shift_type(
"exponential",默认为"exponential") — 要应用的时间偏移类型。
DEISMultistepScheduler 是一种用于求解扩散常微分方程(ODE)的快速高阶求解器。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。
convert_model_output
< source >( model_output: Tensor*argssample: Tensor = None**kwargs ) → torch.Tensor
将模型输出转换为 DEIS 算法所需的对应类型。
deis_first_order_update
< source >( model_output: Tensor*argssample: Tensor = None**kwargs ) → torch.Tensor
一阶 DEIS 的一步(等效于 DDIM)。
index_for_timestep
< source >( timestep: typing.Union[int, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int
查找给定时间步在调度器中的索引。
multistep_deis_second_order_update
< source >( model_output_list: list*argssample: Tensor = None**kwargs ) → torch.Tensor
二阶多步 DEIS 的一步。
multistep_deis_third_order_update
< source >( model_output_list: list*argssample: Tensor = None**kwargs ) → torch.Tensor
三阶多步 DEIS 的一步。
scale_model_input
< source >( sample: Tensor*args**kwargs ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
设置调度器的起始索引。此函数应在推理之前从流水线中运行。
set_timesteps
< source >( num_inference_steps: intdevice: typing.Union[str, torch.device] = Nonemu: float | None = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( model_output: Tensortimestep: typing.Union[int, torch.Tensor]sample: Tensorreturn_dict: bool = True ) → SchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int或torch.Tensor) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - return_dict (
bool,默认为True) — 是否返回 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 来预测上一步时间步的样本。此函数使用多步 DEIS 传播样本。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source >( prev_sample: Tensor )
调度器 step 函数输出的基类。