Diffusers 文档

TCDScheduler

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

TCDScheduler

Trajectory Consistency Distillation 由 Jianbin Zheng, Minghui Hu, Zhongyi Fan, Chaoyue Wang, Changxing Ding, Dacheng Tao 和 Tat-Jen Cham 提出,介绍了一种策略性随机采样(算法 4),能够在少量步骤中生成良好的样本。策略性随机采样被区分为 Consistency Models 中的多步调度器(算法 1)的改进迭代,专门为轨迹一致性函数量身定制。

论文摘要如下:

Latent Consistency Model (LCM) 将 Consistency Model 扩展到潜在空间,并利用引导一致性蒸馏技术在加速文本到图像合成方面取得了令人印象深刻的性能。 然而,我们观察到 LCM 在生成既清晰又细致的图像方面存在困难。 为了解决这个限制,我们初步深入研究并阐明了根本原因。 我们的调查确定,主要问题源于三个不同领域的错误。 因此,我们引入了 Trajectory Consistency Distillation (TCD),其中包括轨迹一致性函数和策略性随机采样。 轨迹一致性函数通过扩大自一致性边界条件的范围并赋予 TCD 准确跟踪概率流 ODE 整个轨迹的能力来减少蒸馏误差。 此外,策略性随机采样专门设计用于规避多步一致性采样中固有的累积误差,而多步一致性采样经过精心定制以补充 TCD 模型。 实验表明,TCD 不仅显着提高了低 NFE 下的图像质量,而且在 高 NFE 下也产生了比教师模型更详细的结果。

原始代码库可以在 jabir-zheng/TCD 找到。

TCDScheduler

diffusers.TCDScheduler

< >

( num_train_timesteps: int = 1000 beta_start: float = 0.00085 beta_end: float = 0.012 beta_schedule: str = 'scaled_linear' trained_betas: typing.Union[numpy.ndarray, typing.List[float], NoneType] = None original_inference_steps: int = 50 clip_sample: bool = False clip_sample_range: float = 1.0 set_alpha_to_one: bool = True steps_offset: int = 0 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 timestep_spacing: str = 'leading' timestep_scaling: float = 10.0 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 (str, 默认为 "linear") — beta 时间表,从 beta 范围到用于步进模型的 beta 序列的映射。从 linearscaled_linearsquaredcos_cap_v2 中选择。
  • trained_betas (np.ndarray, 可选) — 直接将 beta 数组传递给构造函数以绕过 beta_startbeta_end
  • original_inference_steps (int, 可选, 默认为 50) — 用于生成线性间隔时间步长计划的默认推理步骤数,我们将最终从中取出 num_inference_steps 个均匀间隔的时间步长,以形成最终的时间步长计划。
  • 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 (str, 默认为 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 (str, 默认为 "leading") — 时间步长应缩放的方式。有关更多信息,请参阅 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。
  • timestep_scaling (float, 默认为 10.0) — 用于计算一致性模型边界条件 c_skipc_out 时,时间步长将乘以的因子。增加此值将减少近似误差(尽管默认值 10.0 的近似误差已经很小)。
  • rescale_betas_zero_snr (bool, 默认为 False) — 是否重新缩放 beta 以使其具有零终端 SNR。这使模型能够生成非常明亮和黑暗的样本,而不是将其限制为具有中等亮度的样本。与 --offset_noise 松散相关。

TCDScheduler 结合了 Trajectory Consistency Distillation 论文引入的 Strategic Stochastic Sampling,扩展了原始的多步一致性采样,以实现不受限制的轨迹遍历。

此代码基于 TCD 的官方仓库 (https://github.com/jabir-zheng/TCD)。

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

scale_model_input

< >

( sample: Tensor timestep: typing.Optional[int] = None ) torch.Tensor

参数

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

返回值

torch.Tensor

缩放后的输入样本。

确保与需要根据当前时间步长缩放去噪模型输入的调度器具有互换性。

set_begin_index

< >

( begin_index: int = 0 )

参数

  • begin_index (int) — 调度器的起始索引。

设置调度器的起始索引。此函数应在推理之前从管道运行。

set_timesteps

< >

( num_inference_steps: typing.Optional[int] = None device: typing.Union[str, torch.device] = None original_inference_steps: typing.Optional[int] = None timesteps: typing.Optional[typing.List[int]] = None strength: float = 1.0 )

参数

  • num_inference_steps (int, 可选) — 使用预训练模型生成样本时使用的扩散步骤数。如果使用,timesteps 必须为 None
  • device (strtorch.device, 可选) — 时间步长应移动到的设备。如果为 None,则时间步长不会移动。
  • original_inference_steps (int, 可选) — 原始推理步骤数,将用于生成线性间隔的时间步长计划(这与标准的 diffusers 实现不同)。然后,我们将从该计划中获取 num_inference_steps 个时间步长,在索引方面均匀间隔,并将其用作最终的时间步长计划。如果未设置,则默认为 original_inference_steps 属性。
  • timesteps (List[int], optional) — 用于支持时间步之间任意间隔的自定义时间步。如果为 None,则使用默认的时间步间距策略,即训练/蒸馏时间步计划上时间步之间的相等间距。如果传递了 timesteps,则 num_inference_steps 必须为 None
  • strength (float, optional, defaults to 1.0) — 用于确定在使用 img2img、inpaint 等时用于推理的时间步数。

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

step

< >

( model_output: Tensor timestep: int sample: Tensor eta: float = 0.3 generator: typing.Optional[torch._C.Generator] = None return_dict: bool = True ) ~schedulers.scheduling_utils.TCDSchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 来自已学习扩散模型的直接输出。
  • timestep (int) — 扩散链中当前的离散时间步。
  • sample (torch.Tensor) — 由扩散过程创建的样本的当前实例。
  • eta (float) — 一个随机参数(在论文中称为 gamma),用于控制每一步的随机性。当 eta = 0 时,它表示确定性采样,而 eta = 1 表示完全随机采样。
  • generator (torch.Generator, optional) — 随机数生成器。
  • return_dict (bool, optional, defaults to True) — 是否返回 TCDSchedulerOutputtuple

返回值

~schedulers.scheduling_utils.TCDSchedulerOutputtuple

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

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

TCDSchedulerOutput

class diffusers.schedulers.scheduling_tcd.TCDSchedulerOutput

< >

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

参数

  • prev_sample (torch.Tensor of shape (batch_size, num_channels, height, width) for images) — 上一个时间步的计算样本 (x_{t-1})prev_sample 应在去噪循环中用作下一个模型输入。
  • pred_noised_sample (torch.Tensor of shape (batch_size, num_channels, height, width) for images) — 基于当前时间步的模型输出,预测的噪声样本 (x_{s})

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

< > 在 GitHub 上更新