Skip to content

Commit bac6fe9

Browse files
author
Kevin-Li-2025
committed
Fix CPU simulated NUMA env parsing
Signed-off-by: Kevin-Li-2025 <2242139@qq.com>
1 parent 6bdabba commit bac6fe9

5 files changed

Lines changed: 68 additions & 2 deletions

File tree

tests/test_envs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ def test_precompiled_install_flags_are_orthogonal() -> None:
116116
assert environment_variables["VLLM_USE_PRECOMPILED_RUST"]() is True
117117

118118

119+
@pytest.mark.parametrize(
120+
("env_value", "expected"),
121+
[
122+
(None, False),
123+
("0", False),
124+
("1", True),
125+
],
126+
)
127+
def test_cpu_sim_multi_numa_env(
128+
monkeypatch: pytest.MonkeyPatch, env_value: str | None, expected: bool
129+
) -> None:
130+
if env_value is None:
131+
monkeypatch.delenv("VLLM_CPU_SIM_MULTI_NUMA", raising=False)
132+
else:
133+
monkeypatch.setenv("VLLM_CPU_SIM_MULTI_NUMA", env_value)
134+
135+
assert envs.VLLM_CPU_SIM_MULTI_NUMA is expected
136+
137+
119138
class TestEnvWithChoices:
120139
"""Test cases for env_with_choices function."""
121140

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3+
4+
import pytest
5+
6+
from vllm.utils import cpu_resource_utils as cr_utils
7+
8+
9+
@pytest.fixture(autouse=True)
10+
def _clear_cpu_memory_envs(monkeypatch: pytest.MonkeyPatch) -> None:
11+
monkeypatch.delenv(cr_utils.DEVICE_CONTROL_ENV_VAR, raising=False)
12+
monkeypatch.delenv("VLLM_CPU_SIM_MULTI_NUMA", raising=False)
13+
14+
15+
def _stub_linux_memory_affinity(monkeypatch: pytest.MonkeyPatch) -> None:
16+
monkeypatch.setattr(cr_utils.sys, "platform", "linux")
17+
monkeypatch.setattr(cr_utils, "get_memory_affinity", lambda: [0, 1, 2, 3])
18+
19+
20+
@pytest.mark.parametrize("sim_multi_numa", [None, "0"])
21+
def test_get_visible_memory_node_uses_visible_nodes_when_not_simulating(
22+
monkeypatch: pytest.MonkeyPatch, sim_multi_numa: str | None
23+
) -> None:
24+
_stub_linux_memory_affinity(monkeypatch)
25+
monkeypatch.setenv(cr_utils.DEVICE_CONTROL_ENV_VAR, "1,3")
26+
if sim_multi_numa is not None:
27+
monkeypatch.setenv("VLLM_CPU_SIM_MULTI_NUMA", sim_multi_numa)
28+
29+
assert cr_utils.get_visible_memory_node() == [1, 3]
30+
31+
32+
def test_get_visible_memory_node_ignores_visible_nodes_when_simulating(
33+
monkeypatch: pytest.MonkeyPatch,
34+
) -> None:
35+
_stub_linux_memory_affinity(monkeypatch)
36+
monkeypatch.setenv(cr_utils.DEVICE_CONTROL_ENV_VAR, "1,3")
37+
monkeypatch.setenv("VLLM_CPU_SIM_MULTI_NUMA", "1")
38+
39+
assert cr_utils.get_visible_memory_node() == [0, 1, 2, 3]

vllm/envs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
VLLM_CPU_KVCACHE_SPACE: int | None = 0
5353
VLLM_CPU_OMP_THREADS_BIND: str = "auto"
5454
VLLM_CPU_NUM_OF_RESERVED_CPU: int | None = None
55+
VLLM_CPU_SIM_MULTI_NUMA: bool = False
5556
VLLM_CPU_SGL_KERNEL: bool = False
5657
VLLM_CPU_ATTN_SPLIT_KV: bool = True
5758
VLLM_ZENTORCH_WEIGHT_PREPACK: bool = True
@@ -838,6 +839,10 @@ def _resolve_rust_frontend_path() -> str | None:
838839
if "VLLM_CPU_NUM_OF_RESERVED_CPU" in os.environ
839840
else None
840841
),
842+
# (CPU backend only) whether to simulate multiple NUMA nodes on a single node.
843+
"VLLM_CPU_SIM_MULTI_NUMA": lambda: bool(
844+
int(os.getenv("VLLM_CPU_SIM_MULTI_NUMA", "0"))
845+
),
841846
# (CPU backend only) whether to use SGL kernels, optimized for small batch.
842847
"VLLM_CPU_SGL_KERNEL": lambda: bool(int(os.getenv("VLLM_CPU_SGL_KERNEL", "0"))),
843848
# (CPU backend only) whether to enable attention spilt KV.
@@ -2123,6 +2128,7 @@ def compile_factors() -> dict[str, object]:
21232128
"VLLM_V1_OUTPUT_PROC_CHUNK_SIZE",
21242129
"VLLM_CPU_KVCACHE_SPACE",
21252130
"VLLM_CPU_MOE_PREPACK",
2131+
"VLLM_CPU_SIM_MULTI_NUMA",
21262132
"VLLM_ZENTORCH_WEIGHT_PREPACK",
21272133
"VLLM_TEST_FORCE_LOAD_FORMAT",
21282134
"VLLM_ENABLE_CUDA_COMPATIBILITY",

vllm/utils/cpu_resource_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import psutil
1212
import regex as re
1313

14+
from vllm import envs
15+
1416
DEVICE_CONTROL_ENV_VAR = "CPU_VISIBLE_MEMORY_NODES"
1517

1618

@@ -136,7 +138,7 @@ def get_visible_memory_node() -> list[int]:
136138

137139
env_key = DEVICE_CONTROL_ENV_VAR
138140
if (
139-
("VLLM_CPU_SIM_MULTI_NUMA" not in os.environ)
141+
not envs.VLLM_CPU_SIM_MULTI_NUMA
140142
and env_key in os.environ
141143
and os.environ[env_key] != ""
142144
):

vllm/utils/ompmultiprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, config: "VllmConfig"):
3333
# is always 1 for non-MoE models
3434
self.internal_dp_size = config.parallel_config._api_process_count
3535

36-
self.simulate_multi_node = os.environ.get("VLLM_CPU_SIM_MULTI_NUMA", "0") != "0"
36+
self.simulate_multi_node = envs.VLLM_CPU_SIM_MULTI_NUMA
3737
ld_preload_str = os.getenv("LD_PRELOAD", "")
3838
self.use_iomp = "libiomp" in ld_preload_str or "libomp" in ld_preload_str
3939
self.use_gomp = "libgomp" in ld_preload_str

0 commit comments

Comments
 (0)