78 lines
5.7 KiB
Bash
78 lines
5.7 KiB
Bash
#!/usr/bin/env bash
|
|
# One GPU stretch of the laptop validation: wait for the machine, take the lock, start the
|
|
# engine with `leleka up --profile ARM.json`, replay one agent-shaped conversation, stop the engine, release the lock. Inside WSL2.
|
|
#
|
|
# lap_stretch.sh LABEL ARM.json FIRST_TOKENS [FOLLOWUPS=3]
|
|
#
|
|
# Safety protocol (owner, 2026-11-13): one GPU run at a time (lock file C:\Users\pavel\work\leleka\.git\laptop-gpu-run.lock, agent name + UTC
|
|
# start; created only when no cargo / rustc / link runs in WSL or on Windows); the whole stretch <= 15 min; `lap_guard.sh` samples every
|
|
# 15 s and stops the engine at 85 C, at P5 with a power cap, at a limit < 100 W under load, at MemAvailable < 2.5 GB, at a 300 s log stall with
|
|
# the GPU busy; no stretch starts at a limit < 100 W, with a SW power cap / thermal slowdown at idle, P5, WSL MemAvailable < 10 GB or Windows
|
|
# available RAM < 10 GB (the stretch waits for that, like for the builds). A cool-down of 120 s follows the previous stretch. Results: <worktree>/target/results/LABEL/.
|
|
set -uo pipefail
|
|
label="$1"; arm="$2"; first_tokens="$3"; followups="${4:-3}"
|
|
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; wt="$(cd "$here/../.." && pwd)"
|
|
bin="$wt/target/release/leleka"; res="$wt/target/results/$label"
|
|
state="$HOME/.local/state/leleka"; elog="$state/logs/engine.log"
|
|
cublas="$HOME/cublas-12.9.1.4/x/libcublas-linux-x86_64-12.9.1.4-archive/lib"
|
|
agent="${LAP_AGENT:-laptop-3080-live}"; budget_s="${LAP_BUDGET_S:-720}" # GPU-busy budget, counted from the engine being ready: the load (cold page cache: 4-10 min) keeps the GPU idle at P8
|
|
mkdir -p "$res"; stamp="$wt/target/results/last_gpu_stretch"
|
|
|
|
source "$here/lap_lib.sh"
|
|
lap_wait_and_lock "$label" # polls every 2 min, then holds the lock
|
|
start=$(date +%s)
|
|
mine=0 # set once this script has verified that no other Leleka service runs; only then does cleanup stop one
|
|
cleanup() {
|
|
touch "$res/guard.tsv.done"
|
|
[ "$mine" = 1 ] && { "$bin" down > "$res/down.log" 2>&1 || true; }
|
|
for kv in ${LAP_ENV:-}; do systemctl --user unset-environment "${kv%%=*}"; done
|
|
date +%s > "$stamp"; lap_done; echo "lock released $(date -u +%FT%TZ)"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
# 2. cool-down and preflight
|
|
last=$(cat "$stamp" 2>/dev/null || echo 0); wait_s=$((120 - ($(date +%s) - last)))
|
|
[ "$wait_s" -gt 0 ] && { echo "cool-down $wait_s s"; sleep "$wait_s"; }
|
|
lap_preflight "$res" || { echo "REFUSED"; exit 4; }
|
|
# The user service and its state directory are shared with other agents and the owner: a service that is already up (or restarting) is theirs and is
|
|
# left alone. (2026-11-14: a replay of this script ran against somebody else's engine, and its cleanup stopped that engine.)
|
|
if systemctl --user is-active --quiet leleka.service || pgrep -f bin/leleka-inference > /dev/null; then
|
|
echo "REFUSED: a Leleka service is already running or starting (not started by this script); it is left alone"; exit 6
|
|
fi
|
|
mine=1
|
|
nvidia-smi -q -d PERFORMANCE,POWER > "$res/gpu_before.txt"
|
|
|
|
# 3. engine up (the guard runs from here on)
|
|
# `leleka up` reads the tail of engine.log for a "REFUSING TO START" line: a refusal of an earlier start made every later start report
|
|
# "the engine refused the load" after 7 s. Keep the old log with the results and start from an empty one.
|
|
cp "$elog" "$res/engine.before.log" 2>/dev/null; : > "$elog"
|
|
size0=0
|
|
bash "$here/lap_guard.sh" "$res/guard.tsv" "$elog" "$bin" 15 2> "$res/guard.err" &
|
|
guard=$!
|
|
# LAP_ENV="NAME=VALUE ..." puts diagnostic switches into the user manager's environment for this stretch only (the profile allow-list
|
|
# does not carry LELEKA_MMQ_*, LELEKA_RING_*, LELEKA_CHUNK_*, LELEKA_BRINGUP_TRACE); cleanup removes them again.
|
|
for kv in ${LAP_ENV:-}; do systemctl --user set-environment "$kv"; done; echo "${LAP_ENV:-}" > "$res/lap_env.txt"
|
|
"$bin" up --profile "$arm" --assets "$HOME/leleka-assets" --preset "${LAP_PRESET:-dedicated}" --cublas-dir "$cublas" --timeout-s 1200 > "$res/up.log" 2>&1
|
|
up_rc=$?; tr '\0' '\n' < "/proc/$(pgrep -f bin/leleka-inference | head -1)/environ" 2>/dev/null | grep -E '^(LELEKA|STRATA)_' > "$res/engine.environ"
|
|
ready_ts=$(date +%s)
|
|
echo "up exit=$up_rc after $(( $(date +%s) - start )) s" | tee -a "$res/up.log"
|
|
tail -c +$((size0 + 1)) "$elog" > "$res/engine.log" 2>/dev/null
|
|
|
|
# 4. the replay, bounded by what is left of the 15 minutes
|
|
left=$(( budget_s - ($(date +%s) - ready_ts) - 60 ))
|
|
if curl -fs -o /dev/null -H "Authorization: Bearer $(cat "$state/api-key")" http://127.0.0.1:8081/v1/models && [ "$left" -gt 60 ]; then
|
|
shape=(--first-tokens "$first_tokens" --followups "$followups"); [ -n "${LAP_LADDER:-}" ] && shape=(--ladder "$LAP_LADDER" --max-output 16) # LAP_LADDER=4096,8192,...: cold prompts of growing size
|
|
timeout -s TERM "$left" python3 "$here/lap_replay.py" --label "$label" "${shape[@]}" --key-file "$state/api-key" --out "$res/replay.jsonl" > "$res/replay.out" 2>&1; echo "replay exit=$? (budget ${left} s)"
|
|
else echo "engine not ready or no time left ($left s)"; fi
|
|
tail -c +$((size0 + 1)) "$elog" > "$res/engine.log" 2>/dev/null
|
|
cp "$state/leleka.json" "$res/leleka.json" 2>/dev/null
|
|
[ -e "$res/guard.tsv.abort" ] && echo "GUARD ABORT: $(head -1 "$res/guard.tsv.abort")"
|
|
|
|
# 5. summary of the sampler (peaks and means); cleanup (engine down, lock released) runs on exit
|
|
touch "$res/guard.tsv.done"; sleep 16
|
|
awk -F'\t' 'NR>1 && $2+0>0 {n++; if ($2>tm) tm=$2; if ($10>vm) vm=$10; if (mn==""||$11<mn) mn=$11; if ($13>rm) rm=$13; if ($6>pm) pm=$6;
|
|
if ($9+0>=50) {b++; sm+=$4; mc+=$5; pw+=$6; if (ln==""||$7<ln) ln=$7}}
|
|
END {printf "samples=%d max_temp=%dC peak_vram=%dMiB min_wsl_avail=%dMiB peak_engine_rss=%dMiB peak_power=%sW busy_samples=%d mean_sm=%dMHz mean_mem=%dMHz mean_power=%.0fW min_limit=%sW\n",
|
|
n,tm,vm,mn,rm,pm,b,(b?sm/b:0),(b?mc/b:0),(b?pw/b:0),ln}' "$res/guard.tsv" | tee "$res/peaks.txt"
|
|
echo "stretch $label took $(( $(date +%s) - start )) s"
|