Skip to content

Commit 6ed4523

Browse files
committed
add changes
1 parent f08a849 commit 6ed4523

35 files changed

+301
-295
lines changed

src/diffusers/utils/testing_utils.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,10 +1088,6 @@ def _is_torch_fp64_available(device):
10881088
"xpu": torch.xpu.reset_peak_memory_stats,
10891089
"default": None,
10901090
}
1091-
BACKEND_RESET_MAX_MEMORY_ALLOCATED = {
1092-
"cuda": torch.cuda.reset_max_memory_allocated,
1093-
"default": None,
1094-
}
10951091
BACKEND_MAX_MEMORY_ALLOCATED = {
10961092
"cuda": torch.cuda.max_memory_allocated,
10971093
"xpu": torch.xpu.max_memory_allocated,
@@ -1101,6 +1097,7 @@ def _is_torch_fp64_available(device):
11011097

11021098
# This dispatches a defined function according to the accelerator from the function definitions.
11031099
def _device_agnostic_dispatch(device: str, dispatch_table: Dict[str, Callable], *args, **kwargs):
1100+
11041101
if device not in dispatch_table:
11051102
return dispatch_table["default"](*args, **kwargs)
11061103

@@ -1131,10 +1128,6 @@ def backend_reset_peak_memory_stats(device: str):
11311128
return _device_agnostic_dispatch(device, BACKEND_RESET_PEAK_MEMORY_STATS)
11321129

11331130

1134-
def backend_reset_max_memory_allocated(device: str):
1135-
return _device_agnostic_dispatch(device, BACKEND_RESET_MAX_MEMORY_ALLOCATED)
1136-
1137-
11381131
def backend_max_memory_allocated(device: str):
11391132
return _device_agnostic_dispatch(device, BACKEND_MAX_MEMORY_ALLOCATED)
11401133

@@ -1196,7 +1189,6 @@ def update_mapping_from_spec(device_fn_dict: Dict[str, Callable], attribute_name
11961189
update_mapping_from_spec(BACKEND_DEVICE_COUNT, "DEVICE_COUNT_FN")
11971190
update_mapping_from_spec(BACKEND_SUPPORTS_TRAINING, "SUPPORTS_TRAINING")
11981191
update_mapping_from_spec(BACKEND_RESET_PEAK_MEMORY_STATS, "RESET_PEAK_MEMORY_STATS_FN")
1199-
update_mapping_from_spec(BACKEND_RESET_MAX_MEMORY_ALLOCATED, "RESET_MAX_MEMORY_ALLOCATED_FN")
12001192
update_mapping_from_spec(BACKEND_MAX_MEMORY_ALLOCATED, "MAX_MEMORY_ALLOCATED_FN")
12011193

12021194

tests/lora/test_lora_layers_sd.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
nightly,
3838
numpy_cosine_similarity_distance,
3939
require_peft_backend,
40-
require_torch_gpu,
40+
require_torch_accelerator,
4141
slow,
4242
torch_device,
4343
)
@@ -101,7 +101,7 @@ def tearDown(self):
101101
# Keeping this test here makes sense because it doesn't look any integration
102102
# (value assertions on logits).
103103
@slow
104-
@require_torch_gpu
104+
@require_torch_accelerator
105105
def test_integration_move_lora_cpu(self):
106106
path = "stable-diffusion-v1-5/stable-diffusion-v1-5"
107107
lora_id = "takuma104/lora-test-text-encoder-lora-target"
@@ -158,7 +158,7 @@ def test_integration_move_lora_cpu(self):
158158
self.assertTrue(m.weight.device != torch.device("cpu"))
159159

160160
@slow
161-
@require_torch_gpu
161+
@require_torch_accelerator
162162
def test_integration_move_lora_dora_cpu(self):
163163
from peft import LoraConfig
164164

@@ -209,18 +209,18 @@ def test_integration_move_lora_dora_cpu(self):
209209

210210
@slow
211211
@nightly
212-
@require_torch_gpu
212+
@require_torch_accelerator
213213
@require_peft_backend
214214
class LoraIntegrationTests(unittest.TestCase):
215215
def setUp(self):
216216
super().setUp()
217217
gc.collect()
218-
torch.cuda.empty_cache()
218+
backend_empty_cache(torch_device)
219219

220220
def tearDown(self):
221221
super().tearDown()
222222
gc.collect()
223-
torch.cuda.empty_cache()
223+
backend_empty_cache(torch_device)
224224

225225
def test_integration_logits_with_scale(self):
226226
path = "stable-diffusion-v1-5/stable-diffusion-v1-5"
@@ -378,7 +378,7 @@ def test_a1111_with_model_cpu_offload(self):
378378
generator = torch.Generator().manual_seed(0)
379379

380380
pipe = StableDiffusionPipeline.from_pretrained("hf-internal-testing/Counterfeit-V2.5", safety_checker=None)
381-
pipe.enable_model_cpu_offload()
381+
pipe.enable_model_cpu_offload(device=torch_device)
382382
lora_model_id = "hf-internal-testing/civitai-light-shadow-lora"
383383
lora_filename = "light_and_shadow.safetensors"
384384
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
@@ -400,7 +400,7 @@ def test_a1111_with_sequential_cpu_offload(self):
400400
generator = torch.Generator().manual_seed(0)
401401

402402
pipe = StableDiffusionPipeline.from_pretrained("hf-internal-testing/Counterfeit-V2.5", safety_checker=None)
403-
pipe.enable_sequential_cpu_offload()
403+
pipe.enable_sequential_cpu_offload(device=torch_device)
404404
lora_model_id = "hf-internal-testing/civitai-light-shadow-lora"
405405
lora_filename = "light_and_shadow.safetensors"
406406
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
@@ -656,7 +656,7 @@ def test_sd_load_civitai_empty_network_alpha(self):
656656
See: https://github.com/huggingface/diffusers/issues/5606
657657
"""
658658
pipeline = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")
659-
pipeline.enable_sequential_cpu_offload()
659+
pipeline.enable_sequential_cpu_offload(device=torch_device)
660660
civitai_path = hf_hub_download("ybelkada/test-ahi-civitai", "ahi_lora_weights.safetensors")
661661
pipeline.load_lora_weights(civitai_path, adapter_name="ahri")
662662

tests/lora/test_lora_layers_sd3.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
nightly,
3333
numpy_cosine_similarity_distance,
3434
require_peft_backend,
35-
require_torch_gpu,
35+
require_torch_accelerator,
3636
slow,
3737
torch_device,
3838
)
@@ -91,7 +91,7 @@ class SD3LoRATests(unittest.TestCase, PeftLoraLoaderMixinTests):
9191
def output_shape(self):
9292
return (1, 32, 32, 3)
9393

94-
@require_torch_gpu
94+
@require_torch_accelerator
9595
def test_sd3_lora(self):
9696
"""
9797
Test loading the loras that are saved with the diffusers and peft formats.
@@ -130,7 +130,7 @@ def test_modify_padding_mode(self):
130130

131131
@slow
132132
@nightly
133-
@require_torch_gpu
133+
@require_torch_accelerator
134134
@require_peft_backend
135135
class LoraSD3IntegrationTests(unittest.TestCase):
136136
pipeline_class = StableDiffusion3Img2ImgPipeline
@@ -139,12 +139,12 @@ class LoraSD3IntegrationTests(unittest.TestCase):
139139
def setUp(self):
140140
super().setUp()
141141
gc.collect()
142-
torch.cuda.empty_cache()
142+
backend_empty_cache(torch_device)
143143

144144
def tearDown(self):
145145
super().tearDown()
146146
gc.collect()
147-
torch.cuda.empty_cache()
147+
backend_empty_cache(torch_device)
148148

149149
def get_inputs(self, device, seed=0):
150150
init_image = load_image(

tests/pipelines/deepfloyd_if/test_if.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from diffusers.utils.import_utils import is_xformers_available
2626
from diffusers.utils.testing_utils import (
2727
backend_empty_cache,
28-
backend_reset_max_memory_allocated,
2928
backend_reset_peak_memory_stats,
3029
load_numpy,
3130
require_accelerator,
@@ -113,7 +112,6 @@ def test_if_text_to_image(self):
113112
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
114113
pipe.enable_model_cpu_offload(device=torch_device)
115114

116-
backend_reset_max_memory_allocated(torch_device)
117115
backend_empty_cache(torch_device)
118116
backend_reset_peak_memory_stats(torch_device)
119117

tests/pipelines/deepfloyd_if/test_if_img2img.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from diffusers.utils.import_utils import is_xformers_available
2525
from diffusers.utils.testing_utils import (
2626
backend_empty_cache,
27-
backend_reset_max_memory_allocated,
2827
backend_reset_peak_memory_stats,
2928
floats_tensor,
3029
load_numpy,
@@ -128,7 +127,6 @@ def test_if_img2img(self):
128127
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
129128
pipe.enable_model_cpu_offload(device=torch_device)
130129

131-
backend_reset_max_memory_allocated(torch_device)
132130
backend_empty_cache(torch_device)
133131
backend_reset_peak_memory_stats(torch_device)
134132

tests/pipelines/deepfloyd_if/test_if_img2img_superresolution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from diffusers.utils.testing_utils import (
2626
backend_empty_cache,
2727
backend_max_memory_allocated,
28-
backend_reset_max_memory_allocated,
2928
backend_reset_peak_memory_stats,
3029
floats_tensor,
3130
load_numpy,

tests/pipelines/deepfloyd_if/test_if_inpainting.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from diffusers.utils.testing_utils import (
2626
backend_empty_cache,
2727
backend_max_memory_allocated,
28-
backend_reset_max_memory_allocated,
2928
backend_reset_peak_memory_stats,
3029
floats_tensor,
3130
load_numpy,
@@ -124,7 +123,6 @@ def test_if_inpainting(self):
124123
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
125124
pipe.enable_model_cpu_offload(device=torch_device)
126125

127-
backend_reset_max_memory_allocated(torch_device)
128126
backend_empty_cache(torch_device)
129127
backend_reset_peak_memory_stats(torch_device)
130128

tests/pipelines/deepfloyd_if/test_if_inpainting_superresolution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from diffusers.utils.testing_utils import (
2626
backend_empty_cache,
2727
backend_max_memory_allocated,
28-
backend_reset_max_memory_allocated,
2928
backend_reset_peak_memory_stats,
3029
floats_tensor,
3130
load_numpy,

tests/pipelines/deepfloyd_if/test_if_superresolution.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from diffusers.utils.testing_utils import (
2626
backend_empty_cache,
2727
backend_max_memory_allocated,
28-
backend_reset_max_memory_allocated,
2928
backend_reset_peak_memory_stats,
3029
floats_tensor,
3130
load_numpy,
@@ -120,7 +119,6 @@ def test_if_superresolution(self):
120119
pipe.enable_model_cpu_offload(device=torch_device)
121120

122121
# Super resolution test
123-
backend_reset_max_memory_allocated(torch_device)
124122
backend_empty_cache(torch_device)
125123
backend_reset_peak_memory_stats(torch_device)
126124

tests/pipelines/marigold/test_marigold_depth.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
UNet2DConditionModel,
3333
)
3434
from diffusers.utils.testing_utils import (
35+
backend_empty_cache,
3536
enable_full_determinism,
3637
floats_tensor,
3738
is_flaky,
3839
load_image,
39-
require_torch_gpu,
40+
require_torch_accelerator,
4041
slow,
42+
torch_device,
4143
)
4244

4345
from ..test_pipelines_common import PipelineTesterMixin
@@ -288,17 +290,17 @@ def test_marigold_depth_dummy_no_processing_resolution(self):
288290

289291

290292
@slow
291-
@require_torch_gpu
293+
@require_torch_accelerator
292294
class MarigoldDepthPipelineIntegrationTests(unittest.TestCase):
293295
def setUp(self):
294296
super().setUp()
295297
gc.collect()
296-
torch.cuda.empty_cache()
298+
backend_empty_cache(torch_device)
297299

298300
def tearDown(self):
299301
super().tearDown()
300302
gc.collect()
301-
torch.cuda.empty_cache()
303+
backend_empty_cache(torch_device)
302304

303305
def _test_marigold_depth(
304306
self,
@@ -317,8 +319,7 @@ def _test_marigold_depth(
317319
from_pretrained_kwargs["torch_dtype"] = torch.float16
318320

319321
pipe = MarigoldDepthPipeline.from_pretrained(model_id, **from_pretrained_kwargs)
320-
if device == "cuda":
321-
pipe.enable_model_cpu_offload()
322+
pipe.enable_model_cpu_offload(device=torch_device)
322323
pipe.set_progress_bar_config(disable=None)
323324

324325
generator = torch.Generator(device=device).manual_seed(generator_seed)
@@ -358,7 +359,7 @@ def test_marigold_depth_einstein_f32_cpu_G0_S1_P32_E1_B1_M1(self):
358359
def test_marigold_depth_einstein_f32_cuda_G0_S1_P768_E1_B1_M1(self):
359360
self._test_marigold_depth(
360361
is_fp16=False,
361-
device="cuda",
362+
device=torch_device,
362363
generator_seed=0,
363364
expected_slice=np.array([0.1244, 0.1265, 0.1292, 0.1240, 0.1252, 0.1266, 0.1246, 0.1226, 0.1180]),
364365
num_inference_steps=1,
@@ -371,7 +372,7 @@ def test_marigold_depth_einstein_f32_cuda_G0_S1_P768_E1_B1_M1(self):
371372
def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E1_B1_M1(self):
372373
self._test_marigold_depth(
373374
is_fp16=True,
374-
device="cuda",
375+
device=torch_device,
375376
generator_seed=0,
376377
expected_slice=np.array([0.1241, 0.1262, 0.1290, 0.1238, 0.1250, 0.1265, 0.1244, 0.1225, 0.1179]),
377378
num_inference_steps=1,
@@ -384,7 +385,7 @@ def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E1_B1_M1(self):
384385
def test_marigold_depth_einstein_f16_cuda_G2024_S1_P768_E1_B1_M1(self):
385386
self._test_marigold_depth(
386387
is_fp16=True,
387-
device="cuda",
388+
device=torch_device,
388389
generator_seed=2024,
389390
expected_slice=np.array([0.1710, 0.1725, 0.1738, 0.1700, 0.1700, 0.1696, 0.1698, 0.1663, 0.1592]),
390391
num_inference_steps=1,
@@ -397,7 +398,7 @@ def test_marigold_depth_einstein_f16_cuda_G2024_S1_P768_E1_B1_M1(self):
397398
def test_marigold_depth_einstein_f16_cuda_G0_S2_P768_E1_B1_M1(self):
398399
self._test_marigold_depth(
399400
is_fp16=True,
400-
device="cuda",
401+
device=torch_device,
401402
generator_seed=0,
402403
expected_slice=np.array([0.1085, 0.1098, 0.1110, 0.1081, 0.1085, 0.1082, 0.1085, 0.1057, 0.0996]),
403404
num_inference_steps=2,
@@ -410,7 +411,7 @@ def test_marigold_depth_einstein_f16_cuda_G0_S2_P768_E1_B1_M1(self):
410411
def test_marigold_depth_einstein_f16_cuda_G0_S1_P512_E1_B1_M1(self):
411412
self._test_marigold_depth(
412413
is_fp16=True,
413-
device="cuda",
414+
device=torch_device,
414415
generator_seed=0,
415416
expected_slice=np.array([0.2683, 0.2693, 0.2698, 0.2666, 0.2632, 0.2615, 0.2656, 0.2603, 0.2573]),
416417
num_inference_steps=1,
@@ -423,7 +424,7 @@ def test_marigold_depth_einstein_f16_cuda_G0_S1_P512_E1_B1_M1(self):
423424
def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E3_B1_M1(self):
424425
self._test_marigold_depth(
425426
is_fp16=True,
426-
device="cuda",
427+
device=torch_device,
427428
generator_seed=0,
428429
expected_slice=np.array([0.1200, 0.1215, 0.1237, 0.1193, 0.1197, 0.1202, 0.1196, 0.1166, 0.1109]),
429430
num_inference_steps=1,
@@ -437,7 +438,7 @@ def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E3_B1_M1(self):
437438
def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E4_B2_M1(self):
438439
self._test_marigold_depth(
439440
is_fp16=True,
440-
device="cuda",
441+
device=torch_device,
441442
generator_seed=0,
442443
expected_slice=np.array([0.1121, 0.1135, 0.1155, 0.1111, 0.1115, 0.1118, 0.1111, 0.1079, 0.1019]),
443444
num_inference_steps=1,
@@ -451,7 +452,7 @@ def test_marigold_depth_einstein_f16_cuda_G0_S1_P768_E4_B2_M1(self):
451452
def test_marigold_depth_einstein_f16_cuda_G0_S1_P512_E1_B1_M0(self):
452453
self._test_marigold_depth(
453454
is_fp16=True,
454-
device="cuda",
455+
device=torch_device,
455456
generator_seed=0,
456457
expected_slice=np.array([0.2671, 0.2690, 0.2720, 0.2659, 0.2676, 0.2739, 0.2664, 0.2686, 0.2573]),
457458
num_inference_steps=1,

0 commit comments

Comments
 (0)