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 / scripts / gb10-serve-chat.sh
| 1 | #!/usr/bin/env 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 | # SC-GB10-SERVE-CHAT (tests/gates/sc-gb10-serve-chat.gate): the GPU receipt an Alpha needs. |
| 18 | # Builds aien-serve-http against the real Omega GPU engine (the build install.sh --with-server |
| 19 | # does), starts it on the GB10 with the Qwen3.8-27B checkpoint through tools/serve-27b.sh, waits |
| 20 | # until the API advertises qwen3.8-27b-fp8, sends one short greedy chat request, checks the reply |
| 21 | # is a well-formed, non-empty chat.completion, then stops the server with SIGTERM and waits for |
| 22 | # its "shutdown complete" line. |
| 23 | # |
| 24 | # Run by tests/gates/run.sh gb10-serve-chat, which first materialises the pinned omega, physics |
| 25 | # and aienos sources and writes $PINS/env.sh (scripts/pins.sh --gpu). Arguments: <env.sh>. |
| 26 | # |
| 27 | # Chip lock: under aien-test (a gb10 gate) the runner already holds /tmp/aien-gb10.lock for |
| 28 | # this whole script and says so with AIEN_GB10_LOCK_HELD=1. The server would queue behind that |
| 29 | # lock until its load gave up, so with the marker it takes no lock, but only after verifying in |
| 30 | # /proc/locks that the holder is one of its ancestors; a marker nobody backs is refused (exit 1, |
| 31 | # so this gate FAILS rather than passes). Without the marker the server takes |
| 32 | # /tmp/aien-gb10.lock itself, as the live server does. |
| 33 | # |
| 34 | # NOT_RUN (exit 77 with a not_run_reason observation, aien-test verdict.rs SELF_NOT_RUN_EXIT), |
| 35 | # never PASS: model_missing (no checkpoint at $MODEL_DIR), memory_short (MemAvailable below the |
| 36 | # 49,497 MiB the loader needs, checked before the server is started; the server is not started). |
| 37 | # Every other problem is a FAIL. Observations: model_dir, model_dir_source (both emitted before any |
| 38 | # check, so a NOT_RUN names them too), build_rc, engine_native, mem_available_mib, chip_lock, |
| 39 | # serve_rc, reply_ok, reply_chars, completion_tokens, shutdown_clean. |
| 40 | # |
| 41 | # Settings: MODEL_DIR (an override, receipted as model_dir_source "override"; default |
| 42 | # $AIEN_MODELS_DIR/qwen3.8-27b-fp8-017b9c7a, "models-mount", else $HOME/models/..., "home"; |
| 43 | # tests/gates/run.sh sets AIEN_MODELS_DIR to the sandbox home's read-only models/ mount because it |
| 44 | # gives each step a fresh writable HOME; tests/gates/step-home.sh gb10_model_dir, host-tested by |
| 45 | # scripts/test-gb10-model-dir.sh), READY_TIMEOUT (default 2400 s, as tools/serve-27b.sh), |
| 46 | # STOP_TIMEOUT (default 900 s). |
| 47 | set -uo pipefail |
| 48 | ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 49 | cd "$ROOT" || exit 3 |
| 50 | ENV_SH=${1:?usage: scripts/gb10-serve-chat.sh <pins env.sh>} |
| 51 | TARGET=${CARGO_TARGET_DIR:-$ROOT/target} |
| 52 | # shellcheck source=tests/gates/step-home.sh |
| 53 | . "$ROOT/tests/gates/step-home.sh" || exit 3 |
| 54 | gb10_model_dir |
| 55 | MODEL_ID=qwen3.8-27b-fp8 |
| 56 | READY_TIMEOUT=${READY_TIMEOUT:-2400} |
| 57 | STOP_TIMEOUT=${STOP_TIMEOUT:-900} |
| 58 | # The loader's own figure (tools/serve-27b.sh NEED_MIB, qwen38_engine.rs admit_load): 41,305 MiB |
| 59 | # for the model at the default context plus 8,192 MiB kept free. |
| 60 | NEED_MIB=49497 |
| 61 | |
| 62 | # The leading newline keeps the line whole when a tool left a partial line. |
| 63 | obs() { printf '\nAIEN-OBS %s %s\n' "$1" "$2"; } |
| 64 | not_run() { echo "gb10-serve-chat: NOT_RUN $1: $2" >&2; obs not_run_reason "\"$1\""; exit 77; } |
| 65 | fail() { echo "gb10-serve-chat: FAIL: $*" >&2; exit 1; } |
| 66 | json_str() { local s=${1//\\/\\\\}; printf '"%s"' "${s//\"/\\\"}"; } |
| 67 | # Model provenance goes into the receipt before any check: an override is never mistaken for the default. |
| 68 | obs model_dir "$(json_str "$MODEL_DIR")" |
| 69 | obs model_dir_source "\"$MODEL_DIR_SOURCE\"" |
| 70 | for t in curl jq ss od tr; do command -v "$t" >/dev/null || fail "needs $t"; done |
| 71 | # This gate starts its own ephemeral server. Keep its credential in memory and |
| 72 | # inherit it only into that child; do not require operator secrets in gate env. |
| 73 | if [ -z "${AIEN_SERVE_HTTP_BEARER_TOKEN:-}" ]; then |
| 74 | AIEN_SERVE_HTTP_BEARER_TOKEN=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n') \ |
| 75 | || fail "cannot generate test-server credential" |
| 76 | [ -n "$AIEN_SERVE_HTTP_BEARER_TOKEN" ] || fail "empty test-server credential" |
| 77 | fi |
| 78 | export AIEN_SERVE_HTTP_BEARER_TOKEN |
| 79 | |
| 80 | [ -f "$MODEL_DIR/config.json" ] && [ -f "$MODEL_DIR/tokenizer.json" ] \ |
| 81 | || not_run model_missing "no config.json or tokenizer.json under $MODEL_DIR" |
| 82 | |
| 83 | # 1. Memory pre-check, before the release build: a chip that cannot hold the |
| 84 | # model is a NOT_RUN, and it should not cost a 5-minute GPU build to learn. |
| 85 | # MemAvailable counts mapped model pages as reclaimable, so beside a resident model server it |
| 86 | # overstates what a second load can get (2026-10-25T02:47Z: a load admitted at MemAvailable |
| 87 | # 53.5 GiB drove a global OOM that killed the live 35B). Count MemAvailable - Mapped instead. |
| 88 | avail_kib=$(awk '/^MemAvailable:/ {a=$2} /^Mapped:/ {m=$2} END {print a-m}' /proc/meminfo 2>/dev/null) |
| 89 | avail_mib=$(( ${avail_kib:-0} / 1024 )) |
| 90 | obs mem_available_mib "$avail_mib" |
| 91 | [ "$avail_mib" -ge "$NEED_MIB" ] \ |
| 92 | || not_run memory_short "MemAvailable-Mapped $avail_mib MiB, the 27B loader needs $NEED_MIB MiB (another model is resident: the GB10 needs an exclusive window); the server was not started" |
| 93 | |
| 94 | # 2. Build, as install.sh --with-server does (pinned sources, release, real GPU engine). |
| 95 | [ -f "$ENV_SH" ] || fail "pins env file $ENV_SH is missing" |
| 96 | # shellcheck disable=SC1090 |
| 97 | . "$ENV_SH" |
| 98 | [ "${AIEN_REQUIRE_NATIVE_LIBS:-}" = 1 ] && [ -n "${AIEN_OMEGA_DIR:-}" ] \ |
| 99 | || fail "$ENV_SH does not select the GPU engine (AIEN_OMEGA_DIR unset)" |
| 100 | cargo build --locked --release -p aien-serve-http |
| 101 | rc=$? |
| 102 | obs build_rc "$rc" |
| 103 | [ "$rc" -eq 0 ] || fail "cargo build of aien-serve-http failed (rc $rc)" |
| 104 | BIN=$TARGET/release/aien-serve-http |
| 105 | info=$("$BIN" --version 2>&1) || fail "$BIN --version failed: $info" |
| 106 | echo "$info" |
| 107 | if grep -q '^omega gpu engine: native' <<<"$info" && grep -q '^qwen38 engine: native' <<<"$info"; then |
| 108 | obs engine_native true |
| 109 | else |
| 110 | obs engine_native false |
| 111 | fail "the built server carries the CPU stub, not the Omega GPU engine" |
| 112 | fi |
| 113 | |
| 114 | STATE=$(mktemp -d) || fail "mktemp failed" |
| 115 | pid="" |
| 116 | stop_server() { |
| 117 | [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null && kill -TERM "$pid" 2>/dev/null |
| 118 | rm -rf "$STATE" |
| 119 | } |
| 120 | trap stop_server EXIT |
| 121 | |
| 122 | # 3. Chip lock (see the header). The marker is passed through untouched; the server itself |
| 123 | # verifies in /proc/locks that an ancestor holds /tmp/aien-gb10.lock and refuses to start |
| 124 | # otherwise (aien-serve-http chip_reservation.rs). There is no private lock and no override. |
| 125 | if [ "${AIEN_GB10_LOCK_HELD:-}" = 1 ]; then |
| 126 | obs chip_lock '"held by the caller (AIEN_GB10_LOCK_HELD=1); the server verifies the holder is an ancestor"' |
| 127 | else |
| 128 | obs chip_lock '"server takes /tmp/aien-gb10.lock"' |
| 129 | fi |
| 130 | |
| 131 | # 4. Start through the project's own start script on a free loopback port (never the live 18091). |
| 132 | port="" |
| 133 | for p in $(seq 18190 18199); do |
| 134 | ss -ltn "sport = :$p" 2>/dev/null | grep -q LISTEN || { port=$p; break; } |
| 135 | done |
| 136 | [ -n "$port" ] || fail "no free port in 18190-18199" |
| 137 | LOG=$STATE/serve-27b.log |
| 138 | BIN=$BIN MODEL_DIR=$MODEL_DIR PORT=$port STATE_DIR=$STATE LOG=$LOG READY_TIMEOUT=$READY_TIMEOUT \ |
| 139 | bash tools/serve-27b.sh |
| 140 | rc=$? |
| 141 | obs serve_rc "$rc" |
| 142 | [ -f "$STATE/serve-27b.pid" ] && pid=$(cat "$STATE/serve-27b.pid") |
| 143 | if [ "$rc" -ne 0 ]; then |
| 144 | sed 's/^/ log: /' "$LOG" >&2 |
| 145 | if grep -q 'after taking the GPU lock (another model may be loaded); refusing' "$LOG"; then |
| 146 | not_run memory_short "the loader refused after taking the GPU lock (memory fell below its need meanwhile)" |
| 147 | fi |
| 148 | fail "tools/serve-27b.sh exited $rc: the server did not come up serving $MODEL_ID" |
| 149 | fi |
| 150 | [ -n "$pid" ] || fail "tools/serve-27b.sh reported ready but wrote no pid file" |
| 151 | |
| 152 | # 5. One short, deterministic chat request: greedy, thinking off, a few tokens. |
| 153 | req=$(jq -nc --arg m "$MODEL_ID" '{model:$m, messages:[{role:"user",content:"Reply with the single word: ready"}], |
| 154 | temperature:0, max_tokens:16, stream:false, chat_template_kwargs:{enable_thinking:false}}') |
| 155 | code=$(curl -s -m 900 -o "$STATE/reply.json" -w '%{http_code}' "http://127.0.0.1:$port/v1/chat/completions" \ |
| 156 | -H "Authorization: Bearer $AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'Content-Type: application/json' -d "$req") || code=000 |
| 157 | echo "reply (HTTP $code):"; cat "$STATE/reply.json"; echo |
| 158 | if [ "$code" = 200 ] && jq -e --arg m "$MODEL_ID" ' |
| 159 | .object == "chat.completion" and .model == $m |
| 160 | and (.choices | length) == 1 |
| 161 | and .choices[0].message.role == "assistant" |
| 162 | and (.choices[0].message.content | type == "string" and (gsub("\\s"; "") | length) > 0) |
| 163 | and (.choices[0].finish_reason | IN("stop", "length")) |
| 164 | and (.usage.completion_tokens | type == "number" and . > 0)' "$STATE/reply.json" >/dev/null; then |
| 165 | obs reply_ok true; reply_ok=1 |
| 166 | else |
| 167 | obs reply_ok false; reply_ok=0 |
| 168 | fi |
| 169 | obs reply_chars "$(jq '.choices[0].message.content // "" | length' "$STATE/reply.json" 2>/dev/null || echo 0)" |
| 170 | obs completion_tokens "$(jq '.usage.completion_tokens // 0' "$STATE/reply.json" 2>/dev/null || echo 0)" |
| 171 | |
| 172 | # 6. Clean shutdown: SIGTERM, the server finishes, drains and logs "shutdown complete". |
| 173 | kill -TERM "$pid" 2>/dev/null |
| 174 | t0=$SECONDS |
| 175 | while kill -0 "$pid" 2>/dev/null && [ $((SECONDS - t0)) -lt "$STOP_TIMEOUT" ]; do sleep 1; done |
| 176 | if kill -0 "$pid" 2>/dev/null; then |
| 177 | kill -KILL "$pid" 2>/dev/null |
| 178 | obs shutdown_clean false |
| 179 | fail "the server was still running $STOP_TIMEOUT s after SIGTERM (killed)" |
| 180 | fi |
| 181 | pid="" |
| 182 | if grep -q 'aien-serve-http: shutdown complete' "$LOG"; then |
| 183 | obs shutdown_clean true |
| 184 | else |
| 185 | obs shutdown_clean false |
| 186 | sed 's/^/ log: /' "$LOG" >&2 |
| 187 | fail "the server exited after SIGTERM without logging 'shutdown complete'" |
| 188 | fi |
| 189 | [ "$reply_ok" = 1 ] || fail "the chat reply was not a well-formed, non-empty chat.completion from $MODEL_ID (HTTP $code)" |
| 190 | sed 's/^/ log: /' "$LOG" |
| 191 | echo "gb10-serve-chat: PASS ($MODEL_ID on port $port answered and shut down cleanly)" |