[diffusion] chore: bind worker CPUs to the GPU's NUMA node when memory policy is not permitted (#43756)

Co-authored-by: Mick Qian <mickqian@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Mick
2026-10-12 21:09:41 +08:00
committed by GitHub
co-authored by Mick Qian Claude Opus 5.5
parent d1357bf87b
commit 279f1fad90
4 changed files with 81 additions and 5 deletions
@@ -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)
@@ -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"}),
+19 -4
View File
@@ -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."
)