Skip to content

Commit 9a34953

Browse files
authored
Additional Memory clean up for slow tests (huggingface#7436)
* update * update * update
1 parent e29f16c commit 9a34953

22 files changed

+188
-29
lines changed

tests/pipelines/deepfloyd_if/test_if.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def test_xformers_attention_forwardGenerator_pass(self):
8585
@slow
8686
@require_torch_gpu
8787
class IFPipelineSlowTests(unittest.TestCase):
88+
def setUp(self):
89+
# clean up the VRAM before each test
90+
super().setUp()
91+
gc.collect()
92+
torch.cuda.empty_cache()
93+
8894
def tearDown(self):
8995
# clean up the VRAM after each test
9096
super().tearDown()

tests/pipelines/deepfloyd_if/test_if_img2img.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def test_inference_batch_single_identical(self):
9494
@slow
9595
@require_torch_gpu
9696
class IFImg2ImgPipelineSlowTests(unittest.TestCase):
97+
def setUp(self):
98+
# clean up the VRAM before each test
99+
super().setUp()
100+
gc.collect()
101+
torch.cuda.empty_cache()
102+
97103
def tearDown(self):
98104
# clean up the VRAM after each test
99105
super().tearDown()
@@ -109,6 +115,10 @@ def test_if_img2img(self):
109115
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
110116
pipe.enable_model_cpu_offload()
111117

118+
torch.cuda.reset_max_memory_allocated()
119+
torch.cuda.empty_cache()
120+
torch.cuda.reset_peak_memory_stats()
121+
112122
image = floats_tensor((1, 3, 64, 64), rng=random.Random(0)).to(torch_device)
113123
generator = torch.Generator(device="cpu").manual_seed(0)
114124
output = pipe(

tests/pipelines/deepfloyd_if/test_if_img2img_superresolution.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def test_inference_batch_single_identical(self):
9292
@slow
9393
@require_torch_gpu
9494
class IFImg2ImgSuperResolutionPipelineSlowTests(unittest.TestCase):
95+
def setUp(self):
96+
# clean up the VRAM before each test
97+
super().setUp()
98+
gc.collect()
99+
torch.cuda.empty_cache()
100+
95101
def tearDown(self):
96102
# clean up the VRAM after each test
97103
super().tearDown()
@@ -107,6 +113,10 @@ def test_if_img2img_superresolution(self):
107113
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
108114
pipe.enable_model_cpu_offload()
109115

116+
torch.cuda.reset_max_memory_allocated()
117+
torch.cuda.empty_cache()
118+
torch.cuda.reset_peak_memory_stats()
119+
110120
generator = torch.Generator(device="cpu").manual_seed(0)
111121

112122
original_image = floats_tensor((1, 3, 256, 256), rng=random.Random(0)).to(torch_device)

tests/pipelines/deepfloyd_if/test_if_inpainting.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def test_inference_batch_single_identical(self):
9292
@slow
9393
@require_torch_gpu
9494
class IFInpaintingPipelineSlowTests(unittest.TestCase):
95+
def setUp(self):
96+
# clean up the VRAM before each test
97+
super().setUp()
98+
gc.collect()
99+
torch.cuda.empty_cache()
100+
95101
def tearDown(self):
96102
# clean up the VRAM after each test
97103
super().tearDown()
@@ -105,7 +111,6 @@ def test_if_inpainting(self):
105111
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
106112
pipe.enable_model_cpu_offload()
107113

108-
# Super resolution test
109114
torch.cuda.empty_cache()
110115
torch.cuda.reset_max_memory_allocated()
111116
torch.cuda.reset_peak_memory_stats()

tests/pipelines/deepfloyd_if/test_if_inpainting_superresolution.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def test_inference_batch_single_identical(self):
9494
@slow
9595
@require_torch_gpu
9696
class IFInpaintingSuperResolutionPipelineSlowTests(unittest.TestCase):
97+
def setUp(self):
98+
# clean up the VRAM before each test
99+
super().setUp()
100+
gc.collect()
101+
torch.cuda.empty_cache()
102+
97103
def tearDown(self):
98104
# clean up the VRAM after each test
99105
super().tearDown()

tests/pipelines/deepfloyd_if/test_if_superresolution.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_inference_batch_single_identical(self):
8787
@slow
8888
@require_torch_gpu
8989
class IFSuperResolutionPipelineSlowTests(unittest.TestCase):
90+
def setUp(self):
91+
# clean up the VRAM before each test
92+
super().setUp()
93+
gc.collect()
94+
torch.cuda.empty_cache()
95+
9096
def tearDown(self):
9197
# clean up the VRAM after each test
9298
super().tearDown()

tests/pipelines/ip_adapters/test_ip_adapter_stable_diffusion.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@
5050
class IPAdapterNightlyTestsMixin(unittest.TestCase):
5151
dtype = torch.float16
5252

53+
def setUp(self):
54+
# clean up the VRAM before each test
55+
super().setUp()
56+
gc.collect()
57+
torch.cuda.empty_cache()
58+
5359
def tearDown(self):
60+
# clean up the VRAM after each test
5461
super().tearDown()
5562
gc.collect()
5663
torch.cuda.empty_cache()
@@ -313,7 +320,7 @@ def test_text_to_image_sdxl(self):
313320
feature_extractor=feature_extractor,
314321
torch_dtype=self.dtype,
315322
)
316-
pipeline.to(torch_device)
323+
pipeline.enable_model_cpu_offload()
317324
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
318325

319326
inputs = self.get_dummy_inputs()
@@ -373,7 +380,7 @@ def test_image_to_image_sdxl(self):
373380
feature_extractor=feature_extractor,
374381
torch_dtype=self.dtype,
375382
)
376-
pipeline.to(torch_device)
383+
pipeline.enable_model_cpu_offload()
377384
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
378385

379386
inputs = self.get_dummy_inputs(for_image_to_image=True)
@@ -442,7 +449,7 @@ def test_inpainting_sdxl(self):
442449
feature_extractor=feature_extractor,
443450
torch_dtype=self.dtype,
444451
)
445-
pipeline.to(torch_device)
452+
pipeline.enable_model_cpu_offload()
446453
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
447454

448455
inputs = self.get_dummy_inputs(for_inpainting=True)
@@ -490,7 +497,7 @@ def test_ip_adapter_single_mask(self):
490497
image_encoder=image_encoder,
491498
torch_dtype=self.dtype,
492499
)
493-
pipeline.to(torch_device)
500+
pipeline.enable_model_cpu_offload()
494501
pipeline.load_ip_adapter(
495502
"h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter-plus-face_sdxl_vit-h.safetensors"
496503
)
@@ -518,7 +525,7 @@ def test_ip_adapter_multiple_masks(self):
518525
image_encoder=image_encoder,
519526
torch_dtype=self.dtype,
520527
)
521-
pipeline.to(torch_device)
528+
pipeline.enable_model_cpu_offload()
522529
pipeline.load_ip_adapter(
523530
"h94/IP-Adapter", subfolder="sdxl_models", weight_name=["ip-adapter-plus-face_sdxl_vit-h.safetensors"] * 2
524531
)

tests/pipelines/kandinsky/test_kandinsky.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ def test_offloads(self):
275275
@slow
276276
@require_torch_gpu
277277
class KandinskyPipelineIntegrationTests(unittest.TestCase):
278+
def setUp(self):
279+
# clean up the VRAM before each test
280+
super().setUp()
281+
gc.collect()
282+
torch.cuda.empty_cache()
283+
278284
def tearDown(self):
279285
# clean up the VRAM after each test
280286
super().tearDown()

tests/pipelines/kandinsky/test_kandinsky_img2img.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ def test_dict_tuple_outputs_equivalent(self):
299299
@slow
300300
@require_torch_gpu
301301
class KandinskyImg2ImgPipelineIntegrationTests(unittest.TestCase):
302+
def setUp(self):
303+
# clean up the VRAM before each test
304+
super().setUp()
305+
gc.collect()
306+
torch.cuda.empty_cache()
307+
302308
def tearDown(self):
303309
# clean up the VRAM after each test
304310
super().tearDown()

tests/pipelines/kandinsky/test_kandinsky_inpaint.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ def test_float16_inference(self):
297297
@nightly
298298
@require_torch_gpu
299299
class KandinskyInpaintPipelineIntegrationTests(unittest.TestCase):
300+
def setUp(self):
301+
# clean up the VRAM before each test
302+
super().setUp()
303+
gc.collect()
304+
torch.cuda.empty_cache()
305+
300306
def tearDown(self):
301307
# clean up the VRAM after each test
302308
super().tearDown()

0 commit comments

Comments
 (0)