interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / scripts / bench / own30b-quiet-run.sh

205 lines · 10175 bytes Raw Blame History
1#!/usr/bin/env bash
2# Run one OWN-30B HTTP or benchmark session inside a bounded Spark quiet window
3# (#625), and record whether the GPU was really uncontended.
4#
5# What it does, in order:
6# 1. Preflight: GPU compute processes, GPU utilization, AIEN listeners and the
7# identity of what is being measured (repo HEAD, server binary SHA-256,
8# model path and size, Omega pin, OWN30B_DECODE_GRAPH).
9# 2. Refuses (exit 3, command not run) when a GPU compute process other than
10# the declared server (--server-pid) is present, or when nvidia-smi cannot
11# say, unless --allow-contended REASON is given. An override runs the
12# command, records the reason and marks the run not comparable.
13# 3. Runs CMD under `quietlock hold` (bounded, owner and reason recorded;
14# quietlock releases the flag when CMD and its process group exit).
15# 4. Postflight: the same GPU query again. A foreign process seen then also
16# marks the run not comparable.
17# 5. Writes OUT_DIR/manifest.json (schema own30b-quiet-run/v1).
18#
19# It never takes /tmp/aien-gb10.lock: aien-serve-http takes that lock itself
20# for each generation, and holding it from outside would deadlock the server.
21# The quiet flag and the GPU lock serve different purposes.
22#
23# File locks only coordinate cooperating workloads. A GPU process that ignores
24# the quiet flag is detected (preflight and postflight), never excluded, and
25# this script never kills, pauses or renices anything. A process that starts
26# and stops between the two samples is not seen.
27#
28# Usage:
29# scripts/bench/own30b-quiet-run.sh --owner ID --out DIR [options] -- CMD...
30# Options:
31# --minutes N quiet hold bound, 1 to 20 [10] (quietlock needs an
32# approval token above 20; this script does not go there)
33# --reason TEXT recorded in the quiet flag and the manifest
34# --server-pid PID the server under test; allowed on the GPU (repeatable)
35# --server-bin PATH server binary, hashed into the manifest
36# --model PATH model file or directory, path and size recorded
37# --omega-pin TEXT Omega pin of the native backend, recorded as given
38# --allow-contended TEXT run even when the GPU is not idle; TEXT is recorded
39# Environment: OWN30B_QR_QUIETLOCK [quietlock], OWN30B_QR_NVIDIA_SMI
40# [nvidia-smi], OWN30B_QR_SS [ss] (the self-test points these at fakes).
41#
42# Exit: CMD's exit status; 2 usage; 3 refused as contended; 75 quiet flag
43# already held (command not run); 128+N when interrupted by signal N (the held
44# command is stopped with SIGTERM through quietlock and the flag released).
45set -uo pipefail
46ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
47die() { echo "own30b-quiet-run: $*" >&2; exit 2; }
48QL=${OWN30B_QR_QUIETLOCK:-quietlock}
49SMI=${OWN30B_QR_NVIDIA_SMI:-nvidia-smi}
50SS=${OWN30B_QR_SS:-ss}
51
52OWNER="" OUT="" MINUTES=10 REASON="OWN-30B uncontended measurement (#625)"
53SERVER_BIN="" MODEL="" OMEGA_PIN="" OVERRIDE="" SERVER_PIDS=()
54while [ $# -gt 0 ]; do
55 case "$1" in
56 --owner) OWNER=${2:-}; shift 2 ;;
57 --out) OUT=${2:-}; shift 2 ;;
58 --minutes) MINUTES=${2:-}; shift 2 ;;
59 --reason) REASON=${2:-}; shift 2 ;;
60 --server-pid) SERVER_PIDS+=("${2:-}"); shift 2 ;;
61 --server-bin) SERVER_BIN=${2:-}; shift 2 ;;
62 --model) MODEL=${2:-}; shift 2 ;;
63 --omega-pin) OMEGA_PIN=${2:-}; shift 2 ;;
64 --allow-contended) OVERRIDE=${2:-}; [ -n "$OVERRIDE" ] || die "--allow-contended needs a reason"; shift 2 ;;
65 --) shift; break ;;
66 *) die "unknown argument $1 (usage: --owner ID --out DIR [options] -- CMD...)" ;;
67 esac
68done
69[ $# -gt 0 ] || die "no command after --"
70[ -n "$OWNER" ] || die "--owner is required"
71[ -n "$OUT" ] || die "--out is required"
72case "$MINUTES" in '' | *[!0-9]*) die "--minutes must be 1 to 20" ;; esac
73[ "$MINUTES" -ge 1 ] && [ "$MINUTES" -le 20 ] || die "--minutes must be 1 to 20"
74for p in ${SERVER_PIDS[@]+"${SERVER_PIDS[@]}"}; do
75 case "$p" in '' | *[!0-9]*) die "--server-pid must be a number" ;; esac
76done
77command -v jq >/dev/null 2>&1 || die "jq not found"
78command -v "$QL" >/dev/null 2>&1 || die "quietlock not found ($QL)"
79mkdir -p "$OUT" || die "cannot create $OUT"
80[ ! -e "$OUT/manifest.json" ] || die "$OUT/manifest.json exists: use a new --out"
81
82now() { date -u +%Y-%m-%dT%H:%M:%SZ; }
83allowed_json=$(printf '%s\n' ${SERVER_PIDS[@]+"${SERVER_PIDS[@]}"} | jq -R -s -c 'split("\n") | map(select(length > 0) | tonumber)')
84CMD_JSON=$(jq -n -c '$ARGS.positional' --args -- "$@")
85
86# gpu_sample: one JSON object with the raw GPU line, the compute processes and
87# the foreign ones (not a declared server). available=false when nvidia-smi
88# is missing or fails; that never counts as idle.
89gpu_sample() {
90 local gpu apps
91 if ! gpu=$("$SMI" --query-gpu=name,utilization.gpu,memory.used,memory.total --format=csv,noheader 2>/dev/null) ||
92 ! apps=$("$SMI" --query-compute-apps=pid,process_name,used_memory --format=csv,noheader 2>/dev/null); then
93 jq -n -c --arg t "$(now)" '{at: $t, available: false, gpu: null, compute_apps: [], foreign: []}'
94 return
95 fi
96 jq -n -c --arg t "$(now)" --arg gpu "$gpu" --arg apps "$apps" --argjson allowed "$allowed_json" '
97 ($apps | split("\n") | map(select(test("\\S")) | split(", ")
98 | {pid: (.[0] | tonumber? // .[0]), name: .[1], used_memory: .[2]})) as $a
99 | {at: $t, available: true, gpu: $gpu, compute_apps: $a,
100 foreign: [$a[] | select(.pid as $p | $allowed | index($p) | not)]}'
101}
102
103listeners() {
104 local out
105 if out=$("$SS" -ltnpH 2>/dev/null); then
106 printf '%s\n' "$out" | grep -i aien | jq -R -s -c 'split("\n") | map(select(length > 0) | gsub("\\s+"; " "))'
107 else
108 echo null
109 fi
110}
111
112sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi; }
113
114identity() {
115 local head dirty bin_sha="" model_kib=""
116 head=$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || echo unknown)
117 if [ -n "$(git -C "$ROOT" status --porcelain 2>/dev/null)" ]; then dirty=true; else dirty=false; fi
118 if [ -n "$SERVER_BIN" ]; then [ -f "$SERVER_BIN" ] && bin_sha=$(sha256 "$SERVER_BIN"); fi
119 if [ -n "$MODEL" ]; then [ -e "$MODEL" ] && model_kib=$(du -skL "$MODEL" 2>/dev/null | cut -f1); fi
120 jq -n -c --arg head "$head" --argjson dirty "$dirty" --arg bin "$SERVER_BIN" --arg sha "$bin_sha" \
121 --arg model "$MODEL" --arg kib "$model_kib" --arg pin "$OMEGA_PIN" --arg graph "${OWN30B_DECODE_GRAPH-}" '
122 {repo_head: $head, repo_dirty: $dirty,
123 server_bin: (if $bin == "" then null else {path: $bin, sha256: (if $sha == "" then null else $sha end)} end),
124 model: (if $model == "" then null else {path: $model, kib: ($kib | tonumber? // null)} end),
125 omega_pin: (if $pin == "" then null else $pin end),
126 own30b_decode_graph: (if $graph == "" then null else $graph end)}'
127}
128
129STARTED=$(now)
130PRE=$(gpu_sample)
131LISTEN=$(listeners)
132IDENT=$(identity)
133REASONS='[]'
134add_reason() { REASONS=$(jq -c --arg r "$1" '. + [$r]' <<<"$REASONS"); }
135if [ "$(jq -r .available <<<"$PRE")" != true ]; then
136 add_reason "preflight: nvidia-smi unavailable, GPU idleness not established"
137fi
138if [ "$(jq '.foreign | length' <<<"$PRE")" -gt 0 ]; then
139 add_reason "preflight: foreign GPU compute process present: $(jq -r '[.foreign[] | "\(.pid) \(.name)"] | join(", ")' <<<"$PRE")"
140fi
141
142POST=null RC=0 OUTCOME=""
143write_manifest() {
144 jq -n --arg owner "$OWNER" --arg reason "$REASON" --argjson minutes "$MINUTES" \
145 --arg started "$STARTED" --arg ended "$(now)" --argjson rc "$RC" --arg outcome "$OUTCOME" \
146 --argjson reasons "$REASONS" --arg override "$OVERRIDE" --argjson pre "$PRE" --argjson post "$POST" \
147 --argjson listeners "$LISTEN" --argjson identity "$IDENT" --argjson allowed "$allowed_json" --argjson cmd "$CMD_JSON" '
148 {schema: "own30b-quiet-run/v1", owner: $owner, reason: $reason, hold_minutes: $minutes,
149 command: $cmd, started: $started, ended: $ended, exit_status: $rc, outcome: $outcome,
150 comparable: ($reasons | length == 0), not_comparable_reasons: $reasons,
151 override: (if $override == "" then null else {reason: $override} end),
152 server_pids: $allowed, identity: $identity, preflight: ($pre + {aien_listeners: $listeners}),
153 postflight: $post,
154 note: "File locks coordinate cooperating workloads only; a GPU process that ignores the quiet flag is detected by the preflight and postflight samples, never excluded."}' \
155 >"$OUT/manifest.json"
156}
157
158if [ "$(jq length <<<"$REASONS")" -gt 0 ] && [ -z "$OVERRIDE" ]; then
159 RC=3 OUTCOME="refused: GPU not idle"
160 write_manifest
161 echo "own30b-quiet-run: refused, GPU not idle (see $OUT/manifest.json); --allow-contended REASON runs it as not comparable" >&2
162 exit 3
163fi
164[ -z "$OVERRIDE" ] || add_reason "override: $OVERRIDE"
165
166if ! "$QL" check >/dev/null 2>&1; then
167 RC=75 OUTCOME="refused: quiet flag held by someone else"
168 write_manifest
169 echo "own30b-quiet-run: quiet flag already held; nothing run" >&2
170 exit 75
171fi
172
173# quietlock runs in the background so a signal to this script reaches it at
174# once. A non-interactive shell starts background jobs with SIGINT ignored, so
175# every stop request is passed on as SIGTERM, which quietlock forwards to the
176# held command's process group before it releases the flag.
177SIG=""
178"$QL" hold --owner "$OWNER" --minutes "$MINUTES" --reason "$REASON" -- "$@" &
179QPID=$!
180on_signal() { SIG=$1; kill -TERM "$QPID" 2>/dev/null; }
181trap 'on_signal INT' INT
182trap 'on_signal TERM' TERM
183trap 'on_signal HUP' HUP
184while :; do
185 wait "$QPID"; RC=$?
186 kill -0 "$QPID" 2>/dev/null || break
187done
188trap - INT TERM HUP
189
190POST=$(gpu_sample)
191if [ "$(jq '.foreign | length' <<<"$POST")" -gt 0 ]; then
192 add_reason "postflight: foreign GPU compute process present: $(jq -r '[.foreign[] | "\(.pid) \(.name)"] | join(", ")' <<<"$POST")"
193fi
194if [ -n "$SIG" ]; then
195 OUTCOME="interrupted: SIG$SIG"
196 add_reason "interrupted by SIG$SIG"
197 case "$SIG" in INT) RC=130 ;; TERM) RC=143 ;; HUP) RC=129 ;; esac
198elif [ "$RC" -eq 0 ]; then
199 OUTCOME="completed"
200else
201 OUTCOME="exit $RC"
202fi
203write_manifest
204echo "own30b-quiet-run: $OUTCOME; comparable=$(jq -r .comparable "$OUT/manifest.json"); manifest $OUT/manifest.json" >&2
205exit "$RC"