Files
leleka/scripts/laptop/lap_run.sh
T

98 lines
6.8 KiB
Bash

#!/usr/bin/env bash
# One laptop run of the integrated head (docs/laptop-profile.md, claude/laptop-opt): production-like configuration of the
# Original serve stack on the RTX 3080 laptop (mapped experts, VRAM tier auto + profile, KAR level 2 as a read-only file,
# MTP depth 4 with the draft vocabulary bound, spec-min-p 0.5, INT8 KV, seed on by default), holding ~/LAPTOP_GPU.lock.
#
# lap_run.sh LABEL TEST_BIN [NAME=VALUE ...] (RUN_CMD="..." runs that command instead of the test, same safeguards)
#
# Environment (all optional):
# PROMPTS file with one comma-separated token-ID line per prompt (default ~/lopt/prompts/short.ids)
# OUT results directory (default ~/lopt/results)
# COLD=1 evict the model files from the page cache first (posix_fadvise DONTNEED; no root needed)
# MON_INTERVAL sampling period of monitor.sh (default 1 s)
# NAME=VALUE arguments are exported, so any LELEKA_* switch of the test (and the engine) passes through.
# Safeguards (the laptop BSODed twice, 0x19C WIN32K_POWER_WATCHDOG_TIMEOUT, under the OEM 30 W profile): before the run the GPU
# state is recorded and the run is REFUSED (exit 4) when the power limit is below 100 W, a SW power cap / SW thermal slowdown is
# active at idle or the state is P5; during the run monitor.sh samples at 1 Hz and kills the run on P5 under load, a limit below
# 100 W, more than 90 C or a stall of the machine (> 10 s between samples; then stop all GPU work). The run is bounded by
# RUN_TIMEOUT (seconds, default 900) and a cool-down of COOLDOWN seconds (default 120) is kept after the previous run
# (stamp file ~/lopt/last_gpu_run).
# Writes OUT/LABEL.{env,stderr,timing,outputs,mem.tsv,mem.peak,gpu} and prints the request lines and the peaks.
set -uo pipefail
label="$1"; bin="$2"; shift 2
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
M="${MODEL_DIR:-$HOME/laptop/model}"
OUT="${OUT:-$HOME/lopt/results}"
mkdir -p "$OUT"; mkdir -p -m 700 "$HOME/laptop/mtp-staging"
export LD_LIBRARY_PATH="$(ls -d "$HOME"/cublas-12.9.1.4/x/*/lib 2>/dev/null | head -1):${LD_LIBRARY_PATH:-}"
export PATH="$HOME/cuda-toolkits/13.2.2/bin:$PATH"
export LELEKA_LAPTOP_BENCH=yes
export LELEKA_STRA169_IQ3S_SHARD0="$M/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf"
export LELEKA_STRA169_IQ3S_SHARD1="$M/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf"
export LELEKA_LAPTOP_KAR_FILE="$M/qwen-iq3-s-l2-v4.kar" LELEKA_STRA169_KAR_PROVENANCE="$M/qwen-iq3-s-l2-v4.kar.provenance.json"
export LELEKA_STRA169_MTP_PACK="$HOME/laptop/data/mtp-rt" LELEKA_STRA169_MTP_STAGING="$HOME/laptop/mtp-staging"
export LELEKA_ORIGINAL_SERVE_RT_DIR="${LELEKA_ORIGINAL_SERVE_RT_DIR-$HOME/laptop/data/mtp-rt}" # the draft vocabulary (--mtp-rt-dir)
export LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS="${LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS:-auto}"
export LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE="${LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE:-$HOME/laptop/data/expert-profile.bin}"
export LELEKA_LAPTOP_PROMPT_IDS="${PROMPTS:-$HOME/lopt/prompts/short.ids}"
export LELEKA_LAPTOP_PROMPT_CACHE="${LELEKA_LAPTOP_PROMPT_CACHE:-0}"
export LELEKA_LAPTOP_MAX_NEW="${LELEKA_LAPTOP_MAX_NEW:-128}" LELEKA_LAPTOP_REPEAT="${LELEKA_LAPTOP_REPEAT:-2}"
export LELEKA_BRINGUP_TRACE="${LELEKA_BRINGUP_TRACE:-1}"
for kv in "$@"; do export "${kv?}"; done
gpu_state() {
nvidia-smi --query-gpu=pstate,clocks.sm,clocks.max.sm,clocks.mem,clocks.max.mem,power.draw,power.limit,temperature.gpu,pcie.link.gen.current,pcie.link.width.current --format=csv,noheader
nvidia-smi -q -d PERFORMANCE | grep -E "SW Power Cap|SW Thermal|HW Slowdown|HW Thermal|HW Power" | tr -s ' ' | head -5 | paste -sd';'
}
preflight() {
local q lim
q="$(nvidia-smi -q -d PERFORMANCE,POWER)"
lim="$(awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); print $2; exit}' <<< "$q")"
echo "preflight: $(nvidia-smi --query-gpu=pstate,clocks.sm,clocks.mem,power.draw,temperature.gpu --format=csv,noheader) limit=${lim:-?} W"
if ! awk -v l="${lim:-0}" 'BEGIN {exit !(l+0 >= 100)}'; then echo "REFUSED: power limit ${lim:-?} W < 100 W"; return 1; fi
if grep -E "SW Power Cap|SW Thermal Slowdown" <<< "$q" | grep -q ": Active"; then echo "REFUSED: SW power cap / thermal slowdown active at idle"; return 1; fi
if grep -E "Performance State" <<< "$q" | grep -q "P5"; then echo "REFUSED: performance state P5"; return 1; fi
}
until mkdir "$HOME/LAPTOP_GPU.lock" 2>/dev/null; do echo "laptop GPU lock held by: $(cat "$HOME/LAPTOP_GPU.lock/owner" 2>/dev/null)"; sleep 20; done
echo "claude/laptop-opt $label $(date -u +%FT%TZ)" > "$HOME/LAPTOP_GPU.lock/owner"
trap 'rm -rf "$HOME/LAPTOP_GPU.lock"; date +%s > "$HOME/lopt/last_gpu_run"' EXIT
last=$(cat "$HOME/lopt/last_gpu_run" 2>/dev/null || echo 0); wait_s=$(( ${COOLDOWN:-120} - ($(date +%s) - last) ))
if [ "$wait_s" -gt 0 ]; then echo "cool-down ${wait_s} s"; sleep "$wait_s"; fi
if ! preflight | tee "$OUT/$label.preflight"; then echo "refused: see $OUT/$label.preflight"; exit 4; fi
if [[ "${COLD:-}" == 1 ]]; then
python3 -I - "$M"/*.gguf "$M"/*.kar <<'PY'
import os, sys
for p in sys.argv[1:]:
fd = os.open(p, os.O_RDONLY)
os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED)
os.close(fd)
PY
fi
{ echo "# $label $(date -u +%FT%TZ) cold=${COLD:-0}"; echo "MemAvailable_MiB=$(awk '$1=="MemAvailable:" {printf "%d", $2/1024}' /proc/meminfo) Cached_MiB=$(awk '$1=="Cached:" {printf "%d", $2/1024}' /proc/meminfo)"
echo "gpu_before: $(gpu_state)"; } > "$OUT/$label.gpu"
env | grep -E '^LELEKA_' | sort > "$OUT/$label.env"
sha256sum "$bin" 2>/dev/null | cut -c1-16 >> "$OUT/$label.env"
if [[ -n "${RUN_CMD:-}" ]]; then # any other GPU job under the same safeguards (microbenchmarks, ...)
timeout -s TERM -k 20 "${RUN_TIMEOUT:-900}" bash -c "$RUN_CMD" > "$OUT/$label.stdout" 2> "$OUT/$label.stderr" &
else
timeout -s TERM -k 20 "${RUN_TIMEOUT:-900}" "$bin" --ignored --exact backend::original_serve_laptop::original_serve_laptop_bench --nocapture --test-threads 1 \
> "$OUT/$label.stdout" 2> "$OUT/$label.stderr" &
fi
tpid=$!
sleep 1
pid="$(pgrep -P "$tpid" | head -1)"; pid="${pid:-$tpid}" # the test itself, not the timeout wrapper
bash "$here/monitor.sh" "$pid" "$OUT/$label.mem.tsv" > "$OUT/$label.mem.peak" &
mon=$!
wait "$tpid"; rc=$?
wait "$mon" || true
{ echo "gpu_after: $(gpu_state)"; echo "exit=$rc"; [ -f "$OUT/$label.mem.tsv.abort" ] && echo "ABORT: $(cat "$OUT/$label.mem.tsv.abort")"; } >> "$OUT/$label.gpu"
cat "$OUT/$label.mem.peak" >> "$OUT/$label.gpu"
grep -hE '^LAPTOP (load|config|request|tier|failed|done)' "$OUT/$label.stderr" "$OUT/$label.stdout" \
| sed -E 's/ (VmRSS|VmHWM|RssAnon|RssFile|RssShmem|VmLck|VmPin|VmSwap|MemAvailable|SwapFree)=/ \1=/g' > "$OUT/$label.timing"
grep -hE '^LAPTOP output' "$OUT/$label.stderr" | sed 's/^LAPTOP //' > "$OUT/$label.outputs"
grep -E '^LAPTOP (request|failed|done)' "$OUT/$label.timing" | sed -E 's/; VmRSS.*//'
cat "$OUT/$label.mem.peak"
echo "exit=$rc label=$label"