Skip to content

Commit

Permalink
Fix Nightly AudioLDM2PipelineFastTests (#10556)
Browse files Browse the repository at this point in the history
* Fix Nightly AudioLDM2PipelineFastTests

* add phonemizer to setup extras test

* fix

* make style
  • Loading branch information
hlky authored Jan 13, 2025
1 parent 980736b commit c3478a4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"transformers>=4.41.2",
"urllib3<=2.0.0",
"black",
"phonemizer",
]

# this is a lookup table with items like:
Expand Down Expand Up @@ -227,6 +228,7 @@ def run(self):
"scipy",
"torchvision",
"transformers",
"phonemizer",
)
extras["torch"] = deps_list("torch", "accelerate")

Expand Down
1 change: 1 addition & 0 deletions src/diffusers/dependency_versions_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
"transformers": "transformers>=4.41.2",
"urllib3": "urllib3<=2.0.0",
"black": "black",
"phonemizer": "phonemizer",
}
18 changes: 15 additions & 3 deletions src/diffusers/pipelines/audioldm2/pipeline_audioldm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def disable_vae_slicing(self):
"""
self.vae.disable_slicing()

def enable_model_cpu_offload(self, gpu_id=0):
def enable_model_cpu_offload(self, gpu_id: Optional[int] = None, device: Union[torch.device, str] = "cuda"):
r"""
Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
to `enable_sequential_cpu_offload`, this method moves one whole model at a time to the GPU when its `forward`
Expand All @@ -249,11 +249,23 @@ def enable_model_cpu_offload(self, gpu_id=0):
else:
raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.")

device = torch.device(f"cuda:{gpu_id}")
torch_device = torch.device(device)
device_index = torch_device.index

if gpu_id is not None and device_index is not None:
raise ValueError(
f"You have passed both `gpu_id`={gpu_id} and an index as part of the passed device `device`={device}"
f"Cannot pass both. Please make sure to either not define `gpu_id` or not pass the index as part of the device: `device`={torch_device.type}"
)

device_type = torch_device.type
device = torch.device(f"{device_type}:{gpu_id or torch_device.index}")

if self.device.type != "cpu":
self.to("cpu", silence_dtype_warnings=True)
torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist)
device_mod = getattr(torch, device.type, None)
if hasattr(device_mod, "empty_cache") and device_mod.is_available():
device_mod.empty_cache() # otherwise we don't see the memory savings (but they probably exist)

model_sequence = [
self.text_encoder.text_model,
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/audioldm2/test_audioldm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ def test_xformers_attention_forwardGenerator_pass(self):
pass

def test_dict_tuple_outputs_equivalent(self):
# increase tolerance from 1e-4 -> 2e-4 to account for large composite model
super().test_dict_tuple_outputs_equivalent(expected_max_difference=2e-4)
# increase tolerance from 1e-4 -> 3e-4 to account for large composite model
super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-4)

def test_inference_batch_single_identical(self):
# increase tolerance from 1e-4 -> 2e-4 to account for large composite model
Expand Down

0 comments on commit c3478a4

Please sign in to comment.