Skip to content

Commit 6b59ecf

Browse files
Update docstring:
- mention no text encoder support yet - make it clear that LoRA is meant - mention that same adapter name should be passed
1 parent 2d407ca commit 6b59ecf

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

src/diffusers/loaders/lora_pipeline.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def load_lora_weights(
8383
hotswap: bool = False,
8484
**kwargs,
8585
):
86-
"""
87-
Load LoRA weights specified in `pretrained_model_name_or_path_or_dict` into `self.unet` and
86+
"""Load LoRA weights specified in `pretrained_model_name_or_path_or_dict` into `self.unet` and
8887
`self.text_encoder`.
8988
9089
All kwargs are forwarded to `self.lora_state_dict`.
@@ -108,11 +107,12 @@ def load_lora_weights(
108107
Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
109108
weights.
110109
hotswap : (`bool`, *optional*)
111-
Defaults to `False`. Whether to substitute an existing adapter with the newly loaded adapter in-place.
112-
This means that, instead of loading an additional adapter, this will take the existing adapter weights
113-
and replace them with the weights of the new adapter. This can be faster and more memory efficient.
114-
However, the main advantage of hotswapping is that when the model is compiled with torch.compile,
115-
loading the new adapter does not require recompilation of the model.
110+
Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
111+
in-place. This means that, instead of loading an additional adapter, this will take the existing
112+
adapter weights and replace them with the weights of the new adapter. This can be faster and more
113+
memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
114+
torch.compile, loading the new adapter does not require recompilation of the model. When using
115+
hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.
116116
117117
If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
118118
to call an additional method before loading the adapter:
@@ -126,10 +126,12 @@ def load_lora_weights(
126126
# optionally compile the model now
127127
```
128128
129-
There are some limitations to this technique, which are documented here:
129+
Note that hotswapping adapters of the text encoder is not yet supported. There are some further
130+
limitations to this technique, which are documented here:
130131
https://huggingface.co/docs/peft/main/en/package_reference/hotswap
131132
kwargs (`dict`, *optional*):
132133
See [`~loaders.StableDiffusionLoraLoaderMixin.lora_state_dict`].
134+
133135
"""
134136
if not USE_PEFT_BACKEND:
135137
raise ValueError("PEFT backend is required for this method.")
@@ -320,11 +322,12 @@ def load_lora_into_unet(
320322
Speed up model loading only loading the pretrained LoRA weights and not initializing the random
321323
weights.
322324
hotswap : (`bool`, *optional*)
323-
Defaults to `False`. Whether to substitute an existing adapter with the newly loaded adapter in-place.
324-
This means that, instead of loading an additional adapter, this will take the existing adapter weights
325-
and replace them with the weights of the new adapter. This can be faster and more memory efficient.
326-
However, the main advantage of hotswapping is that when the model is compiled with torch.compile,
327-
loading the new adapter does not require recompilation of the model.
325+
Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
326+
in-place. This means that, instead of loading an additional adapter, this will take the existing
327+
adapter weights and replace them with the weights of the new adapter. This can be faster and more
328+
memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
329+
torch.compile, loading the new adapter does not require recompilation of the model. When using
330+
hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.
328331
329332
If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
330333
to call an additional method before loading the adapter:
@@ -338,7 +341,8 @@ def load_lora_into_unet(
338341
# optionally compile the model now
339342
```
340343
341-
There are some limitations to this technique, which are documented here:
344+
Note that hotswapping adapters of the text encoder is not yet supported. There are some further
345+
limitations to this technique, which are documented here:
342346
https://huggingface.co/docs/peft/main/en/package_reference/hotswap
343347
"""
344348
if not USE_PEFT_BACKEND:
@@ -405,13 +409,17 @@ def load_lora_into_text_encoder(
405409
Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
406410
weights.
407411
hotswap : (`bool`, *optional*)
408-
Defaults to `False`. Whether to substitute an existing adapter with the newly loaded adapter in-place.
409-
This means that, instead of loading an additional adapter, this will take the existing adapter weights
410-
and replace them with the weights of the new adapter. This can be faster and more memory efficient.
411-
However, the main advantage of hotswapping is that when the model is compiled with torch.compile,
412-
loading the new adapter does not require recompilation of the model. If the new adapter and the old
413-
adapter have different ranks and/or LoRA alphas (i.e. scaling), you need to call an additional method
414-
before loading the adapter:
412+
hotswap : (`bool`, *optional*)
413+
Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
414+
in-place. This means that, instead of loading an additional adapter, this will take the existing
415+
adapter weights and replace them with the weights of the new adapter. This can be faster and more
416+
memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
417+
torch.compile, loading the new adapter does not require recompilation of the model. When using
418+
hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.
419+
420+
If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
421+
to call an additional method before loading the adapter:
422+
415423
```py
416424
pipeline = ... # load diffusers pipeline
417425
max_rank = ... # the highest rank among all LoRAs that you want to load
@@ -420,7 +428,9 @@ def load_lora_into_text_encoder(
420428
pipeline.load_lora_weights(file_name)
421429
# optionally compile the model now
422430
```
423-
There are some limitations to this technique, which are documented here:
431+
432+
Note that hotswapping adapters of the text encoder is not yet supported. There are some further
433+
limitations to this technique, which are documented here:
424434
https://huggingface.co/docs/peft/main/en/package_reference/hotswap
425435
"""
426436
_load_lora_into_text_encoder(
@@ -809,11 +819,12 @@ def load_lora_into_unet(
809819
Speed up model loading only loading the pretrained LoRA weights and not initializing the random
810820
weights.
811821
hotswap : (`bool`, *optional*)
812-
Defaults to `False`. Whether to substitute an existing adapter with the newly loaded adapter in-place.
813-
This means that, instead of loading an additional adapter, this will take the existing adapter weights
814-
and replace them with the weights of the new adapter. This can be faster and more memory efficient.
815-
However, the main advantage of hotswapping is that when the model is compiled with torch.compile,
816-
loading the new adapter does not require recompilation of the model.
822+
Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
823+
in-place. This means that, instead of loading an additional adapter, this will take the existing
824+
adapter weights and replace them with the weights of the new adapter. This can be faster and more
825+
memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
826+
torch.compile, loading the new adapter does not require recompilation of the model. When using
827+
hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.
817828
818829
If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
819830
to call an additional method before loading the adapter:
@@ -827,7 +838,8 @@ def load_lora_into_unet(
827838
# optionally compile the model now
828839
```
829840
830-
There are some limitations to this technique, which are documented here:
841+
Note that hotswapping adapters of the text encoder is not yet supported. There are some further
842+
limitations to this technique, which are documented here:
831843
https://huggingface.co/docs/peft/main/en/package_reference/hotswap
832844
"""
833845
if not USE_PEFT_BACKEND:

src/diffusers/loaders/peft.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def _optionally_disable_offloading(cls, _pipeline):
148148
def load_lora_adapter(
149149
self, pretrained_model_name_or_path_or_dict, prefix="transformer", hotswap: bool = False, **kwargs
150150
):
151-
r"""Loads a LoRA adapter into the underlying model.
151+
r"""
152+
Loads a LoRA adapter into the underlying model.
152153
153154
Parameters:
154155
pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
@@ -191,14 +192,15 @@ def load_lora_adapter(
191192
Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
192193
weights.
193194
hotswap : (`bool`, *optional*)
194-
Defaults to `False`. Whether to substitute an existing adapter with the newly loaded adapter in-place.
195-
This means that, instead of loading an additional adapter, this will take the existing adapter weights
196-
and replace them with the weights of the new adapter. This can be faster and more memory efficient.
197-
However, the main advantage of hotswapping is that when the model is compiled with torch.compile,
198-
loading the new adapter does not require recompilation of the model.
195+
Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
196+
in-place. This means that, instead of loading an additional adapter, this will take the existing
197+
adapter weights and replace them with the weights of the new adapter. This can be faster and more
198+
memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
199+
torch.compile, loading the new adapter does not require recompilation of the model. When using
200+
hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.
199201
200-
If the model is compiled, or if the new adapter and the old adapter have different ranks and/or LoRA
201-
alphas (i.e. scaling), you need to call an additional method before loading the adapter:
202+
If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
203+
to call an additional method before loading the adapter:
202204
203205
```py
204206
pipeline = ... # load diffusers pipeline
@@ -209,9 +211,9 @@ def load_lora_adapter(
209211
# optionally compile the model now
210212
```
211213
212-
There are some limitations to this technique, which are documented here:
214+
Note that hotswapping adapters of the text encoder is not yet supported. There are some further
215+
limitations to this technique, which are documented here:
213216
https://huggingface.co/docs/peft/main/en/package_reference/hotswap
214-
215217
"""
216218
from peft import LoraConfig, inject_adapter_in_model, set_peft_model_state_dict
217219
from peft.tuners.tuners_utils import BaseTunerLayer

0 commit comments

Comments
 (0)