omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / window / d2-restore.sh
| 1 | #!/bin/sh |
| 2 | # window/d2-restore.sh --go -- R: bring the resident 35B back on 18091 under nvidia.ko and remove every window |
| 3 | # file. Status: NOT_RUN. Runs from the boot unit after the reboot that W7 ends with, directly from W7 |
| 4 | # (RESTORE_PATH=rebind), or from the W1 abort path. Safe to run again: each part checks before it acts. |
| 5 | # R0 hold-back file still present: remove it and reboot (the unit runs R again) |
| 6 | # R1 nvidia.ko back: unbind vfio-pci, clear driver_override, modprobe, bind, nvidia-smi lists the GB10. |
| 7 | # Whether nvidia.ko re-initialises GSP after the FLR/bus reset of the vfio-pci release is NOT_RUN on this |
| 8 | # chip, so any failure takes the reboot fallback once (state stays "restore", the unit runs R again). |
| 9 | # R2 nvidia-persistenced running; kernel.panic / panic_on_oops back to the values W1 saved |
| 10 | # R3 the 35B restarted exactly as it was started (W0 checked window/live-35b.snapshot against this recipe), |
| 11 | # /v1/models lists qwen, one greedy chat, exactly one aien-serve-http on the GPU |
| 12 | # R4 boot unit removed, privilege state clean, markers removed, BOARD line |
| 13 | set -eu |
| 14 | R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) |
| 15 | . "$R/window/common.sh" |
| 16 | LOG=$LOGDIR/d2-$(date -u +%Y%m%dT%H%M%SZ)-restore.log |
| 17 | [ "${1:-}" = --go ] || { echo "usage: $0 --go"; exit 2; } |
| 18 | was=$(cat "$STATE" 2>/dev/null || true) |
| 19 | is_identity=false |
| 20 | if [ "${2:-}" = W0 ] || [ "$was" = "restore W0" ] || [ "$was" = "run W0" ] || [ -e "$IDENTITY" ] || [ -e "$W0SUM" ]; then |
| 21 | is_identity=true |
| 22 | echo "restore W0" > "$STATE" |
| 23 | else |
| 24 | echo restore > "$STATE" |
| 25 | fi |
| 26 | # Any refusal leaves state "restore" and the unit in place (the next boot tries R again) and says so on BOARD. |
| 27 | trap 'rc=$?; [ "$rc" -eq 0 ] || board "restore STOPPED rc=$rc, 18091 may be down, log $LOG (page Main; next boot retries R)"' EXIT |
| 28 | |
| 29 | # R0 ------------------------------------------------------------------------- |
| 30 | # Identity windows have a two-reboot budget. A failed restore leaves evidence for attended recovery. |
| 31 | identity_state= |
| 32 | if "$is_identity"; then |
| 33 | identity_state=$(cat "$IDENTITY") || die "identity service record unavailable" |
| 34 | case $identity_state in up | down) ;; *) die "invalid identity service record" ;; esac |
| 35 | fi |
| 36 | if [ -e "$HOLD" ]; then |
| 37 | sudo -n rm -f "$HOLD" |
| 38 | [ -z "$identity_state" ] || die "removed remaining hold-back. Attended recovery required" |
| 39 | log "R0 hold-back file was still present: removed; rebooting; the boot unit runs R again" |
| 40 | board "restore: hold-back file was still present, removed, rebooting" |
| 41 | sync |
| 42 | sudo -n systemctl reboot |
| 43 | exit 0 |
| 44 | fi |
| 45 | |
| 46 | # R1 ------------------------------------------------------------------------- |
| 47 | FALLBACK=$LOGDIR/reboot-fallback-used |
| 48 | fallback() { |
| 49 | [ -z "$identity_state" ] || die "identity restore failed: $*. Attended recovery required" |
| 50 | if [ -e "$FALLBACK" ]; then |
| 51 | board "restore FAILED after the reboot fallback: $* (page Main and Drake; GPU state unknown; log $LOG)" |
| 52 | die "$* after the reboot fallback already ran" |
| 53 | fi |
| 54 | date -u > "$FALLBACK" |
| 55 | log "R1 reboot fallback: $*; the boot unit runs R again after the reboot" |
| 56 | board "restore: $*; reboot fallback, then R again from the boot unit" |
| 57 | sync |
| 58 | sudo -n systemctl reboot |
| 59 | exit 0 |
| 60 | } |
| 61 | if [ "$(drv)" = vfio-pci ]; then echo "$BDF" | sudo -n tee "$PCI/drivers/vfio-pci/unbind" >/dev/null || true; fi |
| 62 | echo | sudo -n tee "$DEV/driver_override" >/dev/null || true |
| 63 | if [ "$(nv_modules)" -ne 4 ]; then |
| 64 | log "R1 nvidia modules loaded: $(nv_modules) of 4; modprobe nvidia_uvm nvidia_drm" |
| 65 | timeout 180 sudo -n modprobe nvidia_uvm || fallback "modprobe nvidia_uvm failed" |
| 66 | timeout 180 sudo -n modprobe nvidia_drm || fallback "modprobe nvidia_drm failed" |
| 67 | [ "$(nv_modules)" -eq 4 ] || fallback "nvidia modules: $(nv_modules) of 4" |
| 68 | fi |
| 69 | if [ "$(drv)" != nvidia ]; then |
| 70 | echo "$BDF" | sudo -n tee "$PCI/drivers_probe" >/dev/null || true |
| 71 | fi |
| 72 | [ "$(drv)" = nvidia ] || fallback "$BDF driver is $(drv), not nvidia" |
| 73 | timeout 120 nvidia-smi -L | tee -a "$LOG" | grep -q GB10 || fallback "nvidia-smi does not list the GB10" |
| 74 | log "R1 PASS: nvidia.ko back, 4 modules, driver bound, nvidia-smi lists the GB10" |
| 75 | |
| 76 | # R2 ------------------------------------------------------------------------- |
| 77 | systemctl is-active --quiet nvidia-persistenced || sudo -n systemctl start nvidia-persistenced || log "R2 nvidia-persistenced did not start" |
| 78 | if [ -s "$SAVED" ]; then |
| 79 | sudo -n sysctl -q -p "$SAVED" || die "could not restore sysctl from $SAVED" |
| 80 | fi |
| 81 | log "R2 persistenced $(systemctl is-active nvidia-persistenced); kernel.panic=$(sysctl -n kernel.panic) panic_on_oops=$(sysctl -n kernel.panic_on_oops)" |
| 82 | |
| 83 | # R3 ------------------------------------------------------------------------- |
| 84 | if [ "$identity_state" = down ]; then |
| 85 | listeners=$(ss -Hltn 'sport = :18091') || die "could not inspect restored port" |
| 86 | [ -z "$listeners" ] && [ -z "$(live_pid)" ] || die "unexpected service on port 18091" |
| 87 | apps="port 18091 was and remains idle" |
| 88 | log "R3 $apps" |
| 89 | else |
| 90 | if [ -z "$(live_pid)" ]; then |
| 91 | K=$(cat "$Q36_KEY") |
| 92 | Q36_BATCH_BIN=$Q36_BIN AIEN_Q36_STROKE=1 AIEN_SERVE_GPU_LOCK=/tmp/aien-serve-18091.lock AIEN_COORD_KEY="$K" \ |
| 93 | "$Q36/start-35b-batch.sh" | tee -a "$LOG" |
| 94 | fi |
| 95 | i=0 |
| 96 | until curl -fsS --max-time 5 http://127.0.0.1:18091/v1/models 2>/dev/null | grep -qi qwen; do |
| 97 | i=$((i + 5)); [ "$i" -le 900 ] || { board "restore: 18091 did not come up in 900 s (page Main; $Q36/serve.log)"; die "18091 did not list qwen within 900 s"; } |
| 98 | sleep 5 |
| 99 | done |
| 100 | log "R3 18091 /v1/models lists qwen after ~${i} s" |
| 101 | out=$(curl -fsS --max-time 120 http://127.0.0.1:18091/v1/chat/completions -H 'Content-Type: application/json' \ |
| 102 | -d '{"messages":[{"role":"user","content":"Reply with the single word: ready"}],"max_tokens":8,"temperature":0}') || |
| 103 | die "greedy chat failed" |
| 104 | log "R3 greedy chat: $out" |
| 105 | p=$(live_pid) |
| 106 | snap35b "$p" > "$SNAP.restored" |
| 107 | if ! snap_expected | cmp -s - "$SNAP.restored"; then |
| 108 | [ "$identity_state" != up ] || die "running service differs from its pre-window snapshot" |
| 109 | log "R3 WARNING: restarted 35B launch record differs from the recipe (see $SNAP.restored)" |
| 110 | fi |
| 111 | apps=$(nvidia-smi --query-compute-apps=process_name,used_memory --format=csv,noheader) |
| 112 | log "R3 compute apps: $apps" |
| 113 | [ "$(printf '%s\n' "$apps" | grep -c aien-serve-http)" -eq 1 ] || die "expected exactly one aien-serve-http on the GPU" |
| 114 | |
| 115 | fi |
| 116 | |
| 117 | # R4 ------------------------------------------------------------------------- |
| 118 | sudo -n systemctl disable aien-owndriver-window.service >/dev/null 2>&1 || true |
| 119 | sudo -n rm -f "$UNIT" |
| 120 | sudo -n systemctl daemon-reload |
| 121 | privcheck || { board "PRIVILEGE PERSISTED after the window, see $LOG (page Main)"; die "privilege state not clean"; } |
| 122 | rm -f "$LIVE_MARKER" "$APPROVAL" "$FALLBACK" "$SAVED" "$IDENTITY" "$W0SUM" "$RPATH" "$LOGDIR/d3-cmd" "$STATE" |
| 123 | board "WINDOW CLOSED (own driver), 18091 restored ($(printf '%s' "$apps" | tr '\n' ' ')). Log $LOG" |
| 124 | log "R PASS" |