Diffusers documentation
EDMDPMSolverMultistepScheduler
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
< source >( 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 toepsilon, optional) — Prediction type of the scheduler function; can beepsilon(predicts the noise of the diffusion process),sample(directly predicts the noisy sample) orv_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
< source >( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。
convert_model_output
< source >( model_output: Tensorsample: Tensor ) → torch.Tensor
将模型输出转换为 DPMSolver/DPMSolver++ 算法所需的对应类型。DPM-Solver 旨在对噪声预测模型的积分进行离散化,而 DPM-Solver++ 则旨在对数据预测模型的积分进行离散化。
> 算法和模型类型是解耦的。对于噪声 > 预测模型和数据预测模型,都可以使用 DPMSolver 或 DPMSolver++。
dpm_solver_first_order_update
< source >( model_output: Tensorsample: Tensornoise: typing.Optional[torch.Tensor] = None ) → 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: typing.List[torch.Tensor]sample: Tensornoise: typing.Optional[torch.Tensor] = None ) → torch.Tensor
二阶多步 DPMSolver 的单步。
multistep_dpm_solver_third_order_update
< source >( model_output_list: listsample: Tensor ) → torch.Tensor
三阶多步 DPMSolver 的单步。
precondition_inputs
< source >( sample: Tensorsigma: typing.Union[float, torch.Tensor] ) → torch.Tensor
根据 EDM 公式对输入样本进行缩放,以对其进行预处理。
precondition_noise
< source >( sigma: typing.Union[float, torch.Tensor] ) → torch.Tensor
通过对噪声水平应用对数变换来进行预处理。
precondition_outputs
< source >( sample: Tensormodel_output: Tensorsigma: typing.Union[float, torch.Tensor] ) → torch.Tensor
根据 EDM 公式对模型输出进行预处理。
scale_model_input
< source >( sample: Tensortimestep: typing.Union[float, torch.Tensor] ) → torch.Tensor
将去噪模型输入进行缩放,以匹配 Euler 算法。确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器具有互换性。
设置调度器的起始索引。此函数应在推理之前从流水线中运行。
set_timesteps
< source >( num_inference_steps: int = Nonedevice: typing.Union[str, torch.device, NoneType] = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( 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
< source >( prev_sample: Tensor )
调度器 step 函数输出的基类。