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 / serve-27b.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 | # Start the Qwen3.8-27B model server (aien-serve-http on AIEN's own Omega GPU engine) in the |
| 18 | # background, with the flags the project's own live server uses. Text chat only, one request at a |
| 19 | # time, OpenAI-compatible API on http://127.0.0.1:$PORT/v1. |
| 20 | # |
| 21 | # Usage: serve-27b.sh [--foreground] [--no-wait] |
| 22 | # --foreground run in this terminal (for systemd or a supervisor) instead of detaching |
| 23 | # --no-wait detach and return at once, without waiting for the model to load (the exit |
| 24 | # code then says nothing about the load; check the log) |
| 25 | # Settings (environment variables, all optional): |
| 26 | # BIN server binary default $HOME/.local/bin/aien-serve-http |
| 27 | # MODEL_DIR Qwen3.8-27B FP8 folder default $HOME/models/qwen3.8-27b-fp8-017b9c7a |
| 28 | # PORT TCP port on 127.0.0.1 default 18091 |
| 29 | # STATE_DIR log and pid file folder default ${XDG_STATE_HOME:-$HOME/.local/state}/aien |
| 30 | # LOG server log default $STATE_DIR/serve-27b.log |
| 31 | # MAX_CONTEXT, MAX_TOKENS_CAP default 131072 and 16384 |
| 32 | # READY_TIMEOUT seconds to wait for "ready" default 2400 (lock queue 1800 + load) |
| 33 | # Memory: the loader needs 49,497 MiB (48.3 GiB) of MemAvailable, checked after it holds the GPU lock. |
| 34 | # Ready: http://127.0.0.1:$PORT/v1/models answers and lists qwen3.8-27b-fp8 (needs curl). |
| 35 | # Exit: 0 ready (or started, with --no-wait), 1 a check failed or the server exited without |
| 36 | # serving, whatever its exit status (for example the loader refused for lack of memory; |
| 37 | # the status and the reason are printed), 2 not ready when READY_TIMEOUT ran out (the |
| 38 | # server keeps running). |
| 39 | set -euo pipefail |
| 40 | |
| 41 | BIN=${BIN:-$HOME/.local/bin/aien-serve-http} |
| 42 | MODEL_DIR=${MODEL_DIR:-$HOME/models/qwen3.8-27b-fp8-017b9c7a} |
| 43 | PORT=${PORT:-18091} |
| 44 | STATE_DIR=${STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/aien} |
| 45 | LOG=${LOG:-$STATE_DIR/serve-27b.log} |
| 46 | PIDFILE=$STATE_DIR/serve-27b.pid |
| 47 | MAX_CONTEXT=${MAX_CONTEXT:-131072} |
| 48 | MAX_TOKENS_CAP=${MAX_TOKENS_CAP:-16384} |
| 49 | FOREGROUND=0 |
| 50 | WAIT=1 |
| 51 | READY_TIMEOUT=${READY_TIMEOUT:-2400} |
| 52 | for a in "$@"; do |
| 53 | case "$a" in |
| 54 | --foreground) FOREGROUND=1 ;; |
| 55 | --no-wait) WAIT=0 ;; |
| 56 | -h|--help) sed -n "17,38p" "$0"; exit 0 ;; |
| 57 | *) echo "serve-27b: unknown argument $a (try --help)" >&2; exit 1 ;; |
| 58 | esac |
| 59 | done |
| 60 | |
| 61 | fail() { echo "serve-27b: $*" >&2; exit 1; } |
| 62 | [ -n "${AIEN_SERVE_HTTP_BEARER_TOKEN:-}" ] || fail "AIEN_SERVE_HTTP_BEARER_TOKEN is required before starting the server" |
| 63 | [[ "$READY_TIMEOUT" =~ ^[1-9][0-9]*$ ]] || fail "READY_TIMEOUT must be a positive number of seconds" |
| 64 | if [ "$FOREGROUND" -eq 0 ] && [ "$WAIT" -eq 1 ]; then |
| 65 | command -v curl >/dev/null || fail "curl is needed to see when the server is ready (apt install curl), or pass --no-wait" |
| 66 | fi |
| 67 | |
| 68 | [ -x "$BIN" ] || fail "server binary not found or not executable: $BIN (install it with install.sh --with-server, or set BIN)" |
| 69 | # The binary must have the real Omega engine linked. A CPU stub build would start and then fail |
| 70 | # every request, so refuse it here. |
| 71 | info=$("$BIN" --version 2>&1) || fail "$BIN --version failed; is it an aien-serve-http build from this branch or newer? |
| 72 | $info" |
| 73 | grep -q '^omega gpu engine: native' <<<"$info" && grep -q '^qwen38 engine: native' <<<"$info" \ |
| 74 | || fail "$BIN was built without the Omega GPU engine (CPU stub). Rebuild with install.sh --with-server. |
| 75 | $info" |
| 76 | [ -f "$MODEL_DIR/config.json" ] && [ -f "$MODEL_DIR/tokenizer.json" ] \ |
| 77 | || fail "model folder $MODEL_DIR has no config.json or tokenizer.json. Download Qwen/Qwen3.8-27B-FP8 (revision 017b9c7af6b5689d5dd426a76e0bc077eb5ca20a) there, or set MODEL_DIR." |
| 78 | compgen -G '/dev/nvidia*' >/dev/null || fail "no /dev/nvidia* device files: the NVIDIA driver is not loaded" |
| 79 | mkdir -p "$STATE_DIR" |
| 80 | if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then |
| 81 | fail "a server started by this script is already running (pid $(cat "$PIDFILE")). Stop it first: kill -TERM $(cat "$PIDFILE")" |
| 82 | fi |
| 83 | if command -v ss >/dev/null && ss -ltn "sport = :$PORT" 2>/dev/null | grep -q LISTEN; then |
| 84 | fail "port $PORT is already in use. Pick another one with PORT=..." |
| 85 | fi |
| 86 | # The loader needs 41,305 MiB for the model and its working memory (at the default MAX_CONTEXT) |
| 87 | # plus 8,192 MiB kept free for other jobs: 49,497 MiB (48.3 GiB) of MemAvailable. A server that |
| 88 | # starts has peaked at 38,499,160 kB (36.7 GiB) resident on the current build (VmHWM, |
| 89 | # evidence/qwen38-serve-memory-20261019/proc-status.txt). The loader makes the binding check itself |
| 90 | # once it holds the GPU lock; this early look only warns, because the lock wait can be many minutes |
| 91 | # and free memory moves while you wait. |
| 92 | NEED_MIB=49497 |
| 93 | avail_kib=$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo 2>/dev/null || echo 0) |
| 94 | if [ "${avail_kib:-0}" -lt $((NEED_MIB * 1024)) ]; then |
| 95 | echo "serve-27b: warning: only $((avail_kib / 1024)) MiB of memory available; the loader needs $NEED_MIB MiB (48.3 GiB) free. It checks again when it gets the GPU lock and refuses if still short." >&2 |
| 96 | fi |
| 97 | |
| 98 | ARGS=(--engine omega --model-dir "$MODEL_DIR" --port "$PORT" |
| 99 | --max-context "$MAX_CONTEXT" --max-tokens-cap "$MAX_TOKENS_CAP" --thinking off) |
| 100 | echo "$(date -u +%FT%TZ) serve-27b: starting $BIN ${ARGS[*]}" >> "$LOG" |
| 101 | if [ "$FOREGROUND" -eq 1 ]; then |
| 102 | exec "$BIN" "${ARGS[@]}" |
| 103 | fi |
| 104 | log_start=$(stat -c %s "$LOG") |
| 105 | setsid nohup "$BIN" "${ARGS[@]}" >> "$LOG" 2>&1 < /dev/null & |
| 106 | pid=$! |
| 107 | echo $pid > "$PIDFILE" |
| 108 | |
| 109 | # A dead process (a zombie counts: this script is its parent) means the load was refused or failed. |
| 110 | gone() { local s; s=$(ps -o stat= -p "$pid" 2>/dev/null || true); [ -z "$s" ] || [ "${s#Z}" != "$s" ]; } |
| 111 | new_log() { tail -c +$((log_start + 1)) "$LOG"; } |
| 112 | # Ready means the API answers and advertises the 27B model, not merely that the process is alive. |
| 113 | api_ready() { |
| 114 | local models |
| 115 | models=$(curl --fail --silent --max-time 2 "http://127.0.0.1:$PORT/v1/models" 2>/dev/null) \ |
| 116 | && grep -Eq '"id"[[:space:]]*:[[:space:]]*"qwen3\.8-27b-fp8"' <<<"$models" |
| 117 | } |
| 118 | |
| 119 | if [ "$WAIT" -eq 1 ]; then |
| 120 | echo "serve-27b: waiting for pid $pid to load the model (it may queue for the GPU lock first; log $LOG)" >&2 |
| 121 | t0=$SECONDS |
| 122 | until api_ready; do |
| 123 | if gone; then |
| 124 | rc=0; wait "$pid" 2>/dev/null || rc=$? |
| 125 | rm -f "$PIDFILE" |
| 126 | echo "serve-27b: the server exited (status $rc) before http://127.0.0.1:$PORT/v1/models listed qwen3.8-27b-fp8; it did not start. Last log lines ($LOG):" >&2 |
| 127 | new_log | tail -n 5 | sed 's/^/ /' >&2 |
| 128 | exit 1 |
| 129 | fi |
| 130 | if [ $((SECONDS - t0)) -ge "$READY_TIMEOUT" ]; then |
| 131 | echo "serve-27b: not ready after $READY_TIMEOUT s: http://127.0.0.1:$PORT/v1/models does not list qwen3.8-27b-fp8 yet; pid $pid is still running (likely queued for the GPU lock). Watch: tail -f $LOG" >&2 |
| 132 | exit 2 |
| 133 | fi |
| 134 | [ $(((SECONDS - t0) % 60)) -eq 0 ] && [ $((SECONDS - t0)) -gt 0 ] && echo "serve-27b: still loading after $((SECONDS - t0)) s" >&2 |
| 135 | sleep 1 |
| 136 | done |
| 137 | fi |
| 138 | cat <<EOT |
| 139 | serve-27b: $([ "$WAIT" -eq 1 ] && echo "ready" || echo "started, not waited for"), pid $pid |
| 140 | log: $LOG |
| 141 | loading takes about one minute once the GPU is free; before that it queues for the GPU lock. |
| 142 | check: curl -s http://127.0.0.1:$PORT/v1/models (lists qwen3.8-27b-fp8 when ready) |
| 143 | chat: curl -s http://127.0.0.1:$PORT/v1/chat/completions -H "Authorization: Bearer \$AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'Content-Type: application/json' \\ |
| 144 | -d '{"messages":[{"role":"user","content":"Hello"}],"max_tokens":64}' |
| 145 | stop: kill -TERM \$(cat $PIDFILE) |
| 146 | (it finishes the reply in progress, then exits; that can take a few minutes) |
| 147 | EOT |