Diffusers documentation
基本的なパフォーマンス
基本的なパフォーマンス
Diffusion は計算負荷の高いランダムなプロセスです。望ましい出力を得るまで DiffusionPipeline を複数回実行する必要がある場合があります。そのため、より迅速に反復するためには、生成速度とメモリ使用量を慎重にバランスさせることが重要です。
このガイドでは、DiffusionPipeline を使用する際の基本的なパフォーマンスに関するヒントをいくつか紹介します。より詳細なパフォーマンスに関するガイドについては、推論の最適化セクションのドキュメント、例えば Accelerate による推論 や メモリ使用量の削減 を参照してください。
メモリ使用量
メモリ使用量の削減は、間接的に生成を高速化し、モデルをデバイスに収めるのに役立ちます。
enable_model_cpu_offload() メソッドは、モデルが使用されていないときに CPU へ移動して GPU メモリを節約します。
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.bfloat16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.enable_model_cpu_offload()
prompt = """
cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California
highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain
"""
pipeline(prompt).images[0]
print(f"Max memory reserved: {torch.cuda.max_memory_allocated() / 1024**3:.2f} GB")推論速度
Denoising は拡散モデルにおける計算負荷が最も高いプロセスです。このプロセスを最適化する方法は、推論速度の高速化につながります。高速化のために、以下の方法を試してください。
device_map="cuda"を追加して、パイプラインを GPU に配置します。GPU などのアクセラレーター上にモデルを配置すると、並列で計算を実行できるため、速度が向上します。dtype=torch.bfloat16を設定すると、パイプラインが半精度で実行されます。データ型の精度を下げると、低精度での計算に要する時間が短くなるため、速度が向上します。
import torch
import time
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.bfloat16,
device_map="cuda
)- DPMSolverMultistepSchedulerのように、約20〜25ステップで済む高速なスケジューラーを使用します。
num_inference_stepsをより低い値に設定してください。推論ステップ数を減らすと、全体の計算量が減少します。ただし、これにより生成品質が低下する可能性があります。
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
prompt = """
cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California
highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain
"""
start_time = time.perf_counter()
image = pipeline(prompt).images[0]
end_time = time.perf_counter()
print(f"Image generation took {end_time - start_time:.3f} seconds")生成品質
多くの最新の拡散モデルは、そのままでも高品質な画像を生成できます。ただし、以下の方法を試すことで、生成品質をさらに向上させることができます。
より詳細で記述的なプロンプトを試してください。画像の媒体、被写体、スタイル、美的感覚などの詳細を含めるとよいです。また、low qualityやblurryなどの単語を用いたネガティブプロンプトにより、望ましくない特徴をモデルが生成しないよう誘導することも有効です。
import torch from diffusers import DiffusionPipeline pipeline = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu" ) prompt = """ cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain """ negative_prompt = "low quality, blurry, ugly, poor details" pipeline(prompt, negative_prompt=negative_prompt).images[0]より良いプロンプトの作成方法の詳細については、Prompt techniquesドキュメントを参照してください。
生成速度を犠牲にして品質を高める HeunDiscreteScheduler や LMSDiscreteScheduler など、別のスケジューラーを試してください。
import torch from diffusers import DiffusionPipeline, HeunDiscreteScheduler pipeline = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu" ) pipeline.scheduler = HeunDiscreteScheduler.from_config(pipeline.scheduler.config) prompt = """ cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain """ negative_prompt = "low quality, blurry, ugly, poor details" pipeline(prompt, negative_prompt=negative_prompt).images[0]
次のステップ
Diffusersでは、group-offloadingやregional compilationなど、より高度で強力な最適化機能を提供しています。パフォーマンスを最大限に引き出す方法については、Inference Optimizationセクションをご覧ください。
Update on GitHub