Diffusers documentation
PNDMScheduler
PNDMScheduler
PNDMScheduler,即用于扩散模型的伪数值方法,采用了更高级的 ODE 积分技术,例如 Runge-Kutta 方法和线性多步法。原始实现可在 crowsonkb/k-diffusion 中找到。
PNDMScheduler
class diffusers.PNDMScheduler
< source >( 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
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据每个时间步的噪声幅度,向原始样本添加噪声(即前向扩散过程)。
scale_model_input
< source >( sample: Tensor*args**kwargs ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
set_timesteps
< source >( num_inference_steps: intdevice: typing.Union[str, torch.device] = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( 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
< source >( 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
< source >( 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
< source >( prev_sample: Tensor )
调度器 step 函数输出的基类。