Pipelines
译者:片刻小哥哥
项目地址:https://huggingface.apachecn.org/docs/diffusers/api/pipelines/overview
原始地址:https://huggingface.co/docs/diffusers/api/pipelines/overview
Pipelines provide a simple way to run state-of-the-art diffusion models in inference by bundling all of the necessary components (multiple independently-trained models, schedulers, and processors) into a single end-to-end class. Pipelines are flexible and they can be adapted to use different schedulers or even model components.
All pipelines are built from the base
DiffusionPipeline
class which provides basic functionality for loading, downloading, and saving all the components. Specific pipeline types (for example
StableDiffusionPipeline
) loaded with
from_pretrained()
are automatically detected and the pipeline components are loaded and passed to the
__init__
function of the pipeline.
You shouldn’t use the DiffusionPipeline class for training. Individual components (for example, UNet2DModel and UNet2DConditionModel ) of diffusion pipelines are usually trained individually, so we suggest directly working with them instead.
Pipelines do not offer any training functionality. You’ll notice PyTorch’s autograd is disabled by decorating the
__call__()
method with a
torch.no_grad
decorator because pipelines should not be used for training. If you’re interested in training, please take a look at the
Training
guides instead!
The table below lists all the pipelines currently available in 🤗 Diffusers and the tasks they support. Click on a pipeline to view its abstract and published paper.
Pipeline | Tasks |
---|---|
AltDiffusion | image2image |
Attend-and-Excite | text2image |
Audio Diffusion | image2audio |
AudioLDM | text2audio |
AudioLDM2 | text2audio |
BLIP Diffusion | text2image |
Consistency Models | unconditional image generation |
ControlNet | text2image, image2image, inpainting |
ControlNet with Stable Diffusion XL | text2image |
Cycle Diffusion | image2image |
Dance Diffusion | unconditional audio generation |
DDIM | unconditional image generation |
DDPM | unconditional image generation |
DeepFloyd IF | text2image, image2image, inpainting, super-resolution |
DiffEdit | inpainting |
DiT | text2image |
GLIGEN | text2image |
InstructPix2Pix | image editing |
Kandinsky | text2image, image2image, inpainting, interpolation |
Kandinsky 2.2 | text2image, image2image, inpainting |
Latent Diffusion | text2image, super-resolution |
LDM3D | text2image, text-to-3D |
MultiDiffusion | text2image |
MusicLDM | text2audio |
PaintByExample | inpainting |
ParaDiGMS | text2image |
Pix2Pix Zero | image editing |
PNDM | unconditional image generation |
RePaint | inpainting |
ScoreSdeVe | unconditional image generation |
Self-Attention Guidance | text2image |
Semantic Guidance | text2image |
Shap-E | text-to-3D, image-to-3D |
Spectrogram Diffusion | |
Stable Diffusion | text2image, image2image, depth2image, inpainting, image variation, latent upscaler, super-resolution |
Stable Diffusion Model Editing | model editing |
Stable Diffusion XL | text2image, image2image, inpainting |
Stable unCLIP | text2image, image variation |
KarrasVe | unconditional image generation |
T2I Adapter | text2image |
Text2Video | text2video, video2video |
Text2Video Zero | text2video |
UnCLIP | text2image, image variation |
Unconditional Latent Diffusion | unconditional image generation |
UniDiffuser | text2image, image2text, image variation, text variation, unconditional image generation, unconditional audio generation |
Value-guided planning | value guided sampling |
Versatile Diffusion | text2image, image variation |
VQ Diffusion | text2image |
Wuerstchen | text2image |
DiffusionPipeline
class
diffusers.
DiffusionPipeline
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L514)
(
)
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
- enable/disable 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
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L869)
(
)
→
export const metadata = 'undefined';
torch.device
Returns
export const metadata = 'undefined';
torch.device
export const metadata = 'undefined';
The torch device on which the pipeline is located.
to
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L710)
(
*args
**kwargs
)
→
export const metadata = 'undefined';
Parameters
- dtype
(
torch.dtype
, optional ) — Returns a pipeline with the specifieddtype
- device
(
torch.Device
, optional ) — Returns a pipeline with the specifieddevice
- silence_dtype_warnings
(
str
, optional , defaults toFalse
) — Whether to omit warnings if the targetdtype
is not compatible with the targetdevice
.
Returns
export const metadata = 'undefined';
export const metadata = 'undefined';
The pipeline converted to specified
dtype
and/or
dtype
.
Performs Pipeline dtype and/or device conversion. A torch.dtype and torch.device are inferred from the
arguments of
self.to(*args, **kwargs).
If the pipeline already has the correct torch.dtype and torch.device, then it is returned as is. Otherwise, the returned pipeline is a copy of self with the desired torch.dtype and torch.device.
Here are the ways to call
to
:
to(dtype, silence_dtype_warnings=False) → DiffusionPipeline
to return a pipeline with the specifieddtype
to(device, silence_dtype_warnings=False) → DiffusionPipeline
to return a pipeline with the specifieddevice
to(device=None, dtype=None, silence_dtype_warnings=False) → DiffusionPipeline
to return a pipeline with the specifieddevice
anddtype
components
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1905)
(
)
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
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L2067)
(
)
Disable sliced attention computation. If
enable_attention_slicing
was previously called, attention is
computed in one step.
disable_xformers_memory_efficient_attention
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L2001)
(
)
Disable memory efficient attention from xFormers .
download
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1519)
(
pretrained_model_name
**kwargs
)
→
export const metadata = 'undefined';
os.PathLike
Parameters
- pretrained_model_name
(
str
oros.PathLike
, optional ) — A string, the repository id (for exampleCompVis/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 calledpipeline.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 ofclip_guided_stable_diffusion.py
). Community pipelines are always loaded from the currentmain
branch of GitHub. - A path to a
directory
(
./my_pipeline_directory/
) containing a custom pipeline. The directory must contain a file calledpipeline.py
that defines the custom pipeline.
- A string, the
repository id
(for example
🧪 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
.
* use_safetensors
(
bool
,
optional
, defaults to
None
) —
If set to
None
, the safetensors weights are downloaded if they’re available
and
if the
safetensors library is installed. If set to
True
, the model is forcibly loaded from safetensors
weights. If set to
False
, safetensors weights are not loaded.
* use_onnx
(
bool
,
optional
, defaults to
False
) —
If set to
True
, ONNX weights will always be downloaded if present. If set to
False
, ONNX weights
will never be downloaded. By default
use_onnx
defaults to the
_is_onnx
class attribute which is
False
for non-ONNX pipelines and
True
for ONNX pipelines. ONNX weights include both files ending
with
.onnx
and
.pb
.
* trust_remote_code
(
bool
,
optional
, defaults to
False
) —
Whether or not to allow for custom pipelines and components defined on the Hub in their own files. This
option should only be set to
True
for repositories you trust and in which you have read the code, as
it will execute code present on the Hub on your local machine.
Returns
export const metadata = 'undefined';
os.PathLike
export const metadata = 'undefined';
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
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L2027)
(
slice_size
: typing.Union[str, int, NoneType] = 'auto'
)
Parameters
- slice_size
(
str
orint
, 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 asattention_head_dim //slice_size
. In this case,attention_head_dim
must be a multiple ofslice_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. For more than one attention head, the computation is performed sequentially over each head. This is useful to save some memory in exchange for a small speed decrease.
⚠️ Don’t enable attention slicing if you’re already using
scaled_dot_product_attention
(SDPA) from PyTorch
2.0 or xFormers. These attention computations are already very memory efficient so you won’t need to enable
this function. If you enable attention slicing with SDPA or xFormers, it can lead to serious slow downs!
Examples:
>>> import torch
>>> from diffusers import StableDiffusionPipeline
>>> pipe = StableDiffusionPipeline.from_pretrained(
... "runwayml/stable-diffusion-v1-5",
... torch_dtype=torch.float16,
... use_safetensors=True,
... )
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> pipe.enable_attention_slicing()
>>> image = pipe(prompt).images[0]
enable_model_cpu_offload
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1377)
(
gpu_id
: typing.Optional[int] = None
device
: typing.Union[torch.device, str] = 'cuda'
)
Parameters
- gpu_id
(
int
, optional ) — The ID of the accelerator that shall be used in inference. If not specified, it will default to 0. - device
(
torch.Device
orstr
, optional , defaults to “cuda”) — The PyTorch device type of the accelerator that shall be used in inference. If not specified, it will default to “cuda”.
Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to
enable_sequential_cpu_offload
, this method moves one whole model at a time to the GPU when its
forward
method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
enable_sequential_cpu_offload
, but performance is much better due to the iterative execution of the
unet
.
enable_sequential_cpu_offload
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1466)
(
gpu_id
: typing.Optional[int] = None
device
: typing.Union[torch.device, str] = 'cuda'
)
Parameters
- gpu_id
(
int
, optional ) — The ID of the accelerator that shall be used in inference. If not specified, it will default to 0. - device
(
torch.Device
orstr
, optional , defaults to “cuda”) — The PyTorch device type of the accelerator that shall be used in inference. If not specified, it will default to “cuda”.
Offloads all models to CPU using 🤗 Accelerate, significantly reducing memory usage. When called, the state
dicts of all
torch.nn.Module
components (except those in
self._exclude_from_cpu_offload
) are saved to CPU
and then moved to
torch.device('meta')
and loaded to GPU only when their specific submodule has its
forward
method called. Offloading happens on a submodule basis. Memory savings are higher than with
enable_model_cpu_offload
, but performance is lower.
enable_xformers_memory_efficient_attention
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1966)
(
attention_op
: typing.Optional[typing.Callable] = None
)
Parameters
- attention_op
(
Callable
, optional ) — Override the defaultNone
operator for use asop
argument to thememory_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
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L899)
(
pretrained_model_name_or_path
: typing.Union[str, os.PathLike, NoneType]
**kwargs
)
Parameters
-
pretrained_model_name_or_path (
str
oros.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
ortorch.dtype
, optional ) — Override the defaulttorch.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 ) —
- A string, the
repo id
(for example
🧪 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](https://github.com/huggingface/diffusers/tree/main/examples/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.
For more information on how to load and create custom pipelines, please have a look at
Loading and
Adding Custom
Pipelines
* 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.
* cache_dir
(
Union[str, os.PathLike]
,
optional
) —
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
is not used.
* 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.
* device_map
(
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 defined for each
parameter/buffer name; once a given module name is inside, every submodule of it will be sent to the
same device.
Set
device_map="auto"
to have 🤗 Accelerate automatically compute the most optimized
device_map
. For
more information about each option see
designing a device
map
.
* max_memory
(
Dict
,
optional
) —
A dictionary device identifier for the maximum memory. Will default to the maximum memory available for
each GPU and the available CPU RAM if unset.
* offload_folder
(
str
or
os.PathLike
,
optional
) —
The path to offload weights if device_map contains the value
"disk"
.
* offload_state_dict
(
bool
,
optional
) —
If
True
, temporarily offloads the CPU state dict to the hard drive to avoid running out of CPU RAM if
the weight of the CPU state dict + the biggest shard of the checkpoint does not fit. Defaults to
True
when there is some disk offload.
* low_cpu_mem_usage
(
bool
,
optional
, defaults to
True
if torch version >= 1.9.0 else
False
) —
Speed up model loading only loading the pretrained weights and not initializing the weights. This also
tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
argument to
True
will raise an error.
* use_safetensors
(
bool
,
optional
, defaults to
None
) —
If set to
None
, the safetensors weights are downloaded if they’re available
and
if the
safetensors library is installed. If set to
True
, the model is forcibly loaded from safetensors
weights. If set to
False
, safetensors weights are not loaded.
* use_onnx
(
bool
,
optional
, defaults to
None
) —
If set to
True
, ONNX weights will always be downloaded if present. If set to
False
, ONNX weights
will never be downloaded. By default
use_onnx
defaults to the
_is_onnx
class attribute which is
False
for non-ONNX pipelines and
True
for ONNX pipelines. ONNX weights include both files ending
with
.onnx
and
.pb
.
* kwargs
(remaining dictionary of keyword arguments,
optional
) —
Can be used to overwrite load and saveable variables (the pipeline components of the specific pipeline
class). The overwritten components are passed directly to the pipelines
__init__
method. See example
below 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
.
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
maybe_free_model_hooks
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1447)
(
)
Function that offloads all components, removes all model hooks that were added when using
enable_model_cpu_offload
and then applies them again. In case the model has not been offloaded this function
is a no-op. Make sure to add this function to the end of the
__call__
function of your pipeline so that it
functions correctly when applying enable_model_cpu_offload.
numpy_to_pil
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L1941)
(
images
)
Convert a NumPy image or a batch of images to a PIL image.
save_pretrained
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_utils.py#L597)
(
save_directory
: typing.Union[str, os.PathLike]
safe_serialization
: bool = True
variant
: typing.Optional[str] = None
push_to_hub
: bool = False
**kwargs
)
Parameters
- save_directory
(
str
oros.PathLike
) — Directory to save a pipeline to. Will be created if it doesn’t exist. - safe_serialization
(
bool
, optional , defaults toTrue
) — Whether to save the model usingsafetensors
or the traditional PyTorch way withpickle
. - variant
(
str
, optional ) — If specified, weights are saved in the formatpytorch_model.<variant>.bin
. - push_to_hub
(
bool
, optional , defaults toFalse
) — Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to withrepo_id
(will default to the name ofsave_directory
in your namespace). - kwargs
(
Dict[str, Any]
, optional ) — Additional keyword arguments passed along to the push_to_hub() method.
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.
FlaxDiffusionPipeline
class
diffusers.
FlaxDiffusionPipeline
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_flax_utils.py#L101)
(
)
Base class for Flax-based pipelines.
FlaxDiffusionPipeline stores all components (models, schedulers, and processors) for diffusion pipelines and provides methods for loading, downloading and saving models. It also includes methods to:
- enable/disable 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.
from_pretrained
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_flax_utils.py#L228)
(
pretrained_model_name_or_path
: typing.Union[str, os.PathLike, NoneType]
**kwargs
)
Parameters
-
pretrained_model_name_or_path (
str
oros.PathLike
, optional ) — Can be either:- A string, the
repo id
(for example
runwayml/stable-diffusion-v1-5
) of a pretrained pipeline hosted on the Hub. - A path to a
directory
(for example
./my_model_directory
) containing the model weights saved using save_pretrained() . - dtype
(
str
orjnp.dtype
, optional ) — Override the defaultjnp.dtype
and load the model under this dtype. If"auto"
, the dtype is automatically derived from the model’s weights. - force_download
(
bool
, optional , defaults toFalse
) — 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 toFalse
) — Whether or not to resume downloading the model weights and configuration files. If set toFalse
, 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 toFalse
) — Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages. - local_files_only
(
bool
, optional , defaults toFalse
) — Whether to only load local model weights and configuration files or not. If set toTrue
, 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. IfTrue
, the token generated fromdiffusers-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. - 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. - kwargs
(remaining dictionary of keyword arguments,
optional
) —
Can be used to overwrite load and saveable variables (the pipeline components) of the specific pipeline
class. The overwritten components are passed directly to the pipelines
__init__
method.
- A string, the
repo id
(for example
Instantiate a Flax-based diffusion pipeline from pretrained pipeline weights.
The pipeline is set in evaluation mode (`model.eval()) by default and dropout modules are deactivated.
If you get the error message below, you need to finetune the weights for your downstream task:
Some weights of FlaxUNet2DConditionModel were not initialized from the model checkpoint at runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match:
To use private or
gated models
, log-in with
huggingface-cli login
.
Examples:
>>> from diffusers import FlaxDiffusionPipeline
>>> # Download pipeline from huggingface.co and cache.
>>> # Requires to be logged in to Hugging Face hub,
>>> # see more in the documentation
>>> pipeline, params = FlaxDiffusionPipeline.from_pretrained(
... "runwayml/stable-diffusion-v1-5",
... revision="bf16",
... dtype=jnp.bfloat16,
... )
>>> # Download pipeline, but use a different scheduler
>>> from diffusers import FlaxDPMSolverMultistepScheduler
>>> model_id = "runwayml/stable-diffusion-v1-5"
>>> dpmpp, dpmpp_state = FlaxDPMSolverMultistepScheduler.from_pretrained(
... model_id,
... subfolder="scheduler",
... )
>>> dpm_pipe, dpm_params = FlaxStableDiffusionPipeline.from_pretrained(
... model_id, revision="bf16", dtype=jnp.bfloat16, scheduler=dpmpp
... )
>>> dpm_params["scheduler"] = dpmpp_state
numpy_to_pil
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_flax_utils.py#L585)
(
images
)
Convert a NumPy image or a batch of images to a PIL image.
save_pretrained
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/pipelines/pipeline_flax_utils.py#L150)
(
save_directory
: typing.Union[str, os.PathLike]
params
: typing.Union[typing.Dict, flax.core.frozen_dict.FrozenDict]
push_to_hub
: bool = False
**kwargs
)
Parameters
- save_directory
(
str
oros.PathLike
) — Directory to which to save. Will be created if it doesn’t exist. - push_to_hub
(
bool
, optional , defaults toFalse
) — Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to withrepo_id
(will default to the name ofsave_directory
in your namespace). - kwargs
(
Dict[str, Any]
, optional ) — Additional keyword arguments passed along to the push_to_hub() method.
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.
PushToHubMixin
class
diffusers.utils.
PushToHubMixin
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/utils/hub_utils.py#L373)
(
)
A Mixin to push a model, scheduler, or pipeline to the Hugging Face Hub.
push_to_hub
[<
source
](https://github.com/huggingface/diffusers/blob/v0.23.0/src/diffusers/utils/hub_utils.py#L402)
(
repo_id
: str
commit_message
: typing.Optional[str] = None
private
: typing.Optional[bool] = None
token
: typing.Optional[str] = None
create_pr
: bool = False
safe_serialization
: bool = True
variant
: typing.Optional[str] = None
)
Parameters
- repo_id
(
str
) — The name of the repository you want to push your model, scheduler, or pipeline files to. It should contain your organization name when pushing to an organization.repo_id
can also be a path to a local directory. - commit_message
(
str
, optional ) — Message to commit while pushing. Default to"Upload {object}"
. - private
(
bool
, optional ) — Whether or not the repository created should be private. - token
(
str
, optional ) — The token to use as HTTP bearer authorization for remote files. The token generated when runninghuggingface-cli login
(stored in~/.huggingface
). - create_pr
(
bool
, optional , defaults toFalse
) — Whether or not to create a PR with the uploaded files or directly commit. - safe_serialization
(
bool
, optional , defaults toTrue
) — Whether or not to convert the model weights to thesafetensors
format. - variant
(
str
, optional ) — If specified, weights are saved in the formatpytorch_model.<variant>.bin
.
Upload model, scheduler, or pipeline files to the 🤗 Hugging Face Hub.
Examples:
from diffusers import UNet2DConditionModel
unet = UNet2DConditionModel.from_pretrained("stabilityai/stable-diffusion-2", subfolder="unet")
# Push the `unet` to your namespace with the name "my-finetuned-unet".
unet.push_to_hub("my-finetuned-unet")
# Push the `unet` to an organization with the name "my-finetuned-unet".
unet.push_to_hub("your-org/my-finetuned-unet")