Diffusers supports loading adapters such as LoRA with the PEFT library with the PeftAdapterMixin class. This allows modeling classes in Diffusers like UNet2DConditionModel, SD3Transformer2DModel to operate with an adapter.
Refer to the Inference with PEFT tutorial for an overview of how to use PEFT in Diffusers for inference.
A class containing all functions for loading and using adapters weights that are supported in PEFT library. For more details about adapters and injecting them in a base model, check out the PEFT documentation.
Install the latest version of PEFT, and use this mixin to:
Gets the current list of active adapters of the model.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Adds a new adapter to the current model for training. If no adapter name is passed, a default name is assigned to the adapter to follow the convention of the PEFT library.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them in the PEFT documentation.
( adapter_names: typing.Union[typing.List[str], str] )
Delete an adapter’s LoRA layers from the underlying model.
Example:
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_names="cinematic"
)
pipeline.delete_adapters("cinematic")
Disable all adapters attached to the model and fallback to inference with the base model only.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Disables the active LoRA layers of the underlying model.
Example:
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.disable_lora()
Enable adapters that are attached to the model. The model uses self.active_adapters()
to retrieve the list of
adapters to enable.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Enables the active LoRA layers of the underlying model.
Example:
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.enable_lora()
( pretrained_model_name_or_path_or_dict prefix = 'transformer' **kwargs )
Parameters
str
or os.PathLike
or dict
) —
Can be either:
google/ddpm-celebahq-256
) of a pretrained model hosted on
the Hub../my_model_directory
) containing the model weights saved
with ModelMixin.save_pretrained().str
, optional) — Prefix to filter the state dict. Union[str, os.PathLike]
, optional) —
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
is not used. 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. 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. 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. 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. 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. str
, optional, defaults to ""
) —
The subfolder location of a model file within a larger model repository on the Hub or locally. Dict[str, float]
) —
The value of the network alpha used for stable learning and preventing underflow. This value has the
same meaning as the --network_alpha
option in the kohya-ss trainer script. Refer to this
link. bool
, optional) —
Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
weights. Loads a LoRA adapter into the underlying model.
( save_directory adapter_name: str = 'default' upcast_before_saving: bool = False safe_serialization: bool = True weight_name: typing.Optional[str] = None )
Parameters
str
or os.PathLike
) —
Directory to save LoRA parameters to. Will be created if it doesn’t exist. str
, defaults to “default”): The name of the adapter to serialize. Useful when the
underlying model has multiple adapters loaded. bool
, defaults to False
) —
Whether to cast the underlying model to torch.float32
before serialization. Callable
) —
The function to use to save the state dictionary. Useful during distributed training when you need to
replace torch.save
with another method. Can be configured with the environment variable
DIFFUSERS_SAVE_MODE
. bool
, optional, defaults to True
) —
Whether to save the model using safetensors
or the traditional PyTorch way with pickle
. str
, optional, defaults to None
): Name of the file to serialize the state dict with. Save the LoRA parameters corresponding to the underlying model.
( adapter_name: typing.Union[str, typing.List[str]] )
Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
( adapter_names: typing.Union[typing.List[str], str] weights: typing.Union[float, typing.Dict, typing.List[float], typing.List[typing.Dict], typing.List[NoneType], NoneType] = None )
Set the currently active adapters for use in the UNet.
Example:
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
pipeline.set_adapters(["cinematic", "pixel"], adapter_weights=[0.5, 0.5])