Skip to content

Commit 90b4e34

Browse files
authored
tests: fix qwen tests from getting oom'd in our CI. (#14474)
1 parent 69a0d5a commit 90b4e34

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

tests/pipelines/qwenimage/test_qwenimage_edit.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
QwenImageTransformer2DModel,
2525
)
2626

27-
from ...testing_utils import assert_tensors_close, torch_device
27+
from ...testing_utils import assert_tensors_close, require_accelerator_memory, torch_device
2828
from ..testing_utils import (
2929
BasePipelineTesterConfig,
3030
MemoryTesterMixin,
@@ -170,6 +170,15 @@ def test_vae_tiling(self, expected_diff_max: float = 0.2):
170170
"VAE tiling should not affect the inference results."
171171
)
172172

173+
# The condition image is always resized to ~1 megapixel before it is encoded, so the VAE attention runs over
174+
# 65k tokens no matter how small the dummy inputs are. On GPUs without bf16 SDPA support that attention falls
175+
# back to the math backend, which materializes the full attention matrix (~16GB).
176+
@require_accelerator_memory(24)
177+
@pytest.mark.skipif(torch_device not in ["cuda", "xpu"], reason="half-precision inference requires CUDA or XPU")
178+
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16], ids=str)
179+
def test_half_precision_inference_no_nan(self, dtype):
180+
super().test_half_precision_inference_no_nan(dtype)
181+
173182
@pytest.mark.xfail(condition=True, reason="Preconfigured embeddings need to be revisited.", strict=True)
174183
def test_encode_prompt_works_in_isolation(self, extra_required_param_value_dict=None, atol=1e-4, rtol=1e-4):
175184
super().test_encode_prompt_works_in_isolation(extra_required_param_value_dict, atol, rtol)
@@ -200,4 +209,7 @@ def test_true_cfg_without_negative_prompt_embeds_mask(self):
200209

201210

202211
class TestQwenImageEditPipelineMemory(QwenImageEditPipelineTesterConfig, MemoryTesterMixin):
203-
pass
212+
# Runs the pipeline in bf16, see the note on `test_half_precision_inference_no_nan` above.
213+
@require_accelerator_memory(24)
214+
def test_layerwise_casting_inference(self):
215+
super().test_layerwise_casting_inference()

tests/pipelines/qwenimage/test_qwenimage_edit_plus.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
QwenImageTransformer2DModel,
2525
)
2626

27-
from ...testing_utils import assert_tensors_close, torch_device
27+
from ...testing_utils import assert_tensors_close, require_accelerator_memory, torch_device
2828
from ..testing_utils import BasePipelineTesterConfig, MemoryTesterMixin, PipelineTesterMixin
2929

3030

@@ -164,6 +164,15 @@ def test_vae_tiling(self, expected_diff_max: float = 0.2):
164164
"VAE tiling should not affect the inference results."
165165
)
166166

167+
# The condition image is always resized to ~1 megapixel before it is encoded, so the VAE attention runs over
168+
# 65k tokens no matter how small the dummy inputs are. On GPUs without bf16 SDPA support that attention falls
169+
# back to the math backend, which materializes the full attention matrix (~16GB).
170+
@require_accelerator_memory(24)
171+
@pytest.mark.skipif(torch_device not in ["cuda", "xpu"], reason="half-precision inference requires CUDA or XPU")
172+
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16], ids=str)
173+
def test_half_precision_inference_no_nan(self, dtype):
174+
super().test_half_precision_inference_no_nan(dtype)
175+
167176
@pytest.mark.xfail(condition=True, reason="Preconfigured embeddings need to be revisited.", strict=True)
168177
def test_encode_prompt_works_in_isolation(self, extra_required_param_value_dict=None, atol=1e-4, rtol=1e-4):
169178
super().test_encode_prompt_works_in_isolation(extra_required_param_value_dict, atol, rtol)
@@ -206,4 +215,7 @@ def test_true_cfg_without_negative_prompt_embeds_mask(self):
206215

207216

208217
class TestQwenImageEditPlusPipelineMemory(QwenImageEditPlusPipelineTesterConfig, MemoryTesterMixin):
209-
pass
218+
# Runs the pipeline in bf16, see the note on `test_half_precision_inference_no_nan` above.
219+
@require_accelerator_memory(24)
220+
def test_layerwise_casting_inference(self):
221+
super().test_layerwise_casting_inference()

tests/testing_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,28 @@ def require_big_accelerator(test_case):
660660
)(test_case)
661661

662662

663+
def require_accelerator_memory(min_memory_gb: int):
664+
"""
665+
Decorator marking a test that needs at least `min_memory_gb` GB of memory on the accelerator it runs on. Tests
666+
running on CPU are not affected.
667+
"""
668+
669+
def decorator(test_case):
670+
if torch_device == "cuda" and torch.cuda.is_available():
671+
total_memory = torch.cuda.get_device_properties(0).total_memory / (1024**3)
672+
elif torch_device == "xpu" and torch.xpu.is_available():
673+
total_memory = torch.xpu.get_device_properties(0).total_memory / (1024**3)
674+
else:
675+
return test_case
676+
677+
return pytest.mark.skipif(
678+
total_memory < min_memory_gb,
679+
reason=f"test requires an accelerator with at least {min_memory_gb} GB memory",
680+
)(test_case)
681+
682+
return decorator
683+
684+
663685
def require_torch_accelerator_with_training(test_case):
664686
"""Decorator marking a test that requires an accelerator with support for training."""
665687
return pytest.mark.skipif(

0 commit comments

Comments
 (0)