Skip to content

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

fix(resume): keep active LoRA requests out of the base prefix cache (#116)

Afloatec41efd3331c
diff --git a/README.md b/README.md
index 52a03fe..2e58fb9 100644
--- a/README.md
+++ b/README.md
@@ -376,6 +376,9 @@ The adapter loads, changes the output and tool calls stay well-formed; speculati
same rate. On 8 GB use `--no-spec` with an adapter, or a machine with more memory. Not measured: whether an adapter
trained on the full-precision model improves this ternary build on any task.
+Requests with an active adapter do not read or write the shared prefix states on disk, which belong to the base model.
+Prompt reuse and checkpoints in memory still work.
+
## 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
@@ -500,4 +503,4 @@ most useful with the output of `./install.sh --check` and the line `./serve` pri
MIT (see `LICENSE`; the ggml authors' copyright is kept). Credits and model licences in `NOTICE`: ggml/llama.cpp,
PrismML (PTQ1_0 and Bonsai-2), unsloth (MTP sidecar), Qwen (Qwen3.8-27B). The weights are Apache 2.0 and are downloaded
-from Hugging Face, not redistributed here.
\ No newline at end of file
+from Hugging Face, not redistributed here.
diff --git a/patches/0007-resume.patch b/patches/0007-resume.patch
index dfcc2d3..55468a9 100644
--- a/patches/0007-resume.patch
+++ b/patches/0007-resume.patch
@@ -234,7 +234,7 @@ index ee2de80..40e52f2 100644
cur.data_tgt = std::move(spare_tgt);
cur.data_dft = std::move(spare_dft);
-@@ -2403,6 +2578,164 @@ private:
+@@ -2403,6 +2578,169 @@ private:
cur.pos_max, cur.n_tokens, (float) cur.size() / 1024 / 1024);
}
@@ -301,6 +301,11 @@ index ee2de80..40e52f2 100644
+ if (rsm.dir.empty()) {
+ return false;
+ }
++ // Adapter weights and per-request scales are not part of the disk-cache namespace.
++ // Keep adapted requests out of it until their state can be keyed independently.
++ if (std::any_of(slot.lora.begin(), slot.lora.end(), [](const auto & adapter) { return adapter.scale != 0.0f; })) {
++ return false;
++ }
+ if (rsm_env_hash == 0) {
+ const std::string d = rsm_env_desc(params_base.model.path, (int) params_base.cache_type_k, (int) params_base.cache_type_v,
+ (int) params_base.flash_attn_type, ctx_dft != nullptr);
diff --git a/tests/test_resume_lora.py b/tests/test_resume_lora.py
new file mode 100644
index 0000000..1676a08
--- /dev/null
+++ b/tests/test_resume_lora.py
@@ -0,0 +1,114 @@
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class ResumeLora(unittest.TestCase):
+ def test_active_adapters_do_not_use_the_base_model_disk_cache(self):
+ compiler = shutil.which('c++') or shutil.which('g++') or shutil.which('clang++')
+ if compiler is None:
+ self.skipTest('needs a C++ compiler')
+ patch = (ROOT / 'patches/0007-resume.patch').read_text()
+ source = '\n'.join(line[1:] for line in patch.splitlines()
+ if line.startswith('+') and not line.startswith('+++'))
+ method = source[source.index(' bool rsm_load_prefix('):
+ source.index(' // 0007: write the prefix state;')]
+ harness = r'''
+#include <algorithm>
+#include <cstdint>
+#include <cstdio>
+#include <filesystem>
+#include <fstream>
+#include <memory>
+#include <string>
+#include <vector>
+using llama_tokens = std::vector<int32_t>;
+using llama_token = int32_t;
+constexpr int LLAMA_TOKEN_NULL = -1;
+constexpr int COMMON_CHAT_ROLE_SYSTEM = 0, COMMON_CHAT_ROLE_UNKNOWN = 1, USER = 2;
+#define SRV_INF(...) ((void) 0)
+#define SLT_INF(...) ((void) 0)
+#define SLT_WRN(...) ((void) 0)
+struct { std::string dir; } rsm;
+static uint64_t rsm_env_hash = 1;
+static int loads = 0;
+static std::string rsm_env_desc(const std::string &, int, int, int, bool) { return "env"; }
+static uint64_t rsm_fnv(uint64_t, const void *, size_t) { return 1; }
+static uint64_t rsm_hash(const llama_tokens &) { return 1; }
+static std::string rsm_path(uint64_t, const char * part) { return rsm.dir + "/prefix." + part; }
+static int64_t ggml_time_us() { return 0; }
+static bool rsm_read_bytes(const std::string &, std::vector<uint8_t> &) { return false; }
+static void common_speculative_set_state(int *, int, const std::vector<uint8_t> &) {}
+static void rsm_drop_cache(const std::string &) {}
+static size_t llama_state_seq_load_file(void *, const char *, int, int32_t * tokens, size_t count, size_t * got) {
+ ++loads;
+ std::fill(tokens, tokens + count, 0);
+ *got = count;
+ return 1;
+}
+struct input_tokens {
+ llama_tokens values = llama_tokens(65, 0);
+ const llama_tokens & get_tokens() const { return values; }
+};
+struct span { int role, pos; };
+struct task_params { struct { std::vector<span> spans = {{COMMON_CHAT_ROLE_SYSTEM,0},{USER,64}}; } message_spans; };
+struct server_task { input_tokens tokens; task_params params; int32_t n_tokens() const { return 65; } };
+struct adapter { float scale; };
+struct server_slot {
+ server_task * task;
+ int id = 0, rsm_prefix = -1;
+ std::vector<adapter> lora;
+ struct { void seq_rm(int, int, int) {} } mem;
+ struct { llama_tokens tokens; void clear() { tokens.clear(); } } prompt;
+};
+struct server {
+ struct { struct { std::string path; } model; int cache_type_k=0, cache_type_v=0, flash_attn_type=0; } params_base;
+ void * ctx_tgt = nullptr, * ctx_dft = nullptr;
+ std::unique_ptr<int> spec;
+'''
+ harness += method
+ harness += r'''
+};
+int main(int argc, char ** argv) {
+ if (argc != 2) return 2;
+ rsm.dir = argv[1];
+ std::ofstream(rsm_path(1, "tgt")).put('x');
+ server model;
+ server_task request;
+ for (float scale : {1.0f, -0.5f, 0.0f}) {
+ server_slot slot{&request}; slot.lora = {{scale}};
+ int32_t n_past = 0; loads = 0;
+ const bool loaded = model.rsm_load_prefix(slot, n_past);
+ const bool expected = scale == 0.0f;
+ if (loaded != expected || loads != (expected ? 1 : 0) || n_past != (expected ? 64 : 0)) {
+ std::fprintf(stderr, "scale=%g: loaded=%d calls=%d n_past=%d\n", scale, loaded, loads, n_past);
+ return 1;
+ }
+ }
+ std::filesystem::remove(rsm_path(1, "tgt"));
+ server_slot active{&request}; active.lora = {{1.0f}};
+ int32_t n_past = 0;
+ if (model.rsm_load_prefix(active, n_past) || active.rsm_prefix != -1) {
+ std::fprintf(stderr, "active LoRA request scheduled a base-namespace cache write\n");
+ return 1;
+ }
+ return 0;
+}
+'''
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ code, executable = root / 'lora.cpp', root / 'lora'
+ code.write_text(harness)
+ build = subprocess.run([compiler, '-std=c++17', '-O2', str(code), '-o', str(executable)],
+ capture_output=True, text=True)
+ self.assertEqual(build.returncode, 0, build.stderr)
+ result = subprocess.run([str(executable), str(root)], capture_output=True, text=True, timeout=20)
+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
+
+
+if __name__ == '__main__':
+ unittest.main()