You are viewing v0.3.0 version. A newer version v0.21.0 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started


The DiffusionPipeline is the easiest way to load any pretrained diffusion pipeline from the Hub and to use it in inference.

One should not use the Diffusion Pipeline class for training or fine-tuning a diffusion model. Individual components of diffusion pipelines are usually trained individually, so we suggest to directly work with `UNetModel` and `UNetConditionModel`.

Any diffusion pipeline that is loaded with from_pretrained() will automatically detect the pipeline type, e.g. StableDiffusionPipeline and consequently load each component of the pipeline and pass them into the __init__ function of the pipeline, e.g. __init__().

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


class diffusers.DiffusionPipeline

< >

( )

Base class for all models.

DiffusionPipeline takes care of storing all components (models, schedulers, processors) for diffusion pipelines and handles methods for loading, downloading and saving models as well as a few methods common to all pipelines 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) — name of the config file that will store the class and module names of all compenents of the diffusion pipeline.


< >

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


  • pretrained_model_name_or_path (str or os.PathLike, optional) — Can be either:

    • A string, the repo id of a pretrained pipeline hosted inside a model repo on Valid repo ids have to be located under a user or organization name, like CompVis/ldm-text2im-large-256.
    • A path to a directory containing pipeline weights saved using save_pretrained(), e.g., ./my_pipeline_directory/.
  • torch_dtype (str or torch.dtype, optional) — Override the default torch.dtype and load the model under this dtype. If "auto" is passed the dtype will be automatically derived from the model’s weights.
  • 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 delete incompletely received files. Will attempt to resume the download if such a file exists.
  • proxies (Dict[str, str], optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g., {'http': '', 'http://hostname': ''}. The proxies are used on each request.
  • output_loading_info(bool, optional, defaults to False) — Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool, optional, defaults to False) — Whether or not to only look at local files (i.e., do not try to download the model).
  • use_auth_token (str or bool, optional) — The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface).
  • 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, since we use a git-based system for storing models and other artifacts on, so revision can be any identifier allowed by git.
  • mirror (str, optional) — Mirror source to accelerate downloads in China. If you are from China and have an accessibility problem, you can set this option to resolve it. Note that we do not guarantee the timeliness or safety. Please refer to the mirror site for more information. specify the folder name here.
  • kwargs (remaining dictionary of keyword arguments, optional) — Can be used to overwrite load - and saveable variables - i.e. the pipeline components - of the speficic pipeline class. The overritten components are then directly passed to the pipelines __init__ method. See example below for more information.

Instantiate a PyTorch diffusion pipeline from pre-trained pipeline weights.

The pipeline is set in evaluation mode by default using model.eval() (Dropout modules are deactivated).

The warning Weights from XXX not initialized from pretrained model means that the weights of XXX do not come pretrained with the rest of the model. It is up to you to train those weights with a downstream fine-tuning task.

The warning Weights from XXX not used in YYY means that the layer XXX is not used by YYY, therefore those weights are discarded.

Passing use_auth_token=True`` is required when you want to use a private model, *e.g.* “CompVis/stable-diffusion-v1-4”`

Activate the special “offline-mode” to use this method in a firewalled environment.


>>> from diffusers import DiffusionPipeline

>>> # Download pipeline from 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](
>>> pipeline = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)

>>> # Download pipeline, but overwrite scheduler
>>> from diffusers import LMSDiscreteScheduler

>>> scheduler = LMSDiscreteScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear")
>>> pipeline = DiffusionPipeline.from_pretrained(
...     "CompVis/stable-diffusion-v1-4", scheduler=scheduler, use_auth_token=True
... )


< >

( save_directory: typing.Union[str, os.PathLike] )


  • save_directory (str or os.PathLike) — Directory to which to save. Will be created if it doesn’t exist.

Save all variables of the pipeline that can be saved and loaded as well as the pipelines configuration file to a directory. A pipeline variable can be saved and loaded if its class implements both a save and loading method. The pipeline can easily be re-loaded using the [from_pretrained()](/docs/diffusers/v0.3.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline.from_pretrained) class method.


By default diffusion pipelines return an object of class

class diffusers.pipeline_utils.ImagePipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )


  • images (List[PIL.Image.Image] or np.ndarray) — List of denoised PIL images of length batch_size or numpy array of shape (batch_size, height, width, num_channels). PIL images or numpy array present the denoised images of the diffusion pipeline.

Output class for image pipelines.