Loaders (original) (raw)

Adapters (textual inversion, LoRA, hypernetworks) allow you to modify a diffusion model to generate images in a specific style without training or finetuning the entire model. The adapter weights are very portable because they’re typically only a tiny fraction of the pretrained model weights. πŸ€— Diffusers provides an easy-to-use LoaderMixin API to load adapter weights.

πŸ§ͺ The LoaderMixins are highly experimental and prone to future changes. To use private or gated models, log-in with huggingface-cli login.

UNet2DConditionLoadersMixin

class diffusers.loaders.UNet2DConditionLoadersMixin

< source >

( )

delete_adapters

< source >

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

Parameters

Disables the active LoRA layers for the unet.

Enables the active LoRA layers for the unet.

load_attn_procs

< source >

( pretrained_model_name_or_path_or_dict: typing.Union[str, typing.Dict[str, torch.Tensor]] **kwargs )

Parameters

Load pretrained attention processor layers into UNet2DConditionModel. Attention processor layers have to be defined inattention_processor.pyand be a torch.nn.Module class.

save_attn_procs

< source >

( save_directory: typing.Union[str, os.PathLike] is_main_process: bool = True weight_name: str = None save_function: typing.Callable = None safe_serialization: bool = True **kwargs )

Parameters

Save an attention processor to a directory so that it can be reloaded using theload_attn_procs() method.

set_adapters

< source >

( adapter_names: typing.Union[typing.List[str], str] weights: typing.Union[typing.List[float], float, NoneType] = None )

Parameters

Sets the adapter layers for the unet.

TextualInversionLoaderMixin

class diffusers.loaders.TextualInversionLoaderMixin

< source >

( )

Load textual inversion tokens and embeddings to the tokenizer and text encoder.

load_textual_inversion

< source >

( pretrained_model_name_or_path: typing.Union[str, typing.List[str], typing.Dict[str, torch.Tensor], typing.List[typing.Dict[str, torch.Tensor]]] token: typing.Union[str, typing.List[str], NoneType] = None tokenizer: typing.Optional[ForwardRef('PreTrainedTokenizer')] = None text_encoder: typing.Optional[ForwardRef('PreTrainedModel')] = None **kwargs )

Parameters

Load textual inversion embeddings into the text encoder of StableDiffusionPipeline (both πŸ€— Diffusers and Automatic1111 formats are supported).

Example:

To load a textual inversion embedding vector in πŸ€— Diffusers format:

from diffusers import StableDiffusionPipeline import torch

model_id = "runwayml/stable-diffusion-v1-5" pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")

pipe.load_textual_inversion("sd-concepts-library/cat-toy")

prompt = "A backpack"

image = pipe(prompt, num_inference_steps=50).images[0] image.save("cat-backpack.png")

To load a textual inversion embedding vector in Automatic1111 format, make sure to download the vector first (for example from civitAI) and then load the vector

locally:

from diffusers import StableDiffusionPipeline import torch

model_id = "runwayml/stable-diffusion-v1-5" pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")

pipe.load_textual_inversion("./charturnerv2.pt", token="charturnerv2")

prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a woman wearing a black jacket and red shirt, best quality, intricate details."

image = pipe(prompt, num_inference_steps=50).images[0] image.save("character.png")

maybe_convert_prompt

< source >

( prompt: typing.Union[str, typing.List[str]] tokenizer: PreTrainedTokenizer ) β†’ str or list of str

Parameters

Returns

str or list of str

The converted prompt

Processes prompts that include a special token corresponding to a multi-vector textual inversion embedding to be replaced with multiple special tokens each corresponding to one of the vectors. If the prompt has no textual inversion token or if the textual inversion token is a single vector, the input prompt is returned.

StableDiffusionXLLoraLoaderMixin

class diffusers.loaders.StableDiffusionXLLoraLoaderMixin

< source >

( )

This class overrides LoraLoaderMixin with LoRA loading/saving code that’s specific to SDXL

load_lora_weights

< source >

( pretrained_model_name_or_path_or_dict: typing.Union[str, typing.Dict[str, torch.Tensor]] adapter_name: typing.Optional[str] = None **kwargs )

Parameters

Load LoRA weights specified in pretrained_model_name_or_path_or_dict into self.unet andself.text_encoder.

All kwargs are forwarded to self.lora_state_dict.

See lora_state_dict() for more details on how the state dict is loaded.

See load_lora_into_unet() for more details on how the state dict is loaded intoself.unet.

See load_lora_into_text_encoder() for more details on how the state dict is loaded into self.text_encoder.

LoraLoaderMixin

class diffusers.loaders.LoraLoaderMixin

< source >

( )

Load LoRA layers into UNet2DConditionModel andCLIPTextModel.

delete_adapters

< source >

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

Parameters

disable_lora_for_text_encoder

< source >

( text_encoder: typing.Optional[ForwardRef('PreTrainedModel')] = None )

Parameters

Disables the LoRA layers for the text encoder.

enable_lora_for_text_encoder

< source >

( text_encoder: typing.Optional[ForwardRef('PreTrainedModel')] = None )

Parameters

Enables the LoRA layers for the text encoder.

fuse_lora

< source >

( fuse_unet: bool = True fuse_text_encoder: bool = True lora_scale: float = 1.0 safe_fusing: bool = False )

Parameters

Fuses the LoRA parameters into the original parameters of the corresponding blocks.

This is an experimental API.

Gets the list of the current active adapters.

Example:

from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", ).to("cuda") pipeline.load_lora_weights("CiroN2022/toy-face", weight_name="toy_face_sdxl.safetensors", adapter_name="toy") pipeline.get_active_adapters()

Gets the current list of all available adapters in the pipeline.

load_lora_into_text_encoder

< source >

( state_dict network_alphas text_encoder prefix = None lora_scale = 1.0 low_cpu_mem_usage = None adapter_name = None _pipeline = None )

Parameters

This will load the LoRA layers specified in state_dict into text_encoder

load_lora_into_unet

< source >

( state_dict network_alphas unet low_cpu_mem_usage = None adapter_name = None _pipeline = None )

Parameters

This will load the LoRA layers specified in state_dict into unet.

load_lora_weights

< source >

( pretrained_model_name_or_path_or_dict: typing.Union[str, typing.Dict[str, torch.Tensor]] adapter_name = None **kwargs )

Parameters

Load LoRA weights specified in pretrained_model_name_or_path_or_dict into self.unet andself.text_encoder.

All kwargs are forwarded to self.lora_state_dict.

See lora_state_dict() for more details on how the state dict is loaded.

See load_lora_into_unet() for more details on how the state dict is loaded intoself.unet.

See load_lora_into_text_encoder() for more details on how the state dict is loaded into self.text_encoder.

lora_state_dict

< source >

( pretrained_model_name_or_path_or_dict: typing.Union[str, typing.Dict[str, torch.Tensor]] **kwargs )

Parameters

Return state dict for lora weights and the network alphas.

We support loading A1111 formatted LoRA checkpoints in a limited capacity.

This function is experimental and might change in the future.

save_lora_weights

< source >

( save_directory: typing.Union[str, os.PathLike] unet_lora_layers: typing.Dict[str, typing.Union[torch.nn.modules.module.Module, torch.Tensor]] = None text_encoder_lora_layers: typing.Dict[str, torch.nn.modules.module.Module] = None is_main_process: bool = True weight_name: str = None save_function: typing.Callable = None safe_serialization: bool = True )

Parameters

Save the LoRA parameters corresponding to the UNet and text encoder.

set_adapters_for_text_encoder

< source >

( adapter_names: typing.Union[typing.List[str], str] text_encoder: typing.Optional[ForwardRef('PreTrainedModel')] = None text_encoder_weights: typing.List[float] = None )

Parameters

Sets the adapter layers for the text encoder.

set_lora_device

< source >

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

Parameters

Moves the LoRAs listed in adapter_names to a target device. Useful for offloading the LoRA to the CPU in case you want to load multiple adapters and free some GPU memory.

unfuse_lora

< source >

( unfuse_unet: bool = True unfuse_text_encoder: bool = True )

Parameters

Reverses the effect ofpipe.fuse_lora().

This is an experimental API.

Unloads the LoRA parameters.

Examples:

pipeline.unload_lora_weights() ...

FromSingleFileMixin

class diffusers.loaders.FromSingleFileMixin

< source >

( )

Load model weights saved in the .ckpt format into a DiffusionPipeline.

from_single_file

< source >

( pretrained_model_link_or_path **kwargs )

Parameters

Instantiate a DiffusionPipeline from pretrained pipeline weights saved in the .ckpt or .safetensorsformat. The pipeline is set in evaluation mode (model.eval()) by default.

Examples:

from diffusers import StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_single_file( ... "https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors" ... )

pipeline = StableDiffusionPipeline.from_single_file("./v1-5-pruned-emaonly")

pipeline = StableDiffusionPipeline.from_single_file( ... "https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.ckpt", ... torch_dtype=torch.float16, ... ) pipeline.to("cuda")

FromOriginalControlnetMixin

class diffusers.loaders.FromOriginalControlnetMixin

< source >

( )

from_single_file

< source >

( pretrained_model_link_or_path **kwargs )

Parameters

Instantiate a ControlNetModel from pretrained controlnet weights saved in the original .ckpt or.safetensors format. The pipeline is set in evaluation mode (model.eval()) by default.

Examples:

from diffusers import StableDiffusionControlNetPipeline, ControlNetModel

url = "https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_canny.pth"
model = ControlNetModel.from_single_file(url)

url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/v1-5-pruned.safetensors"
pipe = StableDiffusionControlNetPipeline.from_single_file(url, controlnet=controlnet)

FromOriginalVAEMixin

class diffusers.loaders.FromOriginalVAEMixin

< source >

( )

from_single_file

< source >

( pretrained_model_link_or_path **kwargs )

Parameters

Instantiate a AutoencoderKL from pretrained controlnet weights saved in the original .ckpt or.safetensors format. The pipeline is format. The pipeline is set in evaluation mode (model.eval()) by default.

Make sure to pass both image_size and scaling_factor to from_single_file() if you want to load a VAE that does accompany a stable diffusion model of v2 or higher or SDXL.

Examples:

from diffusers import AutoencoderKL

url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors"
model = AutoencoderKL.from_single_file(url)