my-homelab-configs/docs/ai-lab.md

202 lines
11 KiB
Markdown

# Jeannie AI lab
The AI lab builds a pinned, separate llama.cpp checkout on the Debian server.
It does not change Ollama, OmniRoute, the Jeannie model routes, or cluster
configuration. The build has CPU and Vulkan support; Vulkan detection does not
guarantee that the integrated GPU will be faster than the CPU.
After pushing this change, on Debian:
```sh
cd ~/my-homelab-configs
git pull --ff-only gitea main
./jeannie ai-lab plan
./jeannie ai-lab install
./jeannie ai-lab devices
```
Installation uses `sudo apt-get` for build and Vulkan packages, downloads the
pinned source, and compiles with two parallel jobs. The default lab directory is
`~/.local/share/homelab/ai-lab` (or `ai-lab` under `HOMELAB_STATE_DIR`); set
`JEANNIE_AI_LAB_DIR` to a writable directory to choose another location. A
previous `/data/homelab-ai-lab` workspace is not moved or deleted. If explicitly
selected, that directory must already be writable; Jeannie does not change its
ownership or permissions.
The install refuses to overwrite an existing source checkout whose origin,
commit, or tracked files do not match the expected source.
Once `ollama ps` shows no loaded models, run:
```sh
./jeannie ai-lab bench ollama
./jeannie ai-lab bench cpu
./jeannie ai-lab bench vulkan
```
The Vulkan run defaults to `Vulkan0`. If `devices` shows a different Radeon
device, set `JEANNIE_AI_LAB_VULKAN_DEVICE` to that name for the run. The lab
refuses to start if Ollama currently has a loaded model; it never stops another
model. The Ollama benchmark asks Ollama to unload the model after its own
request. Avoid concurrent AI workloads when comparing results.
Each run reads the existing `llama3.2:3b` GGUF blob; it does not copy, delete,
or modify the model. The Debian user must be able to read the path printed by
`ollama show --modelfile llama3.2:3b`. Inaccessible or non-GGUF files stop the
test. Reports and model output are saved with mode 0600 under the lab's `reports`
directory. The report labels llama.cpp's prompt and generation speeds when its
timing summary is present; a null value means timing was not recognized.
The lab captures the process's controlling terminal so its output is included
in the private log. Review the log to confirm actual GPU offload.
Each benchmark is one cold request, using the same prompt, maximum output,
context size and temperature. CLI and Ollama prompt templates, sampling options,
backend allocation and measured time boundaries can differ, so this is an
initial comparison rather than a controlled scientific benchmark. Repeat runs,
inspect answers, and compare end-to-end user experience before changing routes.
## Colibri experiment
The optional Colibri engine is pinned separately and runs on CPU. Its Qwen3
engine reads a small Qwen3-0.6B Q8_0 GGUF model. Preparation downloads about
639 MB from Qwen's Hugging Face repository and verifies its pinned SHA-256;
interrupted downloads are cleaned up and can be retried. Existing model files
with a different checksum are preserved and refused. No production model or
service is changed.
```sh
./jeannie ai-lab colibri-install
./jeannie ai-lab colibri-prepare
./jeannie ai-lab bench colibri
./jeannie ai-lab bench qwen-cpu
```
The last two commands use the exact same GGUF with Colibri and llama.cpp. The
llama.cpp build must be installed for `qwen-cpu`. Colibri checks for at least
2 GiB currently available before starting, but other workloads may use memory
while the benchmark runs. The engines may use different prompts, sampling,
context handling and timing boundaries; compare both the logs and answer
quality rather than treating the elapsed times as a conclusive ranking. These
are single-request experiments; neither backend is registered with OmniRoute.
Both Qwen runs request greedy sampling and disable thinking, so the limited
output budget is available for the answer. Check the logs to confirm that each
engine honored those settings and produced a complete answer.
The Colibri report extracts weight load time, RSS after loading weights, and
lookup decoder tokens, forwards and draft acceptance when those log lines are
present. The lookup token count describes tokens fed to the decoder, not
necessarily the visible output count. Its derived tokens/second includes the
whole command startup and is not directly comparable to llama.cpp's
generation-only rate. RSS after model loading is not peak process memory.
Fields are null when the log does not contain the corresponding measurement.
## Doctor model comparison
On the Debian server, install the existing lab engines first. The doctor model
preparation downloads an approximately 18 GB GGUF to the AI lab workspace and
verifies its SHA-256 before importing it for Colibri:
```sh
./jeannie ai-lab install
./jeannie ai-lab colibri-install
./jeannie ai-lab doctor-prepare
./jeannie ai-lab doctor-suite
```
The suite runs ten synthetic diagnostic cases twice per backend, including
contrasting known and unknown intent for the runner, stopped services, and a
maintenance window. Sandbox intent is tested as unknown. Pass a repeat
count from 1 to 5 to `doctor-suite` to change that. It tests the existing
Ollama models `qwen2.5:0.5b`, `llama3.2:3b`, `qwen2.5-coder:7b`, and
`qwen2.5:14b` sequentially on CPU. Missing models are reported as skipped;
models whose GGUF weights alone reach the 8 GiB limit are also skipped. The
installed 14B model is approximately 9 GB, so it is expected to be skipped.
It also tests the 7B GGUF using llama.cpp and Qwen3-Coder-30B-A3B Q4_K_M
using Colibri, both on CPU. The 30B model is the largest
candidate in Colibri's pinned catalog with an 8 GB minimum, not a guarantee
that this particular GGUF and context will fit. Keep at least 22 GiB free in
the lab workspace for the download and at least 9 GiB of host MemAvailable
before each backend starts; it waits up to 60 seconds for memory to recover
between runs. The suite requires a functioning systemd user
manager and cgroup v2 memory controller. It refuses to load a model unless an
8 GiB memory limit and zero swap are verified; a low-memory watchdog stops its
own scope. This limit can terminate a model and cannot eliminate host pressure.
The report includes each response, citation and command validation, timing,
and available memory measurements. It and server logs are private files under
the lab reports directory. A valid response is not proof of factual accuracy.
The lab accepts one complete outer
JSON code fence before applying the same validator and preserves raw answers;
it does not change production Doctor parsing. Different model sizes and output
constraints also mean Colibri quality results do not isolate engine performance.
The suite also sends the same **synthetic cases** to the smart direct model in
`models.env` through the loopback OmniRoute gateway. If no direct model is
recorded, it uses the smart route alias. Override with
`LAB_DOCTOR_LAB_OMNI_MODEL` or `LAB_DOCTOR_LAB_OMNI_URL` (loopback `/v1` only).
OmniRoute can fall back or change providers; the report records the requested
and returned model names where available, which are not proof of backend identity.
OmniRoute requests allow up to 4096 output tokens including reasoning. An empty
or malformed JSON response gets one fresh retry with an explicit JSON-only
instruction. Both responses, usage, finish reasons and elapsed times are kept in
the private report; total latency includes both attempts. This additional retry
and larger output budget differ from local models and affect direct comparisons.
The final answer must still pass the same evidence and command validation.
This can use an external provider and its quotas; no live Doctor evidence is sent.
External inference has no local scope memory measurement or GPU equivalence.
This experiment does not change the production doctor model or stop services.
Ollama must be idle before the test; stop or unload models deliberately first.
Each completed suite automatically scores valid responses using a separate,
memory-limited instance of `qwen2.5-coder:7b` as an LLM judge, using the case
evidence and rubric. It prints the ranking and saves the scores. The judge is
also a candidate, so it can favor itself or similar models; scores are estimates,
not verified accuracy. Contract failures score zero in otherwise usable runs;
backends with no valid responses remain unranked. If the judge fails, the raw
report remains intact. To automatically score an existing report, run:
```sh
./jeannie ai-lab doctor-auto-rank /path/to/report.json
```
The ranking averages accuracy (0-2), uncertainty (0-2) and next step (0-1)
over all cases and repeats. Skipped, incomplete, unscored and wholly invalid
backends remain unranked. Equal scores tie; latency and memory are separate and
do not change quality rank. The shuffled `blind-review.json` remains available
for optional human scoring with `doctor-rank`; it is not required. Neither
scoring method establishes a statistical accuracy guarantee.
## Repeated comparison suite
After the llama.cpp and Colibri installation and model preparation above:
```sh
./jeannie ai-lab suite
```
The default is three repetitions of six prompts per available backend; pass
an integer from 1 to 10 to change it, for example `./jeannie ai-lab suite 5`.
It rotates backend order and runs Ollama, llama.cpp CPU, llama.cpp Vulkan when
the selected Vulkan device is detected, and Colibri CPU. Unavailable engines
are listed as skipped. Each run uses the same verified Qwen3 0.6B Q8 GGUF,
4096-token context and 128-token output limit, requesting no thinking and
temperature zero. Ollama chooses its own CPU/GPU placement; Vulkan layer
offload is only confirmed when its log reports it. No backend is selected as a
winner automatically.
The first run registers a persistent Ollama model named
`jeannie-ai-lab-qwen3-q8:latest` from the verified local GGUF. A pre-existing
alias pointing elsewhere is never replaced. This does not change production
routes or models. The suite checks Ollama is idle rather than stopping loaded
models, requests unload after its own Ollama inference, and requires at least
2 GiB available RAM before each run. Other host workloads can still start
concurrently and affect or interrupt a benchmark.
The summary includes median end-to-end time per backend and per prompt, simple
exact-match checks for arithmetic, JSON, extraction and a recovery signal, plus
manual-review Kubernetes and untrusted-log prompts. Those checks are examples, not a comprehensive
measurement of model quality. The process or server request starts cold, but
filesystem and accelerator caches may be warm; startup boundaries and chat
templates can vary. The report includes each run, errors, skipped engines,
available throughput metrics and private per-run logs in a new mode-0700 report
directory. A nonzero exit means at least one attempted run failed; inspect
`report.json` and the corresponding log before drawing conclusions.