5959from diffusers .utils .testing_utils import (
6060 CaptureLogger ,
6161 backend_empty_cache ,
62+ backend_max_memory_allocated ,
63+ backend_reset_peak_memory_stats ,
64+ backend_synchronize ,
6265 floats_tensor ,
6366 get_python_version ,
6467 is_torch_compile ,
@@ -341,7 +344,7 @@ def test_weight_overwrite(self):
341344
342345 assert model .config .in_channels == 9
343346
344- @require_torch_gpu
347+ @require_torch_accelerator
345348 def test_keep_modules_in_fp32 (self ):
346349 r"""
347350 A simple tests to check if the modules under `_keep_in_fp32_modules` are kept in fp32 when we load the model in fp16/bf16
@@ -1480,16 +1483,16 @@ def test_layerwise_casting(storage_dtype, compute_dtype):
14801483 test_layerwise_casting (torch .float8_e5m2 , torch .float32 )
14811484 test_layerwise_casting (torch .float8_e4m3fn , torch .bfloat16 )
14821485
1483- @require_torch_gpu
1486+ @require_torch_accelerator
14841487 def test_layerwise_casting_memory (self ):
14851488 MB_TOLERANCE = 0.2
14861489 LEAST_COMPUTE_CAPABILITY = 8.0
14871490
14881491 def reset_memory_stats ():
14891492 gc .collect ()
1490- torch . cuda . synchronize ( )
1491- torch . cuda . empty_cache ( )
1492- torch . cuda . reset_peak_memory_stats ( )
1493+ backend_synchronize ( torch_device )
1494+ backend_empty_cache ( torch_device )
1495+ backend_reset_peak_memory_stats ( torch_device )
14931496
14941497 def get_memory_usage (storage_dtype , compute_dtype ):
14951498 torch .manual_seed (0 )
@@ -1502,7 +1505,7 @@ def get_memory_usage(storage_dtype, compute_dtype):
15021505 reset_memory_stats ()
15031506 model (** inputs_dict )
15041507 model_memory_footprint = model .get_memory_footprint ()
1505- peak_inference_memory_allocated_mb = torch . cuda . max_memory_allocated ( ) / 1024 ** 2
1508+ peak_inference_memory_allocated_mb = backend_max_memory_allocated ( torch_device ) / 1024 ** 2
15061509
15071510 return model_memory_footprint , peak_inference_memory_allocated_mb
15081511
@@ -1512,7 +1515,7 @@ def get_memory_usage(storage_dtype, compute_dtype):
15121515 torch .float8_e4m3fn , torch .bfloat16
15131516 )
15141517
1515- compute_capability = get_torch_cuda_device_capability ()
1518+ compute_capability = get_torch_cuda_device_capability () if torch_device == "cuda" else None
15161519 self .assertTrue (fp8_e4m3_bf16_memory_footprint < fp8_e4m3_fp32_memory_footprint < fp32_memory_footprint )
15171520 # NOTE: the following assertion would fail on our CI (running Tesla T4) due to bf16 using more memory than fp32.
15181521 # On other devices, such as DGX (Ampere) and Audace (Ada), the test passes. So, we conditionally check it.
@@ -1527,7 +1530,7 @@ def get_memory_usage(storage_dtype, compute_dtype):
15271530 )
15281531
15291532 @parameterized .expand ([False , True ])
1530- @require_torch_gpu
1533+ @require_torch_accelerator
15311534 def test_group_offloading (self , record_stream ):
15321535 init_dict , inputs_dict = self .prepare_init_args_and_inputs_for_common ()
15331536 torch .manual_seed (0 )
@@ -1714,6 +1717,37 @@ def test_push_to_hub_library_name(self):
17141717 delete_repo (self .repo_id , token = TOKEN )
17151718
17161719
1720+ class TorchCompileTesterMixin :
1721+ def setUp (self ):
1722+ # clean up the VRAM before each test
1723+ super ().setUp ()
1724+ torch ._dynamo .reset ()
1725+ gc .collect ()
1726+ backend_empty_cache (torch_device )
1727+
1728+ def tearDown (self ):
1729+ # clean up the VRAM after each test in case of CUDA runtime errors
1730+ super ().tearDown ()
1731+ torch ._dynamo .reset ()
1732+ gc .collect ()
1733+ backend_empty_cache (torch_device )
1734+
1735+ @require_torch_gpu
1736+ @require_torch_2
1737+ @is_torch_compile
1738+ @slow
1739+ def test_torch_compile_recompilation_and_graph_break (self ):
1740+ torch ._dynamo .reset ()
1741+ init_dict , inputs_dict = self .prepare_init_args_and_inputs_for_common ()
1742+
1743+ model = self .model_class (** init_dict ).to (torch_device )
1744+ model = torch .compile (model , fullgraph = True )
1745+
1746+ with torch ._dynamo .config .patch (error_on_recompile = True ), torch .no_grad ():
1747+ _ = model (** inputs_dict )
1748+ _ = model (** inputs_dict )
1749+
1750+
17171751@slow
17181752@require_torch_2
17191753@require_torch_accelerator
0 commit comments