#!/usr/bin/env bash
# Rocky Tested: AirLLM claim test. Stop other GPU jobs first: the normal-load baseline needs the whole model in GPU memory.
# Throwaway --rm containers, models mounted read-only, no network, no keys. Raw JSON lines -> $OUT/results.jsonl
set -u
D=${WORK:-$PWD}
OUT=${OUT_DIR:-$D/out}; mkdir -p "$OUT"; cp "$D/bench.py" "$OUT/"
HUB=${HF_HUB:-$HOME/.cache/huggingface/hub}
M9=$(ls -d $HUB/models--Qwen--Qwen3.5-9B/snapshots/*/ | head -1)
M27=$(ls -d $HUB/models--Qwen--Qwen3.6-27B/snapshots/*/ | head -1)
R="docker run --rm --user $(id -u):$(id -g) -e HOME=/tmp -e USER=bench -e LOGNAME=bench --gpus all --network none --ipc host -e HF_HUB_OFFLINE=1 -v $HUB:$HUB:ro -v $D/shards:/shards -v $OUT:/out rockytest-airllm:fd3bd7e python3 /out/bench.py"
J=/out/results.jsonl
{ echo "start $(date -Is)"; free -g; } | tee "$OUT/env.txt"
docker image inspect rockytest-airllm:fd3bd7e --format '{{.Id}}' >> "$OUT/env.txt"
# 1) the claim: stream with GPU allocator capped at 4 GB
timeout 40m $R --mode airllm --model $M9  --shards /shards/9b --label "9B  AirLLM, 4 GB cap" --cap-gb 4 --out $J
timeout 90m $R --mode airllm --model $M27 --shards /shards/27b --label "27B AirLLM, 4 GB cap" --cap-gb 4 --out $J
# 2) baseline: same model, whole thing on the GPU, plain transformers
timeout 30m $R --mode baseline --model $M9  --shards /shards/9b --label "9B  normal full load" --out $J
timeout 40m $R --mode baseline --model $M27 --shards /shards/27b --label "27B normal full load" --out $J
echo "end $(date -Is)" | tee -a "$OUT/env.txt"
cp $OUT/results.jsonl $D/results-latest.jsonl 2>/dev/null
python3 - "$OUT/results.jsonl" <<'PY'
import json,sys
for l in open(sys.argv[1]):
    r=json.loads(l); print(r["label"], "ok" if r.get("ok") else "FAIL", r.get("tokens_per_sec"), "tok/s  torch-peak", r.get("torch_peak_allocated_gb"), "GB  rss", r.get("peak_rss_gb"), "GB", r.get("error",""))
PY
