Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

serve --multi: reuse a shared system prompt from disk; survive a cut-off state file (#17)

0xarone7f78c80c9ab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e67fbcb..ecc5398 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@ All notable changes to openset-llm. Dates are YYYY-MM-DD.
## Unreleased
- `bench/quick.sh`: a 3-minute speed check that prints a block to paste into a "Measurement on my machine" issue.
+- `docker-compose.yml`: `docker compose up` builds the image, keeps the model in a named volume and waits on `/health`.
+- `./serve --multi N`: a system prompt the streams share is read once and loaded from disk afterwards (the prefix part
+ of agent-loop resume; it was off with `--multi`). Same replies as `--no-resume`, no extra memory. `--multi` now
+ writes prefix states to `~/.openset-llm/state` like the single-stream server does.
+- A prefix state file that was cut short on disk (for example by a full disk) made the server abort when a request
+ tried to load it. Such a file is now detected, removed and written again.
## 0.1.0 (2026-11-25)
diff --git a/README.md b/README.md
index 5974f07..262d8b5 100644
--- a/README.md
+++ b/README.md
@@ -307,7 +307,8 @@ trained on the full-precision model improves this ternary build on any task.
## Several requests at once (`--multi`)
`./serve --multi N` serves N requests together: every pass over the ~6 GB of weights produces one token for each of
-them. Speculation and agent-loop resume are off in this mode (speculation with it is untested); `--ctx` is per stream.
+them. Speculation is off in this mode; `--ctx` is per stream. A system prompt (and tool list) that the streams share is
+read once, kept on disk, and loaded by every stream that needs it.
| 8 vCPU / 8 GB, Ice Lake Xeon (2026-11-11) | one stream (`--no-spec`) | `--multi 4`, four short prompts together |
|---|---|---|
@@ -319,6 +320,16 @@ them. Speculation and agent-loop resume are off in this mode (speculation with i
- With ~800-token prompts the total falls to 7–10 tok/s: a newcomer's prompt is read on its own, and the other streams
pause for those batches (that keeps outputs identical).
- 8 streams leave < 0.3 GB free on an 8 GB machine, so `serve` warns above 6.
+- **Agents that share a system prompt** (measured 2026-11-25 on 8 vCPU / 8 GB Ice Lake boxes; every agent sends a
+ 1.5k–3.9k-token prompt whose ending changes each step, 1.2k–1.8k tokens of it a shared system prompt + tools): the
+ server reads 32 % (4 streams) or 46 % (2 streams) of the prompt tokens instead of all of them, and a step of all
+ agents takes 58–71 s instead of 157–177 s with 4 streams (2.5–2.8×), 85 s instead of 168–170 s with 2 (2.0×).
+ It costs no memory, and the replies are byte-identical to `--no-resume` and to each request alone (every request
+ compared). The gain is the shared part: a stream still reads everything after the system prompt again.
+- The single-stream server's exact resume point (one checkpoint per stream, ~150 MiB each) is off with `--multi`; it
+ can be tried with `./serve --multi N -- --ctx-checkpoints 1` (experimental; 4 streams with it leave < 0.1 GB free on 8 GB).
+- Speculation inside `--multi` is not offered: a first look with 2 streams on 8 GB ran at 0.4–0.7× the speed of
+ `--multi 2` without it, with replies that no longer matched the request alone; with 4 streams it ran out of memory.
- One user wanting the fastest single reply: plain `./serve`. Several users, agents or best-of-N
sampling: `--multi`.
@@ -394,7 +405,7 @@ curl http://127.0.0.1:8080/v1/chat/completions -H 'Content-Type: application/jso
(summaries, rewrites, code edits) and less on short open questions; the numbers above are for the benchmark prompts.
- **Resume keeps prompt states on disk.** To continue agent loops quickly, the server saves up to 8 prefix states
(about 0.3 GB each, built from the start of your prompts) in `~/.openset-llm/state`. Delete that folder to clear
- them, or run `./serve --no-resume`.
+ them, or run `./serve --no-resume`. `--multi` uses the same folder (its states are separate from single-stream ones).
- **The model is PrismML's ternary Bonsai-2-27B**, with thinking off by default (`./serve --think`). The server
repacks the weights every time it starts, before it accepts requests (20–45 s on the box we measured).
diff --git a/patches/0010-multi-resume.patch b/patches/0010-multi-resume.patch
new file mode 100644
index 0000000..a88fe1b
--- /dev/null
+++ b/patches/0010-multi-resume.patch
@@ -0,0 +1,133 @@
+diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
+index 3ac30a7..b81ed57 100644
+--- a/tools/server/server-context.cpp
++++ b/tools/server/server-context.cpp
+@@ -92,6 +92,12 @@ static bool rsm_on() {
+ }
+ return rsm.on == 1;
+ }
++// 0010: LLAMA_RESUME_PREFIX_ONLY=1 keeps part (c) alive with --ctx-checkpoints 0 (serve --multi): the prefix state on
++// disk is written once and loaded whenever a request's cache cannot be continued; no checkpoint is kept in RAM
++static bool rsm_prefix_only() {
++ static const bool v = [] { const char * e = getenv("LLAMA_RESUME_PREFIX_ONLY"); return e && atoi(e) != 0; }();
++ return v && rsm_on() && !rsm.dir.empty();
++}
+ // resume rate of a kind: observed (hits, tries) plus a prior worth 2 tries
+ static double rsm_rate(int k) {
+ static const double prior[RSM_N_KINDS] = { 0.2, 0.5, 0.3, 0.5, 0.9 };
+@@ -178,6 +184,31 @@ static void rsm_prune() {
+ for (const char * ext : { ".tgt", ".dft", ".spec" }) { p.replace_extension(ext); std::filesystem::remove(p, ec); }
+ }
+ }
++// 0010: a state file is sealed with a 16-byte trailer (magic + the length of what precedes it) once it is complete on
++// disk. The loader ignores bytes after the state. A file without a matching trailer (cut short: a full disk, a copy
++// that broke off) is never handed to the loader, which aborts the server on it.
++static const char RSM_SEAL[8] = { 'R', 'S', 'M', 'S', 'E', 'A', 'L', '1' };
++static bool rsm_seal(const std::string & path, uint64_t n) {
++ std::error_code ec;
++ if (n == 0 || std::filesystem::file_size(path, ec) != n || ec) { return false; }
++ std::ofstream f(path, std::ios::binary | std::ios::app);
++ f.write(RSM_SEAL, sizeof(RSM_SEAL));
++ f.write((const char *) &n, sizeof(n));
++ f.flush();
++ return (bool) f;
++}
++static bool rsm_sealed(const std::string & path) {
++ std::error_code ec;
++ const uint64_t sz = std::filesystem::file_size(path, ec);
++ if (ec || sz < 16) { return false; }
++ std::ifstream f(path, std::ios::binary);
++ f.seekg((std::streamoff) (sz - 16));
++ char m[sizeof(RSM_SEAL)];
++ uint64_t n = 0;
++ f.read(m, sizeof(m));
++ f.read((char *) &n, sizeof(n));
++ return f && std::equal(m, m + sizeof(m), RSM_SEAL) && n == sz - 16;
++}
+ static bool rsm_read_bytes(const std::string & path, std::vector<uint8_t> & out) {
+ std::ifstream f(path, std::ios::binary);
+ if (!f) { return false; }
+@@ -2669,7 +2700,8 @@ private:
+ slot.mem.seq_rm(slot.id, -1, -1);
+ llama_tokens got(p);
+ size_t n_got = 0;
+- bool ok = llama_state_seq_load_file(ctx_tgt, ft.c_str(), slot.id, got.data(), got.size(), &n_got) > 0 && n_got == (size_t) p && got == pre;
++ bool ok = rsm_sealed(ft) && (!ctx_dft || rsm_sealed(rsm_path(h, "dft"))); // 0010: complete files only
++ ok = ok && llama_state_seq_load_file(ctx_tgt, ft.c_str(), slot.id, got.data(), got.size(), &n_got) > 0 && n_got == (size_t) p && got == pre;
+ if (ok && ctx_dft) {
+ size_t n_d = 0;
+ ok = llama_state_seq_load_file(ctx_dft, rsm_path(h, "dft").c_str(), slot.id, got.data(), got.size(), &n_d) > 0 && n_d == (size_t) p;
+@@ -2711,18 +2743,22 @@ private:
+ std::error_code ec;
+ const std::string ft = rsm_path(h, "tgt");
+ const std::string tmp_t = ft + ".tmp";
+- bytes += llama_state_seq_save_file(ctx_tgt, tmp_t.c_str(), slot.id, pre.data(), pre.size());
++ const size_t n_t = llama_state_seq_save_file(ctx_tgt, tmp_t.c_str(), slot.id, pre.data(), pre.size());
++ bytes += n_t;
++ ok = rsm_seal(tmp_t, n_t); // 0010
+ rsm_drop_cache(tmp_t);
+ if (ctx_dft) {
+ const std::string fd = rsm_path(h, "dft");
+- bytes += llama_state_seq_save_file(ctx_dft, fd.c_str(), slot.id, pre.data(), pre.size());
++ const size_t n_d = llama_state_seq_save_file(ctx_dft, fd.c_str(), slot.id, pre.data(), pre.size());
++ bytes += n_d;
++ ok = rsm_seal(fd, n_d) && ok;
+ rsm_drop_cache(fd);
+ }
+ std::vector<uint8_t> sp;
+ if (common_speculative_get_state(spec.get(), slot.id, sp)) {
+ std::ofstream f(rsm_path(h, "spec"), std::ios::binary);
+ f.write((const char *) sp.data(), sp.size());
+- ok = (bool) f;
++ ok = (bool) f && ok;
+ bytes += sp.size();
+ } else {
+ std::filesystem::remove(rsm_path(h, "spec"), ec);
+@@ -2730,6 +2766,8 @@ private:
+ if (ok) {
+ std::filesystem::rename(tmp_t, ft, ec); // the .tgt file appears last: it marks a complete set
+ ok = !ec;
++ } else {
++ std::filesystem::remove(tmp_t, ec);
+ }
+ SLT_INF(slot, "resume: %s the %d-token prefix state (%.1f MiB) in %.2f s\n", ok ? "wrote" : "FAILED to write", p,
+ bytes / 1024.0 / 1024.0, (ggml_time_us() - t0) / 1e6);
+@@ -3693,6 +3731,10 @@ private:
+
+ if (rsm_on() && params_base.n_ctx_checkpoints > 0 && slot.task->type == SERVER_TASK_TYPE_COMPLETION) {
+ rsm_task_start(slot, n_past);
++ } else if (rsm_prefix_only() && slot.task->type == SERVER_TASK_TYPE_COMPLETION) {
++ slot.rsm_points.clear();
++ slot.rsm_prefix = -1;
++ rsm_load_prefix(slot, n_past);
+ }
+
+ llama_pos pos_next = slot.prompt.tokens.pos_next(n_past);
+@@ -3793,7 +3835,7 @@ private:
+ "https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055");
+ pos_next = 0;
+ n_past = 0;
+- if (rsm_on() && params_base.n_ctx_checkpoints > 0 && slot.task->type == SERVER_TASK_TYPE_COMPLETION) {
++ if (rsm_on() && (params_base.n_ctx_checkpoints > 0 || rsm_prefix_only()) && slot.task->type == SERVER_TASK_TYPE_COMPLETION) {
+ int32_t np = 0;
+ if (rsm_load_prefix(slot, np)) { // the cache is now exactly the prefix
+ n_past = np;
+@@ -3989,6 +4031,9 @@ private:
+ if (do_checkpoint && rsm_on() && (slot.rsm_is_point(slot.prompt.n_tokens()) || slot.prompt.n_tokens() == slot.rsm_prefix)) {
+ break;
+ }
++ if (!do_checkpoint && rsm_prefix_only() && slot.prompt.n_tokens() == slot.rsm_prefix) {
++ break;
++ }
+
+ // process the last few tokens of the prompt separately in order to allow for a checkpoint to be created.
+ // create checkpoints that many tokens before the end of the prompt:
+@@ -4022,7 +4067,7 @@ private:
+ const bool is_user_start = spans.is_user_start(n_tokens_start);
+ const bool is_last_user_message = n_tokens_start == last_user_pos;
+ const bool rsm_point = do_checkpoint && rsm_on() && slot.rsm_is_point(n_tokens_start);
+- if (do_checkpoint && rsm_on() && n_tokens_start == slot.rsm_prefix && !has_mtmd) {
++ if ((do_checkpoint ? rsm_on() : rsm_prefix_only()) && n_tokens_start == slot.rsm_prefix && !has_mtmd) {
+ rsm_save_prefix(slot);
+ }
+
diff --git a/patches/apply.sh b/patches/apply.sh
index 8976e69..db674bd 100755
--- a/patches/apply.sh
+++ b/patches/apply.sh
@@ -14,6 +14,9 @@
# q8_K as a KV cache type (int8 KV, opt-in). GGML_ATTN=0 selects the fork's paths.
# 10. 0009-*.patch: kv5 / kv6 KV cache types (kernels/kvq.h: 5 / 6-bit rows read by attn-fast.inc; opt-in,
# `serve --kv6` / `--kv5`).
+# 11. 0010-*.patch: resume for `serve --multi`: LLAMA_RESUME_PREFIX_ONLY=1 with --ctx-checkpoints 0 uses the prefix states
+# on disk without a checkpoint in RAM (unset = 0007's resume logic); and state files are sealed with a trailer, so a
+# file cut short on disk is written again instead of aborting the server.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
src=${1:?usage: apply.sh <llama.cpp checkout>}
diff --git a/serve b/serve
index a38fda4..fec3cb5 100755
--- a/serve
+++ b/serve
@@ -3,7 +3,8 @@
#
# ./serve # 127.0.0.1:8080, 4k context, speculative decoding on, thinking off
# ./serve --host 0.0.0.0 --port 8080 --ctx 8192 --threads 8 # 8k on 8 GB: plain decoding, ~1 GB to spare
-# ./serve --multi 4 # 4 requests decode together: ~2.3x the total tok/s; no speculation, no resume, 2k ctx each
+# ./serve --multi 4 # 4 requests decode together: ~2.3x the total tok/s; no speculation, 2k ctx each;
+# # a system prompt the streams share is read once and then loaded from disk
# ./serve --no-webui # API only: do not serve the chat page at /
# ./serve --no-spec # plain decoding (no speculation)
# ./serve --spec # keep speculation on where serve would turn it off: for lack of memory
@@ -56,16 +57,23 @@ case $MULTI in ''|*[!0-9]*) echo "--multi takes a number of streams (2-8)" >&2;
for v in "$PORT" "$THREADS" ${CTX:+"$CTX"} ${DEPTH:+"$DEPTH"}; do
case $v in ''|*[!0-9]*) echo "--port, --ctx, --threads and --spec-depth take a number (got: $v)" >&2; exit 2 ;; esac
done
-CKPT=1
+CKPT=1; RESUME_MODE=full
if [ "$MULTI" -ge 2 ]; then
# X2: N streams share each pass over the weights. --ctx is per stream here. Speculation is off (X1/MTP + X2 is untested).
[ "$MULTI" -gt 8 ] && { echo "--multi: at most 8 streams" >&2; exit 2; }
[ "$MULTI" -gt 6 ] && echo "warning: --multi $MULTI > 6: measured 8 streams leave < 0.3 GB free on an 8 GB machine" >&2
[ "$SPEC" = 1 ] && echo "note: --multi turns speculation off (the two together are untested)" >&2
- [ "$RESUME" = 1 ] && echo "note: --multi turns agent-loop resume off" >&2
SPEC=0
[ -z "$CTX" ] && { [ "$MULTI" -le 4 ] && CTX=2048 || CTX=1024; }
NP=$MULTI; CTX_TOTAL=$((CTX * MULTI)); CKPT=0
+ # Resume with several streams (0010; measured 2026-11-25 on 8 vCPU / 8 GB boxes with 2, 4 and 6 streams): the state at
+ # the end of the system(+tools) prompt is written to disk once and loaded by every stream that cannot continue what it
+ # holds (a new conversation, a rewritten ending, a restart). No checkpoint in RAM, the prompt is read in the same
+ # batches as without it, and every generated token's logprobs came out equal to --no-resume.
+ # The per-stream checkpoint of the single-stream server (the exact resume point after a rewritten ending) stays off
+ # here: ~150 MiB of RAM per stream (4 x 2048 on 8 GB: 0.08 GB left) and logprobs not always bit-equal to --no-resume.
+ # It can be tried with: ./serve --multi N -- --ctx-checkpoints 1
+ [ "$RESUME" = 1 ] && RESUME_MODE=prefix
export GGML_X2=${GGML_X2:-95} LLAMA_X2_SERIAL_PREFILL=${LLAMA_X2_SERIAL_PREFILL:-1} LLAMA_X2_CKPT_SPLIT=${LLAMA_X2_CKPT_SPLIT:-1}
if [ "$CTX_TOTAL" -gt 8192 ]; then
echo "warning: $MULTI streams x $CTX tokens = $CTX_TOTAL > 8192 total; measured safe on 8 GB: 4 x 2048, 6 x 1024" >&2
@@ -94,6 +102,7 @@ fi
if [ "$RESUME" = 1 ]; then
# 0007: one checkpoint in RAM (as before), placed where the next request will resume; prefix states on disk (~0.3 GB each)
export LLAMA_RESUME=${LLAMA_RESUME:-1} LLAMA_RESUME_DIR=${LLAMA_RESUME_DIR-$PREFIX/state}
+ [ "$RESUME_MODE" = prefix ] && export LLAMA_RESUME_PREFIX_ONLY=${LLAMA_RESUME_PREFIX_ONLY:-1}
else
export LLAMA_RESUME=0 LLAMA_RESUME_DIR=
fi
@@ -174,5 +183,5 @@ fi
export LD_LIBRARY_PATH="$BIN${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
SHOW=$HOST; [ "$HOST" = 0.0.0.0 ] && SHOW=127.0.0.1
[ "$WEBUI" = 1 ] && echo "chat: open http://$SHOW:$PORT in a browser (the page answers once the model has loaded)" >&2
-echo "serving on http://$HOST:$PORT/v1 (speculation: $SPECNAME, streams: $NP, ctx: $CTX_TOTAL, thinking: $THINK, resume: $([ "$RESUME" = 1 ] && [ "$CKPT" -gt 0 ] && echo on || echo off))" >&2
+echo "serving on http://$HOST:$PORT/v1 (speculation: $SPECNAME, streams: $NP, ctx: $CTX_TOTAL, thinking: $THINK, resume: $([ "$RESUME" = 0 ] && echo off || { [ "$RESUME_MODE" = prefix ] && echo "on (shared prefix)" || echo on; }))" >&2
exec "$BIN/llama-server" "${args[@]}" "${EXTRA[@]}"