diff --git a/src/diffusers/pipelines/pipeline_loading_utils.py b/src/diffusers/pipelines/pipeline_loading_utils.py index 681c6f386aca..91b176775423 100644 --- a/src/diffusers/pipelines/pipeline_loading_utils.py +++ b/src/diffusers/pipelines/pipeline_loading_utils.py @@ -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] diff --git a/tests/pipelines/test_pipeline_utils.py b/tests/pipelines/test_pipeline_utils.py index c62e582597fc..a9ff1dc1355f 100644 --- a/tests/pipelines/test_pipeline_utils.py +++ b/tests/pipelines/test_pipeline_utils.py @@ -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 = [