Skip to content

Commit 24b5f6f

Browse files
a-r-r-o-wtolgacangoz
authored andcommitted
[tests] Improve Flux tests (huggingface#11919)
update
1 parent eb31dff commit 24b5f6f

File tree

1 file changed

+30
-77
lines changed

1 file changed

+30
-77
lines changed

tests/pipelines/flux/test_pipeline_flux.py

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_flux_different_prompts(self):
155155

156156
# Outputs should be different here
157157
# For some reasons, they don't show large differences
158-
assert max_diff > 1e-6
158+
self.assertGreater(max_diff, 1e-6, "Outputs should be different for different prompts.")
159159

160160
def test_fused_qkv_projections(self):
161161
device = "cpu" # ensure determinism for the device-dependent torch.Generator
@@ -187,14 +187,17 @@ def test_fused_qkv_projections(self):
187187
image = pipe(**inputs).images
188188
image_slice_disabled = image[0, -3:, -3:, -1]
189189

190-
assert np.allclose(original_image_slice, image_slice_fused, atol=1e-3, rtol=1e-3), (
191-
"Fusion of QKV projections shouldn't affect the outputs."
190+
self.assertTrue(
191+
np.allclose(original_image_slice, image_slice_fused, atol=1e-3, rtol=1e-3),
192+
("Fusion of QKV projections shouldn't affect the outputs."),
192193
)
193-
assert np.allclose(image_slice_fused, image_slice_disabled, atol=1e-3, rtol=1e-3), (
194-
"Outputs, with QKV projection fusion enabled, shouldn't change when fused QKV projections are disabled."
194+
self.assertTrue(
195+
np.allclose(image_slice_fused, image_slice_disabled, atol=1e-3, rtol=1e-3),
196+
("Outputs, with QKV projection fusion enabled, shouldn't change when fused QKV projections are disabled."),
195197
)
196-
assert np.allclose(original_image_slice, image_slice_disabled, atol=1e-2, rtol=1e-2), (
197-
"Original outputs should match when fused QKV projections are disabled."
198+
self.assertTrue(
199+
np.allclose(original_image_slice, image_slice_disabled, atol=1e-2, rtol=1e-2),
200+
("Original outputs should match when fused QKV projections are disabled."),
198201
)
199202

200203
def test_flux_image_output_shape(self):
@@ -209,7 +212,11 @@ def test_flux_image_output_shape(self):
209212
inputs.update({"height": height, "width": width})
210213
image = pipe(**inputs).images[0]
211214
output_height, output_width, _ = image.shape
212-
assert (output_height, output_width) == (expected_height, expected_width)
215+
self.assertEqual(
216+
(output_height, output_width),
217+
(expected_height, expected_width),
218+
f"Output shape {image.shape} does not match expected shape {(expected_height, expected_width)}",
219+
)
213220

214221
def test_flux_true_cfg(self):
215222
pipe = self.pipeline_class(**self.get_dummy_components()).to(torch_device)
@@ -220,7 +227,9 @@ def test_flux_true_cfg(self):
220227
inputs["negative_prompt"] = "bad quality"
221228
inputs["true_cfg_scale"] = 2.0
222229
true_cfg_out = pipe(**inputs, generator=torch.manual_seed(0)).images[0]
223-
assert not np.allclose(no_true_cfg_out, true_cfg_out)
230+
self.assertFalse(
231+
np.allclose(no_true_cfg_out, true_cfg_out), "Outputs should be different when true_cfg_scale is set."
232+
)
224233

225234

226235
@nightly
@@ -269,45 +278,17 @@ def test_flux_inference(self):
269278

270279
image = pipe(**inputs).images[0]
271280
image_slice = image[0, :10, :10]
281+
# fmt: off
272282
expected_slice = np.array(
273-
[
274-
0.3242,
275-
0.3203,
276-
0.3164,
277-
0.3164,
278-
0.3125,
279-
0.3125,
280-
0.3281,
281-
0.3242,
282-
0.3203,
283-
0.3301,
284-
0.3262,
285-
0.3242,
286-
0.3281,
287-
0.3242,
288-
0.3203,
289-
0.3262,
290-
0.3262,
291-
0.3164,
292-
0.3262,
293-
0.3281,
294-
0.3184,
295-
0.3281,
296-
0.3281,
297-
0.3203,
298-
0.3281,
299-
0.3281,
300-
0.3164,
301-
0.3320,
302-
0.3320,
303-
0.3203,
304-
],
283+
[0.3242, 0.3203, 0.3164, 0.3164, 0.3125, 0.3125, 0.3281, 0.3242, 0.3203, 0.3301, 0.3262, 0.3242, 0.3281, 0.3242, 0.3203, 0.3262, 0.3262, 0.3164, 0.3262, 0.3281, 0.3184, 0.3281, 0.3281, 0.3203, 0.3281, 0.3281, 0.3164, 0.3320, 0.3320, 0.3203],
305284
dtype=np.float32,
306285
)
286+
# fmt: on
307287

308288
max_diff = numpy_cosine_similarity_distance(expected_slice.flatten(), image_slice.flatten())
309-
310-
assert max_diff < 1e-4
289+
self.assertLess(
290+
max_diff, 1e-4, f"Image slice is different from expected slice: {image_slice} != {expected_slice}"
291+
)
311292

312293

313294
@slow
@@ -377,42 +358,14 @@ def test_flux_ip_adapter_inference(self):
377358
image = pipe(**inputs).images[0]
378359
image_slice = image[0, :10, :10]
379360

361+
# fmt: off
380362
expected_slice = np.array(
381-
[
382-
0.1855,
383-
0.1680,
384-
0.1406,
385-
0.1953,
386-
0.1699,
387-
0.1465,
388-
0.2012,
389-
0.1738,
390-
0.1484,
391-
0.2051,
392-
0.1797,
393-
0.1523,
394-
0.2012,
395-
0.1719,
396-
0.1445,
397-
0.2070,
398-
0.1777,
399-
0.1465,
400-
0.2090,
401-
0.1836,
402-
0.1484,
403-
0.2129,
404-
0.1875,
405-
0.1523,
406-
0.2090,
407-
0.1816,
408-
0.1484,
409-
0.2110,
410-
0.1836,
411-
0.1543,
412-
],
363+
[0.1855, 0.1680, 0.1406, 0.1953, 0.1699, 0.1465, 0.2012, 0.1738, 0.1484, 0.2051, 0.1797, 0.1523, 0.2012, 0.1719, 0.1445, 0.2070, 0.1777, 0.1465, 0.2090, 0.1836, 0.1484, 0.2129, 0.1875, 0.1523, 0.2090, 0.1816, 0.1484, 0.2110, 0.1836, 0.1543],
413364
dtype=np.float32,
414365
)
366+
# fmt: on
415367

416368
max_diff = numpy_cosine_similarity_distance(expected_slice.flatten(), image_slice.flatten())
417-
418-
assert max_diff < 1e-4, f"{image_slice} != {expected_slice}"
369+
self.assertLess(
370+
max_diff, 1e-4, f"Image slice is different from expected slice: {image_slice} != {expected_slice}"
371+
)

0 commit comments

Comments
 (0)