使用 CPU 安装#

vLLM 最初支持在 x86 CPU 平台上进行基本模型推理和服务,数据类型为 FP32 和 BF16。

目录:

  1. 要求

  2. 使用 Dockerfile 快速入门

  3. 从源代码构建

  4. 相关运行时环境变量

  5. Intel Extension for PyTorch

  6. 性能提示

要求#

  • 操作系统:Linux

  • 编译器:gcc/g++>=12.3.0(可选,推荐)

  • 指令集体系结构 (ISA) 要求:AVX512(可选,推荐)

使用 Dockerfile 快速入门#

$ docker build -f Dockerfile.cpu -t vllm-cpu-env --shm-size=4g .
$ docker run -it \
             --rm \
             --network=host \
             --cpuset-cpus=<cpu-id-list, optional> \
             --cpuset-mems=<memory-node, optional> \
             vllm-cpu-env

从源代码构建#

  • 首先,安装推荐的编译器。我们建议使用 gcc/g++ >= 12.3.0 作为默认编译器,以避免潜在问题。例如,在 Ubuntu 22.4 上,你可以运行:

$ sudo apt-get update  -y
$ sudo apt-get install -y gcc-12 g++-12 libnuma-dev
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 10 --slave /usr/bin/g++ g++ /usr/bin/g++-12
  • 其次,安装用于 vLLM CPU 后端构建的 Python 包:

$ pip install --upgrade pip
$ pip install wheel packaging ninja "setuptools>=49.4.0" numpy
$ pip install -v -r requirements-cpu.txt --extra-index-url https://download.pytorch.org/whl/cpu
  • 第三,从源代码构建并安装 oneDNN 库:

$ git clone -b rls-v3.5 https://github.com/oneapi-src/oneDNN.git
$ cmake -B ./oneDNN/build -S ./oneDNN -G Ninja -DONEDNN_LIBRARY_TYPE=STATIC \
    -DONEDNN_BUILD_DOC=OFF \
    -DONEDNN_BUILD_EXAMPLES=OFF \
    -DONEDNN_BUILD_TESTS=OFF \
    -DONEDNN_BUILD_GRAPH=OFF \
    -DONEDNN_ENABLE_WORKLOAD=INFERENCE \
    -DONEDNN_ENABLE_PRIMITIVE=MATMUL
$ cmake --build ./oneDNN/build --target install --config Release
  • 最后,构建并安装 vLLM CPU 后端:

$ VLLM_TARGET_DEVICE=cpu python setup.py install

备注

  • BF16 是当前 CPU 后端的默认数据类型(这意味着后端将 FP16 转换为 BF16),并且与所有支持 AVX512 ISA 的 CPU 兼容。

  • AVX512_BF16 是一个扩展 ISA,它提供本机 BF16 数据类型转换和向量乘积指令,与纯 AVX512 相比,它将带来一些性能提升。CPU 后端构建脚本将检查主机 CPU 标志以确定是否启用 AVX512_BF16。

  • 如果你想强制为交叉编译启用 AVX512_BF16,请在构建之前设置环境变量 VLLM_CPU_AVX512BF16=1。

英特尔 PyTorch 扩展#

性能提示#

  • 我们强烈建议使用 TCMalloc 进行高性能内存分配和更好的缓存局部性。例如,在 Ubuntu 22.4 上,你可以运行:

$ sudo apt-get install libtcmalloc-minimal4 # install TCMalloc library
$ find / -name *libtcmalloc* # find the dynamic link library path
$ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4:$LD_PRELOAD # prepend the library to LD_PRELOAD
$ python examples/offline_inference.py # run vLLM
  • 在使用在线服务时,建议为服务框架保留 1-2 个 CPU 内核,以避免 CPU 过度订阅。例如,在一个具有 32 个物理 CPU 内核的平台上,为框架保留 CPU 30 和 31,并将 CPU 0-29 用于 OpenMP:

$ export VLLM_CPU_KVCACHE_SPACE=40
$ export VLLM_CPU_OMP_THREADS_BIND=0-29
$ vllm serve facebook/opt-125m
  • 如果在支持超线程的机器上使用 vLLM CPU 后端,建议使用 VLLM_CPU_OMP_THREADS_BIND 将每个物理 CPU 内核绑定到一个 OpenMP 线程。在支持超线程的平台上,如果拥有 16 个逻辑 CPU 内核 / 8 个物理 CPU 内核:

$ lscpu -e # check the mapping between logical CPU cores and physical CPU cores

# The "CPU" column means the logical CPU core IDs, and the "CORE" column means the physical core IDs. On this platform, two logical cores are sharing one physical core.
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE    MAXMHZ   MINMHZ      MHZ
0    0      0    0 0:0:0:0          yes 2401.0000 800.0000  800.000
1    0      0    1 1:1:1:0          yes 2401.0000 800.0000  800.000
2    0      0    2 2:2:2:0          yes 2401.0000 800.0000  800.000
3    0      0    3 3:3:3:0          yes 2401.0000 800.0000  800.000
4    0      0    4 4:4:4:0          yes 2401.0000 800.0000  800.000
5    0      0    5 5:5:5:0          yes 2401.0000 800.0000  800.000
6    0      0    6 6:6:6:0          yes 2401.0000 800.0000  800.000
7    0      0    7 7:7:7:0          yes 2401.0000 800.0000  800.000
8    0      0    0 0:0:0:0          yes 2401.0000 800.0000  800.000
9    0      0    1 1:1:1:0          yes 2401.0000 800.0000  800.000
10   0      0    2 2:2:2:0          yes 2401.0000 800.0000  800.000
11   0      0    3 3:3:3:0          yes 2401.0000 800.0000  800.000
12   0      0    4 4:4:4:0          yes 2401.0000 800.0000  800.000
13   0      0    5 5:5:5:0          yes 2401.0000 800.0000  800.000
14   0      0    6 6:6:6:0          yes 2401.0000 800.0000  800.000
15   0      0    7 7:7:7:0          yes 2401.0000 800.0000  800.000

# On this platform, it is recommend to only bind openMP threads on logical CPU cores 0-7 or 8-15
$ export VLLM_CPU_OMP_THREADS_BIND=0-7
$ python examples/offline_inference.py
  • 如果在具有 NUMA 的多插槽机器上使用 vLLM CPU 后端,请注意使用 VLLM_CPU_OMP_THREADS_BIND 设置 CPU 内核,以避免跨 NUMA 节点内存访问。