diff --git a/scripts/laptop/lap_lib.sh b/scripts/laptop/lap_lib.sh index 83d1241d..7ff9c8e7 100644 --- a/scripts/laptop/lap_lib.sh +++ b/scripts/laptop/lap_lib.sh @@ -3,8 +3,8 @@ # guard. The protocol is the owner's safety protocol of 2026-11-13 (docs/laptop-profile.md "Update 2026-11-13"); lap_stretch.sh carries an older # copy of the same logic and is to be reduced to a user of this file once no stretch is waiting on it. # -# lap_want_gpu LABEL create the flag C:\...\.git\laptop-gpu-wanted.flag: the refactor agents start no new build while it exists -# lap_wait_and_lock LABEL wait for: no cargo/rustc/link in WSL or Windows, Windows available RAM >= 10 GB, no lock; then take the lock +# lap_want_gpu LABEL create the flag C:\...\.git\laptop-gpu-wanted.flag (only at a moment the stretch can start at once; lap_done and the lock remove it) +# lap_wait_and_lock LABEL wait for: no cargo/rustc/link in WSL or Windows, Windows available RAM >= 8 GB (no builds), no lock; then take the lock # lap_preflight RESULT_DIR refuse (return 4) at a power limit < 100 W, SW cap / thermal slowdown at idle, P5, a bad turbo watchdog, WSL RAM < 10 GB # lap_done remove the lock and the flag (call from a trap) # lap_gpu_idle_check print the GPU state line @@ -26,12 +26,26 @@ lap_windows_avail_gb() { powershell.exe -NoProfile -Command 'Add-Type -AssemblyName Microsoft.VisualBasic; [math]::Floor((New-Object Microsoft.VisualBasic.Devices.ComputerInfo).AvailablePhysicalMemory/1GB)' | tr -dc '0-9' } +# The engine's own start check (footprint 25.2 GiB for this profile + soft floor and reserve 5 GiB in the dedicated preset) refuses below ~30.2 GiB of VM +# MemAvailable ("REFUSING TO START: memory budget refused (host_ram)"); waiting for it is cheaper than a refused `leleka up`. +LAP_MIN_WSL_GB=${LAP_MIN_WSL_GB:-31} +lap_wsl_avail_gb() { awk '$1=="MemAvailable:" {printf "%d", $2/1048576}' /proc/meminfo; } + lap_power_limit_w() { nvidia-smi -q -d POWER | awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); printf "%d", $2; exit}'; } -# Waits as long as it takes (the caller bounds it with `timeout` when it must report), then holds the lock. +# Rule of the coordinator (2026-11-14): poll every 2 minutes WITHOUT the flag (it would stall the refactor agents for nothing); at a moment when no +# cargo/rustc/link runs and Windows has >= LAP_MIN_WIN_GB (8) available, raise the flag, take the lock and drop the flag again at once (the lock keeps the +# others out for the length of the stretch). Waits as long as it takes; the caller bounds it with `timeout` when it must report. +LAP_MIN_WIN_GB=${LAP_MIN_WIN_GB:-8} lap_wait_and_lock() { - lap_want_gpu "$1" - until ! lap_builds_running && [ "$(lap_windows_avail_gb)" -ge 10 ] && (set -C; echo "$LAP_AGENT $(date -u +%FT%TZ) $1" > "$LAP_LOCK") 2>/dev/null; do sleep 20; done + while true; do + if [ ! -e "$LAP_LOCK" ] && ! lap_builds_running && [ "$(lap_windows_avail_gb)" -ge "$LAP_MIN_WIN_GB" ] && [ "$(lap_wsl_avail_gb)" -ge "$LAP_MIN_WSL_GB" ]; then + lap_want_gpu "$1" + if (set -C; echo "$LAP_AGENT $(date -u +%FT%TZ) $1" > "$LAP_LOCK") 2>/dev/null; then rm -f "$LAP_FLAG"; return 0; fi + rm -f "$LAP_FLAG" + fi + sleep 120 + done } lap_done() { rm -f "$LAP_LOCK" "$LAP_FLAG"; } @@ -45,5 +59,5 @@ lap_preflight() { [ "${lim:-0}" -ge 100 ] || return 4 ! grep -E "SW Power Cap|SW Thermal Slowdown" <<< "$q" | grep -q ": Active" || return 4 ! grep "Performance State" <<< "$q" | grep -q P5 || return 4 - [ "${turbo%% *}" != BAD ] && [ "$avail" -ge 10 ] && [ "${wfree:-0}" -ge 10 ] + [ "${turbo%% *}" != BAD ] && [ "$avail" -ge "$LAP_MIN_WSL_GB" ] && [ "${wfree:-0}" -ge "$LAP_MIN_WIN_GB" ] } diff --git a/scripts/laptop/lap_replay.py b/scripts/laptop/lap_replay.py index 4e0c35d4..8a5ea78f 100644 --- a/scripts/laptop/lap_replay.py +++ b/scripts/laptop/lap_replay.py @@ -59,8 +59,24 @@ def one_turn(args, base, key, model, items): return info, reply +def ladder(args, base, key, model, files): + """Cold prompts of growing size (--ladder 4096,8192,...), each on its own part of the corpus so that no prefix is shared; stops at the first failure.""" + position, results = 0, [] + for rung, tokens in enumerate(int(t) for t in args.ladder.split(",")): + chunk, position = ls.take(files, position, int(tokens * CHARS_PER_TOKEN)) + items = [{"role": "user", "content": [{"type": "input_text", "text": chunk + "\n\n" + ls.QUESTIONS[rung % len(ls.QUESTIONS)]}]}] + info, _ = one_turn(args, base, key, model, items) + info.update(label=args.label, rung=rung, target_tokens=tokens) + print(json.dumps(info), flush=True) + results.append(info) + if "error" in info or info.get("status") not in ("completed", "incomplete"): + break + return results + + def main(): parser = argparse.ArgumentParser() + parser.add_argument("--ladder", help="comma separated prompt sizes: cold prompts of growing size instead of one conversation") parser.add_argument("--label", required=True) parser.add_argument("--src", default=str(pathlib.Path(__file__).resolve().parents[2] / "crates")) parser.add_argument("--first-tokens", type=int, default=30000) @@ -77,6 +93,11 @@ def main(): if not files: print("lap_replay: no .rs file under " + args.src, file=sys.stderr) return 2 + if args.ladder: + results = ladder(args, base, key, model, files) + with open(args.out, "a", encoding="utf-8") as out: + out.writelines(json.dumps(r) + "\n" for r in results) + return 0 if all(r.get("status") in ("completed", "incomplete") for r in results) else 1 header = {"label": args.label, "corpus_sha256": corpus_digest(files, args.src), "files": len(files), "model": model} items, failed = [], False with open(args.out, "a", encoding="utf-8") as out: diff --git a/scripts/laptop/lap_stretch.sh b/scripts/laptop/lap_stretch.sh index 663f674d..7a57cf8d 100644 --- a/scripts/laptop/lap_stretch.sh +++ b/scripts/laptop/lap_stretch.sh @@ -13,43 +13,33 @@ 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 +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" - "$bin" down > "$res/down.log" 2>&1 || true + [ "$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"; rm -f "$lock"; echo "lock released $(date -u +%FT%TZ)" + 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"; } -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; } +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) @@ -71,8 +61,8 @@ 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)" + 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