Diffusers documentation

潜在一致性模型多步调度器

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

潜在一致性模型多步调度器

概述

随 Simian Luo、Yiqin Tan、Longbo Huang、Jian Li 和 Hang Zhao 的论文 Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference 一同引入的多步和单步调度器(Algorithm 3),该论文提出了潜在一致性模型。 该调度器应能够在 1-8 步内从 LatentConsistencyModelPipeline 生成高质量的样本。

LCMScheduler

class diffusers.LCMScheduler

< >

( num_train_timesteps: int = 1000beta_start: float = 0.00085beta_end: float = 0.012beta_schedule: str = 'scaled_linear'trained_betas: numpy.ndarray | list[float] | None = Noneoriginal_inference_steps: int = 50clip_sample: bool = Falseclip_sample_range: float = 1.0set_alpha_to_one: bool = Truesteps_offset: int = 0prediction_type: str = 'epsilon'thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995sample_max_value: float = 1.0timestep_spacing: str = 'leading'timestep_scaling: float = 10.0rescale_betas_zero_snr: bool = False )

参数

  • num_train_timesteps (int,默认为 1000) — 用于训练模型的扩散步数。
  • beta_start (float,默认为 0.00085) — 推理的起始 beta 值。
  • beta_end (float,默认为 0.012) — 最终的 beta 值。
  • beta_schedule (str,默认为 "scaled_linear") — Beta 调度,将 beta 范围映射为用于模型步进的一系列 beta 值。可选 linear、scaled_linear 或 squaredcos_cap_v2。
  • trained_betas(np.ndarray 或 list[float],可选)— 直接传入 betas 数组到构造函数中,以绕过 beta_start 和 beta_end。
  • original_inference_steps (int,默认为 50) — 用于生成线性间隔时间步调度的默认推理步数,最终将从中均匀选取 num_inference_steps 个时间步,以构成最终的时间步调度。
  • clip_sample (bool,默认为 False) — 对预测样本进行裁剪,以确保数值稳定性。
  • 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 (str, defaults to "epsilon") — Prediction type of the scheduler function; can be epsilon (predicts the noise of the diffusion process), sample (directly predicts the noisy sample) or v_prediction` (see section 2.4 of Imagen Video paper).
  • thresholding (bool,默认为 False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。
  • dynamic_thresholding_ratio (float, 默认值为 0.995) — 动态阈值化方法的比例。仅在 thresholding=True 时有效。
  • sample_max_value (float,默认为 1.0) — 动态阈值化的阈值。仅在 thresholding=True 时有效。
  • timestep_spacing (str,默认为 "leading") — 时间步的缩放方式。更多信息请参阅 常见扩散噪声调度和采样步骤存在缺陷 中的表 2。
  • timestep_scaling (float,默认为 10.0) — 在计算一致性模型边界条件 c_skip 和 c_out 时,时间步(timesteps)将被乘以的系数。增大该值会降低近似误差(尽管在默认值 10.0 下,近似误差已经相当小)。
  • rescale_betas_zero_snr (bool,默认为 False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与 --offset_noise 大致相关。

LCMScheduler 扩展了去噪扩散概率模型(DDPM)中引入的去噪过程,并加入了非马尔可夫引导。

该模型继承自 SchedulerMixin 和 ConfigMixin。~ConfigMixin 负责存储通过调度器的 __init__ 函数传入的所有配置属性,例如 num_train_timesteps。可以通过 scheduler.config.num_train_timesteps 访问这些属性。SchedulerMixin 通过 SchedulerMixin.save_pretrained() 和 from_pretrained() 函数提供通用的加载和保存功能。

add_noise

< >

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

参数

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

返回值

torch.Tensor

带噪声的样本。

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

get_scalings_for_boundary_condition_discrete

< >

( timestep: int ) → tuple[float, float]

参数

  • timestep (int) — 用于计算缩放系数的离散时间步。

返回值

tuple[float, float]

一个元组,包含 c_skip(用于输入样本的缩放)和 c_out(用于预测的去噪样本的缩放)。

计算给定离散时间步的边界条件缩放系数(c_skip 和 c_out),其用法与 Latent Consistency Model 一致。

get_velocity

< >

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

参数

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

返回值

torch.Tensor

计算得到的速度。

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

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。

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

previous_timestep

< >

( timestep ) → int or torch.Tensor

参数

  • timestep (int) — 当前时间步。

返回值

int 或 torch.Tensor

上一个时间步。

计算扩散链中的前一个时间步。

scale_model_input

< >

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

参数

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

返回值

torch.Tensor

经过缩放的输入样本。

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

set_begin_index

< >

( begin_index: int = 0 )

参数

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

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

set_timesteps

< >

( num_inference_steps: int | None = Nonedevice: typing.Union[str, torch.device, NoneType] = Noneoriginal_inference_steps: int | None = Nonetimesteps: list[int] | None = Nonestrength: float = 1.0 ) → None

参数

  • num_inference_steps (int, 可选) — 使用预训练模型生成样本时所用的扩散步数。如果使用了该参数,timesteps 必须为 None。
  • device (str 或 torch.device,可选) — 时间步应移动到的设备。如果为 None,则不移动时间步。
  • original_inference_steps (int,可选) — 原始推理步数,用于生成线性间隔的时间步调度(与标准的 diffusers 实现不同)。随后,我们将按索引均匀地从该调度中选取 num_inference_steps 个时间步,并将其作为最终的时间步调度。如果未设置,则默认为 original_inference_steps 属性。
  • timesteps (list[int], 可选) — 用于支持任意时间步间隔的自定义时间步。如果为 None,则使用训练/蒸馏时间步调度中时间步等间隔的默认时间步间隔策略。如果传入了 timesteps,则 num_inference_steps 必须为 None。
  • strength (float,默认为 1.0) — 用于生成部分时间步调度的强度因子(例如用于图像到图像任务)。值为 1.0 时使用完整调度。

返回值

None

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

步骤

< >

( model_output: Tensortimestep: intsample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → LCMSchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • timestep (int) — 扩散链中的当前离散时间步。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • generator (torch.Generator, 可选) — 用于可复现采样的随机数生成器。
  • return_dict (bool,默认为 True) — 是否返回 LCMSchedulerOutput 或 tuple。

返回值

LCMSchedulerOutput 或 tuple

If return_dict is True, LCMSchedulerOutput is returned, otherwise a tuple is returned where the first element is the sample tensor.

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

Update on GitHub