Diffusers 文件

DPMSolverMultistepScheduler

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

DPMSolverMultistepScheduler

DPMSolverMultistepScheduler 是一個多步排程器,源自 Cheng Lu、Yuhao Zhou、Fan Bao、Jianfei Chen、Chongxuan Li 和 Jun Zhu 的論文 DPM-Solver:一種用於擴散機率模型取樣的快速 ODE 求解器,約需 10 步DPM-Solver++:用於擴散機率模型引導取樣的快速求解器

DPMSolver(及其改進版 DPMSolver++)是一種快速專用高階求解器,用於擴散 ODE,具有收斂階保證。根據經驗,DPMSolver 僅需 20 步取樣即可生成高質量樣本,即使在 10 步內也能生成相當好的樣本。

提示

建議將 solver_order 設定為 2 用於引導取樣,將 solver_order 設定為 3 用於無條件取樣。

支援 Imagen 中的動態閾值處理,對於畫素空間擴散模型,您可以同時設定 algorithm_type="dpmsolver++"thresholding=True 以使用動態閾值處理。此閾值處理方法不適用於潛在空間擴散模型,例如 Stable Diffusion。

DPMSolver 和 DPM-Solver++ 的 SDE 變體也受支援,但僅限於一階和二階求解器。這是一個用於逆向擴散 SDE 的快速 SDE 求解器。建議使用二階 sde-dpmsolver++

DPMSolverMultistepScheduler

class diffusers.DPMSolverMultistepScheduler

< >

( 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_lu_lambdas: typing.Optional[bool] = False use_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 final_sigmas_type: typing.Optional[str] = 'zero' lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None timestep_spacing: str = 'linspace' steps_offset: int = 0 rescale_betas_zero_snr: bool = False )

引數

  • num_train_timesteps (int, 預設為 1000) — 訓練模型的擴散步數。
  • beta_start (float, 預設為 0.0001) — 推理的起始 beta 值。
  • beta_end (float, 預設為 0.02) — 最終 beta 值。
  • beta_schedule (str, 預設為 "linear") — beta 排程,從 beta 範圍到用於步進模型的一系列 beta 的對映。可選擇 linearscaled_linearsquaredcos_cap_v2
  • trained_betas (np.ndarray, 可選) — 直接向建構函式傳遞一個 betas 陣列以繞過 beta_startbeta_end
  • solver_order (int, 預設為 2) — DPMSolver 階數,可以是 123。建議使用 solver_order=2 用於引導取樣,使用 solver_order=3 用於無條件取樣。
  • prediction_type (str, 預設為 epsilon, 可選) — 排程器函式的預測型別;可以是 epsilon(預測擴散過程的噪聲)、sample(直接預測噪聲樣本)、v_prediction(參見 Imagen Video 論文的 2.4 節)或 flow_prediction
  • thresholding (bool, 預設為 False) — 是否使用“動態閾值處理”方法。這不適用於潛在空間擴散模型,例如 Stable Diffusion。
  • dynamic_thresholding_ratio (float, 預設為 0.995) — 動態閾值處理方法的比率。僅在 thresholding=True 時有效。
  • sample_max_value (float, 預設為 1.0) — 動態閾值處理的閾值。僅在 thresholding=Truealgorithm_type="dpmsolver++" 時有效。
  • algorithm_type (str, 預設為 dpmsolver++) — 求解器的演算法型別;可以是 dpmsolverdpmsolver++sde-dpmsolversde-dpmsolver++dpmsolver 型別實現了 DPMSolver 論文中的演算法,而 dpmsolver++ 型別實現了 DPMSolver++ 論文中的演算法。對於 Stable Diffusion 等引導取樣,建議使用 dpmsolver++sde-dpmsolver++solver_order=2
  • solver_type (str, 預設為 midpoint) — 二階求解器的求解器型別;可以是 midpointheun。求解器型別會稍微影響樣本質量,特別是對於少量步數。建議使用 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,則 sigmas 根據一系列噪聲水平 {σi} 確定。
  • use_exponential_sigmas (bool, 可選, 預設為 False) — 在取樣過程中,是否使用指數 sigmas 作為噪聲排程中的步長。
  • use_beta_sigmas (bool, 可選, 預設為 False) — 在取樣過程中,是否使用 beta sigmas 作為噪聲排程中的步長。更多資訊請參閱 Beta Sampling is All You Need
  • use_lu_lambdas (bool, 可選, 預設為 False) — 在取樣過程中,是否使用 Lu 的 DPM-Solver 提出的統一對數信噪比(uniform-logSNR)作為噪聲排程中的步長。如果為 True,則 sigmas 和時間步長根據一系列 lambda(t) 確定。
  • use_flow_sigmas (bool, 可選, 預設為 False) — 在取樣過程中,是否使用流 sigmas 作為噪聲排程中的步長。
  • flow_shift (float, 可選, 預設為 1.0) — 流匹配時間步長排程的偏移值。
  • final_sigmas_type (str, 預設為 "zero") — 取樣過程中噪聲排程的最終 sigma 值。如果為 "sigma_min",則最終 sigma 與訓練排程中的最後一個 sigma 相同。如果為 zero,則最終 sigma 設定為 0。
  • 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) — 推理步驟的偏移量,某些模型系列需要。
  • rescale_betas_zero_snr (bool, 預設為 False) — 是否重新縮放 betas 以具有零終端信噪比(SNR)。這使得模型能夠生成非常明亮和黑暗的樣本,而不是將其限制為中等亮度的樣本。與 --offset_noise 鬆散相關。

DPMSolverMultistepScheduler 是一個用於擴散 ODE 的快速專用高階求解器。

此模型繼承自 SchedulerMixinConfigMixin。有關庫為所有排程器(例如載入和儲存)實現的通用方法,請檢視超類文件。

convert_model_output

< >

( model_output: Tensor *args sample: Tensor = None **kwargs ) torch.Tensor

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。

返回

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

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。

返回

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

引數

  • model_output_list (List[torch.Tensor]) — 來自學習擴散模型在當前和後續時間步的直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。

返回

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

引數

  • model_output_list (List[torch.Tensor]) — 來自學習擴散模型在當前和後續時間步的直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。

返回

torch.Tensor

上一個時間步的樣本張量。

三階多步DPMSolver的一個步驟。

scale_model_input

< >

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

引數

  • sample (torch.Tensor) — 輸入樣本。

返回

torch.Tensor

一個縮放後的輸入樣本。

確保與需要根據當前時間步縮放去噪模型輸入的排程器互換使用。

set_begin_index

< >

( begin_index: int = 0 )

引數

  • begin_index (int) — 排程器的起始索引。

設定排程器的起始索引。此函式應在推理之前從管道中執行。

set_timesteps

< >

( num_inference_steps: int = None device: typing.Union[str, torch.device] = None timesteps: typing.Optional[typing.List[int]] = None )

引數

  • num_inference_steps (int) — 使用預訓練模型生成樣本時使用的擴散步數。
  • device (strtorch.device, 可選) — 時間步應移動到的裝置。如果為 None,則時間步不移動。
  • timesteps (List[int], 可選) — 用於支援任意時間步排程器的自定義時間步。如果為 None,時間步將根據 timestep_spacing 屬性生成。如果傳遞了 timesteps,則 num_inference_stepssigmas 必須為 None,並且 timestep_spacing 屬性將被忽略。

設定用於擴散鏈的離散時間步(在推理之前執行)。

步驟

< >

( model_output: Tensor timestep: typing.Union[int, torch.Tensor] sample: Tensor generator = None variance_noise: typing.Optional[torch.Tensor] = None return_dict: bool = True ) SchedulerOutputtuple

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中當前的離散時間步。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。
  • generator (torch.Generator, 可選) — 隨機數生成器。
  • variance_noise (torch.Tensor) — 生成噪聲的替代方法,透過直接提供方差本身的噪聲。對於 LEdits++ 等方法很有用。
  • return_dict (bool) — 是否返回 SchedulerOutputtuple

返回

排程器輸出tuple

如果 return_dict 為 True,則返回 SchedulerOutput,否則返回一個元組,其中第一個元素是樣本張量。

透過反轉 SDE 預測前一個時間步的樣本。此函式使用多步 DPMSolver 傳播樣本。

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< >

( prev_sample: Tensor )

引數

  • prev_sample (torch.Tensor,影像形狀為 (batch_size, num_channels, height, width)) — 計算出的上一個時間步的樣本 (x_{t-1})prev_sample 應作為去噪迴圈中的下一個模型輸入。

排程器 step 函式輸出的基類。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.