|
| 1 | +"""Substrate-similarity attention: L1 distance in K-dim Fibonacci basis. |
| 2 | +
|
| 3 | +Per the user's "we need an architecture that extrapolates differently, |
| 4 | +not just compresses": standard attention's Q·K^T dot product has nothing |
| 5 | +substrate-aware about it. This module replaces it with L1 distance in a |
| 6 | +K-dim Fibonacci-basis signature space — the substrate's canonical |
| 7 | +nearness metric, the same one used for attractor snapping. |
| 8 | +
|
| 9 | +The architectural claim: nearness in K-dim Fibonacci basis IS the |
| 10 | +substrate-aligned way to ask "do these two tokens share structure?" |
| 11 | +The dot-product operator only knows about magnitudes and orientations |
| 12 | +in a generic Euclidean space. |
| 13 | +
|
| 14 | +Attention computation: |
| 15 | + sig[t] = W_sig · x[t] # [K]: substrate signature |
| 16 | + dist[i,j] = ||sig[i] - sig[j]||_1 # L1 in Fibonacci basis |
| 17 | + attn[i,j] = softmax(-dist[i,j] / sqrt(K)) # nearness ~ attention |
| 18 | +
|
| 19 | +Compute cost: O(T·d·K) for the projection + O(T²·K) for the pairwise |
| 20 | +L1 (vs O(T·d²) + O(T²·d) for dense attention). At d=4096, K=32 the |
| 21 | +L1-score computation is 128× cheaper than dense Q·K^T. |
| 22 | +
|
| 23 | +The model uses FibGen weights too (compressed storage). So we have |
| 24 | +SUBSTRATE COMPRESSED WEIGHTS + SUBSTRATE NATIVE OPERATOR. Two distinct |
| 25 | +substrate properties stacked. |
| 26 | +""" |
| 27 | + |
| 28 | +import math |
| 29 | +import sys |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | +import torch |
| 33 | +import torch.nn as nn |
| 34 | +import torch.nn.functional as F |
| 35 | + |
| 36 | +sys.path.insert(0, str(Path(__file__).parent)) |
| 37 | +from models_fibgen import FibGenLinear, FIBONACCI |
| 38 | + |
| 39 | + |
| 40 | +class SubstrateSimilarityAttention(nn.Module): |
| 41 | + """L1-distance attention in K-dim Fibonacci-basis signature space. |
| 42 | +
|
| 43 | + Substrate-native at TWO levels: |
| 44 | + - WEIGHTS: W_sig, W_v, W_out are FibGen (Fibonacci-basis seeds, ~100x |
| 45 | + smaller storage than dense). |
| 46 | + - OPERATOR: attention scores via L1 distance in the K-dim signature |
| 47 | + space, NOT Q·K^T. Tokens with matching Fibonacci signatures |
| 48 | + attend; tokens with disparate signatures are gated out. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, d_model: int, K: int = 32, seq_len: int = 128, |
| 52 | + fibgen_K: int = 32, mode: str = "cross"): |
| 53 | + super().__init__() |
| 54 | + self.d_model = d_model |
| 55 | + self.K = K |
| 56 | + self.W_sig = FibGenLinear(d_model, K, K=fibgen_K, mode=mode, bias=False) |
| 57 | + self.W_v = FibGenLinear(d_model, d_model, K=fibgen_K, mode=mode, bias=False) |
| 58 | + self.W_out = FibGenLinear(d_model, d_model, K=fibgen_K, mode=mode, bias=False) |
| 59 | + # Standard causal mask; substrate-distance attention is dense in |
| 60 | + # principle. Could also use Fibonacci-offset mask for sparsity. |
| 61 | + mask = torch.tril(torch.ones(seq_len, seq_len)) |
| 62 | + self.register_buffer("mask", mask) |
| 63 | + |
| 64 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 65 | + B, T, D = x.shape |
| 66 | + sig = self.W_sig(x) # [B, T, K] |
| 67 | + v = self.W_v(x) # [B, T, D] |
| 68 | + # Pairwise L1 distance across the T axis: [B, T, T] |
| 69 | + diff = sig.unsqueeze(2) - sig.unsqueeze(1) # [B, T, T, K] |
| 70 | + dist = diff.abs().sum(dim=-1) # [B, T, T] |
| 71 | + scores = -dist / math.sqrt(self.K) |
| 72 | + # Causal mask: cells where mask=0 set to -inf so softmax zeros them. |
| 73 | + m = self.mask[:T, :T] |
| 74 | + scores = scores.masked_fill(m == 0, float("-inf")) |
| 75 | + attn = F.softmax(scores, dim=-1) |
| 76 | + out = attn @ v |
| 77 | + return self.W_out(out) |
| 78 | + |
| 79 | + |
| 80 | +class SubsimBlock(nn.Module): |
| 81 | + """Substrate-similarity attention + FibGen FFN.""" |
| 82 | + |
| 83 | + def __init__(self, d_model: int, seq_len: int, K: int = 32, |
| 84 | + fibgen_K: int = 32, mode: str = "cross"): |
| 85 | + super().__init__() |
| 86 | + self.attn = SubstrateSimilarityAttention(d_model, K=K, seq_len=seq_len, |
| 87 | + fibgen_K=fibgen_K, mode=mode) |
| 88 | + # FFN with FibGen weights (separate K for FFN if desired) |
| 89 | + self.w1 = FibGenLinear(d_model, 4 * d_model, K=fibgen_K, mode=mode) |
| 90 | + self.w2 = FibGenLinear(4 * d_model, d_model, K=fibgen_K, mode=mode) |
| 91 | + self.ln1 = nn.LayerNorm(d_model) |
| 92 | + self.ln2 = nn.LayerNorm(d_model) |
| 93 | + |
| 94 | + def forward(self, x): |
| 95 | + x = x + self.attn(self.ln1(x)) |
| 96 | + x = x + self.w2(F.gelu(self.w1(self.ln2(x)))) |
| 97 | + return x |
| 98 | + |
| 99 | + |
| 100 | +class SubsimLM(nn.Module): |
| 101 | + """Char-level LM with: |
| 102 | + - Standard learned embedding (subspace defined by the input vocabulary) |
| 103 | + - CRT-Fibonacci positional encoding |
| 104 | + - SubstrateSimilarityAttention (L1-distance in K-dim Fibonacci basis) |
| 105 | + - FibGen FFN weights |
| 106 | + - Tied LM head |
| 107 | + """ |
| 108 | + |
| 109 | + def __init__(self, vocab_size: int, d_model: int, n_blocks: int, |
| 110 | + seq_len: int, K: int = 32, fibgen_K: int = 32, |
| 111 | + mode: str = "cross"): |
| 112 | + super().__init__() |
| 113 | + self.seq_len = seq_len |
| 114 | + self.K = K |
| 115 | + self.embed = nn.Embedding(vocab_size, d_model) |
| 116 | + pe = self._crt_pe(seq_len, d_model) |
| 117 | + self.register_buffer("pe", pe) |
| 118 | + self.blocks = nn.ModuleList([ |
| 119 | + SubsimBlock(d_model, seq_len, K=K, fibgen_K=fibgen_K, mode=mode) |
| 120 | + for _ in range(n_blocks) |
| 121 | + ]) |
| 122 | + self.ln_f = nn.LayerNorm(d_model) |
| 123 | + self.head = nn.Linear(d_model, vocab_size, bias=False) |
| 124 | + self.head.weight = self.embed.weight |
| 125 | + |
| 126 | + @staticmethod |
| 127 | + def _crt_pe(seq_len: int, d_model: int) -> torch.Tensor: |
| 128 | + pe = torch.zeros(seq_len, d_model) |
| 129 | + pos = torch.arange(0, seq_len, dtype=torch.float) |
| 130 | + moduli = [5, 8, 13, 21, 34, 55, 89, 144] |
| 131 | + n_pairs = d_model // 2 |
| 132 | + for i in range(n_pairs): |
| 133 | + m = moduli[i % len(moduli)] |
| 134 | + angle = 2 * math.pi * (pos % m) / m |
| 135 | + pe[:, 2 * i] = torch.sin(angle) |
| 136 | + pe[:, 2 * i + 1] = torch.cos(angle) |
| 137 | + return pe |
| 138 | + |
| 139 | + def forward(self, token_ids): |
| 140 | + B, T = token_ids.shape |
| 141 | + h = self.embed(token_ids) + self.pe[:T] |
| 142 | + for block in self.blocks: |
| 143 | + h = block(h) |
| 144 | + h = self.ln_f(h) |
| 145 | + return self.head(h) |
| 146 | + |
| 147 | + def storage_summary(self): |
| 148 | + stored = 0 |
| 149 | + dense_eq = 0 |
| 150 | + for m in self.modules(): |
| 151 | + if isinstance(m, FibGenLinear): |
| 152 | + stored += m.n_stored_params |
| 153 | + dense_eq += m.n_dense_equivalent_params |
| 154 | + for n, p in self.named_parameters(): |
| 155 | + # Approximation: any param not inside a FibGen counts as itself. |
| 156 | + # (The embedding and LayerNorms are intentionally not compressed.) |
| 157 | + if not any(s in n for s in ("W_sig", "W_v", "W_out", ".w1.", ".w2.")): |
| 158 | + stored += p.numel() |
| 159 | + dense_eq += p.numel() |
| 160 | + return {"stored": stored, "dense_equivalent": dense_eq, |
| 161 | + "compression": dense_eq / max(stored, 1)} |
0 commit comments