Skip to content

Commit a75ac3f

Browse files
authored
Sequential cpu offload: require accelerate 0.14.0 (#2517)
* Sequential cpu offload: require accelerate 0.14.0. * Import is_accelerate_version * Missing copy.
1 parent 477aaa9 commit a75ac3f

15 files changed

+44
-35
lines changed

src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
191191
Note that offloading happens on a submodule basis. Memory savings are higher than with
192192
`enable_model_cpu_offload`, but performance is lower.
193193
"""
194-
if is_accelerate_available():
194+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
195195
from accelerate import cpu_offload
196196
else:
197-
raise ImportError("Please install accelerate via `pip install accelerate`")
197+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
198198

199199
device = torch.device(f"cuda:{gpu_id}")
200200

src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
213213
Note that offloading happens on a submodule basis. Memory savings are higher than with
214214
`enable_model_cpu_offload`, but performance is lower.
215215
"""
216-
if is_accelerate_available():
216+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
217217
from accelerate import cpu_offload
218218
else:
219-
raise ImportError("Please install accelerate via `pip install accelerate`")
219+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
220220

221221
device = torch.device(f"cuda:{gpu_id}")
222222

src/diffusers/pipelines/stable_diffusion/pipeline_cycle_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
230230
Note that offloading happens on a submodule basis. Memory savings are higher than with
231231
`enable_model_cpu_offload`, but performance is lower.
232232
"""
233-
if is_accelerate_available():
233+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
234234
from accelerate import cpu_offload
235235
else:
236-
raise ImportError("Please install accelerate via `pip install accelerate`")
236+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
237237

238238
device = torch.device(f"cuda:{gpu_id}")
239239

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
194194
Note that offloading happens on a submodule basis. Memory savings are higher than with
195195
`enable_model_cpu_offload`, but performance is lower.
196196
"""
197-
if is_accelerate_available():
197+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
198198
from accelerate import cpu_offload
199199
else:
200-
raise ImportError("Please install accelerate via `pip install accelerate`")
200+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
201201

202202
device = torch.device(f"cuda:{gpu_id}")
203203

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_attend_and_excite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from ...models import AutoencoderKL, UNet2DConditionModel
2525
from ...models.cross_attention import CrossAttention
2626
from ...schedulers import KarrasDiffusionSchedulers
27-
from ...utils import is_accelerate_available, logging, randn_tensor, replace_example_docstring
27+
from ...utils import is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring
2828
from ..pipeline_utils import DiffusionPipeline
2929
from . import StableDiffusionPipelineOutput
3030
from .safety_checker import StableDiffusionSafetyChecker
@@ -256,10 +256,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
256256
Note that offloading happens on a submodule basis. Memory savings are higher than with
257257
`enable_model_cpu_offload`, but performance is lower.
258258
"""
259-
if is_accelerate_available():
259+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
260260
from accelerate import cpu_offload
261261
else:
262-
raise ImportError("Please install accelerate via `pip install accelerate`")
262+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
263263

264264
device = torch.device(f"cuda:{gpu_id}")
265265

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
218218
Note that offloading happens on a submodule basis. Memory savings are higher than with
219219
`enable_model_cpu_offload`, but performance is lower.
220220
"""
221-
if is_accelerate_available():
221+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
222222
from accelerate import cpu_offload
223223
else:
224-
raise ImportError("Please install accelerate via `pip install accelerate`")
224+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
225225

226226
device = torch.device(f"cuda:{gpu_id}")
227227

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
265265
Note that offloading happens on a submodule basis. Memory savings are higher than with
266266
`enable_model_cpu_offload`, but performance is lower.
267267
"""
268-
if is_accelerate_available():
268+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
269269
from accelerate import cpu_offload
270270
else:
271-
raise ImportError("Please install accelerate via `pip install accelerate`")
271+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
272272

273273
device = torch.device(f"cuda:{gpu_id}")
274274

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint_legacy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
209209
Note that offloading happens on a submodule basis. Memory savings are higher than with
210210
`enable_model_cpu_offload`, but performance is lower.
211211
"""
212-
if is_accelerate_available():
212+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
213213
from accelerate import cpu_offload
214214
else:
215-
raise ImportError("Please install accelerate via `pip install accelerate`")
215+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
216216

217217
device = torch.device(f"cuda:{gpu_id}")
218218

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
398398
Note that offloading happens on a submodule basis. Memory savings are higher than with
399399
`enable_model_cpu_offload`, but performance is lower.
400400
"""
401-
if is_accelerate_available():
401+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
402402
from accelerate import cpu_offload
403403
else:
404-
raise ImportError("Please install accelerate via `pip install accelerate`")
404+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
405405

406406
device = torch.device(f"cuda:{gpu_id}")
407407

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_k_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
130130
Note that offloading happens on a submodule basis. Memory savings are higher than with
131131
`enable_model_cpu_offload`, but performance is lower.
132132
"""
133-
if is_accelerate_available():
133+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
134134
from accelerate import cpu_offload
135135
else:
136-
raise ImportError("Please install accelerate via `pip install accelerate`")
136+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
137137

138138
device = torch.device(f"cuda:{gpu_id}")
139139

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_panorama.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from ...models import AutoencoderKL, UNet2DConditionModel
2121
from ...schedulers import DDIMScheduler, PNDMScheduler
22-
from ...utils import is_accelerate_available, logging, randn_tensor, replace_example_docstring
22+
from ...utils import is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring
2323
from ..pipeline_utils import DiffusionPipeline
2424
from . import StableDiffusionPipelineOutput
2525
from .safety_checker import StableDiffusionSafetyChecker
@@ -151,10 +151,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
151151
Note that offloading happens on a submodule basis. Memory savings are higher than with
152152
`enable_model_cpu_offload`, but performance is lower.
153153
"""
154-
if is_accelerate_available():
154+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
155155
from accelerate import cpu_offload
156156
else:
157-
raise ImportError("Please install accelerate via `pip install accelerate`")
157+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
158158

159159
device = torch.device(f"cuda:{gpu_id}")
160160

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_pix2pix_zero.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
365365
Note that offloading happens on a submodule basis. Memory savings are higher than with
366366
`enable_model_cpu_offload`, but performance is lower.
367367
"""
368-
if is_accelerate_available():
368+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
369369
from accelerate import cpu_offload
370370
else:
371-
raise ImportError("Please install accelerate via `pip install accelerate`")
371+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
372372

373373
device = torch.device(f"cuda:{gpu_id}")
374374

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from ...models import AutoencoderKL, UNet2DConditionModel
2424
from ...schedulers import KarrasDiffusionSchedulers
25-
from ...utils import is_accelerate_available, logging, randn_tensor, replace_example_docstring
25+
from ...utils import is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring
2626
from ..pipeline_utils import DiffusionPipeline
2727
from . import StableDiffusionPipelineOutput
2828
from .safety_checker import StableDiffusionSafetyChecker
@@ -169,10 +169,10 @@ def enable_sequential_cpu_offload(self, gpu_id=0):
169169
Note that offloading happens on a submodule basis. Memory savings are higher than with
170170
`enable_model_cpu_offload`, but performance is lower.
171171
"""
172-
if is_accelerate_available():
172+
if is_accelerate_available() and is_accelerate_version(">=", "0.14.0"):
173173
from accelerate import cpu_offload
174174
else:
175-
raise ImportError("Please install accelerate via `pip install accelerate`")
175+
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
176176

177177
device = torch.device(f"cuda:{gpu_id}")
178178

tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@
3939
StableDiffusionDepth2ImgPipeline,
4040
UNet2DConditionModel,
4141
)
42-
from diffusers.utils import floats_tensor, is_accelerate_available, load_image, load_numpy, nightly, slow, torch_device
42+
from diffusers.utils import (
43+
floats_tensor,
44+
is_accelerate_available,
45+
is_accelerate_version,
46+
load_image,
47+
load_numpy,
48+
nightly,
49+
slow,
50+
torch_device,
51+
)
4352
from diffusers.utils.testing_utils import require_torch_gpu, skip_mps
4453

4554
from ...test_pipelines_common import PipelineTesterMixin
@@ -227,8 +236,8 @@ def test_float16_inference(self):
227236
self.assertLess(max_diff, 1.3e-2, "The outputs of the fp16 and fp32 pipelines are too different.")
228237

229238
@unittest.skipIf(
230-
torch_device != "cuda" or not is_accelerate_available(),
231-
reason="CPU offload is only available with CUDA and `accelerate` installed",
239+
torch_device != "cuda" or not is_accelerate_available() or is_accelerate_version("<", "0.14.0"),
240+
reason="CPU offload is only available with CUDA and `accelerate v0.14.0` or higher",
232241
)
233242
def test_cpu_offload_forward_pass(self):
234243
components = self.get_dummy_components()

tests/test_pipelines_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import diffusers
1414
from diffusers import DiffusionPipeline
1515
from diffusers.utils import logging
16-
from diffusers.utils.import_utils import is_accelerate_available, is_xformers_available
16+
from diffusers.utils.import_utils import is_accelerate_available, is_accelerate_version, is_xformers_available
1717
from diffusers.utils.testing_utils import require_torch, torch_device
1818

1919

@@ -417,8 +417,8 @@ def _test_attention_slicing_forward_pass(self, test_max_difference=True):
417417
assert_mean_pixel_difference(output_with_slicing[0], output_without_slicing[0])
418418

419419
@unittest.skipIf(
420-
torch_device != "cuda" or not is_accelerate_available(),
421-
reason="CPU offload is only available with CUDA and `accelerate` installed",
420+
torch_device != "cuda" or not is_accelerate_available() or is_accelerate_version("<", "0.14.0"),
421+
reason="CPU offload is only available with CUDA and `accelerate v0.14.0` or higher",
422422
)
423423
def test_cpu_offload_forward_pass(self):
424424
if not self.test_cpu_offload:

0 commit comments

Comments
 (0)