#!/usr/bin/env bash # SPDX-License-Identifier: Apache-2.0 # Adapted from this workspace's GLM-OCR bundle (code/scripts/device_server_smoke.sh, Apache-2.0). # Serving gate on the host checkout (no container): start the HTTP server on the device, run server/smoke_test.py # against it, stop the server. The server is the ONLY device process; run the whole script inside ONE device-lock # window, e.g. from the workspace: # # bin/devrun -t 3600 -- bash bundles/deepseek-ocr/code/scripts/device_server_smoke.sh [port] [out.json] # # It exits at the FIRST failure: the server process dying (start-up error, crash) ends the run at once with the # server's exit code instead of waiting for the smoke test's timeout; a smoke failure stops the server and exits 1. # The server is always stopped with SIGTERM (its lifespan releases the model and closes the chip on the main thread) # and given 120 s to exit before a SIGKILL; a server that had to be killed makes the run fail (exit 5). # # Env: the server's DSOCR_* configuration (defaults as tt-model.yaml: dec=mk, ETH dispatch, every mode); # DSOCR_MODEL_DIR = a local checkpoint directory (else the HF cache snapshot of the pinned commit; HF_HUB_OFFLINE=1: # the cache only); DSOCR_SMOKE_WAIT = seconds to wait for the server to become ready (default 3000: the first start # JIT-compiles the kernels and packs the megakernel arena); DSOCR_RUN_DIR = where the server log goes; # DSOCR_SMOKE_ARGS = extra smoke_test.py options (--allow-fake to try the script with DSOCR_BACKEND=fake). # Exit code: 0 PASS; the smoke test's 1; the server's own code if it died (uvicorn: 3 = start-up failed); # 4 crash lines in the server log; 5 the server did not stop; 2 usage. set -u PORT="${1:-20431}" OUT="${2:-}" case "$PORT" in ''|*[!0-9]*) echo "usage: device_server_smoke.sh [port] [out.json]" >&2; exit 2 ;; esac HERE="$(cd "$(dirname "$0")" && pwd)" CODE="$(cd "$HERE/.." && pwd)" RUN="${DSOCR_RUN_DIR:-${TMPDIR:-/tmp}/dsocr-server-smoke}" mkdir -p "$RUN" || exit 2 LOG="$RUN/server-$PORT.log" PY="${PYTHON:-python}" export PYTHONPATH="$CODE${PYTHONPATH:+:$PYTHONPATH}" if ! "$PY" -c "import fastapi, uvicorn" 2>/dev/null; then # the workspace keeps them in tools/dev-overlay for d in "$CODE/../../../tools/dev-overlay"; do [ -d "$d" ] && export PYTHONPATH="$PYTHONPATH:$(cd "$d" && pwd)" done fi "$PY" -c "import fastapi, uvicorn, tt_dsocr.server.app" || { echo "device_server_smoke.sh: FAIL: the server does not import" >&2; exit 1; } export TT_MESH_SHAPE="${TT_MESH_SHAPE:-1x1}" export DSOCR_BACKEND="${DSOCR_BACKEND:-device}" export PYTHONFAULTHANDLER=1 cd "$RUN" || exit 2 "$PY" -u -m uvicorn --host 127.0.0.1 --port "$PORT" --lifespan on tt_dsocr.server.app:app > "$LOG" 2>&1 & SRV=$! SMK="" stop() { local rc=$? trap - EXIT TERM INT [ -n "$SMK" ] && kill -TERM "$SMK" 2>/dev/null if kill -0 "$SRV" 2>/dev/null; then kill -TERM "$SRV" 2>/dev/null for _ in $(seq 1 120); do kill -0 "$SRV" 2>/dev/null || break; sleep 1; done if kill -0 "$SRV" 2>/dev/null; then echo "device_server_smoke.sh: FAIL: the server did not stop within 120 s: SIGKILL (the chip needs a reset)" >&2 kill -KILL "$SRV" 2>/dev/null [ "$rc" = 0 ] && rc=5 fi fi wait "$SRV" 2>/dev/null if grep -qE "terminate called|Fatal Python error|Aborted|CHIP_IN_USE" "$LOG" 2>/dev/null; then echo "device_server_smoke.sh: FAIL: crash lines in $LOG" >&2 [ "$rc" = 0 ] && rc=4 fi echo "device_server_smoke.sh: rc=$rc; server log $LOG" exit "$rc" } trap stop EXIT trap 'exit 143' TERM trap 'exit 130' INT python3 "$CODE/tt_dsocr/server/smoke_test.py" --url "http://127.0.0.1:$PORT" --wait "${DSOCR_SMOKE_WAIT:-3000}" \ ${OUT:+--out "$OUT"} ${DSOCR_SMOKE_ARGS:-} & SMK=$! while :; do if ! kill -0 "$SMK" 2>/dev/null; then wait "$SMK" rc=$? SMK="" [ "$rc" = 0 ] || echo "device_server_smoke.sh: FAIL: smoke test exit $rc" >&2 exit "$rc" fi if ! kill -0 "$SRV" 2>/dev/null; then wait "$SRV" rc=$? echo "device_server_smoke.sh: FAIL: the server exited ($rc) before the smoke test finished; last lines:" >&2 tail -n 30 "$LOG" >&2 exit $(( rc == 0 ? 1 : rc )) fi sleep 2 done