#!/usr/bin/env bash # Long-context matrix: SGLang vs vLLM on DeepSeek-V4-Flash (H200). set -Eeuo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" EXPERIMENT_NAME="$(basename "$SCRIPT_DIR")" # shellcheck source=/dev/null source "${SCRIPT_DIR}/../../scripts/common/lib.sh" # shellcheck source=/dev/null source "${SCRIPT_DIR}/../../scripts/common/platform.sh" # shellcheck source=/dev/null source "${SCRIPT_DIR}/config.env" RUN_ID="${RUN_ID:-$(date '+%Y%m%d-%H%M%S')}" RESULT_BASE="${SCRIPT_DIR}/results" log_dir_global="${RESULT_BASE}/${RUN_ID}/logs" mkdir -p "$log_dir_global" log_init "${log_dir_global}/orchestrator.log" log "experiment=${EXPERIMENT_NAME}" log "run_id=${RUN_ID}" log "platform=${PLATFORM}" log "hardware=${HARDWARE}" log "model=${MODEL_PATH}" log "groups=${#CONTEXT_GROUPS[@]}" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- is_server_healthy() { local port="$1" curl --fail --silent --show-error --max-time 5 "http://127.0.0.1:${port}/health" >/dev/null 2>&1 } stop_server() { local backend="$1" local pid_file="/data/user1/yy/dsv4_h200_long_context_matrix_${backend}.pid" if [[ -f "$pid_file" ]]; then local pid pid="$(cat "$pid_file")" if kill -0 "$pid" 2>/dev/null; then log "stopping ${backend} server pid=${pid}" kill "$pid" 2>/dev/null || true sleep 5 kill -9 "$pid" 2>/dev/null || true fi rm -f "$pid_file" fi # Fallback cleanup. if [[ "$backend" == "sglang" ]]; then pkill -9 -f "sglang serve.*DeepSeek-V4-Flash" 2>/dev/null || true else pkill -9 -f "vllm serve.*DeepSeek-V4-Flash" 2>/dev/null || true fi sleep 2 } start_server() { local backend="$1" local max_model_len="$2" local max_slots="$3" local start_script if [[ "$backend" == "sglang" ]]; then start_script="${SGLANG_START_SCRIPT}" local port="$SGLANG_PORT" else start_script="${VLLM_START_SCRIPT}" local port="$VLLM_PORT" fi log "starting ${backend} server (max_model_len=${max_model_len}, slots=${max_slots}) with ${start_script}" bash "${start_script}" "$max_model_len" "$max_slots" >> "${log_dir_global}/${backend}_${max_model_len}.server.outer.log" 2>&1 if ! is_server_healthy "$port"; then log "error: ${backend} server failed to become healthy" return 1 fi log "${backend} server is healthy on port ${port}" } run_warmup() { local backend="$1" local input_len="$2" local output_len="${3:-256}" local port if [[ "$backend" == "sglang" ]]; then port="$SGLANG_PORT" else port="$VLLM_PORT" fi log "warming up ${backend} (input=${input_len}, output=${output_len}, num=1)" "${VENV_CLIENT}/bin/python" "${SCRIPT_DIR}/../../scripts/common/warmup.py" \ --backend "$backend" \ --port "$port" \ --input-len "$input_len" \ --output-len "$output_len" \ --num 1 \ --env-python "${VENV_CLIENT}/bin/python" \ >> "${log_dir_global}/${backend}.warmup.log" 2>&1 log "warmup for ${backend} completed" } scenario_already_completed() { local output_file="$1" local expected="$2" [[ -s "$output_file" ]] || return 1 local completed completed="$(${VENV_CLIENT}/bin/python -c " import json, sys path = sys.argv[1] try: with open(path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if line: data = json.loads(line) print(data.get('completed', 0)) break except Exception: print(0) " "$output_file")" [[ "${completed:-0}" -ge "$expected" ]] } run_group() { local backend="$1" local group_label="$2" local max_model_len="$3" local max_slots="$4" local -n scenarios_ref="$5" local result_root="${RESULT_BASE}/${RUN_ID}/${backend}" local raw_dir="${result_root}/raw_outputs" local phase_log_dir="${result_root}/logs" mkdir -p "$raw_dir" "$phase_log_dir" local port if [[ "$backend" == "sglang" ]]; then port="$SGLANG_PORT" else port="$VLLM_PORT" fi log "===== ${backend} ${group_label} START (max_model_len=${max_model_len}) =====" stop_server "$backend" if ! start_server "$backend" "$max_model_len" "$max_slots"; then log "ERROR: ${backend} failed to start for ${group_label}; skipping group" stop_server "$backend" return 1 fi # Warmup with the shortest output for this input length to save time. local warmup_input="${scenarios_ref[0]}" local warmup_input_len warmup_input_len="$(echo "$warmup_input" | awk '{print $2}')" run_warmup "$backend" "$warmup_input_len" 256 for scenario in "${scenarios_ref[@]}"; do read -r concurrency input_len output_len num_prompts <<< "$scenario" output_file="${raw_dir}/${backend}_${group_label}_$(date '+%m%d')_${concurrency}_${input_len}_${output_len}.jsonl" detail_log="${phase_log_dir}/${backend}_${group_label}_c${concurrency}_i${input_len}_o${output_len}.log" if scenario_already_completed "$output_file" "$num_prompts"; then log "skipping already-completed ${backend} ${group_label} scenario: c=${concurrency} i=${input_len} o=${output_len}" continue fi log "running ${backend} ${group_label} scenario: c=${concurrency} i=${input_len} o=${output_len} n=${num_prompts}" "${VENV_CLIENT}/bin/python" -m sglang.bench_serving \ --backend "$backend" \ --host 127.0.0.1 \ --port "$port" \ --dataset-name random \ --random-input-len "$input_len" \ --random-output-len "$output_len" \ --num-prompts "$num_prompts" \ --max-concurrency "$concurrency" \ --request-rate 10000 \ --output-file "$output_file" \ --output-details \ > "$detail_log" 2>&1 || { log "ERROR: ${backend} ${group_label} scenario c=${concurrency} i=${input_len} o=${output_len} failed; see ${detail_log}" continue } log "finished ${backend} ${group_label} scenario: output=${output_file}" done stop_server "$backend" log "===== ${backend} ${group_label} DONE =====" } parse_backend() { local backend="$1" local result_root="${RESULT_BASE}/${RUN_ID}/${backend}" log "parsing ${backend} results in ${result_root}" "${VENV_CLIENT}/bin/python" "${SCRIPT_DIR}/../../scripts/common/parse_backend.py" "$result_root" --backend "$backend" \ >> "${result_root}/logs/parse.log" 2>&1 || { log "WARNING: parser failed for ${backend}; see ${result_root}/logs/parse.log" } } write_backend_metadata() { local backend="$1" local result_root="${RESULT_BASE}/${RUN_ID}/${backend}" ensure_result_root "$result_root" local meta_json="${result_root}/results.json" local env_path if [[ "$backend" == "sglang" ]]; then env_path="$VENV_SGLANG" else env_path="$VENV_VLLM" fi write_metadata_json \ "$meta_json" \ "${EXPERIMENT_NAME}_${backend}" \ "$RUN_ID" \ "$MODEL_PATH" \ "$backend" \ "$backend" \ "$HARDWARE" \ "$ACCELERATOR" \ "$CHIP" \ "experiments/${EXPERIMENT_NAME}/run_bench.sh" \ "$env_path" \ "H200 long-context matrix ${backend} TP=8 for DeepSeek-V4-Flash" # Embed config. jq --arg backend "$backend" \ '.config = { "tp": 8, "cuda_visible_devices": "0,1,2,3,4,5,6,7", "backend": $backend, "server_start_script": "experiments/dsv4_h200_long_context_matrix/start_\($backend).sh", "groups": [ {"label": "64k", "max_model_len": 80000, "max_slots": 25}, {"label": "128k", "max_model_len": 140000, "max_slots": 10}, {"label": "256k", "max_model_len": 270000, "max_slots": 10} ] }' "$meta_json" > "${meta_json}.tmp" && mv "${meta_json}.tmp" "$meta_json" } # --------------------------------------------------------------------------- # Main # --------------------------------------------------------------------------- # Cleanup any leftovers. stop_server sglang stop_server vllm write_backend_metadata sglang write_backend_metadata vllm for group in "${CONTEXT_GROUPS[@]}"; do read -r group_label max_model_len max_slots <<< "$group" # Resolve scenario array for this group. scenario_array_name="SCENARIOS_${group_label^^}" if [[ -z "${!scenario_array_name:-}" ]]; then log "WARNING: no scenarios defined for group ${group_label}" continue fi # Run SGLang for this group. run_group sglang "$group_label" "$max_model_len" "$max_slots" "$scenario_array_name" # Run vLLM for this group. run_group vllm "$group_label" "$max_model_len" "$max_slots" "$scenario_array_name" done # Parse and compare. parse_backend sglang parse_backend vllm log "generating comparison report" "${VENV_CLIENT}/bin/python" "${SCRIPT_DIR}/../../scripts/common/compare.py" \ --sglang "${RESULT_BASE}/${RUN_ID}/sglang" \ --vllm "${RESULT_BASE}/${RUN_ID}/vllm" \ --output "${RESULT_BASE}/${RUN_ID}/comparison.md" \ >> "${log_dir_global}/compare.log" 2>&1 || { log "WARNING: comparison script failed; see ${log_dir_global}/compare.log" } log "all results saved to ${RESULT_BASE}/${RUN_ID}"