Skip to content

Commit 650424e

Browse files
committed
fix multiple issues.
1 parent 8144a0e commit 650424e

3 files changed

Lines changed: 47 additions & 114 deletions

File tree

tests/modular_pipelines/qwen/test_modular_pipeline_qwenimage.py

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

1616

17-
import numpy as np
1817
import PIL
1918
import pytest
2019

21-
from diffusers import ClassifierFreeGuidance
2220
from diffusers.modular_pipelines import (
2321
QwenImageAutoBlocks,
2422
QwenImageEditAutoBlocks,
@@ -28,32 +26,10 @@
2826
QwenImageModularPipeline,
2927
)
3028

31-
from ...testing_utils import torch_device
32-
from ..test_modular_pipelines_common import ModularPipelineTesterMixin
29+
from ..test_modular_pipelines_common import ModularGuiderTesterMixin, ModularPipelineTesterMixin
3330

3431

35-
class QwenImageModularGuiderMixin:
36-
def test_guider_cfg(self, tol=1e-2):
37-
pipe = self.get_pipeline()
38-
pipe = pipe.to(torch_device)
39-
40-
guider = ClassifierFreeGuidance(guidance_scale=1.0)
41-
pipe.update_components(guider=guider)
42-
43-
inputs = self.get_dummy_inputs()
44-
out_no_cfg = pipe(**inputs, output="images")
45-
46-
guider = ClassifierFreeGuidance(guidance_scale=7.5)
47-
pipe.update_components(guider=guider)
48-
inputs = self.get_dummy_inputs()
49-
out_cfg = pipe(**inputs, output="images")
50-
51-
assert out_cfg.shape == out_no_cfg.shape
52-
max_diff = np.abs(out_cfg - out_no_cfg).max()
53-
assert max_diff > tol, "Output with CFG must be different from normal inference"
54-
55-
56-
class TestQwenImageModularPipelineFast(ModularPipelineTesterMixin, QwenImageModularGuiderMixin):
32+
class TestQwenImageModularPipelineFast(ModularPipelineTesterMixin, ModularGuiderTesterMixin):
5733
pipeline_class = QwenImageModularPipeline
5834
pipeline_blocks_class = QwenImageAutoBlocks
5935
repo = "hf-internal-testing/tiny-qwenimage-modular"
@@ -76,7 +52,7 @@ def get_dummy_inputs(self):
7652
return inputs
7753

7854

79-
class TestQwenImageEditModularPipelineFast(ModularPipelineTesterMixin, QwenImageModularGuiderMixin):
55+
class TestQwenImageEditModularPipelineFast(ModularPipelineTesterMixin, ModularGuiderTesterMixin):
8056
pipeline_class = QwenImageEditModularPipeline
8157
pipeline_blocks_class = QwenImageEditAutoBlocks
8258
repo = "hf-internal-testing/tiny-qwenimage-edit-modular"
@@ -102,7 +78,7 @@ def test_guider_cfg(self):
10278
super().test_guider_cfg(7e-5)
10379

10480

105-
class TestQwenImageEditPlusModularPipelineFast(ModularPipelineTesterMixin, QwenImageModularGuiderMixin):
81+
class TestQwenImageEditPlusModularPipelineFast(ModularPipelineTesterMixin, ModularGuiderTesterMixin):
10682
pipeline_class = QwenImageEditPlusModularPipeline
10783
pipeline_blocks_class = QwenImageEditPlusAutoBlocks
10884
repo = "hf-internal-testing/tiny-qwenimage-edit-plus-modular"

tests/modular_pipelines/stable_diffusion_xl/test_modular_pipeline_stable_diffusion_xl.py

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from ...models.unets.test_models_unet_2d_condition import create_ip_adapter_state_dict
2727
from ...testing_utils import enable_full_determinism, floats_tensor, torch_device
28-
from ..test_modular_pipelines_common import ModularPipelineTesterMixin
28+
from ..test_modular_pipelines_common import ModularGuiderTesterMixin, ModularPipelineTesterMixin
2929

3030

3131
enable_full_determinism()
@@ -37,13 +37,11 @@ class SDXLModularTesterMixin:
3737
"""
3838

3939
def _test_stable_diffusion_xl_euler(self, expected_image_shape, expected_slice, expected_max_diff=1e-2):
40-
sd_pipe = self.get_pipeline()
41-
sd_pipe = sd_pipe.to(torch_device)
42-
sd_pipe.set_progress_bar_config(disable=None)
40+
sd_pipe = self.get_pipeline().to(torch_device)
4341

4442
inputs = self.get_dummy_inputs()
4543
image = sd_pipe(**inputs, output="images")
46-
image_slice = image[0, -3:, -3:, -1]
44+
image_slice = image[0, -3:, -3:, -1].cpu()
4745

4846
assert image.shape == expected_image_shape
4947
max_diff = torch.abs(image_slice.flatten() - expected_slice).max()
@@ -110,7 +108,7 @@ def test_ip_adapter(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
110108
pipe = blocks.init_pipeline(self.repo)
111109
pipe.load_components(torch_dtype=torch.float32)
112110
pipe = pipe.to(torch_device)
113-
pipe.set_progress_bar_config(disable=None)
111+
114112
cross_attention_dim = pipe.unet.config.get("cross_attention_dim")
115113

116114
# forward pass without ip adapter
@@ -219,9 +217,7 @@ def test_controlnet(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
219217
# compare against static slices and that can be shaky (with a VVVV low probability).
220218
expected_max_diff = 9e-4 if torch_device == "cpu" else expected_max_diff
221219

222-
pipe = self.get_pipeline()
223-
pipe = pipe.to(torch_device)
224-
pipe.set_progress_bar_config(disable=None)
220+
pipe = self.get_pipeline().to(torch_device)
225221

226222
# forward pass without controlnet
227223
inputs = self.get_dummy_inputs()
@@ -251,9 +247,7 @@ def test_controlnet(self, expected_max_diff: float = 1e-4, expected_pipe_slice=N
251247
assert max_diff_with_controlnet_scale > 1e-2, "Output with controlnet must be different from normal inference"
252248

253249
def test_controlnet_cfg(self):
254-
pipe = self.get_pipeline()
255-
pipe = pipe.to(torch_device)
256-
pipe.set_progress_bar_config(disable=None)
250+
pipe = self.get_pipeline().to(torch_device)
257251

258252
# forward pass with CFG not applied
259253
guider = ClassifierFreeGuidance(guidance_scale=1.0)
@@ -273,35 +267,11 @@ def test_controlnet_cfg(self):
273267
assert max_diff > 1e-2, "Output with CFG must be different from normal inference"
274268

275269

276-
class SDXLModularGuiderTesterMixin:
277-
def test_guider_cfg(self):
278-
pipe = self.get_pipeline()
279-
pipe = pipe.to(torch_device)
280-
pipe.set_progress_bar_config(disable=None)
281-
282-
# forward pass with CFG not applied
283-
guider = ClassifierFreeGuidance(guidance_scale=1.0)
284-
pipe.update_components(guider=guider)
285-
286-
inputs = self.get_dummy_inputs()
287-
out_no_cfg = pipe(**inputs, output="images")
288-
289-
# forward pass with CFG applied
290-
guider = ClassifierFreeGuidance(guidance_scale=7.5)
291-
pipe.update_components(guider=guider)
292-
inputs = self.get_dummy_inputs()
293-
out_cfg = pipe(**inputs, output="images")
294-
295-
assert out_cfg.shape == out_no_cfg.shape
296-
max_diff = np.abs(out_cfg - out_no_cfg).max()
297-
assert max_diff > 1e-2, "Output with CFG must be different from normal inference"
298-
299-
300270
class TestSDXLModularPipelineFast(
301271
SDXLModularTesterMixin,
302272
SDXLModularIPAdapterTesterMixin,
303273
SDXLModularControlNetTesterMixin,
304-
SDXLModularGuiderTesterMixin,
274+
ModularGuiderTesterMixin,
305275
ModularPipelineTesterMixin,
306276
):
307277
"""Test cases for Stable Diffusion XL modular pipeline fast tests."""
@@ -335,18 +305,7 @@ def test_stable_diffusion_xl_euler(self):
335305
self._test_stable_diffusion_xl_euler(
336306
expected_image_shape=self.expected_image_output_shape,
337307
expected_slice=torch.tensor(
338-
[
339-
0.5966781,
340-
0.62939394,
341-
0.48465094,
342-
0.51573336,
343-
0.57593524,
344-
0.47035995,
345-
0.53410417,
346-
0.51436996,
347-
0.47313565,
348-
],
349-
device=torch_device,
308+
[0.3886, 0.4685, 0.4953, 0.4217, 0.4317, 0.3945, 0.4847, 0.4704, 0.4731],
350309
),
351310
expected_max_diff=1e-2,
352311
)
@@ -359,7 +318,7 @@ class TestSDXLImg2ImgModularPipelineFast(
359318
SDXLModularTesterMixin,
360319
SDXLModularIPAdapterTesterMixin,
361320
SDXLModularControlNetTesterMixin,
362-
SDXLModularGuiderTesterMixin,
321+
ModularGuiderTesterMixin,
363322
ModularPipelineTesterMixin,
364323
):
365324
"""Test cases for Stable Diffusion XL image-to-image modular pipeline fast tests."""
@@ -400,20 +359,7 @@ def get_dummy_inputs(self, seed=0):
400359
def test_stable_diffusion_xl_euler(self):
401360
self._test_stable_diffusion_xl_euler(
402361
expected_image_shape=self.expected_image_output_shape,
403-
expected_slice=torch.tensor(
404-
[
405-
0.56943184,
406-
0.4702148,
407-
0.48048905,
408-
0.6235963,
409-
0.551138,
410-
0.49629188,
411-
0.60031277,
412-
0.5688907,
413-
0.43996853,
414-
],
415-
device=torch_device,
416-
),
362+
expected_slice=torch.tensor([0.5246, 0.4466, 0.444, 0.3246, 0.4443, 0.5108, 0.5225, 0.559, 0.5147]),
417363
expected_max_diff=1e-2,
418364
)
419365

@@ -425,7 +371,7 @@ class SDXLInpaintingModularPipelineFastTests(
425371
SDXLModularTesterMixin,
426372
SDXLModularIPAdapterTesterMixin,
427373
SDXLModularControlNetTesterMixin,
428-
SDXLModularGuiderTesterMixin,
374+
ModularGuiderTesterMixin,
429375
ModularPipelineTesterMixin,
430376
):
431377
"""Test cases for Stable Diffusion XL inpainting modular pipeline fast tests."""

tests/modular_pipelines/test_modular_pipelines_common.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import diffusers
99
from diffusers import ComponentsManager, ModularPipeline, ModularPipelineBlocks
10+
from diffusers.guiders import ClassifierFreeGuidance
1011
from diffusers.utils import logging
1112

1213
from ..testing_utils import backend_empty_cache, numpy_cosine_similarity_distance, torch_device
@@ -104,6 +105,7 @@ def teardown_method(self):
104105
def get_pipeline(self, components_manager=None, torch_dtype=torch.float32):
105106
pipeline = self.pipeline_blocks_class().init_pipeline(self.repo, components_manager=components_manager)
106107
pipeline.load_components(torch_dtype=torch_dtype)
108+
pipeline.set_progress_bar_config(disable=None)
107109
return pipeline
108110

109111
def test_pipeline_call_signature(self):
@@ -121,9 +123,7 @@ def _check_for_parameters(parameters, expected_parameters, param_type):
121123
_check_for_parameters(self.optional_params, optional_parameters, "optional")
122124

123125
def test_inference_batch_consistent(self, batch_sizes=[2], batch_generator=True):
124-
pipe = self.get_pipeline()
125-
pipe.to(torch_device)
126-
pipe.set_progress_bar_config(disable=None)
126+
pipe = self.get_pipeline().to(torch_device)
127127

128128
inputs = self.get_dummy_inputs()
129129
inputs["generator"] = self.get_generator(0)
@@ -162,9 +162,8 @@ def test_inference_batch_single_identical(
162162
batch_size=2,
163163
expected_max_diff=1e-4,
164164
):
165-
pipe = self.get_pipeline()
166-
pipe.to(torch_device)
167-
pipe.set_progress_bar_config(disable=None)
165+
pipe = self.get_pipeline().to(torch_device)
166+
168167
inputs = self.get_dummy_inputs()
169168

170169
# Reset generator in case it is has been used in self.get_dummy_inputs
@@ -202,7 +201,6 @@ def test_inference_batch_single_identical(
202201
def test_float16_inference(self, expected_max_diff=5e-2):
203202
pipe = self.get_pipeline()
204203
pipe.to(torch_device, torch.float32)
205-
pipe.set_progress_bar_config(disable=None)
206204

207205
pipe_fp16 = self.get_pipeline()
208206
pipe_fp16.to(torch_device, torch.float16)
@@ -229,10 +227,8 @@ def test_float16_inference(self, expected_max_diff=5e-2):
229227

230228
@pytest.mark.skipif(torch_device == "cpu", reason="Test needs an accelerator.")
231229
def test_to_device(self):
232-
pipe = self.get_pipeline()
233-
pipe.set_progress_bar_config(disable=None)
230+
pipe = self.get_pipeline().to("cpu")
234231

235-
pipe.to("cpu")
236232
model_devices = [
237233
component.device.type for component in pipe.components.values() if hasattr(component, "device")
238234
]
@@ -247,30 +243,23 @@ def test_to_device(self):
247243
)
248244

249245
def test_inference_is_not_nan_cpu(self):
250-
pipe = self.get_pipeline()
251-
pipe.set_progress_bar_config(disable=None)
252-
pipe.to("cpu")
246+
pipe = self.get_pipeline().to("cpu")
253247

254248
output = pipe(**self.get_dummy_inputs(), output="images")
255249
assert torch.isnan(output).sum() == 0, "CPU Inference returns NaN"
256250

257251
@pytest.mark.skipif(torch_device == "cpu", reason="Test needs an accelerator.")
258252
def test_inference_is_not_nan(self):
259-
pipe = self.get_pipeline()
260-
pipe.set_progress_bar_config(disable=None)
261-
pipe.to(torch_device)
253+
pipe = self.get_pipeline().to(torch_device)
262254

263255
output = pipe(**self.get_dummy_inputs(), output="images")
264256
assert torch.isnan(output).sum() == 0, "Accelerator Inference returns NaN"
265257

266258
def test_num_images_per_prompt(self):
267-
pipe = self.get_pipeline()
259+
pipe = self.get_pipeline().to(torch_device)
268260

269261
if "num_images_per_prompt" not in pipe.blocks.input_names:
270-
return
271-
272-
pipe = pipe.to(torch_device)
273-
pipe.set_progress_bar_config(disable=None)
262+
pytest.mark.skip("Skipping test as `num_images_per_prompt` is not present in input names.")
274263

275264
batch_sizes = [1, 2]
276265
num_images_per_prompts = [1, 2]
@@ -325,3 +314,25 @@ def test_save_from_pretrained(self):
325314
image_slices.append(image[0, -3:, -3:, -1].flatten())
326315

327316
assert torch.abs(image_slices[0] - image_slices[1]).max() < 1e-3
317+
318+
319+
class ModularGuiderTesterMixin:
320+
def test_guider_cfg(self, expected_max_diff=1e-2):
321+
pipe = self.get_pipeline().to(torch_device)
322+
323+
# forward pass with CFG not applied
324+
guider = ClassifierFreeGuidance(guidance_scale=1.0)
325+
pipe.update_components(guider=guider)
326+
327+
inputs = self.get_dummy_inputs()
328+
out_no_cfg = pipe(**inputs, output="images")
329+
330+
# forward pass with CFG applied
331+
guider = ClassifierFreeGuidance(guidance_scale=7.5)
332+
pipe.update_components(guider=guider)
333+
inputs = self.get_dummy_inputs()
334+
out_cfg = pipe(**inputs, output="images")
335+
336+
assert out_cfg.shape == out_no_cfg.shape
337+
max_diff = torch.abs(out_cfg - out_no_cfg).max()
338+
assert max_diff > expected_max_diff, "Output with CFG must be different from normal inference"

0 commit comments

Comments
 (0)