Diffusers documentation

PEFT

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.29.2).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

PEFT

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.

PeftAdapterMixin

class diffusers.loaders.PeftAdapterMixin

< >

( )

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:

  • Attach new adapters in the model.
  • Attach multiple adapters and iteratively activate/deactivate them.
  • Activate/deactivate all adapters from the model.
  • Get a list of the active adapters.

active_adapters

< >

( )

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.

add_adapter

< >

( adapter_config adapter_name: str = 'default' )

Parameters

  • adapter_config ([~peft.PeftConfig]) — The configuration of the adapter to add; supported adapters are non-prefix tuning and adaption prompt methods.
  • adapter_name (str, optional, defaults to "default") — The name of the adapter to add. If no name is passed, a default name is assigned to the adapter.

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.

delete_adapters

< >

( adapter_names: Union )

Parameters

  • adapter_names (Union[List[str], str]) — The names (single string or list of strings) of the adapter to delete.

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_adapters

< >

( )

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.

disable_lora

< >

( )

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

< >

( )

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.

enable_lora

< >

( )

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

set_adapter

< >

( adapter_name: Union )

Parameters

  • adapter_name (Union[str, List[str]])) — The list of adapters to set or the adapter name in the case of a single adapter.

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.

set_adapters

< >

( adapter_names: Union weights: Union = None )

Parameters

  • adapter_names (List[str] or str) — The names of the adapters to use.
  • adapter_weights (Union[List[float], float], optional) — The adapter(s) weights to use with the UNet. If None, the weights are set to 1.0 for all the adapters.

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])
< > Update on GitHub