Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Docker build (#96): the engine compiled in docker build, STRATA_EXECV so the server is PID 1, SIGTERM stops it cleanly, KV/GPUS/LOW_RAM as env vars
Stratab782066e3cc3
diff --git a/.dockerignore b/.dockerignorenew file mode 100644index 000000000..4f341af33--- /dev/null+++ b/.dockerignore@@ -0,0 +1,27 @@+# Keep the image to the source only: everything generated is re-created on+# first start (inside the container or on the /data volume).+.git++# local Python environment (rebuilt in the image)+.venv/+__pycache__/+*.pyc+.pytest_cache/++# setup.py outputs and downloads (engine, packs, models, logs)+/engine/+/build/+/build*/+/dist/+/packs/+/mtp/+/models/+/Strata-data/+third_party/llama.cpp/+third_party/*.zip+*.done+*.log+strata-*.json+strata-*.txt+run-*.sh+run-*.batdiff --git a/Dockerfile b/Dockerfilenew file mode 100644index 000000000..b1b796176--- /dev/null+++ b/Dockerfile@@ -0,0 +1,116 @@+# syntax=docker/dockerfile:1+#+# Strata: Qwen3.8-Flash-Next on NVIDIA GPUs (RTX 30/40/50, 12+ GB VRAM; two or+# three cards can share one model, 8 GB each - docs/MULTI_GPU.md).+#+# The engine is compiled during docker build, so the first container start only+# downloads the model (~70 GB) and starts the server. docker build has no GPU,+# so the CUDA architectures are fixed here instead of read from nvidia-smi: the+# engine is a fat binary with a cubin per listed arch, and the runtime picks the+# one matching your card. Narrow CUDA_ARCHITECTURES to your card for a faster+# build; a card outside the set needs a rebuild with its own arch.+#+# Build:+# docker build -t strata .+# docker build -t strata --build-arg CUDA_ARCHITECTURES=89 . # RTX 40 only+#+# Run (host needs an NVIDIA driver >= 580 and nvidia-container-toolkit):+# docker run --rm --gpus all \+# -p 8080:8080 \+# --ulimit memlock=-1 \+# -v strata-data:/data \+# -e MODEL=IQ2_XS \+# strata+#+# Setup choices are env vars, read by docker-entrypoint.sh: FAMILY, MODEL, CONTEXT,+# VISION (no | yes | cpu), KV (int8 | q4_0 | k8v4), GPU (one card) or GPUS ("0,2"+# or "all", with LAYER_SPLIT), LOW_RAM (auto | on | off), HOST, PORT, API_KEY.+#+# Only the model files, the prepared pack, the MTP layer and the install config+# live in the /data volume; the engine is part of the image. Strata loads 32-62 GB+# into RAM, so a capped container needs -e LOW_RAM=on: setup.py reads the RAM from+# /proc/meminfo, which here is the host's total, not the container's limit. Add an+# API key before exposing the port to a network: -e API_KEY=<secret>. Pass+# -e REINSTALL=1 to change the model settings later.+#+# --gpus all on a host with two usable cards: setup takes both (the layer split is+# its recommended default). Pin one card with -e GPU=0, or name them with+# -e GPUS=0,2. A volume set up for one card switches to the pair on its first start+# on a two-card host unless GPU or GPUS pins it. LOW_RAM=on runs on one card.++FROM nvidia/cuda:13.0.0-devel-ubuntu24.04++# STRATA_EXECV=1: setup.py replaces itself with the server, so the server is PID 1+# and docker stop's SIGTERM reaches it (see setup.start). Normal Linux starts, which+# don't set it, keep spawning the server as a child.+ENV DEBIAN_FRONTEND=noninteractive PYTHONUNBUFFERED=1 LANG=C.UTF-8 STRATA_EXECV=1++RUN apt-get update && apt-get install -y --no-install-recommends \+ build-essential ca-certificates curl git libatomic1 libgomp1 \+ python3 python3-pip python3-venv unzip \+ && rm -rf /var/lib/apt/lists/*++WORKDIR /opt/strata+COPY . .++# RTX 20 (75), RTX 30 (86), RTX 40 (89), RTX 50 (120), plus 80 for A-series. CMakeLists+# refuses anything below 75. BUILD_VISION=0 skips the image encoder build.+ARG CUDA_ARCHITECTURES=75;80;86;89;120+ARG BUILD_VISION=1++RUN python3 -m venv .venv \+ && .venv/bin/pip install --no-cache-dir --upgrade pip \+ && .venv/bin/pip install --no-cache-dir \+ numpy jinja2 regex pyyaml tqdm requests cmake ninja pillow psutil \+ && chmod +x setup.sh docker-entrypoint.sh++# llama.cpp at the pinned commit, then the engine and the image encoder, built+# exactly the way setup.py builds them. BUILD.json is what setup.py reads to+# decide whether an engine is current: source=local with a matching src hash+# means the first start reuses it instead of recompiling.+RUN .venv/bin/python - <<'PYEOF'+import json, os, pathlib, shutil+import setup++llama = setup.get_llama_cpp()+nvcc, _ = setup.find_nvcc()+arch = os.environ.get("CUDA_ARCHITECTURES", "75;80;86;89;120").strip().strip('"').replace(",", ";")+vision = "gpu" if os.environ.get("BUILD_VISION", "1") == "1" else "none"++setup.cmake_build(setup.ROOT, setup.ROOT / "build", "strata",+ ["-DSTRATA_ENABLE_CUDA=ON", "-DSTRATA_BUILD_TESTS=OFF",+ f"-DCMAKE_CUDA_ARCHITECTURES={arch}", f"-DCMAKE_CUDA_COMPILER={nvcc}",+ f"-DSTRATA_GGML_DIR={llama}"], None, "build-strata.bat")+if vision != "none":+ setup.cmake_build(setup.ROOT / "tools" / "vision", setup.ROOT / "build-vision", "strata-vision",+ [f"-DLLAMA_DIR={llama}", "-DSTRATA_VISION_CUDA=ON",+ f"-DCMAKE_CUDA_ARCHITECTURES={arch}", f"-DCMAKE_CUDA_COMPILER={nvcc}"], None, "build-vision.bat")++eng = setup.ROOT / "engine"+eng.mkdir(exist_ok=True)+shutil.copy2(setup.ROOT / "build" / setup.EXE, eng / setup.EXE)+if vision != "none":+ shutil.copy2(setup.ROOT / "build-vision" / "bin" / setup.VEXE, eng / setup.VEXE)+bindir = pathlib.Path(nvcc).parent+meta = {"source": "local", "version": setup.source_version(),+ "archs": [int(a.split("-")[0]) for a in arch.split(";") if a.split("-")[0].isdigit()], "vision": vision,+ "cuda_dirs": [str(d) for d in (bindir, bindir / "x64", bindir.parent / "lib64") if d.is_dir()],+ "src": setup.source_hash(setup.ENGINE_SOURCES),+ "vision_src": setup.source_hash(setup.VISION_SOURCES) if vision != "none" else None}+(eng / "BUILD.json").write_text(json.dumps(meta, indent=1))+PYEOF++# the cmake trees are build-time only; the engine itself is what the container needs+RUN rm -rf build build-vision++VOLUME ["/data"]+EXPOSE 8080++# /health is answered before the API key gate, so it works with or without one.+# The port only opens after the model loads (1-3 minutes, longer on a first run),+# so the start period is generous: a too short one marks a still-loading container+# unhealthy and a restart policy would kill it mid-download.+HEALTHCHECK --interval=30s --timeout=5s --start-period=600s --retries=3 \+ CMD curl -fs "http://127.0.0.1:${PORT:-8080}/health" || exit 1++ENTRYPOINT ["./docker-entrypoint.sh"]diff --git a/README.md b/README.mdindex 2df0838cb..3863e63fb 100644--- a/README.md+++ b/README.md@@ -122,6 +122,38 @@ the same way - nothing big is downloaded again.**Linux:** run `./setup.sh` - same questions, same result.+**Docker (Linux):** the same idea, in a container.++1. Host: Docker with the [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)+ and a driver **580 or newer** (CUDA 13.0).+2. Build (this compiles the engine into the image, so the container never compiles):+ `docker build -t strata .`+ `docker build -t strata --build-arg CUDA_ARCHITECTURES=89 .` builds for one card only (faster).+ The default covers RTX 30 (86), RTX 40 (89), RTX 50 (120) and A-series (80); a card outside that+ set needs a rebuild with its own arch. Add `--build-arg BUILD_VISION=0` to skip the image encoder.+3. Run (the first start downloads the ~70 GB model, then starts; later starts go straight to serving):+ `docker run --rm --gpus all -p 8080:8080 --ulimit memlock=-1 -v strata-data:/data strata`++ The setup choices are env vars: `-e MODEL=IQ2_XS -e FAMILY=qwen -e CONTEXT=32768 -e VISION=no`+ (or `MODEL=Q2_0|IQ3_XXS|IQ3_S`, `FAMILY=swift|coder`; the defaults above are the recommended ones).+ `-e VISION=cpu` keeps the image encoder on the CPU. `-e KV=int8|q4_0|k8v4` picks the KV cache+ precision; `k8v4` is INT8 K with 4-bit V and keeps its KV in VRAM from 64K up.+ Only the model files, the prepared pack, the MTP layer and the install config live in the+ `strata-data` volume; the engine is part of the image. Switching between models already on the+ volume needs no setup pass: `-e MODEL=Q2_0 -e FAMILY=coder` picks that model's config. Add+ `-e REINSTALL=1` only to change settings for a model already set up (context, vision, KV, host,+ api_key, LOW_RAM), since those are recorded in its config.+ Strata loads 32-62 GB into RAM. `--gpus all` on a host with two usable cards takes both: the+ layer split is setup's recommended default ([docs/MULTI_GPU.md](docs/MULTI_GPU.md)), and a volume+ set up for one card switches to the pair on its first start there. Pin one card with `-e GPU=0`,+ or name them with `-e GPUS=0,2` and where the later card's layers start with `-e LAYER_SPLIT=18`.+ A memory limit needs `-e LOW_RAM=on`, which maps the model's experts from the pack instead of+ keeping them in RAM: setup.py measures the host's RAM, not the container's limit, so it cannot+ see a cap. LOW_RAM runs on one card.+ The server listens on `0.0.0.0:8080` by default; set `-e API_KEY=<secret>` before exposing the port+ to a network. The image has a `HEALTHCHECK` on `/health`, so `docker ps` shows the container+ healthy once the model is loaded, and `GET /v1/status` says what it is running.+## Using it<p align="center"><img src="docs/media/runpagoda.png" width="900" alt="The Strata app's Monitor tab next to a coding agent"><br>diff --git a/docker-entrypoint.sh b/docker-entrypoint.shnew file mode 100755index 000000000..f167bed0b--- /dev/null+++ b/docker-entrypoint.sh@@ -0,0 +1,65 @@+#!/bin/sh+# Entry point for the Strata container. The engine is compiled during docker+# build and lives in the image, so the first start only downloads the model.+# The install config is kept on the /data volume so a recreated container skips+# the setup pass and goes straight to serving.+set -e+cd /opt/strata || exit 1++STRATA_DATA="${STRATA_DATA:-/data}"+FAMILY="${FAMILY:-qwen}"+MODEL="${MODEL:-IQ2_XS}"+CONTEXT="${CONTEXT:-32768}"+VISION="${VISION:-no}" # no | yes | cpu (the image encoder on the CPU)+HOST="${HOST:-0.0.0.0}"+PORT="${PORT:-8080}"+API_KEY="${API_KEY:-}"+KV="${KV:-}" # int8 | q4_0 | k8v4; empty: setup.py's own default (int8)+GPUS="${GPUS:-}" # "0,2" or "all": one model across several cards (docs/MULTI_GPU.md)+GPU="${GPU:-}" # one card, numbered as nvidia-smi numbers them+LAYER_SPLIT="${LAYER_SPLIT:-}" # with GPUS: where each later card's layers start (default: auto)+LOW_RAM="${LOW_RAM:-auto}" # on: the experts come from the pack's experts.bin, not from RAM++# setup.py starts the newest strata-*.json it finds, so link in exactly the one+# this family and model were set up with. The config is the recorded output of+# that setup (the pack, the profile, the quant, the KV decision), not settings+# the entry point could rebuild from env vars. qwen has an empty family tag.+case "$FAMILY" in qwen) prefix="" ;; *) prefix="${FAMILY}-" ;; esac+tag="${prefix}$(printf '%s' "$MODEL" | tr 'A-Z' 'a-z')"+cfg="$STRATA_DATA/config/strata-$tag.json"+mkdir -p "$STRATA_DATA/config"++# REINSTALL is only needed to change settings for a model that is already set up+# (context, vision, KV, host, api_key). Switching between models already on the+# volume needs no setup pass: their config is already there.+#+# KV / GPUS / GPU / LAYER_SPLIT are passed only when set, so an unset one keeps+# setup.py's own default. LOW_RAM is always passed: setup.py measures the PC's RAM+# from /proc/meminfo, which in a container is the host's total, not the container's+# limit, so a memory-capped container has to ask for the low-RAM mode itself.+if [ "${REINSTALL:-0}" = "1" ] || [ ! -f "$cfg" ]; then+ echo "Setting up $tag: downloading the model (~70 GB; the engine is already in the image)."+ set -- --family "$FAMILY" --model "$MODEL" --context "$CONTEXT" --vision "$VISION" \+ --data-dir "$STRATA_DATA" --host "$HOST" --api-key "$API_KEY" \+ --port "$PORT" --no-start --low-ram "$LOW_RAM"+ if [ -n "$KV" ]; then set -- "$@" --kv "$KV"; fi+ if [ -n "$GPUS" ]; then set -- "$@" --gpus "$GPUS"; fi+ if [ -n "$GPU" ]; then set -- "$@" --gpu "$GPU"; fi+ if [ -n "$LAYER_SPLIT" ]; then set -- "$@" --layer-split "$LAYER_SPLIT"; fi+ .venv/bin/python setup.py --setup --yes "$@"+ [ -e "/opt/strata/strata-$tag.json" ] && { cmp -s "/opt/strata/strata-$tag.json" "$cfg" || cp -f "/opt/strata/strata-$tag.json" "$cfg"; }+else+ [ -e "/opt/strata/strata-$tag.json" ] || ln -s "$cfg" "/opt/strata/strata-$tag.json"+fi++# Later starts skip straight here: setup.py finds the installed config and+# launches serve/server.py (OpenAI- and Anthropic-compatible API on :8080).+# GPUS / GPU / LAYER_SPLIT are repeated on purpose. Given at the start they pin the+# cards for this model, and setup.py saves them in its config; without them a config+# that names one card is offered once to a pair, on its own, when the host has two+# cards that can share the model (setup.py's offer_together, docs/MULTI_GPU.md).+set -- --port "$PORT"+if [ -n "$GPUS" ]; then set -- "$@" --gpus "$GPUS"; fi+if [ -n "$GPU" ]; then set -- "$@" --gpu "$GPU"; fi+if [ -n "$LAYER_SPLIT" ]; then set -- "$@" --layer-split "$LAYER_SPLIT"; fi+exec .venv/bin/python setup.py "$@"diff --git a/serve/server.py b/serve/server.pyindex 2d41ac439..e03379e08 100644--- a/serve/server.py+++ b/serve/server.py@@ -30,6 +30,7 @@ import ctypesimport jsonimport osimport queue+import signalimport subprocessimport sysimport tempfile@@ -1985,6 +1986,15 @@ def main() -> int:if a.open:import webbrowserwebbrowser.open(f"http://{'127.0.0.1' if a.host in ('0.0.0.0', '') else a.host}:{a.port}/")+ # #96: docker stop sends SIGTERM, which Python ignores by default, so the container's PID 1 would be killed after+ # the grace period with the engine still running. SIGTERM takes Ctrl+C's path below (QUIT to the engine).+ # SIGINT keeps Python's own handler, so Ctrl+C and a second Ctrl+C work as before.+ def on_sigterm(signum, frame):+ raise KeyboardInterrupt+ try:+ signal.signal(signal.SIGTERM, on_sigterm)+ except (ValueError, OSError, AttributeError): # not the main thread+ passtry:while True:time.sleep(1) # Windows never delivers Ctrl+C to an untimed Event.wait()diff --git a/setup.py b/setup.pyindex e05cda39a..daecede8c 100644--- a/setup.py+++ b/setup.py@@ -1517,6 +1517,11 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_say(" restart). That is normal: please wait and don't close this window - the browser opens when it is ready.")say(" Later, closing this window stops the model.")say(" " + "-" * 100)+ if not WIN and os.environ.get("STRATA_EXECV"):+ # Replace this process instead of spawning a child. The Docker image sets STRATA_EXECV=1,+ # so there the server is PID 1 and docker stop's SIGTERM reaches the process that can+ # answer the engine with QUIT. Normal Linux starts keep spawning the server as a child.+ os.execv(cmd[0], cmd)return subprocess.call(cmd)