Diffusers 文件

UniPCMultistepScheduler

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

UniPCMultistepScheduler

UniPCMultistepScheduler 是一個無需訓練的框架,專為擴散模型的快速取樣而設計。它由 Wenliang Zhao、Lujia Bai、Yongming Rao、Jie Zhou、Jiwen Lu 在 UniPC: A Unified Predictor-Corrector Framework for Fast Sampling of Diffusion Models 中提出。

它包含一個校正器(UniC)和一個預測器(UniP),它們共享統一的分析形式並支援任意階數。UniPC 在設計上是模型無關的,支援無條件/條件取樣上的畫素空間/潛在空間 DPM。它還可以應用於噪聲預測和資料預測模型。校正器 UniC 也可以在任何現成求解器之後應用,以提高精度階數。

論文摘要如下:

擴散機率模型(DPMs)在高解析度影像合成方面表現出非常有前途的能力。然而,從預訓練的 DPMs 取樣由於去噪網路的多次評估而耗時,這使得加速 DPMs 的取樣變得越來越重要。儘管最近在設計快速取樣器方面取得了進展,但現有方法在許多偏好更少步驟(例如,<10)的應用中仍無法生成令人滿意的影像。在本文中,我們開發了一個統一校正器(UniC),可以在任何現有 DPM 取樣器之後應用,以在不增加額外模型評估的情況下提高精度階數,並作為一個副產品推匯出一個支援任意階數的統一預測器(UniP)。結合 UniP 和 UniC,我們提出了一個名為 UniPC 的統一預測-校正框架,用於 DPMs 的快速取樣,它具有適用於任何階數的統一分析形式,並且可以顯著提高取樣質量,尤其是在極少步驟中。我們透過廣泛的實驗評估了我們的方法,包括使用畫素空間和潛在空間 DPMs 的無條件和條件取樣。我們的 UniPC 可以在 CIFAR10(無條件)上以僅 10 次函式評估實現 3.87 FID,在 ImageNet 256×256(條件)上實現 7.51 FID。程式碼可在此處獲取:this https URL

提示

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

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

UniPCMultistepScheduler

diffusers.UniPCMultistepScheduler

< >

( 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 predict_x0: bool = True solver_type: str = 'bh2' lower_order_final: bool = True disable_corrector: typing.List[int] = [] solver_p: SchedulerMixin = None 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 timestep_spacing: str = 'linspace' steps_offset: int = 0 final_sigmas_type: typing.Optional[str] = 'zero' 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, 可選) — 直接向建構函式傳遞一個 beta 陣列,以繞過 beta_startbeta_end
  • solver_order (int, 預設為 2) — UniPC 階數,可以是任何正整數。由於 UniC,有效精度階數為 solver_order + 1。建議在引導取樣中使用 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=Truepredict_x0=True 時有效。
  • predict_x0 (bool, 預設為 True) — 是否在預測的 x0 上使用更新演算法。
  • solver_type (str, 預設為 bh2) — UniPC 的求解器型別。建議在步數 < 10 的無條件取樣中使用 bh1,否則使用 bh2
  • lower_order_final (bool, 預設為 True) — 是否在最後幾個步驟中使用低階求解器。僅適用於 < 15 推理步驟。這可以穩定 DPMSolver 的取樣,尤其是在步驟 <= 10 的情況下。
  • disable_corrector (list, 預設為 []) — 決定在哪個步驟停用校正器,以緩解 epsilon_theta(x_t, c)epsilon_theta(x_t^c, c) 之間的錯位,這會影響大引導尺度的收斂性。校正器通常在最初幾個步驟中停用。
  • solver_p (SchedulerMixin, 預設為 None) — 如果指定,則演算法變為 solver_p + UniC 的任何其他排程器。
  • 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
  • timestep_spacing (str, 預設為 "linspace") — 時間步長的縮放方式。更多資訊請參閱 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。
  • steps_offset (int, 預設為 0) — 新增到推理步驟的偏移量,某些模型系列需要。
  • final_sigmas_type (str, 預設為 "zero") — 取樣過程中噪聲排程的最終 sigma 值。如果為 "sigma_min",則最終 sigma 與訓練排程中的最後一個 sigma 相同。如果為 zero,則最終 sigma 設定為 0。
  • rescale_betas_zero_snr (bool, 預設為 False) — 是否重新縮放 beta 以使最終信噪比為零。這使得模型能夠生成非常明亮和非常暗的樣本,而不是將其限制在亮度適中的樣本。與 --offset_noise loosely related。

UniPCMultistepScheduler 是一個無需訓練的框架,專為擴散模型的快速取樣而設計。

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

convert_model_output

< >

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

引數

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

返回

torch.Tensor

轉換後的模型輸出。

將模型輸出轉換為 UniPC 演算法所需的相應型別。

multistep_uni_c_bh_update

< >

( this_model_output: Tensor *args last_sample: Tensor = None this_sample: Tensor = None order: int = None **kwargs ) torch.Tensor

引數

  • this_model_output (torch.Tensor) — x_t 處的模型輸出。
  • this_timestep (int) — 當前時間步 t
  • last_sample (torch.Tensor) — 上一個預測器 `x_{t-1}` 生成的樣本。
  • this_sample (torch.Tensor) — 上一個預測器 `x_{t}` 生成的樣本。
  • order (int) — 此步驟中 UniC-p 的 `p`。有效精度階數應為 `order + 1`。

返回

torch.Tensor

當前時間步的校正樣本張量。

UniC(B(h) 版本)的一個步驟。

multistep_uni_p_bh_update

< >

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

引數

  • model_output (torch.Tensor) — 從當前時間步的已學習擴散模型直接輸出。
  • prev_timestep (int) — 擴散鏈中的上一個離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本當前例項。
  • order (int) — 此時間步 UniP 的階數(對應於 UniPC-p 中的 `p`)。

返回

torch.Tensor

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

UniP (B(h) 版本) 的一個步驟。或者,如果指定,則使用 `self.solver_p`。

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 device: typing.Union[str, torch.device] = None )

引數

  • num_inference_steps (int) — 使用預訓練模型生成樣本時使用的擴散步數。
  • device (strtorch.device, 可選) — 時間步應移動到的裝置。如果為 `None`,則時間步不移動。

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

步驟

< >

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

引數

  • model_output (torch.Tensor) — 從已學習擴散模型直接輸出。
  • timestep (int) — 擴散鏈中的當前離散時間步。
  • sample (torch.Tensor) — 擴散過程建立的樣本當前例項。
  • return_dict (bool) — 是否返回 SchedulerOutputtuple

返回

排程器輸出tuple

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

透過反轉 SDE 從上一個時間步預測樣本。此函式使用多步 UniPC 傳播樣本。

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.