okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge commit 'ae31e3cd3' into f-split-int2aeb246cf

main
6.6 KiB108 linesraw
1#!/bin/bash
2# Smoke test of one Strata image on a real card (docs/DOCKER.md, "Build it yourself"). Run it under your own GPU lock if the box is shared.
3#
4#   IMAGE=strata:0.1.45-sycl NAME=strata-t PORT=18080 BASE=http://127.0.0.1:18080 \
5#   RUN_FLAGS="--device /dev/dri -p 18080:8080" ENVS="-e MODEL=IQ3_S -e MODEL_PATH=/models -e API_KEY=..." VOLS="-v $HOME/dt/data:/data -v /m:/models:ro" \
6#   GPU_USED_CMD='nvidia-smi --query-gpu=memory.used --format=csv,noheader' OUT=$HOME/dt/out docker/smoke-test.sh
7#
8# Steps: run -> wait for the model (health check) -> GET /v1/models -> pong chat -> tool call -> docker exec strata monitor --once ->
9# strata connect opencode --check (from inside) -> docker stop (time, exit code, log) -> GPU memory back, no strata process left.
10set -u
11: "${IMAGE:?}" "${NAME:?}" "${BASE:?}" "${OUT:?}"
12DOCKER=${DOCKER:-docker}
13WAIT=${WAIT:-2400}
14KEY=${KEY:-test-key-$(head -c 12 /dev/urandom | od -An -tx1 | tr -d ' \n')}
15mkdir -p "$OUT"
16LOG="$OUT/test.log"
17exec > >(tee -a "$LOG") 2>&1
18ts() { date +%H:%M:%S; }
19step() { echo; echo "### $(ts) $*"; }
20fail=0
21ok() { echo "PASS: $*"; }
22bad() { echo "FAIL: $*"; fail=1; }
23gpu_used() { if [ -n "${GPU_USED_CMD:-}" ]; then bash -c "$GPU_USED_CMD" 2>&1 | head -3 | tr '\n' ' '; else echo n/a; fi; }
24procs() { pgrep -af "strata --serve|strata.py (serve|runtime)" | grep -v -E "pgrep|test_container|bash -c" | cut -c1-120; }
25
26step "before: GPU used = $(gpu_used); strata-like host processes:"; procs
27$DOCKER rm -f "$NAME" >/dev/null 2>&1
28step "docker run"
29# shellcheck disable=SC2086
30$DOCKER run -d --name "$NAME" --stop-timeout 150 --shm-size 2g --ulimit memlock=-1:-1 -e API_KEY="$KEY" ${ENVS:-} ${VOLS:-} ${RUN_FLAGS:-} "$IMAGE" || { bad "docker run"; exit 1; }
31t0=$(date +%s)
32
33step "wait for the model (health check = GET /v1/models with the key), up to ${WAIT}s"
34state=""
35while :; do
36  now=$(date +%s); el=$((now - t0))
37  st=$($DOCKER inspect -f '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' "$NAME" 2>/dev/null)
38  if [ "$st" != "$state" ]; then echo "$(ts) +${el}s container: $st"; state=$st; fi
39  case "$st" in exited*|dead*) bad "container ended before serving"; $DOCKER logs --tail 60 "$NAME"; exit 1 ;; esac
40  code=$(curl -s -o /dev/null -w '%{http_code}' -m 5 -H "Authorization: Bearer $KEY" "$BASE/v1/models" 2>/dev/null)
41  [ "$code" = "200" ] && break
42  [ "$el" -gt "$WAIT" ] && { bad "not serving after ${WAIT}s"; $DOCKER logs --tail 60 "$NAME"; exit 1; }
43  sleep 10
44done
45echo "serving after $((($(date +%s) - t0)))s"
46step "first log lines"; $DOCKER logs "$NAME" 2>&1 | grep -v "^\s*$" | head -40 | cut -c1-200
47
48step "GET /v1/models"
49models=$(curl -s -m 10 -H "Authorization: Bearer $KEY" "$BASE/v1/models"); echo "$models" | cut -c1-300
50echo "$models" | grep -q '"data"' && ok "GET /v1/models" || bad "GET /v1/models"
51code=$(curl -s -o /dev/null -w '%{http_code}' -m 5 "$BASE/v1/models"); [ "$code" = "401" ] && ok "no key -> 401" || bad "no key gave $code (expected 401)"
52MODEL=$(echo "$models" | python3 -c 'import sys,json; print(json.load(sys.stdin)["data"][0]["id"])' 2>/dev/null || echo strata)
53
54step "pong chat"
55r=$(curl -s -m 300 -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' "$BASE/v1/chat/completions" \
56    -d "{\"model\":\"$MODEL\",\"max_tokens\":256,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with exactly one word: pong\"}]}")
57echo "$r" | cut -c1-400
58echo "$r" | python3 -c 'import sys,json; t=json.load(sys.stdin)["choices"][0]["message"].get("content") or ""; print("answer:",repr(t)); sys.exit(0 if "pong" in t.lower() else 1)' && ok "pong chat" || bad "pong chat"
59
60step "tool call"
61r=$(curl -s -m 300 -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' "$BASE/v1/chat/completions" \
62    -d "{\"model\":\"$MODEL\",\"max_tokens\":1536,\"messages\":[{\"role\":\"user\",\"content\":\"What is the weather in Paris? Use the get_weather tool.\"}],\"tools\":[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Weather of a city\",\"parameters\":{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}},\"required\":[\"city\"]}}}]}")
63echo "$r" | cut -c1-500
64echo "$r" | python3 -c '
65import sys,json
66m=json.load(sys.stdin)["choices"][0]["message"]; tc=m.get("tool_calls") or []
67print("tool_calls:",[(t["function"]["name"],t["function"]["arguments"]) for t in tc])
68sys.exit(0 if tc and tc[0]["function"]["name"]=="get_weather" and "paris" in tc[0]["function"]["arguments"].lower() else 1)' && ok "tool call get_weather(Paris)" || bad "tool call"
69
70step "docker exec strata monitor --once (no key typed: the wrapper supplies it)"
71$DOCKER exec "$NAME" strata monitor --once 2>&1 | head -40 && ok "monitor --once ran" || bad "monitor --once"
72step "docker exec strata status"
73$DOCKER exec "$NAME" strata status 2>&1 | head -12
74step "docker exec strata connect opencode --check (inside the container)"
75chk=$($DOCKER exec "$NAME" strata connect opencode --check 2>&1); echo "$chk" | tail -8
76echo "$chk" | grep -q "^FAIL" && bad "connect opencode --check reported a failure (model answered in words instead of calling the tool? see above)" || ok "connect opencode --check: prompt and tool call"
77step "GPU used while serving: $(gpu_used)"
78step "docker stats / health"
79$DOCKER inspect -f 'health={{.State.Health.Status}}' "$NAME"
80$DOCKER stats --no-stream --format 'mem={{.MemUsage}} cpu={{.CPUPerc}}' "$NAME"
81
82step "docker stop (clean stop)"
83s0=$(date +%s)
84$DOCKER stop "$NAME"
85s1=$(date +%s)
86ec=$($DOCKER inspect -f '{{.State.ExitCode}}' "$NAME")
87echo "stop took $((s1 - s0))s, exit code $ec"
88[ "$ec" = "0" ] && ok "exit code 0 after docker stop" || bad "exit code $ec after docker stop"
89$DOCKER logs --tail 12 "$NAME" 2>&1 | cut -c1-200
90sleep 3
91echo "GPU used after stop: $(gpu_used)"
92leftover=$(procs); [ -z "$leftover" ] && ok "no strata process left on the host" || { bad "processes left:"; echo "$leftover"; }
93
94if [ "${RESTART:-1}" = "1" ]; then
95  step "docker start (second start: the preset in /data is reused, no download)"
96  t1=$(date +%s); $DOCKER start "$NAME" >/dev/null
97  while :; do
98    code=$(curl -s -o /dev/null -w '%{http_code}' -m 5 -H "Authorization: Bearer $KEY" "$BASE/v1/models" 2>/dev/null)
99    [ "$code" = "200" ] && break
100    [ $(($(date +%s) - t1)) -gt "$WAIT" ] && { bad "second start not serving"; break; }
101    sleep 10
102  done
103  echo "second start serving after $(($(date +%s) - t1))s"; [ "$code" = "200" ] && ok "restart reuses /data"
104  $DOCKER logs --tail 25 "$NAME" 2>&1 | grep -E "Config|preset|Serving|Starting|installed|tuned" | cut -c1-200
105  $DOCKER stop "$NAME" >/dev/null; echo "second stop exit code $($DOCKER inspect -f '{{.State.ExitCode}}' "$NAME"); GPU used $(gpu_used)"
106fi
107step "RESULT: $([ $fail = 0 ] && echo ALL PASS || echo SOME FAILED)"
108exit $fail