Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

3.4 KB
Raw Blame
#!/bin/bash
# Starts one OpenAI-compatible inference server per GPU (0..3) on 127.0.0.1:BASE_PORT+gpu
# for the pi.dev swarm and egress arenas. Any server that accepts llama-server style
# flags works; extra flags go in SERVER_EXTRA_ARGS.
set -euo pipefail
cd "$(dirname "$0")/.."

STATE_DIR="$PWD/runs/.swarm-models"
mkdir -p "$STATE_DIR"

SERVER_BIN="${SERVER_BIN:-llama-server}"
MODEL_PATH="${MODEL_PATH:?set MODEL_PATH to the GGUF model file}"
MODEL_ALIAS="${SWARM_MODEL_ALIAS:-inkling-2-mini-iq3s}"
MAX_CONTEXT="${SWARM_MAX_CONTEXT:-131072}"
BASE_PORT="${SWARM_BASE_PORT:-18100}"
READY_TIMEOUT="${SWARM_READY_TIMEOUT:-180}"
GPUS="${SWARM_GPUS:-0 1 2 3}"
read -r -a EXTRA_ARGS <<< "${SERVER_EXTRA_ARGS:-}"

command -v "$SERVER_BIN" >/dev/null 2>&1 || { echo "Missing server binary: $SERVER_BIN (set SERVER_BIN)" >&2; exit 1; }
[[ -f "$MODEL_PATH" ]] || { echo "Missing model file: $MODEL_PATH" >&2; exit 1; }

port_ready() {
    local port="$1"
    curl -sf --max-time 2 "http://127.0.0.1:${port}/v1/models" | grep -q "\"${MODEL_ALIAS}\""
}

for gpu in $GPUS; do
    port=$((BASE_PORT + gpu))
    pid_file="$STATE_DIR/gpu${gpu}.pid"
    srv_log="$STATE_DIR/gpu${gpu}.server.log"

    if [[ -f "$pid_file" ]]; then
        old_pid=$(cat "$pid_file" 2>/dev/null || true)
        if [[ -n "$old_pid" ]] && kill -0 "$old_pid" 2>/dev/null && port_ready "$port"; then
            echo "[gpu${gpu}] already running on 127.0.0.1:${port} (pid=${old_pid}, model=${MODEL_ALIAS})"
            continue
        fi
        rm -f "$pid_file"
    fi

    if ss -tln | grep -qE "127\.0\.0\.1:${port}\b|:${port}\b"; then
        echo "Port ${port} is already in use by another process; refusing to overwrite." >&2
        exit 1
    fi

    (
        exec setsid env \
            CUDA_DEVICE_ORDER=PCI_BUS_ID \
            CUDA_VISIBLE_DEVICES="${gpu}" \
            "$SERVER_BIN" \
                --model "$MODEL_PATH" \
                --alias "$MODEL_ALIAS" \
                --ctx-size "$MAX_CONTEXT" \
                --n-gpu-layers 999 \
                --host 127.0.0.1 \
                --port "$port" \
                "${EXTRA_ARGS[@]}" \
                >"$srv_log" 2>&1 </dev/null
    ) &
    pid=$!
    printf '%s\n' "$pid" > "$pid_file"
    echo "[gpu${gpu}] starting on 127.0.0.1:${port} (pid=${pid}, ctx=${MAX_CONTEXT})"
done

deadline=$((SECONDS + READY_TIMEOUT))
for gpu in $GPUS; do
    port=$((BASE_PORT + gpu))
    pid=$(cat "$STATE_DIR/gpu${gpu}.pid")
    while true; do
        if port_ready "$port"; then
            break
        fi
        if ! kill -0 "$pid" 2>/dev/null; then
            echo "[gpu${gpu}] server exited unexpectedly. Check $STATE_DIR/gpu${gpu}.server.log" >&2
            tail -n 30 "$STATE_DIR/gpu${gpu}.server.log" 2>/dev/null >&2 || true
            exit 1
        fi
        if (( SECONDS >= deadline )); then
            echo "[gpu${gpu}] timed out waiting for http://127.0.0.1:${port}/v1/models" >&2
            exit 1
        fi
        sleep 1
    done
    echo "[gpu${gpu}] READY http://127.0.0.1:${port}/v1/models -> ${MODEL_ALIAS}"
done

echo ""
echo "=== Swarm Inference Servers Ready ==="
echo "Server:    ${SERVER_BIN}"
echo "Model:     ${MODEL_PATH}"
echo "Context:   ${MAX_CONTEXT}"
echo "Alias:     ${MODEL_ALIAS}"
echo "Endpoints: http://127.0.0.1:${BASE_PORT}..$((BASE_PORT + 3))/v1"
echo ""
nvidia-smi --query-gpu=index,name,memory.used,memory.total,utilization.gpu --format=csv || true