Buckets:

|
download
raw
4.74 kB
# AutoPipeline
[AutoPipeline](../api/pipelines/auto_pipeline) 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.
This is unlike [DiffusionPipeline](/docs/diffusers/pr_14865/en/api/pipelines/overview#diffusers.DiffusionPipeline), a *model-only* pipeline that automatically selects the pipeline subclass based on the model.
```text
AutoPipelineForImage2Image.from_pretrained(model_id)
|
+-- read model_index.json (e.g. StableDiffusionXLPipeline)
+-- task mapping (image-to-image)
|
v
StableDiffusionXLImg2ImgPipeline // returned instance
```
[AutoPipelineForImage2Image](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image) returns the task-specific subclass (for example, [StableDiffusionXLImg2ImgPipeline](/docs/diffusers/pr_14865/en/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLImg2ImgPipeline)), which can only be used for image-to-image tasks.
```py
import torch
from diffusers import AutoPipelineForImage2Image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"RunDiffusion/Juggernaut-XL-v9", dtype=torch.bfloat16, device_map="cuda", # or "mps", "xpu", "cpu"
)
print(pipeline)
# StableDiffusionXLImg2ImgPipeline {
# "_class_name": "StableDiffusionXLImg2ImgPipeline",
# ...
# }
```
Loading the same model with [DiffusionPipeline](/docs/diffusers/pr_14865/en/api/pipelines/overview#diffusers.DiffusionPipeline) returns the default text-to-image subclass, [StableDiffusionXLPipeline](/docs/diffusers/pr_14865/en/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLPipeline). That pipeline is for text-to-image. For image-to-image or inpainting, load a task AutoPipeline such as [AutoPipelineForImage2Image](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image) or [AutoPipelineForInpainting](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForInpainting), or the matching task-specific subclass.
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"RunDiffusion/Juggernaut-XL-v9", dtype=torch.bfloat16, device_map="cuda", # or "mps", "xpu", "cpu"
)
print(pipeline)
# StableDiffusionXLPipeline {
# "_class_name": "StableDiffusionXLPipeline",
# ...
# }
```
## Switch tasks with from_pipe
Load a task AutoPipeline once, then switch tasks with [from_pipe()](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image.from_pipe) without downloading the weights again. Components are reused from the source pipeline.
```py
import torch
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
pipeline_t2i = AutoPipelineForText2Image.from_pretrained(
"RunDiffusion/Juggernaut-XL-v9", dtype=torch.bfloat16, device_map="cuda", # or "mps", "xpu", "cpu"
)
pipeline_i2i = AutoPipelineForImage2Image.from_pipe(pipeline_t2i)
```
See [Reusing models in multiple pipelines](../using-diffusers/loading#reusing-models-in-multiple-pipelines) for more details.
Check the [mappings](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/auto_pipeline.py) to see whether a model is supported or not. Trying to load an unsupported model returns an error.
```py
import torch
from diffusers import AutoPipelineForImage2Image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"openai/shap-e-img2img", dtype=torch.float16,
)
"ValueError: AutoPipeline can't find a pipeline linked to ShapEImg2ImgPipeline for None"
```
There are four types of [AutoPipeline](../api/pipelines/auto_pipeline) classes:
- [AutoPipelineForText2Image](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForText2Image)
- [AutoPipelineForImage2Image](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForImage2Image)
- [AutoPipelineForInpainting](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForInpainting)
- [AutoPipelineForText2Audio](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.AutoPipelineForText2Audio)
Each of these classes has a predefined mapping, linking a pipeline to their task-specific subclass.
When [from_pretrained()](/docs/diffusers/pr_14865/en/api/pipelines/auto_pipeline#diffusers.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.

Xet Storage Details

Size:
4.74 kB
·
Xet hash:
df9393b1e518e12060221a3b3fa323af7226f698dd845132282127fa7ec5ddff

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.