Files
alzeo/run.sh
T
Hasan BasbunarandClaude Opus 5.5 a9fd5e4c3d 27B lane: SGLang says what the prefix cache served (#49)
Discussion #49 asked for --enable-cache-report. With it, each answer says
how much of its prompt the prefix cache served:
usage.prompt_tokens_details.cached_tokens on the OpenAI routes (8,192
then 9,472 of a 9,539-token prompt that repeats its system message),
cache_read_input_tokens beside input_tokens on /v1/messages (31 and
9,600 where it said 9,631 before), and the proxy's
x-systemone-cached-tokens (0, then 2,176). Without it the cache serves
the same and says nothing.

Measured on the 27B lane behind the proxy on 2026-10-24, with the flag
and without: the SDKs, fetch under Node and Bun, LiteLLM, Claude Code,
opencode, Hermes and pi each did their task; opencode 1.19.4 recorded
33,536 of a turn's 33,650 prompt tokens as read from the cache, its
total unchanged, so it compacts where it did; bench.sh gave 64.9 against
64.0 tok/s (three runs each, their spreads overlapping).

The three 27B units and run.sh pass it, and the recipes' drift check
knows the switch. The flash lane, on another build, is not measured and
does not pass it. tests/test_engine_cache_report.py fails on the old
files and passes on the new ones; the 28 test modules that read these
files pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 10:20:12 +02:00

157 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run the exact same pinned config as the systemd service, but in the
# foreground: no systemd, no sudo, Ctrl+C stops it, --rm leaves nothing behind.
# For people who want to try / A/B this without committing to a service.
#
# Prerequisite (one time): ./install.sh --no-service (image, checkpoints, key, template)
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
die() { printf '\n\033[1;31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
# ── Same pins as install.sh (read from it: single source of truth) ──
PINS="$(grep -E '^(IMAGE|STOCK_REPO|STOCK_REV|UNC_REPO|UNC_REV|FP8_REPO|FP8_REV|UNCFP8_REPO|UNCFP8_REV|MODEL_CHOICE|CONTEXT_MODE|DRAFT2_REPO|DRAFT2_REV|DRAFT2_QUANT|DRAFT2_TOKENS|SERVE_IMAGE|PORT|HF_CACHE|CONFIG_DIR)=' "$REPO_DIR/install.sh" || true)"
# A count of matched lines was the old check, and adding a pin broke both scripts
# at once (it did, on 2026-09-08). What matters is not how many lines matched but
# whether every name this script goes on to use is defined, so that is what is
# asserted, and a failure says which one.
eval "$PINS"
for _v in IMAGE STOCK_REPO STOCK_REV UNC_REPO UNC_REV FP8_REPO FP8_REV UNCFP8_REPO UNCFP8_REV MODEL_CHOICE DRAFT2_REPO DRAFT2_REV DRAFT2_QUANT DRAFT2_TOKENS SERVE_IMAGE PORT HF_CACHE CONFIG_DIR; do
eval "[ -n \"\${$_v:-}\" ]" || die "install.sh no longer defines $_v (repo layout changed?)"
done
unset _v
# CONTEXT_MODE is checked for being DEFINED, not for being non-empty: since
# v1.12.1 install.sh leaves it empty on purpose ("not chosen yet") and resolves
# the default further down, once the lane and the flags are known. The pin block
# must still break if the name disappears, which is what this line is for.
grep -qE '^CONTEXT_MODE=' "$REPO_DIR/install.sh" \
|| die "install.sh no longer defines CONTEXT_MODE (repo layout changed?)"
# A 64 GB DGX Spark: this runs the 27B with a 128 GB box's memory settings, where its own unit
# (qwen38-sglang-64.service.template) has settings sized for it, and it took the FP8 targets too.
# Refused there, as ./install.sh --no-service is, which prepares it (found in review, 2026-10-23).
[ "$(python3 -I "$REPO_DIR/boxmem.py" class 2>/dev/null)" != 64 ] \
|| die "./run.sh runs the 27B with the memory settings of a 128 GB DGX Spark, and this box is a 64 GB one. Its service, which ./install.sh installs, runs the 27B with settings sized for it."
# run.sh is the foreground path, which serves the native window by definition.
# An explicit CONTEXT_MODE=1m from the environment survives this and is refused
# by name below, which is the behaviour that path has always had.
CONTEXT_MODE="${CONTEXT_MODE:-native}"
case "${MODEL_CHOICE}" in
stock) MODEL_REPO="$STOCK_REPO"; MODEL_REV="${MODEL_REV:-$STOCK_REV}" ;;
uncensored) MODEL_REPO="$UNC_REPO"; MODEL_REV="${MODEL_REV:-$UNC_REV}" ;;
fp8) MODEL_REPO="$FP8_REPO"; MODEL_REV="${MODEL_REV:-$FP8_REV}" ;;
uncensored-fp8) MODEL_REPO="$UNCFP8_REPO"; MODEL_REV="${MODEL_REV:-$UNCFP8_REV}" ;;
flash|flash-nvda|flash-uncensored) die "the flash targets are service-only in this release: MODEL_CHOICE=$MODEL_CHOICE ./install.sh (./run.sh covers the 27B targets)" ;;
*) die "MODEL_CHOICE must be stock, uncensored, fp8, uncensored-fp8, flash, flash-nvda or flash-uncensored (got: ${MODEL_CHOICE})" ;;
esac
# See install.sh: Qwen's FP8 checkpoint carries no KV scales, so without this the
# KV cache falls back to bf16 and costs about half the pool.
case "${MODEL_CHOICE}" in
fp8|uncensored-fp8) KV_CACHE_ARGS="--kv-cache-dtype fp8_e4m3" ;;
*) KV_CACHE_ARGS="" ;;
esac
if [ "${CONTEXT_MODE}" = "1m" ]; then
die "CONTEXT_MODE=1m needs the systemd path (keepalive proxy + YaRN service units): run CONTEXT_MODE=1m ./install.sh. ./run.sh serves the native 262144 config only."
fi
# The engine listens where the service's does: on loopback, unless ENGINE_BIND says
# otherwise. This path has no proxy in front of it, and it served 0.0.0.0: the engine,
# with none of the proxy's guards against the fields it dies on, open to anyone on the
# network who holds the key (found in review, 2026-09-24).
ENGINE_BIND="${ENGINE_BIND:-127.0.0.1}"
case "$ENGINE_BIND" in
*[!0-9.]*|"") die "ENGINE_BIND takes an IPv4 address (got: $ENGINE_BIND)" ;;
esac
# The fix-it command echoed by every check below, carrying the active choices
# so following it prepares THIS configuration (not the defaults).
PREP="./install.sh --no-service"
[ "$MODEL_CHOICE" != "stock" ] && PREP="MODEL_CHOICE=$MODEL_CHOICE $PREP"
# ── Everything prepared? ──
[ -s "$CONFIG_DIR/api-key" ] || die "no API key at $CONFIG_DIR/api-key. Run: $PREP"
[ -s "$CONFIG_DIR/chat-template-sglang.jinja" ] || die "no patched template in $CONFIG_DIR. Run: $PREP"
# Checkpoints: validate the snapshot of the PINNED revision, never
# "the first snapshot dir": a cache can hold several revisions (an old
# MODEL_REV=main attempt, an upstream bump), and `ls | head -1` picks
# alphabetically, failing a healthy install on a stale partial dir (issue #5).
for PAIR in "$MODEL_REPO=$MODEL_REV" "$DRAFT2_REPO=$DRAFT2_REV"; do
REPO="${PAIR%%=*}"; REV="${PAIR#*=}"
DIR="$HF_CACHE/hub/models--${REPO//\//--}"
[ -d "$DIR/snapshots" ] || die "checkpoint $REPO not found in $HF_CACHE. Run: $PREP"
# A ref name (e.g. 'main') resolves through refs/, a sha is used as-is.
REV_SHA="$REV"
[ -f "$DIR/refs/$REV" ] && REV_SHA="$(cat "$DIR/refs/$REV")"
SNAP="$DIR/snapshots/$REV_SHA/"
[ -d "$SNAP" ] || die "checkpoint $REPO is cached, but not at the pinned revision ${REV_SHA:0:12} (cached: $(ls "$DIR/snapshots" 2>/dev/null | cut -c1-12 | paste -sd' ')). Re-run: $PREP"
# An interrupted download leaves snapshots/ in place with only the small
# files, so also require finished blobs and actual weights (credit: helge).
if compgen -G "$DIR/blobs/*.incomplete" >/dev/null 2>&1; then
die "checkpoint $REPO download is incomplete (blobs/*.incomplete). Re-run: $PREP (it resumes)"
fi
compgen -G "${SNAP}*.safetensors" >/dev/null 2>&1 \
|| die "checkpoint $REPO has no weight files in its pinned snapshot ${REV_SHA:0:12}. Re-run: $PREP"
done
# A YaRN-patched cache (left by a 1m install) crashes a native server at load:
# refuse early with the restore path instead of ten minutes into the boot.
for PAIR in "$MODEL_REPO=$MODEL_REV" "$DRAFT2_REPO=$DRAFT2_REV"; do
REPO="${PAIR%%=*}"; REV="${PAIR#*=}"
CHK_OUT="$(python3 "$REPO_DIR/patch-yarn.py" --check "$HF_CACHE" "$REPO" "$REV" 2>&1)" \
|| { printf '%s\n' "$CHK_OUT" >&2; die "checkpoint $REPO is YaRN-patched for 1M context (details above). With a backup present, restore the native configs with: CONTEXT_MODE=native $PREP"; }
done
docker image inspect "$SERVE_IMAGE" >/dev/null 2>&1 || die "serving image $SERVE_IMAGE not built. Run: $PREP"
# ── Nothing else may be using the GPU or the port (GB10: one engine at a time) ──
if systemctl is-active --quiet qwen38-sglang 2>/dev/null; then
die "the qwen38-sglang systemd service is running. Stop it first: sudo systemctl stop qwen38-sglang"
fi
for NAME in qwen38-sglang qwen38-sglang-run; do
[ -z "$(docker ps -q -f "name=^${NAME}$")" ] || die "container $NAME is already running (docker stop $NAME)"
done
if ss -tlnH 2>/dev/null | awk '{print $4}' | grep -q ":$PORT\$"; then
die "port $PORT is already in use (ss -tlnp | grep :$PORT). Free it or run: PORT=<other> ./run.sh"
fi
GPU_APPS="$(nvidia-smi --query-compute-apps=pid,process_name --format=csv,noheader 2>/dev/null || true)"
[ -z "$GPU_APPS" ] || die "the GPU is busy. This config needs the machine to itself (unified memory):
$GPU_APPS"
# the key reaches the server through a file SGLang merges in memory, never the command line
bash "$REPO_DIR/engine-secrets.sh" "$CONFIG_DIR" || die "could not write the engine's key file from $CONFIG_DIR/api-key"
mkdir -p "$CONFIG_DIR/sglang-cache"
echo "Starting in the foreground (first boot ≈ 9 min: torch.compile + CUDA graph capture)."
echo " Ready when the log says: The server is fired up and ready to roll!"
echo " Config: native 262144 window, template defaults (memory fraction 0.50)."
echo " A hand-tuned service unit may serve a different fraction: compare like with like."
echo " Listening on: $ENGINE_BIND:$PORT, with no proxy in front (ENGINE_BIND=0.0.0.0 opens it to the network)"
echo " Test from another shell: curl http://127.0.0.1:$PORT/health"
echo " opencode: provider config at $CONFIG_DIR/opencode.json (README, \"opencode integration\")"
echo " Stop: Ctrl+C (container removed; compile cache kept for faster next boots)"
echo
# Verbatim the systemd unit's docker run (same caps, mounts, flags), foreground.
exec docker run --rm --name qwen38-sglang-run --gpus all \
--memory 100g --memory-swap 100g --shm-size 16g --network host --ipc=host \
-e TORCHINDUCTOR_CACHE_DIR=/cache/inductor \
-e SGLANG_ENABLE_REQUEST_HEADER_OVERRIDES=1 \
-e HF_HUB_OFFLINE=1 \
-v "$CONFIG_DIR/sglang-cache":/cache \
-v "$HF_CACHE":/root/.cache/huggingface \
-v "$CONFIG_DIR":/out:ro \
"$SERVE_IMAGE" \
python3 -m sglang.launch_server \
--model-path "$MODEL_REPO" --revision "$MODEL_REV" --tp-size 1 \
--served-model-name qwen3.8-27b \
--mem-fraction-static 0.50 ${KV_CACHE_ARGS} \
--attention-backend flashinfer --chunked-prefill-size 8192 \
--disable-prefill-cuda-graph --cuda-graph-max-bs-decode 8 \
--disable-flashinfer-autotune \
--speculative-algorithm DFLASH --speculative-draft-model-path "$DRAFT2_REPO" \
--speculative-draft-model-revision "$DRAFT2_REV" \
--speculative-num-draft-tokens "$DRAFT2_TOKENS" --speculative-draft-model-quantization "$DRAFT2_QUANT" \
--mamba-radix-cache-strategy extra_buffer --mamba-ssm-dtype bfloat16 \
--max-mamba-cache-size 96 --max-running-requests 8 \
--enable-torch-compile --torch-compile-max-bs 4 \
--num-continuous-decode-steps 2 \
--reasoning-parser qwen3 --tool-call-parser qwen3_coder \
--chat-template /out/chat-template-sglang.jinja \
--sleep-on-idle \
--enable-metrics --enable-cache-report \
--config /out/engine-secrets.yaml \
--host "$ENGINE_BIND" --port "$PORT"