From 9b7b9bdfe5a838f7546593b7439f8d0def942fb9 Mon Sep 17 00:00:00 2001 From: charchit7 Date: Thu, 22 Oct 2026 16:39:46 +0530 Subject: [PATCH 1/2] [core] accumulate CPU-offloaded components in balanced device map instead of overwriting --- .../pipelines/pipeline_loading_utils.py | 5 ++- tests/pipelines/test_pipeline_utils.py | 40 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) 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..72c5e25e5243 100644 --- a/tests/pipelines/test_pipeline_utils.py +++ b/tests/pipelines/test_pipeline_utils.py @@ -24,12 +24,50 @@ StableDiffusionPipeline, UNet2DConditionModel, ) -from diffusers.pipelines.pipeline_loading_utils import is_safetensors_compatible, variant_compatible_siblings +from diffusers.pipelines.pipeline_loading_utils import ( + _assign_components_to_devices, + 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 TestAssignComponentsToDevices: + @staticmethod + def _flatten(mapping): + return [component for components in mapping.values() for component in components] + + def test_components_placed_on_devices_when_they_fit(self): + module_sizes = {"transformer": 10.0, "text_encoder": 5.0, "vae": 1.0} + device_memory = {0: 20.0, 1: 20.0} + + mapping = _assign_components_to_devices(module_sizes, device_memory) + + assert "cpu" not in mapping + assert sorted(self._flatten(mapping)) == sorted(module_sizes) + + def test_single_oversized_component_placed_on_cpu(self): + module_sizes = {"transformer": 30.0, "text_encoder": 5.0, "vae": 1.0} + device_memory = {0: 20.0, 1: 20.0} + + mapping = _assign_components_to_devices(module_sizes, device_memory) + + assert mapping["cpu"] == ["transformer"] + assert sorted(self._flatten(mapping)) == sorted(module_sizes) + + def test_multiple_oversized_components_all_placed_on_cpu(self): + # Every component that does not fit on any device must be listed under "cpu". + module_sizes = {"transformer": 30.0, "text_encoder": 25.0, "vae": 1.0} + device_memory = {0: 20.0, 1: 20.0} + + mapping = _assign_components_to_devices(module_sizes, device_memory) + + assert mapping["cpu"] == ["transformer", "text_encoder"] + assert sorted(self._flatten(mapping)) == sorted(module_sizes) + + class TestIsSafetensorsCompatible: def test_all_is_compatible(self): filenames = [ From 1a0657b81d273cab01702928e8d75a488d38af4f Mon Sep 17 00:00:00 2001 From: charchit7 Date: Mon, 16 Nov 2026 14:23:18 +0530 Subject: [PATCH 2/2] [tests] test balanced device map overflow through from_pretrained --- tests/pipelines/test_pipeline_utils.py | 47 +++++++++----------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/tests/pipelines/test_pipeline_utils.py b/tests/pipelines/test_pipeline_utils.py index 72c5e25e5243..a9ff1dc1355f 100644 --- a/tests/pipelines/test_pipeline_utils.py +++ b/tests/pipelines/test_pipeline_utils.py @@ -25,7 +25,6 @@ UNet2DConditionModel, ) from diffusers.pipelines.pipeline_loading_utils import ( - _assign_components_to_devices, is_safetensors_compatible, variant_compatible_siblings, ) @@ -34,38 +33,24 @@ from ..testing_utils import require_torch_accelerator, torch_device -class TestAssignComponentsToDevices: - @staticmethod - def _flatten(mapping): - return [component for components in mapping.values() for component in components] - - def test_components_placed_on_devices_when_they_fit(self): - module_sizes = {"transformer": 10.0, "text_encoder": 5.0, "vae": 1.0} - device_memory = {0: 20.0, 1: 20.0} - - mapping = _assign_components_to_devices(module_sizes, device_memory) - - assert "cpu" not in mapping - assert sorted(self._flatten(mapping)) == sorted(module_sizes) - - def test_single_oversized_component_placed_on_cpu(self): - module_sizes = {"transformer": 30.0, "text_encoder": 5.0, "vae": 1.0} - device_memory = {0: 20.0, 1: 20.0} - - mapping = _assign_components_to_devices(module_sizes, device_memory) - - assert mapping["cpu"] == ["transformer"] - assert sorted(self._flatten(mapping)) == sorted(module_sizes) - - def test_multiple_oversized_components_all_placed_on_cpu(self): - # Every component that does not fit on any device must be listed under "cpu". - module_sizes = {"transformer": 30.0, "text_encoder": 25.0, "vae": 1.0} - device_memory = {0: 20.0, 1: 20.0} +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, + ) - mapping = _assign_components_to_devices(module_sizes, device_memory) + assert pipe.hf_device_map["unet"] == "cpu" + assert pipe.hf_device_map["vae"] == "cpu" + assert pipe.hf_device_map["text_encoder"] == device - assert mapping["cpu"] == ["transformer", "text_encoder"] - assert sorted(self._flatten(mapping)) == sorted(module_sizes) + image = pipe("foo", num_inference_steps=2, output_type="np").images + assert image.shape == (1, 128, 128, 3) class TestIsSafetensorsCompatible: