Buckets:

mishig's picture
|
download
raw
14.6 kB
# DiffusionPipeline
Diffusionモデルは、UNetやdiffusion transformer(DiT)、テキストエンコーダー、変分オートエンコーダー(VAE)、スケジューラーといった複数のコンポーネントで構成されています。[DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline)は、これらのすべてのコンポーネントを単一の使いやすいAPIにまとめつつ、コンポーネントを修正する柔軟性を失いません。
このガイドでは、[DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline)を読み込む方法について説明します。
## パイプラインの読み込み
[DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline)は、`model_index.json`ファイル内のクラス名をスキャンして、[QwenImagePipeline](/docs/diffusers/main/ja/api/pipelines/qwenimage#diffusers.QwenImagePipeline)などのモデルのパイプラインサブクラスのインスタンスを自動的に選択して返す、ベースのパイプラインクラスです。
パイプラインを読み込むには、[from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)にモデルIDを渡します。
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
```
すべてのモデルには、[DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline)を継承する特定のpipelineサブクラスがあります。サブクラスは通常、狭い焦点を持ち、タスクに特化しています。例として、以下の表を参照してください。
| pipeline サブクラス | タスク |
|---|---|
| [QwenImagePipeline](/docs/diffusers/main/ja/api/pipelines/qwenimage#diffusers.QwenImagePipeline) | テキストから画像 |
| [QwenImageImg2ImgPipeline](/docs/diffusers/main/ja/api/pipelines/qwenimage#diffusers.QwenImageImg2ImgPipeline) | image-to-image |
| [QwenImageInpaintPipeline](/docs/diffusers/main/ja/api/pipelines/qwenimage#diffusers.QwenImageInpaintPipeline) | inpaint |
モデルIDを[from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)に渡すことで、サブクラスを直接使用できます。
```py
import torch
from diffusers import QwenImagePipeline
pipeline = QwenImagePipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
```
> [!TIP]
> 単一ファイルモデルの読み込み方法については、[Single file format](./other-formats#single-file-format)のドキュメントを参照してください。
### ローカルパイプライン
Pipelines はローカルでも実行できます。[snapshot_download](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/file_download#huggingface_hub.snapshot_download) を使用してモデルリポジトリをダウンロードします。
```py
from huggingface_hub import snapshot_download
local_dir = snapshot_download(repo_id="Qwen/Qwen-Image")
```
そのパスを[from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)に渡して読み込みます。
```py
import torch
from diffusers import QwenImagePipeline
pipeline = QwenImagePipeline.from_pretrained(
local_dir, dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
```
[from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) メソッドは、ローカルパスを検出すると、Hub からファイルをダウンロードしません。ただし、これにはモデルに対して行われた更新もダウンロード・キャッシュされないという点も含まれます。
## Pipelineのデータ型
`dtype` 引数を使用して [from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) を呼び出すことで、特定のデータ型でモデルを読み込むことができます。これにより、異なる精度で異なるモデルを読み込むことが可能になります。例えば、大規模な Transformer モデルを半精度で読み込むと、必要なメモリ量を削減できます。
`dtype` に、各モデルのデータ型を辞書として渡します。`default` キーを使用して、デフォルトのデータ型を設定します。モデルが辞書に存在せず、`default` も指定されていない場合、フルプレシション(`torch.float32`)で読み込まれます。
```py
import torch
from diffusers import QwenImagePipeline
pipeline = QwenImagePipeline.from_pretrained(
"Qwen/Qwen-Image",
dtype={"transformer": torch.bfloat16, "default": torch.float16},
)
print(pipeline.transformer.dtype, pipeline.vae.dtype)
```
すべてのモデルを同じデータ型で読み込む場合は、辞書を使用する必要はありません。
```py
import torch
from diffusers import QwenImagePipeline
pipeline = QwenImagePipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16
)
print(pipeline.transformer.dtype, pipeline.vae.dtype)
```
## デバイスの配置
`device_map` 引数は、パイプラインをデバイスに配置します。実行時において、Diffusers は `"balanced"`、`"cpu"`、およびお使いのマシンのアクセラレータ文字列(通常は `"cuda"`、`"mps"`、または `"xpu"`)を受け入れます。
| パラメータ | 説明 |
|---|---|
| accelerator 文字列 | そのデバイスにパイプラインを配置します。マシンがサポートするアクセラレーター、例えば`"cuda"`、`"mps"`、または`"xpu"`を指定します。 |
| `"balanced"` | パイプラインのコンポーネントを、表示可能な GPU に分散配置します。デバイスごとの上限が必要な場合は、`max_memory` と併せて使用してください。 |
| `"cpu"` | パイプラインをCPUに配置します。 |
[from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) の `max_memory` 引数を使用して、各デバイスで使用するメモリ量の上限を割り当てます。デフォルトでは、Diffusers は利用可能な最大メモリ量を使用します。
```py
import torch
from diffusers import DiffusionPipeline
max_memory = {0: "16GB", 1: "16GB"}
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image",
dtype=torch.bfloat16,
device_map="balanced",
max_memory=max_memory,
)
```
`hf_device_map` 属性を使用すると、`device_map` にアクセスして表示できます。
```py
print(pipeline.hf_device_map)
# {'transformer': 1, 'vae': 1, 'text_encoder': 0}
```
[reset_device_map()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.reset_device_map) メソッドを使用して、パイプラインの `device_map` をリセットします。`.to()`、[enable_sequential_cpu_offload()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.enable_sequential_cpu_offload)、[enable_model_cpu_offload()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload) のようなメソッドを使用する場合は、この操作が必要です。
```py
pipeline.reset_device_map()
```
## 並列読み込み
大規模モデルは、読み込みを容易にするため、多くの場合、より小さなファイルに[シャード](../optimization/memory#sharded-checkpoints)分割されています。Diffusers は、読み込みプロセスを高速化するために、シャードの並列読み込みをサポートしています。
`HF_ENABLE_PARALLEL_LOADING` を `"YES"` に設定すると、シャードの並列読み込みが有効になります。
`device_map` 引数は、モデルサイズに基づいて大きなメモリ領域を事前確保するために `"cuda"` に設定する必要があります。これにより、メモリアロケータのウォームアップ時に後続の小さなアロケータ呼び出しを多数回避できるため、モデルの読み込み時間が大幅に短縮されます。
```py
import os
import torch
from diffusers import DiffusionPipeline
os.environ["HF_ENABLE_PARALLEL_LOADING"] = "YES"
pipeline = DiffusionPipeline.from_pretrained(
"Wan-AI/Wan2.2-I2V-A14B-Diffusers", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
```
## パイプライン内のモデルの置き換え
[DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline)は柔軟性があり、他のモデルやスケジューラーの読み込みにも対応しています。生成速度や品質の最適化のために異なるスケジューラーを試すことができ、より高性能なモデルに置き換えることもできます。
以下の例では、より安定したVAEのバージョンを使用しています。
```py
import torch
from diffusers import DiffusionPipeline, AutoModel
vae = AutoModel.from_pretrained(
"madebyollin/sdxl-vae-fp16-fix", dtype=torch.float16
)
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
vae=vae,
dtype=torch.float16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
```
## 複数のパイプラインでモデルを再利用する
同じモデルを使用する複数のパイプラインを扱う場合、[from_pipe()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe) メソッドにより、モデルを毎回読み込む代わりに再利用できます。これにより、メモリ使用量を増やすことなく複数のパイプラインを使用できます。
メモリ使用量は、パイプラインの数がいくつであっても、最もメモリ要件の高いパイプラインによって決定されます。
[from_pipe()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe) はメモリ内でモデルを共有します。2つのリポジトリが同一のファイル(同じテキストエンコーダーなど)を含む場合、別々の [from_pretrained()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) 呼び出しはディスク上のファイルを共有します。詳細は [リポジトリ間でファイルを共有する](../installation#share-files-across-repositories) を参照してください。
The example below loads a pipeline and then loads a second pipeline with [from_pipe()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe) to use [perturbed-attention guidance (PAG)](../api/pipelines/pag) to improve generation quality.
> [!WARNING]
> [DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline) は PAG をサポートしていないため、[AutoPipelineForText2Image](/docs/diffusers/main/ja/api/pipelines/auto_pipeline#diffusers.AutoPipelineForText2Image) を使用してください。詳細については、[AutoPipeline](../tutorials/autopipeline) のドキュメントを参照してください。
```py
import torch
from diffusers import AutoPipelineForText2Image
pipeline_sdxl = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.float16, 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
"""
image = pipeline_sdxl(prompt).images[0]
print(f"Max memory reserved: {torch.cuda.max_memory_allocated() / 1024**3:.2f} GB")
# Max memory reserved: 10.47 GB
```
2番目のパイプラインで`enable_pag=True`を設定してPAGを有効にします。2番目のパイプラインは1番目とモデルの重みを共有するため、使用するメモリ量は同じです。
```py
pipeline = AutoPipelineForText2Image.from_pipe(
pipeline_sdxl, enable_pag=True
)
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
"""
image = pipeline(prompt).images[0]
print(f"Max memory reserved: {torch.cuda.max_memory_allocated() / 1024**3:.2f} GB")
# Max memory reserved: 10.47 GB
```
> [!WARNING]
> [from_pipe()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe) で作成されたパイプラインは、同じモデルと *state* を共有します。あるパイプラインでモデルの状態を変更すると、同じモデルを共有する他のすべてのパイプラインにも影響します。
Some methods may not work correctly on pipelines created with [from_pipe()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pipe). For example, [enable_model_cpu_offload()](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.enable_model_cpu_offload) relies on a unique model execution order, which may differ in the new pipeline. To ensure proper functionality, reapply these methods on the new pipeline.
## 次のステップ
パイプラインが読み込まれた後、通常はデノイジングスケジュールの調整、重み形式の切り替え、またはアダプターの付与を行います。
- [Schedulers](./schedulers)では、デノイジングアルゴリズムの切り替えと設定について説明しています。
- [モデル形式](./other-formats)では、GGUF、単一ファイルのチェックポイント、その他の重みのレイアウトについて説明しています。
- [メモリ使用量の削減](../optimization/memory)では、オフローディングやその他のメモリ関連ツールについて説明しています。
- [LoRA](../tutorials/using_peft_for_inference)では、パイプラインにアダプターを読み込む方法について説明しています。
- [Legacy checkpoints](./legacy_checkpoints#safety-checker)では、旧バージョンのStable Diffusionチェックポイント向けのセーフティチェッカーについて説明しています。

Xet Storage Details

Size:
14.6 kB
·
Xet hash:
5fad5f7dd90afaf7b79b3dc33c90276b4e4e72d6d7cfe70ab3aa82730bd8fdd1

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