基准测试与性能分析#
基准测试#
对运行单个静态批处理(不带服务器)的延迟进行基准测试。参数与
launch_server.py
的参数相同。请注意,这是一个不带动态批处理服务器的简化测试脚本,因此对于实际服务器可以处理的批量大小,它可能会内存不足。实际服务器会将预填充(prefill)截断为多个批次,而此简化脚本不会。python -m sglang.bench_one_batch --model-path meta-llama/Meta-Llama-3.1-8B-Instruct --batch 32 --input-len 256 --output-len 32
对离线处理进行基准测试。此脚本将启动一个离线引擎并运行基准测试。
python3 -m sglang.bench_offline_throughput --model-path meta-llama/Meta-Llama-3.1-8B-Instruct --num-prompts 10
对在线服务进行基准测试。请先使用
sglang.launch_server
启动服务器,然后运行以下命令。python3 -m sglang.bench_serving --backend sglang --num-prompt 10
使用 PyTorch Profiler 进行性能分析#
PyTorch Profiler 是一个方便的基础工具,用于检查内核执行时间、调用栈、内核重叠和占用率。
对服务器进行性能分析
# set trace path export SGLANG_TORCH_PROFILER_DIR=/root/sglang/profile_log # start server python -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct # send profiling request from client python -m sglang.bench_serving --backend sglang --model meta-llama/Llama-3.1-8B-Instruct --num-prompts 10 --sharegpt-output-len 100 --profile
请确保在服务器和客户端都设置了
SGLANG_TORCH_PROFILER_DIR
,否则无法正确生成跟踪文件。一种安全的方法是在 shell 的.*rc
文件(例如 bash shell 的~/.bashrc
)中设置SGLANG_TORCH_PROFILER_DIR
。对离线进行性能分析
export SGLANG_TORCH_PROFILER_DIR=/root/sglang/profile_log # profile one batch with bench_one_batch.py # batch size can be controlled with --batch argument python3 -m sglang.bench_one_batch --model-path meta-llama/Llama-3.1-8B-Instruct --batch 32 --input-len 1024 --output-len 10 --profile # profile multiple batches with bench_offline_throughput.py python -m sglang.bench_offline_throughput --model-path meta-llama/Llama-3.1-8B-Instruct --dataset-name random --num-prompts 10 --profile --mem-frac=0.8
查看跟踪
跟踪文件可以从以下地址加载和可视化:
https://ui.perfetto.dev/ (任意浏览器)
chrome://tracing (仅限 Chrome 浏览器)
如果浏览器因跟踪文件过大而无法打开,客户端可以通过控制提示数量和提示输出长度来生成较小的跟踪文件(<100MB)。例如,在对服务器进行性能分析时:
python -m sglang.bench_serving --backend sglang --model meta-llama/Llama-3.1-8B-Instruct --num-prompts 2 --sharegpt-output-len 100 --profile
此命令通过
--num-prompts
参数设置提示数量为 2,并通过--sharegpt-output-len
参数限制输出序列长度为 100,这可以生成一个较小的跟踪文件,以便浏览器顺利打开。此外,如果您想通过跟踪中的 CUDA 内核定位 SGLang Python 源代码,需要在启动服务时禁用 CUDA Graph。这可以通过在启动服务的命令中使用
--disable-cuda-graph
参数来完成。
使用 Nsight 进行性能分析#
Nsight Systems 是一款高级工具,可暴露更多性能分析细节,例如寄存器和共享内存使用情况、注释的代码区域以及低级 CUDA API 和事件。
先决条件
使用 apt 安装,或在 NVIDIA Docker 容器 或 SGLang Docker 容器 中运行。
# install nsys # https://docs.nvda.net.cn/nsight-systems/InstallationGuide/index.html 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 --trace-fork-before-exec=true --cuda-graph-trace=node python3 -m sglang.bench_one_batch --model meta-llama/Meta-Llama-3-8B --batch-size 64 --input-len 512
对服务器进行性能分析,例如:
# launch the server, set the delay and duration times according to needs # after the duration time has been used up, server will be killed by nsys nsys profile --trace-fork-before-exec=true --cuda-graph-trace=node -o sglang.out --delay 60 --duration 70 python3 -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct --disable-radix-cache # client python3 -m sglang.bench_serving --backend sglang --num-prompts 1000 --dataset-name random --random-input 1024 --random-output 512
在实践中,我们建议用户将
--duration
参数设置为一个较大的值。无论何时用户想让服务器停止性能分析,首先运行nsys sessions list
以获取
profile-XXXXX
形式的会话 ID,然后运行nsys stop --session=profile-XXXXX
以手动终止性能分析器并立即生成
nsys-rep
文件。使用 NVTX 注释代码区域,例如查看它们的执行时间。
# install nvtx pip install nvtx
# code snippets import nvtx with nvtx.annotate("description", color="color"): # some critical code
其他技巧#
您可以通过仅提供 config.json 文件,使用虚拟权重对模型进行基准测试。这允许快速测试模型变体,而无需训练。为此,请在上述命令中添加
--load-format dummy
,然后您只需在 checkpoint 文件夹下有一个正确的config.json
文件即可。您可以使用
--json-model-override-args
对修改配置(例如,更少的层)的模型进行基准测试。例如,您可以使用以下命令对一个只有 2 层和 2 个 kv 头的模型进行基准测试:python -m sglang.bench_one_batch --model-path meta-llama/Meta-Llama-3.1-8B-Instruct --batch 32 --input-len 256 --output-len 32 --load-format dummy --json-model-override-args '{"num_hidden_layers": 1, "num_key_value_heads": 1}'
您可以使用
--python-backtrace=cuda
查看所有 CUDA 内核的 python 调用栈,就像在 PyTorch Profiler 中一样。(注意:这可能会导致基于 CUDA 事件的计时方法计算出的内核运行时间不准确地偏长)有关更多参数,请参阅 Nsight Systems 用户指南。