Diffusers documentation

PNDMScheduler

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

PNDMScheduler

PNDMScheduler,即用于扩散模型的伪数值方法,采用了更高级的 ODE 积分技术,例如 Runge-Kutta 方法和线性多步法。原始实现可在 crowsonkb/k-diffusion 中找到。

PNDMScheduler

class diffusers.PNDMScheduler

< >

( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: str = 'linear'trained_betas: numpy.ndarray | list[float] | None = Noneskip_prk_steps: bool = Falseset_alpha_to_one: bool = Falseprediction_type: typing.Literal['epsilon', 'v_prediction'] = 'epsilon'timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'leading'steps_offset: int = 0 )

参数

  • 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 值。
  • trained_betas (np.ndarray, 可选) — 直接传入 betas 数组到构造函数中,以绕过 beta_start 和 beta_end。
  • skip_prk_steps (bool,默认为 False) — 允许调度器跳过原始论文中定义的、在 PLMS 步骤之前所需的 Runge-Kutta 步骤。
  • set_alpha_to_one (bool,默认为 False) — 每个扩散步骤都会使用当前步骤和上一步骤的 alpha 乘积值。对于最后一步,不存在上一步的 alpha。当此选项为 True 时,上一步的 alpha 乘积固定为 1,否则使用第 0 步的 alpha 值。
  • prediction_type("epsilon" 或 "v_prediction",默认为 "epsilon")— 调度器函数的预测类型;可以是 epsilon(预测扩散过程的噪声)或 v_prediction(参见 Imagen Video 论文的第 2.4 节)。
  • timestep_spacing("linspace"、"leading" 或 "trailing",默认为 "leading")— 时间步的缩放方式。更多信息请参阅 常见的扩散噪声调度和采样步骤存在缺陷 中的表 2。
  • steps_offset(int,默认为 0)— 添加到推理步数中的偏移量,某些模型系列需要此参数。

PNDMScheduler 使用伪数值方法(如 Runge-Kutta 方法和线性多步法)来处理扩散模型。

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

add_noise

< >

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

参数

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

返回值

torch.Tensor

带噪声的样本。

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

scale_model_input

< >

( sample: Tensor*args**kwargs ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本。

返回值

torch.Tensor

经过缩放的输入样本。

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

set_timesteps

< >

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

参数

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

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

步骤

< >

( model_output: Tensortimestep: intsample: Tensorreturn_dict: bool = True ) → SchedulerOutput or tuple

参数

  • model_output (torch.Tensor) — 学习到的扩散模型的直接输出。
  • timestep (int) — 扩散链中的当前离散时间步。
  • sample (torch.Tensor) — 扩散过程生成的样本的当前实例。
  • return_dict (bool,默认为 True) — 是否返回 SchedulerOutput 或 tuple。

返回值

SchedulerOutput 或 tuple

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

通过逆向 SDE 预测上一时间步的样本。此函数根据学习到的模型输出(通常是预测的噪声)传播扩散过程,并根据内部变量 counter 调用 step_prk() 或 step_plms()。

step_plms

< >

( model_output: Tensortimestep: intsample: Tensorreturn_dict: bool = True ) → SchedulerOutput or tuple

参数

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

返回值

SchedulerOutput 或 tuple

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

通过逆向 SDE 预测上一步时间步的样本。此函数使用线性多步法传播样本。它多次执行前向传播以近似求解。

step_prk

< >

( model_output: Tensortimestep: intsample: Tensorreturn_dict: bool = True ) → SchedulerOutput or tuple

参数

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

返回值

SchedulerOutput 或 tuple

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

通过逆向 SDE 预测上一步时间步的样本。此函数使用 Runge-Kutta 方法传播样本。它执行四次前向传播来近似求解微分方程。

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< >

( prev_sample: Tensor )

参数

  • prev_sample(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 前一时间步的已计算样本 (x_{t-1})。在去噪循环中,应将 prev_sample 用作下一个模型输入。

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

Update on GitHub