diff --git a/scripts/laptop/lap_guard.sh b/scripts/laptop/lap_guard.sh index f6fef6b4..da61fa3e 100644 --- a/scripts/laptop/lap_guard.sh +++ b/scripts/laptop/lap_guard.sh @@ -11,9 +11,11 @@ # LIMIT power limit under 100 W while the GPU is busy # MEM MemAvailable of the VM under 2,500 MiB # STALL the engine log has not grown for 300 s while the GPU utilisation was >= 95 % in the last 8 samples +# TURBO the status file of turbo-watchdog.ps1 is fresh and says ok:false (turbo_status.sh; absent or stale is not a trip) # GAP two samples more than 4 intervals apart (the machine stalled): all GPU work must stop # A trip writes OUT.tsv.abort and runs `LELEKA_BIN down`, then TERMs leleka-inference. Columns of OUT.tsv: unix time, temperature, -# pstate, SM MHz, memory MHz, power W, limit W, throttle reasons, utilisation, VRAM MiB, MemAvailable MiB, engine log bytes. +# pstate, SM MHz, memory MHz, power W, limit W, throttle reasons, utilisation, VRAM MiB, MemAvailable MiB, engine log bytes, +# engine RSS MiB (leleka-inference). set -u out="$1"; log="$2"; bin="$3"; interval="${4:-15}" rm -f "$out.abort" "$out.done" @@ -22,7 +24,7 @@ trip() { echo "$1 at $(date -u +%FT%TZ)" > "$out.abort" echo "GUARD TRIP: $1" >&2 "$bin" down >> "$out.abort" 2>&1 || true - pkill -TERM -x leleka-inference 2>/dev/null || true + pkill -TERM -f bin/leleka-inference 2>/dev/null || true exit 3 } prev=$(date +%s); busy=0; log_size=0; log_changed=$prev @@ -30,18 +32,21 @@ while [ ! -e "$out.done" ]; do IFS=, read -r temp ps smc memc pw thr lim util vram < <(nvidia-smi \ --query-gpu=temperature.gpu,pstate,clocks.sm,clocks.mem,power.draw,clocks_throttle_reasons.active,power.limit,utilization.gpu,memory.used \ --format=csv,noheader,nounits 2>/dev/null | tr -d ' ') + # the query field power.limit reads [N/A] on this laptop: take the limit from the POWER section + lim=$(nvidia-smi -q -d POWER 2>/dev/null | awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); printf "%d", $2; exit}') now=$(date +%s) avail=$(awk '$1=="MemAvailable:" {printf "%d", $2/1024}' /proc/meminfo) size=$(stat -c %s "$log" 2>/dev/null || echo 0) if [ "$size" != "$log_size" ]; then log_size=$size; log_changed=$now; fi printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$now" "${temp:-0}" "${ps:-}" "${smc:-0}" "${memc:-0}" "${pw:-0}" "${lim:-0}" \ - "${thr:-0x0}" "${util:-0}" "${vram:-0}" "$avail" "$size" >> "$out" + "${thr:-0x0}" "${util:-0}" "${vram:-0}" "$avail" "$size" "${rss:-0}" >> "$out" if [ "${util:-0}" -ge 95 ] 2>/dev/null; then busy=$((busy + 1)); else busy=0; fi [ $((now - prev)) -gt $((interval * 4)) ] && trip "GAP $((now - prev)) s between samples" [ "${temp:-0}" -ge 85 ] 2>/dev/null && trip "TEMP ${temp} C" [ "${ps:-}" = P5 ] && [ "${memc:-0}" -lt 2000 ] 2>/dev/null && [ $(( ${thr:-0x0} & 0x24 )) -ne 0 ] 2>/dev/null && trip "P5CAP reasons ${thr}" [ "${util:-0}" -ge 50 ] 2>/dev/null && awk -v l="${lim:-0}" 'BEGIN {exit !(l+0 > 0 && l+0 < 100)}' && trip "LIMIT ${lim} W" [ "$avail" -lt 2500 ] && trip "MEM ${avail} MiB" + turbo=$(bash "$(dirname "${BASH_SOURCE[0]}")/turbo_status.sh"); [ "${turbo%% *}" = BAD ] && trip "TURBO watchdog: $turbo" [ "$busy" -ge 8 ] && [ $((now - log_changed)) -gt 300 ] && trip "STALL log idle $((now - log_changed)) s with the GPU busy" prev=$now sleep "$interval" diff --git a/scripts/laptop/lap_stretch.sh b/scripts/laptop/lap_stretch.sh new file mode 100644 index 00000000..663f674d --- /dev/null +++ b/scripts/laptop/lap_stretch.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# One GPU stretch of the laptop validation (docs/laptop-profile.md "Update 2026-11-13"): 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: /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" +lock=/mnt/c/Users/pavel/work/leleka/.git/laptop-gpu-run.lock +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" + +busy_builds() { + pgrep -x cargo >/dev/null || pgrep -x rustc >/dev/null || pgrep -x ld >/dev/null || pgrep -x cc1plus >/dev/null && return 0 + [ "$(powershell.exe -NoProfile -Command 'Get-Process cargo,rustc,link,lld-link -ErrorAction SilentlyContinue | Measure-Object | % Count' | tr -d '\r')" != 0 ] +} +# Windows "available" physical memory (free + standby): the figure the protocol's 10 GB rule is about. The WSL VM's page cache is held by +# vmmemWSL and is not available to Windows, so a stretch on a busy host waits. +windows_free_gb() { + powershell.exe -NoProfile -Command 'Add-Type -AssemblyName Microsoft.VisualBasic; [math]::Floor((New-Object Microsoft.VisualBasic.Devices.ComputerInfo).AvailablePhysicalMemory/1GB)' | tr -dc '0-9' +} +limit_w() { nvidia-smi -q -d POWER | awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); printf "%d", $2; exit}'; } + +# 1. wait for the builds to finish, then take the lock atomically +until ! busy_builds && [ "$(windows_free_gb)" -ge 10 ] && (set -C; echo "$agent $(date -u +%FT%TZ) $label" > "$lock") 2>/dev/null; do sleep 20; done +start=$(date +%s) +cleanup() { + touch "$res/guard.tsv.done" + "$bin" down > "$res/down.log" 2>&1 || true + for kv in ${LAP_ENV:-}; do systemctl --user unset-environment "${kv%%=*}"; done + date +%s > "$stamp"; rm -f "$lock"; 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"; } +lim=$(limit_w); q=$(nvidia-smi -q -d PERFORMANCE,POWER); avail=$(awk '$1=="MemAvailable:" {printf "%d", $2/1048576}' /proc/meminfo); wfree=$(windows_free_gb) +echo "preflight: limit=${lim:-?} W $(nvidia-smi --query-gpu=pstate,temperature.gpu,clocks.sm --format=csv,noheader) wsl_avail=${avail} GB win_avail=${wfree} GB" | tee "$res/preflight.txt" +{ [ "${lim:-0}" -ge 100 ] && ! grep -E "SW Power Cap|SW Thermal Slowdown" <<< "$q" | grep -q ": Active" \ + && ! grep "Performance State" <<< "$q" | grep -q P5 && [ "$avail" -ge 10 ] && [ "${wfree:-0}" -ge 10 ]; } || { echo "REFUSED"; exit 4; } +turbo=$(bash "$here/turbo_status.sh"); echo "turbo watchdog: $turbo" | tee -a "$res/preflight.txt" +[ "${turbo%% *}" = BAD ] && { echo "REFUSED (turbo watchdog)"; exit 4; } +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 + timeout -s TERM "$left" python3 "$here/lap_replay.py" --label "$label" --first-tokens "$first_tokens" --followups "$followups" \ + --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==""||$11rm) rm=$13; if ($6>pm) pm=$6; + if ($9+0>=50) {b++; sm+=$4; mc+=$5; pw+=$6; if (ln==""||$7/dev/null || echo stale +import json, sys, datetime +d = json.load(open(sys.argv[1], encoding="utf-8-sig")) +t = datetime.datetime.fromisoformat(str(d["checked_utc"]).replace("Z", "+00:00")) +if t.tzinfo is None: + t = t.replace(tzinfo=datetime.timezone.utc) +age = (datetime.datetime.now(datetime.timezone.utc) - t).total_seconds() +if age > 180: + print("stale") +elif d.get("ok") is False: + print("BAD", d.get("reasons")) +else: + print("ok") +PY