Skip to content

Commit ff8dbce

Browse files
authored
Merge branch 'main' into fix/unfuse-lora-merged-adapters-sync
2 parents 88fa75f + 09514d4 commit ff8dbce

14 files changed

Lines changed: 210 additions & 241 deletions

src/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2v.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,6 @@ def prepare_latents(
725725
)
726726

727727
visual_cond_mask[:, 0:1] = 1
728-
visual_cond[:, 0:1] = image_latents
729728

730729
latents = torch.cat([latents, visual_cond, visual_cond_mask], dim=-1)
731730

tests/others/test_check_copies.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import re
1717
import shutil
1818
import sys
19-
import tempfile
20-
import unittest
19+
20+
import pytest
2121

2222

2323
git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
@@ -45,56 +45,58 @@
4545
"""
4646

4747

48-
class CopyCheckTester(unittest.TestCase):
49-
def setUp(self):
50-
self.diffusers_dir = tempfile.mkdtemp()
51-
os.makedirs(os.path.join(self.diffusers_dir, "schedulers/"))
52-
check_copies.DIFFUSERS_PATH = self.diffusers_dir
48+
class TestCopyCheck:
49+
@pytest.fixture
50+
def diffusers_dir(self, tmp_path, monkeypatch):
51+
"""A stand-in `src/diffusers` holding only `scheduling_ddpm.py`, pointed at by `check_copies`."""
52+
os.makedirs(tmp_path / "schedulers")
5353
shutil.copy(
5454
os.path.join(git_repo_path, "src/diffusers/schedulers/scheduling_ddpm.py"),
55-
os.path.join(self.diffusers_dir, "schedulers/scheduling_ddpm.py"),
55+
tmp_path / "schedulers" / "scheduling_ddpm.py",
5656
)
57+
monkeypatch.setattr(check_copies, "DIFFUSERS_PATH", str(tmp_path))
58+
return tmp_path
5759

58-
def tearDown(self):
59-
check_copies.DIFFUSERS_PATH = "src/diffusers"
60-
shutil.rmtree(self.diffusers_dir)
61-
62-
def check_copy_consistency(self, comment, class_name, class_code, overwrite_result=None):
60+
def check_copy_consistency(self, diffusers_dir, comment, class_name, class_code, overwrite_result=None):
6361
code = comment + f"\nclass {class_name}(nn.Module):\n" + class_code
6462
if overwrite_result is not None:
6563
expected = comment + f"\nclass {class_name}(nn.Module):\n" + overwrite_result
6664
code = check_copies.run_ruff(code)
67-
fname = os.path.join(self.diffusers_dir, "new_code.py")
65+
fname = diffusers_dir / "new_code.py"
6866
with open(fname, "w", newline="\n") as f:
6967
f.write(code)
7068
if overwrite_result is None:
71-
self.assertTrue(len(check_copies.is_copy_consistent(fname)) == 0)
69+
assert len(check_copies.is_copy_consistent(fname)) == 0
7270
else:
73-
check_copies.is_copy_consistent(f.name, overwrite=True)
71+
check_copies.is_copy_consistent(fname, overwrite=True)
7472
with open(fname, "r") as f:
75-
self.assertTrue(f.read(), expected)
73+
assert f.read() == expected
7674

77-
def test_find_code_in_diffusers(self):
75+
def test_find_code_in_diffusers(self, diffusers_dir):
76+
# `diffusers_dir` is requested for its `DIFFUSERS_PATH` patch — the lookup below resolves against it.
7877
code = check_copies.find_code_in_diffusers("schedulers.scheduling_ddpm.DDPMSchedulerOutput")
79-
self.assertEqual(code, REFERENCE_CODE)
78+
assert code == REFERENCE_CODE
8079

81-
def test_is_copy_consistent(self):
80+
def test_is_copy_consistent(self, diffusers_dir):
8281
# Base copy consistency
8382
self.check_copy_consistency(
83+
diffusers_dir,
8484
"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput",
8585
"DDPMSchedulerOutput",
8686
REFERENCE_CODE + "\n",
8787
)
8888

8989
# With no empty line at the end
9090
self.check_copy_consistency(
91+
diffusers_dir,
9192
"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput",
9293
"DDPMSchedulerOutput",
9394
REFERENCE_CODE,
9495
)
9596

9697
# Copy consistency with rename
9798
self.check_copy_consistency(
99+
diffusers_dir,
98100
"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test",
99101
"TestSchedulerOutput",
100102
re.sub("DDPM", "Test", REFERENCE_CODE),
@@ -103,13 +105,15 @@ def test_is_copy_consistent(self):
103105
# Copy consistency with a really long name
104106
long_class_name = "TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason"
105107
self.check_copy_consistency(
108+
diffusers_dir,
106109
f"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}",
107110
f"{long_class_name}SchedulerOutput",
108111
re.sub("Bert", long_class_name, REFERENCE_CODE),
109112
)
110113

111114
# Copy consistency with overwrite
112115
self.check_copy_consistency(
116+
diffusers_dir,
113117
"# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test",
114118
"TestSchedulerOutput",
115119
REFERENCE_CODE,

tests/others/test_check_dummies.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import os
1616
import sys
17-
import unittest
1817

1918

2019
git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
@@ -28,48 +27,46 @@
2827
check_dummies.PATH_TO_DIFFUSERS = os.path.join(git_repo_path, "src", "diffusers")
2928

3029

31-
class CheckDummiesTester(unittest.TestCase):
30+
class TestCheckDummies:
3231
def test_find_backend(self):
3332
simple_backend = find_backend(" if not is_torch_available():")
34-
self.assertEqual(simple_backend, "torch")
33+
assert simple_backend == "torch"
3534

3635
# backend_with_underscore = find_backend(" if not is_tensorflow_text_available():")
37-
# self.assertEqual(backend_with_underscore, "tensorflow_text")
36+
# assert backend_with_underscore == "tensorflow_text"
3837

3938
double_backend = find_backend(" if not (is_torch_available() and is_transformers_available()):")
40-
self.assertEqual(double_backend, "torch_and_transformers")
39+
assert double_backend == "torch_and_transformers"
4140

4241
# double_backend_with_underscore = find_backend(
4342
# " if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
4443
# )
45-
# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")
44+
# assert double_backend_with_underscore == "sentencepiece_and_tensorflow_text"
4645

4746
triple_backend = find_backend(
4847
" if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"
4948
)
50-
self.assertEqual(triple_backend, "torch_and_transformers_and_onnx")
49+
assert triple_backend == "torch_and_transformers_and_onnx"
5150

5251
def test_read_init(self):
5352
objects = read_init()
5453
# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
55-
self.assertIn("torch", objects)
56-
self.assertIn("torch_and_transformers", objects)
57-
self.assertIn("torch_and_transformers_and_onnx", objects)
54+
assert "torch" in objects
55+
assert "torch_and_transformers" in objects
56+
assert "torch_and_transformers_and_onnx" in objects
5857

5958
# Likewise, we can't assert on the exact content of a key
60-
self.assertIn("UNet2DModel", objects["torch"])
61-
self.assertIn("StableDiffusionPipeline", objects["torch_and_transformers"])
62-
self.assertIn("LMSDiscreteScheduler", objects["torch_and_scipy"])
63-
self.assertIn("OnnxStableDiffusionPipeline", objects["torch_and_transformers_and_onnx"])
59+
assert "UNet2DModel" in objects["torch"]
60+
assert "StableDiffusionPipeline" in objects["torch_and_transformers"]
61+
assert "LMSDiscreteScheduler" in objects["torch_and_scipy"]
62+
assert "OnnxStableDiffusionPipeline" in objects["torch_and_transformers_and_onnx"]
6463

6564
def test_create_dummy_object(self):
6665
dummy_constant = create_dummy_object("CONSTANT", "'torch'")
67-
self.assertEqual(dummy_constant, "\nCONSTANT = None\n")
66+
assert dummy_constant == "\nCONSTANT = None\n"
6867

6968
dummy_function = create_dummy_object("function", "'torch'")
70-
self.assertEqual(
71-
dummy_function, "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n"
72-
)
69+
assert dummy_function == "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n"
7370

7471
expected_dummy_class = """
7572
class FakeClass(metaclass=DummyObject):
@@ -87,7 +84,7 @@ def from_pretrained(cls, *args, **kwargs):
8784
requires_backends(cls, 'torch')
8885
"""
8986
dummy_class = create_dummy_object("FakeClass", "'torch'")
90-
self.assertEqual(dummy_class, expected_dummy_class)
87+
assert dummy_class == expected_dummy_class
9188

9289
def test_create_dummy_files(self):
9390
expected_dummy_pytorch_file = """# This file is autogenerated by the command `make fix-copies`, do not edit.
@@ -116,4 +113,4 @@ def from_pretrained(cls, *args, **kwargs):
116113
requires_backends(cls, ["torch"])
117114
"""
118115
dummy_files = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]})
119-
self.assertEqual(dummy_files["torch"], expected_dummy_pytorch_file)
116+
assert dummy_files["torch"] == expected_dummy_pytorch_file
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import sys
3-
import unittest
43
from unittest.mock import mock_open, patch
54

65

@@ -10,10 +9,8 @@
109
from check_support_list import check_documentation # noqa: E402
1110

1211

13-
class TestCheckSupportList(unittest.TestCase):
14-
def setUp(self):
15-
# Mock doc and source contents that we can reuse
16-
self.doc_content = """# Documentation
12+
# Mock doc and source contents that we can reuse
13+
DOC_CONTENT = """# Documentation
1714
## FooProcessor
1815
1916
[[autodoc]] module.FooProcessor
@@ -22,20 +19,22 @@ def setUp(self):
2219
2320
[[autodoc]] module.BarProcessor
2421
"""
25-
self.source_content = """
22+
SOURCE_CONTENT = """
2623
class FooProcessor(nn.Module):
2724
pass
2825
2926
class BarProcessor(nn.Module):
3027
pass
3128
"""
3229

30+
31+
class TestCheckSupportList:
3332
def test_check_documentation_all_documented(self):
3433
# In this test, both FooProcessor and BarProcessor are documented
35-
with patch("builtins.open", mock_open(read_data=self.doc_content)) as doc_file:
34+
with patch("builtins.open", mock_open(read_data=DOC_CONTENT)) as doc_file:
3635
doc_file.side_effect = [
37-
mock_open(read_data=self.doc_content).return_value,
38-
mock_open(read_data=self.source_content).return_value,
36+
mock_open(read_data=DOC_CONTENT).return_value,
37+
mock_open(read_data=SOURCE_CONTENT).return_value,
3938
]
4039

4140
undocumented = check_documentation(
@@ -44,7 +43,7 @@ def test_check_documentation_all_documented(self):
4443
doc_regex=r"\[\[autodoc\]\]\s([^\n]+)",
4544
src_regex=r"class\s+(\w+Processor)\(.*?nn\.Module.*?\):",
4645
)
47-
self.assertEqual(len(undocumented), 0, f"Expected no undocumented classes, got {undocumented}")
46+
assert len(undocumented) == 0, f"Expected no undocumented classes, got {undocumented}"
4847

4948
def test_check_documentation_missing_class(self):
5049
# In this test, only FooProcessor is documented, but BarProcessor is missing from the docs
@@ -56,7 +55,7 @@ def test_check_documentation_missing_class(self):
5655
with patch("builtins.open", mock_open(read_data=doc_content_missing)) as doc_file:
5756
doc_file.side_effect = [
5857
mock_open(read_data=doc_content_missing).return_value,
59-
mock_open(read_data=self.source_content).return_value,
58+
mock_open(read_data=SOURCE_CONTENT).return_value,
6059
]
6160

6261
undocumented = check_documentation(
@@ -65,4 +64,4 @@ def test_check_documentation_missing_class(self):
6564
doc_regex=r"\[\[autodoc\]\]\s([^\n]+)",
6665
src_regex=r"class\s+(\w+Processor)\(.*?nn\.Module.*?\):",
6766
)
68-
self.assertIn("BarProcessor", undocumented, f"BarProcessor should be undocumented, got {undocumented}")
67+
assert "BarProcessor" in undocumented, f"BarProcessor should be undocumented, got {undocumented}"

tests/others/test_config.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# limitations under the License.
1515

1616
import json
17-
import tempfile
18-
import unittest
1917
from pathlib import Path
2018

19+
import pytest
20+
2121
from diffusers import (
2222
DDIMScheduler,
2323
DDPMScheduler,
@@ -102,9 +102,9 @@ def __init__(self, test_file_1=Path("foo/bar"), test_file_2=Path("foo bar\\bar")
102102
pass
103103

104104

105-
class ConfigTester(unittest.TestCase):
105+
class TestConfig:
106106
def test_load_not_from_mixin(self):
107-
with self.assertRaises(ValueError):
107+
with pytest.raises(ValueError):
108108
ConfigMixin.load_config("dummy_path")
109109

110110
def test_register_to_config(self):
@@ -143,7 +143,7 @@ def test_register_to_config(self):
143143
assert config["d"] == "for diffusion"
144144
assert config["e"] == [1, 3]
145145

146-
def test_save_load(self):
146+
def test_save_load(self, tmp_path):
147147
obj = SampleObject()
148148
config = obj.config
149149

@@ -153,10 +153,9 @@ def test_save_load(self):
153153
assert config["d"] == "for diffusion"
154154
assert config["e"] == [1, 3]
155155

156-
with tempfile.TemporaryDirectory() as tmpdirname:
157-
obj.save_config(tmpdirname)
158-
new_obj = SampleObject.from_config(SampleObject.load_config(tmpdirname))
159-
new_config = new_obj.config
156+
obj.save_config(tmp_path)
157+
new_obj = SampleObject.from_config(SampleObject.load_config(tmp_path))
158+
new_config = new_obj.config
160159

161160
# unfreeze configs
162161
config = dict(config)
@@ -262,7 +261,7 @@ def test_load_dpmsolver(self):
262261
# no warning should be thrown
263262
assert cap_logger.out == ""
264263

265-
def test_use_default_values(self):
264+
def test_use_default_values(self, tmp_path):
266265
# let's first save a config that should be in the form
267266
# a=2,
268267
# b=5,
@@ -277,14 +276,13 @@ def test_use_default_values(self):
277276
# make sure that default config has all keys in `_use_default_values`
278277
assert set(config_dict.keys()) == set(config.config._use_default_values)
279278

280-
with tempfile.TemporaryDirectory() as tmpdirname:
281-
config.save_config(tmpdirname)
279+
config.save_config(tmp_path)
282280

283-
# now loading it with SampleObject2 should put f into `_use_default_values`
284-
config = SampleObject2.from_config(SampleObject2.load_config(tmpdirname))
281+
# now loading it with SampleObject2 should put f into `_use_default_values`
282+
config = SampleObject2.from_config(SampleObject2.load_config(tmp_path))
285283

286-
assert "f" in config.config._use_default_values
287-
assert config.config.f == [1, 3]
284+
assert "f" in config.config._use_default_values
285+
assert config.config.f == [1, 3]
288286

289287
# now loading the config, should **NOT** use [1, 3] for `f`, but the default [1, 4] value
290288
# **BECAUSE** it is part of `config.config._use_default_values`

0 commit comments

Comments
 (0)