#
Pipelines

The DiffusionPipeline is the quickest way to load any pretrained diffusion pipeline from the Hub for inference.

You shouldn’t use the DiffusionPipeline class for training or finetuning a diffusion model. Individual components (for example, UNet2DModel and UNet2DConditionModel) of diffusion pipelines are usually trained individually, so we suggest directly working with them instead.

The pipeline type (for example StableDiffusionPipeline) of any diffusion pipeline loaded with from_pretrained() is automatically detected and pipeline components are loaded and passed to the `__init__` function of the pipeline.

Any pipeline object can be saved locally with save_pretrained().

##
DiffusionPipeline

###
class diffusers.DiffusionPipeline

< source >

( )

Base class for all pipelines.

DiffusionPipeline stores all components (models, schedulers, and processors) for diffusion pipelines and provides methods for loading, downloading and saving models. It also includes methods to:

- move all PyTorch modules to the device of your choice
- enabling/disabling the progress bar for the denoising iteration

Class attributes:

- config_name (`str`) — The configuration filename that stores the class and module names of all the diffusion pipeline’s components.
- _optional_components (List`str`) — List of all optional components that don’t have to be passed to the pipeline to function (should be overridden by subclasses).

####
__call__

( *args **kwargs )

Call self as a function.

####
device

< source >

( ) → `torch.device`

Returns

`torch.device`

The torch device on which the pipeline is located.

####
to

< source >

( torch_device: typing.Union[str, torch.device, NoneType] = None torch_dtype: typing.Optional[torch.dtype] = None silence_dtype_warnings: bool = False )

####
components

< source >

( )

The `self.components` property can be useful to run different pipelines with the same weights and configurations without reallocating additional memory.

Returns (`dict`): A dictionary containing all the modules needed to initialize the pipeline.

Examples:

    >>> from diffusers import (
    ...     StableDiffusionPipeline,
    ...     StableDiffusionImg2ImgPipeline,
    ...     StableDiffusionInpaintPipeline,
    ... )

    >>> text2img = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    >>> img2img = StableDiffusionImg2ImgPipeline(**text2img.components)
    >>> inpaint = StableDiffusionInpaintPipeline(**text2img.components)

####
disable_attention_slicing

< source >

( )

Disable sliced attention computation. If `enable_attention_slicing` was previously called, attention is computed in one step.

####
disable_xformers_memory_efficient_attention

< source >

( )

Disable memory efficient attention from xFormers.

####
download

< source >

( pretrained_model_name **kwargs ) → `os.PathLike`

Parameters

- pretrained_model_name (`str` or `os.PathLike`, optional) — A string, the repository id (for example `CompVis/ldm-text2im-large-256`) of a pretrained pipeline hosted on the Hub.
- custom_pipeline (`str`, optional) — Can be either:

  - A string, the repository id (for example `CompVis/ldm-text2im-large-256`) of a pretrained pipeline hosted on the Hub. The repository must contain a file called `pipeline.py` that defines the custom pipeline.

  - A string, the file name of a community pipeline hosted on GitHub under Community. Valid file names must match the file name and not the pipeline script (`clip_guided_stable_diffusion` instead of `clip_guided_stable_diffusion.py`). Community pipelines are always loaded from the current `main` branch of GitHub.

  - A path to a directory (`./my_pipeline_directory/`) containing a custom pipeline. The directory must contain a file called `pipeline.py` that defines the custom pipeline.

  🧪 This is an experimental feature and may change in the future.

  For more information on how to load and create custom pipelines, take a look at How to contribute a community pipeline.

- force_download (`bool`, optional, defaults to `False`) — Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.
- resume_download (`bool`, optional, defaults to `False`) — Whether or not to resume downloading the model weights and configuration files. If set to `False`, any incompletely downloaded files are deleted.
- proxies (`Dict[str, str]`, optional) — A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
- output_loading_info(`bool`, optional, defaults to `False`) — Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
- local_files_only (`bool`, optional, defaults to `False`) — Whether to only load local model weights and configuration files or not. If set to `True`, the model won’t be downloaded from the Hub.
- use_auth_token (`str` or bool, optional) — The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from `diffusers-cli login` (stored in `~/.huggingface`) is used.
- revision (`str`, optional, defaults to `"main"`) — The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier allowed by Git.
- custom_revision (`str`, optional, defaults to `"main"`) — The specific model version to use. It can be a branch name, a tag name, or a commit id similar to `revision` when loading a custom pipeline from the Hub. It can be a 🤗 Diffusers version when loading a custom pipeline from GitHub, otherwise it defaults to `"main"` when loading from the Hub.
- mirror (`str`, optional) — Mirror source to resolve accessibility issues if you’re downloading a model in China. We do not guarantee the timeliness or safety of the source, and you should refer to the mirror site for more information.
- variant (`str`, optional) — Load weights from a specified variant filename such as `"fp16"` or `"ema"`. This is ignored when loading `from_flax`.

Returns

`os.PathLike`

A path to the downloaded pipeline.

Download and cache a PyTorch diffusion pipeline from pretrained pipeline weights.

To use private or gated models, log-in with `huggingface-cli login`.

####
enable_attention_slicing

< source >

( slice_size: typing.Union[str, int, NoneType] = 'auto' )

Parameters

- slice_size (`str` or `int`, optional, defaults to `"auto"`) — When `"auto"`, halves the input to the attention heads, so attention will be computed in two steps. If `"max"`, maximum amount of memory will be saved by running only one slice at a time. If a number is provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim` must be a multiple of `slice_size`.

Enable sliced attention computation.

When this option is enabled, the attention module splits the input tensor in slices to compute attention in several steps. This is useful to save some memory in exchange for a small speed decrease.

####
enable_xformers_memory_efficient_attention

< source >

( attention_op: typing.Optional[typing.Callable] = None )

Parameters

- attention_op (`Callable`, optional) — Override the default `None` operator for use as `op` argument to the `memory_efficient_attention()` function of xFormers.

Enable memory efficient attention from xFormers.

When this option is enabled, you should observe lower GPU memory usage and a potential speed up during inference. Speed up during training is not guaranteed.

⚠️ When memory efficient attention and sliced attention are both enabled, memory efficient attention takes precedent.

Examples:

    >>> import torch
    >>> from diffusers import DiffusionPipeline
    >>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp

    >>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
    >>> pipe = pipe.to("cuda")
    >>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
    >>> # Workaround for not accepting attention shape using VAE for Flash Attention
    >>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)

####
from_pretrained

< source >

( pretrained_model_name_or_path: typing.Union[str, os.PathLike, NoneType] **kwargs )

Parameters

- pretrained_model_name_or_path (`str` or `os.PathLike`, optional) — Can be either:
  - A string, the repo id (for example `CompVis/ldm-text2im-large-256`) of a pretrained pipeline hosted on the Hub.
  - A path to a directory (for example `./my_pipeline_directory/`) containing pipeline weights saved using save_pretrained().
- torch_dtype (`str` or `torch.dtype`, optional) — Override the default `torch.dtype` and load the model with another dtype. If “auto” is passed, the dtype is automatically derived from the model’s weights.
- custom_pipeline (`str`, optional) —

  🧪 This is an experimental feature and may change in the future.

  Can be either:

  - A string, the repo id (for example `hf-internal-testing/diffusers-dummy-pipeline`) of a custom pipeline hosted on the Hub. The repository must contain a file called pipeline.py that defines the custom pipeline.
  - A string, the file name of a community pipeline hosted on GitHub under Community. Valid file names must match the file name and not the pipeline script (`clip_guided_stable_diffusion` instead of `clip_guided_stable_diffusion.py`). Community pipelines are always loaded from the current main branch of GitHub.
  - A path to a directory (`./my_pipeline_directory/`) containing a custom pipeline. The directory must contain a file called `pipeline.py` that defines the custom pipeline.

Instantiate a PyTorch diffusion pipeline from pretrained pipeline weights.

The pipeline is set in evaluation mode (`model.eval()`) by default.

If you get the error message below, you need to finetune the weights for your downstream task:

    Some weights of UNet2DConditionModel were not initialized from the model checkpoint at runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match:
    - conv_in.weight: found shape torch.Size([320, 4, 3, 3]) in the checkpoint and torch.Size([320, 9, 3, 3]) in the model instantiated
    You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

To use private or gated models, log-in with `huggingface-cli login`.

Examples:

    >>> from diffusers import DiffusionPipeline

    >>> # Download pipeline from huggingface.co and cache.
    >>> pipeline = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")

    >>> # Download pipeline that requires an authorization token
    >>> # For more information on access tokens, please refer to this section
    >>> # of the documentation](https://huggingface.co/docs/hub/security-tokens)
    >>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")

    >>> # Use a different scheduler
    >>> from diffusers import LMSDiscreteScheduler

    >>> scheduler = LMSDiscreteScheduler.from_config(pipeline.scheduler.config)
    >>> pipeline.scheduler = scheduler

####
numpy_to_pil

< source >

( images )

Convert a NumPy image or a batch of images to a PIL image.

####
save_pretrained

< source >

( save_directory: typing.Union[str, os.PathLike] safe_serialization: bool = False variant: typing.Optional[str] = None )

Parameters

- save_directory (`str` or `os.PathLike`) — Directory to save a pipeline to. Will be created if it doesn’t exist.
- safe_serialization (`bool`, optional, defaults to `False`) — Whether to save the model using `safetensors` or the traditional PyTorch way with `pickle`.
- variant (`str`, optional) — If specified, weights are saved in the format `pytorch_model.<variant>.bin`.

Save all saveable variables of the pipeline to a directory. A pipeline variable can be saved and loaded if its class implements both a save and loading method. The pipeline is easily reloaded using the from_pretrained() class method.