Skip to content

Commit 28097d5

Browse files
gausah01bigPYJ1151
andauthored
[Bugfix][CPU] Fix CPU KV cache fallback memory allocation (#29604)
Signed-off-by: Gauri Sahnan <[email protected]> Co-authored-by: Li, Jiang <[email protected]>
1 parent dd38ba3 commit 28097d5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

vllm/platforms/cpu.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dataclasses import dataclass
1111
from typing import TYPE_CHECKING
1212

13+
import psutil
1314
import regex as re
1415
import torch
1516

@@ -147,11 +148,21 @@ def get_device_total_memory(cls, device_id: int = 0) -> int:
147148
from vllm.utils.mem_constants import GiB_bytes
148149

149150
kv_cache_space = envs.VLLM_CPU_KVCACHE_SPACE
151+
node_dir = "/sys/devices/system/node"
150152
if kv_cache_space is None:
151-
kv_cache_space = 4 * GiB_bytes # type: ignore
153+
nodes = (
154+
[d for d in os.listdir(node_dir) if d.startswith("node")]
155+
if os.path.exists(node_dir)
156+
else []
157+
)
158+
num_numa_nodes = len(nodes) or 1
159+
free_cpu_memory = psutil.virtual_memory().total // num_numa_nodes
160+
DEFAULT_CPU_MEM_UTILIZATION = 0.5
161+
kv_cache_space = int(free_cpu_memory * DEFAULT_CPU_MEM_UTILIZATION)
162+
kv_cache_space_gib = kv_cache_space / GiB_bytes
152163
logger.warning_once(
153-
"Environment variable VLLM_CPU_KVCACHE_SPACE (GiB) "
154-
"for CPU backend is not set, using 4 by default."
164+
"VLLM_CPU_KVCACHE_SPACE not set. Using "
165+
f"{kv_cache_space_gib:.2f} GiB for KV cache."
155166
)
156167
else:
157168
kv_cache_space *= GiB_bytes

0 commit comments

Comments
 (0)