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 / gb10-serve-chat.sh

191 lines · 10305 bytes Raw Blame History
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).
47set -uo pipefail
48ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
49cd "$ROOT" || exit 3
50ENV_SH=${1:?usage: scripts/gb10-serve-chat.sh <pins env.sh>}
51TARGET=${CARGO_TARGET_DIR:-$ROOT/target}
52# shellcheck source=tests/gates/step-home.sh
53. "$ROOT/tests/gates/step-home.sh" || exit 3
54gb10_model_dir
55MODEL_ID=qwen3.8-27b-fp8
56READY_TIMEOUT=${READY_TIMEOUT:-2400}
57STOP_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.
60NEED_MIB=49497
61
62# The leading newline keeps the line whole when a tool left a partial line.
63obs() { printf '\nAIEN-OBS %s %s\n' "$1" "$2"; }
64not_run() { echo "gb10-serve-chat: NOT_RUN $1: $2" >&2; obs not_run_reason "\"$1\""; exit 77; }
65fail() { echo "gb10-serve-chat: FAIL: $*" >&2; exit 1; }
66json_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.
68obs model_dir "$(json_str "$MODEL_DIR")"
69obs model_dir_source "\"$MODEL_DIR_SOURCE\""
70for 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.
73if [ -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"
77fi
78export 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.
88avail_kib=$(awk '/^MemAvailable:/ {a=$2} /^Mapped:/ {m=$2} END {print a-m}' /proc/meminfo 2>/dev/null)
89avail_mib=$(( ${avail_kib:-0} / 1024 ))
90obs 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)"
100cargo build --locked --release -p aien-serve-http
101rc=$?
102obs build_rc "$rc"
103[ "$rc" -eq 0 ] || fail "cargo build of aien-serve-http failed (rc $rc)"
104BIN=$TARGET/release/aien-serve-http
105info=$("$BIN" --version 2>&1) || fail "$BIN --version failed: $info"
106echo "$info"
107if grep -q '^omega gpu engine: native' <<<"$info" && grep -q '^qwen38 engine: native' <<<"$info"; then
108 obs engine_native true
109else
110 obs engine_native false
111 fail "the built server carries the CPU stub, not the Omega GPU engine"
112fi
113
114STATE=$(mktemp -d) || fail "mktemp failed"
115pid=""
116stop_server() {
117 [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null && kill -TERM "$pid" 2>/dev/null
118 rm -rf "$STATE"
119}
120trap 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.
125if [ "${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"'
127else
128 obs chip_lock '"server takes /tmp/aien-gb10.lock"'
129fi
130
131# 4. Start through the project's own start script on a free loopback port (never the live 18091).
132port=""
133for p in $(seq 18190 18199); do
134 ss -ltn "sport = :$p" 2>/dev/null | grep -q LISTEN || { port=$p; break; }
135done
136[ -n "$port" ] || fail "no free port in 18190-18199"
137LOG=$STATE/serve-27b.log
138BIN=$BIN MODEL_DIR=$MODEL_DIR PORT=$port STATE_DIR=$STATE LOG=$LOG READY_TIMEOUT=$READY_TIMEOUT \
139 bash tools/serve-27b.sh
140rc=$?
141obs serve_rc "$rc"
142[ -f "$STATE/serve-27b.pid" ] && pid=$(cat "$STATE/serve-27b.pid")
143if [ "$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"
149fi
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.
153req=$(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}}')
155code=$(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
157echo "reply (HTTP $code):"; cat "$STATE/reply.json"; echo
158if [ "$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
166else
167 obs reply_ok false; reply_ok=0
168fi
169obs reply_chars "$(jq '.choices[0].message.content // "" | length' "$STATE/reply.json" 2>/dev/null || echo 0)"
170obs 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".
173kill -TERM "$pid" 2>/dev/null
174t0=$SECONDS
175while kill -0 "$pid" 2>/dev/null && [ $((SECONDS - t0)) -lt "$STOP_TIMEOUT" ]; do sleep 1; done
176if 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)"
180fi
181pid=""
182if grep -q 'aien-serve-http: shutdown complete' "$LOG"; then
183 obs shutdown_clean true
184else
185 obs shutdown_clean false
186 sed 's/^/ log: /' "$LOG" >&2
187 fail "the server exited after SIGTERM without logging 'shutdown complete'"
188fi
189[ "$reply_ok" = 1 ] || fail "the chat reply was not a well-formed, non-empty chat.completion from $MODEL_ID (HTTP $code)"
190sed 's/^/ log: /' "$LOG"
191echo "gb10-serve-chat: PASS ($MODEL_ID on port $port answered and shut down cleanly)"