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
aien-sovereign-core-mv / tools / qwen38_serve_speed.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Quick speed test through the live Qwen3.8-27B server (sc #642): decode on a short prompt (3 runs, |
| 18 | # 256 tokens, greedy), cold TTFT at about 1.7k and 30k prompt tokens, and next-turn TTFT on a |
| 19 | # follow-up that extends the 30k conversation. Client wall from curl; prefill / decode / chip |
| 20 | # figures from the server's own log line for each reply id. Token ids are requested for every |
| 21 | # reply; the three decodes are compared with the shipped reference ids (MATCH or MISMATCH). |
| 22 | # Usage: qwen38_serve_speed.sh [base-url] [corpus.txt] <out-dir> [server-log] |
| 23 | # base-url default http://127.0.0.1:18091 |
| 24 | # corpus.txt default: the shipped corpus crates/aien-inference-abi/fixtures/qwen38/speed-corpus.txt |
| 25 | # ("" also means default); any UTF-8 text of at least 100 kB works |
| 26 | # server-log default $AIEN_SERVE_LOG, else the log tools/serve-27b.sh writes: |
| 27 | # ${XDG_STATE_HOME:-$HOME/.local/state}/aien/serve-27b.log |
| 28 | # Writes <out-dir>/runs.tsv and <out-dir>/receipt.json (build, binary sha256, model checksum list, |
| 29 | # machine state, per-run figures, median chip ms per token, tokens per second, share of the |
| 30 | # 10.15 tokens per second ceiling in docs/qwen38/CEILING.md, MATCH or MISMATCH). See |
| 31 | # tools/qwen38_receipt.sh for how the server is found. Needs curl, jq, iconv, sha256sum and awk. |
| 32 | set -uo pipefail |
| 33 | REPO=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 34 | FIX="$REPO/crates/aien-inference-abi/fixtures/qwen38" |
| 35 | USAGE="usage: qwen38_serve_speed.sh [base-url] [corpus.txt] <out-dir> [server-log]" |
| 36 | URL=${1:-http://127.0.0.1:18091} |
| 37 | CORPUS=${2:-$FIX/speed-corpus.txt} |
| 38 | OUT=${3:?$USAGE} |
| 39 | LOG=${4:-${AIEN_SERVE_LOG:-${XDG_STATE_HOME:-$HOME/.local/state}/aien/serve-27b.log}} |
| 40 | REF="$FIX/decode-lighthouses-256.json" |
| 41 | for t in curl jq iconv sha256sum awk; do command -v "$t" >/dev/null || { echo "qwen38_serve_speed.sh: needs $t" >&2; exit 2; }; done |
| 42 | [ -n "${AIEN_SERVE_HTTP_BEARER_TOKEN:-}" ] || { echo "qwen38_serve_speed.sh: AIEN_SERVE_HTTP_BEARER_TOKEN required" >&2; exit 2; } |
| 43 | [ -f "$CORPUS" ] || { echo "qwen38_serve_speed.sh: corpus not found: $CORPUS" >&2; exit 2; } |
| 44 | [ -f "$REF" ] || { echo "qwen38_serve_speed.sh: reference ids not found: $REF" >&2; exit 2; } |
| 45 | [ -f "$LOG" ] || { echo "qwen38_serve_speed.sh: server log not found: $LOG (pass it as the 4th argument or set AIEN_SERVE_LOG)" >&2; exit 2; } |
| 46 | mkdir -p "$OUT" |
| 47 | # shellcheck source=tools/qwen38_receipt.sh |
| 48 | . "$REPO/tools/qwen38_receipt.sh" |
| 49 | q38_server_facts "$URL" |
| 50 | # shellcheck disable=SC2034 # read by q38_write_receipt |
| 51 | Q38_MACHINE_START=$(q38_machine_json) |
| 52 | : > "$OUT/runs.jsonl" |
| 53 | # run <name> <messages-json> <max_tokens> |
| 54 | run() { |
| 55 | local w="" |
| 56 | jq -n --argjson m "$2" --argjson n "$3" \ |
| 57 | '{messages:$m, temperature:0, max_tokens:$n, chat_template_kwargs:{enable_thinking:false}, aien_return_token_ids:true}' > "$OUT/$1.req.json" |
| 58 | for _ in $(seq 1 240); do |
| 59 | w=$(curl -s -m 1800 -o "$OUT/$1.json" -w '%{time_total}' "$URL/v1/chat/completions" \ |
| 60 | -H "Authorization: Bearer $AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'Content-Type: application/json' -d @"$OUT/$1.req.json") |
| 61 | # Retry only "busy" answers (server_busy, chip lock); any other error is the result. |
| 62 | jq -e '.error | tostring | test("busy"; "i")' "$OUT/$1.json" >/dev/null 2>&1 || break |
| 63 | sleep 5 |
| 64 | done |
| 65 | q38_run_json "$1" "$OUT/$1.json" "$w" "$LOG" >> "$OUT/runs.jsonl" |
| 66 | printf '%s\tclient_wall_s=%s\tprompt=%s\tcompletion=%s\t%s\n' "$1" "$w" \ |
| 67 | "$(jq '.usage.prompt_tokens' "$OUT/$1.json")" "$(jq '.usage.completion_tokens' "$OUT/$1.json")" \ |
| 68 | "$(q38_log_line "$LOG" "$(jq -r '.id' "$OUT/$1.json")")" | tee -a "$OUT/runs.tsv" |
| 69 | } |
| 70 | text() { head -c "$1" "$CORPUS" | iconv -c -f utf-8 -t utf-8; } |
| 71 | : > "$OUT/runs.tsv" |
| 72 | # Same request as the reference (evidence/qwen38-serve-fast-20261019/decode_runs.sh). |
| 73 | SHORT=$(jq -c '.request.messages' "$REF") |
| 74 | for i in 1 2 3; do run "decode$i" "$SHORT" 256; done |
| 75 | T17=$(text 7100); T30=$(text 100000) |
| 76 | run ttft_1p7k "$(jq -n --arg t "$T17" '[{role:"user",content:("Summarize this text in one sentence.\n\n" + $t)}]')" 1 |
| 77 | run ttft_30k "$(jq -n --arg t "$T30" '[{role:"user",content:("Summarize this text in one sentence.\n\n" + $t)}]')" 1 |
| 78 | A=$(jq -r '.choices[0].message.content' "$OUT/ttft_30k.json") |
| 79 | run next_turn_30k "$(jq -n --arg t "$T30" --arg a "$A" '[{role:"user",content:("Summarize this text in one sentence.\n\n" + $t)},{role:"assistant",content:$a},{role:"user",content:"Now name the three most important documents it mentions."}]')" 1 |
| 80 | |
| 81 | # Decode ids against the shipped reference, and the speed summary. |
| 82 | WANT=$(jq -c '.ids' "$REF") |
| 83 | MATCH=MATCH |
| 84 | for i in 1 2 3; do |
| 85 | [ "$(jq -c '.usage.aien_token_ids' "$OUT/decode$i.json")" = "$WANT" ] || MATCH=MISMATCH |
| 86 | done |
| 87 | MED=$(jq -s '[.[] | select(.name | startswith("decode")) | .chip_ms_per_token | select(. != null)] | sort |
| 88 | | if length == 0 then null elif length % 2 == 1 then .[length / 2 | floor] else (.[length / 2 - 1] + .[length / 2]) / 2 end' "$OUT/runs.jsonl") |
| 89 | EXTRA=$(jq -n --arg ref "crates/aien-inference-abi/fixtures/qwen38/decode-lighthouses-256.json" \ |
| 90 | --arg refsha "$(sha256sum "$REF" | cut -d' ' -f1)" --arg corpus "$CORPUS" \ |
| 91 | --arg csha "$(sha256sum "$CORPUS" | cut -d' ' -f1)" --arg m "$MATCH" --argjson med "$MED" \ |
| 92 | --argjson ceil "$Q38_CEILING_TOK_S" \ |
| 93 | '{reference:{file:$ref, sha256:$refsha}, corpus:{file:$corpus, sha256:$csha}, |
| 94 | decode_ids:$m, |
| 95 | decode_median_chip_ms_per_token:$med, |
| 96 | decode_tokens_per_s:(if $med then 1000 / $med else null end), |
| 97 | ceiling_tokens_per_s:$ceil, |
| 98 | share_of_ceiling:(if $med then (1000 / $med) / $ceil else null end)}') |
| 99 | q38_write_receipt "$OUT/receipt.json" speed "$URL" "$OUT/runs.jsonl" "$EXTRA" |
| 100 | echo "server binary sha256 $Q38_BIN_SHA256, build $Q38_BUILD, model dir $Q38_MODEL_DIR" |
| 101 | echo "decode ids against the shipped reference: $MATCH" |
| 102 | echo "median chip ms per token (3 decodes): $MED" |
| 103 | echo "receipt: $OUT/receipt.json" |