File size: 12,924 Bytes
51b2aff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | # MLE β Morpho-Logic Engine
> **A novel energy-based reasoning AI architecture, CPU-native, gradient-free, built on hyperdimensional computing.**
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[]()
```
ββββ ββββ βββ ββββββββ
βββββ βββββ βββ ββββββββ
βββββββββββ βββ ββββββ Morpho-Logic Engine v0.1.0
βββββββββββ βββ ββββββ Energy-Based Reasoning AI
βββ βββ βββ ββββββββββββββββ
βββ βββ ββββββββββββββββ
```
---
## π§ What is MLE?
MLE is a **new class of reasoning engine** that replaces neural network backpropagation with energy-based dynamics operating on hyperdimensional binary vectors. It draws from:
- **Kanerva's Sparse Distributed Memory** β memory indexed by proximity in Hamming space
- **Holographic Reduced Representations** (Plate 1995) β circular convolution for semantic binding
- **Modern Hopfield Networks** (Ramsauer et al. 2020) β energy-based pattern completion
- **Binary Spatter Codes** β ultra-fast XOR binding for CPU-native computation
The result is a system that can reason about concepts, solve analogies, compose meanings, and traverse knowledge graphs β all **without GPU, without gradients, without training** β using pure bitwise operations optimized for CPU SIMD instructions.
---
## ποΈ Architecture
```
βββββββββββββββ ββββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Query βββββΆβ Routing βββββΆβ Binding βββββΆβ Energy β
β Encoder β β (JIT Beam) β β (Compose) β β (Relax) β
β β β Top-500 β β XOR / FFT β β Hopfield + β
β strβ4096b β β LSH+Expand β β Conv Circ. β β Bit-flip β
βββββββββββββββ ββββββββββββββββ ββββββββββββββββ βββββββββββββββ
β β
β ββββββββββββββββ ββββββββββββββββ β
βββββββββββββ Response ββββββ Decode ββββββββββββ
β β β NN + Role β
ββββββββββββββββ ββββββββββββββββ
```
### 5 Modules
| Module | File | Description |
|--------|------|-------------|
| **`memory`** | `sparse_address_table.py` | Sparse Address Table: 4096-bit binary vectors, SoA layout, LSH index (32 tables Γ 8-bit signatures), multi-probe search, activation tracking |
| **`routing`** | `recursive_jit_router.py` | Recursive JIT Routing: LSH init β beam-500 refinement β neighbor expansion β convergence check. Multi-hop chaining for chain-of-thought |
| **`binding`** | `semantic_binding.py` | Dual binding: **Binary (XOR)** O(N/64) exact recovery + **HRR (FFT)** O(N log N) approximate. Triple encoding, analogy queries, sequence binding via permutation |
| **`energy`** | `energy_model.py` | Composite energy: compatibility + binding coherence + sparsity + smoothness. **Hopfield** (continuous, attention-based) + **Binary relaxation** (simulated annealing). Hybrid mode for best results |
| **`inference`** | `reasoning_engine.py` | Full pipeline: encode β route β bind β relax β decode. Association, analogy, composition, structured queries. Multi-step reasoning with convergence detection |
### SIMD-Optimized Core (`utils/simd_ops.py`)
All core operations are backed by a **GCC-compiled C library** with `-march=native` for automatic SIMD vectorization:
```c
// Compiles to AVX-512 VPOPCNTQ or AVX2 POPCNT automatically
int hamming_single(const uint64_t *a, const uint64_t *b, int n) {
int cnt = 0;
for (int i = 0; i < n; i++)
cnt += __builtin_popcountll(a[i] ^ b[i]);
return cnt;
}
```
| Operation | Throughput | Notes |
|-----------|-----------|-------|
| Hamming distance (single) | ~100M ops/s | 64 Γ `POPCNT` per pair |
| Hamming batch (100K vectors) | 25-41M vecs/s | Vectorized XOR + popcount |
| Top-500 selection | O(N log K) | Max-heap in C |
| Binary bind (XOR) | ~95K ops/s | 64 Γ `XOR` per op |
| HRR bind (FFT) | ~10K ops/s | `numpy.fft.rfft` |
**Fallback**: Pure NumPy LUT-based popcount when GCC isn't available β portable across all platforms.
---
## π Core Concepts
### 4096-bit Binary Vectors
Every concept, relation, and memory address is a **4096-bit binary vector** stored as 64 Γ `uint64` words (512 bytes):
```python
# Each vector: 4096 bits = 512 bytes = 64 Γ uint64
# Storage layout: Structure of Arrays (SoA) for cache locality
# addresses: (N, 64) uint64 β contiguous, cache-aligned
# contents: (N, 64) uint64 β separate for SIMD batch ops
```
**Key property**: Random vectors have Hamming distance β 2048 (50%). Semantic similarity is encoded as deviation from this baseline. Vectors with distance << 2048 are "similar"; vectors at ~2048 are orthogonal.
### Sparse Address Table
Memory entries are indexed by binary vectors. Access uses **Hamming distance** as the proximity metric:
```python
from mle import SparseAddressTable
sat = SparseAddressTable(capacity=100_000)
sat.store(address_vec, content_vec, metadata={'name': 'cat'})
results = sat.query_nearest(query_vec, k=10) # [(index, distance), ...]
```
**LSH Index**: 32 hash tables with 8-bit random-bit-sampling signatures. Multi-probe search (1-bit and 2-bit flips) for high recall. Sub-linear search time for large memories.
### Binding Operations
**Binary (XOR)**: `bind(A, B) = A β B`. Self-inverse (exact recovery), quasi-orthogonal to inputs, O(N/64).
**HRR (FFT)**: `bind(A, B) = IFFT(FFT(A) Β· FFT(B))`. Circular convolution, approximate recovery, similarity-preserving, O(N log N).
```python
from mle.binding import BinaryBinding
# Encode: "king IS_A man"
triple = BinaryBinding.encode_triple(king_vec, is_a_vec, man_vec)
# Decode: recover man from triple
decoded = BinaryBinding.unbind(BinaryBinding.unbind(triple, king_vec), is_a_vec)
# decoded == man_vec (exact with XOR!)
# Analogy: king:man :: queen:?
query = BinaryBinding.create_analogy_query(king_vec, man_vec, queen_vec)
# query β woman_vec (find nearest in codebook)
```
### Energy-Based Reasoning
**No backpropagation. No gradients stored.** Reasoning is energy minimization:
```
E(state) = α·E_compat + β·E_binding + γ·E_sparse + δ·E_smooth
```
| Component | Formula | Purpose |
|-----------|---------|---------|
| Compatibility | -Ξ£ wα΅’ Β· sim(state, contextα΅’) | State agrees with activated memories |
| Binding coherence | Ξ£ hamming(unbind(bα΅’, rα΅’), fα΅’) / N | Stored relations remain intact |
| Sparsity | βactivationsββ | Focused, not diffuse, activation |
| Smoothness | hamming(current, previous) / N | Stable reasoning trajectory |
**Two-phase minimization**:
1. **Hopfield update**: `ΞΎ_new = X @ softmax(Ξ² Β· X^T @ ΞΎ)` β fast coarse convergence via attention over patterns
2. **Binary relaxation**: bit-flip search with simulated annealing β fine discrete refinement
---
## π Quick Start
```python
from mle import MorphoLogicEngine
# Initialize
engine = MorphoLogicEngine(beam_width=500, energy_mode='hybrid')
# Build knowledge
engine.add_concept("cat")
engine.add_concept("dog")
engine.add_concept("animal")
engine.add_relation("cat", "is_a", "animal")
engine.add_relation("dog", "is_a", "animal")
# Reason
result = engine.reason("cat", max_steps=3)
print(result['response']['nearest_concepts'])
# β [('cat', 0.99), ('animal', 0.75), ...]
# Associations
assocs = engine.associate("cat", top_k=5)
# β [('cat_is_a_animal', 0.74), ('dog', 0.52), ...]
# Analogy: king:man :: queen:?
analogy = engine.solve_analogy("king", "man", "queen")
print(analogy['codebook_ranking'][:3])
# Composition: water + animal β ?
comp = engine.compose("water", "animal")
print(comp['response']['nearest_concepts'][:3])
```
---
## π Benchmarks
Measured on a 2-vCPU machine (cpu-basic), single-threaded:
### SIMD Throughput
| Corpus Size | Batch Hamming | Top-500 |
|-------------|--------------|---------|
| 1,000 | 0.04ms (28M/s) | 0.06ms |
| 10,000 | 0.29ms (35M/s) | 0.32ms |
| 100,000 | 4.56ms (22M/s) | 4.79ms |
### Routing Latency
| Memory Size | Avg Latency | P99 | Candidates |
|-------------|-------------|-----|------------|
| 1,000 | 3.8ms | 5.4ms | 953 |
| 10,000 | 2.5ms | 3.2ms | 3,335 |
| 50,000 | 2.7ms | 3.5ms | 2,679 |
### Memory Efficiency
- **1,024 bytes/entry** (512 address + 512 content)
- **1,000 entries = 1 MB**
- **100,000 entries = 100 MB**
### Binding Performance
- Binary (XOR): **95,000 ops/sec**
- HRR (FFT): **10,500 ops/sec**
---
## π§ͺ Tests
```bash
pip install numpy scipy
python -m mle.tests.test_full_system
```
**7/7 test groups passing:**
- β
SIMD Operations (correctness + performance)
- β
Memory & LSH (storage, retrieval, 100% cluster recall)
- β
Routing (beam width, convergence, scalability)
- β
Binding (XOR exact recovery, HRR approximate recovery, triple encoding)
- β
Energy Convergence (monotonic decrease, Hopfield attention concentration)
- β
Reasoning (association, query, analogy, composition, structured queries)
- β
Integration (500+ concept KB, batch queries, memory efficiency)
---
## π― Demo
```bash
python -m mle.demo
```
Runs a full demonstration with 40+ concepts, 42 relations, and tests for concept queries, associations, analogies, compositions, structured queries, and multi-step reasoning.
---
## π Project Structure
```
mle/
βββ __init__.py # Package init, public API
βββ demo.py # Interactive demonstration
βββ utils/
β βββ __init__.py
β βββ simd_ops.py # SIMD C library + NumPy fallback
βββ memory/
β βββ __init__.py
β βββ sparse_address_table.py # SparseAddressTable + HammingLSH
βββ routing/
β βββ __init__.py
β βββ recursive_jit_router.py # RecursiveJITRouter
βββ binding/
β βββ __init__.py
β βββ semantic_binding.py # HRRBinding + BinaryBinding + BindingEngine
βββ energy/
β βββ __init__.py
β βββ energy_model.py # EnergyFunction + Relaxation + Hopfield
βββ inference/
β βββ __init__.py
β βββ reasoning_engine.py # ReasoningEngine (full pipeline)
βββ tests/
βββ __init__.py
βββ test_full_system.py # Comprehensive test suite
```
---
## π¬ Theoretical Foundations
| Paper | Contribution to MLE |
|-------|-------------------|
| Kanerva (1988) "Sparse Distributed Memory" | Binary vector addressing, Hamming distance proximity |
| Plate (1995) "Holographic Reduced Representations" | Circular convolution binding, FFT implementation |
| Gayler (2003) "Vector Symbolic Architectures" | XOR binding (BSC), majority-vote bundling |
| Ramsauer et al. (2020) "Hopfield Networks Is All You Need" | Modern Hopfield energy, exponential capacity, attention β‘ update rule |
| Frady et al. (2021) "SDM and Transformers" | SDM Hamming threshold β transformer attention |
| Thomas et al. (2023) "Efficient HDC with Static Optimization" | Optimal BSC dimensions, analytical thresholds |
| Langford et al. (2024) "Linear Codes for HDC" | GF(2) factorization, 100% XOR recovery |
---
## π€οΈ Roadmap
- [ ] **Persistent storage**: Serialize memory to disk (mmap for instant loading)
- [ ] **Learned embeddings**: Pre-encode concepts from text corpora (word2vec β binary projection)
- [ ] **Multi-threaded SIMD**: Parallel batch Hamming with OpenMP
- [ ] **Graph walk reasoning**: Follow relation chains for multi-hop inference
- [ ] **Incremental learning**: Hebbian-style weight updates from experience
- [ ] **Benchmark suite**: Standardized reasoning tasks (bAbI, CLUTRR, etc.)
---
## π License
MIT
---
## π Acknowledgments
Inspired by the vision of frugal, explainable AI that reasons rather than retrieves. Built on decades of research in hyperdimensional computing, energy-based models, and sparse distributed memory.
|