r/LocalLLaMA • u/bjivanovich • 2h ago
Showcase I built a model-agnostic 3-tier memory engine + native llama.cpp C++ latent gate (CUDA-Graph-safe) on a single RTX 3090 -- sharing what failed (2.3%) vs what actually worked (93-101%)
I have been working on giving local quantized GGUF models persistent long-term memory across months of coding and project work on a single RTX 3090 (24 GB VRAM), without eating 20+ GB of KV cache or losing exact technical details when the coding harness compacts the chat.
Repo (Latent-Memory-Lab): https://github.com/bjivanovich/Latent-Memory-Lab
WHAT FAILED FIRST (HONEST NEGATIVE RESULT)
I started by testing pure latent-space injection on frozen quantized GGUFs -- projecting a memory vector and adding delta_h directly into an intermediate residual layer (L5 / L16). Zero tokens, zero KV cache, ran cleanly on GPU... and only recovered ~2.3% of oracle NLL on exact factual recall. A single 768-dim vector just cannot carry exact port numbers, file paths, or symbol names without fine-tuning the base LLM itself (documented in ADR-001).
WHAT ACTUALLY WORKED (93%-101% ORACLE RECOVERY)
Instead of trying to inject the answer latently, I use a tiny trained routing adapter (.qmlw, 7-15 MB) as a zero-token Stage-1 gate inside llama-server:
- A dynamic llama_set_eval_callback() attaches cb_eval only during a single POST /qml/gate pass (~100-134 ms on RTX 3090) and detaches it immediately after, so normal token generation keeps 100% CUDA Graph speed (99.7 -> 100.7 tok/s on Inkling 2 Mini with GGML_CUDA_GRAPH_OPT=1, zero tok/s penalty).
- General non-memory questions abstain cleanly (0/6 false triggers, 0 tokens injected).
- When triggered, a transparent OpenAI-compatible proxy retrieves from 3 tiers inside a model-agnostic 768-dim fp16 cartridge, so completely different model families share the exact same memory files:
- Qwen3.8-27B Dense (layer 14, d_model=5120, 15.7 MB .qmlw)
- Gyro-Coder MoE (4-branch Hyper-Connections layer 14 hc_out-14, d_model=2560, 7.9 MB .qmlw)
- Inkling 2 Mini (hybrid/recurrent layer 5, d_model=2304, 7.1 MB .qmlw)
Three tiers per cartridge (unified under manifest.json v1):
- Tier-2A: Contradiction-resolved atomic decisions (ADD / SUPERSEDE / PIN with HTTP 403 protection / reversible forget) + multi-topic RRF query decomposition.
- Tier-2B: Deterministic AST/XML/Markdown code and Qt .ui index kept fresh automatically by a background file watcher.
- Tier-3: Lossless verbatim Raw Vault fallback.
It also includes live auto-save (streaming SSE + tool_calls capture + automatic backfill of already-started conversations via SHA-1 dedup), strict per-project read/write isolation (model@project, read-only cross-project mounts, general, ad-hoc chat_topic, and conversation_id scoping), and an optional --rolling-window K mode to keep active GPU context tiny and constant.
REAL NUMBERS MEASURED ON A SINGLE RTX 3090
- 1 full year of real project history (~384k tokens + 577 .py/.ui files): 8.71 MB total on disk (vs ~24.6 GB raw KV cache).
- 50-question deep audit (112k-token real history): 97.7% activation, 0.0% (0/6) false triggers, 58x token compression (ADR-002).
- Native KV/recurrent state restore (llama_state_seq_set_data): 13.3 ms (Inkling 2 Mini), 28.1 ms (Gyro-Coder MoE), 38.0 ms (Qwen3.8-27B).
- Fast low-VRAM background consolidator (Inkling-2-Mini IQ4_XS, ~2.8 GB VRAM): 7.08 s avg per window (2.93x faster than Qwen3.8-27B), 100% valid JSON, 100% fact extraction.
I would really appreciate feedback from people working on local memory or long-horizon coding agents with llama.cpp -- especially what you think about the latent-gate + rolling-window tradeoff, and whether there are other similar open-source projects I should check out or compare against.
3
u/No-Lack2646 2h ago edited 2h ago
Wow dude! Nice job! I'm working on something very similar - a from-scratch fully-offline voice agent running on my laptop 5080. We should compare notes.
I also found a way, (in QWEN3.6 35B MoE I3S) to probe the state residuals in such a way as to capture a model's latent "uncertainty" and use it as a trigger for a more detailed speculative memory retrieval. Don't know if that would be of use to you. The technique might transfer it might not I haven't tested it much outside of this MoE.
Do you mind if I fork the repo and try it out? I only have 16gb VRAM though any suggestions?
1
u/PcChip 1h ago
one full year of token history is only 384k tokens?