Diffusers documentation

DDIMScheduler

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

DDIMScheduler

Jiaming Song、Chenlin Meng 和 Stefano Ermon 提出的去噪扩散隐式模型(DDIM)。

该论文的摘要如下:

去噪扩散概率模型(DDPMs)无需对抗训练即可实现高质量的图像生成,但生成一个样本需要模拟马尔可夫链的许多步。为了加速采样,我们提出了去噪扩散隐式模型(DDIMs),这是一类更高效的迭代隐式概率模型,其训练流程与 DDPMs 相同。在 DDPMs 中,生成过程被定义为马尔可夫扩散过程的逆过程。我们构造了一类非马尔可夫扩散过程,它们具有相同的训练目标,但其逆过程的采样速度可以快得多。我们通过实验证明,与 DDPMs 相比,DDIMs 在墙钟时间上可以将高质量样本的生成速度提高 10 到 50 倍,允许我们在计算量和样本质量之间进行权衡,并且可以直接在潜在空间中执行语义上有意义的图像插值。

本文的原始代码库可在 ermongroup/ddim 中找到,您可以通过 tsong.me 联系作者。

提示

The paper Common Diffusion Noise Schedules and Sample Steps are Flawed claims that a mismatch between the training and inference settings leads to suboptimal inference generation results for Stable Diffusion. To fix this, the authors propose:

🧪 这是一个实验性功能!

  1. 重新缩放噪声调度,以强制终端信噪比(SNR)为零
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, rescale_betas_zero_snr=True)
  1. train a model with v_prediction (add the following argument to the train_text_to_image.py or train_text_to_image_lora.py scripts)
--prediction_type="v_prediction"
  1. 将采样器更改为始终从最后一个时间步开始
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
  1. 重新缩放 classifier-free guidance 以防止过度曝光
image = pipe(prompt, guidance_rescale=0.7).images[0]

例如:

from diffusers import DiffusionPipeline, DDIMScheduler
import torch

pipe = DiffusionPipeline.from_pretrained("ptx0/pseudo-journey-v2", dtype=torch.float16)
pipe.scheduler = DDIMScheduler.from_config(
    pipe.scheduler.config, rescale_betas_zero_snr=True, timestep_spacing="trailing"
)
pipe.to("cuda")  # or "mps", "xpu", "cpu"

prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"
image = pipe(prompt, guidance_rescale=0.7).images[0]
image

DDIMScheduler

class diffusers.DDIMScheduler

< >

( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: str = 'linear'trained_betas: numpy.ndarray | list[float] | None = Noneclip_sample: bool = Trueset_alpha_to_one: bool = Truesteps_offset: int = 0prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction'] = 'epsilon'thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995clip_sample_range: float = 1.0sample_max_value: float = 1.0timestep_spacing: typing.Literal['leading', 'trailing', 'linspace'] = 'leading'rescale_betas_zero_snr: bool = False )

参数

  • num_train_timesteps (int,默认为 1000) — 用于训练模型的扩散步数。
  • beta_start (float,默认为 0.0001) — 推理的起始 beta 值。
  • beta_end (float,默认为 0.02) — 最终的 beta 值。
  • beta_schedule (Literal["linear", "scaled_linear", "squaredcos_cap_v2"],默认为 "linear") — beta 调度,将 beta 范围映射为用于模型步进的一系列 beta 值。必须是 "linear"、"scaled_linear" 或 "squaredcos_cap_v2" 之一。
  • trained_betas (np.ndarray, 可选) — 直接传入 betas 数组到构造函数中,以绕过 beta_start 和 beta_end。
  • clip_sample (bool,默认为 True) — 对预测样本进行裁剪,以确保数值稳定性。
  • clip_sample_range (float,默认为 1.0) — 样本裁剪的最大幅度。仅在 clip_sample=True 时有效。
  • set_alpha_to_one (bool,默认为 True) — 每个扩散步骤都会使用当前步骤和上一步骤的 alpha 乘积值。对于最后一步,不存在上一步的 alpha。当此选项为 True 时,上一步的 alpha 乘积固定为 1,否则使用步骤 0 的 alpha 值。
  • steps_offset (int,默认为 0) — 添加到推理步数中的偏移量,某些模型系列需要此参数。
  • prediction_type (Literal["epsilon", "sample", "v_prediction"],默认为 "epsilon") — 调度器函数的预测类型。必须是 "epsilon"(预测扩散过程的噪声)、"sample"(直接预测含噪样本)或 "v_prediction"(参见 Imagen Video 论文的第 2.4 节)之一。
  • thresholding (bool,默认为 False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。
  • dynamic_thresholding_ratio (float,默认为 0.995) — 动态阈值化方法的比例。仅在 thresholding=True 时有效。
  • sample_max_value (float,默认为 1.0) — 动态阈值化的阈值。仅在 thresholding=True 时有效。
  • timestep_spacing (Literal["leading", "trailing", "linspace"],默认为 "leading") — 时间步的缩放方式。必须是 "leading"、"trailing" 或 "linspace" 之一。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。
  • rescale_betas_zero_snr (bool,默认为 False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与 --offset_noise 大致相关。

DDIMScheduler 在去噪扩散概率模型(DDPM)所引入的去噪过程基础上,扩展了非马尔可夫引导。

该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。

add_noise

< >

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

参数

  • original_samples (torch.Tensor) — 将添加噪声的原始样本。
  • noise (torch.Tensor) — 要添加到样本中的噪声。
  • timesteps (torch.IntTensor) — 指示每个样本噪声水平的时间步。

返回值

torch.Tensor

带噪声的样本。

根据每个时间步的噪声幅度,向原始样本添加噪声(即前向扩散过程)。

get_velocity

< >

( sample: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本。
  • noise (torch.Tensor) — 噪声张量。
  • timesteps (torch.IntTensor) — 用于速度计算的 timesteps。

返回值

torch.Tensor

计算得到的速度。

根据速度公式,从样本和噪声计算速度预测。

scale_model_input

< >

( sample: Tensortimestep: int = None ) → torch.Tensor

参数

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

返回值

torch.Tensor

经过缩放的输入样本。

确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。

set_timesteps

< >

( num_inference_steps: intdevice: typing.Union[str, torch.device] = None )

参数

  • num_inference_steps (int) — 使用预训练模型生成样本时所用的扩散步数。
  • device (str | torch.device,可选) — 用于 timesteps 的设备。

引发

ValueError

  • ValueError — 如果 num_inference_steps 大于 self.config.num_train_timesteps。

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

步骤

< >

( model_output: Tensortimestep: intsample: Tensoreta: float = 0.0use_clipped_model_output: bool = Falsegenerator: typing.Optional[torch.Generator] = Nonevariance_noise: typing.Optional[torch.Tensor] = Nonereturn_dict: bool = True ) → DDIMSchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • timestep (int) — 扩散链中的当前离散时间步。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • eta (float,可选,默认为 0.0) — 扩散步骤中添加噪声的权重。值为 0 对应 DDIM(确定性),值为 1 对应 DDPM(完全随机)。
  • use_clipped_model_output (bool,可选,默认为 False) — 如果为 True,则从经过裁剪的预测原始样本计算“修正后的”model_output。这是必要的,因为当 self.config.clip_sample 为 True 时,预测的原始样本会被裁剪到 [-1, 1] 范围内。如果未发生裁剪,则“修正后的”model_output 将与作为输入提供的那个相同,use_clipped_model_output 不起作用。
  • generator (torch.Generator, 可选) — 用于可复现采样的随机数生成器。
  • variance_noise (torch.Tensor, 可选) — 直接提供方差本身的噪声,作为使用 generator 生成噪声的替代方案。适用于 CycleDiffusion 等方法。
  • return_dict (bool,可选,默认为 True) — 是否返回 DDIMSchedulerOutput 或 tuple。

返回值

DDIMSchedulerOutput 或 tuple

如果 return_dict 为 True,则返回 DDIMSchedulerOutput,否则返回一个元组,其中第一个元素是样本张量。

通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出(通常是预测的噪声)进行传播。

DDIMSchedulerOutput

类 diffusers.schedulers.scheduling_ddim.DDIMSchedulerOutput

< >

( prev_sample: Tensorpred_original_sample: typing.Optional[torch.Tensor] = None )

参数

  • prev_sample(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 前一时间步的已计算样本 (x_{t-1})。在去噪循环中,应将 prev_sample 用作下一个模型输入。
  • pred_original_sample(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 基于当前时间步的模型输出所预测的去噪样本 (x_{0})。 pred_original_sample 可用于预览进度或进行引导。

调度器 step 函数输出的输出类。

Update on GitHub