Skip to content

Commit 77cfc79

Browse files
authored
Merge branch 'main' into allow-flux-lora-tests
2 parents 704392a + b5fd6f1 commit 77cfc79

34 files changed

+274
-159
lines changed

src/diffusers/loaders/single_file_utils.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@
6262
"xl_base": "conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias",
6363
"xl_refiner": "conditioner.embedders.0.model.transformer.resblocks.9.mlp.c_proj.bias",
6464
"upscale": "model.diffusion_model.input_blocks.10.0.skip_connection.bias",
65-
"controlnet": "control_model.time_embed.0.weight",
65+
"controlnet": [
66+
"control_model.time_embed.0.weight",
67+
"controlnet_cond_embedding.conv_in.weight",
68+
],
69+
# TODO: find non-Diffusers keys for controlnet_xl
70+
"controlnet_xl": "add_embedding.linear_1.weight",
71+
"controlnet_xl_large": "down_blocks.1.attentions.0.transformer_blocks.0.attn1.to_k.weight",
72+
"controlnet_xl_mid": "down_blocks.1.attentions.0.norm.weight",
6673
"playground-v2-5": "edm_mean",
6774
"inpainting": "model.diffusion_model.input_blocks.0.0.weight",
6875
"clip": "cond_stage_model.transformer.text_model.embeddings.position_embedding.weight",
@@ -96,6 +103,9 @@
96103
"inpainting": {"pretrained_model_name_or_path": "stable-diffusion-v1-5/stable-diffusion-inpainting"},
97104
"inpainting_v2": {"pretrained_model_name_or_path": "stabilityai/stable-diffusion-2-inpainting"},
98105
"controlnet": {"pretrained_model_name_or_path": "lllyasviel/control_v11p_sd15_canny"},
106+
"controlnet_xl_large": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0"},
107+
"controlnet_xl_mid": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0-mid"},
108+
"controlnet_xl_small": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0-small"},
99109
"v2": {"pretrained_model_name_or_path": "stabilityai/stable-diffusion-2-1"},
100110
"v1": {"pretrained_model_name_or_path": "stable-diffusion-v1-5/stable-diffusion-v1-5"},
101111
"stable_cascade_stage_b": {"pretrained_model_name_or_path": "stabilityai/stable-cascade", "subfolder": "decoder"},
@@ -481,8 +491,16 @@ def infer_diffusers_model_type(checkpoint):
481491
elif CHECKPOINT_KEY_NAMES["upscale"] in checkpoint:
482492
model_type = "upscale"
483493

484-
elif CHECKPOINT_KEY_NAMES["controlnet"] in checkpoint:
485-
model_type = "controlnet"
494+
elif any(key in checkpoint for key in CHECKPOINT_KEY_NAMES["controlnet"]):
495+
if CHECKPOINT_KEY_NAMES["controlnet_xl"] in checkpoint:
496+
if CHECKPOINT_KEY_NAMES["controlnet_xl_large"] in checkpoint:
497+
model_type = "controlnet_xl_large"
498+
elif CHECKPOINT_KEY_NAMES["controlnet_xl_mid"] in checkpoint:
499+
model_type = "controlnet_xl_mid"
500+
else:
501+
model_type = "controlnet_xl_small"
502+
else:
503+
model_type = "controlnet"
486504

487505
elif (
488506
CHECKPOINT_KEY_NAMES["stable_cascade_stage_c"] in checkpoint
@@ -1072,6 +1090,9 @@ def convert_controlnet_checkpoint(
10721090
config,
10731091
**kwargs,
10741092
):
1093+
# Return checkpoint if it's already been converted
1094+
if "time_embedding.linear_1.weight" in checkpoint:
1095+
return checkpoint
10751096
# Some controlnet ckpt files are distributed independently from the rest of the
10761097
# model components i.e. https://huggingface.co/thibaud/controlnet-sd21/
10771098
if "time_embed.0.weight" in checkpoint:

src/diffusers/utils/testing_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ def require_note_seq(test_case):
373373
return unittest.skipUnless(is_note_seq_available(), "test requires note_seq")(test_case)
374374

375375

376+
def require_accelerator(test_case):
377+
"""
378+
Decorator marking a test that requires a hardware accelerator backend. These tests are skipped when there are no
379+
hardware accelerator available.
380+
"""
381+
return unittest.skipUnless(torch_device != "cpu", "test requires a hardware accelerator")(test_case)
382+
383+
376384
def require_torchsde(test_case):
377385
"""
378386
Decorator marking a test that requires torchsde. These tests are skipped when torchsde isn't installed.

tests/lora/test_lora_layers_cogvideox.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import unittest
1717

1818
import numpy as np
19+
import pytest
1920
import torch
2021
from transformers import AutoTokenizer, T5EncoderModel
2122

@@ -29,6 +30,7 @@
2930
from diffusers.utils.testing_utils import (
3031
floats_tensor,
3132
is_peft_available,
33+
is_torch_version,
3234
require_peft_backend,
3335
skip_mps,
3436
torch_device,
@@ -126,6 +128,11 @@ def get_dummy_inputs(self, with_generator=True):
126128
return noise, input_ids, pipeline_inputs
127129

128130
@skip_mps
131+
@pytest.mark.xfail(
132+
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
133+
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
134+
strict=True,
135+
)
129136
def test_lora_fuse_nan(self):
130137
for scheduler_cls in self.scheduler_classes:
131138
components, text_lora_config, denoiser_lora_config = self.get_dummy_components(scheduler_cls)

tests/lora/test_lora_layers_mochi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import unittest
1717

1818
import numpy as np
19+
import pytest
1920
import torch
2021
from transformers import AutoTokenizer, T5EncoderModel
2122

2223
from diffusers import AutoencoderKLMochi, FlowMatchEulerDiscreteScheduler, MochiPipeline, MochiTransformer3DModel
2324
from diffusers.utils.testing_utils import (
2425
floats_tensor,
2526
is_peft_available,
27+
is_torch_version,
2628
require_peft_backend,
2729
skip_mps,
2830
torch_device,
@@ -105,6 +107,11 @@ def get_dummy_inputs(self, with_generator=True):
105107

106108
return noise, input_ids, pipeline_inputs
107109

110+
@pytest.mark.xfail(
111+
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
112+
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
113+
strict=True,
114+
)
108115
def test_lora_fuse_nan(self):
109116
for scheduler_cls in self.scheduler_classes:
110117
components, text_lora_config, denoiser_lora_config = self.get_dummy_components(scheduler_cls)

tests/lora/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from itertools import product
2020

2121
import numpy as np
22+
import pytest
2223
import torch
2324

2425
from diffusers import (
@@ -32,6 +33,7 @@
3233
from diffusers.utils.testing_utils import (
3334
CaptureLogger,
3435
floats_tensor,
36+
is_torch_version,
3537
require_peft_backend,
3638
require_peft_version_greater,
3739
require_transformers_version_greater,
@@ -1510,6 +1512,11 @@ def test_simple_inference_with_text_denoiser_multi_adapter_weighted(self):
15101512
)
15111513

15121514
@skip_mps
1515+
@pytest.mark.xfail(
1516+
condtion=torch.device(torch_device).type == "cpu" and is_torch_version(">=", "2.5"),
1517+
reason="Test currently fails on CPU and PyTorch 2.5.1 but not on PyTorch 2.4.1.",
1518+
strict=True,
1519+
)
15131520
def test_lora_fuse_nan(self):
15141521
for scheduler_cls in self.scheduler_classes:
15151522
components, text_lora_config, denoiser_lora_config = self.get_dummy_components(scheduler_cls)

tests/pipelines/amused/test_amused.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from diffusers import AmusedPipeline, AmusedScheduler, UVit2DModel, VQModel
2323
from diffusers.utils.testing_utils import (
2424
enable_full_determinism,
25-
require_torch_gpu,
25+
require_torch_accelerator,
2626
slow,
2727
torch_device,
2828
)
@@ -129,7 +129,7 @@ def test_inference_batch_single_identical(self):
129129

130130

131131
@slow
132-
@require_torch_gpu
132+
@require_torch_accelerator
133133
class AmusedPipelineSlowTests(unittest.TestCase):
134134
def test_amused_256(self):
135135
pipe = AmusedPipeline.from_pretrained("amused/amused-256")

tests/pipelines/amused/test_amused_img2img.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from diffusers.utils import load_image
2424
from diffusers.utils.testing_utils import (
2525
enable_full_determinism,
26-
require_torch_gpu,
26+
require_torch_accelerator,
2727
slow,
2828
torch_device,
2929
)
@@ -131,7 +131,7 @@ def test_inference_batch_single_identical(self):
131131

132132

133133
@slow
134-
@require_torch_gpu
134+
@require_torch_accelerator
135135
class AmusedImg2ImgPipelineSlowTests(unittest.TestCase):
136136
def test_amused_256(self):
137137
pipe = AmusedImg2ImgPipeline.from_pretrained("amused/amused-256")

tests/pipelines/amused/test_amused_inpaint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from diffusers.utils import load_image
2424
from diffusers.utils.testing_utils import (
2525
enable_full_determinism,
26-
require_torch_gpu,
26+
require_torch_accelerator,
2727
slow,
2828
torch_device,
2929
)
@@ -135,7 +135,7 @@ def test_inference_batch_single_identical(self):
135135

136136

137137
@slow
138-
@require_torch_gpu
138+
@require_torch_accelerator
139139
class AmusedInpaintPipelineSlowTests(unittest.TestCase):
140140
def test_amused_256(self):
141141
pipe = AmusedInpaintPipeline.from_pretrained("amused/amused-256")

tests/pipelines/animatediff/test_animatediff.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
)
2020
from diffusers.models.attention import FreeNoiseTransformerBlock
2121
from diffusers.utils import is_xformers_available, logging
22-
from diffusers.utils.testing_utils import numpy_cosine_similarity_distance, require_torch_gpu, slow, torch_device
22+
from diffusers.utils.testing_utils import (
23+
numpy_cosine_similarity_distance,
24+
require_accelerator,
25+
require_torch_gpu,
26+
slow,
27+
torch_device,
28+
)
2329

2430
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
2531
from ..test_pipelines_common import (
@@ -272,7 +278,7 @@ def test_inference_batch_single_identical(
272278
max_diff = np.abs(to_np(output_batch[0][0]) - to_np(output[0][0])).max()
273279
assert max_diff < expected_max_diff
274280

275-
@unittest.skipIf(torch_device != "cuda", reason="CUDA and CPU are required to switch devices")
281+
@require_accelerator
276282
def test_to_device(self):
277283
components = self.get_dummy_components()
278284
pipe = self.pipeline_class(**components)
@@ -288,14 +294,14 @@ def test_to_device(self):
288294
output_cpu = pipe(**self.get_dummy_inputs("cpu"))[0]
289295
self.assertTrue(np.isnan(output_cpu).sum() == 0)
290296

291-
pipe.to("cuda")
297+
pipe.to(torch_device)
292298
model_devices = [
293299
component.device.type for component in pipe.components.values() if hasattr(component, "device")
294300
]
295-
self.assertTrue(all(device == "cuda" for device in model_devices))
301+
self.assertTrue(all(device == torch_device for device in model_devices))
296302

297-
output_cuda = pipe(**self.get_dummy_inputs("cuda"))[0]
298-
self.assertTrue(np.isnan(to_np(output_cuda)).sum() == 0)
303+
output_device = pipe(**self.get_dummy_inputs(torch_device))[0]
304+
self.assertTrue(np.isnan(to_np(output_device)).sum() == 0)
299305

300306
def test_to_dtype(self):
301307
components = self.get_dummy_components()

tests/pipelines/animatediff/test_animatediff_controlnet.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from diffusers.models.attention import FreeNoiseTransformerBlock
2222
from diffusers.utils import logging
2323
from diffusers.utils.import_utils import is_xformers_available
24-
from diffusers.utils.testing_utils import torch_device
24+
from diffusers.utils.testing_utils import require_accelerator, torch_device
2525

2626
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
2727
from ..test_pipelines_common import (
@@ -281,7 +281,7 @@ def test_inference_batch_single_identical(
281281
max_diff = np.abs(to_np(output_batch[0][0]) - to_np(output[0][0])).max()
282282
assert max_diff < expected_max_diff
283283

284-
@unittest.skipIf(torch_device != "cuda", reason="CUDA and CPU are required to switch devices")
284+
@require_accelerator
285285
def test_to_device(self):
286286
components = self.get_dummy_components()
287287
pipe = self.pipeline_class(**components)
@@ -297,14 +297,14 @@ def test_to_device(self):
297297
output_cpu = pipe(**self.get_dummy_inputs("cpu"))[0]
298298
self.assertTrue(np.isnan(output_cpu).sum() == 0)
299299

300-
pipe.to("cuda")
300+
pipe.to(torch_device)
301301
model_devices = [
302302
component.device.type for component in pipe.components.values() if hasattr(component, "device")
303303
]
304-
self.assertTrue(all(device == "cuda" for device in model_devices))
304+
self.assertTrue(all(device == torch_device for device in model_devices))
305305

306-
output_cuda = pipe(**self.get_dummy_inputs("cuda"))[0]
307-
self.assertTrue(np.isnan(to_np(output_cuda)).sum() == 0)
306+
output_device = pipe(**self.get_dummy_inputs(torch_device))[0]
307+
self.assertTrue(np.isnan(to_np(output_device)).sum() == 0)
308308

309309
def test_to_dtype(self):
310310
components = self.get_dummy_components()

0 commit comments

Comments
 (0)