Diffusers documentation

AutoPipeline

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

and get access to the augmented documentation experience

to get started

AutoPipeline

AutoPipeline 是一种按任务和模型选择的pipeline,会根据任务自动选择正确的pipeline子类。这样你就不用提前知道具体的pipeline子类名称,也能加载不同类型的pipeline。

这和 DiffusionPipeline 不同。后者是只按模型选择的pipeline,会根据模型自动选择pipeline子类。

AutoPipelineForImage2Image 会返回某个特定的pipeline子类,例如 StableDiffusionXLImg2ImgPipeline,它只能用于 image-to-image 任务。

import torch
from diffusers import AutoPipelineForImage2Image

pipeline = AutoPipelineForImage2Image.from_pretrained(
  "RunDiffusion/Juggernaut-XL-v9", torch_dtype=torch.bfloat16, device_map="cuda",
)
print(pipeline)
"StableDiffusionXLImg2ImgPipeline {
  "_class_name": "StableDiffusionXLImg2ImgPipeline",
  ...
"

如果用同一个模型加载 DiffusionPipeline,则会返回 StableDiffusionXLPipeline 子类。它可以根据输入用于 text-to-image、image-to-image 或 inpainting 任务。

import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
  "RunDiffusion/Juggernaut-XL-v9", torch_dtype=torch.bfloat16, device_map="cuda",
)
print(pipeline)
"StableDiffusionXLPipeline {
  "_class_name": "StableDiffusionXLPipeline",
  ...
"

你可以查看 mappings,确认某个模型是否受支持。

如果尝试加载不受支持的模型,就会报错。

import torch
from diffusers import AutoPipelineForImage2Image

pipeline = AutoPipelineForImage2Image.from_pretrained(
    "openai/shap-e-img2img", torch_dtype=torch.float16,
)
"ValueError: AutoPipeline can't find a pipeline linked to ShapEImg2ImgPipeline for None"

AutoPipeline 一共有四种类型:

  • AutoPipelineForText2Image
  • AutoPipelineForImage2Image
  • AutoPipelineForInpainting
  • AutoPipelineForText2Audio

这些类都带有预定义的映射关系,会把某个pipeline关联到对应任务的子类上。

调用 from_pretrained() 时,它会从 model_index.json 文件中提取类名,并根据映射关系为该任务选择合适的pipeline子类。

Update on GitHub