#!/usr/bin/env bash

set -euo pipefail

TP="${TP:?TP must be set to 1, 2, or 4}"
case "${TP}" in
  1|2|4) ;;
  *) echo "ERROR: invalid TP=${TP}" >&2; exit 1 ;;
esac

OUTPUT_ROOT="${OUTPUT_ROOT:?OUTPUT_ROOT must be set}"
VENV_ROOT="${VENV_ROOT:-/tmp/wjh/venvs/vllm-0.20.0-cu129-profiler-v1}"
VLLM_SOURCE="${VLLM_SOURCE:-/home/admin/cpfs/wjh/agentic-kv/third_party/vllm_v20_build}"
MODEL="${MODEL:-/home/admin/cpfs/wjh/models/Qwen/Qwen3-30B-A3B}"
CAMPAIGN_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROFILE_SCRIPT="${CAMPAIGN_ROOT}/../frontier-qwen30-vllm020-profile-v1/profile_vllm020_flashattn.py"
BATCH_SPECS=(
  q1s16k 8q1s16k 16q1s16k 32q1s16k 64q1s16k
  q1s32k 8q1s32k 16q1s32k 32q1s32k 64q1s32k
  q1s40k 8q1s40k 16q1s40k 32q1s40k 64q1s40k
  q512_8q1s16k q1k_16q1s16k q2k_32q1s16k
  q4k_16q1s32k q2k_32q1s32k q1k_64q1s32k
)

mkdir -p "${OUTPUT_ROOT}/logs" "${OUTPUT_ROOT}/provenance" "${OUTPUT_ROOT}/raw"
exec > >(tee -a "${OUTPUT_ROOT}/logs/long-context.log") 2>&1

IFS=',' read -r -a GPU_IDS <<< "${CUDA_VISIBLE_DEVICES:?fleet GPU is required}"
if [[ "${#GPU_IDS[@]}" -ne 1 ]]; then
  echo "ERROR: expected exactly one GPU, got ${CUDA_VISIBLE_DEVICES}" >&2
  exit 1
fi

echo "PROFILE_LAUNCH_ECHO host=$(hostname) gpu=${CUDA_VISIBLE_DEVICES} model=${MODEL} runtime=vLLM-0.20.0+cu129 operator=FlashAttention3 tp=${TP} batch_specs=${BATCH_SPECS[*]} profile_script=${PROFILE_SCRIPT} output=${OUTPUT_ROOT} expected_wall=1-5m hard_wall=900s hard_gpu_cap=0.25_H20h"
date -u +"START_UTC=%Y-%m-%dT%H:%M:%SZ"
nvidia-smi --query-gpu=index,name,driver_version,memory.used,utilization.gpu --format=csv,noheader

test -x "${VENV_ROOT}/bin/python"
test -f "${VLLM_SOURCE}/benchmarks/attention_benchmarks/runner.py"
test -f "${MODEL}/config.json"
test -f "${PROFILE_SCRIPT}"

git -C "${CAMPAIGN_ROOT}/../.." rev-parse HEAD > "${OUTPUT_ROOT}/provenance/aituner.commit"
git -C "${VLLM_SOURCE}" rev-parse HEAD > "${OUTPUT_ROOT}/provenance/vllm-source.commit"
sha256sum "${PROFILE_SCRIPT}" "${BASH_SOURCE[0]}" > "${OUTPUT_ROOT}/provenance/source.sha256"
uv pip freeze --python "${VENV_ROOT}/bin/python" > "${OUTPUT_ROOT}/provenance/pip-freeze.txt"
nvidia-smi --query-gpu=index,uuid,name,driver_version,memory.total --format=csv,noheader > "${OUTPUT_ROOT}/provenance/gpus.csv"
printf '%s\n' "${BATCH_SPECS[@]}" > "${OUTPUT_ROOT}/provenance/batch-specs.txt"

timeout --signal=TERM --kill-after=30s 780 \
  "${VENV_ROOT}/bin/python" "${PROFILE_SCRIPT}" \
  --vllm-source "${VLLM_SOURCE}" \
  --model "${MODEL}" \
  --output "${OUTPUT_ROOT}/raw/flashattn-long-context-tp${TP}.json" \
  --tp "${TP}" \
  --batch-specs "${BATCH_SPECS[@]}" \
  --warmup-iters 5 \
  --repeats 10 \
  --profile-kv-update

test -s "${OUTPUT_ROOT}/raw/flashattn-long-context-tp${TP}.json"
sha256sum "${OUTPUT_ROOT}/raw/flashattn-long-context-tp${TP}.json" "${OUTPUT_ROOT}/provenance"/* > "${OUTPUT_ROOT}/artifacts.sha256"
nvidia-smi --query-gpu=index,name,memory.used,utilization.gpu --format=csv,noheader
date -u +"END_UTC=%Y-%m-%dT%H:%M:%SZ"
echo "FLASHATTN_LONG_CONTEXT_COMPLETE tp=${TP} cases=${#BATCH_SPECS[@]}"
