The DiffusionPipeline is the easiest way to load any pretrained diffusion pipeline from the Hub and to use it in inference.
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().
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:
Class attributes:
str
) — name of the config file that will store the class and module names of all
components of the diffusion pipeline.str
) — list of all components that are optional so they don’t have to be
passed for the pipeline to function (should be overridden by subclasses).( pretrained_model_name_or_path: typing.Union[str, os.PathLike, NoneType] **kwargs )
Parameters
str
or os.PathLike
, optional) —
Can be either:
CompVis/ldm-text2im-large-256
../my_pipeline_directory/
.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.
str
, optional) —
This is an experimental feature and is likely to change in the future.
Can be either:
A string, the repo id of a custom pipeline hosted inside a model repo on
https://huggingface.co/. Valid repo ids have to be located under a user or organization name,
like hf-internal-testing/diffusers-dummy-pipeline
.
It is required that the model repo has a file, called pipeline.py
that defines the custom
pipeline.
A string, the file name of a community pipeline hosted on GitHub under
https://github.com/huggingface/diffusers/tree/main/examples/community. Valid file names have to
match exactly the file name without .py
located under the above link, e.g.
clip_guided_stable_diffusion
.
Community pipelines are always loaded from the current main
branch of GitHub.
A path to a directory containing a custom pipeline, e.g., ./my_pipeline_directory/
.
It is required that the directory has a file, called pipeline.py
that defines the custom
pipeline.
For more information on how to load and create custom pipelines, please have a look at Loading and Adding Custom Pipelines
str
or torch.dtype
, optional) —
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.
bool
, optional, defaults to False
) —
Whether or not to delete incompletely received files. Will attempt to resume the download if such a
file exists.
Dict[str, str]
, optional) —
A dictionary of proxy servers to use by protocol or endpoint, e.g., {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
. The proxies are used on each request.
bool
, optional, defaults to False
) —
Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
bool
, optional, defaults to False
) —
Whether or not to only look at local files (i.e., do not try to download the model).
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
).
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 huggingface.co, so revision
can be any
identifier allowed by git.
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.
str
or Dict[str, Union[int, str, torch.device]]
, optional) —
A map that specifies where each submodule should go. It doesn’t need to be refined to each
parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the
same device.
To have Accelerate compute the most optimized device_map
automatically, set device_map="auto"
. For
more information about each option see designing a device
map.
bool
, optional, defaults to True
if torch version >= 1.9.0 else False
) —
Speed up model loading by not initializing the weights and only loading the pre-trained weights. This
also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the
model. This is only supported when torch version >= 1.9.0. If you are using an older version of torch,
setting this argument to True
will raise an error.
bool
, optional, defaults to False
) —
If set to True
, path to downloaded cached folder will be returned in addition to loaded pipeline.
__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.
It is required to be logged in (huggingface-cli login
) when you want to use private or gated
models, e.g. "runwayml/stable-diffusion-v1-5"
Activate the special “offline-mode” to use this method in a firewalled environment.
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
( save_directory: typing.Union[str, os.PathLike] safe_serialization: bool = False )
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/main/en/api/diffusion_pipeline#diffusers.DiffusionPipeline.from_pretrained)
class method.
(
)
→
torch.device
Returns
torch.device
The torch device on which the pipeline is located.
The self.components
property can be useful to run different pipelines with the same weights and
configurations to not have to re-allocate memory.
Examples:
>>> from diffusers import (
... StableDiffusionPipeline,
... StableDiffusionImg2ImgPipeline,
... StableDiffusionInpaintPipeline,
... )
>>> text2img = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
>>> img2img = StableDiffusionImg2ImgPipeline(**text2img.components)
>>> inpaint = StableDiffusionInpaintPipeline(**text2img.components)
( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Output class for image pipelines.