#!/usr/bin/env bash

# DeepSeek-V4-Pro B200 single-node vLLM MTP variant of dsv4_fp4_b200_vllm.sh.
# Adds --speculative-config '{"method":"mtp","num_speculative_tokens":2}' and
# routes prompts through chat-formatted encoding via --dsv4 (required for
# meaningful MTP acceptance numbers).

source "$(dirname "$0")/../../benchmark_lib.sh"

check_env_vars \
    MODEL \
    TP \
    DP_ATTENTION \
    CONC \
    ISL \
    OSL \
    MAX_MODEL_LEN \
    RANDOM_RANGE_RATIO \
    RESULT_FILENAME

if [[ -n "$SLURM_JOB_ID" ]]; then
  echo "JOB $SLURM_JOB_ID running on $SLURMD_NODENAME"
fi

nvidia-smi

if [[ "$MODEL" != /* ]]; then hf download "$MODEL"; fi

SERVER_LOG=/workspace/server.log

# DeepSeek-V4-Pro weights are large; engine startup can exceed the default
# 600s. Give it an hour to load.
export VLLM_ENGINE_READY_TIMEOUT_S=3600

PARALLEL_ARGS=(--tensor-parallel-size "$TP" --data-parallel-size 1)
if [ "${DP_ATTENTION}" = "true" ]; then
    PARALLEL_ARGS=(--tensor-parallel-size 1 --data-parallel-size "$TP")
fi

EP_ARGS=()
if [ "${EP_SIZE:-1}" -gt 1 ]; then
    EP_ARGS=(--enable-expert-parallel)
fi

# Mega-MoE backend and the lower GMU only kick in on the DP-attn path,
# per the vLLM v0.20.0 DeepSeek-V4-Pro recipe. All configs share the
# FULL_AND_PIECEWISE compilation config.
GMU_ARGS=()
MOE_ARGS=()
PREFILL_SCHEDULE_ARGS=()
if [ "${DP_ATTENTION}" = "true" ]; then
    GMU_ARGS=(--gpu-memory-utilization 0.9)
    MOE_ARGS=(--moe-backend deep_gemm_mega_moe)
    PREFILL_SCHEDULE_ARGS=(--prefill-schedule-interval 4)
fi

MAX_NUM_BATCHED_TOKENS=$(( ISL * 2 ))
BENCHMARK_MAX_MODEL_LEN="$MAX_MODEL_LEN"

if [ "${EVAL_ONLY}" = "true" ]; then
    EVAL_MAX_MODEL_LEN=$(compute_eval_context_length "$MODEL" "$BENCHMARK_MAX_MODEL_LEN")
    export EVAL_MAX_MODEL_LEN
    SERVE_MAX_MODEL_LEN="$EVAL_MAX_MODEL_LEN"
else
    SERVE_MAX_MODEL_LEN="$BENCHMARK_MAX_MODEL_LEN"
fi

# use 2 speculative tokens for all configs for now
NUM_SPEC_TOKENS=2

# Start GPU monitoring (power, temperature, clocks every second)
start_gpu_monitor

set -x
vllm serve "$MODEL" --host 0.0.0.0 --port "$PORT" \
    --trust-remote-code \
    --kv-cache-dtype fp8 \
    --block-size 256 \
    --no-enable-prefix-caching \
    "${PARALLEL_ARGS[@]}" \
    "${EP_ARGS[@]}" \
    "${GMU_ARGS[@]}" \
    "${MOE_ARGS[@]}" \
    "${PREFILL_SCHEDULE_ARGS[@]}" \
    --compilation-config '{"cudagraph_mode":"FULL_AND_PIECEWISE","custom_ops":["all"]}' \
    --attention_config.use_fp4_indexer_cache=True \
    --tokenizer-mode deepseek_v4 \
    --tool-call-parser deepseek_v4 \
    --enable-auto-tool-choice \
    --reasoning-parser deepseek_v4 \
    --max-cudagraph-capture-size 2048 \
    --speculative-config "{\"method\": \"mtp\", \"num_speculative_tokens\": $NUM_SPEC_TOKENS}" \
    --max-model-len "$SERVE_MAX_MODEL_LEN" \
    --max-num-batched-tokens "$MAX_NUM_BATCHED_TOKENS" > "$SERVER_LOG" 2>&1 &

SERVER_PID=$!

# Wait for server to be ready
wait_for_server_ready --port "$PORT" --server-log "$SERVER_LOG" --server-pid "$SERVER_PID"

pip install -q datasets pandas

# MTP acceptance rate degrades on raw random tokens; --dsv4 routes prompts
# through chat-formatted encoding as required for speculative decoding benchmarks.
run_benchmark_serving \
    --model "$MODEL" \
    --port "$PORT" \
    --backend vllm \
    --input-len "$ISL" \
    --output-len "$OSL" \
    --random-range-ratio "$RANDOM_RANGE_RATIO" \
    --num-prompts "$((CONC * 10))" \
    --max-concurrency "$CONC" \
    --result-filename "$RESULT_FILENAME" \
    --result-dir /workspace/ \
    --trust-remote-code \
    --dsv4

# After throughput, run evaluation only if RUN_EVAL is true
if [ "${RUN_EVAL}" = "true" ]; then
    run_eval --framework lm-eval --port "$PORT"
    append_lm_eval_summary
fi

# Stop GPU monitoring
stop_gpu_monitor
set +x
