interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / ops / spark-baseline

150 lines · 11171 bytes Raw Blame History
1#!/bin/sh
2# spark-baseline MODEL: the one baseline receipt per model per measurement window.
3#
4# spark-baseline 27b one fixed request to the resident 27B server on
5# 127.0.0.1:18091: fixed prompt, temperature 0, seed 1,
6# 64 tokens, sent once the GPU lock is quiet, timed through
7# spark-receipt. Never starts, stops or restarts a server.
8# Run inside an open window (spark-window run spark-baseline 27b). Writes
9# ~/yardsticks/windows/<id>/baseline-27b.json with the decode rate (streamed reply,
10# first to last token) and request tok/s, wall J/token over the decode
11# (receipt wall_J / tokens) and J/token above idle (window calibration idle W),
12# the reply's sha256 (same server build + same request = same reply) and the
13# server process, so a different build is visible.
14set -u
15WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows}
16model=${1:?usage: spark-baseline 27b}
17case $model in
18 27b) port=${SPARK_BASELINE_PORT:-18091}; mid=qwen3.8-27b-fp8 ;; # port override only for tests against a stand-in server
19 *) echo "spark-baseline: unknown model $model (known: 27b)" >&2; exit 2 ;;
20esac
21id=${SPARK_WINDOW:-$(spark-window id)} || { echo "spark-baseline: no open window; run spark-window open NAME first" >&2; exit 1; }
22[ "$(cat "$WR/OPEN" 2>/dev/null)" = "$id" ] && [ -f "$WR/$id/held" ] || { echo "spark-baseline: window $id is not the open, held window" >&2; exit 1; }
23held() { [ "$(cat "$WR/OPEN" 2>/dev/null)" = "$id" ] && [ -f "$WR/$id/held" ] && [ ! -f "$WR/$id/expired" ] && kill -0 "$(cat "$WR/$id/holder.pid" 2>/dev/null)" 2>/dev/null; }
24d=$WR/$id; b=$d/baseline-$model; mkdir -p "$b"
25export SPARK_WINDOW=$id SPARK_WINDOW_DIR=$d
26# The 27B server takes the GPU lock itself for each reply. A window that holds that lock (spark-window
27# open --gb10 exclusive|shared, L154) would make the server wait for the window to end: refuse up front.
28case $(cat "$d/gb10" 2>/dev/null) in exclusive|shared)
29 echo "spark-baseline: window $id holds the GPU lock (--gb10 $(cat "$d/gb10")), so the 27B server cannot answer inside it; open the window with --gb10 none" >&2; exit 1 ;; esac
30[ -f "$d/calibration.json" ] || echo "spark-baseline: warning: window $id has no calibration.json yet (run spark-calibrate first); J/token above idle will be null" >&2
31
32N=64; SEED=1
33PROMPT="Count upward from one in English words, separated by commas, and do not stop: one, two, three,"
34REQ=$(printf '{"model": "%s", "messages": [{"role": "user", "content": "%s"}], "temperature": 0, "seed": %s, "max_tokens": %s, "stream": true, "stream_options": {"include_usage": true}}' "$mid" "$PROMPT" "$SEED" "$N")
35echo "$REQ" > "$b/request.json"
36
37spid=$(ss -ltnpH "sport = :$port" 2>/dev/null | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -n 1)
38[ -n "$spid" ] || { echo "spark-baseline: nothing listens on 127.0.0.1:$port; not starting one" >&2; exit 1; }
39sexe=$(readlink "/proc/$spid/exe" 2>/dev/null); sargs=$(tr '\0' ' ' < "/proc/$spid/cmdline" 2>/dev/null | cut -c1-300)
40models=$(curl -s --max-time 10 "http://127.0.0.1:$port/v1/models")
41case $models in *"\"$mid\""*) ;; *) echo "spark-baseline: server on :$port does not serve $mid: $models" >&2; exit 1 ;; esac
42
43# The server takes the GPU lock itself for each generation (holding it here would deadlock),
44# and GPU-only jobs keep that lock busy. So the reply is streamed and every chunk is timestamped:
45# from the first to the last token the server holds the GPU lock for us, which makes the decode
46# rate and its energy clean whatever the queue did before. The lock table is sampled throughout;
47# any other process seen holding the GPU lock during the decode marks the baseline not ok.
48# Sample the lock spark-window uses. SPARK_GB10_LOCK is for tests only (see spark-window:63).
49G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock}; gino=$(stat -c %i "$G")
50held || { echo "spark-baseline: window $id is not held; nothing measured" >&2; exit 1; }
51( while :; do grep -E ":$gino " /proc/locks | sed "s/^/$(date +%s.%N) /"; sleep 0.5; done ) > "$b/gb10-during.txt" 2>/dev/null &
52smp=$!
53# bash for EPOCHREALTIME: a clock read without starting a process per line, so a burst stays a burst.
54# The server serves one reply at a time and turns a request away at once (503) while another runs
55# (other clients, e.g. aien-translate, use it around the clock; a window cannot stop them). So: watch
56# /health until busy is false, send at once, and on 503 go back to watching; give up after 20 min.
57# Every try is logged in attempts.txt. A reply line without a final newline (an error body) is kept.
58rm -f "$b/attempts.txt" "$b/headers.txt"
59spark-receipt "baseline-$model-$id" --idle 10 -- bash -c '
60 end=$(( ${EPOCHREALTIME%.*} + 1200 )); i=0; w=0
61 while [ ${EPOCHREALTIME%.*} -lt $end ]; do
62 case $(curl -s --max-time 2 "$6") in *"\"busy\":false"*) ;; *) w=$((w + 1)); sleep 0.2; continue ;; esac
63 i=$((i + 1)); echo "send $EPOCHREALTIME after $w busy polls" >> "$5"
64 curl -sN -D "$4" -H "Content-Type: application/json" --data-binary @"$1" "$2" |
65 while IFS= read -r l || [ -n "$l" ]; do printf "%s %s\n" "$EPOCHREALTIME" "$l"; done > "$3"
66 c=$(awk "NR == 1 { print \$2 }" "$4" 2>/dev/null)
67 echo "try $i http ${c:-none} $EPOCHREALTIME $(head -c 160 "$3" | tr "\n" " ")" >> "$5"
68 [ "$c" = 503 ] || break
69 done
70 exit 0' bash "$b/request.json" "http://127.0.0.1:$port/v1/chat/completions" "$b/stream.txt" "$b/headers.txt" "$b/attempts.txt" "http://127.0.0.1:$port/health" > "$b/receipt.out" 2>&1
71rc=$?
72tsend=$(awk '$1 == "send" { t = $2 } END { print t }' "$b/attempts.txt" 2>/dev/null)
73tries=$(grep -c '^try ' "$b/attempts.txt" 2>/dev/null); tries=${tries:-0}
74kill $smp 2>/dev/null; wait $smp 2>/dev/null
75held && wheld=true || wheld=false
76# chunks: "<epoch> data: {json}"; content chunks carry choices[0].delta.content
77grep -E '^[0-9.]+ data: \{' "$b/stream.txt" | while read -r t _ j; do
78 printf '%s\t%s\n' "$t" "$(printf '%s' "$j" | jq -c '[(.choices[0].delta.content // ""), (.usage.completion_tokens // null), (.choices[0].finish_reason // null)]')"
79done > "$b/chunks.tsv"
80cut -f2 "$b/chunks.tsv" | jq -rj '.[0]' > "$b/reply.txt"
81nchunk=$(cut -f2 "$b/chunks.tsv" | jq -r 'select(.[0] != "") | 1' | wc -l)
82t_first=$(awk -F'\t' '$2 !~ /^\[""/ {print $1; exit}' "$b/chunks.tsv")
83t_last=$(awk -F'\t' '$2 !~ /^\[""/ {t=$1} END {print t}' "$b/chunks.tsv")
84toks=$(cut -f2 "$b/chunks.tsv" | jq -r '.[1] // empty' | tail -n 1); toks=${toks:-$nchunk}
85fin=$(cut -f2 "$b/chunks.tsv" | jq -r '.[2] // empty' | tail -n 1); fin=${fin:-none}
86# the server logs its own prefill and decode timing per reply under the reply id: keep that line as a cross-check
87cid=$(grep -m 1 -E '^[0-9.]+ data: \{' "$b/stream.txt" | cut -d" " -f3- | jq -r '.id // empty' 2>/dev/null)
88slog=$(readlink "/proc/$spid/fd/2" 2>/dev/null)
89sline=""; [ -n "$cid" ] && [ -f "$slog" ] && sline=$(grep -F "$cid:" "$slog" | tail -n 1 | cut -c1-400)
90code=$(awk 'NR == 1 { print $2 }' "$b/headers.txt" 2>/dev/null); code=${code:-0}
91secs=$(awk -v a="$tsend" -v b="$t_last" 'BEGIN { if (b == "") print "null"; else printf "%.3f\n", b - a }')
92ttft=$(awk -v a="$tsend" -v b="$t_first" 'BEGIN { if (b == "") print "null"; else printf "%.3f\n", b - a }')
93dsecs=$(awk -v a="$t_first" -v b="$t_last" 'BEGIN { if (a == "" || b == "") print "null"; else printf "%.3f\n", b - a }')
94others=$(awk -v s="$spid" -v a="${t_first:-0}" -v e="${t_last:-0}" '$1 >= a && $1 <= e { if ($3 != "->" && $6 != s) print $6 }' "$b/gb10-during.txt" | sort -u | tr '\n' ' ')
95# wall power over the decode only, from the sensor rows (same CSV spark-receipt reads)
96dec=$(awk -F, -v a="${t_first:-0}" -v e="${t_last:-0}" '$1 ~ /^[0-9]+$/ && $1 >= int(a) && $1 <= int(e) + 1 && $3 ~ /^[0-9.]+$/ { w += $3; n++ } END { if (n) printf "%.6g %d\n", w / n, n; else print "null 0" }' "$HOME/yardsticks/sensors/$(date -d "@${t_first%.*}" +%F 2>/dev/null || date +%F).csv" 2>/dev/null)
97dW=${dec% *}; dn=${dec#* }
98r=$(sed -n 's/^receipt: //p' "$b/receipt.out"); cp "$r" "$b/receipt.json" 2>/dev/null
99rsha=$(sha256sum < "$b/reply.txt" | cut -c1-64)
100idleW=$(jq -r '.idle.wall_W_mean // "null"' "$d/calibration.json" 2>/dev/null); idleW=${idleW:-null}
101num() { awk -v x="$1" 'BEGIN { if (x == "" || x == "null") print "null"; else print x + 0 }'; }
102# values go in as awk variables, never pasted into the program: a pasted 0 divisor is a gawk error
103q() { awk -v s="$1" -v t="$2" -v w="$3" -v i="$4" -v op="$5" 'BEGIN {
104 if (s == "" || s == "null" || s + 0 <= 0 || t + 0 == 0 || (op != "rate" && (w == "" || w == "null")) || (op == "above" && (i == "" || i == "null"))) { print "null"; exit }
105 if (op == "rate") printf "%.4g\n", t / s; else if (op == "wall") printf "%.4g\n", w * s / t; else printf "%.4g\n", (w - i) * s / t }'; }
106# tokens that arrived after the first content chunk; when chunks carry more than one token, scale by tokens per chunk
107dtoks=$(awk -v t="$toks" -v c="$nchunk" 'BEGIN { if (c < 2) print 0; else if (t == c) print t - 1; else printf "%.6g\n", (c - 1) * t / c }')
108tps=$(q "$secs" "$toks" "" "" rate)
109dtps=$(q "$dsecs" "$dtoks" "" "" rate)
110djpt=$(q "$dsecs" "$dtoks" "$dW" "" wall)
111djpt_inc=$(q "$dsecs" "$dtoks" "$dW" "$idleW" above)
112# a decode of under 50 ms for many tokens means the server sent the reply in one burst, not as it decoded
113burst=$(awk -v s="$dsecs" -v t="$dtoks" 'BEGIN { print (t + 0 > 4 && (s == "null" || s + 0 < 0.05)) ? "true" : "false" }')
114ok=true; [ "$rc" = 0 ] && [ "$code" = 200 ] && [ "$toks" = "$N" ] && [ -z "$others" ] && [ "$wheld" = true ] && [ "$burst" = false ] || ok=false
115cat > "$b.json.tmp" <<EOJ
116{
117 "window_id": "$id",
118 "model": "$mid",
119 "ok": $ok,
120 "request": {"url": "http://127.0.0.1:$port/v1/chat/completions", "max_tokens": $N, "temperature": 0, "seed": $SEED, "stream": true, "file": "$b/request.json"},
121 "completion_tokens": $toks,
122 "content_chunks": $nchunk,
123 "decode_tokens_counted": $dtoks,
124 "stream_arrived_in_one_burst": $burst,
125 "finish_reason": "$fin",
126 "http_code": $code,
127 "tries": $tries,
128 "server_log_line": $(printf '%s' "$sline" | jq -Rs .),
129 "request_seconds": $(num "$secs"),
130 "first_token_seconds": $(num "$ttft"),
131 "tok_per_s_request": $tps,
132 "decode_seconds": $(num "$dsecs"),
133 "decode_tok_per_s": $dtps,
134 "decode_wall_W_mean": $(num "$dW"),
135 "decode_sensor_samples": $dn,
136 "decode_wall_J_per_token": $djpt,
137 "idle_W_calibration": $(num "$idleW"),
138 "decode_J_per_token_above_idle": $djpt_inc,
139 "reply_sha256": "$rsha",
140 "window_held_throughout": $wheld,
141 "gb10_other_holders_during_decode": "$others",
142 "server": {"pid": $spid, "exe": "$sexe", "exe_sha256": "$(sha256sum "$sexe" 2>/dev/null | cut -c1-64)", "args": "$(printf '%s' "$sargs" | sed 's/"/\\"/g')"},
143 "receipt": "$r",
144 "calibration": "$( [ -f "$d/calibration.json" ] && echo "$d/calibration.json")",
145 "note": "one streamed request; first_token_seconds includes the wait in the server's GPU-lock queue and the prefill; decode = first to last token, while the server holds the GPU lock; wall W is the whole UPS output in 10 W steps every ~2 s, so a few-second decode rests on a few samples: read J/token as coarse"
146}
147EOJ
148mv -f "$b.json.tmp" "$b.json"
149cat "$b.json"; echo "baseline: $b.json"
150[ "$ok" = true ]