Buckets:

hf-doc-build/doc / diffusers /v0.43.0 /ja /stable_diffusion.md
mishig's picture
|
download
raw
6.67 kB
# 基本的なパフォーマンス
Diffusion は計算負荷の高いランダムなプロセスです。望ましい出力を得るまで [DiffusionPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline) を複数回実行する必要がある場合があります。そのため、より迅速に反復するためには、生成速度とメモリ使用量を慎重にバランスさせることが重要です。
このガイドでは、[DiffusionPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline) を使用する際の基本的なパフォーマンスに関するヒントをいくつか紹介します。より詳細なパフォーマンスに関するガイドについては、推論の最適化セクションのドキュメント、例えば [Accelerate による推論](./optimization/fp16) や [メモリ使用量の削減](./optimization/memory) を参照してください。
## メモリ使用量
メモリ使用量の削減は、間接的に生成を高速化し、モデルをデバイスに収めるのに役立ちます。
[enable_model_cpu_offload()](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload) メソッドは、モデルが使用されていないときに CPU へ移動して GPU メモリを節約します。
```py
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`を設定すると、パイプラインが半精度で実行されます。データ型の精度を下げると、低精度での計算に要する時間が短くなるため、速度が向上します。
```py
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](/docs/diffusers/v0.43.0/ja/api/schedulers/multistep_dpm_solver#diffusers.DPMSolverMultistepScheduler)のように、約20〜25ステップで済む高速なスケジューラーを使用します。
- `num_inference_steps` をより低い値に設定してください。推論ステップ数を減らすと、全体の計算量が減少します。ただし、これにより生成品質が低下する可能性があります。
```py
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などの単語を用いたネガティブプロンプトにより、望ましくない特徴をモデルが生成しないよう誘導することも有効です。
```py
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](./using-diffusers/weighted_prompts)ドキュメントを参照してください。
- 生成速度を犠牲にして品質を高める [HeunDiscreteScheduler](/docs/diffusers/v0.43.0/ja/api/schedulers/heun#diffusers.HeunDiscreteScheduler) や [LMSDiscreteScheduler](/docs/diffusers/v0.43.0/ja/api/schedulers/lms_discrete#diffusers.LMSDiscreteScheduler) など、別のスケジューラーを試してください。
```py
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](./optimization/memory#group-offloading)や[regional compilation](./optimization/fp16#regional-compilation)など、より高度で強力な最適化機能を提供しています。パフォーマンスを最大限に引き出す方法については、Inference Optimizationセクションをご覧ください。

Xet Storage Details

Size:
6.67 kB
·
Xet hash:
fba48feb860493556f8f91850c98e16887f1eb09a147f3285b28bba5f19e885c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.