Diffusers documentation

EDMEulerScheduler

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

EDMEulerScheduler

Karras 等人论文 Elucidating the Design Space of Diffusion-Based Generative Models 中提出的 Euler 调度器(算法 2)的 Karras 公式。这是一种快速调度器,通常能在 20-30 步内生成良好的输出。该调度器基于 Katherine Crowson 的原始 k-diffusion 实现。

EDMEulerScheduler

class diffusers.EDMEulerScheduler

< >

( sigma_min: float = 0.002sigma_max: float = 80.0sigma_data: float = 0.5sigma_schedule: typing.Literal['karras', 'exponential'] = 'karras'num_train_timesteps: int = 1000prediction_type: typing.Literal['epsilon', 'v_prediction'] = 'epsilon'rho: float = 7.0final_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero' )

参数

  • sigma_min (float,可选,默认为 0.002) — sigma 调度中的最小噪声幅度。EDM 论文 [1] 中将其设置为 0.002;合理的取值范围为 [0, 10]。
  • sigma_max (float,可选,默认值为 80.0) — sigma 调度中的最大噪声幅度。在 EDM 论文 [1] 中,该值被设置为 80.0;合理的取值范围为 [0.2, 80.0]。
  • sigma_data (float,可选,默认为 0.5) — 数据分布的标准差。在 EDM 论文 [1] 中,该值被设置为 0.5。
  • sigma_schedule (Literal["karras", "exponential"],可选,默认为 "karras") — 用于计算 sigmas 的 Sigma 调度。默认情况下,我们使用 EDM 论文中提出的调度 (https://huggingface.co/papers/2206.00364)。"exponential" 调度已在此模型中引入: https://huggingface.co/stabilityai/cosxl。
  • num_train_timesteps (int,可选,默认为 1000) — 用于训练模型的扩散步数。
  • prediction_type (Literal["epsilon", "v_prediction"],可选,默认为 "epsilon") — 调度器函数的预测类型。"epsilon" 预测扩散过程中的噪声,而 "v_prediction"(参见 Imagen Video 论文的第 2.4 节)。
  • rho (float,可选,默认值为 7.0) — 用于计算 Karras sigma 调度的 rho 参数,在 EDM 论文 [1] 中该值被设置为 7.0。
  • final_sigmas_type (Literal["zero", "sigma_min"],可选,默认为 "zero") — 采样过程中噪声调度的最终 sigma 值。如果为 "sigma_min",则最终 sigma 与训练调度中的最后一个 sigma 相同。如果为 "zero",则最终 sigma 设为 0。

实现了 Karras 等人 2022 年 [1] 中提出的 EDM 形式下的 Euler 调度器。

[1] Karras, Tero, et al. “Elucidating the Design Space of Diffusion-Based Generative Models.” https://huggingface.co/papers/2206.00364

该模型继承自 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。

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

precondition_inputs

< >

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

参数

  • sample (torch.Tensor) — 用于预处理的输入样本张量。
  • sigma(float 或 torch.Tensor)— 当前的 sigma(噪声水平)值。

返回值

torch.Tensor

缩放后的输入样本。

根据 EDM 公式对输入样本进行缩放,以对其进行预处理。

precondition_noise

< >

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

参数

  • sigma (float 或 torch.Tensor) — 用于预处理的 sigma(噪声水平)值。

返回值

torch.Tensor

计算得到的预条件噪声值,即 0.25 * log(sigma)。

通过对噪声水平应用对数变换来进行预处理。

precondition_outputs

< >

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

参数

  • sample (torch.Tensor) — 输入样本张量。
  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • sigma(float 或 torch.Tensor)— 当前的 sigma(噪声水平)值。

返回值

torch.Tensor

通过结合跳跃连接和输出缩放计算得到的去噪样本。

根据 EDM 公式对模型输出进行预处理。

scale_model_input

< >

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

参数

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

返回值

torch.Tensor

经过缩放的输入样本。

将去噪模型输入进行缩放,以匹配 Euler 算法。确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器具有互换性。

set_begin_index

< >

( begin_index: int = 0 )

参数

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

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

set_timesteps

< >

( num_inference_steps: int = Nonedevice: typing.Union[str, torch.device] = Nonesigmas: typing.Union[list[float], torch.Tensor, NoneType] = None )

参数

  • num_inference_steps (int, 可选) — 使用预训练模型生成样本时所用的扩散步数。
  • device (str 或 torch.device,可选) — 时间步应移动到的设备。如果为 None,则不移动时间步。
  • sigmas (torch.Tensor | list[float], 可选) — 用于去噪过程的自定义 sigmas。如果未定义,将使用传入 num_inference_steps 时的默认行为。

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

步骤

< >

( model_output: Tensortimestep: typing.Union[float, torch.Tensor]sample: Tensors_churn: float = 0.0s_tmin: float = 0.0s_tmax: float = infs_noise: float = 1.0generator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = Truepred_original_sample: typing.Optional[torch.Tensor] = None ) → EDMEulerSchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • timestep (float 或 torch.Tensor) — 扩散链中的当前离散时间步。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • s_churn (float,可选,默认值为 0.0) — 每一步添加的随机性程度。值越高,添加的噪声越多。
  • s_tmin (float,可选,默认为 0.0) — 低于此最小 sigma 阈值时,不会添加任何噪声。
  • s_tmax(float,可选,默认为 float("inf"))— 超过该最大 sigma 阈值后不再添加噪声。
  • s_noise (float,可选,默认为 1.0) — 添加到样本中的噪声的缩放因子。
  • generator (torch.Generator, 可选) — 用于确保可复现性的随机数生成器。
  • return_dict (bool,可选,默认为 True) — 是否返回 EDMEulerSchedulerOutput 或元组。
  • pred_original_sample (torch.Tensor, 可选) — 上一步预测的去噪样本。如果提供,则跳过重新计算。

返回值

EDMEulerSchedulerOutput 或 tuple

如果 return_dict 为 True,则返回 EDMEulerSchedulerOutput;否则返回一个元组,其中第一个元素是前一个样本张量,第二个元素是预测的原始样本张量。

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

EDMEulerSchedulerOutput

class diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput

< >

( 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