Diffusers 文档
潜在一致性模型多步调度器
并获得增强的文档体验
开始使用
潜在一致性模型多步调度器
概述
多步和单步调度器(算法 3)与潜在一致性模型一同在论文 Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference 中被介绍,作者为 Simian Luo、Yiqin Tan、Longbo Huang、Jian Li 和 Hang Zhao。此调度器应该能够从 LatentConsistencyModelPipeline 在 1-8 步内生成良好的样本。
LCMScheduler
class diffusers.LCMScheduler
< source >( 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 序列的映射。可选择linear
、scaled_linear
或squaredcos_cap_v2
。 - trained_betas (
np.ndarray
, 可选) — 直接将 beta 数组传递给构造函数以绕过beta_start
和beta_end
。 - original_inference_steps (
int
, optional, defaults to 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_skip
和c_out
时,时间步将乘以的因子。增加此值将减少近似误差(尽管默认值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() 函数提供通用的加载和保存功能。
scale_model_input
< source >( sample: Tensor timestep: typing.Optional[int] = None ) → torch.Tensor
确保与需要根据当前时间步缩放去噪模型输入的调度器具有互换性。
设置调度器的起始索引。此函数应在推理之前从管道运行。
set_timesteps
< source >( 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: int = 1.0 )
参数
- 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
。
设置用于扩散链的离散时间步(在推理之前运行)。
step
< source >( model_output: Tensor timestep: int sample: Tensor generator: typing.Optional[torch._C.Generator] = None return_dict: bool = True ) → ~schedulers.scheduling_utils.LCMSchedulerOutput
或 tuple
参数
- model_output (
torch.Tensor
) — 来自学习的扩散模型的直接输出。 - timestep (
float
) — 扩散链中当前的离散时间步。 - sample (
torch.Tensor
) — 由扩散过程创建的样本的当前实例。 - generator (
torch.Generator
, 可选) — 一个随机数生成器。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回LCMSchedulerOutput
或tuple
。
返回值
~schedulers.scheduling_utils.LCMSchedulerOutput
或 tuple
如果 return_dict 为 True
,则返回 LCMSchedulerOutput
,否则返回一个元组,其中第一个元素是样本张量。
通过反转 SDE,从上一个时间步预测样本。此函数从学习到的模型输出(通常是预测的噪声)传播扩散过程。