Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/blob/main/tools/ops/spark-baseline # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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//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. 14 set -u 15 WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows} 16 model=${1:?usage: spark-baseline 27b} 17 case $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 ;; 20 esac 21 id=${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; } 23 held() { [ "$(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; } 24 d=$WR/$id; b=$d/baseline-$model; mkdir -p "$b" 25 export 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. 28 case $(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 32 N=64; SEED=1 33 PROMPT="Count upward from one in English words, separated by commas, and do not stop: one, two, three," 34 REQ=$(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") 35 echo "$REQ" > "$b/request.json" 36 37 spid=$(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; } 39 sexe=$(readlink "/proc/$spid/exe" 2>/dev/null); sargs=$(tr '\0' ' ' < "/proc/$spid/cmdline" 2>/dev/null | cut -c1-300) 40 models=$(curl -s --max-time 10 "http://127.0.0.1:$port/v1/models") 41 case $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). 49 G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock}; gino=$(stat -c %i "$G") 50 held || { 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 & 52 smp=$! 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. 58 rm -f "$b/attempts.txt" "$b/headers.txt" 59 spark-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 71 rc=$? 72 tsend=$(awk '$1 == "send" { t = $2 } END { print t }' "$b/attempts.txt" 2>/dev/null) 73 tries=$(grep -c '^try ' "$b/attempts.txt" 2>/dev/null); tries=${tries:-0} 74 kill $smp 2>/dev/null; wait $smp 2>/dev/null 75 held && wheld=true || wheld=false 76 # chunks: " data: {json}"; content chunks carry choices[0].delta.content 77 grep -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)]')" 79 done > "$b/chunks.tsv" 80 cut -f2 "$b/chunks.tsv" | jq -rj '.[0]' > "$b/reply.txt" 81 nchunk=$(cut -f2 "$b/chunks.tsv" | jq -r 'select(.[0] != "") | 1' | wc -l) 82 t_first=$(awk -F'\t' '$2 !~ /^\[""/ {print $1; exit}' "$b/chunks.tsv") 83 t_last=$(awk -F'\t' '$2 !~ /^\[""/ {t=$1} END {print t}' "$b/chunks.tsv") 84 toks=$(cut -f2 "$b/chunks.tsv" | jq -r '.[1] // empty' | tail -n 1); toks=${toks:-$nchunk} 85 fin=$(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 87 cid=$(grep -m 1 -E '^[0-9.]+ data: \{' "$b/stream.txt" | cut -d" " -f3- | jq -r '.id // empty' 2>/dev/null) 88 slog=$(readlink "/proc/$spid/fd/2" 2>/dev/null) 89 sline=""; [ -n "$cid" ] && [ -f "$slog" ] && sline=$(grep -F "$cid:" "$slog" | tail -n 1 | cut -c1-400) 90 code=$(awk 'NR == 1 { print $2 }' "$b/headers.txt" 2>/dev/null); code=${code:-0} 91 secs=$(awk -v a="$tsend" -v b="$t_last" 'BEGIN { if (b == "") print "null"; else printf "%.3f\n", b - a }') 92 ttft=$(awk -v a="$tsend" -v b="$t_first" 'BEGIN { if (b == "") print "null"; else printf "%.3f\n", b - a }') 93 dsecs=$(awk -v a="$t_first" -v b="$t_last" 'BEGIN { if (a == "" || b == "") print "null"; else printf "%.3f\n", b - a }') 94 others=$(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) 96 dec=$(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) 97 dW=${dec% *}; dn=${dec#* } 98 r=$(sed -n 's/^receipt: //p' "$b/receipt.out"); cp "$r" "$b/receipt.json" 2>/dev/null 99 rsha=$(sha256sum < "$b/reply.txt" | cut -c1-64) 100 idleW=$(jq -r '.idle.wall_W_mean // "null"' "$d/calibration.json" 2>/dev/null); idleW=${idleW:-null} 101 num() { 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 103 q() { 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 107 dtoks=$(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 }') 108 tps=$(q "$secs" "$toks" "" "" rate) 109 dtps=$(q "$dsecs" "$dtoks" "" "" rate) 110 djpt=$(q "$dsecs" "$dtoks" "$dW" "" wall) 111 djpt_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 113 burst=$(awk -v s="$dsecs" -v t="$dtoks" 'BEGIN { print (t + 0 > 4 && (s == "null" || s + 0 < 0.05)) ? "true" : "false" }') 114 ok=true; [ "$rc" = 0 ] && [ "$code" = 200 ] && [ "$toks" = "$N" ] && [ -z "$others" ] && [ "$wheld" = true ] && [ "$burst" = false ] || ok=false 115 cat > "$b.json.tmp" </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 } 147 EOJ 148 mv -f "$b.json.tmp" "$b.json" 149 cat "$b.json"; echo "baseline: $b.json" 150 [ "$ok" = true ]