Skip to content

Commit ec1aded

Browse files
authored
Optimize test files by fixing CPU-offloading usage (huggingface#8409)
* Refactor code to remove unnecessary calls to `to(torch_device)` * Refactor code to remove unnecessary calls to `to("cuda")` * Update pipeline_stable_diffusion_diffedit.py
1 parent 151a56b commit ec1aded

16 files changed

+1
-21
lines changed

docs/source/en/api/pipelines/animatediff.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ from PIL import Image
165165
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float16)
166166
# load SD 1.5 based finetuned model
167167
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
168-
pipe = AnimateDiffVideoToVideoPipeline.from_pretrained(model_id, motion_adapter=adapter, torch_dtype=torch.float16).to("cuda")
168+
pipe = AnimateDiffVideoToVideoPipeline.from_pretrained(model_id, motion_adapter=adapter, torch_dtype=torch.float16)
169169
scheduler = DDIMScheduler.from_pretrained(
170170
model_id,
171171
subfolder="scheduler",

tests/pipelines/stable_diffusion/test_stable_diffusion.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
11351135
torch.cuda.reset_peak_memory_stats()
11361136

11371137
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
1138-
pipe = pipe.to(torch_device)
11391138
pipe.set_progress_bar_config(disable=None)
11401139
pipe.enable_attention_slicing(1)
11411140
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion/test_stable_diffusion_img2img.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
513513
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
514514
"CompVis/stable-diffusion-v1-4", safety_checker=None, torch_dtype=torch.float16
515515
)
516-
pipe = pipe.to(torch_device)
517516
pipe.set_progress_bar_config(disable=None)
518517
pipe.enable_attention_slicing(1)
519518
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion/test_stable_diffusion_inpaint.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,6 @@ def test_stable_diffusion_inpaint_with_sequential_cpu_offloading(self):
705705
pipe = StableDiffusionInpaintPipeline.from_pretrained(
706706
"runwayml/stable-diffusion-inpainting", safety_checker=None, torch_dtype=torch.float16
707707
)
708-
pipe = pipe.to(torch_device)
709708
pipe.set_progress_bar_config(disable=None)
710709
pipe.enable_attention_slicing(1)
711710
pipe.enable_sequential_cpu_offload()
@@ -911,7 +910,6 @@ def test_stable_diffusion_inpaint_with_sequential_cpu_offloading(self):
911910
"runwayml/stable-diffusion-inpainting", safety_checker=None, torch_dtype=torch.float16
912911
)
913912
pipe.vae = vae
914-
pipe = pipe.to(torch_device)
915913
pipe.set_progress_bar_config(disable=None)
916914
pipe.enable_attention_slicing(1)
917915
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion/test_stable_diffusion_instruction_pix2pix.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
394394
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
395395
"timbrooks/instruct-pix2pix", safety_checker=None, torch_dtype=torch.float16
396396
)
397-
pipe = pipe.to(torch_device)
398397
pipe.set_progress_bar_config(disable=None)
399398
pipe.enable_attention_slicing(1)
400399
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion_2/test_stable_diffusion.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
462462
pipe = StableDiffusionPipeline.from_pretrained(
463463
"stabilityai/stable-diffusion-2-base", torch_dtype=torch.float16
464464
)
465-
pipe = pipe.to(torch_device)
466465
pipe.set_progress_bar_config(disable=None)
467466
pipe.enable_attention_slicing(1)
468467
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
506506
pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
507507
"stabilityai/stable-diffusion-2-depth", safety_checker=None, torch_dtype=torch.float16
508508
)
509-
pipe = pipe.to(torch_device)
510509
pipe.set_progress_bar_config(disable=None)
511510
pipe.enable_attention_slicing(1)
512511
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion_2/test_stable_diffusion_inpaint.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
261261
scheduler=pndm,
262262
torch_dtype=torch.float16,
263263
)
264-
pipe.to(torch_device)
265264
pipe.set_progress_bar_config(disable=None)
266265
pipe.enable_attention_slicing(1)
267266
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading(self):
472472
model_id,
473473
torch_dtype=torch.float16,
474474
)
475-
pipe.to(torch_device)
476475
pipe.set_progress_bar_config(disable=None)
477476
pipe.enable_attention_slicing(1)
478477
pipe.enable_sequential_cpu_offload()

tests/pipelines/stable_diffusion_2/test_stable_diffusion_v_pred.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ def test_stable_diffusion_pipeline_with_sequential_cpu_offloading_v_pred(self):
537537
prompt = "Andromeda galaxy in a bottle"
538538

539539
pipeline = StableDiffusionPipeline.from_pretrained(pipeline_id, torch_dtype=torch.float16)
540-
pipeline = pipeline.to(torch_device)
541540
pipeline.enable_attention_slicing(1)
542541
pipeline.enable_sequential_cpu_offload()
543542

0 commit comments

Comments
 (0)