autopipeline.md 2.94 KB
Newer Older
Aryan's avatar
Aryan committed
1
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

13
14
# AutoPipeline

Steven Liu's avatar
Steven Liu committed
15
[AutoPipeline](../api/models/auto_model) is a *task-and-model* pipeline that automatically selects the correct pipeline subclass based on the task. It handles the complexity of loading different pipeline subclasses without needing to know the specific pipeline subclass name.
16

Steven Liu's avatar
Steven Liu committed
17
This is unlike [`DiffusionPipeline`], a *model-only* pipeline that automatically selects the pipeline subclass based on the model.
18

Steven Liu's avatar
Steven Liu committed
19
[`AutoPipelineForImage2Image`] returns a specific pipeline subclass, (for example, [`StableDiffusionXLImg2ImgPipeline`]), which can only be used for image-to-image tasks.
20
21
22
23

```py
import torch
from diffusers import AutoPipelineForImage2Image
Steven Liu's avatar
Steven Liu committed
24

Steven Liu's avatar
Steven Liu committed
25
26
27
28
29
30
31
32
pipeline = AutoPipelineForImage2Image.from_pretrained(
  "RunDiffusion/Juggernaut-XL-v9", torch_dtype=torch.bfloat16, device_map="cuda",
)
print(pipeline)
"StableDiffusionXLImg2ImgPipeline {
  "_class_name": "StableDiffusionXLImg2ImgPipeline",
  ...
"
33
34
```

Steven Liu's avatar
Steven Liu committed
35
Loading the same model with [`DiffusionPipeline`] returns the [`StableDiffusionXLPipeline`] subclass. It can be used for text-to-image, image-to-image, or inpainting tasks depending on the inputs.
36
37

```py
38
import torch
Steven Liu's avatar
Steven Liu committed
39
from diffusers import DiffusionPipeline
40

Steven Liu's avatar
Steven Liu committed
41
42
43
44
45
46
47
48
pipeline = DiffusionPipeline.from_pretrained(
  "RunDiffusion/Juggernaut-XL-v9", torch_dtype=torch.bfloat16, device_map="cuda",
)
print(pipeline)
"StableDiffusionXLPipeline {
  "_class_name": "StableDiffusionXLPipeline",
  ...
"
49
50
```

Steven Liu's avatar
Steven Liu committed
51
Check the [mappings](https://github.com/huggingface/diffusers/blob/130fd8df54f24ffb006d84787b598d8adc899f23/src/diffusers/pipelines/auto_pipeline.py#L114) to see whether a model is supported or not.
52

Steven Liu's avatar
Steven Liu committed
53
Trying to load an unsupported model returns an error.
54
55
56

```py
import torch
Steven Liu's avatar
Steven Liu committed
57
from diffusers import AutoPipelineForImage2Image
58
59

pipeline = AutoPipelineForImage2Image.from_pretrained(
Steven Liu's avatar
Steven Liu committed
60
    "openai/shap-e-img2img", torch_dtype=torch.float16,
61
62
63
)
"ValueError: AutoPipeline can't find a pipeline linked to ShapEImg2ImgPipeline for None"
```
Steven Liu's avatar
Steven Liu committed
64
65
66
67

There are three types of [AutoPipeline](../api/models/auto_model) classes, [`AutoPipelineForText2Image`], [`AutoPipelineForImage2Image`] and [`AutoPipelineForInpainting`]. Each of these classes have a predefined mapping, linking a pipeline to their task-specific subclass.

When [`~AutoPipelineForText2Image.from_pretrained`] is called, it extracts the class name from the `model_index.json` file and selects the appropriate pipeline subclass for the task based on the mapping.