Profiling vLLM#

警告

性能分析仅供 vLLM 开发者和维护者用于了解代码库不同部分所花费的时间比例。 vLLM 最终用户绝不应启用性能分析,因为它会显著降低推理速度。

使用 PyTorch Profiler 进行性能分析#

我们支持使用 torch.profiler 模块跟踪 vLLM 工作进程。您可以通过设置 VLLM_TORCH_PROFILER_DIR 环境变量为要保存跟踪的目录来启用跟踪: VLLM_TORCH_PROFILER_DIR=/mnt/traces/

OpenAI 服务器也需要使用设置好的 VLLM_TORCH_PROFILER_DIR 环境变量启动。

当使用 benchmarks/benchmark_serving.py 时,您可以通过传递 --profile 标志来启用性能分析。

可以使用 https://ui.perfetto.dev/ 可视化跟踪结果。

提示

性能分析时,仅通过 vLLM 发送少量请求,因为跟踪文件可能会变得非常大。 此外,无需解压跟踪文件,它们可以直接查看。

提示

要停止性能分析器 - 它会将所有性能分析跟踪文件刷新到目录。 这需要时间,例如对于 llama 70b 大约 100 个请求的数据量,在 H100 上刷新大约需要 10 分钟。 在启动服务器之前,将环境变量 VLLM_RPC_TIMEOUT 设置为一个较大的数字。 例如 30 分钟左右。 export VLLM_RPC_TIMEOUT=1800000

示例命令和用法#

离线推理#

有关示例,请参阅 examples/offline_inference/simple_profiling.py

OpenAI 服务器#

VLLM_TORCH_PROFILER_DIR=./vllm_profile python -m vllm.entrypoints.openai.api_server --model meta-llama/Meta-Llama-3-70B

benchmark_serving.py

python benchmarks/benchmark_serving.py --backend vllm --model meta-llama/Meta-Llama-3-70B --dataset-name sharegpt --dataset-path sharegpt.json --profile --num-prompts 2

使用 NVIDIA Nsight Systems 进行性能分析#

Nsight systems 是一款高级工具,可公开更多性能分析详细信息,例如寄存器和共享内存使用情况、带注释的代码区域以及低级 CUDA API 和事件。

安装 nsight-systems,使用您的软件包管理器。 以下代码块是 Ubuntu 的示例。

apt update
apt install -y --no-install-recommends gnupg
echo "deb http://developer.download.nvidia.com/devtools/repos/ubuntu$(source /etc/lsb-release; echo "$DISTRIB_RELEASE" | tr -d .)/$(dpkg --print-architecture) /" | tee /etc/apt/sources.list.d/nvidia-devtools.list
apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
apt update
apt install nsight-systems-cli

示例命令和用法#

离线推理#

对于基本用法,您只需在您运行的任何现有离线推理脚本之前附加 nsys profile -o report.nsys-rep --trace-fork-before-exec=true --cuda-graph-trace=node 即可。

以下是使用 benchmarks/benchmark_latency.py 脚本的示例

nsys profile -o report.nsys-rep --trace-fork-before-exec=true --cuda-graph-trace=node python benchmarks/benchmark_latency.py --model meta-llama/Llama-3.1-8B-Instruct --num-iters-warmup 5 --num-iters 1 --batch-size 16 --input-len 512 --output-len 8

OpenAI 服务器#

要分析服务器性能,您需要像离线推理一样,在 vllm serve 命令前加上 nsys profile,但是您必须根据基准测试的需要指定 --delay XX --duration YY 参数。 持续时间用完后,服务器将被终止。

# server
nsys profile -o report.nsys-rep --trace-fork-before-exec=true --cuda-graph-trace=node --delay 30 --duration 60 vllm serve meta-llama/Llama-3.1-8B-Instruct

# client
python benchmarks/benchmark_serving.py --backend vllm --model meta-llama/Llama-3.1-8B-Instruct --num-prompts 1 --dataset-name random --random-input 1024 --random-output 512

在实践中,您应该将 --duration 参数设置为较大的值。 无论何时您想要服务器停止性能分析,请运行

nsys sessions list

以获取 profile-XXXXX 形式的会话 ID,然后运行

nsys stop --session=profile-XXXXX

以手动终止性能分析器并生成您的 nsys-rep 报告。

分析#

您可以使用 CLI 中的摘要(使用 nsys stats [profile-file])或通过安装 Nsight GUI 本地按照此处的说明来查看这些性能分析结果。

CLI 示例

nsys stats report1.nsys-rep
...
 ** CUDA GPU Kernel Summary (cuda_gpu_kern_sum):

 Time (%)  Total Time (ns)  Instances   Avg (ns)     Med (ns)    Min (ns)  Max (ns)   StdDev (ns)                                                  Name                                                
 --------  ---------------  ---------  -----------  -----------  --------  ---------  -----------  ----------------------------------------------------------------------------------------------------
     46.3   10,327,352,338     17,505    589,965.9    144,383.0    27,040  3,126,460    944,263.8  sm90_xmma_gemm_bf16bf16_bf16f32_f32_tn_n_tilesize128x128x64_warpgroupsize1x1x1_execute_segment_k_of…
     14.8    3,305,114,764      5,152    641,520.7    293,408.0   287,296  2,822,716    867,124.9  sm90_xmma_gemm_bf16bf16_bf16f32_f32_tn_n_tilesize256x128x64_warpgroupsize2x1x1_execute_segment_k_of…
     12.1    2,692,284,876     14,280    188,535.4     83,904.0    19,328  2,862,237    497,999.9  sm90_xmma_gemm_bf16bf16_bf16f32_f32_tn_n_tilesize64x128x64_warpgroupsize1x1x1_execute_segment_k_off…
      9.5    2,116,600,578     33,920     62,399.8     21,504.0    15,326  2,532,285    290,954.1  sm90_xmma_gemm_bf16bf16_bf16f32_f32_tn_n_tilesize64x64x64_warpgroupsize1x1x1_execute_segment_k_off_…
      5.0    1,119,749,165     18,912     59,208.4      9,056.0     6,784  2,578,366    271,581.7  void vllm::act_and_mul_kernel<c10::BFloat16, &vllm::silu_kernel<c10::BFloat16>, (bool)1>(T1 *, cons…
      4.1      916,662,515     21,312     43,011.6     19,776.0     8,928  2,586,205    199,790.1  void cutlass::device_kernel<flash::enable_sm90_or_later<flash::FlashAttnFwdSm90<flash::CollectiveMa…
      2.6      587,283,113     37,824     15,526.7      3,008.0     2,719  2,517,756    139,091.1  std::enable_if<T2>(int)0&&vllm::_typeConvert<T1>::exists, void>::type vllm::fused_add_rms_norm_kern…
      1.9      418,362,605     18,912     22,121.5      3,871.0     3,328  2,523,870    175,248.2  void vllm::rotary_embedding_kernel<c10::BFloat16, (bool)1>(const long *, T1 *, T1 *, const T1 *, in      0.7      167,083,069     18,880      8,849.7      2,240.0     1,471  2,499,996    101,436.1  void vllm::reshape_and_cache_flash_kernel<__nv_bfloat16, __nv_bfloat16, (vllm::Fp8KVCacheDataType)0…
... 

GUI 示例

Screenshot 2025-03-05 at 11 48 42 AM

Profiling vLLM Python 代码#

Python 标准库包含 cProfile,用于分析 Python 代码。 vLLM 包含一些辅助工具,可以轻松地将其应用于 vLLM 的一部分。 vllm.utils.cprofilevllm.utils.cprofile_context 函数都可用于分析一段代码的性能。

示例用法 - 装饰器#

第一个辅助工具是一个 Python 装饰器,可用于分析函数的性能。 如果指定了文件名,则性能分析结果将保存到该文件。 如果未指定文件名,则性能分析数据将打印到 stdout。

import vllm.utils

@vllm.utils.cprofile("expensive_function.prof")
def expensive_function():
    # some expensive code
    pass

示例用法 - 上下文管理器#

第二个辅助工具是一个上下文管理器,可用于分析代码块的性能。 与装饰器类似,文件名是可选的。

import vllm.utils

def another_function():
    # more expensive code
    pass

with vllm.utils.cprofile_context("another_function.prof"):
    another_function()

分析性能分析结果#

有多种工具可用于帮助分析性能分析结果。 其中一个例子是 snakeviz

pip install snakeviz
snakeviz expensive_function.prof