Diffusers documentation

スケジューラー

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

and get access to the augmented documentation experience

to get started

スケジューラー

スケジューラは、デノイジングループにおいて各ステップでどの程度のノイズを除去するかを指示します。スケジューラによって、速度と品質のトレードオフが異なります。

このガイドでは、スケジューラーを読み込み、そのタイムステップスケジュール、間隔、シグマをカスタマイズする方法を示します。

スケジューラーの選択

チェックポイントのデフォルトから開始してください。速度と品質のトレードオフを変更する必要がある場合のみ、切り替えてください。

スケジューラーの読み込み

Qwen-Image や Flux などの Flow-matching モデルでは、デフォルトとして FlowMatchEulerDiscreteScheduler が同梱されています。意図的に実験を行わない限り、このスケジューラーを維持してください。代替のスケジューラーがチェックポイントと互換性がある場合のみ、from_config() を使用して切り替えてください。

Scheduler は設定のみで構成され、重みテンソルは含まれません。パイプラインの .scheduler 属性にアクセスして、読み込まれた設定を確認できます。

import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.float16, device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.scheduler

読み込まれたパイプラインでスケジューラを切り替える場合は、既存のスケジューラ設定を from_config() に渡して使用し、num_train_timesteps および関連するフィールドが整合した状態を維持してください。FlowMatch チェックポイント(Qwen-Image、Flux など)では、互換性のある代替スケジューラを意図的に実験する場合を除き、FlowMatchEulerDiscreteScheduler を維持してください。

from diffusers import DPMSolverMultistepScheduler

pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)

from_pretrained() を使用して Hub からスケジューラーの設定を読み込み、scheduler を通じて from_pretrained() に渡すこともできます。

from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler

dpm = DPMSolverMultistepScheduler.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler"
)
pipeline = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    scheduler=dpm,
    dtype=torch.float16,
    device_map="cuda",  # or "mps", "xpu", "cpu"
)
pipeline.scheduler

タイムステップスケジュール

Timestep or noise schedule は、デノイジングプロセスにおけるノイズの分布を決定します。スケジュールは線形である場合もあれば、開始時または終了時に集中するものもあります。これはスケジューラーのデフォルト設定から生成されるノイズレベルの事前計算されたシーケンスですが、他のスケジュールを使用するようにカスタマイズすることもできます。

linear (even steps)                 AYS (denser where it matters)
noise                               noise
  ^                                   ^
  | *                                 | *
  |  *                                |   *
  |   *                               |    **
  |    *                              |     ***
  |     *                             |      **
  |      *                            |        *
  |       *                           |          *
  +-----------------> step            +-----------------> step

カスタムのtimestepsは、そのスケジューラのset_timestepsが引数を受け入れる場合にのみ機能します(パイプラインはretrieve_timesteps経由でシグネチャを確認し、受け入れられない場合はValueErrorを送出します)。渡す前に、スケジューラのAPIページまたはset_timestepsのシグネチャを確認してください。

以下の例では、Align Your Steps (AYS)スケジュールを使用しています。このスケジュールは10ステップで高品質な画像を生成でき、生成を大幅に高速化し、計算時間を短縮します。

スケジュールをインポートし、パイプラインのtimesteps引数に渡します。

import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.schedulers import AysSchedules

sampling_schedule = AysSchedules["StableDiffusionXLTimesteps"]
print(sampling_schedule)
# [999, 845, 730, 587, 443, 310, 193, 116, 53, 13]

pipeline = DiffusionPipeline.from_pretrained(
    "SG161222/RealVisXL_V4.0",
    dtype=torch.float16,
    device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
  pipeline.scheduler.config, algorithm_type="sde-dpmsolver++"
)

prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
    prompt=prompt,
    negative_prompt="",
    timesteps=sampling_schedule,
).images[0]
AYSタイムステップスケジュール 10ステップ
等間隔のタイムステップスケジュール 10ステップ
等間隔のタイムステップスケジュール 25ステップ

旧チェックポイントにおけるv-prediction/zero-SNRリスケールについては、Legacy checkpointsを参照してください。

タイムステップ間隔

Timestep spacingとは、スケジュールからサンプリングする特定のステップtを指します。Diffusersでは、以下に示す3つのspacingタイプが提供されています。

間隔戦略間隔の計算例のタイムステップ
leading等間隔のステップ[900, 800, 700, ..., 100, 0]
linspace最初のステップと最後のステップを含み、残りの中間ステップを均等に分割します[1000, 888.89, 777.78, ..., 111.11, 0]
trailing最終ステップを含み、残りの中間ステップを末尾から均等に分割します[999, 899, 799, 699, 599, 499, 399, 299, 199, 99]

スケジューラの timestep_spacing 引数に、間隔戦略を渡します。

trailing戦略は、より少ないステップ数で、より多くの詳細を含む高品質な画像を生成する傾向がありますが、より標準的なステップ数では、品質の差はそれほど明白ではありません。

import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler

pipeline = DiffusionPipeline.from_pretrained(
    "SG161222/RealVisXL_V4.0",
    dtype=torch.float16,
    device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
  pipeline.scheduler.config, timestep_spacing="trailing"
)

prompt = "A cinematic shot of a cute little black cat sitting on a pumpkin at night"
image = pipeline(
    prompt=prompt,
    negative_prompt="",
    num_inference_steps=5,
).images[0]
image
5ステップ後の末尾スペース
5ステップ後の先行スペース

シグマ

Sigmas は、スケジュールによって定義される特定のステップにおけるサンプルのノイズ量を表す指標です。カスタムの sigmas を使用する場合、timesteps はデフォルトのスケジューラ設定ではなく、これらの値から計算されます。

step:   0    1    2    3    4
sigma:  σ0 > σ1 > σ2 > σ3 > σ4 ≈ 0
        high noise  --->  clean sample

カスタム sigmas は、そのスケジューラの set_timesteps が引数を受け入れる場合にのみ機能します(パイプラインは retrieve_timesteps を介してシグネチャを確認し、受け入れられない場合は ValueError を送出します)。渡す前に、スケジューラの API ページまたは set_timesteps のシグネチャを確認してください。

カスタムシグマをパイプラインのsigmas引数に渡します。以下の例では、10ステップAYSスケジュールのsigmasを使用しています。

import torch
from diffusers import DiffusionPipeline, EulerDiscreteScheduler
from diffusers.schedulers import AysSchedules

pipeline = DiffusionPipeline.from_pretrained(
    "SG161222/RealVisXL_V4.0",
    dtype=torch.float16,
    device_map="cuda",  # or "mps", "xpu", "cpu"
)
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)

sigmas = AysSchedules["StableDiffusionXLSigmas"]
prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
    prompt=prompt,
    negative_prompt="",
    sigmas=sigmas,
).images[0]

Karrasシグマ

Karras sigmas は、シグマをシーケンスの中央(構造の再構築が重要となる部分)に高密度に集約し、ノイズの変化の影響が小さいシーケンスの前後ではシグマを少なくすることで、ノイズスケジュールを再サンプリングし、より効率的なサンプリングを実現します。これにより、生成画像の詳細レベルを向上させることができます。

default σ:  *  *  *  *  *  *  *  *     even-ish
Karras σ:   *   * * * * *   *          denser mid, sparser ends
            |---structure---|

スケジューラでuse_karras_sigmas=Trueを設定して有効にします。

import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler

pipeline = DiffusionPipeline.from_pretrained(
    "SG161222/RealVisXL_V4.0",
    dtype=torch.float16,
    device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(
  pipeline.scheduler.config,
  algorithm_type="sde-dpmsolver++",
  use_karras_sigmas=True,
)

prompt = "A cinematic shot of a cute little rabbit wearing a jacket and doing a thumbs up"
image = pipeline(
    prompt=prompt,
    negative_prompt="",
    num_inference_steps=20,
).images[0]
Karrasシグマが有効
Karras sigmas が無効化されました

Karras sigmasをサポートするスケジューラーの一覧については、スケジューラーAPIの概要を参照してください。Karras sigmasで学習されたモデルでのみ使用すべきです。

次のステップ

Update on GitHub