Diffusers documentation
CMStochasticIterativeScheduler
CMStochasticIterativeScheduler
Yang Song、Prafulla Dhariwal、Mark Chen 和 Ilya Sutskever 提出的 Consistency Models 引入了一种多步和单步调度器(Algorithm 1),能够在一步或少数几步内生成高质量的样本。
该论文的摘要如下:
扩散模型在图像、音频和视频生成领域取得了显著进展,但它们依赖于迭代采样过程,导致生成速度缓慢。为了克服这一局限性,我们提出了一致性模型(consistency models),这是一种新型模型,通过直接将噪声映射到数据来生成高质量样本。它们在设计上支持快速的一步生成,同时仍允许多步采样,以在计算量和样本质量之间进行权衡。它们还支持零样本数据编辑,例如图像修复、上色和超分辨率,而无需针对这些任务进行显式训练。一致性模型可以通过蒸馏预训练的扩散模型来训练,也可以作为独立的生成模型进行训练。通过大量实验,我们证明它们在单步和少步采样中优于现有的扩散模型蒸馏技术,在 CIFAR-10 上实现了 3.55 的新 SOTA FID,在 ImageNet 64x64 上实现了 6.20 的新 SOTA FID(均为单步生成)。当独立训练时,一致性模型成为一种新的生成模型家族,在 CIFAR-10、ImageNet 64x64 和 LSUN 256x256 等标准基准上,能够超越现有的单步、非对抗性生成模型。
原始代码库可在 openai/consistency_models 中找到。
CMStochasticIterativeScheduler
class diffusers.CMStochasticIterativeScheduler
< source >( num_train_timesteps: int = 40sigma_min: float = 0.002sigma_max: float = 80.0sigma_data: float = 0.5s_noise: float = 1.0rho: float = 7.0clip_denoised: bool = True )
参数
- num_train_timesteps (
int,默认为 40) — 用于训练模型的扩散步数。 - sigma_min (
float,默认为 0.002) — sigma 调度中的最小噪声幅度。默认值为 0.002,沿用自原始实现。 - sigma_max (
float,默认为 80.0) — sigma 调度中的最大噪声幅度。默认值为 80.0,沿用自原始实现。 - sigma_data (
float,默认为 0.5) — EDM 论文中数据分布的标准差。默认值为 0.5,与原始实现一致。 - s_noise (
float,默认为 1.0) — 用于抵消采样过程中细节损失的额外噪声量。合理的取值范围为 [1.000, 1.011]。默认值为 1.0,与原始实现一致。 - rho (
float,默认值为 7.0) — 用于从 EDM 论文中计算 Karras sigma 调度的参数。默认值为 7.0,与原始实现一致。 - clip_denoised (
bool,默认为True) — 是否将去噪输出裁剪到(-1, 1)范围内。 - timesteps (
list或np.ndarray或torch.Tensor,可选) — 可显式指定的时间步调度。时间步应按递增顺序排列。
一致性模型的多步采样与单步采样。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。
get_scalings
< source >( sigma: Tensor ) → tuple[torch.Tensor, torch.Tensor]
计算一致性模型输出的缩放因子。
get_scalings_for_boundary_condition
< source >( sigma: Tensor ) → tuple[torch.Tensor, torch.Tensor]
获取一致性模型参数化(来自论文附录 C)中用于强制边界条件的缩放因子。
>
c_skip和c_out公式中的epsilon被设置为sigma_min。
index_for_timestep
< source >( timestep: typing.Union[float, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int
查找给定时间步在时间步调度中的索引。
scale_model_input
< source >( sample: Tensortimestep: typing.Union[float, torch.Tensor] ) → torch.Tensor
将一致性模型输入乘以 (sigma**2 + sigma_data**2) ** 0.5。
设置调度器的起始索引。此函数应在推理之前从流水线中运行。
set_timesteps
< source >( num_inference_steps: int | None = Nonedevice: typing.Union[str, torch.device] = Nonetimesteps: list[int] | None = None )
设置扩散链所使用的 timesteps(需在推理之前运行)。
sigma_to_t
< source >( sigmas: float | numpy.ndarray ) → np.ndarray
根据 Karras sigmas 获取用于一致性模型输入的缩放时间步。
步骤
< source >( model_output: Tensortimestep: typing.Union[float, torch.Tensor]sample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → CMStochasticIterativeSchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
float或torch.Tensor) — 扩散链中的当前时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - generator (
torch.Generator,可选) — 随机数生成器。 - return_dict (
bool,默认为True) — 是否返回 CMStochasticIterativeSchedulerOutput 或tuple。
返回值
If return_dict is True,
CMStochasticIterativeSchedulerOutput is returned,
otherwise a tuple is returned where the first element is the sample tensor.
通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出(通常是预测的噪声)进行传播。
CMStochasticIterativeSchedulerOutput
class diffusers.schedulers.scheduling_consistency_models.CMStochasticIterativeSchedulerOutput
< source >( prev_sample: Tensor )
调度器 step 函数的输出类。