Skip to content

Commit 8577a14

Browse files
committed
update
1 parent fed282b commit 8577a14

15 files changed

+123
-65
lines changed

src/diffusers/utils/testing_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import gc
32
import importlib
43
import importlib.metadata
54
import inspect

tests/pipelines/cogvideo/test_cogvideox_image2video.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import gc
1516
import inspect
1617
import unittest
1718

@@ -23,8 +24,8 @@
2324
from diffusers import AutoencoderKLCogVideoX, CogVideoXImageToVideoPipeline, CogVideoXTransformer3DModel, DDIMScheduler
2425
from diffusers.utils import load_image
2526
from diffusers.utils.testing_utils import (
27+
backend_empty_cache,
2628
enable_full_determinism,
27-
flush_memory,
2829
numpy_cosine_similarity_distance,
2930
require_torch_accelerator,
3031
slow,
@@ -350,11 +351,13 @@ class CogVideoXImageToVideoPipelineIntegrationTests(unittest.TestCase):
350351

351352
def setUp(self):
352353
super().setUp()
353-
flush_memory(torch_device, gc_collect=True)
354+
gc.collect()
355+
backend_empty_cache(torch_device)
354356

355357
def tearDown(self):
356358
super().tearDown()
357-
flush_memory(torch_device, gc_collect=True)
359+
gc.collect()
360+
backend_empty_cache(torch_device)
358361

359362
def test_cogvideox(self):
360363
generator = torch.Generator("cpu").manual_seed(0)

tests/pipelines/controlnet/test_controlnet.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import tempfile
1718
import traceback
1819
import unittest
@@ -33,8 +34,8 @@
3334
from diffusers.pipelines.controlnet.pipeline_controlnet import MultiControlNetModel
3435
from diffusers.utils.import_utils import is_xformers_available
3536
from diffusers.utils.testing_utils import (
37+
backend_empty_cache,
3638
enable_full_determinism,
37-
flush_memory,
3839
get_python_version,
3940
is_torch_compile,
4041
load_image,
@@ -703,11 +704,13 @@ def test_save_pretrained_raise_not_implemented_exception(self):
703704
class ControlNetPipelineSlowTests(unittest.TestCase):
704705
def setUp(self):
705706
super().setUp()
706-
flush_memory(torch_device, gc_collect=True)
707+
gc.collect()
708+
backend_empty_cache(torch_device)
707709

708710
def tearDown(self):
709711
super().tearDown()
710-
flush_memory(torch_device, gc_collect=True)
712+
gc.collect()
713+
backend_empty_cache(torch_device)
711714

712715
def test_canny(self):
713716
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny")
@@ -1066,11 +1069,13 @@ def test_v11_shuffle_global_pool_conditions(self):
10661069
class StableDiffusionMultiControlNetPipelineSlowTests(unittest.TestCase):
10671070
def setUp(self):
10681071
super().setUp()
1069-
flush_memory(torch_device, gc_collect=True)
1072+
gc.collect()
1073+
backend_empty_cache(torch_device)
10701074

10711075
def tearDown(self):
10721076
super().tearDown()
1073-
flush_memory(torch_device, gc_collect=True)
1077+
gc.collect()
1078+
backend_empty_cache(torch_device)
10741079

10751080
def test_pose_and_canny(self):
10761081
controlnet_canny = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny")

tests/pipelines/controlnet/test_controlnet_sdxl.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import copy
17+
import gc
1718
import unittest
1819

1920
import numpy as np
@@ -34,8 +35,8 @@
3435
from diffusers.pipelines.controlnet.pipeline_controlnet import MultiControlNetModel
3536
from diffusers.utils.import_utils import is_xformers_available
3637
from diffusers.utils.testing_utils import (
38+
backend_empty_cache,
3739
enable_full_determinism,
38-
flush_memory,
3940
load_image,
4041
require_torch_accelerator,
4142
slow,
@@ -893,11 +894,13 @@ def test_negative_conditions(self):
893894
class ControlNetSDXLPipelineSlowTests(unittest.TestCase):
894895
def setUp(self):
895896
super().setUp()
896-
flush_memory(torch_device, gc_collect=True)
897+
gc.collect()
898+
backend_empty_cache(torch_device)
897899

898900
def tearDown(self):
899901
super().tearDown()
900-
flush_memory(torch_device, gc_collect=True)
902+
gc.collect()
903+
backend_empty_cache(torch_device)
901904

902905
def test_canny(self):
903906
controlnet = ControlNetModel.from_pretrained("diffusers/controlnet-canny-sdxl-1.0")

tests/pipelines/controlnet_hunyuandit/test_controlnet_hunyuandit.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import unittest
1718

1819
import numpy as np
@@ -28,8 +29,8 @@
2829
from diffusers.models import HunyuanDiT2DControlNetModel, HunyuanDiT2DMultiControlNetModel
2930
from diffusers.utils import load_image
3031
from diffusers.utils.testing_utils import (
32+
backend_empty_cache,
3133
enable_full_determinism,
32-
flush_memory,
3334
require_torch_accelerator,
3435
slow,
3536
torch_device,
@@ -184,11 +185,13 @@ class HunyuanDiTControlNetPipelineSlowTests(unittest.TestCase):
184185

185186
def setUp(self):
186187
super().setUp()
187-
flush_memory(torch_device, gc_collect=True)
188+
gc.collect()
189+
backend_empty_cache(torch_device)
188190

189191
def tearDown(self):
190192
super().tearDown()
191-
flush_memory(torch_device, gc_collect=True)
193+
gc.collect()
194+
backend_empty_cache(torch_device)
192195

193196
def test_canny(self):
194197
controlnet = HunyuanDiT2DControlNetModel.from_pretrained(

tests/pipelines/controlnet_xs/test_controlnetxs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import traceback
1718
import unittest
1819

@@ -33,8 +34,8 @@
3334
)
3435
from diffusers.utils.import_utils import is_xformers_available
3536
from diffusers.utils.testing_utils import (
37+
backend_empty_cache,
3638
enable_full_determinism,
37-
flush_memory,
3839
is_torch_compile,
3940
load_image,
4041
load_numpy,
@@ -338,7 +339,8 @@ def test_to_device(self):
338339
class ControlNetXSPipelineSlowTests(unittest.TestCase):
339340
def tearDown(self):
340341
super().tearDown()
341-
flush_memory(torch_device, gc_collect=True)
342+
gc.collect()
343+
backend_empty_cache(torch_device)
342344

343345
def test_canny(self):
344346
controlnet = ControlNetXSAdapter.from_pretrained(

tests/pipelines/controlnet_xs/test_controlnetxs_sdxl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import unittest
1718

1819
import numpy as np
@@ -31,8 +32,8 @@
3132
)
3233
from diffusers.utils.import_utils import is_xformers_available
3334
from diffusers.utils.testing_utils import (
35+
backend_empty_cache,
3436
enable_full_determinism,
35-
flush_memory,
3637
load_image,
3738
require_torch_accelerator,
3839
slow,
@@ -379,7 +380,8 @@ def test_multi_vae(self):
379380
class StableDiffusionXLControlNetXSPipelineSlowTests(unittest.TestCase):
380381
def tearDown(self):
381382
super().tearDown()
382-
flush_memory(torch_device, gc_collect=True)
383+
gc.collect()
384+
backend_empty_cache(torch_device)
383385

384386
def test_canny(self):
385387
controlnet = ControlNetXSAdapter.from_pretrained(

tests/pipelines/deepfloyd_if/test_if.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import unittest
1718

1819
import torch
@@ -23,7 +24,9 @@
2324
from diffusers.models.attention_processor import AttnAddedKVProcessor
2425
from diffusers.utils.import_utils import is_xformers_available
2526
from diffusers.utils.testing_utils import (
26-
flush_memory,
27+
backend_empty_cache,
28+
backend_reset_max_memory_allocated,
29+
backend_reset_peak_memory_stats,
2730
load_numpy,
2831
require_accelerator,
2932
require_torch_accelerator,
@@ -96,19 +99,23 @@ class IFPipelineSlowTests(unittest.TestCase):
9699
def setUp(self):
97100
# clean up the VRAM before each test
98101
super().setUp()
99-
flush_memory(torch_device, gc_collect=True)
102+
gc.collect()
103+
backend_empty_cache(torch_device)
100104

101105
def tearDown(self):
102106
# clean up the VRAM after each test
103107
super().tearDown()
104-
flush_memory(torch_device, gc_collect=True)
108+
gc.collect()
109+
backend_empty_cache(torch_device)
105110

106111
def test_if_text_to_image(self):
107112
pipe = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0", variant="fp16", torch_dtype=torch.float16)
108113
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
109114
pipe.enable_model_cpu_offload(device=torch_device)
110115

111-
flush_memory(torch_device, reset_mem_stats=True)
116+
backend_reset_max_memory_allocated(torch_device)
117+
backend_empty_cache(torch_device)
118+
backend_reset_peak_memory_stats(torch_device)
112119

113120
generator = torch.Generator(device="cpu").manual_seed(0)
114121
output = pipe(

tests/pipelines/deepfloyd_if/test_if_img2img.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
from diffusers.models.attention_processor import AttnAddedKVProcessor
2424
from diffusers.utils.import_utils import is_xformers_available
2525
from diffusers.utils.testing_utils import (
26+
backend_empty_cache,
27+
backend_reset_max_memory_allocated,
28+
backend_reset_peak_memory_stats,
2629
floats_tensor,
27-
flush_memory,
2830
load_numpy,
2931
require_accelerator,
3032
require_torch_accelerator,
@@ -109,13 +111,13 @@ def setUp(self):
109111
# clean up the VRAM before each test
110112
super().setUp()
111113
gc.collect()
112-
torch.cuda.empty_cache()
114+
backend_empty_cache(torch_device)
113115

114116
def tearDown(self):
115117
# clean up the VRAM after each test
116118
super().tearDown()
117119
gc.collect()
118-
torch.cuda.empty_cache()
120+
backend_empty_cache(torch_device)
119121

120122
def test_if_img2img(self):
121123
pipe = IFImg2ImgPipeline.from_pretrained(
@@ -126,7 +128,9 @@ def test_if_img2img(self):
126128
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
127129
pipe.enable_model_cpu_offload(device=torch_device)
128130

129-
flush_memory(torch_device, reset_mem_stats=True)
131+
backend_reset_max_memory_allocated(torch_device)
132+
backend_empty_cache(torch_device)
133+
backend_reset_peak_memory_stats(torch_device)
130134

131135
image = floats_tensor((1, 3, 64, 64), rng=random.Random(0)).to(torch_device)
132136
generator = torch.Generator(device="cpu").manual_seed(0)

tests/pipelines/deepfloyd_if/test_if_img2img_superresolution.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import gc
1617
import random
1718
import unittest
1819

@@ -22,8 +23,11 @@
2223
from diffusers.models.attention_processor import AttnAddedKVProcessor
2324
from diffusers.utils.import_utils import is_xformers_available
2425
from diffusers.utils.testing_utils import (
26+
backend_empty_cache,
27+
backend_max_memory_allocated,
28+
backend_reset_max_memory_allocated,
29+
backend_reset_peak_memory_stats,
2530
floats_tensor,
26-
flush_memory,
2731
load_numpy,
2832
require_accelerator,
2933
require_torch_accelerator,
@@ -104,12 +108,14 @@ class IFImg2ImgSuperResolutionPipelineSlowTests(unittest.TestCase):
104108
def setUp(self):
105109
# clean up the VRAM before each test
106110
super().setUp()
107-
flush_memory(torch_device, gc_collect=True)
111+
gc.collect()
112+
backend_empty_cache(torch_device)
108113

109114
def tearDown(self):
110115
# clean up the VRAM after each test
111116
super().tearDown()
112-
flush_memory(torch_device, gc_collect=True)
117+
gc.collect()
118+
backend_empty_cache(torch_device)
113119

114120
def test_if_img2img_superresolution(self):
115121
pipe = IFImg2ImgSuperResolutionPipeline.from_pretrained(
@@ -120,7 +126,9 @@ def test_if_img2img_superresolution(self):
120126
pipe.unet.set_attn_processor(AttnAddedKVProcessor())
121127
pipe.enable_model_cpu_offload(device=torch_device)
122128

123-
flush_memory(torch_device, reset_mem_stats=True)
129+
backend_reset_max_memory_allocated(torch_device)
130+
backend_empty_cache(torch_device)
131+
backend_reset_peak_memory_stats(torch_device)
124132

125133
generator = torch.Generator(device="cpu").manual_seed(0)
126134

@@ -140,10 +148,7 @@ def test_if_img2img_superresolution(self):
140148

141149
assert image.shape == (256, 256, 3)
142150

143-
if torch_device == "cuda":
144-
mem_bytes = torch.cuda.max_memory_allocated()
145-
elif torch_device == "xpu":
146-
mem_bytes = torch.xpu.max_memory_allocated()
151+
mem_bytes = backend_max_memory_allocated(torch_device)
147152

148153
assert mem_bytes < 12 * 10**9
149154

0 commit comments

Comments
 (0)