#!/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}"
LOG_DIR="${OUTPUT_ROOT}/logs"
PROVENANCE_DIR="${OUTPUT_ROOT}/provenance"

PREFILL_SPECS=(q64 q128 q256 q512 q1k q2k q4k q8k)
PREFIX_EXTEND_SPECS=(q64s512 q128s1k q256s2k q512s4k q1ks8k)
DECODE_SPECS=(
  q1s128 8q1s128 16q1s128 32q1s128 64q1s128
  8q1s1k 16q1s1k 32q1s1k 64q1s1k
  8q1s2k 16q1s2k 32q1s2k 64q1s2k
  8q1s4k 16q1s4k 32q1s4k 64q1s4k
  8q1s8k 16q1s8k 32q1s8k 64q1s8k
)
MIXED_SPECS=(
  q64_8q1s512
  q128_8q1s1k q128_16q1s1k
  q256_16q1s2k q256_32q1s2k
  q512_32q1s4k q512_64q1s4k
  q1k_64q1s8k q2k_32q1s4k q4k_16q1s4k
)
BATCH_SPECS=(
  "${PREFILL_SPECS[@]}"
  "${PREFIX_EXTEND_SPECS[@]}"
  "${DECODE_SPECS[@]}"
  "${MIXED_SPECS[@]}"
)

mkdir -p "${LOG_DIR}" "${PROVENANCE_DIR}" "${OUTPUT_ROOT}/raw"
exec > >(tee -a "${LOG_DIR}/full.log") 2>&1

if [[ -z "${CUDA_VISIBLE_DEVICES:-}" ]]; then
  echo "ERROR: CUDA_VISIBLE_DEVICES must contain the fleet-allocated GPU" >&2
  exit 1
fi
IFS=',' read -r -a GPU_IDS <<< "${CUDA_VISIBLE_DEVICES}"
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 operators=FlashAttention3,reshape_and_cache_flash tp=${TP} cases=${#BATCH_SPECS[@]} regimes=prefill,prefix_extend,decode,true_mixed dtype=BF16 block=16 output=${OUTPUT_ROOT} expected_wall=4-10m hard_wall=1200s hard_gpu_cap=0.33_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_vllm020_flashattn.py

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

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

test -s "${OUTPUT_ROOT}/raw/flashattn-tp${TP}.json"
sha256sum "${OUTPUT_ROOT}/raw/flashattn-tp${TP}.json" \
  "${PROVENANCE_DIR}"/* > "${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_FULL_COMPLETE tp=${TP} cases=${#BATCH_SPECS[@]}"
