#!/bin/bash
# ARM 12, step one: put Kev on benchbox. Nothing here is measured.
#
# Kev is Apache-2.0 and so is its base, which is the whole reason arm 12 exists
# (GAPS-CARD.md §"Arm 12"): every OpenJev number in this bench is fenced
# CC BY-NC and can never become a seat, and this one could.
#
# THE ONE THING THAT NEEDS SAYING HERE: `flash-linear-attention` is NOT in Kev's
# own `serve` extra, and Kev's README says CUDA needs it for the Qwen3.5 models
# — without it the Gated DeltaNet layers run reference PyTorch. It is installed
# below, and whether it actually loaded is read from the SERVER's own startup
# rather than from this script's exit code. If it is absent, every latency, watt
# and joule row of arm 12 is labelled `reference-kernels` and is not compared
# with a fast-path number.
set -u
KEV=/workshop/kev
PIN=90990a5fac2995b9faa3190f7d437e84f2067768
echo "KEV SETUP START $(date -u +%FT%TZ)"
/bin/df -h / | tail -1

if [ ! -d "$KEV/.git" ]; then
  git clone https://github.com/jaredpalmer/kev.git "$KEV" || exit 1
fi
cd "$KEV" || exit 1
git fetch --depth 50 origin || true
git checkout -q "$PIN" 2>/dev/null || echo "NOTE: pinned commit $PIN not reachable; HEAD stays $(git rev-parse HEAD)"
echo "--- kev at commit: $(git rev-parse HEAD)  ($(git log -1 --format=%cI))"

export PATH=/workshop/.local/bin:$PATH
uv sync --extra serve
echo "SYNC RC=$?"
# the CUDA kernels the Qwen3.5 bases need; named as an addition, not a default
uv pip install flash-linear-attention
echo "FLA RC=$?"
uv run python -c "import torch, transformers, peft; print('torch', torch.__version__, 'transformers', transformers.__version__, 'peft', peft.__version__); print('cuda', torch.cuda.is_available(), torch.cuda.device_count())"
uv run python -c "import fla; print('flash-linear-attention', getattr(fla, '__version__', 'present'))" || echo "flash-linear-attention ABSENT — arm 12 rows will be labelled reference-kernels"
/bin/df -h / | tail -1
echo "KEV SETUP END $(date -u +%FT%TZ)"
