Diffusers documentation

Latent Consistency Model Multistep Scheduler

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Latent Consistency Model Multistep Scheduler

概要

Simian Luo、Yiqin Tan、Longbo Huang、Jian Li、Hang Zhao による論文 Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference で、latent consistency models とともに導入された Multistep and onestep scheduler (Algorithm 3) です。 この scheduler は、LatentConsistencyModelPipeline から 1〜8 ステップで良好なサンプルを生成できるはずです。

LCMScheduler

class diffusers.LCMScheduler

< >

( num_train_timesteps: int = 1000beta_start: float = 0.00085beta_end: float = 0.012beta_schedule: str = 'scaled_linear'trained_betas: numpy.ndarray | list[float] | None = Noneoriginal_inference_steps: int = 50clip_sample: bool = Falseclip_sample_range: float = 1.0set_alpha_to_one: bool = Truesteps_offset: int = 0prediction_type: str = 'epsilon'thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995sample_max_value: float = 1.0timestep_spacing: str = 'leading'timestep_scaling: float = 10.0rescale_betas_zero_snr: bool = False )

パラメータ

  • num_train_timesteps (int、デフォルトは 1000) — モデルを学習するための拡散ステップ数。
  • beta_start (float、デフォルトは 0.00085) — 推論の開始時の beta 値です。
  • beta_end (float、デフォルト値は 0.012) — 最終的な beta 値。
  • beta_schedule (str、デフォルトは "scaled_linear") — ベータスケジュールです。モデルのステップ処理において、ベータの範囲からベータのシーケンスへの変換を定義します。linear、scaled_linear、または squaredcos_cap_v2 から選択してください。
  • trained_betas (np.ndarray または list[float]、省略可) — beta_start と beta_end をバイパスして、ベータの配列を直接コンストラクタに渡すことができます。
  • original_inference_steps (int、デフォルト値は 50) — 線形間隔のタイムステップスケジュールを生成するために使用されるデフォルトの推論ステップ数です。最終的なタイムステップスケジュールを形成するために、このスケジュールから等間隔の num_inference_steps 個のタイムステップを抽出します。
  • clip_sample (bool、デフォルトは False) — 数値の安定性を確保するために、予測されたサンプルをクリップします。
  • clip_sample_range (float, デフォルトは 1.0) — サンプルクリッピングの最大振幅です。clip_sample=True の場合のみ有効です。
  • set_alpha_to_one (bool、デフォルトはTrue) — 各拡散ステップでは、そのステップと直前のステップのアルファの積の値が使用されます。最終ステップでは直前のアルファが存在しません。このオプションがTrueの場合、直前のアルファの積は1に固定され、それ以外の場合はステップ0のアルファの値が使用されます。
  • steps_offset (int、デフォルトは 0) — 一部のモデルファミリーで必要とされる、推論ステップに追加されるオフセットです。
  • prediction_type (str, defaults to "epsilon") — Prediction type of the scheduler function; can be epsilon (predicts the noise of the diffusion process), sample (directly predicts the noisy sample) or v_prediction` (see section 2.4 of Imagen Video paper).
  • thresholding (bool、デフォルトは False) — 「動的閾値処理」メソッドを使用するかどうか。Stable Diffusion などの潜在空間拡散モデルには不向きです。
  • dynamic_thresholding_ratio (float、デフォルトは0.995) — 動的閾値化手法の比率。thresholding=Trueの場合にのみ有効です。
  • sample_max_value (float, デフォルトは 1.0) — 動的閾値処理の閾値です。thresholding=True の場合のみ有効です。
  • timestep_spacing (str, defaults to "leading") — The way the timesteps should be scaled. Refer to Table 2 of the Common Diffusion Noise Schedules and Sample Steps are Flawed for more information.
  • timestep_scaling (float、デフォルト値は 10.0) — consistency model の境界条件である c_skip および c_out を計算する際に、timestep に乗算される係数です。この値を大きくすると近似誤差は小さくなります(ただし、デフォルト値の 10.0 における近似誤差はすでにかなり小さいです)。
  • rescale_betas_zero_snr (bool、デフォルトは False) — betas を再スケーリングして、終端 SNR をゼロにするかどうかを指定します。これにより、モデルは中間の明るさに限定されず、非常に明るく暗いサンプルを生成できるようになります。--offset_noise とゆるく関連しています。

LCMScheduler は、非マルコフ的ガイダンスを用いて、DDPM(denoising diffusion probabilistic models)で導入されたデノイジング手順を拡張します。

このモデルは SchedulerMixin と ConfigMixin を継承しています。~ConfigMixin は、スケジューラの __init__ 関数に渡されるすべての設定属性(num_train_timesteps など)の保存を担当します。これらは scheduler.config.num_train_timesteps を介してアクセスできます。SchedulerMixin は、SchedulerMixin.save_pretrained() および from_pretrained() 関数を通じて、一般的な読み込みおよび保存機能を提供します。

add_noise

< >

( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor

パラメータ

  • original_samples (torch.Tensor) — ノイズが加えられる元のサンプルです。
  • noise (torch.Tensor) — サンプルに追加するノイズ。
  • timesteps (torch.IntTensor) — 各サンプルのノイズレベルを示すタイムステップです。

戻り値

torch.Tensor

ノイズ付きサンプル。

各タイムステップにおけるノイズの大きさに従って、元のサンプルにノイズを追加します(これはフォワード拡散プロセスです)。

get_scalings_for_boundary_condition_discrete

< >

( timestep: int ) → tuple[float, float]

パラメータ

  • timestep (int) — スケーリングを計算する離散タイムステップ。

戻り値

tuple[float, float]

c_skip(入力サンプルのスケーリング)とc_out(予測されたノイズ除去済みサンプルのスケーリング)を含むタプル。

Latent Consistency Model で使用される、与えられた離散タイムステップに対する境界条件のスケーリング(c_skip および c_out)を計算します。

get_velocity

< >

( sample: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor

パラメータ

  • sample (torch.Tensor) — 入力サンプルです。
  • noise (torch.Tensor) — ノイズテンソルです。
  • timesteps (torch.IntTensor) — 速度計算に使用するタイムステップです。

戻り値

torch.Tensor

計算された速度。

速度の式に従って、サンプルとノイズから速度予測を計算します。

index_for_timestep

< >

( timestep: typing.Union[float, torch.Tensor]schedule_timesteps: typing.Optional[torch.Tensor] = None ) → int

パラメータ

  • timestep (float または torch.Tensor) — スケジュール内で検索するタイムステップの値。
  • schedule_timesteps (torch.Tensor、任意) — 検索対象となるタイムステップスケジュールです。None の場合、self.timesteps が使用されます。

戻り値

int

スケジュールにおけるタイムステップのインデックスです。最初のステップでは、スケジュールの途中で開始する場合(画像から画像への生成など)にシグマをスキップすることを避けるため、複数の一致が存在する場合は2番目のインデックスを返します。

与えられたタイムステップのタイムステップスケジュールにおけるインデックスを特定します。

previous_timestep

< >

( timestep ) → int or torch.Tensor

パラメータ

  • timestep (int) — 現在のタイムステップ。

戻り値

int または torch.Tensor

直前のタイムステップ。

拡散チェーンにおける直前のタイムステップを計算します。

scale_model_input

< >

( sample: Tensortimestep: int | None = None ) → torch.Tensor

パラメータ

  • sample (torch.Tensor) — 入力サンプルです。
  • timestep (int, 任意) — 拡散チェーンにおける現在のタイムステップ。

戻り値

torch.Tensor

スケーリングされた入力サンプル。

現在のタイムステップに応じて、デノイジングモデルの入力をスケーリングする必要があるスケジューラーとの相互運用性を保証します。

set_begin_index

< >

( begin_index: int = 0 )

パラメータ

  • begin_index (int、デフォルトは0) — スケジューラの開始インデックスです。

スケジューラの開始インデックスを設定します。この関数は、推論の前にパイプラインから実行する必要があります。

set_timesteps

< >

( num_inference_steps: int | None = Nonedevice: typing.Union[str, torch.device, NoneType] = Noneoriginal_inference_steps: int | None = Nonetimesteps: list[int] | None = Nonestrength: float = 1.0 ) → None

パラメータ

  • num_inference_steps (int, 省略可) — 事前学習済みモデルでサンプルを生成する際に使用する拡散ステップ数。使用する場合、timesteps は None である必要があります。
  • device (str または torch.device、省略可) — timesteps を移動先とするデバイスです。None の場合、timesteps は移動されません。
  • original_inference_steps (int, 任意) — 線形間隔のタイムステップスケジュールを生成するために使用される、元の推論ステップ数です(これは標準的な diffusers の実装とは異なります)。次に、このスケジュールからインデックスの観点で等間隔に num_inference_steps 個のタイムステップを抽出し、それを最終的なタイムステップスケジュールとして使用します。設定されていない場合、original_inference_steps 属性の値がデフォルトとして使用されます。
  • timesteps (list[int], 任意) — タイムステップ間の間隔を任意に設定するために使用するカスタムタイムステップ。None の場合、学習/蒸留のタイムステップスケジュールにおいて、タイムステップ間に等間隔のデフォルトのタイムステップ間隔戦略が使用されます。timesteps が渡された場合、num_inference_steps は None でなければなりません。
  • strength (float、デフォルト値は 1.0) — 部分タイムステップスケジュールを生成するために使用される強度係数(画像から画像への生成など)。 1.0 の場合、完全なスケジュールが使用されます。

戻り値

None

拡散チェーンで使用される離散タイムステップを設定します(推論の実行前に実行する必要があります)。

ステップ

< >

( model_output: Tensortimestep: intsample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → LCMSchedulerOutput or tuple

パラメータ

  • model_output (torch.Tensor) — 学習済み拡散モデルからの直接の出力です。
  • timestep (int) — 拡散チェーンにおける現在の離散タイムステップ。
  • sample (torch.Tensor) — 拡散プロセスによって生成されたサンプルの現在のインスタンス。
  • generator (torch.Generator、任意) — 再現可能なサンプリング用の乱数生成器。
  • return_dict (bool、デフォルトは True) — LCMSchedulerOutput を返すか、tuple を返すかを指定します。

戻り値

LCMSchedulerOutput または tuple

True の場合、LCMSchedulerOutput が返され、それ以外の場合は、最初の要素がサンプルテンソルであるタプルが返されます。

前のタイムステップのサンプルを、SDEを逆方向に適用することで予測します。この関数は、学習済みモデルの出力(多くの場合、予測されたノイズ)から拡散過程を伝播させます。

Update on GitHub