PEFT (original) (raw)

Refer to the Inference with PEFT tutorial for an overview of how to use PEFT in Diffusers for inference.

class diffusers.loaders.PeftAdapterMixin

< source >

( )

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 PEFTdocumentation.

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 PEFTdocumentation.

add_adapter

< source >

( adapter_config adapter_name: str = 'default' )

Parameters

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 PEFTdocumentation.

delete_adapters

< source >

( adapter_names: typing.Union[typing.List[str], str] )

Parameters

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 PEFTdocumentation.

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 PEFTdocumentation.

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()

enable_lora_hotswap

< source >

( target_rank: int = 128 check_compiled: typing.Literal['error', 'warn', 'ignore'] = 'error' )

Parameters

Enables the possibility to hotswap LoRA adapters.

Calling this method is only required when hotswapping adapters and if the model is compiled or if the ranks of the loaded adapters differ.

load_lora_adapter

< source >

( pretrained_model_name_or_path_or_dict prefix = 'transformer' hotswap: bool = False **kwargs )

Parameters

Loads a LoRA adapter into the underlying model.

save_lora_adapter

< source >

( save_directory adapter_name: str = 'default' upcast_before_saving: bool = False safe_serialization: bool = True weight_name: typing.Optional[str] = None )

Parameters

Save the LoRA parameters corresponding to the underlying model.

set_adapter

< source >

( adapter_name: typing.Union[str, typing.List[str]] )

Parameters

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 PEFTdocumentation.

set_adapters

< source >

( 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 )

Parameters

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])