Diffusers 文件

DPMSolverSinglestepScheduler

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

DPMSolverSinglestepScheduler

DPMSolverSinglestepScheduler 是一個單步排程器,來自 Cheng Lu、Yuhao Zhou、Fan Bao、Jianfei Chen、Chongxuan Li 和 Jun Zhu 的論文 DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 StepsDPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models

DPMSolver(及其改進版本 DPMSolver++)是一個快速的專用高階求解器,用於求解擴散 ODE,並提供收斂階保證。根據經驗,DPMSolver 只需 20 步取樣即可生成高質量樣本,甚至 10 步也能生成相當不錯的樣本。

原始實現可在 LuChengTHU/dpm-solver 找到。

提示

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

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

DPMSolverSinglestepScheduler

class diffusers.DPMSolverSinglestepScheduler

< >

( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: typing.Optional[numpy.ndarray] = 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 = 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 final_sigmas_type: typing.Optional[str] = 'zero' lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None )

引數

  • 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 節)。
  • 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-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 步)時的取樣。
  • 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
  • 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”。如果設定,模型的輸出包含預測的高斯方差。

DPMSolverSinglestepScheduler 是一個快速的專用高階擴散 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) — 從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前離散時間步。
  • prev_timestep (int) — 擴散鏈中的上一個離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本的當前例項。

返回

torch.Tensor

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

一階DPMSolver(等同於DDIM)的一個步驟。

get_order_list

< >

( num_inference_steps: int )

引數

  • num_inference_steps (int) — 使用預訓練模型生成樣本時使用的擴散步驟數。

計算每個時間步的求解器階數。

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,則使用時間步之間等間距的預設時間步間距策略。如果傳入了timesteps,則num_inference_steps必須為None

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

singlestep_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]) — 當前和後續時間步從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前和後續離散時間步。
  • prev_timestep (int) — 擴散鏈中的上一個離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本的當前例項。

返回

torch.Tensor

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

用於計算從時間 timestep_list[-2] 到時間 prev_timestep 的解的二階單步 DPMSolver 的一步。

singlestep_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]) — 當前和後續時間步從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前和後續離散時間步。
  • prev_timestep (int) — 擴散鏈中的上一個離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本的當前例項。

返回

torch.Tensor

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

用於計算從時間 timestep_list[-3] 到時間 prev_timestep 的解的三階單步 DPMSolver 的一步。

singlestep_dpm_solver_update

< >

( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None order: int = None noise: typing.Optional[torch.Tensor] = None **kwargs ) torch.Tensor

引數

  • model_output_list (List[torch.Tensor]) — 當前和後續時間步從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前和後續離散時間步。
  • prev_timestep (int) — 擴散鏈中的上一個離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本的當前例項。
  • order (int) — 此步驟的求解器階數。

返回

torch.Tensor

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

單步 DPMSolver 的一步。

步驟

< >

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

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本的當前例項。
  • 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.