Skip to content

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

fix(resume): invalidate prefix states after model weight replacement (#101)

Afloat0104aa0fa1bd
diff --git a/patches/0007-resume.patch b/patches/0007-resume.patch
index 88d48b1..dfcc2d3 100644
--- a/patches/0007-resume.patch
+++ b/patches/0007-resume.patch
@@ -16,7 +16,7 @@ diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
index ee2de80..40e52f2 100644
--- a/tools/server/server-context.cpp
+++ b/tools/server/server-context.cpp
-@@ -43,6 +43,149 @@ extern "C" int llama_x1_draft_list_ready(void);
+@@ -43,6 +43,151 @@ extern "C" int llama_x1_draft_list_ready(void);
extern "C" int llama_x1_draft_list_has(int32_t t);
extern "C" void llama_x1_draft_set_full(int full);
@@ -81,7 +81,7 @@ index ee2de80..40e52f2 100644
+ for (llama_token x : t) { h ^= (uint32_t) x; h *= 1099511628211ULL; }
+ return (h ^ (uint64_t) t.size()) * 31 + rsm_env_hash; // the file name keys the tokens AND the environment
+}
-+// what else a saved state depends on: the model file (path, size, FNV of its first MiB), this binary (size + mtime of
++// what else a saved state depends on: the model file (path, size, mtime, FNV of its first MiB), this binary (size + mtime of
+// /proc/self/exe: any patch or kernel change rebuilds it), KV types, flash-attention mode, whether a draft context
+// exists, and every GGML_* / LLAMA_* / X1_* environment variable (kernel modes, X1/X2 switches), LLAMA_RESUME* excepted
+static uint64_t rsm_fnv(uint64_t h, const void * p, size_t n) {
@@ -95,6 +95,8 @@ index ee2de80..40e52f2 100644
+static std::string rsm_env_desc(const std::string & model_path, int type_k, int type_v, int fa, bool has_dft) {
+ std::error_code ec;
+ std::string d = "model=" + model_path + " size=" + std::to_string((unsigned long long) std::filesystem::file_size(model_path, ec));
++ const auto mt = std::filesystem::last_write_time(model_path, ec).time_since_epoch().count();
++ d += " mtime=" + (ec ? std::string("unavailable") : std::to_string((long long) mt));
+ {
+ std::ifstream f(model_path, std::ios::binary);
+ std::vector<char> head(1 << 20);
diff --git a/tests/test_resume_model_identity.py b/tests/test_resume_model_identity.py
new file mode 100644
index 0000000..103b682
--- /dev/null
+++ b/tests/test_resume_model_identity.py
@@ -0,0 +1,70 @@
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class ResumeModelIdentity(unittest.TestCase):
+ def test_weight_replacement_invalidates_the_environment_key(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()
+ additions = '\n'.join(line[1:] for line in patch.splitlines()
+ if line.startswith('+') and not line.startswith('+++'))
+ start = additions.index('static uint64_t rsm_fnv(')
+ end = additions.index('static std::string rsm_path(')
+ helpers = additions[start:end]
+ source = r'''
+#include <algorithm>
+#include <chrono>
+#include <cstdint>
+#include <cstdio>
+#include <filesystem>
+#include <fstream>
+#include <string>
+#include <vector>
+using llama_tokens = std::vector<int32_t>;
+static uint64_t rsm_env_hash = 0;
+'''
+ source += helpers
+ source += r'''
+int main(int argc, char ** argv) {
+ if (argc != 2) return 2;
+ const std::string path = argv[1];
+ const auto before = rsm_env_desc(path, 1, 1, 1, false);
+ if (before != rsm_env_desc(path, 1, 1, 1, false)) return 3;
+ const auto stamp = std::filesystem::last_write_time(path);
+ const auto size = std::filesystem::file_size(path);
+ {
+ std::fstream file(path, std::ios::binary | std::ios::in | std::ios::out);
+ file.seekp((1 << 20) + 128);
+ file.put('x');
+ }
+ std::filesystem::last_write_time(path, stamp + std::chrono::seconds(1));
+ if (std::filesystem::file_size(path) != size) return 4;
+ const auto after = rsm_env_desc(path, 1, 1, 1, false);
+ if (before == after) {
+ std::fprintf(stderr, "changed model weights reuse the original prefix-cache key\n");
+ return 1;
+ }
+ return 0;
+}
+'''
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ code, executable, model = root / 'identity.cpp', root / 'identity', root / 'model.gguf'
+ code.write_text(source)
+ model.write_bytes(bytes((1 << 20) + 256))
+ 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(model)], capture_output=True, text=True, timeout=20)
+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
+
+
+if __name__ == '__main__':
+ unittest.main()