Diffusers documentation

EulerAncestralDiscreteScheduler

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

EulerAncestralDiscreteScheduler

一个使用欧拉法步长进行祖先采样的调度器。这是一个快速的调度器,通常可以在 20-30 步内生成良好的输出。该调度器基于 Katherine Crowson 的原始 k-diffusion 实现。

EulerAncestralDiscreteScheduler

class diffusers.EulerAncestralDiscreteScheduler

< >

( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: str = 'linear'trained_betas: numpy.ndarray | list[float] | None = Noneprediction_type: str = 'epsilon'timestep_spacing: str = 'linspace'steps_offset: int = 0rescale_betas_zero_snr: bool = False )

参数

  • num_train_timesteps (int,默认为 1000) — 用于训练模型的扩散步数。
  • beta_start (float,默认为 0.0001) — 推理的起始 beta 值。
  • beta_end (float,默认为 0.02) — 最终的 beta 值。
  • beta_schedule("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。
  • prediction_type ("epsilon", "sample", or "v_prediction", defaults to "epsilon", optional) — 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).
  • timestep_spacing("linspace"、"leading" 或 "trailing",默认为 "linspace") — 时间步的缩放方式。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。
  • steps_offset (int,默认为 0) — 添加到推理步数中的偏移量,某些模型系列需要此参数。
  • rescale_betas_zero_snr (bool,默认为 False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与 --offset_noise 大致相关。

使用欧拉方法步长的祖先采样。

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

add_noise

< >

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

参数

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

返回值

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。

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

scale_model_input

< >

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

参数

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

返回值

torch.Tensor

经过缩放的输入样本。

确保与需要根据当前时间步缩放去噪模型输入的调度器具有互换性。将去噪模型输入按 (sigma**2 + 1) ** 0.5 进行缩放,以匹配 Euler 算法。

set_begin_index

< >

( begin_index: int = 0 )

参数

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

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

set_timesteps

< >

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

参数

  • num_inference_steps (int) — 使用预训练模型生成样本时所用的扩散步数。
  • device (str 或 torch.device,可选) — 时间步应移动到的设备。如果为 None,则不移动时间步。

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

步骤

< >

( model_output: Tensortimestep: typing.Union[float, torch.Tensor]sample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → EulerAncestralDiscreteSchedulerOutput or tuple

参数

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

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

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

EulerAncestralDiscreteSchedulerOutput

class diffusers.schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput

< >

( 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