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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: engram
|
| 3 |
+
tags:
|
| 4 |
+
- kv-cache
|
| 5 |
+
- fingerprinting
|
| 6 |
+
- fourier
|
| 7 |
+
- retrieval
|
| 8 |
+
- hnsw
|
| 9 |
+
- session-memory
|
| 10 |
+
- cross-model
|
| 11 |
+
- inference
|
| 12 |
+
- mcp
|
| 13 |
+
- llm-memory
|
| 14 |
+
license: apache-2.0
|
| 15 |
+
language:
|
| 16 |
+
- en
|
| 17 |
+
pipeline_tag: feature-extraction
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# ENGRAM: KV Cache Fingerprinting Protocol
|
| 21 |
+
|
| 22 |
+
**You Don't Need Adapters: Cross-Model Document Retrieval via Intrinsic KV Cache Geometry**
|
| 23 |
+
|
| 24 |
+
ENGRAM extracts Fourier fingerprints from LLM KV caches, stores them as compact binary certificates (`.eng` files, ~800 bytes), and retrieves them via HNSW approximate nearest neighbor search. This enables **persistent cross-session memory** for large language models with zero training.
|
| 25 |
+
|
| 26 |
+
> *By ENIGMA*
|
| 27 |
+
|
| 28 |
+
## Key Results
|
| 29 |
+
|
| 30 |
+
| Metric | Value |
|
| 31 |
+
|---|---|
|
| 32 |
+
| Recall@1 (N=200) | **100.0%** (post Stage-4) |
|
| 33 |
+
| Raw Fourier recall | **98.0%** (f0+f1 DFT) |
|
| 34 |
+
| HNSW search latency | **51.8 us** |
|
| 35 |
+
| HNSW speedup | **5.7x** vs brute-force |
|
| 36 |
+
| Cross-model transfer | **+0.124 margin** (FCDB, no adapter) |
|
| 37 |
+
| CKA isomorphism | **0.975** within-family, **0.927** cross-family |
|
| 38 |
+
| Certificate size | **~800 bytes** per document |
|
| 39 |
+
| Architectures | llama, gemma, gemma4/ISWA, phi, qwen, mistral |
|
| 40 |
+
| Tests | **220 passing** |
|
| 41 |
+
|
| 42 |
+
## How It Works
|
| 43 |
+
|
| 44 |
+
```
|
| 45 |
+
KV cache blob --> layer key extraction --> DFT(f0+f1) --> fingerprint (~800 bytes)
|
| 46 |
+
|
|
| 47 |
+
Query fingerprint --> HNSW search --> geodesic retrieval --> matched session/document
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
### The Fourier Fingerprint
|
| 51 |
+
|
| 52 |
+
ENGRAM decomposes per-layer key trajectories using a 2-component DFT:
|
| 53 |
+
- **f0** (DC component): captures the mean activation level per layer
|
| 54 |
+
- **f1** (first harmonic): captures the dominant oscillation pattern
|
| 55 |
+
|
| 56 |
+
The resulting fingerprint is a compact, deterministic signature of the KV cache state that is:
|
| 57 |
+
- **Model-intrinsic**: derived from the model's own geometry, not learned embeddings
|
| 58 |
+
- **Cross-model transferable**: via Frechet Cross-Domain Bridge (FCDB)
|
| 59 |
+
- **Compression-robust**: 0.99998 cosine similarity after INT8 quantization
|
| 60 |
+
|
| 61 |
+
### 4-Stage Geodesic Retrieval
|
| 62 |
+
|
| 63 |
+
```
|
| 64 |
+
Stage 0: Prior preemption (IndexC chronic failure -> skip HNSW)
|
| 65 |
+
Stage 1: HNSW search -> HIGH / MEDIUM confidence
|
| 66 |
+
Stage 2: Trajectory correction -> MEDIUM (interpolation w=0.3)
|
| 67 |
+
Stage 3: Negative constraints -> LOW (apophatic layer)
|
| 68 |
+
Stage 4: Metadata disambig -> LOW + stage4_used=True
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Install
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
# Python (core library)
|
| 75 |
+
pip install engram-kv
|
| 76 |
+
|
| 77 |
+
# Node.js (MCP client)
|
| 78 |
+
npm install engram-kv-mcp
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### From source
|
| 82 |
+
|
| 83 |
+
```bash
|
| 84 |
+
git clone https://github.com/infraax/engram.git
|
| 85 |
+
cd engram
|
| 86 |
+
python3 -m venv .venv && source .venv/bin/activate
|
| 87 |
+
pip install -e ".[dev]"
|
| 88 |
+
|
| 89 |
+
# Run tests
|
| 90 |
+
KMP_DUPLICATE_LIB_OK=TRUE OMP_NUM_THREADS=1 PYTHONPATH=. pytest tests/ -x -q
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
## Architecture Support
|
| 94 |
+
|
| 95 |
+
| Architecture | Attention Type | Status |
|
| 96 |
+
|---|---|---|
|
| 97 |
+
| Llama (1B-70B) | Standard MHA | Fully supported |
|
| 98 |
+
| Gemma (2B-27B) | Standard MHA | Fully supported |
|
| 99 |
+
| Gemma 4 (26B) | ISWA (sliding + global) | Fully supported |
|
| 100 |
+
| Phi (3.8B) | Standard MHA | Fully supported |
|
| 101 |
+
| Qwen (1.8B-72B) | GQA | Fully supported |
|
| 102 |
+
| Mistral (7B) | GQA + sliding window | Fully supported |
|
| 103 |
+
|
| 104 |
+
## Cross-Model Transfer
|
| 105 |
+
|
| 106 |
+
9 strategies evaluated. **FCDB (Frechet Cross-Domain Bridge)** wins:
|
| 107 |
+
|
| 108 |
+
| Strategy | Margin | Method |
|
| 109 |
+
|---|---|---|
|
| 110 |
+
| FCDB | **+0.124** | Frechet mean of cross-model fingerprints |
|
| 111 |
+
| TruncAlign | +0.098 | Truncate to min shared layers |
|
| 112 |
+
| ZeroPad | +0.067 | Pad shorter fingerprint with zeros |
|
| 113 |
+
| SpectralInterp | +0.045 | Interpolate in frequency domain |
|
| 114 |
+
|
| 115 |
+
No adapter training required. The geometry is intrinsic.
|
| 116 |
+
|
| 117 |
+
## MCP Server (Claude Code Integration)
|
| 118 |
+
|
| 119 |
+
ENGRAM includes an MCP server for persistent session memory in Claude Code:
|
| 120 |
+
|
| 121 |
+
```bash
|
| 122 |
+
claude mcp add --global engram-memory \
|
| 123 |
+
-e ENGRAM_SESSIONS_DIR=~/.engram/sessions \
|
| 124 |
+
-- python3 mcp/engram_memory.py
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
**7 tools**: `write_session_engram`, `get_last_session`, `retrieve_relevant_sessions`, `get_relevant_context`, `list_indexed`, `index_knowledge`
|
| 128 |
+
|
| 129 |
+
## EIGENGRAM Binary Format (v1.2)
|
| 130 |
+
|
| 131 |
+
Compact, versioned binary certificates:
|
| 132 |
+
|
| 133 |
+
```
|
| 134 |
+
Header: magic(4B) + version(2B) + flags(2B) + dimensions
|
| 135 |
+
Vectors: vec_perdoc + vec_fcdb + joint_center + vec_fourier + vec_fourier_v2
|
| 136 |
+
Meta: corpus_hash + model_id + metrics + task_description
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
~800 bytes per document. Deterministic encoding. Cross-platform portable.
|
| 140 |
+
|
| 141 |
+
## Theoretical Contributions
|
| 142 |
+
|
| 143 |
+
1. **Margin Power Law**: margin ~ A * N^alpha where alpha = -0.207 (graceful degradation, no cliff)
|
| 144 |
+
2. **CKA Manifold Isomorphism**: within-family 0.975, cross-family 0.927 (geometry is intrinsic)
|
| 145 |
+
3. **Frequency Ablation**: f0+f1 is the sweet spot (f0-only: -23% recall, f0+f1+f2: -0.3% margin)
|
| 146 |
+
4. **FCDB Scaling Law**: cross-model recall drops from 100% (N<=20) to 0% (N=200) -- adapter-free has limits
|
| 147 |
+
|
| 148 |
+
## Citation
|
| 149 |
+
|
| 150 |
+
```bibtex
|
| 151 |
+
@article{enigma2026engram,
|
| 152 |
+
title={You Don't Need Adapters: Cross-Model Document Retrieval
|
| 153 |
+
via Intrinsic KV Cache Geometry},
|
| 154 |
+
author={ENIGMA},
|
| 155 |
+
year={2026},
|
| 156 |
+
url={https://github.com/infraax/engram}
|
| 157 |
+
}
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
## Links
|
| 161 |
+
|
| 162 |
+
- [GitHub](https://github.com/infraax/engram)
|
| 163 |
+
- [PyPI: engram-kv](https://pypi.org/project/engram-kv/)
|
| 164 |
+
- [npm: engram-kv-mcp](https://www.npmjs.com/package/engram-kv-mcp)
|
| 165 |
+
|
| 166 |
+
## License
|
| 167 |
+
|
| 168 |
+
Apache-2.0
|