#!/usr/bin/env bash

set -euo pipefail

TP="${TP:?TP must be set to 2, 4, or 8}"
case "${TP}" in
  2) HARD_GPU_CAP="0.40_H20h" ;;
  4) HARD_GPU_CAP="0.80_H20h" ;;
  8) HARD_GPU_CAP="1.60_H20h" ;;
  *) 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}"
NUM_TOKENS="${NUM_TOKENS:-8}"
HIDDEN_DIM="${HIDDEN_DIM:-2048}"
COLLECTIVE_CONTRACT="${COLLECTIVE_CONTRACT:-flashinfer-trtllm}"
TRIALS="${TRIALS:-1}"
REPEATS="${REPEATS:-10}"
mkdir -p "${OUTPUT_ROOT}/logs" "${OUTPUT_ROOT}/provenance" "${OUTPUT_ROOT}/raw"
exec > >(tee -a "${OUTPUT_ROOT}/logs/profile.log") 2>&1

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

case "${COLLECTIVE_CONTRACT}" in
  flashinfer-trtllm)
    export VLLM_ALLREDUCE_USE_FLASHINFER=1
    export VLLM_FLASHINFER_ALLREDUCE_BACKEND=trtllm
    export VLLM_ALLREDUCE_USE_SYMM_MEM=0
    ;;
  qwen235-serving-projected)
    export VLLM_ALLREDUCE_USE_FLASHINFER=1
    export VLLM_FLASHINFER_ALLREDUCE_BACKEND=trtllm
    export VLLM_ALLREDUCE_USE_SYMM_MEM=1
    ;;
  *) echo "ERROR: invalid COLLECTIVE_CONTRACT=${COLLECTIVE_CONTRACT}" >&2; exit 1 ;;
esac
echo "PROFILE_LAUNCH_ECHO host=$(hostname) gpus=${CUDA_VISIBLE_DEVICES} model=${MODEL} runtime=vLLM-0.20.0+cu129 operator=tensor_model_parallel_all_reduce contract=${COLLECTIVE_CONTRACT} tp=${TP} tokens=${NUM_TOKENS} hidden=${HIDDEN_DIM} dtype=BF16 trials=${TRIALS} repeats=${REPEATS} output=${OUTPUT_ROOT} expected_wall=2-8m hard_wall=720s hard_gpu_cap=${HARD_GPU_CAP}"
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

git rev-parse HEAD > "${OUTPUT_ROOT}/provenance/aituner.commit"
git -C "${VLLM_SOURCE}" rev-parse HEAD > "${OUTPUT_ROOT}/provenance/vllm-source.commit"
sha256sum "${MODEL}/config.json" > "${OUTPUT_ROOT}/provenance/model-config.sha256"
sha256sum profile_vllm020_allreduce.py run_allreduce_profile.sh \
  > "${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"

read -r -a TOKEN_ARGS <<< "${NUM_TOKENS}"
timeout --signal=TERM --kill-after=30s 600 \
  "${VENV_ROOT}/bin/torchrun" --standalone --nproc_per_node="${TP}" \
  profile_vllm020_allreduce.py \
  --vllm-source "${VLLM_SOURCE}" \
  --model "${MODEL}" \
  --output "${OUTPUT_ROOT}/raw/allreduce-tp${TP}.json" \
  --num-tokens "${TOKEN_ARGS[@]}" \
  --hidden-dim "${HIDDEN_DIM}" \
  --collective-contract "${COLLECTIVE_CONTRACT}" \
  --warmup-iters 3 \
  --repeats "${REPEATS}" \
  --trials "${TRIALS}"

test -s "${OUTPUT_ROOT}/raw/allreduce-tp${TP}.json"
sha256sum "${OUTPUT_ROOT}/raw/allreduce-tp${TP}.json" \
  "${OUTPUT_ROOT}/provenance"/* > "${OUTPUT_ROOT}/artifacts.sha256"
date -u +"END_UTC=%Y-%m-%dT%H:%M:%SZ"
echo "ALLREDUCE_PROFILE_COMPLETE tp=${TP} tokens=${NUM_TOKENS}"
