Skip to content

Commit f9f1535

Browse files
committed
Merge branch 'main' into torchao-quantizer
2 parents 820ac88 + 40fc389 commit f9f1535

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

docs/source/en/using-diffusers/loading_adapters.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,16 @@ The [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`] method loads L
134134
- the LoRA weights don't have separate identifiers for the UNet and text encoder
135135
- the LoRA weights have separate identifiers for the UNet and text encoder
136136

137-
But if you only need to load LoRA weights into the UNet, then you can use the [`~loaders.UNet2DConditionLoadersMixin.load_attn_procs`] method. Let's load the [jbilcke-hf/sdxl-cinematic-1](https://huggingface.co/jbilcke-hf/sdxl-cinematic-1) LoRA:
137+
To directly load (and save) a LoRA adapter at the *model-level*, use [`~PeftAdapterMixin.load_lora_adapter`], which builds and prepares the necessary model configuration for the adapter. Like [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`], [`PeftAdapterMixin.load_lora_adapter`] can load LoRAs for both the UNet and text encoder. For example, if you're loading a LoRA for the UNet, [`PeftAdapterMixin.load_lora_adapter`] ignores the keys for the text encoder.
138+
139+
Use the `weight_name` parameter to specify the specific weight file and the `prefix` parameter to filter for the appropriate state dicts (`"unet"` in this case) to load.
138140

139141
```py
140142
from diffusers import AutoPipelineForText2Image
141143
import torch
142144

143145
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
144-
pipeline.unet.load_attn_procs("jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors")
146+
pipeline.unet.load_lora_adapter("jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", prefix="unet")
145147

146148
# use cnmt in the prompt to trigger the LoRA
147149
prompt = "A cute cnmt eating a slice of pizza, stunning color scheme, masterpiece, illustration"
@@ -153,6 +155,8 @@ image
153155
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/load_attn_proc.png" />
154156
</div>
155157

158+
Save an adapter with [`~PeftAdapterMixin.save_lora_adapter`].
159+
156160
To unload the LoRA weights, use the [`~loaders.StableDiffusionLoraLoaderMixin.unload_lora_weights`] method to discard the LoRA weights and restore the model to its original weights:
157161

158162
```py

src/diffusers/models/model_loading_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def load_model_dict_into_meta(
175175
hf_quantizer=None,
176176
keep_in_fp32_modules=None,
177177
) -> List[str]:
178-
device = device or torch.device("cpu")
178+
if device is not None and not isinstance(device, (str, torch.device)):
179+
raise ValueError(f"Expected device to have type `str` or `torch.device`, but got {type(device)=}.")
180+
if hf_quantizer is None:
181+
device = device or torch.device("cpu")
179182
dtype = dtype or torch.float32
180183
is_quantized = hf_quantizer is not None
181184

src/diffusers/models/modeling_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
829829
param_device = "cpu"
830830
# TODO (sayakpaul, SunMarc): remove this after model loading refactor
831831
else:
832-
param_device = torch.cuda.current_device()
832+
param_device = torch.device(torch.cuda.current_device())
833833
state_dict = load_state_dict(model_file, variant=variant)
834834
model._convert_deprecated_attention_blocks(state_dict)
835835

tests/lora/test_lora_layers_cogvideox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def get_dummy_inputs(self, with_generator=True):
129129

130130
@skip_mps
131131
@pytest.mark.xfail(
132-
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
132+
condition=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
133133
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
134134
strict=True,
135135
)

tests/lora/test_lora_layers_mochi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get_dummy_inputs(self, with_generator=True):
108108
return noise, input_ids, pipeline_inputs
109109

110110
@pytest.mark.xfail(
111-
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
111+
condition=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
112112
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
113113
strict=True,
114114
)

tests/lora/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ def test_simple_inference_with_text_denoiser_multi_adapter_weighted(self):
15131513

15141514
@skip_mps
15151515
@pytest.mark.xfail(
1516-
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
1516+
condition=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
15171517
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
15181518
strict=True,
15191519
)

0 commit comments

Comments
 (0)