Diffusers documentation
クイックスタート
クイックスタート
Diffusersは、開発者や研究者向けのライブラリであり、画像、動画、音声の生成のためのシンプルな推論APIと、新しいワークフローを実装するための基盤となる要素を提供します。
Diffusers は、メモリが限られた環境で大型モデルを読み込み・実行できるようにする、または推論を高速化するための多くの最適化を標準で提供しています。
このクイックスタートでは、Diffusersの概要を説明し、すぐに生成を開始できるようにします。
開始する前に、Flux などのゲート付きモデルを使用するには、Hugging Face の アカウント が必要であることを確認してください。カスタムパイプラインを構築する場合は、Modular Diffusers クイックスタート と 概要 を参照してください。
インストールされていない場合は、Installationガイドに従って Diffusers をインストールしてください。
エージェントプロンプト
これをコーディングエージェントに貼り付けて、Diffusers を推論用にセットアップしてください。
Help me get set up with Hugging Face Diffusers for inference. 1. Install Diffusers for my environment with `uv pip install "diffusers[torch]"`. 2. If I need gated Hub models, help me authenticate to the Hugging Face Hub. 3. Install Diffusers coding agent skills with `diffusers-cli skills add diffusers-cli`. Pass `--cursor`, `--claude`, or `--codex` if auto-detect fails. Optionally pass `--all` to install every skill in the registry. 4. Run a first text-to-image with DiffusionPipeline or `diffusers-cli run`, using a small or current Quickstart model and the right `device_map` for my machine. 5. Ask what I want next and point me at the matching docs.
DiffusionPipeline
DiffusionPipeline packages the pieces of a diffusion model (text encoder, scheduler, UNet or DiT, and VAE) into one class for inference. Load with from_pretrained(), then call the pipeline.
__call__() の引数である num_inference_steps などは、品質と速度に影響します。読み込みの詳細やコンポーネントの組み合わせについては、パイプラインの読み込みを参照してください。スケジューラーの切り替えについては、スケジューラーを参照してください。
以下の例では、デフォルトの引数値を使用しています。
生成された画像出力にアクセスするには、.images[0] を使用します。
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", 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
"""
pipeline(prompt).images[0].frames[0] を使用して生成された動画出力にアクセスし、export_to_video() を使用して動画を保存します。
import torch
from diffusers import AutoencoderKLWan, DiffusionPipeline
from diffusers.utils import export_to_video
vae = AutoencoderKLWan.from_pretrained(
"Wan-AI/Wan2.2-T2V-A14B-Diffusers",
subfolder="vae",
dtype=torch.float32
)
pipeline = DiffusionPipeline.from_pretrained(
"Wan-AI/Wan2.2-T2V-A14B-Diffusers",
vae=vae,
dtype=torch.bfloat16,
device_map="cuda" # or "mps", "xpu", "cpu"
)
prompt = """
Cinematic video of a sleek cat lounging on a colorful inflatable in a crystal-clear turquoise pool in Palm Springs,
sipping a salt-rimmed margarita through a straw. Golden-hour sunlight glows over mid-century modern homes and swaying palms.
Shot in rich Sony a7S III: with moody, glamorous color grading, subtle lens flares, and soft vintage film grain.
Ripples shimmer as a warm desert breeze stirs the water, blending luxury and playful charm in an epic, gorgeously composed frame.
"""
video = pipeline(prompt=prompt, num_frames=81, num_inference_steps=40).frames[0]
export_to_video(video, "output.mp4", fps=16)LoRA
LoRA アダプターは、ベースのパイプラインの上に小さなスタイルや被体のチェックポイントを追加します。load_lora_weights() を使用して読み込みます。一部の LoRA ではトリガーフレーズが必要です。LoRA のモデルカードを確認してください。
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.load_lora_weights(
"threecrowco/VolkClipartQwen",
weight_name="pytorch_lora_weights.safetensors",
)
prompt = """
Volk clipart style drawing of a cat sipping a margarita in a pool in Palm Springs, California, flat colors, bold outlines, simple shapes
"""
pipeline(prompt).images[0]量子化と最適化
大規模モデルでは、メモリ使用量の削減や高速化のニーズが高くなります。メモリ上の重みを縮小するには量子化を、後続の生成を高速化するにはcompile_repeated_blocks()を使用してください。モデルのオフロードやその他のオプションについては、最適化とスケーリングを参照してください。
メモリ使用量を削減するには、bitsandbytes を使用して 4 ビットで読み込みます。
import torch
from diffusers import DiffusionPipeline
from diffusers.quantizers import PipelineQuantizationConfig
quant_config = PipelineQuantizationConfig(
quant_backend="bitsandbytes_4bit",
quant_kwargs={"load_in_4bit": True, "bnb_4bit_quant_type": "nf4", "bnb_4bit_compute_dtype": torch.bfloat16},
components_to_quantize=["transformer", "text_encoder"],
)
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image",
dtype=torch.bfloat16,
quantization_config=quant_config,
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
"""
pipeline(prompt).images[0]トランスフォーマーの繰り返しブロックをコンパイルして、以降の実行を高速化します。コンパイル直後の最初の generate はコールドスタートとなり、処理が遅くなります。その後の generate は高速になります。
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
pipeline.transformer.compile_repeated_blocks(fullgraph=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
"""
pipeline(prompt).images[0]次のステップ
- 推論 — パイプライン、プロンプト、アダプター
- 最適化とスケーリング — メモリ、速度、量子化、サービング
- Modular Diffusers — 組み合わせ可能なブロックとカスタムパイプライン
- 学習とファインチューニング — 学習スクリプトとアダプター
- CLI — コマンドラインから生成