Skip to content

Commit a7cd73d

Browse files
committed
enable big gpu cases
1 parent 1f04f66 commit a7cd73d

File tree

6 files changed

+54
-26
lines changed

6 files changed

+54
-26
lines changed

src/diffusers/utils/testing_utils.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,17 @@ def require_torch_multi_gpu(test_case):
322322

323323
def require_torch_multi_accelerator(test_case):
324324
"""
325-
Decorator marking a test that requires a multi-accelerator setup (in PyTorch). These tests are skipped on a machine without
326-
multiple hardware accelerators.
325+
Decorator marking a test that requires a multi-accelerator setup (in PyTorch). These tests are skipped on a machine
326+
without multiple hardware accelerators.
327327
"""
328328
if not is_torch_available():
329329
return unittest.skip("test requires PyTorch")(test_case)
330330

331331
import torch
332332

333-
return unittest.skipUnless(torch.cuda.device_count() > 1 or torch.xpu.device_count() > 1, "test requires multiple hardware accelerators")(test_case)
333+
return unittest.skipUnless(
334+
torch.cuda.device_count() > 1 or torch.xpu.device_count() > 1, "test requires multiple hardware accelerators"
335+
)(test_case)
334336

335337

336338
def require_torch_accelerator_with_fp16(test_case):
@@ -367,6 +369,31 @@ def require_big_gpu_with_torch_cuda(test_case):
367369
)(test_case)
368370

369371

372+
def require_big_accelerator(test_case):
373+
"""
374+
Decorator marking a test that requires a bigger hardware accelerator (24GB) for execution. Some example pipelines:
375+
Flux, SD3, Cog, etc.
376+
"""
377+
if not is_torch_available():
378+
return unittest.skip("test requires PyTorch")(test_case)
379+
380+
import torch
381+
382+
if not (torch.cuda.is_available() or torch.xpu.is_available()):
383+
return unittest.skip("test requires PyTorch CUDA")(test_case)
384+
385+
if torch.xpu.is_available():
386+
device_properties = torch.xpu.get_device_properties(0)
387+
else:
388+
device_properties = torch.cuda.get_device_properties(0)
389+
390+
total_memory = device_properties.total_memory / (1024**3)
391+
return unittest.skipUnless(
392+
total_memory >= BIG_GPU_MEMORY,
393+
f"test requires a hardware accelerator with at least {BIG_GPU_MEMORY} GB memory",
394+
)(test_case)
395+
396+
370397
def require_torch_accelerator_with_training(test_case):
371398
"""Decorator marking a test that requires an accelerator with support for training."""
372399
return unittest.skipUnless(

tests/pipelines/controlnet_sd3/test_controlnet_sd3.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
from diffusers.models import SD3ControlNetModel, SD3MultiControlNetModel
3232
from diffusers.utils import load_image
3333
from diffusers.utils.testing_utils import (
34+
backend_empty_cache,
3435
enable_full_determinism,
3536
numpy_cosine_similarity_distance,
36-
require_big_gpu_with_torch_cuda,
37+
require_big_accelerator,
3738
slow,
3839
torch_device,
3940
)
@@ -219,20 +220,20 @@ def test_xformers_attention_forwardGenerator_pass(self):
219220

220221

221222
@slow
222-
@require_big_gpu_with_torch_cuda
223+
@require_big_accelerator
223224
@pytest.mark.big_gpu_with_torch_cuda
224225
class StableDiffusion3ControlNetPipelineSlowTests(unittest.TestCase):
225226
pipeline_class = StableDiffusion3ControlNetPipeline
226227

227228
def setUp(self):
228229
super().setUp()
229230
gc.collect()
230-
torch.cuda.empty_cache()
231+
backend_empty_cache(torch_device)
231232

232233
def tearDown(self):
233234
super().tearDown()
234235
gc.collect()
235-
torch.cuda.empty_cache()
236+
backend_empty_cache(torch_device)
236237

237238
def test_canny(self):
238239
controlnet = SD3ControlNetModel.from_pretrained("InstantX/SD3-Controlnet-Canny", torch_dtype=torch.float16)
@@ -272,7 +273,7 @@ def test_pose(self):
272273
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
273274
"stabilityai/stable-diffusion-3-medium-diffusers", controlnet=controlnet, torch_dtype=torch.float16
274275
)
275-
pipe.enable_model_cpu_offload()
276+
pipe.enable_model_cpu_offload(device=torch_device)
276277
pipe.set_progress_bar_config(disable=None)
277278

278279
generator = torch.Generator(device="cpu").manual_seed(0)
@@ -304,7 +305,7 @@ def test_tile(self):
304305
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
305306
"stabilityai/stable-diffusion-3-medium-diffusers", controlnet=controlnet, torch_dtype=torch.float16
306307
)
307-
pipe.enable_model_cpu_offload()
308+
pipe.enable_model_cpu_offload(device=torch_device)
308309
pipe.set_progress_bar_config(disable=None)
309310

310311
generator = torch.Generator(device="cpu").manual_seed(0)
@@ -338,7 +339,7 @@ def test_multi_controlnet(self):
338339
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
339340
"stabilityai/stable-diffusion-3-medium-diffusers", controlnet=controlnet, torch_dtype=torch.float16
340341
)
341-
pipe.enable_model_cpu_offload()
342+
pipe.enable_model_cpu_offload(device=torch_device)
342343
pipe.set_progress_bar_config(disable=None)
343344

344345
generator = torch.Generator(device="cpu").manual_seed(0)

tests/pipelines/flux/test_pipeline_flux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
backend_empty_cache,
1313
nightly,
1414
numpy_cosine_similarity_distance,
15-
require_big_gpu_with_torch_cuda,
15+
require_big_accelerator,
1616
slow,
1717
torch_device,
1818
)
@@ -204,7 +204,7 @@ def test_flux_true_cfg(self):
204204

205205

206206
@nightly
207-
@require_big_gpu_with_torch_cuda
207+
@require_big_accelerator
208208
@pytest.mark.big_gpu_with_torch_cuda
209209
class FluxPipelineSlowTests(unittest.TestCase):
210210
pipeline_class = FluxPipeline
@@ -292,7 +292,7 @@ def test_flux_inference(self):
292292

293293

294294
@slow
295-
@require_big_gpu_with_torch_cuda
295+
@require_big_accelerator
296296
@pytest.mark.big_gpu_with_torch_cuda
297297
class FluxIPAdapterPipelineSlowTests(unittest.TestCase):
298298
pipeline_class = FluxPipeline
@@ -304,12 +304,12 @@ class FluxIPAdapterPipelineSlowTests(unittest.TestCase):
304304
def setUp(self):
305305
super().setUp()
306306
gc.collect()
307-
torch.cuda.empty_cache()
307+
backend_empty_cache(torch_device)
308308

309309
def tearDown(self):
310310
super().tearDown()
311311
gc.collect()
312-
torch.cuda.empty_cache()
312+
backend_empty_cache(torch_device)
313313

314314
def get_inputs(self, device, seed=0):
315315
if str(device).startswith("mps"):

tests/pipelines/flux/test_pipeline_flux_redux.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
from diffusers import FluxPipeline, FluxPriorReduxPipeline
99
from diffusers.utils import load_image
1010
from diffusers.utils.testing_utils import (
11+
backend_empty_cache,
1112
numpy_cosine_similarity_distance,
12-
require_big_gpu_with_torch_cuda,
13+
require_big_accelerator,
1314
slow,
1415
torch_device,
1516
)
1617

1718

1819
@slow
19-
@require_big_gpu_with_torch_cuda
20+
@require_big_accelerator
2021
@pytest.mark.big_gpu_with_torch_cuda
2122
class FluxReduxSlowTests(unittest.TestCase):
2223
pipeline_class = FluxPriorReduxPipeline
@@ -27,12 +28,12 @@ class FluxReduxSlowTests(unittest.TestCase):
2728
def setUp(self):
2829
super().setUp()
2930
gc.collect()
30-
torch.cuda.empty_cache()
31+
backend_empty_cache(torch_device)
3132

3233
def tearDown(self):
3334
super().tearDown()
3435
gc.collect()
35-
torch.cuda.empty_cache()
36+
backend_empty_cache(torch_device)
3637

3738
def get_inputs(self, device, seed=0):
3839
init_image = load_image(
@@ -59,7 +60,7 @@ def test_flux_redux_inference(self):
5960
self.base_repo_id, torch_dtype=torch.bfloat16, text_encoder=None, text_encoder_2=None
6061
)
6162
pipe_redux.to(torch_device)
62-
pipe_base.enable_model_cpu_offload()
63+
pipe_base.enable_model_cpu_offload(device=torch_device)
6364

6465
inputs = self.get_inputs(torch_device)
6566
base_pipeline_inputs = self.get_base_pipeline_inputs(torch_device)

tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from diffusers.utils.testing_utils import (
1111
backend_empty_cache,
1212
numpy_cosine_similarity_distance,
13-
require_big_gpu_with_torch_cuda,
13+
require_big_accelerator,
1414
slow,
1515
torch_device,
1616
)
@@ -232,7 +232,7 @@ def test_skip_guidance_layers(self):
232232

233233

234234
@slow
235-
@require_big_gpu_with_torch_cuda
235+
@require_big_accelerator
236236
@pytest.mark.big_gpu_with_torch_cuda
237237
class StableDiffusion3PipelineSlowTests(unittest.TestCase):
238238
pipeline_class = StableDiffusion3Pipeline

tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3_img2img.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
backend_empty_cache,
1919
floats_tensor,
2020
numpy_cosine_similarity_distance,
21-
require_big_gpu_with_torch_cuda,
21+
require_big_accelerator,
2222
slow,
2323
torch_device,
2424
)
@@ -166,7 +166,7 @@ def test_multi_vae(self):
166166

167167

168168
@slow
169-
@require_big_gpu_with_torch_cuda
169+
@require_big_accelerator
170170
@pytest.mark.big_gpu_with_torch_cuda
171171
class StableDiffusion3Img2ImgPipelineSlowTests(unittest.TestCase):
172172
pipeline_class = StableDiffusion3Img2ImgPipeline
@@ -202,11 +202,10 @@ def get_inputs(self, device, seed=0):
202202
}
203203

204204
def test_sd3_img2img_inference(self):
205+
torch.manual_seed(0)
205206
pipe = self.pipeline_class.from_pretrained(self.repo_id, torch_dtype=torch.float16)
206207
pipe.enable_model_cpu_offload(device=torch_device)
207-
208208
inputs = self.get_inputs(torch_device)
209-
210209
image = pipe(**inputs).images[0]
211210
image_slice = image[0, :10, :10]
212211
expected_slice = np.array(

0 commit comments

Comments
 (0)