优化与调优#

本指南旨在帮助用户提升 vllm-ascend 在系统层面的性能。它包括操作系统配置、库优化、部署指南等。欢迎提供任何反馈。

准备工作#

运行容器

# Update DEVICE according to your device (/dev/davinci[0-7])
export DEVICE=/dev/davinci0
# Update the cann base image
export IMAGE=m.daocloud.io/quay.io/ascend/cann:8.3.rc2-910b-ubuntu22.04-py3.11
docker run --rm \
--name performance-test \
--shm-size=1g \
--device $DEVICE \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /root/.cache:/root/.cache \
-it $IMAGE bash

配置您的环境

# Configure the mirror
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list

# Install os packages
apt update && apt install wget gcc g++ libnuma-dev git vim -y

安装 vllm 和 vllm-ascend

# Install necessary dependencies
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install modelscope pandas datasets gevent sacrebleu rouge_score pybind11 pytest

# Configure this var to speed up model download
VLLM_USE_MODELSCOPE=true

请遵循 安装指南,确保 vLLM 和 vllm-ascend 安装正确。

注意

请确保在完成 Python 配置后,再安装 vLLM 和 vllm-ascend,因为这些包会使用当前环境中的 Python 构建二进制文件。如果您在完成 1.1 部分之前安装 vLLM 和 vllm-ascend,则构建的二进制文件将不会使用优化的 Python。

优化项#

1. 编译优化#

1.1. 安装优化后的 python#

Python 从 3.6 及以上版本开始支持 **LTO** 和 **PGO** 优化,可以在编译时启用。为了方便起见,我们直接为用户提供了优化后的 python 包。您也可以按照此 教程,根据您的具体场景重现 python 的构建。

mkdir -p /workspace/tmp
cd /workspace/tmp

# Download prebuilt lib and packages
wget https://repo.oepkgs.net/ascend/pytorch/vllm/lib/libcrypto.so.1.1
wget https://repo.oepkgs.net/ascend/pytorch/vllm/lib/libomp.so
wget https://repo.oepkgs.net/ascend/pytorch/vllm/lib/libssl.so.1.1
wget https://repo.oepkgs.net/ascend/pytorch/vllm/python/py311_bisheng.tar.gz

# Configure python and pip
cp ./*.so* /usr/local/lib
tar -zxvf ./py311_bisheng.*  -C /usr/local/
mv  /usr/local/py311_bisheng/  /usr/local/python
sed -i "1c#\!/usr/local/python/bin/python3.11" /usr/local/python/bin/pip3
sed -i "1c#\!/usr/local/python/bin/python3.11" /usr/local/python/bin/pip3.11
ln -sf  /usr/local/python/bin/python3  /usr/bin/python
ln -sf  /usr/local/python/bin/python3  /usr/bin/python3
ln -sf  /usr/local/python/bin/python3.11  /usr/bin/python3.11
ln -sf  /usr/local/python/bin/pip3  /usr/bin/pip3
ln -sf  /usr/local/python/bin/pip3  /usr/bin/pip

export PATH=/usr/bin:/usr/local/python/bin:$PATH

2. OS 优化#

2.1. jemalloc#

**jemalloc** 是一个内存分配器,可提高多线程场景下的性能并减少内存碎片。jemalloc 使用本地线程内存管理器分配变量,可避免线程间的锁竞争,从而大幅优化性能。

# Install jemalloc
sudo apt update
sudo apt install libjemalloc2

# Configure jemalloc
export LD_PRELOAD=/usr/lib/"$(uname -i)"-linux-gnu/libjemalloc.so.2 $LD_PRELOAD

2.2. Tcmalloc#

**Tcmalloc (Thread Caching Malloc)** 是一个通用的内存分配器,通过引入多级缓存结构,减少互斥锁竞争并优化大型对象处理流程,从而在保证低延迟的同时提高整体性能。更多详细信息请 在此 查看。

# Install tcmalloc
sudo apt update
sudo apt install libgoogle-perftools4 libgoogle-perftools-dev

# Get the location of libtcmalloc.so*
find /usr -name libtcmalloc.so*

# Make the priority of tcmalloc higher
# The <path> is the location of libtcmalloc.so we get from the upper command
# Example: "$LD_PRELOAD:/usr/lib/aarch64-linux-gnu/libtcmalloc.so"
export LD_PRELOAD="$LD_PRELOAD:<path>"

# Verify your configuration
# The path of libtcmalloc.so will be contained in the result if your configuration is valid
ldd `which python`

3. torch_npu 优化#

torch_npu 中的一些性能调优功能由环境变量控制。以下显示了一些功能及其相关环境变量。

内存优化

# Upper limit of memory block splitting allowed (MB): Setting this parameter can prevent large memory blocks from being split.
export PYTORCH_NPU_ALLOC_CONF="max_split_size_mb:250"

# When operators on the communication stream have dependencies, they all need to be ended before being released for reuse. The logic of multi-stream reuse is to release the memory on the communication stream in advance so that the computing stream can be reused.
export PYTORCH_NPU_ALLOC_CONF="expandable_segments:True"

调度优化

# Optimize operator delivery queue. This will affect the memory peak value, and may degrade if the memory is tight.
export TASK_QUEUE_ENABLE=2

# This will greatly improve the CPU bottleneck model and ensure the same performance for the NPU bottleneck model.
export CPU_AFFINITY_CONF=1

4. CANN 优化#

4.1. HCCL 优化#

HCCL 中有一些性能调优功能,它们由环境变量控制。

通过设置如下环境变量,您可以将 HCCL 配置为使用“AIV”模式来优化性能。在“AIV”模式下,通信由 AI 向量核直接通过 RoCE 进行调度,而不是由 AI CPU 调度。

export HCCL_OP_EXPANSION_MODE="AIV"

此外,还有更多针对特定场景的性能优化功能,如下所示。

  • HCCL_INTRA_ROCE_ENABLE:在两个 8P 之间使用 RDMA 链路而不是 SDMA 链路作为 Mesh 互连链路。更多详细信息请 在此 查看。

  • HCCL_RDMA_TC:使用此变量配置 RDMA NIC 的流量类别。更多详细信息请 在此 查看。

  • HCCL_RDMA_SL:使用此变量配置 RDMA NIC 的服务级别。更多详细信息请 在此 查看。

  • HCCL_BUFFSIZE:使用此变量控制两个 NPU 之间共享数据的缓存大小。更多详细信息请 在此 查看。

5. OS 优化#

本节描述在主机(裸金属或 Kubernetes 节点)上应用的操作系统级别优化,以提高推理工作负载的性能稳定性、延迟和吞吐量。

注意

这些设置必须在主机操作系统上以 root 权限应用,而不是在容器内。

5.1#

将 CPU 频率调节器设置为 performance

echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

目的

  • 强制所有 CPU 核心在 performance 调节器下运行

  • 禁用动态频率缩放(例如 ondemandpowersave

优势

  • 保持 CPU 核心以最高频率运行

  • 降低延迟抖动

  • 提高推理工作负载的可预测性

5.2 禁用 Swap 使用#

sysctl -w vm.swappiness=0

目的

  • 最小化内核将内存页面交换到磁盘的倾向

优势

  • 防止因交换引起的严重延迟峰值

  • 提高大型内存模型(in-memory models)的稳定性

注意事项

  • 对于推理工作负载,Swap 会引入二级延迟

  • 推荐值是 01

5.3 禁用自动 NUMA 平衡#

sysctl -w kernel.numa_balancing=0

目的

  • 禁用内核的自动 NUMA 页面迁移机制

优势

  • 防止后台内存页面迁移

  • 减少不可预测的内存访问延迟

  • 提高 NUMA 系统上的性能稳定性

推荐用于

  • 多路服务器

  • 具有显式 NUMA 绑定的 Ascend / NPU 部署

  • 手动管理 CPU 和内存亲和性的系统

5.4 增加调度器迁移成本#

sysctl -w kernel.sched_migration_cost_ns=50000

目的

  • 增加调度器在 CPU 核心之间迁移任务的成本

优势

  • 减少频繁的线程迁移

  • 提高 CPU 缓存局部性

  • 降低推理工作负载的延迟抖动

参数详情

  • 单位:纳秒 (ns)

  • 典型推荐范围:50000–100000

  • 更高的值会促使线程停留在同一个 CPU 核心上