Diffusers 文件
DPMSolverMultistepInverse
並獲得增強的文件體驗
開始使用
DPMSolverMultistepInverse
DPMSolverMultistepInverse
是來自 DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 Steps 和 DPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models(作者:Cheng Lu, Yuhao Zhou, Fan Bao, Jianfei Chen, Chongxuan Li, Jun Zhu)的反向排程器。
此實現主要基於 Null-text Inversion for Editing Real Images using Guided Diffusion Models 的 DDIM 反演定義和 Xiang-cd/DiffEdit-stable-diffusion 中 DiffEdit
潛在反演的筆記本實現。
提示
支援 Imagen 的動態閾值處理,對於畫素空間擴散模型,您可以同時設定 algorithm_type="dpmsolver++"
和 thresholding=True
以使用動態閾值處理。此閾值處理方法不適用於潛在空間擴散模型(如 Stable Diffusion)。
DPMSolverMultistepInverseScheduler
class diffusers.DPMSolverMultistepInverseScheduler
< 源 >( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: typing.Union[numpy.ndarray, typing.List[float], NoneType] = None solver_order: int = 2 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 algorithm_type: str = 'dpmsolver++' solver_type: str = 'midpoint' lower_order_final: bool = True euler_at_final: bool = False use_karras_sigmas: typing.Optional[bool] = False use_exponential_sigmas: typing.Optional[bool] = False use_beta_sigmas: typing.Optional[bool] = False use_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None timestep_spacing: str = 'linspace' steps_offset: int = 0 )
引數
- 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
。 - solver_order (
int
, 預設為 2) — DPMSolver 階數,可以是1
、2
或3
。建議在引導取樣中使用solver_order=2
,在無條件取樣中使用solver_order=3
。 - 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
且algorithm_type="dpmsolver++"
時有效。 - algorithm_type (
str
, 預設為dpmsolver++
) — 求解器的演算法型別;可以是dpmsolver
、dpmsolver++
、sde-dpmsolver
或sde-dpmsolver++
。dpmsolver
型別實現了 DPMSolver 論文中的演算法,dpmsolver++
型別實現了 DPMSolver++ 論文中的演算法。建議在 Stable Diffusion 等引導取樣中使用dpmsolver++
或sde-dpmsolver++
和solver_order=2
。 - solver_type (
str
, 預設為midpoint
) — 二階求解器的求解器型別;可以是midpoint
或heun
。求解器型別會稍微影響樣本質量,特別是對於少量步驟。建議使用midpoint
求解器。 - lower_order_final (
bool
, 預設為True
) — 是否在最後幾步使用低階求解器。僅對少於 15 個推理步驟有效。這可以穩定 DPMSolver 在少於 15 個步驟的取樣,特別是對於少於或等於 10 個步驟的情況。 - euler_at_final (
bool
, 預設為False
) — 是否在最後一步使用尤拉方法。這是數值穩定性與細節豐富性之間的權衡。這可以穩定 DPMSolver 的 SDE 變體在少量推理步驟下的取樣,但有時可能導致模糊。 - use_karras_sigmas (
bool
, 可選, 預設為False
) — 是否在取樣過程中使用 Karras sigmas 作為噪聲排程中的步長。如果為True
,則根據一系列噪聲水平 {σi} 確定 sigmas。 - use_exponential_sigmas (
bool
, 可選, 預設為False
) — 是否在取樣過程中使用指數 sigmas 作為噪聲排程中的步長。 - use_beta_sigmas (
bool
, 可選, 預設為False
) — 是否在取樣過程中使用 beta sigmas 作為噪聲排程中的步長。有關更多資訊,請參閱 Beta Sampling is All You Need。 - lambda_min_clipped (
float
, 預設為-inf
) — 用於數值穩定性的lambda(t)
最小值的剪裁閾值。這對於餘弦 (squaredcos_cap_v2
) 噪聲排程至關重要。 - variance_type (
str
, 可選) — 對於預測方差的擴散模型,設定為“learned”或“learned_range”。如果設定,模型的輸出將包含預測的高斯方差。 - timestep_spacing (
str
, 預設為"linspace"
) — 時間步長的縮放方式。有關更多資訊,請參閱 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。 - steps_offset (
int
, 預設為 0) — 根據某些模型系列的要求,新增到推理步驟的偏移量。
DPMSolverMultistepInverseScheduler
是 DPMSolverMultistepScheduler 的反向排程器。
此模型繼承自 SchedulerMixin 和 ConfigMixin。請檢視超類文件,瞭解庫為所有排程器實現的通用方法,例如載入和儲存。
convert_model_output
< 源 >( model_output: Tensor *args sample: Tensor = None **kwargs ) → torch.Tensor
將模型輸出轉換為 DPMSolver/DPMSolver++ 演算法所需的相應型別。DPM-Solver 旨在離散化噪聲預測模型的積分,而 DPM-Solver++ 旨在離散化資料預測模型的積分。
演算法和模型型別是解耦的。噪聲預測模型和資料預測模型都可以使用DPMSolver或DPMSolver++。
dpm_solver_first_order_update
< 源 >( model_output: Tensor *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
一階DPMSolver(等同於DDIM)的一個步驟。
multistep_dpm_solver_second_order_update
< 源 >( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
二階多步DPMSolver的一個步驟。
multistep_dpm_solver_third_order_update
< 源 >( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) → torch.Tensor
三階多步DPMSolver的一個步驟。
scale_model_input
< source 原始碼 >( sample: Tensor *args **kwargs ) → torch.Tensor
確保與需要根據當前時間步縮放去噪模型輸入的排程器互換使用。
set_timesteps
< source 原始碼 >( num_inference_steps: int = None device: typing.Union[str, torch.device] = None )
設定用於擴散鏈的離散時間步(在推理之前執行)。
步驟
< source 原始碼 >( model_output: Tensor timestep: typing.Union[int, torch.Tensor] sample: Tensor generator = None variance_noise: typing.Optional[torch.Tensor] = None return_dict: bool = True ) → SchedulerOutput 或 tuple
引數
- model_output (
torch.Tensor
) — 已學習擴散模型的直接輸出。 - timestep (
int
) — 擴散鏈中的當前離散時間步。 - sample (
torch.Tensor
) — 由擴散過程建立的當前樣本例項。 - generator (
torch.Generator
, 可選) — 隨機數生成器。 - variance_noise (
torch.Tensor
) — 用於透過直接提供方差本身的噪聲來生成噪聲的替代方法。對於CycleDiffusion
等方法很有用。 - return_dict (
bool
) — 是否返回 SchedulerOutput 或tuple
。
返回
排程器輸出 或 tuple
如果 return_dict
為 True
,則返回 SchedulerOutput,否則返回一個元組,其中第一個元素是樣本張量。
透過反轉 SDE 預測前一個時間步的樣本。此函式使用多步 DPMSolver 傳播樣本。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< source 原始碼 >( prev_sample: Tensor )
排程器 step
函式輸出的基類。