Diffusers documentation

クイックスタート

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

クイックスタート

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]

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]

次のステップ

Update on GitHub