基准测试和分析#
基准测试#
在没有服务器的情况下运行单个静态批处理的延迟基准测试。参数与
launch_server.py相同。 请注意,这是一个没有动态批处理服务器的简化测试脚本,因此对于真实服务器可以处理的批处理大小,它可能会耗尽内存。真实服务器将预填充截断为多个批次,而这个简化脚本则不会。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
使用Nsight进行性能分析#
先决条件
# install nsys
# https://docs.nvidia.com/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要分析服务器,例如
# server
# set the delay and duration times according to needs
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
使用NVTX,例如。
# install nvtx
pip install nvtx
# code snippets
import nvtx
with nvtx.annotate("description", color="color"):
# some critical code
其他提示#
你可以通过仅提供config.json文件来使用虚拟权重对模型进行基准测试。这允许快速测试模型变体而无需训练。为此,请将
--load-format dummy添加到上述命令中,然后你只需要在检查点文件夹下有一个正确的config.json。你可以通过使用
--json-model-override-args来对具有修改配置(例如,较少的层)的模型进行基准测试。例如,你可以使用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}'来基准测试一个只有2层和2个kv头的模型。
使用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
python -m sglang.bench_serving --backend sglang --model-path meta-llama/Llama-3.1-8B-Instruct --num-prompts 10 --profile
可以使用 https://ui.perfetto.dev/ 来可视化跟踪。
离线分析
export SGLANG_TORCH_PROFILER_DIR=/root/sglang/profile_log
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