Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
fix(attention): release cached workspaces when worker threads exit (#100)
Afloate02d53bbb82b
diff --git a/kernels/attn-fast.inc b/kernels/attn-fast.incindex 7bf7c07..63fbf13 100644--- a/kernels/attn-fast.inc+++ b/kernels/attn-fast.inc@@ -69,9 +69,19 @@ typedef struct {int8_t * K8; // [T][DMAX] kv4/5/6: K tile unpacked to int8 levels} attnf_ws;+struct attnf_ws_owner {+ attnf_ws * value = NULL;+ ~attnf_ws_owner() {+ if (value) {+ free(value->Q); // Q is the first slice of the aligned allocation+ free(value);+ }+ }+};+static attnf_ws * attnf_ws_get(void) {- static thread_local attnf_ws * ws = NULL;- if (ws) return ws;+ static thread_local attnf_ws_owner owner;+ if (owner.value) return owner.value;const size_t RD = (size_t) ATTNF_RMAX * ATTNF_DMAX, TD = (size_t) ATTNF_T * ATTNF_DMAX, RT = (size_t) ATTNF_RMAX * ATTNF_T;const size_t bytes = sizeof(float) * (RD + ATTNF_RMAX + TD + TD + RT + RT + RD + RD) + 2 * RD + TD + 64 * 16;char * p = (char *) aligned_alloc(64, (bytes + 63) / 64 * 64);@@ -85,8 +95,8 @@ static attnf_ws * attnf_ws_get(void) {ATTNF_TAKE(w->Mt, float, RT); ATTNF_TAKE(w->O, float, RD); ATTNF_TAKE(w->OF, float, RD);ATTNF_TAKE(w->K8, int8_t, TD);#undef ATTNF_TAKE- ws = w;- return ws;+ owner.value = w;+ return owner.value;}static const int8_t attnf_zero_row[ATTNF_DMAX] = {0};diff --git a/tests/test_attention_workspace.py b/tests/test_attention_workspace.pynew file mode 100644index 0000000..df17f6e--- /dev/null+++ b/tests/test_attention_workspace.py@@ -0,0 +1,71 @@+from pathlib import Path+import shutil+import subprocess+import tempfile+import unittest++ROOT = Path(__file__).resolve().parents[1]+++class AttentionWorkspace(unittest.TestCase):+ def test_worker_exit_releases_its_cached_workspace(self):+ compiler = shutil.which('c++') or shutil.which('g++') or shutil.which('clang++')+ if compiler is None:+ self.skipTest('needs a C++ compiler')+ # Compile the production workspace implementation without requiring a full engine build.+ source = (ROOT / 'kernels/attn-fast.inc').read_text()+ workspace = source[:source.index('static const int8_t attnf_zero_row')]+ harness = r'''+#include <atomic>+#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <thread>+#include <stdlib.h>+#include <string.h>+#include <math.h>+enum ggml_type { GGML_TYPE_F16, GGML_TYPE_Q8_K, GGML_TYPE_KV4, GGML_TYPE_KV5, GGML_TYPE_KV6 };+static std::atomic<int> live_allocations{0};+static void * tracked_aligned_alloc(size_t alignment, size_t size) {+ void * p = ::aligned_alloc(alignment, size);+ if (p) ++live_allocations;+ return p;+}+static void tracked_free(void * p) {+ if (p) --live_allocations;+ ::free(p);+}+#define aligned_alloc tracked_aligned_alloc+#define free tracked_free+'''+ harness += workspace+ harness += r'''+int main() {+ for (int i = 0; i < 8; ++i) {+ std::thread worker([] {+ attnf_ws * first = attnf_ws_get();+ if (!first || attnf_ws_get() != first) std::abort();+ first->Q[0] = 1.0f;+ if (attnf_ws_get()->Q[0] != 1.0f) std::abort();+ });+ worker.join();+ if (live_allocations != 0) {+ std::fprintf(stderr, "%d workspace allocations remain after worker exit\n", live_allocations.load());+ return 1;+ }+ }+ return 0;+}+'''+ with tempfile.TemporaryDirectory() as tmp:+ source_file, executable = Path(tmp) / 'workspace.cpp', Path(tmp) / 'workspace'+ source_file.write_text(harness)+ build = subprocess.run([compiler, '-std=c++17', '-O2', '-pthread', '-I', str(ROOT / 'kernels'),+ str(source_file), '-o', str(executable)], capture_output=True, text=True)+ self.assertEqual(build.returncode, 0, build.stderr)+ result = subprocess.run([str(executable)], capture_output=True, text=True, timeout=20)+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+++if __name__ == '__main__':+ unittest.main()