From cd8ed1bb551f1e649db1291645423c7eda37c2b7 Mon Sep 17 00:00:00 2001 From: adenzhou1350 Date: Mon, 12 Oct 2026 11:34:58 +0800 Subject: [PATCH] [Bugfix] Preserve available host memory for integrated MUSA devices (#43398) Signed-off-by: Xucheng Zhou --- python/sglang/srt/utils/common.py | 3 +- .../unit/platforms/test_platform_interface.py | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index e14043ed2d..31e7e9f8a1 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -540,7 +540,8 @@ def get_available_gpu_memory( # available due to not including cache memory. So we use the system available # memory metric instead. free_gpu_memory = psutil.virtual_memory().available - free_gpu_memory, total_gpu_memory = torch.musa.mem_get_info() + else: + free_gpu_memory, _ = torch.musa.mem_get_info(gpu_id) elif device == "mps": from sglang.srt.hardware_backend.mlx.runtime import use_mlx diff --git a/test/registered/unit/platforms/test_platform_interface.py b/test/registered/unit/platforms/test_platform_interface.py index 4355b466dd..35249d37ba 100644 --- a/test/registered/unit/platforms/test_platform_interface.py +++ b/test/registered/unit/platforms/test_platform_interface.py @@ -543,6 +543,42 @@ class TestMpsDeviceMixin(CustomTestCase): common.get_device.cache_clear() +class TestMusaAvailableMemory(CustomTestCase): + def test_integrated_and_discrete_memory_sources(self): + from sglang.srt.utils import common + + gpu_id = 3 + for integrated in (True, False): + for empty_cache in (True, False): + with self.subTest(integrated=integrated, empty_cache=empty_cache): + musa = MagicMock() + musa.device_count.return_value = 4 + musa.current_device.return_value = gpu_id + musa.get_device_properties.return_value.is_integrated = integrated + musa.mem_get_info.return_value = (2 << 30, 16 << 30) + with ( + patch.object(torch, "musa", musa, create=True), + patch.object(common.psutil, "virtual_memory") as host_memory, + patch.object(common, "empty_device_cache") as clear_cache, + ): + host_memory.return_value.available = 6 << 30 + self.assertEqual( + common.get_available_gpu_memory( + "musa", gpu_id, empty_cache=empty_cache + ), + 6.0 if integrated else 2.0, + ) + if integrated: + musa.mem_get_info.assert_not_called() + else: + musa.mem_get_info.assert_called_once_with(gpu_id) + host_memory.assert_not_called() + if empty_cache: + clear_cache.assert_called_once_with(musa) + else: + clear_cache.assert_not_called() + + class TestPinMemoryAvailability(CustomTestCase): """Tests for common pin-memory helper dispatch through platforms."""