useful-scripts / transformers-backend-vllm-benchmark.sh
ariG23498's picture
ariG23498 HF Staff
Create transformers-backend-vllm-benchmark.sh
1d0c03f verified
Raw
History Blame Contribute Delete
7.15 kB
#!/usr/bin/env bash
# =============================================================================
# Reproduce the benchmark behind vLLM PR #47187
# "Make the Transformers modeling backend as fast as native vLLM"
# =============================================================================
#
# What this measures
# ------------------
# For each model we compare THREE conditions on identical hardware, workload
# and flags — the only thing that changes is the code path:
#
# native --model-impl vllm vLLM's hand-written model (the bar to match)
# after --model-impl transformers generic Transformers backend, WITH PR #47187
# before --model-impl transformers generic Transformers backend, WITHOUT PR #47187
#
# PR #47187 is 100% Python and lives entirely under
# vllm/model_executor/models/transformers/ — no CUDA/C++/build changes. So the
# honest before/after delta is obtained by `git checkout`-ing just those files
# to the PR's parent commit and back. No recompile, nothing else moves.
#
# Every number the graphic reports is a WITHIN-model, WITHIN-harness ratio
# (% of native, and after/before), so the comparison is apples-to-apples even
# though absolute tok/s differ across model sizes and harnesses.
#
# Hardware used for the published figures: 8x NVIDIA H100 80GB.
#
# Usage
# -----
# ./benchmark.sh # run every condition for every model
# ./benchmark.sh q3_235b # run only conditions whose tag matches this
# Results (JSON + logs) land in ./results/. Re-running skips completed configs.
# =============================================================================
set -uo pipefail
# --- configure these two paths for your machine ------------------------------
VLLM=${VLLM:-/fsx/harry/vllm} # editable vLLM checkout on PR #47187
VENV=${VENV:-/fsx/harry/.venv/bin} # venv with that vLLM installed
# -----------------------------------------------------------------------------
PR=5bce653e09 # PR #47187 merge commit
PARENT="${PR}^" # its parent = "before"
TFDIR=vllm/model_executor/models/transformers/
OUT="$(cd "$(dirname "$0")" && pwd)/results"
PORT=8231
FILTER="${1:-}" # optional tag substring filter
mkdir -p "$OUT"
# Shared workload for every run: 1024-token prompts, 128-token generations.
INLEN=1024; OUTLEN=128; NPROMPTS=1000
# Model matrix that produced the graphic.
# tag | model | harness | tp | gpus | extra-flags
# harness = "throughput" (offline) or "serve" (online; required for data parallel).
MODELS=(
"q3_4b|Qwen/Qwen3-4B|throughput|1|0|"
"q3_32b|Qwen/Qwen3-32B|throughput|2|0,1|"
"q3_235b|Qwen/Qwen3-235B-A22B-FP8|serve|8|0,1,2,3,4,5,6,7|--data-parallel-size 8 --enable-expert-parallel --max-model-len 8192"
)
# Always leave the tree back on the PR files, whatever happens.
restore(){ git -C "$VLLM" checkout "$PR" -- "$TFDIR" 2>/dev/null; }
trap restore EXIT
# Node-local compile caches on tmpfs. On a networked filesystem the multi-rank
# inductor/triton autotune cache races and throws "CUDA driver error: file not
# found"; keeping caches on /dev/shm with single-threaded compile avoids it.
setcache(){
local tag=$1 c=/dev/shm/benchcache/$1
rm -rf "$c"; mkdir -p "$c"/{triton,inductor,vllm}
export TRITON_CACHE_DIR="$c/triton" TORCHINDUCTOR_CACHE_DIR="$c/inductor"
export VLLM_CACHE_ROOT="$c/vllm" TORCHINDUCTOR_COMPILE_THREADS=1
}
free_gpus(){ # wait until the GPUs are actually released before the next run
pkill -9 -f "EngineCore" 2>/dev/null; pkill -9 -f "vllm serve" 2>/dev/null
pkill -9 -f "VLLM::" 2>/dev/null
for _ in $(seq 1 90); do
local u; u=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits \
| awk '{s+=$1} END{print s+0}')
[ "$u" -lt 3000 ] && break; sleep 2
done
}
# ---- offline throughput harness (dense models) ------------------------------
run_throughput(){ # tag model impl tp gpus extra...
local tag=$1 model=$2 impl=$3 tp=$4 gpus=$5; shift 5
local json="$OUT/$tag.json" log="$OUT/$tag.log"
grep -q tokens_per_second "$json" 2>/dev/null && { echo "SKIP $tag (done)"; return; }
setcache "$tag"
echo ">>> $tag : throughput impl=$impl tp=$tp gpus=$gpus"
CUDA_VISIBLE_DEVICES=$gpus VLLM_LOGGING_LEVEL=INFO timeout 5400 \
"$VENV/vllm" bench throughput \
--model "$model" --model-impl "$impl" --tensor-parallel-size "$tp" \
--dataset-name random --input-len $INLEN --output-len $OUTLEN \
--num-prompts $NPROMPTS --output-json "$json" "$@" > "$log" 2>&1
echo -n " "; grep -o '"tokens_per_second":[^,]*' "$json" 2>/dev/null || echo "NO RESULT"
echo " fused-op log lines: $(grep -cE '^Fused:' "$log")"
free_gpus
}
# ---- online serving harness (data-parallel MoE) -----------------------------
run_serve(){ # tag model impl tp gpus extra...
local tag=$1 model=$2 impl=$3 tp=$4 gpus=$5; shift 5
local json="$OUT/$tag.json" slog="$OUT/${tag}_server.log" clog="$OUT/${tag}_client.log"
grep -q total_token_throughput "$json" 2>/dev/null && { echo "SKIP $tag (done)"; return; }
setcache "$tag"; rm -f "$json"
echo ">>> $tag : serve impl=$impl gpus=$gpus"
CUDA_VISIBLE_DEVICES=$gpus VLLM_LOGGING_LEVEL=INFO \
"$VENV/vllm" serve "$model" --model-impl "$impl" --port $PORT "$@" > "$slog" 2>&1 &
local spid=$! ready=0
for _ in $(seq 1 400); do
[ "$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:$PORT/health)" = 200 ] \
&& { ready=1; break; }
kill -0 $spid 2>/dev/null || { echo " server died (see $slog)"; break; }
sleep 3
done
if [ "$ready" = 1 ]; then
echo " server ready -> client"
"$VENV/vllm" bench serve --model "$model" --host 127.0.0.1 --port $PORT \
--dataset-name random --random-input-len $INLEN --random-output-len $OUTLEN \
--num-prompts $NPROMPTS --ignore-eos --save-result --result-filename "$json" > "$clog" 2>&1
echo -n " "; grep -oE '"total_token_throughput":[^,}]*' "$json" 2>/dev/null || echo "NO RESULT"
echo " fused-op log lines: $(grep -cE '^Fused:' "$slog")"
fi
kill -9 $spid 2>/dev/null; free_gpus
}
dispatch(){ # spec impl condtag
IFS='|' read -r tag model harness tp gpus extra <<< "$1"
[ -n "$FILTER" ] && [[ "$tag" != *"$FILTER"* ]] && return
# shellcheck disable=SC2086
run_"$harness" "${tag}_$3" "$model" "$2" "$tp" "$gpus" $extra
}
# === Phase A: native + after — run on the PR files ===========================
echo "######## PHASE A: native + after (PR #47187 files) ########"
git -C "$VLLM" checkout "$PR" -- "$TFDIR"
for spec in "${MODELS[@]}"; do
dispatch "$spec" vllm native
dispatch "$spec" transformers after
done
# === Phase B: before — swap ONLY the transformers/ dir to the parent commit ==
echo "######## PHASE B: before (parent commit ${PARENT}) ########"
git -C "$VLLM" checkout "$PARENT" -- "$TFDIR"
git -C "$VLLM" status --porcelain "$TFDIR"
for spec in "${MODELS[@]}"; do
dispatch "$spec" transformers before
done
restore
echo "######## DONE — results in $OUT ; tree restored to PR files ########"