Diffusers documentation

CMStochasticIterativeScheduler

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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

< >

( 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

< >

( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor

参数

  • original_samples (torch.Tensor) — 将添加噪声的原始样本。
  • noise (torch.Tensor) — 要添加到原始样本中的噪声张量。
  • timesteps (torch.Tensor) — 用于添加噪声的时间步,决定调度器中的噪声水平。

返回值

torch.Tensor

根据时间步调度对添加的噪声进行缩放后的含噪样本。

根据指定时间步的噪声调度,向原始样本添加噪声。

get_scalings

< >

( sigma: Tensor ) → tuple[torch.Tensor, torch.Tensor]

参数

  • sigma (torch.Tensor) — 噪声调度中当前的 sigma 值。

返回值

tuple[torch.Tensor, torch.Tensor]

一个元组,包含 c_skip(用于输入样本的缩放)和 c_out(用于模型输出的缩放)。

计算一致性模型输出的缩放因子。

get_scalings_for_boundary_condition

< >

( sigma: Tensor ) → tuple[torch.Tensor, torch.Tensor]

参数

  • sigma (torch.Tensor) — Karras sigma 调度中当前的 sigma 值。

返回值

tuple[torch.Tensor, torch.Tensor]

一个二元组,其中 c_skip(用于加权当前样本)是第一个元素,c_out(用于加权一致性模型的输出)是第二个元素。

获取一致性模型参数化(来自论文附录 C)中用于强制边界条件的缩放因子。

> c_skip 和 c_out 公式中的 epsilon 被设置为 sigma_min。

index_for_timestep

< >

( timestep: typing.Union[float, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int

参数

  • timestep(float 或 torch.Tensor)— 要在调度器中查找的时间步值。
  • schedule_timesteps (torch.Tensor,可选) — 要搜索的时间步调度。如果为 None,则使用 self.timesteps。

返回值

int

时间步在调度器中的索引。对于第一个步骤,如果存在多个匹配项,则返回第二个索引,以避免在从调度器中间开始(例如,用于图像到图像任务)时跳过某个 sigma。

查找给定时间步在时间步调度中的索引。

scale_model_input

< >

( sample: Tensortimestep: typing.Union[float, torch.Tensor] ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本。
  • timestep (float 或 torch.Tensor) — 扩散链中的当前时间步。

返回值

torch.Tensor

经过缩放的输入样本。

将一致性模型输入乘以 (sigma**2 + sigma_data**2) ** 0.5。

set_begin_index

< >

( begin_index: int = 0 )

参数

  • begin_index (int,默认为 0) — 调度器的起始索引。

设置调度器的起始索引。此函数应在推理之前从流水线中运行。

set_timesteps

< >

( num_inference_steps: int | None = Nonedevice: typing.Union[str, torch.device] = Nonetimesteps: list[int] | None = None )

参数

  • num_inference_steps (int, 可选) — 使用预训练模型生成样本时所用的扩散步数。
  • device (str 或 torch.device,可选) — 时间步应移动到的设备。如果为 None,则不移动时间步。
  • timesteps (list[int], 可选) — 用于支持任意时间步间隔的自定义时间步。如果为 None,则使用默认的时间步间隔策略,即时间步之间等间隔。如果传入了 timesteps,则 num_inference_steps 必须为 None。

设置扩散链所使用的 timesteps(需在推理之前运行)。

sigma_to_t

< >

( sigmas: float | numpy.ndarray ) → np.ndarray

参数

  • sigmas (float 或 np.ndarray) — 单个 Karras sigma 或一组 Karras sigmas。

返回值

np.ndarray

缩放后的输入时间步数组。

根据 Karras sigmas 获取用于一致性模型输入的缩放时间步。

步骤

< >

( 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

< >

( prev_sample: Tensor )

参数

  • prev_sample(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 前一时间步的已计算样本 (x_{t-1})。在去噪循环中,应将 prev_sample 用作下一个模型输入。

调度器 step 函数的输出类。

Update on GitHub