Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/diffusers/pipelines/pipeline_loading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@ def _assign_components_to_devices(

# If the GPU doesn't fit the current component offload to the CPU.
if component_memory > curr_device_memory:
device_id_component_mapping["cpu"] = [component]
if "cpu" not in device_id_component_mapping:
device_id_component_mapping["cpu"] = [component]
else:
device_id_component_mapping["cpu"].append(component)
else:
if device_id not in device_id_component_mapping:
device_id_component_mapping[device_id] = [component]
Expand Down
25 changes: 24 additions & 1 deletion tests/pipelines/test_pipeline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,35 @@
StableDiffusionPipeline,
UNet2DConditionModel,
)
from diffusers.pipelines.pipeline_loading_utils import is_safetensors_compatible, variant_compatible_siblings
from diffusers.pipelines.pipeline_loading_utils import (
is_safetensors_compatible,
variant_compatible_siblings,
)

from ..others.test_utils import TOKEN, USER, is_staging_test
from ..testing_utils import require_torch_accelerator, torch_device


class TestBalancedDeviceMap:
@require_torch_accelerator
def test_components_that_overflow_stay_in_device_map(self):
# The budget fits the text encoder only, so the unet and the vae both overflow to the CPU.
device = torch_device if torch_device == "mps" else 0
pipe = StableDiffusionPipeline.from_pretrained(
"hf-internal-testing/tiny-stable-diffusion-torch",
device_map="balanced",
max_memory={device: "300KB"},
safety_checker=None,
)

assert pipe.hf_device_map["unet"] == "cpu"
assert pipe.hf_device_map["vae"] == "cpu"
assert pipe.hf_device_map["text_encoder"] == device

image = pipe("foo", num_inference_steps=2, output_type="np").images
assert image.shape == (1, 128, 128, 3)


class TestIsSafetensorsCompatible:
def test_all_is_compatible(self):
filenames = [
Expand Down
Loading