Skip to content

Commit 53f8ef5

Browse files
committed
Use our own TemporaryDirectory
1 parent fbdba4a commit 53f8ef5

File tree

84 files changed

+514
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+514
-408
lines changed

examples/advanced_diffusion_training/test_dreambooth_lora_flux_advanced.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
import logging
1717
import os
1818
import sys
19-
import tempfile
2019

2120
import safetensors
2221

22+
from diffusers.utils.testing_utils import TemporaryDirectory
23+
2324

2425
sys.path.append("..")
2526
from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402
@@ -39,7 +40,7 @@ class DreamBoothLoRAFluxAdvanced(ExamplesTestsAccelerate):
3940
script_path = "examples/advanced_diffusion_training/train_dreambooth_lora_flux_advanced.py"
4041

4142
def test_dreambooth_lora_flux(self):
42-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
43+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
4344
test_args = f"""
4445
{self.script_path}
4546
--pretrained_model_name_or_path {self.pretrained_model_name_or_path}
@@ -71,7 +72,7 @@ def test_dreambooth_lora_flux(self):
7172
self.assertTrue(starts_with_transformer)
7273

7374
def test_dreambooth_lora_text_encoder_flux(self):
74-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
75+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
7576
test_args = f"""
7677
{self.script_path}
7778
--pretrained_model_name_or_path {self.pretrained_model_name_or_path}
@@ -104,7 +105,7 @@ def test_dreambooth_lora_text_encoder_flux(self):
104105
self.assertTrue(starts_with_expected_prefix)
105106

106107
def test_dreambooth_lora_pivotal_tuning_flux_clip(self):
107-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
108+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
108109
test_args = f"""
109110
{self.script_path}
110111
--pretrained_model_name_or_path {self.pretrained_model_name_or_path}
@@ -146,7 +147,7 @@ def test_dreambooth_lora_pivotal_tuning_flux_clip(self):
146147
self.assertTrue(starts_with_transformer)
147148

148149
def test_dreambooth_lora_pivotal_tuning_flux_clip_t5(self):
149-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
150+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
150151
test_args = f"""
151152
{self.script_path}
152153
--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):
189190
self.assertTrue(starts_with_transformer)
190191

191192
def test_dreambooth_lora_latent_caching(self):
192-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
193+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
193194
test_args = f"""
194195
{self.script_path}
195196
--pretrained_model_name_or_path {self.pretrained_model_name_or_path}
@@ -222,7 +223,7 @@ def test_dreambooth_lora_latent_caching(self):
222223
self.assertTrue(starts_with_transformer)
223224

224225
def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit(self):
225-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
226+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
226227
test_args = f"""
227228
{self.script_path}
228229
--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):
245246
)
246247

247248
def test_dreambooth_lora_flux_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):
248-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
249+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
249250
test_args = f"""
250251
{self.script_path}
251252
--pretrained_model_name_or_path={self.pretrained_model_name_or_path}

examples/community/sde_drag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
import tempfile
32
from typing import List, Optional
43

54
import numpy as np
@@ -21,6 +20,7 @@
2120
SlicedAttnAddedKVProcessor,
2221
)
2322
from diffusers.optimization import get_scheduler
23+
from diffusers.utils.testing_utils import TemporaryDirectory
2424

2525

2626
class SdeDragPipeline(DiffusionPipeline):
@@ -320,7 +320,7 @@ def train_lora(self, prompt, image, lora_step=100, lora_rank=16, generator=None)
320320
lr_scheduler.step()
321321
optimizer.zero_grad()
322322

323-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as save_lora_dir:
323+
with TemporaryDirectory(ignore_cleanup_errors=True) as save_lora_dir:
324324
StableDiffusionLoraLoaderMixin.save_lora_weights(
325325
save_directory=save_lora_dir,
326326
unet_lora_layers=unet_lora_layers,

examples/consistency_distillation/test_lcm_lora.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
import logging
1717
import os
1818
import sys
19-
import tempfile
2019

2120
import safetensors
2221

22+
from diffusers.utils.testing_utils import TemporaryDirectory
23+
2324

2425
sys.path.append("..")
2526
from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402
@@ -34,7 +35,7 @@
3435

3536
class TextToImageLCM(ExamplesTestsAccelerate):
3637
def test_text_to_image_lcm_lora_sdxl(self):
37-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
38+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
3839
test_args = f"""
3940
examples/consistency_distillation/train_lcm_distill_lora_sdxl.py
4041
--pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe
@@ -61,7 +62,7 @@ def test_text_to_image_lcm_lora_sdxl(self):
6162
self.assertTrue(is_lora)
6263

6364
def test_text_to_image_lcm_lora_sdxl_checkpointing(self):
64-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
65+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
6566
test_args = f"""
6667
examples/consistency_distillation/train_lcm_distill_lora_sdxl.py
6768
--pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe

examples/controlnet/test_controlnet.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import logging
1717
import os
1818
import sys
19-
import tempfile
19+
20+
from diffusers.utils.testing_utils import TemporaryDirectory
2021

2122

2223
sys.path.append("..")
@@ -32,7 +33,7 @@
3233

3334
class ControlNet(ExamplesTestsAccelerate):
3435
def test_controlnet_checkpointing_checkpoints_total_limit(self):
35-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
36+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
3637
test_args = f"""
3738
examples/controlnet/train_controlnet.py
3839
--pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe
@@ -55,7 +56,7 @@ def test_controlnet_checkpointing_checkpoints_total_limit(self):
5556
)
5657

5758
def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):
58-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
59+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
5960
test_args = f"""
6061
examples/controlnet/train_controlnet.py
6162
--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
9899

99100
class ControlNetSDXL(ExamplesTestsAccelerate):
100101
def test_controlnet_sdxl(self):
101-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
102+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
102103
test_args = f"""
103104
examples/controlnet/train_controlnet_sdxl.py
104105
--pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe
@@ -119,7 +120,7 @@ def test_controlnet_sdxl(self):
119120

120121
class ControlNetSD3(ExamplesTestsAccelerate):
121122
def test_controlnet_sd3(self):
122-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
123+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
123124
test_args = f"""
124125
examples/controlnet/train_controlnet_sd3.py
125126
--pretrained_model_name_or_path=DavyMorgan/tiny-sd3-pipe
@@ -140,7 +141,7 @@ def test_controlnet_sd3(self):
140141

141142
class ControlNetSD35(ExamplesTestsAccelerate):
142143
def test_controlnet_sd3(self):
143-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
144+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
144145
test_args = f"""
145146
examples/controlnet/train_controlnet_sd3.py
146147
--pretrained_model_name_or_path=hf-internal-testing/tiny-sd35-pipe
@@ -161,7 +162,7 @@ def test_controlnet_sd3(self):
161162

162163
class ControlNetflux(ExamplesTestsAccelerate):
163164
def test_controlnet_flux(self):
164-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
165+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
165166
test_args = f"""
166167
examples/controlnet/train_controlnet_flux.py
167168
--pretrained_model_name_or_path=hf-internal-testing/tiny-flux-pipe

examples/custom_diffusion/test_custom_diffusion.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import logging
1717
import os
1818
import sys
19-
import tempfile
19+
20+
from diffusers.utils.testing_utils import TemporaryDirectory
2021

2122

2223
sys.path.append("..")
@@ -32,7 +33,7 @@
3233

3334
class CustomDiffusion(ExamplesTestsAccelerate):
3435
def test_custom_diffusion(self):
35-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
36+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
3637
test_args = f"""
3738
examples/custom_diffusion/train_custom_diffusion.py
3839
--pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe
@@ -57,7 +58,7 @@ def test_custom_diffusion(self):
5758
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "<new1>.bin")))
5859

5960
def test_custom_diffusion_checkpointing_checkpoints_total_limit(self):
60-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
61+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
6162
test_args = f"""
6263
examples/custom_diffusion/train_custom_diffusion.py
6364
--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):
7980
self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"})
8081

8182
def test_custom_diffusion_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):
82-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
83+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
8384
test_args = f"""
8485
examples/custom_diffusion/train_custom_diffusion.py
8586
--pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe

examples/dreambooth/test_dreambooth.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import os
1818
import shutil
1919
import sys
20-
import tempfile
2120

2221
from diffusers import DiffusionPipeline, UNet2DConditionModel
22+
from diffusers.utils.testing_utils import TemporaryDirectory
2323

2424

2525
sys.path.append("..")
@@ -35,7 +35,7 @@
3535

3636
class DreamBooth(ExamplesTestsAccelerate):
3737
def test_dreambooth(self):
38-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
38+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
3939
test_args = f"""
4040
examples/dreambooth/train_dreambooth.py
4141
--pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-pipe
@@ -58,7 +58,7 @@ def test_dreambooth(self):
5858
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json")))
5959

6060
def test_dreambooth_if(self):
61-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
61+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
6262
test_args = f"""
6363
examples/dreambooth/train_dreambooth.py
6464
--pretrained_model_name_or_path hf-internal-testing/tiny-if-pipe
@@ -87,7 +87,7 @@ def test_dreambooth_checkpointing(self):
8787
instance_prompt = "photo"
8888
pretrained_model_name_or_path = "hf-internal-testing/tiny-stable-diffusion-pipe"
8989

90-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
90+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
9191
# Run training script with checkpointing
9292
# max_train_steps == 4, checkpointing_steps == 2
9393
# Should create checkpoints at steps 2, 4
@@ -163,7 +163,7 @@ def test_dreambooth_checkpointing(self):
163163
self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6")))
164164

165165
def test_dreambooth_checkpointing_checkpoints_total_limit(self):
166-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
166+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
167167
test_args = f"""
168168
examples/dreambooth/train_dreambooth.py
169169
--pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe
@@ -186,7 +186,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self):
186186
)
187187

188188
def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):
189-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
189+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
190190
test_args = f"""
191191
examples/dreambooth/train_dreambooth.py
192192
--pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe

examples/dreambooth/test_dreambooth_flux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import os
1818
import shutil
1919
import sys
20-
import tempfile
2120

2221
from diffusers import DiffusionPipeline, FluxTransformer2DModel
22+
from diffusers.utils.testing_utils import TemporaryDirectory
2323

2424

2525
sys.path.append("..")
@@ -40,7 +40,7 @@ class DreamBoothFlux(ExamplesTestsAccelerate):
4040
script_path = "examples/dreambooth/train_dreambooth_flux.py"
4141

4242
def test_dreambooth(self):
43-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
43+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
4444
test_args = f"""
4545
{self.script_path}
4646
--pretrained_model_name_or_path {self.pretrained_model_name_or_path}
@@ -63,7 +63,7 @@ def test_dreambooth(self):
6363
self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json")))
6464

6565
def test_dreambooth_checkpointing(self):
66-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
66+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
6767
# Run training script with checkpointing
6868
# max_train_steps == 4, checkpointing_steps == 2
6969
# Should create checkpoints at steps 2, 4
@@ -139,7 +139,7 @@ def test_dreambooth_checkpointing(self):
139139
self.assertTrue(os.path.isdir(os.path.join(tmpdir, "checkpoint-6")))
140140

141141
def test_dreambooth_checkpointing_checkpoints_total_limit(self):
142-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
142+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
143143
test_args = f"""
144144
{self.script_path}
145145
--pretrained_model_name_or_path={self.pretrained_model_name_or_path}
@@ -162,7 +162,7 @@ def test_dreambooth_checkpointing_checkpoints_total_limit(self):
162162
)
163163

164164
def test_dreambooth_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):
165-
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
165+
with TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
166166
test_args = f"""
167167
{self.script_path}
168168
--pretrained_model_name_or_path={self.pretrained_model_name_or_path}

0 commit comments

Comments
 (0)