Diffusers 文件
潛在一致性模型多步排程器
並獲得增強的文件體驗
開始使用
潛在一致性模型多步排程器
概覽
多步和單步排程器(演算法 3),與潛在一致性模型一同在 Simian Luo、Yiqin Tan、Longbo Huang、Jian Li 和 Hang Zhao 的論文 潛在一致性模型:透過少量步驟推理合成高解析度影像 中提出。此排程器應該能夠在 1-8 步內從 LatentConsistencyModelPipeline 生成良好的樣本。
LCMScheduler
class diffusers.LCMScheduler
< 源 >( num_train_timesteps: int = 1000 beta_start: float = 0.00085 beta_end: float = 0.012 beta_schedule: str = 'scaled_linear' trained_betas: typing.Union[numpy.ndarray, typing.List[float], NoneType] = None original_inference_steps: int = 50 clip_sample: bool = False clip_sample_range: float = 1.0 set_alpha_to_one: bool = True steps_offset: int = 0 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 timestep_spacing: str = 'leading' timestep_scaling: float = 10.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 序列的對映。可選擇linear、scaled_linear或squaredcos_cap_v2。 - trained_betas (
np.ndarray, 可選) — 直接向建構函式傳遞 beta 陣列,以繞過beta_start和beta_end。 - original_inference_steps (
int, 可選, 預設為 50) — 用於生成線性間隔時間步排程器的預設推理步數,最終將從該排程器中均勻間隔地取num_inference_steps個時間步,以形成最終的時間步排程器。 - clip_sample (
bool, 預設為True) — 剪輯預測樣本以確保數值穩定性。 - clip_sample_range (
float, 預設為 1.0) — 樣本剪輯的最大幅度。僅在clip_sample=True時有效。 - set_alpha_to_one (
bool, 預設為True) — 每個擴散步驟都使用該步驟和前一步的 alphas 乘積值。對於最後一步,沒有前一個 alpha。當此選項為True時,前一個 alpha 乘積固定為1,否則它使用步驟 0 的 alpha 值。 - steps_offset (
int, 預設為 0) — 新增到推理步驟的偏移量,某些模型家族需要此偏移量。 - 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時有效。 - timestep_spacing (
str, 預設為"leading") — 時間步的縮放方式。更多資訊請參閱 常用擴散噪聲排程器和樣本步驟存在缺陷 的表 2。 - timestep_scaling (
float, 預設為 10.0) — 計算一致性模型邊界條件c_skip和c_out時,時間步將被乘以的因子。增加此值將減小近似誤差(儘管預設值10.0時的近似誤差已經很小)。 - rescale_betas_zero_snr (
bool, 預設為False) — 是否重新縮放 beta 值以使其具有零終端信噪比 (SNR)。這使得模型能夠生成非常明亮和黑暗的樣本,而不是將其限制在亮度適中的樣本。與--offset_noise有鬆散的關係。
LCMScheduler 擴充套件了去噪擴散機率模型 (DDPM) 中引入的去噪過程,並增加了非馬爾可夫引導。
此模型繼承自 SchedulerMixin 和 ConfigMixin。~ConfigMixin 負責儲存排程器 __init__ 函式中傳遞的所有配置屬性,例如 num_train_timesteps。可以透過 scheduler.config.num_train_timesteps 訪問它們。SchedulerMixin 透過 SchedulerMixin.save_pretrained() 和 from_pretrained() 函式提供通用的載入和儲存功能。
scale_model_input
< 源 >( sample: Tensor timestep: typing.Optional[int] = None ) → torch.Tensor
確保與需要根據當前時間步縮放去噪模型輸入的排程器互換使用。
設定排程器的起始索引。此函式應在推理之前從管道中執行。
set_timesteps
< 源 >( num_inference_steps: typing.Optional[int] = None device: typing.Union[str, torch.device] = None original_inference_steps: typing.Optional[int] = None timesteps: typing.Optional[typing.List[int]] = None strength: int = 1.0 )
引數
- num_inference_steps (
int, 可選) — 使用預訓練模型生成樣本時使用的擴散步數。如果使用此引數,則timesteps必須為None。 - device (
str或torch.device, 可選) — 時間步應移動到的裝置。如果為None,則時間步不移動。 - original_inference_steps (
int, 可選) — 原始推理步數,將用於生成線性間隔的時間步排程器(這與標準diffusers實現不同)。然後,我們將從該排程器中均勻間隔地取num_inference_steps個時間步,並將其用作最終的時間步排程器。如果未設定,則預設為original_inference_steps屬性。 - timesteps (
List[int], 可選) — 用於支援時間步之間任意間隔的自定義時間步。如果為None,則使用訓練/蒸餾時間步排程器上時間步之間等距的預設時間步間隔策略。如果傳遞了timesteps,則num_inference_steps必須為None。
設定用於擴散鏈的離散時間步(在推理之前執行)。
步驟
< 源 >( model_output: Tensor timestep: int sample: Tensor generator: typing.Optional[torch._C.Generator] = None return_dict: bool = True ) → ~schedulers.scheduling_utils.LCMSchedulerOutput 或 tuple
引數
- model_output (
torch.Tensor) — 從學習到的擴散模型直接輸出。 - timestep (
float) — 擴散鏈中的當前離散時間步。 - sample (
torch.Tensor) — 擴散過程建立的樣本的當前例項。 - generator (
torch.Generator, 可選) — 隨機數生成器。 - return_dict (
bool, 可選, 預設為True) — 是否返回LCMSchedulerOutput或tuple。
返回
~schedulers.scheduling_utils.LCMSchedulerOutput 或 tuple
如果 return_dict 為 True,則返回 LCMSchedulerOutput,否則返回一個元組,其中第一個元素是樣本張量。
透過逆轉 SDE 預測前一個時間步的樣本。此函式從學習到的模型輸出(通常是預測的噪聲)傳播擴散過程。