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