From fbdba4a2eab35ebc430a7dd34cb7d496e14c53a3 Mon Sep 17 00:00:00 2001 From: hlky Date: Wed, 9 Apr 2025 08:29:21 +0100 Subject: [PATCH 1/4] Use `ignore_cleanup_errors=True` in `tempfile.TemporaryDirectory` for Windows compatability --- .../test_dreambooth_lora_flux_advanced.py | 14 +-- examples/community/sde_drag.py | 2 +- .../consistency_distillation/test_lcm_lora.py | 4 +- examples/controlnet/test_controlnet.py | 12 +- .../custom_diffusion/test_custom_diffusion.py | 6 +- examples/dreambooth/test_dreambooth.py | 10 +- examples/dreambooth/test_dreambooth_flux.py | 8 +- examples/dreambooth/test_dreambooth_lora.py | 22 ++-- .../dreambooth/test_dreambooth_lora_edm.py | 4 +- .../dreambooth/test_dreambooth_lora_flux.py | 12 +- .../test_dreambooth_lora_lumina2.py | 10 +- .../dreambooth/test_dreambooth_lora_sana.py | 10 +- .../dreambooth/test_dreambooth_lora_sd3.py | 14 +-- examples/dreambooth/test_dreambooth_sd3.py | 8 +- .../instruct_pix2pix/test_instruct_pix2pix.py | 4 +- examples/t2i_adapter/test_t2i_adapter.py | 2 +- examples/text_to_image/test_text_to_image.py | 12 +- .../text_to_image/test_text_to_image_lora.py | 12 +- .../test_textual_inversion.py | 6 +- .../test_textual_inversion_sdxl.py | 6 +- .../test_unconditional.py | 6 +- examples/vqgan/test_vqgan.py | 10 +- .../pipelines/transformers_loading_utils.py | 4 +- src/diffusers/utils/hub_utils.py | 2 +- tests/lora/test_deprecated_utilities.py | 2 +- tests/lora/test_lora_layers_cogview4.py | 2 +- tests/lora/test_lora_layers_flux.py | 6 +- tests/lora/utils.py | 16 +-- tests/models/test_attention_processor.py | 2 +- tests/models/test_modeling_common.py | 46 ++++---- .../unets/test_models_unet_2d_condition.py | 6 +- tests/models/unets/test_models_unet_motion.py | 6 +- tests/others/test_config.py | 4 +- tests/others/test_ema.py | 8 +- tests/pipelines/allegro/test_allegro.py | 2 +- tests/pipelines/controlnet/test_controlnet.py | 4 +- .../controlnet/test_controlnet_img2img.py | 2 +- .../controlnet/test_controlnet_inpaint.py | 2 +- tests/pipelines/deepfloyd_if/__init__.py | 4 +- .../pipelines/hunyuandit/test_hunyuan_dit.py | 2 +- tests/pipelines/latte/test_latte.py | 2 +- tests/pipelines/pag/test_pag_hunyuan_dit.py | 2 +- tests/pipelines/pag/test_pag_pixart_sigma.py | 2 +- tests/pipelines/pixart_alpha/test_pixart.py | 2 +- tests/pipelines/pixart_sigma/test_pixart.py | 2 +- .../test_semantic_diffusion.py | 2 +- .../test_onnx_stable_diffusion.py | 2 +- .../stable_diffusion/test_stable_diffusion.py | 2 +- .../test_stable_diffusion_depth.py | 4 +- .../test_stable_diffusion_diffedit.py | 2 +- .../test_stable_diffusion_upscale.py | 2 +- .../test_safe_diffusion.py | 2 +- .../test_stable_diffusion_xl.py | 2 +- .../test_stable_video_diffusion.py | 6 +- tests/pipelines/test_pipelines.py | 110 +++++++++--------- tests/pipelines/test_pipelines_common.py | 20 ++-- tests/pipelines/test_pipelines_flax.py | 2 +- .../test_text_to_video_zero_sdxl.py | 2 +- tests/quantization/bnb/test_4bit.py | 4 +- tests/quantization/bnb/test_mixed_int8.py | 4 +- tests/quantization/quanto/test_quanto.py | 2 +- tests/quantization/torchao/test_torchao.py | 8 +- tests/schedulers/test_scheduler_deis.py | 4 +- tests/schedulers/test_scheduler_dpm_multi.py | 4 +- .../test_scheduler_dpm_multi_inverse.py | 4 +- tests/schedulers/test_scheduler_dpm_single.py | 4 +- .../test_scheduler_edm_dpmsolver_multistep.py | 4 +- tests/schedulers/test_scheduler_edm_euler.py | 2 +- tests/schedulers/test_scheduler_flax.py | 16 +-- tests/schedulers/test_scheduler_ipndm.py | 4 +- tests/schedulers/test_scheduler_lcm.py | 2 +- tests/schedulers/test_scheduler_pndm.py | 4 +- .../schedulers/test_scheduler_score_sde_ve.py | 4 +- tests/schedulers/test_scheduler_unipc.py | 4 +- tests/schedulers/test_schedulers.py | 20 ++-- .../single_file/single_file_testing_utils.py | 12 +- ...iffusion_controlnet_img2img_single_file.py | 6 +- ...iffusion_controlnet_inpaint_single_file.py | 6 +- ...stable_diffusion_controlnet_single_file.py | 6 +- .../test_stable_diffusion_single_file.py | 2 +- ...stable_diffusion_xl_adapter_single_file.py | 6 +- ...ble_diffusion_xl_controlnet_single_file.py | 6 +- utils/update_metadata.py | 2 +- 83 files changed, 312 insertions(+), 310 deletions(-) diff --git a/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py b/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py index e29c99821303..d6116c297b15 100644 --- a/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py +++ b/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py @@ -39,7 +39,7 @@ class DreamBoothLoRAFluxAdvanced(ExamplesTestsAccelerate): script_path = "examples/advanced_diffusion_training/train_dreambooth_lora_flux_advanced.py" def test_dreambooth_lora_flux(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -71,7 +71,7 @@ def test_dreambooth_lora_flux(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_flux(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -104,7 +104,7 @@ def test_dreambooth_lora_text_encoder_flux(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_pivotal_tuning_flux_clip(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -146,7 +146,7 @@ def test_dreambooth_lora_pivotal_tuning_flux_clip(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_pivotal_tuning_flux_clip_t5(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -189,7 +189,7 @@ def test_dreambooth_lora_pivotal_tuning_flux_clip_t5(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -222,7 +222,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -245,7 +245,7 @@ def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/community/sde_drag.py b/examples/community/sde_drag.py index 902eaa99f417..4ceee1e13a89 100644 --- a/examples/community/sde_drag.py +++ b/examples/community/sde_drag.py @@ -320,7 +320,7 @@ def train_lora(self, prompt, image, lora_step=100, lora_rank=16, generator=None) lr_scheduler.step() optimizer.zero_grad() - with tempfile.TemporaryDirectory() as save_lora_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as save_lora_dir: StableDiffusionLoraLoaderMixin.save_lora_weights( save_directory=save_lora_dir, unet_lora_layers=unet_lora_layers, diff --git a/examples/consistency_distillation/test_lcm_lora.py b/examples/consistency_distillation/test_lcm_lora.py index 5ca66909a206..17028842027c 100644 --- a/examples/consistency_distillation/test_lcm_lora.py +++ b/examples/consistency_distillation/test_lcm_lora.py @@ -34,7 +34,7 @@ class TextToImageLCM(ExamplesTestsAccelerate): def test_text_to_image_lcm_lora_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/consistency_distillation/train_lcm_distill_lora_sdxl.py --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -61,7 +61,7 @@ def test_text_to_image_lcm_lora_sdxl(self): self.assertTrue(is_lora) def test_text_to_image_lcm_lora_sdxl_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/consistency_distillation/train_lcm_distill_lora_sdxl.py --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/controlnet/test_controlnet.py b/examples/controlnet/test_controlnet.py index d595a1a312b0..0522e765f0b2 100644 --- a/examples/controlnet/test_controlnet.py +++ b/examples/controlnet/test_controlnet.py @@ -32,7 +32,7 @@ class ControlNet(ExamplesTestsAccelerate): def test_controlnet_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -55,7 +55,7 @@ def test_controlnet_checkpointing_checkpoints_total_limit(self): ) def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -98,7 +98,7 @@ def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_check class ControlNetSDXL(ExamplesTestsAccelerate): def test_controlnet_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sdxl.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -119,7 +119,7 @@ def test_controlnet_sdxl(self): class ControlNetSD3(ExamplesTestsAccelerate): def test_controlnet_sd3(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sd3.py --pretrained_model_name_or_path=DavyMorgan/tiny-sd3-pipe @@ -140,7 +140,7 @@ def test_controlnet_sd3(self): class ControlNetSD35(ExamplesTestsAccelerate): def test_controlnet_sd3(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sd3.py --pretrained_model_name_or_path=hf-internal-testing/tiny-sd35-pipe @@ -161,7 +161,7 @@ def test_controlnet_sd3(self): class ControlNetflux(ExamplesTestsAccelerate): def test_controlnet_flux(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_flux.py --pretrained_model_name_or_path=hf-internal-testing/tiny-flux-pipe diff --git a/examples/custom_diffusion/test_custom_diffusion.py b/examples/custom_diffusion/test_custom_diffusion.py index 0d7e895b3bb8..c47a12e8f21f 100644 --- a/examples/custom_diffusion/test_custom_diffusion.py +++ b/examples/custom_diffusion/test_custom_diffusion.py @@ -32,7 +32,7 @@ class CustomDiffusion(ExamplesTestsAccelerate): def test_custom_diffusion(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -57,7 +57,7 @@ def test_custom_diffusion(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, ".bin"))) def test_custom_diffusion_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -79,7 +79,7 @@ def test_custom_diffusion_checkpointing_checkpoints_total_limit(self): self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"}) def test_custom_diffusion_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/dreambooth/test_dreambooth.py b/examples/dreambooth/test_dreambooth.py index 86d0438fc25f..d3b67c4aa435 100644 --- a/examples/dreambooth/test_dreambooth.py +++ b/examples/dreambooth/test_dreambooth.py @@ -35,7 +35,7 @@ class DreamBooth(ExamplesTestsAccelerate): def test_dreambooth(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -58,7 +58,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_if(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path hf-internal-testing/tiny-if-pipe @@ -87,7 +87,7 @@ def test_dreambooth_checkpointing(self): instance_prompt = "photo" pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -163,7 +163,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -186,7 +186,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/dreambooth/test_dreambooth_flux.py b/examples/dreambooth/test_dreambooth_flux.py index 2d5703d2a24a..1fbf36575623 100644 --- a/examples/dreambooth/test_dreambooth_flux.py +++ b/examples/dreambooth/test_dreambooth_flux.py @@ -40,7 +40,7 @@ class DreamBoothFlux(ExamplesTestsAccelerate): script_path = "examples/dreambooth/train_dreambooth_flux.py" def test_dreambooth(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -63,7 +63,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -139,7 +139,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -162,7 +162,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora.py b/examples/dreambooth/test_dreambooth_lora.py index 9df104e071af..7ba29009f95e 100644 --- a/examples/dreambooth/test_dreambooth_lora.py +++ b/examples/dreambooth/test_dreambooth_lora.py @@ -36,7 +36,7 @@ class DreamBoothLoRA(ExamplesTestsAccelerate): def test_dreambooth_lora(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -68,7 +68,7 @@ def test_dreambooth_lora(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_with_text_encoder(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -102,7 +102,7 @@ def test_dreambooth_lora_with_text_encoder(self): self.assertTrue(is_correct_naming) def test_dreambooth_lora_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -125,7 +125,7 @@ def test_dreambooth_lora_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -163,7 +163,7 @@ def test_dreambooth_lora_checkpointing_checkpoints_total_limit_removes_multiple_ self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-6", "checkpoint-8"}) def test_dreambooth_lora_if_model(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-if-pipe @@ -200,7 +200,7 @@ def test_dreambooth_lora_if_model(self): class DreamBoothLoRASDXL(ExamplesTestsAccelerate): def test_dreambooth_lora_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -232,7 +232,7 @@ def test_dreambooth_lora_sdxl(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_sdxl_with_text_encoder(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -268,7 +268,7 @@ def test_dreambooth_lora_sdxl_with_text_encoder(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_sdxl_custom_captions(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -289,7 +289,7 @@ def test_dreambooth_lora_sdxl_custom_captions(self): run_command(self._launch_args + test_args) def test_dreambooth_lora_sdxl_text_encoder_custom_captions(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -313,7 +313,7 @@ def test_dreambooth_lora_sdxl_text_encoder_custom_captions(self): def test_dreambooth_lora_sdxl_checkpointing_checkpoints_total_limit(self): pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path {pipeline_path} @@ -345,7 +345,7 @@ def test_dreambooth_lora_sdxl_checkpointing_checkpoints_total_limit(self): def test_dreambooth_lora_sdxl_text_encoder_checkpointing_checkpoints_total_limit(self): pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path {pipeline_path} diff --git a/examples/dreambooth/test_dreambooth_lora_edm.py b/examples/dreambooth/test_dreambooth_lora_edm.py index 0f6b3674b8b3..f873ff140e00 100644 --- a/examples/dreambooth/test_dreambooth_lora_edm.py +++ b/examples/dreambooth/test_dreambooth_lora_edm.py @@ -34,7 +34,7 @@ class DreamBoothLoRASDXLWithEDM(ExamplesTestsAccelerate): def test_dreambooth_lora_sdxl_with_edm(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -67,7 +67,7 @@ def test_dreambooth_lora_sdxl_with_edm(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_playground(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-playground-v2-5-pipe diff --git a/examples/dreambooth/test_dreambooth_lora_flux.py b/examples/dreambooth/test_dreambooth_lora_flux.py index a76825e29448..77f599e3376c 100644 --- a/examples/dreambooth/test_dreambooth_lora_flux.py +++ b/examples/dreambooth/test_dreambooth_lora_flux.py @@ -40,7 +40,7 @@ class DreamBoothLoRAFlux(ExamplesTestsAccelerate): transformer_layer_type = "single_transformer_blocks.0.attn.to_k" def test_dreambooth_lora_flux(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +72,7 @@ def test_dreambooth_lora_flux(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_flux(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -105,7 +105,7 @@ def test_dreambooth_lora_text_encoder_flux(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -138,7 +138,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -175,7 +175,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -198,7 +198,7 @@ def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_lumina2.py b/examples/dreambooth/test_dreambooth_lora_lumina2.py index 1b729a0ff52e..6da5f51f7c58 100644 --- a/examples/dreambooth/test_dreambooth_lora_lumina2.py +++ b/examples/dreambooth/test_dreambooth_lora_lumina2.py @@ -39,7 +39,7 @@ class DreamBoothLoRAlumina2(ExamplesTestsAccelerate): transformer_layer_type = "layers.0.attn.to_k" def test_dreambooth_lora_lumina2(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +72,7 @@ def test_dreambooth_lora_lumina2(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -106,7 +106,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -142,7 +142,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -166,7 +166,7 @@ def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_sana.py b/examples/dreambooth/test_dreambooth_lora_sana.py index dfceb09a9736..685a49cac599 100644 --- a/examples/dreambooth/test_dreambooth_lora_sana.py +++ b/examples/dreambooth/test_dreambooth_lora_sana.py @@ -39,7 +39,7 @@ class DreamBoothLoRASANA(ExamplesTestsAccelerate): transformer_layer_type = "transformer_blocks.0.attn1.to_k" def test_dreambooth_lora_sana(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +72,7 @@ def test_dreambooth_lora_sana(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -106,7 +106,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -142,7 +142,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -166,7 +166,7 @@ def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_sd3.py b/examples/dreambooth/test_dreambooth_lora_sd3.py index 5d6c8bb9938a..400e651b14b8 100644 --- a/examples/dreambooth/test_dreambooth_lora_sd3.py +++ b/examples/dreambooth/test_dreambooth_lora_sd3.py @@ -42,7 +42,7 @@ class DreamBoothLoRASD3(ExamplesTestsAccelerate): layer_type = "attn.to_k" def test_dreambooth_lora_sd3(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -74,7 +74,7 @@ def test_dreambooth_lora_sd3(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_sd3(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -107,7 +107,7 @@ def test_dreambooth_lora_text_encoder_sd3(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -140,7 +140,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_block(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -176,7 +176,7 @@ def test_dreambooth_lora_block(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layer(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -208,7 +208,7 @@ def test_dreambooth_lora_layer(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -231,7 +231,7 @@ def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_sd3.py b/examples/dreambooth/test_dreambooth_sd3.py index 19fb7243cefd..fd9b1d070b0b 100644 --- a/examples/dreambooth/test_dreambooth_sd3.py +++ b/examples/dreambooth/test_dreambooth_sd3.py @@ -40,7 +40,7 @@ class DreamBoothSD3(ExamplesTestsAccelerate): script_path = "examples/dreambooth/train_dreambooth_sd3.py" def test_dreambooth(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -63,7 +63,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -139,7 +139,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -162,7 +162,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/instruct_pix2pix/test_instruct_pix2pix.py b/examples/instruct_pix2pix/test_instruct_pix2pix.py index f72f95db6afd..bff99e498b39 100644 --- a/examples/instruct_pix2pix/test_instruct_pix2pix.py +++ b/examples/instruct_pix2pix/test_instruct_pix2pix.py @@ -32,7 +32,7 @@ class InstructPix2Pix(ExamplesTestsAccelerate): def test_instruct_pix2pix_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/instruct_pix2pix/train_instruct_pix2pix.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -55,7 +55,7 @@ def test_instruct_pix2pix_checkpointing_checkpoints_total_limit(self): ) def test_instruct_pix2pix_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/instruct_pix2pix/train_instruct_pix2pix.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/t2i_adapter/test_t2i_adapter.py b/examples/t2i_adapter/test_t2i_adapter.py index cdf124cdd932..1c17c920a016 100644 --- a/examples/t2i_adapter/test_t2i_adapter.py +++ b/examples/t2i_adapter/test_t2i_adapter.py @@ -32,7 +32,7 @@ class T2IAdapter(ExamplesTestsAccelerate): def test_t2i_adapter_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/t2i_adapter/train_t2i_adapter_sdxl.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/text_to_image/test_text_to_image.py b/examples/text_to_image/test_text_to_image.py index 7a599aeb351d..c99b9fa6308e 100644 --- a/examples/text_to_image/test_text_to_image.py +++ b/examples/text_to_image/test_text_to_image.py @@ -36,7 +36,7 @@ class TextToImage(ExamplesTestsAccelerate): def test_text_to_image(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -63,7 +63,7 @@ def test_text_to_image_checkpointing(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -145,7 +145,7 @@ def test_text_to_image_checkpointing_use_ema(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -229,7 +229,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -268,7 +268,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit_removes_multiple_ch pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -341,7 +341,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit_removes_multiple_ch class TextToImageSDXL(ExamplesTestsAccelerate): def test_text_to_image_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/text_to_image/test_text_to_image_lora.py b/examples/text_to_image/test_text_to_image_lora.py index 2406515c36d2..53c990fc69b0 100644 --- a/examples/text_to_image/test_text_to_image_lora.py +++ b/examples/text_to_image/test_text_to_image_lora.py @@ -40,7 +40,7 @@ def test_text_to_image_lora_sdxl_checkpointing_checkpoints_total_limit(self): prompt = "a prompt" pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -77,7 +77,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -120,7 +120,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit_removes_multip pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -201,7 +201,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit_removes_multip class TextToImageLoRASDXL(ExamplesTestsAccelerate): def test_text_to_image_lora_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -227,7 +227,7 @@ def test_text_to_image_lora_sdxl(self): self.assertTrue(is_lora) def test_text_to_image_lora_sdxl_with_text_encoder(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -265,7 +265,7 @@ def test_text_to_image_lora_sdxl_text_encoder_checkpointing_checkpoints_total_li prompt = "a prompt" pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 diff --git a/examples/textual_inversion/test_textual_inversion.py b/examples/textual_inversion/test_textual_inversion.py index fa0b2c2bcf09..013479753604 100644 --- a/examples/textual_inversion/test_textual_inversion.py +++ b/examples/textual_inversion/test_textual_inversion.py @@ -32,7 +32,7 @@ class TextualInversion(ExamplesTestsAccelerate): def test_textual_inversion(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -58,7 +58,7 @@ def test_textual_inversion(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.safetensors"))) def test_textual_inversion_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -90,7 +90,7 @@ def test_textual_inversion_checkpointing(self): ) def test_textual_inversion_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/textual_inversion/test_textual_inversion_sdxl.py b/examples/textual_inversion/test_textual_inversion_sdxl.py index a861708a70f9..13c5b6fa5de9 100644 --- a/examples/textual_inversion/test_textual_inversion_sdxl.py +++ b/examples/textual_inversion/test_textual_inversion_sdxl.py @@ -32,7 +32,7 @@ class TextualInversionSdxl(ExamplesTestsAccelerate): def test_textual_inversion_sdxl(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe @@ -58,7 +58,7 @@ def test_textual_inversion_sdxl(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.safetensors"))) def test_textual_inversion_sdxl_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe @@ -90,7 +90,7 @@ def test_textual_inversion_sdxl_checkpointing(self): ) def test_textual_inversion_sdxl_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe diff --git a/examples/unconditional_image_generation/test_unconditional.py b/examples/unconditional_image_generation/test_unconditional.py index ef71da0a114c..df7fc7668b98 100644 --- a/examples/unconditional_image_generation/test_unconditional.py +++ b/examples/unconditional_image_generation/test_unconditional.py @@ -32,7 +32,7 @@ class Unconditional(ExamplesTestsAccelerate): def test_train_unconditional(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data @@ -53,7 +53,7 @@ def test_train_unconditional(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_unconditional_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: initial_run_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data @@ -80,7 +80,7 @@ def test_unconditional_checkpointing_checkpoints_total_limit(self): ) def test_unconditional_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: initial_run_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data diff --git a/examples/vqgan/test_vqgan.py b/examples/vqgan/test_vqgan.py index aa5d4c67b642..2933d5ae7a22 100644 --- a/examples/vqgan/test_vqgan.py +++ b/examples/vqgan/test_vqgan.py @@ -88,7 +88,7 @@ def get_vq_and_discriminator_configs(self, tmpdir): return vqmodel_config_path, discriminator_config_path def test_vqmodel(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) test_args = f""" examples/vqgan/train_vqgan.py @@ -115,7 +115,7 @@ def test_vqmodel(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "vqmodel", "diffusion_pytorch_model.safetensors"))) def test_vqmodel_checkpointing(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 @@ -199,7 +199,7 @@ def test_vqmodel_checkpointing(self): ) def test_vqmodel_checkpointing_use_ema(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 @@ -283,7 +283,7 @@ def test_vqmodel_checkpointing_use_ema(self): ) def test_vqmodel_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 @@ -321,7 +321,7 @@ def test_vqmodel_checkpointing_checkpoints_total_limit(self): self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"}) def test_vqmodel_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 diff --git a/src/diffusers/pipelines/transformers_loading_utils.py b/src/diffusers/pipelines/transformers_loading_utils.py index b52d154d6ba2..21687afbe4cc 100644 --- a/src/diffusers/pipelines/transformers_loading_utils.py +++ b/src/diffusers/pipelines/transformers_loading_utils.py @@ -44,7 +44,7 @@ def _load_tokenizer_from_dduf( files. There is an extra cost of extracting the files, but of limited impact as the tokenizer files are usually small-ish. """ - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: for entry_name, entry in dduf_entries.items(): if entry_name.startswith(name + "/"): tmp_entry_path = os.path.join(tmp_dir, *entry_name.split("/")) @@ -91,7 +91,7 @@ def _load_transformers_model_from_dduf( "You can install it with: `pip install --upgrade transformers`" ) - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: from transformers import AutoConfig, GenerationConfig tmp_config_file = os.path.join(tmp_dir, "config.json") diff --git a/src/diffusers/utils/hub_utils.py b/src/diffusers/utils/hub_utils.py index f80f96a3425d..207ff4a7626a 100644 --- a/src/diffusers/utils/hub_utils.py +++ b/src/diffusers/utils/hub_utils.py @@ -542,7 +542,7 @@ def push_to_hub( if "Scheduler" not in self.__class__.__name__: save_kwargs.update({"variant": variant}) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: self.save_pretrained(tmpdir, **save_kwargs) # Update model card if needed: diff --git a/tests/lora/test_deprecated_utilities.py b/tests/lora/test_deprecated_utilities.py index 4275ef8089a3..166273946e17 100644 --- a/tests/lora/test_deprecated_utilities.py +++ b/tests/lora/test_deprecated_utilities.py @@ -29,7 +29,7 @@ def test_fetch_state_dict_cls_method_raises_warning(self): assert "Using the `_fetch_state_dict()` method from" in warning_message def test_best_guess_weight_name_cls_method_raises_warning(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: state_dict = torch.nn.Linear(3, 3).state_dict() torch.save(state_dict, os.path.join(tmpdir, "pytorch_lora_weights.bin")) diff --git a/tests/lora/test_lora_layers_cogview4.py b/tests/lora/test_lora_layers_cogview4.py index 178de2069b7e..d29d43b5b7fc 100644 --- a/tests/lora/test_lora_layers_cogview4.py +++ b/tests/lora/test_lora_layers_cogview4.py @@ -128,7 +128,7 @@ def test_simple_inference_save_pretrained(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_from_pretrained = self.pipeline_class.from_pretrained(tmpdirname) diff --git a/tests/lora/test_lora_layers_flux.py b/tests/lora/test_lora_layers_flux.py index 860aa6511689..7237d8fb0b8a 100644 --- a/tests/lora/test_lora_layers_flux.py +++ b/tests/lora/test_lora_layers_flux.py @@ -130,7 +130,7 @@ def test_with_alpha_in_state_dict(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0)).images - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) @@ -186,7 +186,7 @@ def test_lora_expansion_works_for_absent_keys(self): "LoRA should lead to different results.", ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) @@ -235,7 +235,7 @@ def test_lora_expansion_works_for_extra_keys(self): "LoRA should lead to different results.", ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) diff --git a/tests/lora/utils.py b/tests/lora/utils.py index 8cdb43c9d085..71f98fc2c20b 100644 --- a/tests/lora/utils.py +++ b/tests/lora/utils.py @@ -400,7 +400,7 @@ def test_low_cpu_mem_usage_with_loading(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -608,7 +608,7 @@ def test_simple_inference_with_text_lora_save_load(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) @@ -720,7 +720,7 @@ def test_simple_inference_save_pretrained(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_from_pretrained = self.pipeline_class.from_pretrained(tmpdirname) @@ -778,7 +778,7 @@ def test_simple_inference_with_text_denoiser_lora_save_load(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -1838,7 +1838,7 @@ def test_missing_keys_warning(self): denoiser.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(denoiser), "Lora not correctly set in denoiser.") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -1875,7 +1875,7 @@ def test_unexpected_keys_warning(self): denoiser.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(denoiser), "Lora not correctly set in denoiser.") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -2047,7 +2047,7 @@ def test_set_adapters_match_attention_kwargs(self): "Lora + scale should match the output of `set_adapters()`.", ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -2310,7 +2310,7 @@ def check_module(denoiser): pipe(**inputs, generator=torch.manual_seed(0))[0] # 2. Test forward with load_lora_weights - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( diff --git a/tests/models/test_attention_processor.py b/tests/models/test_attention_processor.py index d070f6ea33e3..4edf28970456 100644 --- a/tests/models/test_attention_processor.py +++ b/tests/models/test_attention_processor.py @@ -114,7 +114,7 @@ def test_conversion_when_using_device_map(self): output_type="np", ).images - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # save the converted model pipe.save_pretrained(tmpdir) diff --git a/tests/models/test_modeling_common.py b/tests/models/test_modeling_common.py index 847677884a35..77ff00ce0128 100644 --- a/tests/models/test_modeling_common.py +++ b/tests/models/test_modeling_common.py @@ -112,7 +112,7 @@ def _test_from_save_pretrained_dynamo(in_queue, out_queue, timeout): model.to(torch_device) model = torch.compile(model) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = model_class.from_pretrained(tmpdirname) new_model.to(torch_device) @@ -230,7 +230,7 @@ def load_model(path): with self.assertWarns(FutureWarning) as warning: if use_local: - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = snapshot_download(repo_id=repo_id) _ = load_model(tmpdirname) else: @@ -288,7 +288,7 @@ def test_cached_files_are_used_when_no_internet(self): def test_one_request_upon_cached(self): use_safetensors = False - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: UNet2DConditionModel.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", @@ -317,7 +317,9 @@ def test_one_request_upon_cached(self): ), "We should call only `model_info` to check for commit hash and knowing if shard index is present." def test_weight_overwrite(self): - with tempfile.TemporaryDirectory() as tmpdirname, self.assertRaises(ValueError) as error_context: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname, self.assertRaises( + ValueError + ) as error_context: UNet2DConditionModel.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", subfolder="unet", @@ -328,7 +330,7 @@ def test_weight_overwrite(self): # make sure that error message states what keys are missing assert "Cannot load" in str(error_context.exception) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model = UNet2DConditionModel.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", subfolder="unet", @@ -448,7 +450,7 @@ def test_from_save_pretrained(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname) if hasattr(new_model, "set_default_attn_processor"): @@ -685,7 +687,7 @@ def test_from_save_pretrained_variant(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, variant="fp16", safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname, variant="fp16") if hasattr(new_model, "set_default_attn_processor"): @@ -740,7 +742,7 @@ def test_from_save_pretrained_dtype(self): for dtype in [torch.float32, torch.float16, torch.bfloat16]: if torch_device == "mps" and dtype == torch.bfloat16: continue - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.to(dtype) model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname, low_cpu_mem_usage=True, torch_dtype=dtype) @@ -817,7 +819,7 @@ def test_model_from_pretrained(self): # test if the model can be loaded from the config # and has all the expected shape - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname) new_model.to(torch_device) @@ -1088,7 +1090,7 @@ def test_save_load_lora_adapter(self, use_dora=False): self.assertFalse(torch.allclose(output_no_lora, outputs_with_lora, atol=1e-4, rtol=1e-4)) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: model.save_lora_adapter(tmpdir) self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) @@ -1135,7 +1137,7 @@ def test_wrong_adapter_name_raises_error(self): model.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(model), "LoRA layers not set correctly") - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: wrong_name = "foo" with self.assertRaises(ValueError) as err_context: model.save_lora_adapter(tmpdir, adapter_name=wrong_name) @@ -1157,7 +1159,7 @@ def test_cpu_offload(self): model_size = compute_module_sizes(model)[""] # We test several splits of sizes to make sure it works. max_gpu_sizes = [int(p * model_size) for p in self.model_split_percents[1:]] - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) for max_size in max_gpu_sizes: @@ -1189,7 +1191,7 @@ def test_disk_offload_without_safetensors(self): # Force disk offload by setting very small CPU memory max_memory = {0: max_size, "cpu": int(0.1 * max_size)} - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, safe_serialization=False) with self.assertRaises(ValueError): # This errors out because it's missing an offload folder @@ -1218,7 +1220,7 @@ def test_disk_offload_with_safetensors(self): base_output = model(**inputs_dict) model_size = compute_module_sizes(model)[""] - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) max_size = int(self.model_split_percents[0] * model_size) @@ -1248,7 +1250,7 @@ def test_model_parallelism(self): model_size = compute_module_sizes(model)[""] # We test several splits of sizes to make sure it works. max_gpu_sizes = [int(p * model_size) for p in self.model_split_percents[1:]] - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) for max_size in max_gpu_sizes: @@ -1276,7 +1278,7 @@ def test_sharded_checkpoints(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, max_shard_size=f"{max_shard_size}KB") self.assertTrue(os.path.exists(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) @@ -1309,7 +1311,7 @@ def test_sharded_checkpoints_with_variant(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. variant = "fp16" - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: # It doesn't matter if the actual model is in fp16 or not. Just adding the variant and # testing if loading works with the variant when the checkpoint is sharded should be # enough. @@ -1348,7 +1350,7 @@ def test_sharded_checkpoints_device_map(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, max_shard_size=f"{max_shard_size}KB") self.assertTrue(os.path.exists(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) @@ -1378,7 +1380,7 @@ def test_variant_sharded_ckpt_right_format(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. variant = "fp16" - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained( tmp_dir, variant=variant, max_shard_size=f"{max_shard_size}KB", safe_serialization=use_safe ) @@ -1605,7 +1607,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) new_model = UNet2DConditionModel.from_pretrained(f"{USER}/{self.repo_id}") @@ -1636,7 +1638,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir, push_to_hub=True, token=TOKEN, repo_id=self.org_repo_id) new_model = UNet2DConditionModel.from_pretrained(self.org_repo_id) @@ -1780,7 +1782,7 @@ def check_model_hotswap(self, do_compile, rank0, rank1, target_modules0, target_ assert not (output0_before == 0).all() assert not (output1_before == 0).all() - with tempfile.TemporaryDirectory() as tmp_dirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints unet.save_lora_adapter(os.path.join(tmp_dirname, "0"), safe_serialization=True, adapter_name="adapter0") unet.save_lora_adapter(os.path.join(tmp_dirname, "1"), safe_serialization=True, adapter_name="adapter1") diff --git a/tests/models/unets/test_models_unet_2d_condition.py b/tests/models/unets/test_models_unet_2d_condition.py index 8e1187f11468..838b259d3959 100644 --- a/tests/models/unets/test_models_unet_2d_condition.py +++ b/tests/models/unets/test_models_unet_2d_condition.py @@ -748,7 +748,7 @@ def test_custom_diffusion_save_load(self): with torch.no_grad(): sample = model(**inputs_dict).sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_attn_procs(tmpdirname, safe_serialization=False) self.assertTrue(os.path.isfile(os.path.join(tmpdirname, "pytorch_custom_diffusion_weights.bin"))) torch.manual_seed(0) @@ -1098,7 +1098,7 @@ def test_load_attn_procs_raise_warning(self): with torch.no_grad(): lora_sample_1 = model(**inputs_dict).sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_attn_procs(tmpdirname) model.unload_lora() @@ -1132,7 +1132,7 @@ def test_save_attn_procs_raise_warning(self): assert check_if_lora_correctly_set(model), "Lora not correctly set in UNet." - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertWarns(FutureWarning) as warning: model.save_attn_procs(tmpdirname) diff --git a/tests/models/unets/test_models_unet_motion.py b/tests/models/unets/test_models_unet_motion.py index 209806a5fe26..acaf75b4c522 100644 --- a/tests/models/unets/test_models_unet_motion.py +++ b/tests/models/unets/test_models_unet_motion.py @@ -127,7 +127,7 @@ def test_saving_motion_modules(self): model = self.model_class(**init_dict) model.to(torch_device) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_motion_modules(tmpdirname) self.assertTrue(os.path.isfile(os.path.join(tmpdirname, "diffusion_pytorch_model.safetensors"))) @@ -211,7 +211,7 @@ def test_from_save_pretrained(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) torch.manual_seed(0) new_model = self.model_class.from_pretrained(tmpdirname) @@ -238,7 +238,7 @@ def test_from_save_pretrained_variant(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, variant="fp16", safe_serialization=False) torch.manual_seed(0) diff --git a/tests/others/test_config.py b/tests/others/test_config.py index 664c36ac33d6..66e416a2879c 100644 --- a/tests/others/test_config.py +++ b/tests/others/test_config.py @@ -152,7 +152,7 @@ def test_save_load(self): assert config["d"] == "for diffusion" assert config["e"] == [1, 3] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) new_obj = SampleObject.from_config(SampleObject.load_config(tmpdirname)) new_config = new_obj.config @@ -276,7 +276,7 @@ def test_use_default_values(self): # make sure that default config has all keys in `_use_default_values` assert set(config_dict.keys()) == set(config.config._use_default_values) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: config.save_config(tmpdirname) # now loading it with SampleObject2 should put f into `_use_default_values` diff --git a/tests/others/test_ema.py b/tests/others/test_ema.py index 7cf8f30ecc44..87d06f097184 100644 --- a/tests/others/test_ema.py +++ b/tests/others/test_ema.py @@ -62,7 +62,7 @@ def simulate_backprop(self, unet): def test_from_pretrained(self): # Save the model parameters to a temporary directory unet, ema_unet = self.get_models() - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) # Load the EMA model from the saved directory @@ -167,7 +167,7 @@ def test_serialization(self): unet, ema_unet = self.get_models() noisy_latents, timesteps, encoder_hidden_states = self.get_dummy_inputs() - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) loaded_unet = UNet2DConditionModel.from_pretrained(tmpdir, model_cls=UNet2DConditionModel) loaded_unet = loaded_unet.to(unet.device) @@ -217,7 +217,7 @@ def simulate_backprop(self, unet): def test_from_pretrained(self): # Save the model parameters to a temporary directory unet, ema_unet = self.get_models() - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) # Load the EMA model from the saved directory @@ -322,7 +322,7 @@ def test_serialization(self): unet, ema_unet = self.get_models() noisy_latents, timesteps, encoder_hidden_states = self.get_dummy_inputs() - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) loaded_unet = UNet2DConditionModel.from_pretrained(tmpdir, model_cls=UNet2DConditionModel) loaded_unet = loaded_unet.to(unet.device) diff --git a/tests/pipelines/allegro/test_allegro.py b/tests/pipelines/allegro/test_allegro.py index 30fdd68cfd36..9dc8b9d0b698 100644 --- a/tests/pipelines/allegro/test_allegro.py +++ b/tests/pipelines/allegro/test_allegro.py @@ -320,7 +320,7 @@ def test_save_load_dduf(self): pipeline_out = pipe(**inputs)[0].cpu() - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dduf_filename = os.path.join(tmpdir, f"{pipe.__class__.__name__.lower()}.dduf") pipe.save_pretrained(tmpdir, safe_serialization=True) export_folder_as_dduf(dduf_filename, folder_path=tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet.py b/tests/pipelines/controlnet/test_controlnet.py index bb21c9ac8dcb..de45ced2815f 100644 --- a/tests/pipelines/controlnet/test_controlnet.py +++ b/tests/pipelines/controlnet/test_controlnet.py @@ -486,7 +486,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) @@ -714,7 +714,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet_img2img.py b/tests/pipelines/controlnet/test_controlnet_img2img.py index 100765ee34cb..705e3f0c0f6b 100644 --- a/tests/pipelines/controlnet/test_controlnet_img2img.py +++ b/tests/pipelines/controlnet/test_controlnet_img2img.py @@ -391,7 +391,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet_inpaint.py b/tests/pipelines/controlnet/test_controlnet_inpaint.py index b06590e13cb6..1032dd702a07 100644 --- a/tests/pipelines/controlnet/test_controlnet_inpaint.py +++ b/tests/pipelines/controlnet/test_controlnet_inpaint.py @@ -443,7 +443,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/deepfloyd_if/__init__.py b/tests/pipelines/deepfloyd_if/__init__.py index 094254a61875..54262cc13526 100644 --- a/tests/pipelines/deepfloyd_if/__init__.py +++ b/tests/pipelines/deepfloyd_if/__init__.py @@ -203,7 +203,7 @@ def _test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) @@ -257,7 +257,7 @@ def _test_save_load_local(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/hunyuandit/test_hunyuan_dit.py b/tests/pipelines/hunyuandit/test_hunyuan_dit.py index 5b1a82eda227..1718d154cbfe 100644 --- a/tests/pipelines/hunyuandit/test_hunyuan_dit.py +++ b/tests/pipelines/hunyuandit/test_hunyuan_dit.py @@ -267,7 +267,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/latte/test_latte.py b/tests/pipelines/latte/test_latte.py index 80d370647f57..0836d3db9128 100644 --- a/tests/pipelines/latte/test_latte.py +++ b/tests/pipelines/latte/test_latte.py @@ -280,7 +280,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pag/test_pag_hunyuan_dit.py b/tests/pipelines/pag/test_pag_hunyuan_dit.py index 31cd9aa666de..01c7dbe8fa9b 100644 --- a/tests/pipelines/pag/test_pag_hunyuan_dit.py +++ b/tests/pipelines/pag/test_pag_hunyuan_dit.py @@ -323,7 +323,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pag/test_pag_pixart_sigma.py b/tests/pipelines/pag/test_pag_pixart_sigma.py index 63f42416dbca..4199f22756b2 100644 --- a/tests/pipelines/pag/test_pag_pixart_sigma.py +++ b/tests/pipelines/pag/test_pag_pixart_sigma.py @@ -200,7 +200,7 @@ def test_save_load_local(self, expected_max_difference=1e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: diff --git a/tests/pipelines/pixart_alpha/test_pixart.py b/tests/pipelines/pixart_alpha/test_pixart.py index ea5cfcef86fd..0d9babc5c9df 100644 --- a/tests/pipelines/pixart_alpha/test_pixart.py +++ b/tests/pipelines/pixart_alpha/test_pixart.py @@ -183,7 +183,7 @@ def test_inference_with_embeddings_and_multiple_images(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pixart_sigma/test_pixart.py b/tests/pipelines/pixart_sigma/test_pixart.py index b220afcfc25a..302ba40f8256 100644 --- a/tests/pipelines/pixart_sigma/test_pixart.py +++ b/tests/pipelines/pixart_sigma/test_pixart.py @@ -184,7 +184,7 @@ def test_inference_with_embeddings_and_multiple_images(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py b/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py index 6cd431f02d58..02cd1f12f945 100644 --- a/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py +++ b/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py @@ -229,7 +229,7 @@ def test_semantic_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py b/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py index f7036dee47f0..ed987e0badcd 100644 --- a/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py +++ b/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py @@ -366,7 +366,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = OnnxStableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion/test_stable_diffusion.py b/tests/pipelines/stable_diffusion/test_stable_diffusion.py index 6e17b86639ea..bcc3d45c1ed9 100644 --- a/tests/pipelines/stable_diffusion/test_stable_diffusion.py +++ b/tests/pipelines/stable_diffusion/test_stable_diffusion.py @@ -428,7 +428,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py index 0a0051816162..31f7c75be952 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py @@ -186,7 +186,7 @@ def test_save_load_local(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) @@ -212,7 +212,7 @@ def test_save_load_float16(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py index 34ea56664a95..239c2c426c82 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py @@ -210,7 +210,7 @@ def test_save_load_optional_components(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py index 5400c21c9f87..9c6ab5b65e66 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py @@ -358,7 +358,7 @@ def test_stable_diffusion_upscale_from_save_pretrained(self): sd_pipe = sd_pipe.to(device) pipes.append(sd_pipe) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd_pipe.save_pretrained(tmpdirname) sd_pipe = StableDiffusionUpscalePipeline.from_pretrained(tmpdirname).to(device) pipes.append(sd_pipe) diff --git a/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py b/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py index 269677c08345..ce5749bc9961 100644 --- a/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py +++ b/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py @@ -219,7 +219,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py b/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py index c68cdf67036a..26728c19593f 100644 --- a/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py +++ b/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py @@ -866,7 +866,7 @@ def test_stable_diffusion_xl_save_from_pretrained(self): sd_pipe = StableDiffusionXLPipeline(**components).to(torch_device) pipes.append(sd_pipe) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd_pipe.save_pretrained(tmpdirname) sd_pipe = StableDiffusionXLPipeline.from_pretrained(tmpdirname).to(torch_device) pipes.append(sd_pipe) diff --git a/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py b/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py index f77a5b1620d2..71a73198c220 100644 --- a/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py +++ b/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py @@ -273,7 +273,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs).frames[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) for component in pipe_loaded.components.values(): @@ -316,7 +316,7 @@ def test_save_load_optional_components(self, expected_max_difference=1e-4): inputs = self.get_dummy_inputs(generator_device) output = pipe(**inputs).frames[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): @@ -353,7 +353,7 @@ def test_save_load_local(self, expected_max_difference=9e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: diff --git a/tests/pipelines/test_pipelines.py b/tests/pipelines/test_pipelines.py index ae5a12e04ba8..2e650cae0d94 100644 --- a/tests/pipelines/test_pipelines.py +++ b/tests/pipelines/test_pipelines.py @@ -121,7 +121,7 @@ def _test_from_save_pretrained_dynamo(in_queue, out_queue, timeout): ddpm.to(torch_device) ddpm.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ddpm.save_pretrained(tmpdirname) new_ddpm = DDPMPipeline.from_pretrained(tmpdirname) new_ddpm.to(torch_device) @@ -160,7 +160,7 @@ def test_one_request_upon_cached(self): if torch_device == "mps": return - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: DiffusionPipeline.download("hf-internal-testing/tiny-stable-diffusion-pipe", cache_dir=tmpdirname) @@ -184,7 +184,7 @@ def test_one_request_upon_cached(self): ), "We should call only `model_info` to check for _commit hash and `send_telemetry`" def test_less_downloads_passed_object(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: cached_folder = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname ) @@ -206,7 +206,7 @@ def test_less_downloads_passed_object_calls(self): if torch_device == "mps": return - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname @@ -234,7 +234,7 @@ def test_less_downloads_passed_object_calls(self): ), "We should call only `model_info` to check for _commit hash and `send_telemetry`" def test_download_only_pytorch(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname @@ -250,7 +250,7 @@ def test_download_only_pytorch(self): assert not any(f.endswith(".safetensors") for f in files) def test_force_safetensors_error(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights with self.assertRaises(EnvironmentError): tmpdirname = DiffusionPipeline.download( @@ -261,7 +261,7 @@ def test_force_safetensors_error(self): ) def test_download_safetensors(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-safetensors", @@ -278,7 +278,7 @@ def test_download_safetensors(self): def test_download_safetensors_index(self): for variant in ["fp16", None]: - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-indexes", cache_dir=tmpdirname, @@ -302,7 +302,7 @@ def test_download_safetensors_index(self): def test_download_bin_index(self): for variant in ["fp16", None]: - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-indexes", cache_dir=tmpdirname, @@ -325,7 +325,7 @@ def test_download_bin_index(self): assert not any(".safetensors" in f for f in files) def test_download_no_openvino_by_default(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-open-vino", cache_dir=tmpdirname, @@ -339,7 +339,7 @@ def test_download_no_openvino_by_default(self): assert not any("openvino_" in f for f in files) def test_download_no_onnx_by_default(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-xl-pipe", cache_dir=tmpdirname, @@ -355,7 +355,7 @@ def test_download_no_onnx_by_default(self): @require_onnxruntime def test_download_onnx_by_default_for_onnx_pipelines(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline", cache_dir=tmpdirname, @@ -394,7 +394,7 @@ def test_load_no_safety_checker_explicit_locally(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_2 = StableDiffusionPipeline.from_pretrained(tmpdirname, safety_checker=None) pipe_2 = pipe_2.to(torch_device) @@ -413,7 +413,7 @@ def test_load_no_safety_checker_default_locally(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_2 = StableDiffusionPipeline.from_pretrained(tmpdirname) pipe_2 = pipe_2.to(torch_device) @@ -461,7 +461,7 @@ def test_local_files_only_are_used_when_no_internet(self): # first check that with local files only the pipeline can only be used if cached with self.assertRaises(FileNotFoundError): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: orig_pipe = DiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", local_files_only=True, cache_dir=tmpdirname ) @@ -490,7 +490,7 @@ def test_local_files_only_are_used_when_no_internet(self): def test_download_from_variant_folder(self): for use_safetensors in [False, True]: other_format = ".bin" if use_safetensors else ".safetensors" - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -512,7 +512,7 @@ def test_download_variant_all(self): this_format = ".safetensors" if use_safetensors else ".bin" variant = "fp16" - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -537,7 +537,7 @@ def test_download_variant_partly(self): this_format = ".safetensors" if use_safetensors else ".bin" variant = "no_ema" - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -564,7 +564,7 @@ def test_download_variants_with_sharded_checkpoints(self): # the `text_encoder`. Their checkpoints can be sharded. for use_safetensors in [True, False]: for variant in ["fp16", None]: - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-variants-right-format", safety_checker=None, @@ -590,7 +590,7 @@ def test_download_legacy_variants_with_sharded_ckpts_raises_warning(self): for is_local in [True, False]: with CaptureLogger(logger) as cap_logger: - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: local_repo_id = repo_id if is_local: local_repo_id = snapshot_download(repo_id, cache_dir=tmpdirname) @@ -608,7 +608,7 @@ def test_download_safetensors_only_variant_exists_for_model(self): use_safetensors = True # text encoder is missing no variant weights, so the following can't work - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -619,7 +619,7 @@ def test_download_safetensors_only_variant_exists_for_model(self): assert "Error no file name" in str(error_context.exception) # text encoder has fp16 variants so we can load it - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-broken-variants", use_safetensors=use_safetensors, @@ -637,7 +637,7 @@ def test_download_bin_only_variant_exists_for_model(self): use_safetensors = False # text encoder is missing Non-variant weights, so the following can't work - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -648,7 +648,7 @@ def test_download_bin_only_variant_exists_for_model(self): assert "Error no file name" in str(error_context.exception) # text encoder has fp16 variants so we can load it - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-broken-variants", use_safetensors=use_safetensors, @@ -666,7 +666,7 @@ def test_download_safetensors_variant_does_not_exist_for_model(self): use_safetensors = True # text encoder is missing no_ema variant weights, so the following can't work - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -682,7 +682,7 @@ def test_download_bin_variant_does_not_exist_for_model(self): use_safetensors = False # text encoder is missing no_ema variant weights, so the following can't work - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -706,7 +706,7 @@ def test_local_save_load_index(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname, variant=variant, safe_serialization=use_safe) pipe_2 = StableDiffusionPipeline.from_pretrained( tmpdirname, safe_serialization=use_safe, variant=variant @@ -728,7 +728,7 @@ def test_text_inversion_download(self): num_tokens = len(pipe.tokenizer) # single token load local - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<*>": torch.ones((32,))} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -744,7 +744,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # single token load local with weight name - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<**>": 2 * torch.ones((1, 32))} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -760,7 +760,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi token load - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<***>": torch.cat([3 * torch.ones((1, 32)), 4 * torch.ones((1, 32)), 5 * torch.ones((1, 32))])} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -783,7 +783,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi token load a1111 - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = { "string_to_param": { "*": torch.cat([3 * torch.ones((1, 32)), 4 * torch.ones((1, 32)), 5 * torch.ones((1, 32))]) @@ -811,8 +811,8 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi embedding load - with tempfile.TemporaryDirectory() as tmpdirname1: - with tempfile.TemporaryDirectory() as tmpdirname2: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname1: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname2: ten = {"<*****>": torch.ones((32,))} torch.save(ten, os.path.join(tmpdirname1, "learned_embeds.bin")) @@ -977,7 +977,7 @@ def test_textual_inversion_unload(self): def test_download_ignore_files(self): # Check https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe-ignore-files/blob/72f58636e5508a218c6b3f60550dc96445547817/model_index.json#L4 - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download("hf-internal-testing/tiny-stable-diffusion-pipe-ignore-files") all_root_files = [t[-1] for t in os.walk(os.path.join(tmpdirname))] @@ -1141,7 +1141,7 @@ def test_custom_model_and_pipeline(self): scheduler=DDIMScheduler(), ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname, safe_serialization=False) pipe_new = CustomPipeline.from_pretrained(tmpdirname) @@ -1186,14 +1186,14 @@ def test_save_pipeline_change_config(self): "hf-internal-testing/tiny-stable-diffusion-torch", safety_checker=None ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = DiffusionPipeline.from_pretrained(tmpdirname) assert pipe.scheduler.__class__.__name__ == "PNDMScheduler" # let's make sure that changing the scheduler is correctly reflected - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) pipe.save_pretrained(tmpdirname) pipe = DiffusionPipeline.from_pretrained(tmpdirname) @@ -1546,7 +1546,7 @@ def test_set_scheduler_consistency(self): def test_save_safe_serialization(self): pipeline = StableDiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-torch") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipeline.save_pretrained(tmpdirname, safe_serialization=True) # Validate that the VAE safetensor exists and are of the correct format @@ -1573,7 +1573,7 @@ def test_save_safe_serialization(self): def test_no_pytorch_download_when_doing_safetensors(self): # by default we don't download - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/diffusers-stable-diffusion-tiny-all", cache_dir=tmpdirname ) @@ -1593,7 +1593,7 @@ def test_no_pytorch_download_when_doing_safetensors(self): def test_no_safetensors_download_when_doing_pytorch(self): use_safetensors = False - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/diffusers-stable-diffusion-tiny-all", cache_dir=tmpdirname, @@ -1632,7 +1632,7 @@ def test_optional_components(self): assert sd.config.requires_safety_checker is True - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that passing None works @@ -1644,7 +1644,7 @@ def test_optional_components(self): assert sd.config.safety_checker == (None, None) assert sd.config.feature_extractor == (None, None) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that loading previous None works @@ -1686,7 +1686,7 @@ def test_optional_components(self): assert sd.config.safety_checker == (None, None) assert sd.config.feature_extractor == (None, None) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that partially loading works @@ -1708,7 +1708,7 @@ def test_optional_components(self): assert sd.config.safety_checker != (None, None) assert sd.config.feature_extractor != (None, None) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) sd = StableDiffusionPipeline.from_pretrained(tmpdirname, feature_extractor=self.dummy_extractor) @@ -1722,7 +1722,7 @@ def test_name_or_path(self): assert sd.name_or_path == model_path - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) sd = DiffusionPipeline.from_pretrained(tmpdirname) @@ -1825,7 +1825,7 @@ def test_pipe_same_device_id_offload(self): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_load_dduf_from_hub(self, dtype): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "DDUF/tiny-flux-dev-pipe-dduf", dduf_file="fluxpipeline.dduf", cache_dir=tmpdir, torch_dtype=dtype ).to(torch_device) @@ -1843,7 +1843,7 @@ def test_load_dduf_from_hub(self, dtype): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_load_dduf_from_hub_local_files_only(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "DDUF/tiny-flux-dev-pipe-dduf", dduf_file="fluxpipeline.dduf", cache_dir=tmpdir ).to(torch_device) @@ -1883,7 +1883,7 @@ def test_wrong_model(self): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_dduf_load_sharded_checkpoint_diffusion_model(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-flux-dev-pipe-sharded-checkpoint-DDUF", dduf_file="tiny-flux-dev-pipe-sharded-checkpoint.dduf", @@ -1919,7 +1919,7 @@ def tearDown(self): def test_smart_download(self): model_id = "hf-internal-testing/unet-pipeline-dummy" - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = DiffusionPipeline.from_pretrained(model_id, cache_dir=tmpdirname, force_download=True) local_repo_name = "--".join(["models"] + model_id.split("/")) snapshot_dir = os.path.join(tmpdirname, local_repo_name, "snapshots") @@ -1941,7 +1941,7 @@ def test_smart_download(self): def test_warning_unused_kwargs(self): model_id = "hf-internal-testing/unet-pipeline-dummy" logger = logging.get_logger("diffusers.pipelines") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with CaptureLogger(logger) as cap_logger: DiffusionPipeline.from_pretrained( model_id, @@ -1972,7 +1972,7 @@ def test_from_save_pretrained(self): ddpm.to(torch_device) ddpm.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ddpm.save_pretrained(tmpdirname) new_ddpm = DDPMPipeline.from_pretrained(tmpdirname) new_ddpm.to(torch_device) @@ -2069,14 +2069,14 @@ def test_from_flax_from_pt(self): from diffusers import FlaxStableDiffusionPipeline - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe_pt.save_pretrained(tmpdirname) pipe_flax, params = FlaxStableDiffusionPipeline.from_pretrained( tmpdirname, safety_checker=None, from_pt=True ) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe_flax.save_pretrained(tmpdirname, params=params) pipe_pt_2 = StableDiffusionPipeline.from_pretrained(tmpdirname, safety_checker=None, from_flax=True) pipe_pt_2.to(torch_device) @@ -2282,7 +2282,7 @@ def check_pipeline_hotswap(self, do_compile, rank0, rank1, target_modules0, targ assert not (output0_before == 0).all() assert not (output1_before == 0).all() - with tempfile.TemporaryDirectory() as tmp_dirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints lora0_state_dicts = self.get_lora_state_dicts({"unet": pipeline.unet}, adapter_name="adapter0") StableDiffusionPipeline.save_lora_weights( @@ -2413,7 +2413,7 @@ def test_hotswap_component_not_supported_raises(self): pipeline.text_encoder.add_adapter(lora_config0, adapter_name="adapter0") pipeline.text_encoder.add_adapter(lora_config1, adapter_name="adapter1") - with tempfile.TemporaryDirectory() as tmp_dirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints lora0_state_dicts = self.get_lora_state_dicts( {"text_encoder": pipeline.text_encoder}, adapter_name="adapter0" diff --git a/tests/pipelines/test_pipelines_common.py b/tests/pipelines/test_pipelines_common.py index d3e39e363f91..1037f252b564 100644 --- a/tests/pipelines/test_pipelines_common.py +++ b/tests/pipelines/test_pipelines_common.py @@ -1136,7 +1136,7 @@ def test_save_load_local(self, expected_max_difference=5e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: @@ -1401,7 +1401,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) for component in pipe_loaded.components.values(): @@ -1444,7 +1444,7 @@ def test_save_load_optional_components(self, expected_max_difference=1e-4): torch.manual_seed(0) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): @@ -1975,7 +1975,7 @@ def test_serialization_with_variants(self): ] variant = "fp16" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, variant=variant, safe_serialization=False) with open(f"{tmpdir}/model_index.json", "r") as f: @@ -1999,7 +1999,7 @@ def is_nan(tensor): has_nan = torch.isnan(tensor).any() return has_nan - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, variant=variant, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, variant=variant) @@ -2026,7 +2026,7 @@ def test_loading_with_incorrect_variants_raises_error(self): pipe = self.pipeline_class(**components) variant = "fp16" - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Don't save with variants. pipe.save_pretrained(tmpdir, safe_serialization=False) @@ -2181,7 +2181,7 @@ def test_save_load_dduf(self, atol=1e-4, rtol=1e-4): pipeline_out = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dduf_filename = os.path.join(tmpdir, f"{pipe.__class__.__name__.lower()}.dduf") pipe.save_pretrained(tmpdir, safe_serialization=True) export_folder_as_dduf(dduf_filename, folder_path=tmpdir) @@ -2355,7 +2355,7 @@ def get_pipeline_components(self): ) text_encoder = CLIPTextModel(text_encoder_config) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dummy_vocab = {"<|startoftext|>": 0, "<|endoftext|>": 1, "!": 2} vocab_path = os.path.join(tmpdir, "vocab.json") with open(vocab_path, "w") as f: @@ -2392,7 +2392,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipeline.save_pretrained(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) new_model = UNet2DConditionModel.from_pretrained(f"{USER}/{self.repo_id}", subfolder="unet") @@ -2416,7 +2416,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipeline.save_pretrained(tmp_dir, push_to_hub=True, token=TOKEN, repo_id=self.org_repo_id) new_model = UNet2DConditionModel.from_pretrained(self.org_repo_id, subfolder="unet") diff --git a/tests/pipelines/test_pipelines_flax.py b/tests/pipelines/test_pipelines_flax.py index efd3da4c6127..dc55b236c2b6 100644 --- a/tests/pipelines/test_pipelines_flax.py +++ b/tests/pipelines/test_pipelines_flax.py @@ -35,7 +35,7 @@ @require_flax class DownloadTests(unittest.TestCase): def test_download_only_pytorch(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights _ = FlaxDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname diff --git a/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py b/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py index db24767b60fc..49d5b6e612d3 100644 --- a/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py +++ b/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py @@ -299,7 +299,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(self.generator_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) pipe_loaded.to(torch_device) diff --git a/tests/quantization/bnb/test_4bit.py b/tests/quantization/bnb/test_4bit.py index 096ee4c34448..7a61e42452c7 100644 --- a/tests/quantization/bnb/test_4bit.py +++ b/tests/quantization/bnb/test_4bit.py @@ -347,7 +347,7 @@ def test_bnb_4bit_errors_loading_incorrect_state_dict(self): r""" Test if loading with an incorrect state dict raises an error. """ - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: nf4_config = BitsAndBytesConfig(load_in_4bit=True) model_4bit = SD3Transformer2DModel.from_pretrained( self.model_name, subfolder="transformer", quantization_config=nf4_config, device_map=torch_device @@ -778,7 +778,7 @@ def test_serialization(self, quant_type="nf4", double_quant=True, safe_serializa device_map=torch_device, ) self.assertTrue("_pre_quantization_dtype" in model_0.config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model_0.save_pretrained(tmpdirname, safe_serialization=safe_serialization) config = SD3Transformer2DModel.load_config(tmpdirname) diff --git a/tests/quantization/bnb/test_mixed_int8.py b/tests/quantization/bnb/test_mixed_int8.py index 1049bfecbaab..b3c4e70f9385 100644 --- a/tests/quantization/bnb/test_mixed_int8.py +++ b/tests/quantization/bnb/test_mixed_int8.py @@ -718,7 +718,7 @@ def test_serialization(self): Test whether it is possible to serialize a model in 8-bit. Uses most typical params as default. """ self.assertTrue("_pre_quantization_dtype" in self.model_0.config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: self.model_0.save_pretrained(tmpdirname) config = SD3Transformer2DModel.load_config(tmpdirname) @@ -749,7 +749,7 @@ def test_serialization(self): self.assertTrue(torch.equal(out_0, out_1)) def test_serialization_sharded(self): - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: self.model_0.save_pretrained(tmpdirname, max_shard_size="200MB") config = SD3Transformer2DModel.load_config(tmpdirname) diff --git a/tests/quantization/quanto/test_quanto.py b/tests/quantization/quanto/test_quanto.py index 9eb6958d2183..0e1b44109275 100644 --- a/tests/quantization/quanto/test_quanto.py +++ b/tests/quantization/quanto/test_quanto.py @@ -143,7 +143,7 @@ def test_serialization(self): with torch.no_grad(): model_output = model(**inputs) - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir) saved_model = self.model_cls.from_pretrained( tmp_dir, diff --git a/tests/quantization/torchao/test_torchao.py b/tests/quantization/torchao/test_torchao.py index 0e671307dd18..1a3447c6a47d 100644 --- a/tests/quantization/torchao/test_torchao.py +++ b/tests/quantization/torchao/test_torchao.py @@ -316,7 +316,7 @@ def test_device_map(self): expected_slice = expected_slice_auto else: expected_slice = expected_slice_offload - with tempfile.TemporaryDirectory() as offload_folder: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: quantization_config = TorchAoConfig("int4_weight_only", group_size=64) quantized_model = FluxTransformer2DModel.from_pretrained( "hf-internal-testing/tiny-flux-pipe", @@ -340,7 +340,7 @@ def test_device_map(self): output_slice = output.flatten()[-9:].detach().float().cpu().numpy() self.assertTrue(numpy_cosine_similarity_distance(output_slice, expected_slice) < 1e-3) - with tempfile.TemporaryDirectory() as offload_folder: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: quantization_config = TorchAoConfig("int4_weight_only", group_size=64) quantized_model = FluxTransformer2DModel.from_pretrained( "hf-internal-testing/tiny-flux-sharded", @@ -576,7 +576,7 @@ def _test_original_model_expected_slice(self, quant_method, quant_method_kwargs, def _check_serialization_expected_slice(self, quant_method, quant_method_kwargs, expected_slice, device): quantized_model = self.get_dummy_model(quant_method, quant_method_kwargs, device) - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: quantized_model.save_pretrained(tmp_dir, safe_serialization=False) loaded_quantized_model = FluxTransformer2DModel.from_pretrained( tmp_dir, torch_dtype=torch.bfloat16, use_safetensors=False @@ -728,7 +728,7 @@ def test_serialization_int8wo(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0].flatten()[:128] - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipe.transformer.save_pretrained(tmp_dir, safe_serialization=False) pipe.remove_all_hooks() del pipe.transformer diff --git a/tests/schedulers/test_scheduler_deis.py b/tests/schedulers/test_scheduler_deis.py index 048bde51c366..c6948f55daba 100644 --- a/tests/schedulers/test_scheduler_deis.py +++ b/tests/schedulers/test_scheduler_deis.py @@ -43,7 +43,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -77,7 +77,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_multi.py b/tests/schedulers/test_scheduler_dpm_multi.py index 55b3202ad0be..69337cf76536 100644 --- a/tests/schedulers/test_scheduler_dpm_multi.py +++ b/tests/schedulers/test_scheduler_dpm_multi.py @@ -53,7 +53,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -87,7 +87,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_multi_inverse.py b/tests/schedulers/test_scheduler_dpm_multi_inverse.py index 0eced957190c..24fc41a0907f 100644 --- a/tests/schedulers/test_scheduler_dpm_multi_inverse.py +++ b/tests/schedulers/test_scheduler_dpm_multi_inverse.py @@ -45,7 +45,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -78,7 +78,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_single.py b/tests/schedulers/test_scheduler_dpm_single.py index 7cbaa5cc5e8d..72d4680ea4da 100644 --- a/tests/schedulers/test_scheduler_dpm_single.py +++ b/tests/schedulers/test_scheduler_dpm_single.py @@ -51,7 +51,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -85,7 +85,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py b/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py index e97d64ec5f1d..0507ee9883bd 100644 --- a/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py +++ b/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py @@ -46,7 +46,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -80,7 +80,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_edm_euler.py b/tests/schedulers/test_scheduler_edm_euler.py index acac4b1f4cae..a41e31aeda87 100644 --- a/tests/schedulers/test_scheduler_edm_euler.py +++ b/tests/schedulers/test_scheduler_edm_euler.py @@ -89,7 +89,7 @@ def test_from_save_pretrained(self): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_flax.py b/tests/schedulers/test_scheduler_flax.py index 8ccb5f6594a5..08ded9a26e08 100644 --- a/tests/schedulers/test_scheduler_flax.py +++ b/tests/schedulers/test_scheduler_flax.py @@ -82,7 +82,7 @@ def check_over_configs(self, time_step=0, **config): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -111,7 +111,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -139,7 +139,7 @@ def test_from_save_pretrained(self): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -394,7 +394,7 @@ def check_over_configs(self, time_step=0, **config): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -422,7 +422,7 @@ def test_from_save_pretrained(self): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -451,7 +451,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -658,7 +658,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals state = state.replace(ets=dummy_past_residuals[:]) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) new_state = new_scheduler.set_timesteps(new_state, num_inference_steps, shape=sample.shape) @@ -746,7 +746,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_ipndm.py b/tests/schedulers/test_scheduler_ipndm.py index ac7973c58295..99b8c7cad020 100644 --- a/tests/schedulers/test_scheduler_ipndm.py +++ b/tests/schedulers/test_scheduler_ipndm.py @@ -34,7 +34,7 @@ def check_over_configs(self, time_step=0, **config): if time_step is None: time_step = scheduler.timesteps[len(scheduler.timesteps) // 2] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -73,7 +73,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): if time_step is None: time_step = scheduler.timesteps[len(scheduler.timesteps) // 2] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_lcm.py b/tests/schedulers/test_scheduler_lcm.py index f3f6e9ba5837..ec7e4bcc7457 100644 --- a/tests/schedulers/test_scheduler_lcm.py +++ b/tests/schedulers/test_scheduler_lcm.py @@ -118,7 +118,7 @@ def test_from_save_pretrained(self): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_pndm.py b/tests/schedulers/test_scheduler_pndm.py index 13c690468222..a88e947ded83 100644 --- a/tests/schedulers/test_scheduler_pndm.py +++ b/tests/schedulers/test_scheduler_pndm.py @@ -37,7 +37,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -73,7 +73,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_score_sde_ve.py b/tests/schedulers/test_scheduler_score_sde_ve.py index 08c30f9b1e0c..f4982ca2a550 100644 --- a/tests/schedulers/test_scheduler_score_sde_ve.py +++ b/tests/schedulers/test_scheduler_score_sde_ve.py @@ -66,7 +66,7 @@ def check_over_configs(self, time_step=0, **config): scheduler_config = self.get_scheduler_config(**config) scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -97,7 +97,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_unipc.py b/tests/schedulers/test_scheduler_unipc.py index 197c831cb015..0fc452436eb1 100644 --- a/tests/schedulers/test_scheduler_unipc.py +++ b/tests/schedulers/test_scheduler_unipc.py @@ -44,7 +44,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -74,7 +74,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_schedulers.py b/tests/schedulers/test_schedulers.py index 42ca1bc54155..157432344497 100755 --- a/tests/schedulers/test_schedulers.py +++ b/tests/schedulers/test_schedulers.py @@ -106,7 +106,7 @@ def test_save_load_from_different_config(self): setattr(diffusers, "SchedulerObject", SchedulerObject) logger = logging.get_logger("diffusers.configuration_utils") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) with CaptureLogger(logger) as cap_logger_1: config = SchedulerObject2.load_config(tmpdirname) @@ -152,7 +152,7 @@ def test_save_load_compatible_schedulers(self): setattr(diffusers, "SchedulerObject2", SchedulerObject2) logger = logging.get_logger("diffusers.configuration_utils") - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) # now save a config parameter that is expected by another class, but not origin class @@ -191,7 +191,7 @@ def test_save_load_from_different_config_comp_schedulers(self): logger = logging.get_logger("diffusers.configuration_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) with CaptureLogger(logger) as cap_logger_1: @@ -398,7 +398,7 @@ def check_over_configs(self, time_step=0, **config): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -451,7 +451,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -497,7 +497,7 @@ def test_from_save_pretrained(self): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -547,7 +547,7 @@ def test_from_pretrained(self): scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_pretrained(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -756,7 +756,7 @@ def test_trained_betas(self): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config, trained_betas=np.array([0.1, 0.3])) - with tempfile.TemporaryDirectory() as tmpdirname: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_pretrained(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -830,7 +830,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_config - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: scheduler.save_config(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) scheduler_loaded = DDIMScheduler.from_pretrained(f"{USER}/{self.repo_id}") @@ -857,7 +857,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_config - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: scheduler.save_config(tmp_dir, repo_id=self.org_repo_id, push_to_hub=True, token=TOKEN) scheduler_loaded = DDIMScheduler.from_pretrained(self.org_repo_id) diff --git a/tests/single_file/single_file_testing_utils.py b/tests/single_file/single_file_testing_utils.py index 4e7bc0af6842..2fcddcadfe12 100644 --- a/tests/single_file/single_file_testing_utils.py +++ b/tests/single_file/single_file_testing_utils.py @@ -100,7 +100,7 @@ def test_single_file_components(self, pipe=None, single_file_pipe=None): def test_single_file_components_local_files_only(self, pipe=None, single_file_pipe=None): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -140,7 +140,7 @@ def test_single_file_components_with_original_config_local_files_only( # we just pass it in here otherwise this test will fail upcast_attention = pipe.unet.config.upcast_attention - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -193,7 +193,7 @@ def test_single_file_components_with_diffusers_config_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) @@ -288,7 +288,7 @@ def test_single_file_components_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -329,7 +329,7 @@ def test_single_file_components_with_original_config_local_files_only( # we just pass it in here otherwise this test will fail upcast_attention = pipe.unet.config.upcast_attention - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -366,7 +366,7 @@ def test_single_file_components_with_diffusers_config_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py index 7589b48028c2..7195284035ea 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py @@ -111,7 +111,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, controlnet=controlnet) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) @@ -139,7 +139,7 @@ def test_single_file_components_with_original_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) @@ -172,7 +172,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py index 1555831db6db..97065902147d 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py @@ -105,7 +105,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None, controlnet=controlnet) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -136,7 +136,7 @@ def test_single_file_components_with_original_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -173,7 +173,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_single_file.py index 2c1e414e5e36..3daeca10e143 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_single_file.py @@ -102,7 +102,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, controlnet=controlnet) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -130,7 +130,7 @@ def test_single_file_components_with_original_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -161,7 +161,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_single_file.py b/tests/single_file/test_stable_diffusion_single_file.py index 78baeb94929c..aa7d45821ecf 100644 --- a/tests/single_file/test_stable_diffusion_single_file.py +++ b/tests/single_file/test_stable_diffusion_single_file.py @@ -64,7 +64,7 @@ def test_single_file_format_inference_is_same_as_pretrained(self): super().test_single_file_format_inference_is_same_as_pretrained(expected_max_diff=1e-3) def test_single_file_legacy_scheduler_loading(self): - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py b/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py index fb5f8725b86e..1680121e7f2f 100644 --- a/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py +++ b/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py @@ -120,7 +120,7 @@ def test_single_file_components_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -152,7 +152,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) @@ -190,7 +190,7 @@ def test_single_file_components_with_original_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py b/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py index 6d8c4369e1e1..0ff1aa27d35e 100644 --- a/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py +++ b/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py @@ -113,7 +113,7 @@ def test_single_file_components_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -152,7 +152,7 @@ def test_single_file_components_with_original_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -184,7 +184,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory() as tmpdir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/utils/update_metadata.py b/utils/update_metadata.py index a97e65801c5f..012f75e16a28 100644 --- a/utils/update_metadata.py +++ b/utils/update_metadata.py @@ -91,7 +91,7 @@ def update_metadata(commit_sha: str): with open(hub_pipeline_tags_json) as f: hub_pipeline_tags_json = f.read() - with tempfile.TemporaryDirectory() as tmp_dir: + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipelines_dataset.to_json(os.path.join(tmp_dir, PIPELINE_TAG_JSON)) with open(os.path.join(tmp_dir, PIPELINE_TAG_JSON)) as f: From 53f8ef59dc337355c8d32e06c4203da778fc508a Mon Sep 17 00:00:00 2001 From: hlky Date: Wed, 9 Apr 2025 09:50:01 +0100 Subject: [PATCH 2/4] Use our own TemporaryDirectory --- .../test_dreambooth_lora_flux_advanced.py | 17 +-- examples/community/sde_drag.py | 4 +- .../consistency_distillation/test_lcm_lora.py | 7 +- examples/controlnet/test_controlnet.py | 15 +-- .../custom_diffusion/test_custom_diffusion.py | 9 +- examples/dreambooth/test_dreambooth.py | 12 +- examples/dreambooth/test_dreambooth_flux.py | 10 +- examples/dreambooth/test_dreambooth_lora.py | 25 ++-- .../dreambooth/test_dreambooth_lora_edm.py | 7 +- .../dreambooth/test_dreambooth_lora_flux.py | 15 +-- .../test_dreambooth_lora_lumina2.py | 13 +- .../dreambooth/test_dreambooth_lora_sana.py | 13 +- .../dreambooth/test_dreambooth_lora_sd3.py | 17 +-- examples/dreambooth/test_dreambooth_sd3.py | 10 +- .../instruct_pix2pix/test_instruct_pix2pix.py | 7 +- examples/t2i_adapter/test_t2i_adapter.py | 5 +- examples/text_to_image/test_text_to_image.py | 14 +-- .../text_to_image/test_text_to_image_lora.py | 14 +-- .../test_textual_inversion.py | 9 +- .../test_textual_inversion_sdxl.py | 9 +- .../test_unconditional.py | 9 +- examples/vqgan/test_vqgan.py | 13 +- .../pipelines/transformers_loading_utils.py | 7 +- src/diffusers/utils/hub_utils.py | 5 +- src/diffusers/utils/testing_utils.py | 84 +++++++++++++ tests/lora/test_deprecated_utilities.py | 4 +- tests/lora/test_lora_layers_cogview4.py | 11 +- tests/lora/test_lora_layers_flux.py | 8 +- tests/lora/utils.py | 18 +-- tests/models/test_attention_processor.py | 5 +- tests/models/test_modeling_common.py | 46 +++---- .../unets/test_models_unet_2d_condition.py | 8 +- tests/models/unets/test_models_unet_motion.py | 8 +- tests/others/test_config.py | 7 +- tests/others/test_ema.py | 11 +- tests/pipelines/allegro/test_allegro.py | 4 +- tests/pipelines/controlnet/test_controlnet.py | 6 +- .../controlnet/test_controlnet_img2img.py | 4 +- .../controlnet/test_controlnet_inpaint.py | 4 +- tests/pipelines/deepfloyd_if/__init__.py | 6 +- .../pipelines/hunyuandit/test_hunyuan_dit.py | 4 +- tests/pipelines/latte/test_latte.py | 4 +- tests/pipelines/pag/test_pag_hunyuan_dit.py | 5 +- tests/pipelines/pag/test_pag_pixart_sigma.py | 4 +- tests/pipelines/pixart_alpha/test_pixart.py | 4 +- tests/pipelines/pixart_sigma/test_pixart.py | 4 +- .../test_semantic_diffusion.py | 4 +- .../test_onnx_stable_diffusion.py | 11 +- .../stable_diffusion/test_stable_diffusion.py | 4 +- .../test_stable_diffusion_depth.py | 6 +- .../test_stable_diffusion_diffedit.py | 4 +- .../test_stable_diffusion_upscale.py | 4 +- .../test_safe_diffusion.py | 12 +- .../test_stable_diffusion_xl.py | 4 +- .../test_stable_video_diffusion.py | 8 +- tests/pipelines/test_pipelines.py | 112 +++++++++--------- tests/pipelines/test_pipelines_common.py | 24 ++-- tests/pipelines/test_pipelines_flax.py | 5 +- .../test_text_to_video_zero_sdxl.py | 4 +- tests/quantization/bnb/test_4bit.py | 6 +- tests/quantization/bnb/test_mixed_int8.py | 6 +- tests/quantization/quanto/test_quanto.py | 4 +- tests/quantization/torchao/test_torchao.py | 10 +- tests/schedulers/test_scheduler_deis.py | 6 +- tests/schedulers/test_scheduler_dpm_multi.py | 6 +- .../test_scheduler_dpm_multi_inverse.py | 7 +- tests/schedulers/test_scheduler_dpm_single.py | 6 +- .../test_scheduler_edm_dpmsolver_multistep.py | 6 +- tests/schedulers/test_scheduler_edm_euler.py | 4 +- tests/schedulers/test_scheduler_flax.py | 19 ++- tests/schedulers/test_scheduler_ipndm.py | 6 +- tests/schedulers/test_scheduler_lcm.py | 5 +- tests/schedulers/test_scheduler_pndm.py | 6 +- .../schedulers/test_scheduler_score_sde_ve.py | 6 +- tests/schedulers/test_scheduler_unipc.py | 7 +- tests/schedulers/test_schedulers.py | 23 ++-- .../single_file/single_file_testing_utils.py | 14 +-- ...iffusion_controlnet_img2img_single_file.py | 8 +- ...iffusion_controlnet_inpaint_single_file.py | 8 +- ...stable_diffusion_controlnet_single_file.py | 8 +- .../test_stable_diffusion_single_file.py | 4 +- ...stable_diffusion_xl_adapter_single_file.py | 8 +- ...ble_diffusion_xl_controlnet_single_file.py | 8 +- utils/update_metadata.py | 4 +- 84 files changed, 514 insertions(+), 408 deletions(-) diff --git a/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py b/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py index d6116c297b15..cf35111b8b67 100644 --- a/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py +++ b/examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -39,7 +40,7 @@ class DreamBoothLoRAFluxAdvanced(ExamplesTestsAccelerate): script_path = "examples/advanced_diffusion_training/train_dreambooth_lora_flux_advanced.py" def test_dreambooth_lora_flux(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -71,7 +72,7 @@ def test_dreambooth_lora_flux(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_flux(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -104,7 +105,7 @@ def test_dreambooth_lora_text_encoder_flux(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_pivotal_tuning_flux_clip(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -146,7 +147,7 @@ def test_dreambooth_lora_pivotal_tuning_flux_clip(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_pivotal_tuning_flux_clip_t5(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -189,7 +190,7 @@ def test_dreambooth_lora_pivotal_tuning_flux_clip_t5(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -222,7 +223,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -245,7 +246,7 @@ def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/community/sde_drag.py b/examples/community/sde_drag.py index 4ceee1e13a89..5a77026fc957 100644 --- a/examples/community/sde_drag.py +++ b/examples/community/sde_drag.py @@ -1,5 +1,4 @@ import math -import tempfile from typing import List, Optional import numpy as np @@ -21,6 +20,7 @@ SlicedAttnAddedKVProcessor, ) from diffusers.optimization import get_scheduler +from diffusers.utils.testing_utils import TemporaryDirectory class SdeDragPipeline(DiffusionPipeline): @@ -320,7 +320,7 @@ def train_lora(self, prompt, image, lora_step=100, lora_rank=16, generator=None) lr_scheduler.step() optimizer.zero_grad() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as save_lora_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as save_lora_dir: StableDiffusionLoraLoaderMixin.save_lora_weights( save_directory=save_lora_dir, unet_lora_layers=unet_lora_layers, diff --git a/examples/consistency_distillation/test_lcm_lora.py b/examples/consistency_distillation/test_lcm_lora.py index 17028842027c..be8bfba9530b 100644 --- a/examples/consistency_distillation/test_lcm_lora.py +++ b/examples/consistency_distillation/test_lcm_lora.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -34,7 +35,7 @@ class TextToImageLCM(ExamplesTestsAccelerate): def test_text_to_image_lcm_lora_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/consistency_distillation/train_lcm_distill_lora_sdxl.py --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -61,7 +62,7 @@ def test_text_to_image_lcm_lora_sdxl(self): self.assertTrue(is_lora) def test_text_to_image_lcm_lora_sdxl_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/consistency_distillation/train_lcm_distill_lora_sdxl.py --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/controlnet/test_controlnet.py b/examples/controlnet/test_controlnet.py index 0522e765f0b2..0ed2dd2b3caf 100644 --- a/examples/controlnet/test_controlnet.py +++ b/examples/controlnet/test_controlnet.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class ControlNet(ExamplesTestsAccelerate): def test_controlnet_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -55,7 +56,7 @@ def test_controlnet_checkpointing_checkpoints_total_limit(self): ) def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -98,7 +99,7 @@ def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_check class ControlNetSDXL(ExamplesTestsAccelerate): def test_controlnet_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sdxl.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -119,7 +120,7 @@ def test_controlnet_sdxl(self): class ControlNetSD3(ExamplesTestsAccelerate): def test_controlnet_sd3(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sd3.py --pretrained_model_name_or_path=DavyMorgan/tiny-sd3-pipe @@ -140,7 +141,7 @@ def test_controlnet_sd3(self): class ControlNetSD35(ExamplesTestsAccelerate): def test_controlnet_sd3(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_sd3.py --pretrained_model_name_or_path=hf-internal-testing/tiny-sd35-pipe @@ -161,7 +162,7 @@ def test_controlnet_sd3(self): class ControlNetflux(ExamplesTestsAccelerate): def test_controlnet_flux(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/controlnet/train_controlnet_flux.py --pretrained_model_name_or_path=hf-internal-testing/tiny-flux-pipe diff --git a/examples/custom_diffusion/test_custom_diffusion.py b/examples/custom_diffusion/test_custom_diffusion.py index c47a12e8f21f..a19c6808c3c7 100644 --- a/examples/custom_diffusion/test_custom_diffusion.py +++ b/examples/custom_diffusion/test_custom_diffusion.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class CustomDiffusion(ExamplesTestsAccelerate): def test_custom_diffusion(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -57,7 +58,7 @@ def test_custom_diffusion(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, ".bin"))) def test_custom_diffusion_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -79,7 +80,7 @@ def test_custom_diffusion_checkpointing_checkpoints_total_limit(self): self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"}) def test_custom_diffusion_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/custom_diffusion/train_custom_diffusion.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/dreambooth/test_dreambooth.py b/examples/dreambooth/test_dreambooth.py index d3b67c4aa435..6f43aadad2f7 100644 --- a/examples/dreambooth/test_dreambooth.py +++ b/examples/dreambooth/test_dreambooth.py @@ -17,9 +17,9 @@ import os import shutil import sys -import tempfile from diffusers import DiffusionPipeline, UNet2DConditionModel +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -35,7 +35,7 @@ class DreamBooth(ExamplesTestsAccelerate): def test_dreambooth(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -58,7 +58,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_if(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path hf-internal-testing/tiny-if-pipe @@ -87,7 +87,7 @@ def test_dreambooth_checkpointing(self): instance_prompt = "photo" pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -163,7 +163,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -186,7 +186,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/dreambooth/test_dreambooth_flux.py b/examples/dreambooth/test_dreambooth_flux.py index 1fbf36575623..755ee6c3a4ad 100644 --- a/examples/dreambooth/test_dreambooth_flux.py +++ b/examples/dreambooth/test_dreambooth_flux.py @@ -17,9 +17,9 @@ import os import shutil import sys -import tempfile from diffusers import DiffusionPipeline, FluxTransformer2DModel +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -40,7 +40,7 @@ class DreamBoothFlux(ExamplesTestsAccelerate): script_path = "examples/dreambooth/train_dreambooth_flux.py" def test_dreambooth(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -63,7 +63,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -139,7 +139,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -162,7 +162,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora.py b/examples/dreambooth/test_dreambooth_lora.py index 7ba29009f95e..90969acbf6ab 100644 --- a/examples/dreambooth/test_dreambooth_lora.py +++ b/examples/dreambooth/test_dreambooth_lora.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -36,7 +37,7 @@ class DreamBoothLoRA(ExamplesTestsAccelerate): def test_dreambooth_lora(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -68,7 +69,7 @@ def test_dreambooth_lora(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_with_text_encoder(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -102,7 +103,7 @@ def test_dreambooth_lora_with_text_encoder(self): self.assertTrue(is_correct_naming) def test_dreambooth_lora_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -125,7 +126,7 @@ def test_dreambooth_lora_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -163,7 +164,7 @@ def test_dreambooth_lora_checkpointing_checkpoints_total_limit_removes_multiple_ self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-6", "checkpoint-8"}) def test_dreambooth_lora_if_model(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora.py --pretrained_model_name_or_path hf-internal-testing/tiny-if-pipe @@ -200,7 +201,7 @@ def test_dreambooth_lora_if_model(self): class DreamBoothLoRASDXL(ExamplesTestsAccelerate): def test_dreambooth_lora_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -232,7 +233,7 @@ def test_dreambooth_lora_sdxl(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_sdxl_with_text_encoder(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -268,7 +269,7 @@ def test_dreambooth_lora_sdxl_with_text_encoder(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_sdxl_custom_captions(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -289,7 +290,7 @@ def test_dreambooth_lora_sdxl_custom_captions(self): run_command(self._launch_args + test_args) def test_dreambooth_lora_sdxl_text_encoder_custom_captions(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -313,7 +314,7 @@ def test_dreambooth_lora_sdxl_text_encoder_custom_captions(self): def test_dreambooth_lora_sdxl_checkpointing_checkpoints_total_limit(self): pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path {pipeline_path} @@ -345,7 +346,7 @@ def test_dreambooth_lora_sdxl_checkpointing_checkpoints_total_limit(self): def test_dreambooth_lora_sdxl_text_encoder_checkpointing_checkpoints_total_limit(self): pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path {pipeline_path} diff --git a/examples/dreambooth/test_dreambooth_lora_edm.py b/examples/dreambooth/test_dreambooth_lora_edm.py index f873ff140e00..3abce155f619 100644 --- a/examples/dreambooth/test_dreambooth_lora_edm.py +++ b/examples/dreambooth/test_dreambooth_lora_edm.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -34,7 +35,7 @@ class DreamBoothLoRASDXLWithEDM(ExamplesTestsAccelerate): def test_dreambooth_lora_sdxl_with_edm(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -67,7 +68,7 @@ def test_dreambooth_lora_sdxl_with_edm(self): self.assertTrue(starts_with_unet) def test_dreambooth_lora_playground(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/dreambooth/train_dreambooth_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-playground-v2-5-pipe diff --git a/examples/dreambooth/test_dreambooth_lora_flux.py b/examples/dreambooth/test_dreambooth_lora_flux.py index 77f599e3376c..d8378c360ea1 100644 --- a/examples/dreambooth/test_dreambooth_lora_flux.py +++ b/examples/dreambooth/test_dreambooth_lora_flux.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -40,7 +41,7 @@ class DreamBoothLoRAFlux(ExamplesTestsAccelerate): transformer_layer_type = "single_transformer_blocks.0.attn.to_k" def test_dreambooth_lora_flux(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +73,7 @@ def test_dreambooth_lora_flux(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_flux(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -105,7 +106,7 @@ def test_dreambooth_lora_text_encoder_flux(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -138,7 +139,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -175,7 +176,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -198,7 +199,7 @@ def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_lumina2.py b/examples/dreambooth/test_dreambooth_lora_lumina2.py index 6da5f51f7c58..aec9f3e53192 100644 --- a/examples/dreambooth/test_dreambooth_lora_lumina2.py +++ b/examples/dreambooth/test_dreambooth_lora_lumina2.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -39,7 +40,7 @@ class DreamBoothLoRAlumina2(ExamplesTestsAccelerate): transformer_layer_type = "layers.0.attn.to_k" def test_dreambooth_lora_lumina2(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +73,7 @@ def test_dreambooth_lora_lumina2(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -106,7 +107,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -142,7 +143,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -166,7 +167,7 @@ def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_lumina2_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_sana.py b/examples/dreambooth/test_dreambooth_lora_sana.py index 685a49cac599..eb3b082bf6e0 100644 --- a/examples/dreambooth/test_dreambooth_lora_sana.py +++ b/examples/dreambooth/test_dreambooth_lora_sana.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -39,7 +40,7 @@ class DreamBoothLoRASANA(ExamplesTestsAccelerate): transformer_layer_type = "transformer_blocks.0.attn1.to_k" def test_dreambooth_lora_sana(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -72,7 +73,7 @@ def test_dreambooth_lora_sana(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -106,7 +107,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -142,7 +143,7 @@ def test_dreambooth_lora_layers(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -166,7 +167,7 @@ def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_sana_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_lora_sd3.py b/examples/dreambooth/test_dreambooth_lora_sd3.py index 400e651b14b8..8fb844d646a2 100644 --- a/examples/dreambooth/test_dreambooth_lora_sd3.py +++ b/examples/dreambooth/test_dreambooth_lora_sd3.py @@ -16,10 +16,11 @@ import logging import os import sys -import tempfile import safetensors +from diffusers.utils.testing_utils import TemporaryDirectory + sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 @@ -42,7 +43,7 @@ class DreamBoothLoRASD3(ExamplesTestsAccelerate): layer_type = "attn.to_k" def test_dreambooth_lora_sd3(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -74,7 +75,7 @@ def test_dreambooth_lora_sd3(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_text_encoder_sd3(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -107,7 +108,7 @@ def test_dreambooth_lora_text_encoder_sd3(self): self.assertTrue(starts_with_expected_prefix) def test_dreambooth_lora_latent_caching(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -140,7 +141,7 @@ def test_dreambooth_lora_latent_caching(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_block(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -176,7 +177,7 @@ def test_dreambooth_lora_block(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layer(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -208,7 +209,7 @@ def test_dreambooth_lora_layer(self): self.assertTrue(starts_with_transformer) def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -231,7 +232,7 @@ def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_lora_sd3_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/dreambooth/test_dreambooth_sd3.py b/examples/dreambooth/test_dreambooth_sd3.py index fd9b1d070b0b..0f2649446865 100644 --- a/examples/dreambooth/test_dreambooth_sd3.py +++ b/examples/dreambooth/test_dreambooth_sd3.py @@ -17,9 +17,9 @@ import os import shutil import sys -import tempfile from diffusers import DiffusionPipeline, SD3Transformer2DModel +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -40,7 +40,7 @@ class DreamBoothSD3(ExamplesTestsAccelerate): script_path = "examples/dreambooth/train_dreambooth_sd3.py" def test_dreambooth(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} @@ -63,7 +63,7 @@ def test_dreambooth(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_dreambooth_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -139,7 +139,7 @@ def test_dreambooth_checkpointing(self): self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6"))) def test_dreambooth_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} @@ -162,7 +162,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self): ) def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} diff --git a/examples/instruct_pix2pix/test_instruct_pix2pix.py b/examples/instruct_pix2pix/test_instruct_pix2pix.py index bff99e498b39..bca4d31707e1 100644 --- a/examples/instruct_pix2pix/test_instruct_pix2pix.py +++ b/examples/instruct_pix2pix/test_instruct_pix2pix.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class InstructPix2Pix(ExamplesTestsAccelerate): def test_instruct_pix2pix_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/instruct_pix2pix/train_instruct_pix2pix.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe @@ -55,7 +56,7 @@ def test_instruct_pix2pix_checkpointing_checkpoints_total_limit(self): ) def test_instruct_pix2pix_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/instruct_pix2pix/train_instruct_pix2pix.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/t2i_adapter/test_t2i_adapter.py b/examples/t2i_adapter/test_t2i_adapter.py index 1c17c920a016..50254fe21a9b 100644 --- a/examples/t2i_adapter/test_t2i_adapter.py +++ b/examples/t2i_adapter/test_t2i_adapter.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class T2IAdapter(ExamplesTestsAccelerate): def test_t2i_adapter_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/t2i_adapter/train_t2i_adapter_sdxl.py --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/text_to_image/test_text_to_image.py b/examples/text_to_image/test_text_to_image.py index c99b9fa6308e..793509bf3d20 100644 --- a/examples/text_to_image/test_text_to_image.py +++ b/examples/text_to_image/test_text_to_image.py @@ -18,9 +18,9 @@ import os import shutil import sys -import tempfile from diffusers import DiffusionPipeline, UNet2DConditionModel # noqa: E402 +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -36,7 +36,7 @@ class TextToImage(ExamplesTestsAccelerate): def test_text_to_image(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -63,7 +63,7 @@ def test_text_to_image_checkpointing(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -145,7 +145,7 @@ def test_text_to_image_checkpointing_use_ema(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -229,7 +229,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -268,7 +268,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit_removes_multiple_ch pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -341,7 +341,7 @@ def test_text_to_image_checkpointing_checkpoints_total_limit_removes_multiple_ch class TextToImageSDXL(ExamplesTestsAccelerate): def test_text_to_image_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe diff --git a/examples/text_to_image/test_text_to_image_lora.py b/examples/text_to_image/test_text_to_image_lora.py index 53c990fc69b0..4a05869d9d1d 100644 --- a/examples/text_to_image/test_text_to_image_lora.py +++ b/examples/text_to_image/test_text_to_image_lora.py @@ -17,11 +17,11 @@ import logging import os import sys -import tempfile import safetensors from diffusers import DiffusionPipeline # noqa: E402 +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -40,7 +40,7 @@ def test_text_to_image_lora_sdxl_checkpointing_checkpoints_total_limit(self): prompt = "a prompt" pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -77,7 +77,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit(self): pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 @@ -120,7 +120,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit_removes_multip pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe" prompt = "a prompt" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 # Should create checkpoints at steps 2, 4 @@ -201,7 +201,7 @@ def test_text_to_image_lora_checkpointing_checkpoints_total_limit_removes_multip class TextToImageLoRASDXL(ExamplesTestsAccelerate): def test_text_to_image_lora_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -227,7 +227,7 @@ def test_text_to_image_lora_sdxl(self): self.assertTrue(is_lora) def test_text_to_image_lora_sdxl_with_text_encoder(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/text_to_image/train_text_to_image_lora_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe @@ -265,7 +265,7 @@ def test_text_to_image_lora_sdxl_text_encoder_checkpointing_checkpoints_total_li prompt = "a prompt" pipeline_path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 # Should create checkpoints at steps 2, 4, 6 diff --git a/examples/textual_inversion/test_textual_inversion.py b/examples/textual_inversion/test_textual_inversion.py index 013479753604..e4aa2a08bb93 100644 --- a/examples/textual_inversion/test_textual_inversion.py +++ b/examples/textual_inversion/test_textual_inversion.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class TextualInversion(ExamplesTestsAccelerate): def test_textual_inversion(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -58,7 +59,7 @@ def test_textual_inversion(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.safetensors"))) def test_textual_inversion_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe @@ -90,7 +91,7 @@ def test_textual_inversion_checkpointing(self): ) def test_textual_inversion_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion.py --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe diff --git a/examples/textual_inversion/test_textual_inversion_sdxl.py b/examples/textual_inversion/test_textual_inversion_sdxl.py index 13c5b6fa5de9..8b5ef7655ab8 100644 --- a/examples/textual_inversion/test_textual_inversion_sdxl.py +++ b/examples/textual_inversion/test_textual_inversion_sdxl.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class TextualInversionSdxl(ExamplesTestsAccelerate): def test_textual_inversion_sdxl(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe @@ -58,7 +59,7 @@ def test_textual_inversion_sdxl(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.safetensors"))) def test_textual_inversion_sdxl_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe @@ -90,7 +91,7 @@ def test_textual_inversion_sdxl_checkpointing(self): ) def test_textual_inversion_sdxl_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/textual_inversion/textual_inversion_sdxl.py --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe diff --git a/examples/unconditional_image_generation/test_unconditional.py b/examples/unconditional_image_generation/test_unconditional.py index df7fc7668b98..7f60e08cce37 100644 --- a/examples/unconditional_image_generation/test_unconditional.py +++ b/examples/unconditional_image_generation/test_unconditional.py @@ -16,7 +16,8 @@ import logging import os import sys -import tempfile + +from diffusers.utils.testing_utils import TemporaryDirectory sys.path.append("..") @@ -32,7 +33,7 @@ class Unconditional(ExamplesTestsAccelerate): def test_train_unconditional(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: test_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data @@ -53,7 +54,7 @@ def test_train_unconditional(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) def test_unconditional_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: initial_run_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data @@ -80,7 +81,7 @@ def test_unconditional_checkpointing_checkpoints_total_limit(self): ) def test_unconditional_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: initial_run_args = f""" examples/unconditional_image_generation/train_unconditional.py --dataset_name hf-internal-testing/dummy_image_class_data diff --git a/examples/vqgan/test_vqgan.py b/examples/vqgan/test_vqgan.py index 2933d5ae7a22..a484383d25aa 100644 --- a/examples/vqgan/test_vqgan.py +++ b/examples/vqgan/test_vqgan.py @@ -19,12 +19,11 @@ import os import shutil import sys -import tempfile import torch from diffusers import VQModel -from diffusers.utils.testing_utils import require_timm +from diffusers.utils.testing_utils import TemporaryDirectory, require_timm sys.path.append("..") @@ -88,7 +87,7 @@ def get_vq_and_discriminator_configs(self, tmpdir): return vqmodel_config_path, discriminator_config_path def test_vqmodel(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) test_args = f""" examples/vqgan/train_vqgan.py @@ -115,7 +114,7 @@ def test_vqmodel(self): self.assertTrue(os.path.isfile(os.path.join(tmpdir, "vqmodel", "diffusion_pytorch_model.safetensors"))) def test_vqmodel_checkpointing(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 @@ -199,7 +198,7 @@ def test_vqmodel_checkpointing(self): ) def test_vqmodel_checkpointing_use_ema(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 @@ -283,7 +282,7 @@ def test_vqmodel_checkpointing_use_ema(self): ) def test_vqmodel_checkpointing_checkpoints_total_limit(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 6, checkpointing_steps == 2, checkpoints_total_limit == 2 @@ -321,7 +320,7 @@ def test_vqmodel_checkpointing_checkpoints_total_limit(self): self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"}) def test_vqmodel_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: vqmodel_config_path, discriminator_config_path = self.get_vq_and_discriminator_configs(tmpdir) # Run training script with checkpointing # max_train_steps == 4, checkpointing_steps == 2 diff --git a/src/diffusers/pipelines/transformers_loading_utils.py b/src/diffusers/pipelines/transformers_loading_utils.py index 21687afbe4cc..48c2bdfe1ac9 100644 --- a/src/diffusers/pipelines/transformers_loading_utils.py +++ b/src/diffusers/pipelines/transformers_loading_utils.py @@ -14,12 +14,13 @@ # limitations under the License. import contextlib import os -import tempfile from typing import TYPE_CHECKING, Dict from huggingface_hub import DDUFEntry from tqdm import tqdm +from diffusers.utils.testing_utils import TemporaryDirectory + from ..utils import is_safetensors_available, is_transformers_available, is_transformers_version @@ -44,7 +45,7 @@ def _load_tokenizer_from_dduf( files. There is an extra cost of extracting the files, but of limited impact as the tokenizer files are usually small-ish. """ - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: for entry_name, entry in dduf_entries.items(): if entry_name.startswith(name + "/"): tmp_entry_path = os.path.join(tmp_dir, *entry_name.split("/")) @@ -91,7 +92,7 @@ def _load_transformers_model_from_dduf( "You can install it with: `pip install --upgrade transformers`" ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: from transformers import AutoConfig, GenerationConfig tmp_config_file = os.path.join(tmp_dir, "config.json") diff --git a/src/diffusers/utils/hub_utils.py b/src/diffusers/utils/hub_utils.py index 207ff4a7626a..7443ebdc06e9 100644 --- a/src/diffusers/utils/hub_utils.py +++ b/src/diffusers/utils/hub_utils.py @@ -18,7 +18,6 @@ import os import re import sys -import tempfile import warnings from pathlib import Path from typing import Dict, List, Optional, Union @@ -46,6 +45,8 @@ from packaging import version from requests import HTTPError +from diffusers.utils.testing_utils import TemporaryDirectory + from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, @@ -542,7 +543,7 @@ def push_to_hub( if "Scheduler" not in self.__class__.__name__: save_kwargs.update({"variant": variant}) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: self.save_pretrained(tmpdir, **save_kwargs) # Update model card if needed: diff --git a/src/diffusers/utils/testing_utils.py b/src/diffusers/utils/testing_utils.py index 51e7e640fb02..283174dd66c8 100644 --- a/src/diffusers/utils/testing_utils.py +++ b/src/diffusers/utils/testing_utils.py @@ -8,12 +8,15 @@ import os import random import re +import shutil import struct import sys import tempfile import time import unittest import urllib.parse +import warnings +import weakref from collections import UserDict from contextlib import contextmanager from io import BytesIO, StringIO @@ -1377,3 +1380,84 @@ def find_expectation(self, key: DeviceProperties = (None, None)) -> Any: def __repr__(self): return f"{self.data}" + + +# This is for Python 3.8/3.9 support of ignore_cleanup_errors + +GenericAlias = List[int] + + +class TemporaryDirectory: + """Create and return a temporary directory. This has the same + behavior as mkdtemp but can be used as a context manager. For + example: + + with TemporaryDirectory() as tmpdir: + ... + + Upon exiting the context, the directory and everything contained + in it are removed. + """ + + def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False): + self.name = tempfile.mkdtemp(suffix, prefix, dir) + self._ignore_cleanup_errors = ignore_cleanup_errors + self._finalizer = weakref.finalize( + self, + self._cleanup, + self.name, + warn_message="Implicitly cleaning up {!r}".format(self), + ignore_errors=self._ignore_cleanup_errors, + ) + + @classmethod + def _rmtree(cls, name, ignore_errors=False): + def onerror(func, path, exc_info): + if issubclass(exc_info[0], PermissionError): + + def resetperms(path): + try: + os.chflags(path, 0) + except AttributeError: + pass + os.chmod(path, 0o700) + + try: + if path != name: + resetperms(os.path.dirname(path)) + resetperms(path) + + try: + os.unlink(path) + # PermissionError is raised on FreeBSD for directories + except (IsADirectoryError, PermissionError): + cls._rmtree(path, ignore_errors=ignore_errors) + except FileNotFoundError: + pass + elif issubclass(exc_info[0], FileNotFoundError): + pass + else: + if not ignore_errors: + raise + + shutil.rmtree(name, onerror=onerror) + + @classmethod + def _cleanup(cls, name, warn_message, ignore_errors=False): + cls._rmtree(name, ignore_errors=ignore_errors) + warnings.warn(warn_message, ResourceWarning) + + def __repr__(self): + return "<{} {!r}>".format(self.__class__.__name__, self.name) + + def __enter__(self): + return self.name + + def __exit__(self, exc, value, tb): + self.cleanup() + + def cleanup(self): + if self._finalizer.detach() or os.path.exists(self.name): + self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors) + + __class_getitem__ = classmethod(GenericAlias) diff --git a/tests/lora/test_deprecated_utilities.py b/tests/lora/test_deprecated_utilities.py index 166273946e17..a29d715cae28 100644 --- a/tests/lora/test_deprecated_utilities.py +++ b/tests/lora/test_deprecated_utilities.py @@ -1,10 +1,10 @@ import os -import tempfile import unittest import torch from diffusers.loaders.lora_base import LoraBaseMixin +from diffusers.utils.testing_utils import TemporaryDirectory class UtilityMethodDeprecationTests(unittest.TestCase): @@ -29,7 +29,7 @@ def test_fetch_state_dict_cls_method_raises_warning(self): assert "Using the `_fetch_state_dict()` method from" in warning_message def test_best_guess_weight_name_cls_method_raises_warning(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: state_dict = torch.nn.Linear(3, 3).state_dict() torch.save(state_dict, os.path.join(tmpdir, "pytorch_lora_weights.bin")) diff --git a/tests/lora/test_lora_layers_cogview4.py b/tests/lora/test_lora_layers_cogview4.py index d29d43b5b7fc..10559c898e00 100644 --- a/tests/lora/test_lora_layers_cogview4.py +++ b/tests/lora/test_lora_layers_cogview4.py @@ -13,7 +13,6 @@ # limitations under the License. import sys -import tempfile import unittest import numpy as np @@ -21,7 +20,13 @@ from transformers import AutoTokenizer, GlmModel from diffusers import AutoencoderKL, CogView4Pipeline, CogView4Transformer2DModel, FlowMatchEulerDiscreteScheduler -from diffusers.utils.testing_utils import floats_tensor, require_peft_backend, skip_mps, torch_device +from diffusers.utils.testing_utils import ( + TemporaryDirectory, + floats_tensor, + require_peft_backend, + skip_mps, + torch_device, +) sys.path.append(".") @@ -128,7 +133,7 @@ def test_simple_inference_save_pretrained(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_from_pretrained = self.pipeline_class.from_pretrained(tmpdirname) diff --git a/tests/lora/test_lora_layers_flux.py b/tests/lora/test_lora_layers_flux.py index 7237d8fb0b8a..e77a48c5ad8d 100644 --- a/tests/lora/test_lora_layers_flux.py +++ b/tests/lora/test_lora_layers_flux.py @@ -16,7 +16,6 @@ import gc import os import sys -import tempfile import unittest import numpy as np @@ -31,6 +30,7 @@ from diffusers.utils import load_image, logging from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, floats_tensor, is_peft_available, nightly, @@ -130,7 +130,7 @@ def test_with_alpha_in_state_dict(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0)).images - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) @@ -186,7 +186,7 @@ def test_lora_expansion_works_for_absent_keys(self): "LoRA should lead to different results.", ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) @@ -235,7 +235,7 @@ def test_lora_expansion_works_for_extra_keys(self): "LoRA should lead to different results.", ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: denoiser_state_dict = get_peft_model_state_dict(pipe.transformer) self.pipeline_class.save_lora_weights(tmpdirname, transformer_lora_layers=denoiser_state_dict) diff --git a/tests/lora/utils.py b/tests/lora/utils.py index 71f98fc2c20b..64e91f4b9766 100644 --- a/tests/lora/utils.py +++ b/tests/lora/utils.py @@ -15,7 +15,6 @@ import inspect import os import re -import tempfile import unittest from itertools import product @@ -33,6 +32,7 @@ from diffusers.utils.import_utils import is_peft_available from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, floats_tensor, is_torch_version, require_peft_backend, @@ -400,7 +400,7 @@ def test_low_cpu_mem_usage_with_loading(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -608,7 +608,7 @@ def test_simple_inference_with_text_lora_save_load(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) @@ -720,7 +720,7 @@ def test_simple_inference_save_pretrained(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_from_pretrained = self.pipeline_class.from_pretrained(tmpdirname) @@ -778,7 +778,7 @@ def test_simple_inference_with_text_denoiser_lora_save_load(self): images_lora = pipe(**inputs, generator=torch.manual_seed(0))[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -1838,7 +1838,7 @@ def test_missing_keys_warning(self): denoiser.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(denoiser), "Lora not correctly set in denoiser.") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -1875,7 +1875,7 @@ def test_unexpected_keys_warning(self): denoiser.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(denoiser), "Lora not correctly set in denoiser.") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -2047,7 +2047,7 @@ def test_set_adapters_match_attention_kwargs(self): "Lora + scale should match the output of `set_adapters()`.", ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( @@ -2310,7 +2310,7 @@ def check_module(denoiser): pipe(**inputs, generator=torch.manual_seed(0))[0] # 2. Test forward with load_lora_weights - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True) lora_state_dicts = self._get_lora_state_dicts(modules_to_save) self.pipeline_class.save_lora_weights( diff --git a/tests/models/test_attention_processor.py b/tests/models/test_attention_processor.py index 4edf28970456..455075b5e559 100644 --- a/tests/models/test_attention_processor.py +++ b/tests/models/test_attention_processor.py @@ -1,4 +1,3 @@ -import tempfile import unittest import numpy as np @@ -7,7 +6,7 @@ from diffusers import DiffusionPipeline from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor -from diffusers.utils.testing_utils import torch_device +from diffusers.utils.testing_utils import TemporaryDirectory, torch_device class AttnAddedKVProcessorTests(unittest.TestCase): @@ -114,7 +113,7 @@ def test_conversion_when_using_device_map(self): output_type="np", ).images - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # save the converted model pipe.save_pretrained(tmpdir) diff --git a/tests/models/test_modeling_common.py b/tests/models/test_modeling_common.py index 77ff00ce0128..508ae6a40a84 100644 --- a/tests/models/test_modeling_common.py +++ b/tests/models/test_modeling_common.py @@ -19,7 +19,6 @@ import json import os import re -import tempfile import traceback import unittest import unittest.mock as mock @@ -57,6 +56,7 @@ from diffusers.utils.hub_utils import _add_variant from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, floats_tensor, get_python_version, @@ -112,7 +112,7 @@ def _test_from_save_pretrained_dynamo(in_queue, out_queue, timeout): model.to(torch_device) model = torch.compile(model) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = model_class.from_pretrained(tmpdirname) new_model.to(torch_device) @@ -230,7 +230,7 @@ def load_model(path): with self.assertWarns(FutureWarning) as warning: if use_local: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = snapshot_download(repo_id=repo_id) _ = load_model(tmpdirname) else: @@ -288,7 +288,7 @@ def test_cached_files_are_used_when_no_internet(self): def test_one_request_upon_cached(self): use_safetensors = False - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: UNet2DConditionModel.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", @@ -317,7 +317,7 @@ def test_one_request_upon_cached(self): ), "We should call only `model_info` to check for commit hash and knowing if shard index is present." def test_weight_overwrite(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname, self.assertRaises( + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname, self.assertRaises( ValueError ) as error_context: UNet2DConditionModel.from_pretrained( @@ -330,7 +330,7 @@ def test_weight_overwrite(self): # make sure that error message states what keys are missing assert "Cannot load" in str(error_context.exception) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model = UNet2DConditionModel.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", subfolder="unet", @@ -450,7 +450,7 @@ def test_from_save_pretrained(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname) if hasattr(new_model, "set_default_attn_processor"): @@ -687,7 +687,7 @@ def test_from_save_pretrained_variant(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, variant="fp16", safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname, variant="fp16") if hasattr(new_model, "set_default_attn_processor"): @@ -742,7 +742,7 @@ def test_from_save_pretrained_dtype(self): for dtype in [torch.float32, torch.float16, torch.bfloat16]: if torch_device == "mps" and dtype == torch.bfloat16: continue - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.to(dtype) model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname, low_cpu_mem_usage=True, torch_dtype=dtype) @@ -819,7 +819,7 @@ def test_model_from_pretrained(self): # test if the model can be loaded from the config # and has all the expected shape - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) new_model = self.model_class.from_pretrained(tmpdirname) new_model.to(torch_device) @@ -1090,7 +1090,7 @@ def test_save_load_lora_adapter(self, use_dora=False): self.assertFalse(torch.allclose(output_no_lora, outputs_with_lora, atol=1e-4, rtol=1e-4)) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: model.save_lora_adapter(tmpdir) self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) @@ -1137,7 +1137,7 @@ def test_wrong_adapter_name_raises_error(self): model.add_adapter(denoiser_lora_config) self.assertTrue(check_if_lora_correctly_set(model), "LoRA layers not set correctly") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: wrong_name = "foo" with self.assertRaises(ValueError) as err_context: model.save_lora_adapter(tmpdir, adapter_name=wrong_name) @@ -1159,7 +1159,7 @@ def test_cpu_offload(self): model_size = compute_module_sizes(model)[""] # We test several splits of sizes to make sure it works. max_gpu_sizes = [int(p * model_size) for p in self.model_split_percents[1:]] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) for max_size in max_gpu_sizes: @@ -1191,7 +1191,7 @@ def test_disk_offload_without_safetensors(self): # Force disk offload by setting very small CPU memory max_memory = {0: max_size, "cpu": int(0.1 * max_size)} - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, safe_serialization=False) with self.assertRaises(ValueError): # This errors out because it's missing an offload folder @@ -1220,7 +1220,7 @@ def test_disk_offload_with_safetensors(self): base_output = model(**inputs_dict) model_size = compute_module_sizes(model)[""] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) max_size = int(self.model_split_percents[0] * model_size) @@ -1250,7 +1250,7 @@ def test_model_parallelism(self): model_size = compute_module_sizes(model)[""] # We test several splits of sizes to make sure it works. max_gpu_sizes = [int(p * model_size) for p in self.model_split_percents[1:]] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir) for max_size in max_gpu_sizes: @@ -1278,7 +1278,7 @@ def test_sharded_checkpoints(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, max_shard_size=f"{max_shard_size}KB") self.assertTrue(os.path.exists(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) @@ -1311,7 +1311,7 @@ def test_sharded_checkpoints_with_variant(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. variant = "fp16" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: # It doesn't matter if the actual model is in fp16 or not. Just adding the variant and # testing if loading works with the variant when the checkpoint is sharded should be # enough. @@ -1350,7 +1350,7 @@ def test_sharded_checkpoints_device_map(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained(tmp_dir, max_shard_size=f"{max_shard_size}KB") self.assertTrue(os.path.exists(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) @@ -1380,7 +1380,7 @@ def test_variant_sharded_ckpt_right_format(self): model_size = compute_module_persistent_sizes(model)[""] max_shard_size = int((model_size * 0.75) / (2**10)) # Convert to KB as these test models are small. variant = "fp16" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.cpu().save_pretrained( tmp_dir, variant=variant, max_shard_size=f"{max_shard_size}KB", safe_serialization=use_safe ) @@ -1607,7 +1607,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) new_model = UNet2DConditionModel.from_pretrained(f"{USER}/{self.repo_id}") @@ -1638,7 +1638,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir, push_to_hub=True, token=TOKEN, repo_id=self.org_repo_id) new_model = UNet2DConditionModel.from_pretrained(self.org_repo_id) @@ -1782,7 +1782,7 @@ def check_model_hotswap(self, do_compile, rank0, rank1, target_modules0, target_ assert not (output0_before == 0).all() assert not (output1_before == 0).all() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints unet.save_lora_adapter(os.path.join(tmp_dirname, "0"), safe_serialization=True, adapter_name="adapter0") unet.save_lora_adapter(os.path.join(tmp_dirname, "1"), safe_serialization=True, adapter_name="adapter1") diff --git a/tests/models/unets/test_models_unet_2d_condition.py b/tests/models/unets/test_models_unet_2d_condition.py index 838b259d3959..e7d2c24f98d5 100644 --- a/tests/models/unets/test_models_unet_2d_condition.py +++ b/tests/models/unets/test_models_unet_2d_condition.py @@ -16,7 +16,6 @@ import copy import gc import os -import tempfile import unittest from collections import OrderedDict @@ -35,6 +34,7 @@ from diffusers.utils import logging from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, backend_max_memory_allocated, backend_reset_max_memory_allocated, @@ -748,7 +748,7 @@ def test_custom_diffusion_save_load(self): with torch.no_grad(): sample = model(**inputs_dict).sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_attn_procs(tmpdirname, safe_serialization=False) self.assertTrue(os.path.isfile(os.path.join(tmpdirname, "pytorch_custom_diffusion_weights.bin"))) torch.manual_seed(0) @@ -1098,7 +1098,7 @@ def test_load_attn_procs_raise_warning(self): with torch.no_grad(): lora_sample_1 = model(**inputs_dict).sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_attn_procs(tmpdirname) model.unload_lora() @@ -1132,7 +1132,7 @@ def test_save_attn_procs_raise_warning(self): assert check_if_lora_correctly_set(model), "Lora not correctly set in UNet." - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertWarns(FutureWarning) as warning: model.save_attn_procs(tmpdirname) diff --git a/tests/models/unets/test_models_unet_motion.py b/tests/models/unets/test_models_unet_motion.py index acaf75b4c522..e894d4939a3f 100644 --- a/tests/models/unets/test_models_unet_motion.py +++ b/tests/models/unets/test_models_unet_motion.py @@ -15,7 +15,6 @@ import copy import os -import tempfile import unittest import numpy as np @@ -25,6 +24,7 @@ from diffusers.utils import logging from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, floats_tensor, torch_device, @@ -127,7 +127,7 @@ def test_saving_motion_modules(self): model = self.model_class(**init_dict) model.to(torch_device) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_motion_modules(tmpdirname) self.assertTrue(os.path.isfile(os.path.join(tmpdirname, "diffusion_pytorch_model.safetensors"))) @@ -211,7 +211,7 @@ def test_from_save_pretrained(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, safe_serialization=False) torch.manual_seed(0) new_model = self.model_class.from_pretrained(tmpdirname) @@ -238,7 +238,7 @@ def test_from_save_pretrained_variant(self, expected_max_diff=5e-5): model.to(torch_device) model.eval() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model.save_pretrained(tmpdirname, variant="fp16", safe_serialization=False) torch.manual_seed(0) diff --git a/tests/others/test_config.py b/tests/others/test_config.py index 66e416a2879c..032ade2ef8cc 100644 --- a/tests/others/test_config.py +++ b/tests/others/test_config.py @@ -14,7 +14,6 @@ # limitations under the License. import json -import tempfile import unittest from pathlib import Path @@ -28,7 +27,7 @@ logging, ) from diffusers.configuration_utils import ConfigMixin, register_to_config -from diffusers.utils.testing_utils import CaptureLogger +from diffusers.utils.testing_utils import CaptureLogger, TemporaryDirectory class SampleObject(ConfigMixin): @@ -152,7 +151,7 @@ def test_save_load(self): assert config["d"] == "for diffusion" assert config["e"] == [1, 3] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) new_obj = SampleObject.from_config(SampleObject.load_config(tmpdirname)) new_config = new_obj.config @@ -276,7 +275,7 @@ def test_use_default_values(self): # make sure that default config has all keys in `_use_default_values` assert set(config_dict.keys()) == set(config.config._use_default_values) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: config.save_config(tmpdirname) # now loading it with SampleObject2 should put f into `_use_default_values` diff --git a/tests/others/test_ema.py b/tests/others/test_ema.py index 87d06f097184..f84e3fa54382 100644 --- a/tests/others/test_ema.py +++ b/tests/others/test_ema.py @@ -13,14 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import tempfile import unittest import torch from diffusers import UNet2DConditionModel from diffusers.training_utils import EMAModel -from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device +from diffusers.utils.testing_utils import TemporaryDirectory, enable_full_determinism, skip_mps, torch_device enable_full_determinism() @@ -62,7 +61,7 @@ def simulate_backprop(self, unet): def test_from_pretrained(self): # Save the model parameters to a temporary directory unet, ema_unet = self.get_models() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) # Load the EMA model from the saved directory @@ -167,7 +166,7 @@ def test_serialization(self): unet, ema_unet = self.get_models() noisy_latents, timesteps, encoder_hidden_states = self.get_dummy_inputs() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) loaded_unet = UNet2DConditionModel.from_pretrained(tmpdir, model_cls=UNet2DConditionModel) loaded_unet = loaded_unet.to(unet.device) @@ -217,7 +216,7 @@ def simulate_backprop(self, unet): def test_from_pretrained(self): # Save the model parameters to a temporary directory unet, ema_unet = self.get_models() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) # Load the EMA model from the saved directory @@ -322,7 +321,7 @@ def test_serialization(self): unet, ema_unet = self.get_models() noisy_latents, timesteps, encoder_hidden_states = self.get_dummy_inputs() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: ema_unet.save_pretrained(tmpdir) loaded_unet = UNet2DConditionModel.from_pretrained(tmpdir, model_cls=UNet2DConditionModel) loaded_unet = loaded_unet.to(unet.device) diff --git a/tests/pipelines/allegro/test_allegro.py b/tests/pipelines/allegro/test_allegro.py index 9dc8b9d0b698..5718671ad2c6 100644 --- a/tests/pipelines/allegro/test_allegro.py +++ b/tests/pipelines/allegro/test_allegro.py @@ -15,7 +15,6 @@ import gc import inspect import os -import tempfile import unittest import numpy as np @@ -24,6 +23,7 @@ from diffusers import AllegroPipeline, AllegroTransformer3DModel, AutoencoderKLAllegro, DDIMScheduler from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, numpy_cosine_similarity_distance, require_hf_hub_version_greater, @@ -320,7 +320,7 @@ def test_save_load_dduf(self): pipeline_out = pipe(**inputs)[0].cpu() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dduf_filename = os.path.join(tmpdir, f"{pipe.__class__.__name__.lower()}.dduf") pipe.save_pretrained(tmpdir, safe_serialization=True) export_folder_as_dduf(dduf_filename, folder_path=tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet.py b/tests/pipelines/controlnet/test_controlnet.py index de45ced2815f..42933ce92e3d 100644 --- a/tests/pipelines/controlnet/test_controlnet.py +++ b/tests/pipelines/controlnet/test_controlnet.py @@ -14,7 +14,6 @@ # limitations under the License. import gc -import tempfile import traceback import unittest @@ -34,6 +33,7 @@ from diffusers.pipelines.controlnet.pipeline_controlnet import MultiControlNetModel from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, backend_max_memory_allocated, backend_reset_max_memory_allocated, @@ -486,7 +486,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) @@ -714,7 +714,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet_img2img.py b/tests/pipelines/controlnet/test_controlnet_img2img.py index 705e3f0c0f6b..b6f682e81d4a 100644 --- a/tests/pipelines/controlnet/test_controlnet_img2img.py +++ b/tests/pipelines/controlnet/test_controlnet_img2img.py @@ -17,7 +17,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -36,6 +35,7 @@ from diffusers.utils import load_image from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, floats_tensor, load_numpy, @@ -391,7 +391,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/controlnet/test_controlnet_inpaint.py b/tests/pipelines/controlnet/test_controlnet_inpaint.py index 1032dd702a07..3b9f76bb1d5f 100644 --- a/tests/pipelines/controlnet/test_controlnet_inpaint.py +++ b/tests/pipelines/controlnet/test_controlnet_inpaint.py @@ -17,7 +17,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -36,6 +35,7 @@ from diffusers.utils import load_image from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, floats_tensor, load_numpy, @@ -443,7 +443,7 @@ def test_save_pretrained_raise_not_implemented_exception(self): pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: try: # save_pretrained is not implemented for Multi-ControlNet pipe.save_pretrained(tmpdir) diff --git a/tests/pipelines/deepfloyd_if/__init__.py b/tests/pipelines/deepfloyd_if/__init__.py index 54262cc13526..ed78f60e1987 100644 --- a/tests/pipelines/deepfloyd_if/__init__.py +++ b/tests/pipelines/deepfloyd_if/__init__.py @@ -7,7 +7,7 @@ from diffusers import DDPMScheduler, UNet2DConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker -from diffusers.utils.testing_utils import torch_device +from diffusers.utils.testing_utils import TemporaryDirectory, torch_device from ..test_pipelines_common import to_np @@ -203,7 +203,7 @@ def _test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) @@ -257,7 +257,7 @@ def _test_save_load_local(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/hunyuandit/test_hunyuan_dit.py b/tests/pipelines/hunyuandit/test_hunyuan_dit.py index 1718d154cbfe..aee6323f3454 100644 --- a/tests/pipelines/hunyuandit/test_hunyuan_dit.py +++ b/tests/pipelines/hunyuandit/test_hunyuan_dit.py @@ -14,7 +14,6 @@ # limitations under the License. import gc -import tempfile import unittest import numpy as np @@ -28,6 +27,7 @@ HunyuanDiTPipeline, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, numpy_cosine_similarity_distance, require_torch_accelerator, @@ -267,7 +267,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/latte/test_latte.py b/tests/pipelines/latte/test_latte.py index 0836d3db9128..5a685cdbf839 100644 --- a/tests/pipelines/latte/test_latte.py +++ b/tests/pipelines/latte/test_latte.py @@ -15,7 +15,6 @@ import gc import inspect -import tempfile import unittest import numpy as np @@ -32,6 +31,7 @@ ) from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -280,7 +280,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pag/test_pag_hunyuan_dit.py b/tests/pipelines/pag/test_pag_hunyuan_dit.py index 01c7dbe8fa9b..a6124adc7752 100644 --- a/tests/pipelines/pag/test_pag_hunyuan_dit.py +++ b/tests/pipelines/pag/test_pag_hunyuan_dit.py @@ -14,7 +14,6 @@ # limitations under the License. import inspect -import tempfile import unittest import numpy as np @@ -28,7 +27,7 @@ HunyuanDiTPAGPipeline, HunyuanDiTPipeline, ) -from diffusers.utils.testing_utils import enable_full_determinism, torch_device +from diffusers.utils.testing_utils import TemporaryDirectory, enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np @@ -323,7 +322,7 @@ def test_save_load_optional_components(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pag/test_pag_pixart_sigma.py b/tests/pipelines/pag/test_pag_pixart_sigma.py index 4199f22756b2..e5dbf3c75a7c 100644 --- a/tests/pipelines/pag/test_pag_pixart_sigma.py +++ b/tests/pipelines/pag/test_pag_pixart_sigma.py @@ -14,7 +14,6 @@ # limitations under the License. import inspect -import tempfile import unittest import numpy as np @@ -32,6 +31,7 @@ from diffusers.utils import logging from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, enable_full_determinism, torch_device, ) @@ -200,7 +200,7 @@ def test_save_load_local(self, expected_max_difference=1e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: diff --git a/tests/pipelines/pixart_alpha/test_pixart.py b/tests/pipelines/pixart_alpha/test_pixart.py index 0d9babc5c9df..b5b84ebf3f84 100644 --- a/tests/pipelines/pixart_alpha/test_pixart.py +++ b/tests/pipelines/pixart_alpha/test_pixart.py @@ -14,7 +14,6 @@ # limitations under the License. import gc -import tempfile import unittest import numpy as np @@ -28,6 +27,7 @@ PixArtTransformer2DModel, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -183,7 +183,7 @@ def test_inference_with_embeddings_and_multiple_images(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/pixart_sigma/test_pixart.py b/tests/pipelines/pixart_sigma/test_pixart.py index 302ba40f8256..f57db5df0157 100644 --- a/tests/pipelines/pixart_sigma/test_pixart.py +++ b/tests/pipelines/pixart_sigma/test_pixart.py @@ -14,7 +14,6 @@ # limitations under the License. import gc -import tempfile import unittest import numpy as np @@ -28,6 +27,7 @@ PixArtTransformer2DModel, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -184,7 +184,7 @@ def test_inference_with_embeddings_and_multiple_images(self): output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py b/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py index 02cd1f12f945..d6bad4a20997 100644 --- a/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py +++ b/tests/pipelines/semantic_stable_diffusion/test_semantic_diffusion.py @@ -15,7 +15,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -25,6 +24,7 @@ from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNet2DConditionModel from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, floats_tensor, nightly, @@ -229,7 +229,7 @@ def test_semantic_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py b/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py index ed987e0badcd..28bbdba0bad6 100644 --- a/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py +++ b/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import tempfile import unittest import numpy as np @@ -27,7 +26,13 @@ OnnxStableDiffusionPipeline, PNDMScheduler, ) -from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu +from diffusers.utils.testing_utils import ( + TemporaryDirectory, + is_onnx_available, + nightly, + require_onnxruntime, + require_torch_gpu, +) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin @@ -366,7 +371,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = OnnxStableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion/test_stable_diffusion.py b/tests/pipelines/stable_diffusion/test_stable_diffusion.py index bcc3d45c1ed9..9e34489df3ee 100644 --- a/tests/pipelines/stable_diffusion/test_stable_diffusion.py +++ b/tests/pipelines/stable_diffusion/test_stable_diffusion.py @@ -15,7 +15,6 @@ import gc -import tempfile import time import traceback import unittest @@ -44,6 +43,7 @@ ) from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, backend_max_memory_allocated, backend_reset_max_memory_allocated, @@ -428,7 +428,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py index 31f7c75be952..9d49fa272af7 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py @@ -15,7 +15,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -37,6 +36,7 @@ UNet2DConditionModel, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, floats_tensor, @@ -186,7 +186,7 @@ def test_save_load_local(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) @@ -212,7 +212,7 @@ def test_save_load_float16(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py index 239c2c426c82..c49613faae17 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_diffedit.py @@ -15,7 +15,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -33,6 +32,7 @@ UNet2DConditionModel, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, floats_tensor, @@ -210,7 +210,7 @@ def test_save_load_optional_components(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) pipe_loaded.to(torch_device) diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py index 9c6ab5b65e66..e059f8cf3422 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_upscale.py @@ -15,7 +15,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -25,6 +24,7 @@ from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNet2DConditionModel from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, backend_max_memory_allocated, backend_reset_max_memory_allocated, @@ -358,7 +358,7 @@ def test_stable_diffusion_upscale_from_save_pretrained(self): sd_pipe = sd_pipe.to(device) pipes.append(sd_pipe) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd_pipe.save_pretrained(tmpdirname) sd_pipe = StableDiffusionUpscalePipeline.from_pretrained(tmpdirname).to(device) pipes.append(sd_pipe) diff --git a/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py b/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py index ce5749bc9961..bbd7490c8319 100644 --- a/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py +++ b/tests/pipelines/stable_diffusion_safe/test_safe_diffusion.py @@ -15,7 +15,6 @@ import gc import random -import tempfile import unittest import numpy as np @@ -24,7 +23,14 @@ from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNet2DConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline -from diffusers.utils.testing_utils import floats_tensor, nightly, require_accelerator, require_torch_gpu, torch_device +from diffusers.utils.testing_utils import ( + TemporaryDirectory, + floats_tensor, + nightly, + require_accelerator, + require_torch_gpu, + torch_device, +) class SafeDiffusionPipelineFastTests(unittest.TestCase): @@ -219,7 +225,7 @@ def test_stable_diffusion_no_safety_checker(self): assert image is not None # check that there's no error when saving a pipeline with one of the models being None - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = StableDiffusionPipeline.from_pretrained(tmpdirname) diff --git a/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py b/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py index 26728c19593f..138fa7ee757b 100644 --- a/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py +++ b/tests/pipelines/stable_diffusion_xl/test_stable_diffusion_xl.py @@ -15,7 +15,6 @@ import copy import gc -import tempfile import unittest import numpy as np @@ -35,6 +34,7 @@ UniPCMultistepScheduler, ) from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, load_image, numpy_cosine_similarity_distance, @@ -866,7 +866,7 @@ def test_stable_diffusion_xl_save_from_pretrained(self): sd_pipe = StableDiffusionXLPipeline(**components).to(torch_device) pipes.append(sd_pipe) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd_pipe.save_pretrained(tmpdirname) sd_pipe = StableDiffusionXLPipeline.from_pretrained(tmpdirname).to(torch_device) pipes.append(sd_pipe) diff --git a/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py b/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py index 71a73198c220..90339c69f4d1 100644 --- a/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py +++ b/tests/pipelines/stable_video_diffusion/test_stable_video_diffusion.py @@ -1,6 +1,5 @@ import gc import random -import tempfile import unittest import numpy as np @@ -22,6 +21,7 @@ from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, enable_full_determinism, floats_tensor, @@ -273,7 +273,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs).frames[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) for component in pipe_loaded.components.values(): @@ -316,7 +316,7 @@ def test_save_load_optional_components(self, expected_max_difference=1e-4): inputs = self.get_dummy_inputs(generator_device) output = pipe(**inputs).frames[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): @@ -353,7 +353,7 @@ def test_save_load_local(self, expected_max_difference=9e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: diff --git a/tests/pipelines/test_pipelines.py b/tests/pipelines/test_pipelines.py index 2e650cae0d94..9f901ce47fb9 100644 --- a/tests/pipelines/test_pipelines.py +++ b/tests/pipelines/test_pipelines.py @@ -20,7 +20,6 @@ import re import shutil import sys -import tempfile import traceback import unittest import unittest.mock as mock @@ -68,6 +67,7 @@ ) from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, enable_full_determinism, floats_tensor, @@ -121,7 +121,7 @@ def _test_from_save_pretrained_dynamo(in_queue, out_queue, timeout): ddpm.to(torch_device) ddpm.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ddpm.save_pretrained(tmpdirname) new_ddpm = DDPMPipeline.from_pretrained(tmpdirname) new_ddpm.to(torch_device) @@ -160,7 +160,7 @@ def test_one_request_upon_cached(self): if torch_device == "mps": return - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: DiffusionPipeline.download("hf-internal-testing/tiny-stable-diffusion-pipe", cache_dir=tmpdirname) @@ -184,7 +184,7 @@ def test_one_request_upon_cached(self): ), "We should call only `model_info` to check for _commit hash and `send_telemetry`" def test_less_downloads_passed_object(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: cached_folder = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname ) @@ -206,7 +206,7 @@ def test_less_downloads_passed_object_calls(self): if torch_device == "mps": return - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with requests_mock.mock(real_http=True) as m: DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname @@ -234,7 +234,7 @@ def test_less_downloads_passed_object_calls(self): ), "We should call only `model_info` to check for _commit hash and `send_telemetry`" def test_download_only_pytorch(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname @@ -250,7 +250,7 @@ def test_download_only_pytorch(self): assert not any(f.endswith(".safetensors") for f in files) def test_force_safetensors_error(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights with self.assertRaises(EnvironmentError): tmpdirname = DiffusionPipeline.download( @@ -261,7 +261,7 @@ def test_force_safetensors_error(self): ) def test_download_safetensors(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-safetensors", @@ -278,7 +278,7 @@ def test_download_safetensors(self): def test_download_safetensors_index(self): for variant in ["fp16", None]: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-indexes", cache_dir=tmpdirname, @@ -302,7 +302,7 @@ def test_download_safetensors_index(self): def test_download_bin_index(self): for variant in ["fp16", None]: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-indexes", cache_dir=tmpdirname, @@ -325,7 +325,7 @@ def test_download_bin_index(self): assert not any(".safetensors" in f for f in files) def test_download_no_openvino_by_default(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-open-vino", cache_dir=tmpdirname, @@ -339,7 +339,7 @@ def test_download_no_openvino_by_default(self): assert not any("openvino_" in f for f in files) def test_download_no_onnx_by_default(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-xl-pipe", cache_dir=tmpdirname, @@ -355,7 +355,7 @@ def test_download_no_onnx_by_default(self): @require_onnxruntime def test_download_onnx_by_default_for_onnx_pipelines(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline", cache_dir=tmpdirname, @@ -394,7 +394,7 @@ def test_load_no_safety_checker_explicit_locally(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_2 = StableDiffusionPipeline.from_pretrained(tmpdirname, safety_checker=None) pipe_2 = pipe_2.to(torch_device) @@ -413,7 +413,7 @@ def test_load_no_safety_checker_default_locally(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe_2 = StableDiffusionPipeline.from_pretrained(tmpdirname) pipe_2 = pipe_2.to(torch_device) @@ -461,7 +461,7 @@ def test_local_files_only_are_used_when_no_internet(self): # first check that with local files only the pipeline can only be used if cached with self.assertRaises(FileNotFoundError): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: orig_pipe = DiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-torch", local_files_only=True, cache_dir=tmpdirname ) @@ -490,7 +490,7 @@ def test_local_files_only_are_used_when_no_internet(self): def test_download_from_variant_folder(self): for use_safetensors in [False, True]: other_format = ".bin" if use_safetensors else ".safetensors" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -512,7 +512,7 @@ def test_download_variant_all(self): this_format = ".safetensors" if use_safetensors else ".bin" variant = "fp16" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -537,7 +537,7 @@ def test_download_variant_partly(self): this_format = ".safetensors" if use_safetensors else ".bin" variant = "no_ema" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-all-variants", cache_dir=tmpdirname, @@ -564,7 +564,7 @@ def test_download_variants_with_sharded_checkpoints(self): # the `text_encoder`. Their checkpoints can be sharded. for use_safetensors in [True, False]: for variant in ["fp16", None]: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = DiffusionPipeline.download( "hf-internal-testing/tiny-stable-diffusion-pipe-variants-right-format", safety_checker=None, @@ -590,7 +590,7 @@ def test_download_legacy_variants_with_sharded_ckpts_raises_warning(self): for is_local in [True, False]: with CaptureLogger(logger) as cap_logger: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: local_repo_id = repo_id if is_local: local_repo_id = snapshot_download(repo_id, cache_dir=tmpdirname) @@ -608,7 +608,7 @@ def test_download_safetensors_only_variant_exists_for_model(self): use_safetensors = True # text encoder is missing no variant weights, so the following can't work - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -619,7 +619,7 @@ def test_download_safetensors_only_variant_exists_for_model(self): assert "Error no file name" in str(error_context.exception) # text encoder has fp16 variants so we can load it - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-broken-variants", use_safetensors=use_safetensors, @@ -637,7 +637,7 @@ def test_download_bin_only_variant_exists_for_model(self): use_safetensors = False # text encoder is missing Non-variant weights, so the following can't work - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -648,7 +648,7 @@ def test_download_bin_only_variant_exists_for_model(self): assert "Error no file name" in str(error_context.exception) # text encoder has fp16 variants so we can load it - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: tmpdirname = StableDiffusionPipeline.download( "hf-internal-testing/stable-diffusion-broken-variants", use_safetensors=use_safetensors, @@ -666,7 +666,7 @@ def test_download_safetensors_variant_does_not_exist_for_model(self): use_safetensors = True # text encoder is missing no_ema variant weights, so the following can't work - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -682,7 +682,7 @@ def test_download_bin_variant_does_not_exist_for_model(self): use_safetensors = False # text encoder is missing no_ema variant weights, so the following can't work - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with self.assertRaises(OSError) as error_context: tmpdirname = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/stable-diffusion-broken-variants", @@ -706,7 +706,7 @@ def test_local_save_load_index(self): generator = torch.manual_seed(0) out = pipe(prompt, num_inference_steps=2, generator=generator, output_type="np").images - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname, variant=variant, safe_serialization=use_safe) pipe_2 = StableDiffusionPipeline.from_pretrained( tmpdirname, safe_serialization=use_safe, variant=variant @@ -728,7 +728,7 @@ def test_text_inversion_download(self): num_tokens = len(pipe.tokenizer) # single token load local - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<*>": torch.ones((32,))} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -744,7 +744,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # single token load local with weight name - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<**>": 2 * torch.ones((1, 32))} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -760,7 +760,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi token load - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = {"<***>": torch.cat([3 * torch.ones((1, 32)), 4 * torch.ones((1, 32)), 5 * torch.ones((1, 32))])} torch.save(ten, os.path.join(tmpdirname, "learned_embeds.bin")) @@ -783,7 +783,7 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi token load a1111 - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ten = { "string_to_param": { "*": torch.cat([3 * torch.ones((1, 32)), 4 * torch.ones((1, 32)), 5 * torch.ones((1, 32))]) @@ -811,8 +811,8 @@ def test_text_inversion_download(self): assert out.shape == (1, 128, 128, 3) # multi embedding load - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname1: - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname2: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname1: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname2: ten = {"<*****>": torch.ones((32,))} torch.save(ten, os.path.join(tmpdirname1, "learned_embeds.bin")) @@ -977,7 +977,7 @@ def test_textual_inversion_unload(self): def test_download_ignore_files(self): # Check https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe-ignore-files/blob/72f58636e5508a218c6b3f60550dc96445547817/model_index.json#L4 - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights tmpdirname = DiffusionPipeline.download("hf-internal-testing/tiny-stable-diffusion-pipe-ignore-files") all_root_files = [t[-1] for t in os.walk(os.path.join(tmpdirname))] @@ -1141,7 +1141,7 @@ def test_custom_model_and_pipeline(self): scheduler=DDIMScheduler(), ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname, safe_serialization=False) pipe_new = CustomPipeline.from_pretrained(tmpdirname) @@ -1186,14 +1186,14 @@ def test_save_pipeline_change_config(self): "hf-internal-testing/tiny-stable-diffusion-torch", safety_checker=None ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname) pipe = DiffusionPipeline.from_pretrained(tmpdirname) assert pipe.scheduler.__class__.__name__ == "PNDMScheduler" # let's make sure that changing the scheduler is correctly reflected - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) pipe.save_pretrained(tmpdirname) pipe = DiffusionPipeline.from_pretrained(tmpdirname) @@ -1546,7 +1546,7 @@ def test_set_scheduler_consistency(self): def test_save_safe_serialization(self): pipeline = StableDiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-torch") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipeline.save_pretrained(tmpdirname, safe_serialization=True) # Validate that the VAE safetensor exists and are of the correct format @@ -1573,7 +1573,7 @@ def test_save_safe_serialization(self): def test_no_pytorch_download_when_doing_safetensors(self): # by default we don't download - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/diffusers-stable-diffusion-tiny-all", cache_dir=tmpdirname ) @@ -1593,7 +1593,7 @@ def test_no_pytorch_download_when_doing_safetensors(self): def test_no_safetensors_download_when_doing_pytorch(self): use_safetensors = False - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = StableDiffusionPipeline.from_pretrained( "hf-internal-testing/diffusers-stable-diffusion-tiny-all", cache_dir=tmpdirname, @@ -1632,7 +1632,7 @@ def test_optional_components(self): assert sd.config.requires_safety_checker is True - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that passing None works @@ -1644,7 +1644,7 @@ def test_optional_components(self): assert sd.config.safety_checker == (None, None) assert sd.config.feature_extractor == (None, None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that loading previous None works @@ -1686,7 +1686,7 @@ def test_optional_components(self): assert sd.config.safety_checker == (None, None) assert sd.config.feature_extractor == (None, None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) # Test that partially loading works @@ -1708,7 +1708,7 @@ def test_optional_components(self): assert sd.config.safety_checker != (None, None) assert sd.config.feature_extractor != (None, None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) sd = StableDiffusionPipeline.from_pretrained(tmpdirname, feature_extractor=self.dummy_extractor) @@ -1722,7 +1722,7 @@ def test_name_or_path(self): assert sd.name_or_path == model_path - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: sd.save_pretrained(tmpdirname) sd = DiffusionPipeline.from_pretrained(tmpdirname) @@ -1825,7 +1825,7 @@ def test_pipe_same_device_id_offload(self): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_load_dduf_from_hub(self, dtype): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "DDUF/tiny-flux-dev-pipe-dduf", dduf_file="fluxpipeline.dduf", cache_dir=tmpdir, torch_dtype=dtype ).to(torch_device) @@ -1843,7 +1843,7 @@ def test_load_dduf_from_hub(self, dtype): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_load_dduf_from_hub_local_files_only(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "DDUF/tiny-flux-dev-pipe-dduf", dduf_file="fluxpipeline.dduf", cache_dir=tmpdir ).to(torch_device) @@ -1883,7 +1883,7 @@ def test_wrong_model(self): @require_hf_hub_version_greater("0.26.5") @require_transformers_version_greater("4.47.1") def test_dduf_load_sharded_checkpoint_diffusion_model(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe = DiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-flux-dev-pipe-sharded-checkpoint-DDUF", dduf_file="tiny-flux-dev-pipe-sharded-checkpoint.dduf", @@ -1919,7 +1919,7 @@ def tearDown(self): def test_smart_download(self): model_id = "hf-internal-testing/unet-pipeline-dummy" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: _ = DiffusionPipeline.from_pretrained(model_id, cache_dir=tmpdirname, force_download=True) local_repo_name = "--".join(["models"] + model_id.split("/")) snapshot_dir = os.path.join(tmpdirname, local_repo_name, "snapshots") @@ -1941,7 +1941,7 @@ def test_smart_download(self): def test_warning_unused_kwargs(self): model_id = "hf-internal-testing/unet-pipeline-dummy" logger = logging.get_logger("diffusers.pipelines") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: with CaptureLogger(logger) as cap_logger: DiffusionPipeline.from_pretrained( model_id, @@ -1972,7 +1972,7 @@ def test_from_save_pretrained(self): ddpm.to(torch_device) ddpm.set_progress_bar_config(disable=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: ddpm.save_pretrained(tmpdirname) new_ddpm = DDPMPipeline.from_pretrained(tmpdirname) new_ddpm.to(torch_device) @@ -2069,14 +2069,14 @@ def test_from_flax_from_pt(self): from diffusers import FlaxStableDiffusionPipeline - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe_pt.save_pretrained(tmpdirname) pipe_flax, params = FlaxStableDiffusionPipeline.from_pretrained( tmpdirname, safety_checker=None, from_pt=True ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe_flax.save_pretrained(tmpdirname, params=params) pipe_pt_2 = StableDiffusionPipeline.from_pretrained(tmpdirname, safety_checker=None, from_flax=True) pipe_pt_2.to(torch_device) @@ -2282,7 +2282,7 @@ def check_pipeline_hotswap(self, do_compile, rank0, rank1, target_modules0, targ assert not (output0_before == 0).all() assert not (output1_before == 0).all() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints lora0_state_dicts = self.get_lora_state_dicts({"unet": pipeline.unet}, adapter_name="adapter0") StableDiffusionPipeline.save_lora_weights( @@ -2413,7 +2413,7 @@ def test_hotswap_component_not_supported_raises(self): pipeline.text_encoder.add_adapter(lora_config0, adapter_name="adapter0") pipeline.text_encoder.add_adapter(lora_config1, adapter_name="adapter1") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dirname: # save the adapter checkpoints lora0_state_dicts = self.get_lora_state_dicts( {"text_encoder": pipeline.text_encoder}, adapter_name="adapter0" diff --git a/tests/pipelines/test_pipelines_common.py b/tests/pipelines/test_pipelines_common.py index 1037f252b564..4e2fe5331324 100644 --- a/tests/pipelines/test_pipelines_common.py +++ b/tests/pipelines/test_pipelines_common.py @@ -2,7 +2,6 @@ import inspect import json import os -import tempfile import unittest import uuid from typing import Any, Callable, Dict, Union @@ -48,6 +47,7 @@ from diffusers.utils.source_code_parsing_utils import ReturnNameVisitor from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, require_accelerate_version_greater, require_accelerator, @@ -1136,7 +1136,7 @@ def test_save_load_local(self, expected_max_difference=5e-4): logger = logging.get_logger("diffusers.pipelines.pipeline_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) with CaptureLogger(logger) as cap_logger: @@ -1401,7 +1401,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) for component in pipe_loaded.components.values(): @@ -1444,7 +1444,7 @@ def test_save_load_optional_components(self, expected_max_difference=1e-4): torch.manual_seed(0) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): @@ -1975,7 +1975,7 @@ def test_serialization_with_variants(self): ] variant = "fp16" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, variant=variant, safe_serialization=False) with open(f"{tmpdir}/model_index.json", "r") as f: @@ -1999,7 +1999,7 @@ def is_nan(tensor): has_nan = torch.isnan(tensor).any() return has_nan - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir, variant=variant, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, variant=variant) @@ -2026,7 +2026,7 @@ def test_loading_with_incorrect_variants_raises_error(self): pipe = self.pipeline_class(**components) variant = "fp16" - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: # Don't save with variants. pipe.save_pretrained(tmpdir, safe_serialization=False) @@ -2181,7 +2181,7 @@ def test_save_load_dduf(self, atol=1e-4, rtol=1e-4): pipeline_out = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dduf_filename = os.path.join(tmpdir, f"{pipe.__class__.__name__.lower()}.dduf") pipe.save_pretrained(tmpdir, safe_serialization=True) export_folder_as_dduf(dduf_filename, folder_path=tmpdir) @@ -2292,7 +2292,7 @@ def test_torch_dtype_dict(self): specified_key = next(iter(components.keys())) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: pipe.save_pretrained(tmpdirname, safe_serialization=False) torch_dtype_dict = {specified_key: torch.bfloat16, "default": torch.float16} loaded_pipe = self.pipeline_class.from_pretrained(tmpdirname, torch_dtype=torch_dtype_dict) @@ -2355,7 +2355,7 @@ def get_pipeline_components(self): ) text_encoder = CLIPTextModel(text_encoder_config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: dummy_vocab = {"<|startoftext|>": 0, "<|endoftext|>": 1, "!": 2} vocab_path = os.path.join(tmpdir, "vocab.json") with open(vocab_path, "w") as f: @@ -2392,7 +2392,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipeline.save_pretrained(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) new_model = UNet2DConditionModel.from_pretrained(f"{USER}/{self.repo_id}", subfolder="unet") @@ -2416,7 +2416,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_pretrained - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipeline.save_pretrained(tmp_dir, push_to_hub=True, token=TOKEN, repo_id=self.org_repo_id) new_model = UNet2DConditionModel.from_pretrained(self.org_repo_id, subfolder="unet") diff --git a/tests/pipelines/test_pipelines_flax.py b/tests/pipelines/test_pipelines_flax.py index dc55b236c2b6..8c6e067b0b0b 100644 --- a/tests/pipelines/test_pipelines_flax.py +++ b/tests/pipelines/test_pipelines_flax.py @@ -14,13 +14,12 @@ # limitations under the License. import os -import tempfile import unittest import numpy as np from diffusers.utils import is_flax_available -from diffusers.utils.testing_utils import require_flax, slow +from diffusers.utils.testing_utils import TemporaryDirectory, require_flax, slow if is_flax_available(): @@ -35,7 +34,7 @@ @require_flax class DownloadTests(unittest.TestCase): def test_download_only_pytorch(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: # pipeline has Flax weights _ = FlaxDiffusionPipeline.from_pretrained( "hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None, cache_dir=tmpdirname diff --git a/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py b/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py index 49d5b6e612d3..da04bc061694 100644 --- a/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py +++ b/tests/pipelines/text_to_video_synthesis/test_text_to_video_zero_sdxl.py @@ -15,7 +15,6 @@ import gc import inspect -import tempfile import unittest import numpy as np @@ -24,6 +23,7 @@ from diffusers import AutoencoderKL, DDIMScheduler, TextToVideoZeroSDXLPipeline, UNet2DConditionModel from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, nightly, require_accelerate_version_greater, @@ -299,7 +299,7 @@ def test_save_load_float16(self, expected_max_diff=1e-2): inputs = self.get_dummy_inputs(self.generator_device) output = pipe(**inputs)[0] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) pipe_loaded.to(torch_device) diff --git a/tests/quantization/bnb/test_4bit.py b/tests/quantization/bnb/test_4bit.py index 7a61e42452c7..1c4958c1b250 100644 --- a/tests/quantization/bnb/test_4bit.py +++ b/tests/quantization/bnb/test_4bit.py @@ -14,7 +14,6 @@ # limitations under the License. import gc import os -import tempfile import unittest import numpy as np @@ -33,6 +32,7 @@ from diffusers.utils import is_accelerate_version, logging from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, is_bitsandbytes_available, is_torch_available, @@ -347,7 +347,7 @@ def test_bnb_4bit_errors_loading_incorrect_state_dict(self): r""" Test if loading with an incorrect state dict raises an error. """ - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: nf4_config = BitsAndBytesConfig(load_in_4bit=True) model_4bit = SD3Transformer2DModel.from_pretrained( self.model_name, subfolder="transformer", quantization_config=nf4_config, device_map=torch_device @@ -778,7 +778,7 @@ def test_serialization(self, quant_type="nf4", double_quant=True, safe_serializa device_map=torch_device, ) self.assertTrue("_pre_quantization_dtype" in model_0.config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: model_0.save_pretrained(tmpdirname, safe_serialization=safe_serialization) config = SD3Transformer2DModel.load_config(tmpdirname) diff --git a/tests/quantization/bnb/test_mixed_int8.py b/tests/quantization/bnb/test_mixed_int8.py index b3c4e70f9385..a8e0c58c4e57 100644 --- a/tests/quantization/bnb/test_mixed_int8.py +++ b/tests/quantization/bnb/test_mixed_int8.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. import gc -import tempfile import unittest import numpy as np @@ -31,6 +30,7 @@ from diffusers.utils import is_accelerate_version from diffusers.utils.testing_utils import ( CaptureLogger, + TemporaryDirectory, backend_empty_cache, is_bitsandbytes_available, is_torch_available, @@ -718,7 +718,7 @@ def test_serialization(self): Test whether it is possible to serialize a model in 8-bit. Uses most typical params as default. """ self.assertTrue("_pre_quantization_dtype" in self.model_0.config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: self.model_0.save_pretrained(tmpdirname) config = SD3Transformer2DModel.load_config(tmpdirname) @@ -749,7 +749,7 @@ def test_serialization(self): self.assertTrue(torch.equal(out_0, out_1)) def test_serialization_sharded(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: self.model_0.save_pretrained(tmpdirname, max_shard_size="200MB") config = SD3Transformer2DModel.load_config(tmpdirname) diff --git a/tests/quantization/quanto/test_quanto.py b/tests/quantization/quanto/test_quanto.py index 0e1b44109275..abf154cf0484 100644 --- a/tests/quantization/quanto/test_quanto.py +++ b/tests/quantization/quanto/test_quanto.py @@ -1,11 +1,11 @@ import gc -import tempfile import unittest from diffusers import FluxPipeline, FluxTransformer2DModel, QuantoConfig from diffusers.models.attention_processor import Attention from diffusers.utils import is_optimum_quanto_available, is_torch_available from diffusers.utils.testing_utils import ( + TemporaryDirectory, nightly, numpy_cosine_similarity_distance, require_accelerate, @@ -143,7 +143,7 @@ def test_serialization(self): with torch.no_grad(): model_output = model(**inputs) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: model.save_pretrained(tmp_dir) saved_model = self.model_cls.from_pretrained( tmp_dir, diff --git a/tests/quantization/torchao/test_torchao.py b/tests/quantization/torchao/test_torchao.py index 1a3447c6a47d..6e817c2485ad 100644 --- a/tests/quantization/torchao/test_torchao.py +++ b/tests/quantization/torchao/test_torchao.py @@ -14,7 +14,6 @@ # limitations under the License. import gc -import tempfile import unittest from typing import List @@ -30,6 +29,7 @@ ) from diffusers.models.attention_processor import Attention from diffusers.utils.testing_utils import ( + TemporaryDirectory, enable_full_determinism, is_torch_available, is_torchao_available, @@ -316,7 +316,7 @@ def test_device_map(self): expected_slice = expected_slice_auto else: expected_slice = expected_slice_offload - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: + with TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: quantization_config = TorchAoConfig("int4_weight_only", group_size=64) quantized_model = FluxTransformer2DModel.from_pretrained( "hf-internal-testing/tiny-flux-pipe", @@ -340,7 +340,7 @@ def test_device_map(self): output_slice = output.flatten()[-9:].detach().float().cpu().numpy() self.assertTrue(numpy_cosine_similarity_distance(output_slice, expected_slice) < 1e-3) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: + with TemporaryDirectory(ignore_cleanup_errors=True) as offload_folder: quantization_config = TorchAoConfig("int4_weight_only", group_size=64) quantized_model = FluxTransformer2DModel.from_pretrained( "hf-internal-testing/tiny-flux-sharded", @@ -576,7 +576,7 @@ def _test_original_model_expected_slice(self, quant_method, quant_method_kwargs, def _check_serialization_expected_slice(self, quant_method, quant_method_kwargs, expected_slice, device): quantized_model = self.get_dummy_model(quant_method, quant_method_kwargs, device) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: quantized_model.save_pretrained(tmp_dir, safe_serialization=False) loaded_quantized_model = FluxTransformer2DModel.from_pretrained( tmp_dir, torch_dtype=torch.bfloat16, use_safetensors=False @@ -728,7 +728,7 @@ def test_serialization_int8wo(self): inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0].flatten()[:128] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipe.transformer.save_pretrained(tmp_dir, safe_serialization=False) pipe.remove_all_hooks() del pipe.transformer diff --git a/tests/schedulers/test_scheduler_deis.py b/tests/schedulers/test_scheduler_deis.py index c6948f55daba..92db90ab84ce 100644 --- a/tests/schedulers/test_scheduler_deis.py +++ b/tests/schedulers/test_scheduler_deis.py @@ -1,4 +1,3 @@ -import tempfile import unittest import torch @@ -9,6 +8,7 @@ DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -43,7 +43,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -77,7 +77,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_multi.py b/tests/schedulers/test_scheduler_dpm_multi.py index 69337cf76536..f695e0d29fd1 100644 --- a/tests/schedulers/test_scheduler_dpm_multi.py +++ b/tests/schedulers/test_scheduler_dpm_multi.py @@ -1,4 +1,3 @@ -import tempfile import unittest import torch @@ -9,6 +8,7 @@ DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -53,7 +53,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -87,7 +87,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_multi_inverse.py b/tests/schedulers/test_scheduler_dpm_multi_inverse.py index 24fc41a0907f..396c3b3920d0 100644 --- a/tests/schedulers/test_scheduler_dpm_multi_inverse.py +++ b/tests/schedulers/test_scheduler_dpm_multi_inverse.py @@ -1,8 +1,7 @@ -import tempfile - import torch from diffusers import DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -45,7 +44,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -78,7 +77,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_dpm_single.py b/tests/schedulers/test_scheduler_dpm_single.py index 72d4680ea4da..25cf490ea6a6 100644 --- a/tests/schedulers/test_scheduler_dpm_single.py +++ b/tests/schedulers/test_scheduler_dpm_single.py @@ -1,4 +1,3 @@ -import tempfile import unittest import torch @@ -9,6 +8,7 @@ DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -51,7 +51,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -85,7 +85,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py b/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py index 0507ee9883bd..cd2402b7f7c2 100644 --- a/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py +++ b/tests/schedulers/test_scheduler_edm_dpmsolver_multistep.py @@ -1,9 +1,9 @@ -import tempfile import unittest import torch from diffusers import EDMDPMSolverMultistepScheduler +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -46,7 +46,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -80,7 +80,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_edm_euler.py b/tests/schedulers/test_scheduler_edm_euler.py index a41e31aeda87..28248483eacc 100644 --- a/tests/schedulers/test_scheduler_edm_euler.py +++ b/tests/schedulers/test_scheduler_edm_euler.py @@ -1,11 +1,11 @@ import inspect -import tempfile import unittest from typing import Dict, List, Tuple import torch from diffusers import EDMEulerScheduler +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -89,7 +89,7 @@ def test_from_save_pretrained(self): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_flax.py b/tests/schedulers/test_scheduler_flax.py index 08ded9a26e08..c84db6c9a575 100644 --- a/tests/schedulers/test_scheduler_flax.py +++ b/tests/schedulers/test_scheduler_flax.py @@ -13,13 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import inspect -import tempfile import unittest from typing import Dict, List, Tuple from diffusers import FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxPNDMScheduler from diffusers.utils import is_flax_available -from diffusers.utils.testing_utils import require_flax +from diffusers.utils.testing_utils import TemporaryDirectory, require_flax if is_flax_available(): @@ -82,7 +81,7 @@ def check_over_configs(self, time_step=0, **config): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -111,7 +110,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -139,7 +138,7 @@ def test_from_save_pretrained(self): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -394,7 +393,7 @@ def check_over_configs(self, time_step=0, **config): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -422,7 +421,7 @@ def test_from_save_pretrained(self): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -451,7 +450,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler = scheduler_class(**scheduler_config) state = scheduler.create_state() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) @@ -658,7 +657,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals state = state.replace(ets=dummy_past_residuals[:]) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) new_state = new_scheduler.set_timesteps(new_state, num_inference_steps, shape=sample.shape) @@ -746,7 +745,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler, new_state = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_ipndm.py b/tests/schedulers/test_scheduler_ipndm.py index 99b8c7cad020..25ac322b4c82 100644 --- a/tests/schedulers/test_scheduler_ipndm.py +++ b/tests/schedulers/test_scheduler_ipndm.py @@ -1,9 +1,9 @@ -import tempfile import unittest import torch from diffusers import IPNDMScheduler +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -34,7 +34,7 @@ def check_over_configs(self, time_step=0, **config): if time_step is None: time_step = scheduler.timesteps[len(scheduler.timesteps) // 2] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -73,7 +73,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): if time_step is None: time_step = scheduler.timesteps[len(scheduler.timesteps) // 2] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_lcm.py b/tests/schedulers/test_scheduler_lcm.py index ec7e4bcc7457..9aadd7a70013 100644 --- a/tests/schedulers/test_scheduler_lcm.py +++ b/tests/schedulers/test_scheduler_lcm.py @@ -1,10 +1,9 @@ -import tempfile from typing import Dict, List, Tuple import torch from diffusers import LCMScheduler -from diffusers.utils.testing_utils import torch_device +from diffusers.utils.testing_utils import TemporaryDirectory, torch_device from .test_schedulers import SchedulerCommonTest @@ -118,7 +117,7 @@ def test_from_save_pretrained(self): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_pndm.py b/tests/schedulers/test_scheduler_pndm.py index a88e947ded83..9d5c5b030f37 100644 --- a/tests/schedulers/test_scheduler_pndm.py +++ b/tests/schedulers/test_scheduler_pndm.py @@ -1,9 +1,9 @@ -import tempfile import unittest import torch from diffusers import PNDMScheduler +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -37,7 +37,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -73,7 +73,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.ets = dummy_past_residuals[:] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_scheduler_score_sde_ve.py b/tests/schedulers/test_scheduler_score_sde_ve.py index f4982ca2a550..82ba10b352af 100644 --- a/tests/schedulers/test_scheduler_score_sde_ve.py +++ b/tests/schedulers/test_scheduler_score_sde_ve.py @@ -1,10 +1,10 @@ -import tempfile import unittest import numpy as np import torch from diffusers import ScoreSdeVeScheduler +from diffusers.utils.testing_utils import TemporaryDirectory class ScoreSdeVeSchedulerTest(unittest.TestCase): @@ -66,7 +66,7 @@ def check_over_configs(self, time_step=0, **config): scheduler_config = self.get_scheduler_config(**config) scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -97,7 +97,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) diff --git a/tests/schedulers/test_scheduler_unipc.py b/tests/schedulers/test_scheduler_unipc.py index 0fc452436eb1..b0ea0d601f81 100644 --- a/tests/schedulers/test_scheduler_unipc.py +++ b/tests/schedulers/test_scheduler_unipc.py @@ -1,5 +1,3 @@ -import tempfile - import torch from diffusers import ( @@ -8,6 +6,7 @@ DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) +from diffusers.utils.testing_utils import TemporaryDirectory from .test_schedulers import SchedulerCommonTest @@ -44,7 +43,7 @@ def check_over_configs(self, time_step=0, **config): # copy over dummy past residuals scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) new_scheduler.set_timesteps(num_inference_steps) @@ -74,7 +73,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): # copy over dummy past residuals (must be after setting timesteps) scheduler.model_outputs = dummy_past_residuals[: scheduler.config.solver_order] - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) # copy over dummy past residuals diff --git a/tests/schedulers/test_schedulers.py b/tests/schedulers/test_schedulers.py index 157432344497..5ee2447b797c 100755 --- a/tests/schedulers/test_schedulers.py +++ b/tests/schedulers/test_schedulers.py @@ -15,7 +15,6 @@ import inspect import json import os -import tempfile import unittest import uuid from typing import Dict, List, Tuple @@ -41,7 +40,7 @@ from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.schedulers.scheduling_utils import SchedulerMixin from diffusers.utils import logging -from diffusers.utils.testing_utils import CaptureLogger, torch_device +from diffusers.utils.testing_utils import CaptureLogger, TemporaryDirectory, torch_device from ..others.test_utils import TOKEN, USER, is_staging_test @@ -106,7 +105,7 @@ def test_save_load_from_different_config(self): setattr(diffusers, "SchedulerObject", SchedulerObject) logger = logging.get_logger("diffusers.configuration_utils") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) with CaptureLogger(logger) as cap_logger_1: config = SchedulerObject2.load_config(tmpdirname) @@ -152,7 +151,7 @@ def test_save_load_compatible_schedulers(self): setattr(diffusers, "SchedulerObject2", SchedulerObject2) logger = logging.get_logger("diffusers.configuration_utils") - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) # now save a config parameter that is expected by another class, but not origin class @@ -191,7 +190,7 @@ def test_save_load_from_different_config_comp_schedulers(self): logger = logging.get_logger("diffusers.configuration_utils") logger.setLevel(diffusers.logging.INFO) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: obj.save_config(tmpdirname) with CaptureLogger(logger) as cap_logger_1: @@ -398,7 +397,7 @@ def check_over_configs(self, time_step=0, **config): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -451,7 +450,7 @@ def check_over_forward(self, time_step=0, **forward_kwargs): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -497,7 +496,7 @@ def test_from_save_pretrained(self): sample = self.dummy_sample residual = 0.1 * sample - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_config(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -547,7 +546,7 @@ def test_from_pretrained(self): scheduler = scheduler_class(**scheduler_config) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_pretrained(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -756,7 +755,7 @@ def test_trained_betas(self): scheduler_config = self.get_scheduler_config() scheduler = scheduler_class(**scheduler_config, trained_betas=np.array([0.1, 0.3])) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: scheduler.save_pretrained(tmpdirname) new_scheduler = scheduler_class.from_pretrained(tmpdirname) @@ -830,7 +829,7 @@ def test_push_to_hub(self): delete_repo(token=TOKEN, repo_id=self.repo_id) # Push to hub via save_config - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: scheduler.save_config(tmp_dir, repo_id=self.repo_id, push_to_hub=True, token=TOKEN) scheduler_loaded = DDIMScheduler.from_pretrained(f"{USER}/{self.repo_id}") @@ -857,7 +856,7 @@ def test_push_to_hub_in_organization(self): delete_repo(token=TOKEN, repo_id=self.org_repo_id) # Push to hub via save_config - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: scheduler.save_config(tmp_dir, repo_id=self.org_repo_id, push_to_hub=True, token=TOKEN) scheduler_loaded = DDIMScheduler.from_pretrained(self.org_repo_id) diff --git a/tests/single_file/single_file_testing_utils.py b/tests/single_file/single_file_testing_utils.py index 2fcddcadfe12..9f9a1c19760f 100644 --- a/tests/single_file/single_file_testing_utils.py +++ b/tests/single_file/single_file_testing_utils.py @@ -1,4 +1,3 @@ -import tempfile from io import BytesIO import requests @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.models.attention_processor import AttnProcessor from diffusers.utils.testing_utils import ( + TemporaryDirectory, numpy_cosine_similarity_distance, torch_device, ) @@ -100,7 +100,7 @@ def test_single_file_components(self, pipe=None, single_file_pipe=None): def test_single_file_components_local_files_only(self, pipe=None, single_file_pipe=None): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -140,7 +140,7 @@ def test_single_file_components_with_original_config_local_files_only( # we just pass it in here otherwise this test will fail upcast_attention = pipe.unet.config.upcast_attention - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -193,7 +193,7 @@ def test_single_file_components_with_diffusers_config_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) @@ -288,7 +288,7 @@ def test_single_file_components_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -329,7 +329,7 @@ def test_single_file_components_with_original_config_local_files_only( # we just pass it in here otherwise this test will fail upcast_attention = pipe.unet.config.upcast_attention - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -366,7 +366,7 @@ def test_single_file_components_with_diffusers_config_local_files_only( ): pipe = pipe or self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py index 7195284035ea..2754cc482a4c 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -111,7 +111,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, controlnet=controlnet) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) @@ -139,7 +139,7 @@ def test_single_file_components_with_original_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) @@ -172,7 +172,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weights_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weights_name, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py index 97065902147d..5064a462bbc2 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -105,7 +105,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, safety_checker=None, controlnet=controlnet) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -136,7 +136,7 @@ def test_single_file_components_with_original_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -173,7 +173,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_controlnet_single_file.py b/tests/single_file/test_stable_diffusion_controlnet_single_file.py index 3daeca10e143..9f9c8382a816 100644 --- a/tests/single_file/test_stable_diffusion_controlnet_single_file.py +++ b/tests/single_file/test_stable_diffusion_controlnet_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -102,7 +102,7 @@ def test_single_file_components_local_files_only(self): controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_canny") pipe = self.pipeline_class.from_pretrained(self.repo_id, controlnet=controlnet) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -130,7 +130,7 @@ def test_single_file_components_with_original_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) @@ -161,7 +161,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): safety_checker=None, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_single_file.py b/tests/single_file/test_stable_diffusion_single_file.py index aa7d45821ecf..91675137bb0a 100644 --- a/tests/single_file/test_stable_diffusion_single_file.py +++ b/tests/single_file/test_stable_diffusion_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, nightly, @@ -64,7 +64,7 @@ def test_single_file_format_inference_is_same_as_pretrained(self): super().test_single_file_format_inference_is_same_as_pretrained(expected_max_diff=1e-3) def test_single_file_legacy_scheduler_loading(self): - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py b/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py index 1680121e7f2f..91a1dc439bea 100644 --- a/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py +++ b/tests/single_file/test_stable_diffusion_xl_adapter_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -11,6 +10,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -120,7 +120,7 @@ def test_single_file_components_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -152,7 +152,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) @@ -190,7 +190,7 @@ def test_single_file_components_with_original_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_original_config = download_original_config(self.original_config, tmpdir) diff --git a/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py b/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py index 0ff1aa27d35e..18651fe7f5c8 100644 --- a/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py +++ b/tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py @@ -1,5 +1,4 @@ import gc -import tempfile import unittest import torch @@ -8,6 +7,7 @@ from diffusers.loaders.single_file_utils import _extract_repo_id_and_weights_name from diffusers.utils import load_image from diffusers.utils.testing_utils import ( + TemporaryDirectory, backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, @@ -113,7 +113,7 @@ def test_single_file_components_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -152,7 +152,7 @@ def test_single_file_components_with_original_config_local_files_only(self): torch_dtype=torch.float16, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) @@ -184,7 +184,7 @@ def test_single_file_components_with_diffusers_config_local_files_only(self): controlnet=controlnet, ) - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir: repo_id, weight_name = _extract_repo_id_and_weights_name(self.ckpt_path) local_ckpt_path = download_single_file_checkpoint(repo_id, weight_name, tmpdir) local_diffusers_config = download_diffusers_config(self.repo_id, tmpdir) diff --git a/utils/update_metadata.py b/utils/update_metadata.py index 012f75e16a28..392651cb4af2 100644 --- a/utils/update_metadata.py +++ b/utils/update_metadata.py @@ -27,7 +27,6 @@ import argparse import os -import tempfile import pandas as pd from datasets import Dataset @@ -38,6 +37,7 @@ AUTO_INPAINT_PIPELINES_MAPPING, AUTO_TEXT2IMAGE_PIPELINES_MAPPING, ) +from diffusers.utils.testing_utils import TemporaryDirectory PIPELINE_TAG_JSON = "pipeline_tags.json" @@ -91,7 +91,7 @@ def update_metadata(commit_sha: str): with open(hub_pipeline_tags_json) as f: hub_pipeline_tags_json = f.read() - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: pipelines_dataset.to_json(os.path.join(tmp_dir, PIPELINE_TAG_JSON)) with open(os.path.join(tmp_dir, PIPELINE_TAG_JSON)) as f: From 314c4618772cf4a944b085ee6b569beba046d792 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 9 Apr 2025 09:07:17 +0000 Subject: [PATCH 3/4] Apply style fixes --- src/diffusers/utils/testing_utils.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/diffusers/utils/testing_utils.py b/src/diffusers/utils/testing_utils.py index 283174dd66c8..89026e799bd1 100644 --- a/src/diffusers/utils/testing_utils.py +++ b/src/diffusers/utils/testing_utils.py @@ -1388,15 +1388,13 @@ def __repr__(self): class TemporaryDirectory: - """Create and return a temporary directory. This has the same - behavior as mkdtemp but can be used as a context manager. For - example: + """Create and return a temporary directory. This has the same + behavior as mkdtemp but can be used as a context manager. For example: with TemporaryDirectory() as tmpdir: ... - Upon exiting the context, the directory and everything contained - in it are removed. + Upon exiting the context, the directory and everything contained in it are removed. """ def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False): From 43a052038d5c07a720a241c5b2b3e34443f9b2f8 Mon Sep 17 00:00:00 2001 From: hlky Date: Wed, 9 Apr 2025 10:09:27 +0100 Subject: [PATCH 4/4] trigger tests