From 279f1fad907dd53044a7b68fcc45bd6a24467c9e Mon Sep 17 00:00:00 2001 From: Mick Date: Mon, 12 Oct 2026 21:09:41 +0800 Subject: [PATCH] [diffusion] chore: bind worker CPUs to the GPU's NUMA node when memory policy is not permitted (#43756) Co-authored-by: Mick Qian Co-authored-by: Claude Opus 5.5 --- .../runtime/managers/gpu_worker.py | 4 +- .../test/unit/test_gpu_worker_cpu_threads.py | 22 +++++++++++ python/sglang/srt/utils/numa_utils.py | 23 ++++++++++-- test/registered/utils/test_numa_utils.py | 37 +++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py b/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py index ca216a6073..8185017273 100644 --- a/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py +++ b/python/sglang/multimodal_gen/runtime/managers/gpu_worker.py @@ -189,7 +189,9 @@ def _bind_worker_to_gpu_numa_node( numa_bind_to_node, ) - numa_node = get_numa_node_if_available(server_args, local_rank) + # Pods without SYS_NICE cannot set memory policy; CPU affinity alone still + # removes the cross-socket drift (FLUX.1 TP2 on GB300: spread 26-60% -> 6-8%). + numa_node = get_numa_node_if_available(server_args, local_rank, cpu_only_ok=True) if numa_node is not None: numa_bind_to_node(numa_node) logger.info("GPU %d worker bound to NUMA node %d", local_rank, numa_node) diff --git a/python/sglang/multimodal_gen/test/unit/test_gpu_worker_cpu_threads.py b/python/sglang/multimodal_gen/test/unit/test_gpu_worker_cpu_threads.py index d04dbf4f61..976f489216 100644 --- a/python/sglang/multimodal_gen/test/unit/test_gpu_worker_cpu_threads.py +++ b/python/sglang/multimodal_gen/test/unit/test_gpu_worker_cpu_threads.py @@ -66,6 +66,28 @@ class TestWorkerNumaBinding(unittest.TestCase): self.assertEqual(node, 1) bind.assert_called_once_with(1) + def test_cpu_affinity_still_binds_without_mempolicy(self): + with ( + patch.dict("os.environ", {"SGLANG_AUTO_NUMA_BIND": "1"}), + patch("sglang.srt.utils.numa_utils._is_cuda", True), + patch("os.path.isdir", return_value=True), + patch("sglang.srt.utils.numa_utils._can_set_mempolicy", return_value=False), + patch( + "sglang.srt.utils.numa_utils.shutil.which", + return_value="/usr/bin/numactl", + ), + patch( + "sglang.srt.utils.numa_utils._query_numa_node_for_gpu", + return_value=[1], + ), + patch("sglang.srt.utils.numa_utils.numa_bind_to_node") as bind, + ): + node = _bind_worker_to_gpu_numa_node( + SimpleNamespace(numa_node=None), local_rank=1 + ) + self.assertEqual(node, 1) + bind.assert_called_once_with(1) + def test_disabled_auto_bind_leaves_worker_unbound(self): with ( patch.dict("os.environ", {"SGLANG_AUTO_NUMA_BIND": "0"}), diff --git a/python/sglang/srt/utils/numa_utils.py b/python/sglang/srt/utils/numa_utils.py index 5cb2dfa85a..51814ec862 100644 --- a/python/sglang/srt/utils/numa_utils.py +++ b/python/sglang/srt/utils/numa_utils.py @@ -122,7 +122,9 @@ def _get_nvml_device_index(device_id: int) -> int: return get_nvml_device_index(device_id) -def get_numa_node_if_available(server_args: ServerArgs, gpu_id: int) -> Optional[int]: +def get_numa_node_if_available( + server_args: ServerArgs, gpu_id: int, cpu_only_ok: bool = False +) -> Optional[int]: """ Returns the NUMA node for the given GPU id. If it is not set in the server_args, it will try to query the NUMA node for the GPU. If the NUMA node is not available, has already been configured externally, or the user lacks permission to set NUMA affinity, it will return None. @@ -130,6 +132,8 @@ def get_numa_node_if_available(server_args: ServerArgs, gpu_id: int) -> Optional Args: server_args: The server arguments. gpu_id: The GPU id. + cpu_only_ok: Also return the node when NUMA memory policy is not + permitted, so the caller can still bind CPUs (no privilege needed). Returns: The NUMA node for the given GPU id or None if it is not available. @@ -138,7 +142,7 @@ def get_numa_node_if_available(server_args: ServerArgs, gpu_id: int) -> Optional return server_args.numa_node[gpu_id] if not envs.SGLANG_AUTO_NUMA_BIND.get(): return None - if _is_numa_available(): + if _is_numa_available(require_mempolicy=not cpu_only_ok): queried_numa_node = _query_numa_node_for_gpu(gpu_id) if len(queried_numa_node) == 0: return None @@ -183,7 +187,10 @@ def numa_bind_to_node(node: int): os.sched_setaffinity(0, target_cpus) else: libnuma.numa_run_on_node(ctypes.c_int(node)) - libnuma.numa_set_preferred(ctypes.c_int(node)) + # Without memory-policy permission, CPU affinity alone keeps first-touch + # allocations on the node. + if _can_set_mempolicy(): + libnuma.numa_set_preferred(ctypes.c_int(node)) class _Bitmask(ctypes.Structure): @@ -359,9 +366,11 @@ def _can_set_mempolicy() -> bool: return False -def _is_numa_available() -> bool: +def _is_numa_available(require_mempolicy: bool = True) -> bool: """ Check if NUMA is available and not already configured externally. + ``require_mempolicy=False`` accepts a host where only CPU affinity can be set + (a container without SYS_NICE). """ if not (_is_cuda or _is_xpu): return False @@ -377,6 +386,12 @@ def _is_numa_available() -> bool: return False if not _can_set_mempolicy(): + if not require_mempolicy: + logger.info( + "User lacks permission to set NUMA memory policy; binding CPUs only. " + "If using docker, --cap-add SYS_NICE also binds host memory." + ) + return True logger.warning( "User lacks permission to set NUMA affinity, skipping NUMA node configuration for GPU. If using docker, try adding --cap-add SYS_NICE to your docker run command." ) diff --git a/test/registered/utils/test_numa_utils.py b/test/registered/utils/test_numa_utils.py index d3d8c42f1f..3db9d8abd3 100644 --- a/test/registered/utils/test_numa_utils.py +++ b/test/registered/utils/test_numa_utils.py @@ -57,6 +57,42 @@ class TestIsNumaAvailable(unittest.TestCase): ): self.assertFalse(_is_numa_available()) + @patch("sglang.srt.utils.numa_utils._can_set_mempolicy", return_value=False) + @patch("sglang.srt.utils.numa_utils.shutil.which", return_value="/usr/bin/numactl") + @patch("sglang.srt.utils.numa_utils._is_cuda", True) + @patch("os.path.isdir", return_value=True) + def test_cpu_only_accepted_when_mempolicy_not_permitted( + self, _mock_isdir, _mock_which, _mock_mempolicy + ): + self.assertTrue(_is_numa_available(require_mempolicy=False)) + + +class TestNumaBindWithoutMempolicy(unittest.TestCase): + """A container without SYS_NICE can set CPU affinity but not memory policy.""" + + def _bind(self, mempolicy_ok: bool) -> MagicMock: + libnuma = MagicMock() + libnuma.numa_available.return_value = 0 + with ( + patch("sglang.srt.utils.numa_utils.get_libnuma", return_value=libnuma), + patch("sglang.srt.utils.numa_utils._node_cpus", return_value={0, 1}), + patch("os.sched_getaffinity", return_value={0, 1, 2, 3}), + patch("os.sched_setaffinity") as setaffinity, + patch( + "sglang.srt.utils.numa_utils._can_set_mempolicy", + return_value=mempolicy_ok, + ), + ): + numa_bind_to_node(0) + setaffinity.assert_called_once_with(0, {0, 1}) + return libnuma + + def test_cpus_bound_and_memory_policy_skipped(self): + self._bind(mempolicy_ok=False).numa_set_preferred.assert_not_called() + + def test_memory_policy_set_when_permitted(self): + self._bind(mempolicy_ok=True).numa_set_preferred.assert_called_once() + # Pin _is_xpu=False so these cases still reach the mocked pynvml on a real XPU # host, where _query_numa_node_for_gpu short-circuits into the XPU sysfs branch. @@ -346,6 +382,7 @@ class TestNumaBindIntersection(unittest.TestCase): ): lib = MagicMock() lib.numa_available.return_value = 0 + lib.get_mempolicy.return_value = 0 mock_libnuma.return_value = lib numa_bind_to_node(0)