#!/bin/bash
# Start plain vLLM (no DSpark) for DeepSeek-V4-Flash.
set -e

cd /data/user1/yy
mkdir -p logs

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/config.env"

VENV="${VENV_VLLM}"
export PATH="$VENV/bin:$PATH"
export PYTHONUNBUFFERED=1
export TMPDIR=/data/user1/yy/tmp
export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES}"

LOG="/data/user1/yy/logs/dsv4_h200_vllm_dspark_vs_default_default_$(date +%Y%m%d_%H%M%S).log"
PID_FILE="/data/user1/yy/dsv4_h200_vllm_dspark_vs_default_default.pid"

rm -f "$PID_FILE"

echo "=== Starting DeepSeek-V4-Flash vLLM default baseline (TP=$TP) ==="
echo "Model: $DEFAULT_MODEL_PATH"
echo "Port: $DEFAULT_PORT"
echo "Log: $LOG"

nohup vllm serve "$DEFAULT_MODEL_PATH" \
    --trust-remote-code \
    --tensor-parallel-size "$TP" \
    --kv-cache-dtype fp8 \
    --max-model-len "$MAX_MODEL_LEN" \
    --max-num-seqs "$MAX_NUM_SEQS" \
    --block-size 256 \
    --gpu-memory-utilization 0.90 \
    --tokenizer-mode deepseek_v4 \
    --reasoning-parser deepseek_v4 \
    --no-disable-hybrid-kv-cache-manager \
    --disable-uvicorn-access-log \
    --port "$DEFAULT_PORT" \
    > "$LOG" 2>&1 &

PID=$!
echo $PID > "$PID_FILE"
echo "PID: $PID"
echo "Waiting for health..."

for i in $(seq 1 240); do
    if curl --fail --silent --show-error --max-time 5 "http://127.0.0.1:${DEFAULT_PORT}/health" >/dev/null 2>&1; then
        echo "Default vLLM server is ready at http://127.0.0.1:${DEFAULT_PORT}"
        echo "Log: $LOG"
        exit 0
    fi
    if ! kill -0 $PID 2>/dev/null; then
        echo "ERROR: Default vLLM server exited early"
        tail -200 "$LOG"
        exit 1
    fi
    echo "Waiting... ($i/240)"
    sleep 5
done

echo "ERROR: Default vLLM server not healthy after 240 retries"
tail -200 "$LOG"
exit 1
