Diffusers documentation

Bắt đầu nhanh

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Bắt đầu nhanh

Diffusers là một thư viện dành cho các nhà phát triển và nhà nghiên cứu, cung cấp API suy luận dễ dàng để tạo hình ảnh, video và âm thanh, cũng như các khối xây dựng để triển khai các quy trình làm việc mới.

Diffusers cung cấp nhiều tối ưu hóa ngay từ đầu, cho phép tải và chạy các mô hình lớn trên các hệ thống có bộ nhớ hạn chế hoặc tăng tốc suy luận.

Quickstart này sẽ cung cấp cho bạn cái nhìn tổng quan về Diffusers và giúp bạn bắt đầu tạo nội dung một cách nhanh chóng.

Trước khi bắt đầu, hãy đảm bảo bạn có tài khoản Hugging Face để sử dụng các model có kiểm soát như Flux. Bạn muốn xây dựng một pipeline tùy chỉnh? Hãy xem Modular Diffusers quickstart và tổng quan.

Làm theo hướng dẫn Cài đặt để cài đặt Diffusers nếu bạn chưa cài đặt.

Lời nhắc cho tác nhân

Dán đoạn này vào tác nhân lập trình của bạn để thiết lập Diffusers cho suy luận.

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 đóng gói các thành phần của một mô hình khuếch tán (text encoder, scheduler, UNet hoặc DiT, và VAE) vào một lớp duy nhất để suy luận. Tải bằng from_pretrained(), sau đó gọi pipeline.

Các đối số trong __call__() như num_inference_steps sẽ thay đổi chất lượng và tốc độ. Để biết chi tiết về việc tải và kết hợp các thành phần, xem Tải pipeline. Để thay đổi scheduler, xem Schedulers.

Các ví dụ bên dưới sử dụng các giá trị tham số mặc định.

text-to-image
text-to-video

Sử dụng .images[0] để truy xuất ảnh đã được tạo.

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 adapters thêm một checkpoint phong cách hoặc chủ đề nhỏ lên trên một pipeline cơ sở. Tải một adapter bằng load_lora_weights(). Một số LoRA cần một cụm từ kích hoạt. Kiểm tra model card của 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]

Lượng tử hóa và tối ưu hóa

Các mô hình lớn thường cần ít bộ nhớ hơn hoặc tốc độ cao hơn. Hãy sử dụng quantization để thu nhỏ trọng số trong bộ nhớ, và compile_repeated_blocks() để tăng tốc các lần generate sau. Để biết về model offloading và các tùy chọn khác, hãy xem Optimize and scale.

Để sử dụng ít bộ nhớ hơn, hãy tải ở 4-bit bằng bitsandbytes.

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]

Để tăng tốc các lần chạy sau, hãy biên dịch các khối lặp lại trên transformer. Lệnh generate đầu tiên sau khi biên dịch là khởi động lạnh và sẽ chậm. Các lệnh generate sau đó sẽ nhanh hơn.

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]

Các bước tiếp theo

Update on GitHub