|
| 1 | +"""4-way attention A/B in PyTorch. |
| 2 | +
|
| 3 | +Reproduce the substrate-attention experiment from |
| 4 | +examples/prometheus_attention_4way.omc. Same architecture, same |
| 5 | +task, same seed semantics (LCG-ported init for fair comparison). |
| 6 | +
|
| 7 | +If PyTorch shows the same monotonic substrate-ladder result (L3 > |
| 8 | +L2 > L1 > L0), the win is cross-framework. If it doesn't, the OMC |
| 9 | +result was specific to our implementation. |
| 10 | +
|
| 11 | +Variants: |
| 12 | + L0: standard QKV (learned matrices) |
| 13 | + L1: K = CRT-PE (substrate), Q + V learned |
| 14 | + L2: K, Q = CRT-PE; V learned |
| 15 | + L3: K, Q = CRT-PE; V = identity (parameter-free attention block) |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import argparse |
| 21 | +import json |
| 22 | +import math |
| 23 | +import statistics |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +import torch |
| 27 | +import torch.nn as nn |
| 28 | +import torch.nn.functional as F |
| 29 | + |
| 30 | + |
| 31 | +# ---- Reproduce OMC's LCG init for fair comparison ---- |
| 32 | + |
| 33 | +def lcg(state: int) -> int: |
| 34 | + return (state * 1103515245 + 12345) % 2147483648 |
| 35 | + |
| 36 | + |
| 37 | +def make_matrix(rows: int, cols: int, bound: float, state: int): |
| 38 | + """Bit-identical port of _prom_random_matrix from prometheus.omc.""" |
| 39 | + m = torch.empty(rows, cols) |
| 40 | + s = state |
| 41 | + for i in range(rows): |
| 42 | + for j in range(cols): |
| 43 | + s = lcg(s) |
| 44 | + r = s / 2147483648.0 |
| 45 | + m[i, j] = (r * 2.0 - 1.0) * bound |
| 46 | + return m, s |
| 47 | + |
| 48 | + |
| 49 | +# ---- CRT-Fibonacci positional encoding (same moduli as OMC) ---- |
| 50 | + |
| 51 | +FIB_MODULI = [5, 8, 13, 21, 34, 55, 89, 144] |
| 52 | + |
| 53 | + |
| 54 | +def crt_pe(seq_len: int, d_model: int) -> torch.Tensor: |
| 55 | + pe = torch.zeros(seq_len, d_model) |
| 56 | + n_pairs = d_model // 2 |
| 57 | + for i in range(n_pairs): |
| 58 | + m = FIB_MODULI[i % len(FIB_MODULI)] |
| 59 | + for pos in range(seq_len): |
| 60 | + residue = pos % m |
| 61 | + angle = 2.0 * math.pi * residue / m |
| 62 | + pe[pos, 2 * i] = math.sin(angle) |
| 63 | + pe[pos, 2 * i + 1] = math.cos(angle) |
| 64 | + return pe |
| 65 | + |
| 66 | + |
| 67 | +# ---- Attention variants ---- |
| 68 | + |
| 69 | + |
| 70 | +class AttentionL0(nn.Module): |
| 71 | + """Standard QKV — learned matrices.""" |
| 72 | + def __init__(self, d_model: int, seq_len: int, seed: int): |
| 73 | + super().__init__() |
| 74 | + W_q, s = make_matrix(d_model, d_model, 0.3, seed + 11) |
| 75 | + W_k, s = make_matrix(d_model, d_model, 0.3, s) |
| 76 | + W_v, s = make_matrix(d_model, d_model, 0.3, s) |
| 77 | + self.W_q = nn.Parameter(W_q) |
| 78 | + self.W_k = nn.Parameter(W_k) |
| 79 | + self.W_v = nn.Parameter(W_v) |
| 80 | + self.rng_state = s |
| 81 | + |
| 82 | + def forward(self, x): |
| 83 | + q = x @ self.W_q |
| 84 | + k = x @ self.W_k |
| 85 | + v = x @ self.W_v |
| 86 | + scores = q @ k.T |
| 87 | + attn = F.softmax(scores, dim=-1) |
| 88 | + return attn @ v |
| 89 | + |
| 90 | + |
| 91 | +class AttentionL1(nn.Module): |
| 92 | + """K = CRT-PE; Q + V learned.""" |
| 93 | + def __init__(self, d_model: int, seq_len: int, seed: int): |
| 94 | + super().__init__() |
| 95 | + W_q, s = make_matrix(d_model, d_model, 0.3, seed + 11) |
| 96 | + W_v, s = make_matrix(d_model, d_model, 0.3, s) |
| 97 | + self.W_q = nn.Parameter(W_q) |
| 98 | + self.W_v = nn.Parameter(W_v) |
| 99 | + self.register_buffer("K_const", crt_pe(seq_len, d_model)) |
| 100 | + self.rng_state = s |
| 101 | + |
| 102 | + def forward(self, x): |
| 103 | + q = x @ self.W_q |
| 104 | + v = x @ self.W_v |
| 105 | + k = self.K_const |
| 106 | + scores = q @ k.T |
| 107 | + attn = F.softmax(scores, dim=-1) |
| 108 | + return attn @ v |
| 109 | + |
| 110 | + |
| 111 | +class AttentionL2(nn.Module): |
| 112 | + """K, Q = CRT-PE; only V learned.""" |
| 113 | + def __init__(self, d_model: int, seq_len: int, seed: int): |
| 114 | + super().__init__() |
| 115 | + W_v, s = make_matrix(d_model, d_model, 0.3, seed + 11) |
| 116 | + self.W_v = nn.Parameter(W_v) |
| 117 | + pe = crt_pe(seq_len, d_model) |
| 118 | + self.register_buffer("K_const", pe) |
| 119 | + self.register_buffer("Q_const", pe) |
| 120 | + self.rng_state = s |
| 121 | + |
| 122 | + def forward(self, x): |
| 123 | + v = x @ self.W_v |
| 124 | + scores = self.Q_const @ self.K_const.T |
| 125 | + attn = F.softmax(scores, dim=-1) |
| 126 | + return attn @ v |
| 127 | + |
| 128 | + |
| 129 | +class AttentionL3(nn.Module): |
| 130 | + """K, Q = CRT-PE; V = identity (parameter-free).""" |
| 131 | + def __init__(self, d_model: int, seq_len: int, seed: int): |
| 132 | + super().__init__() |
| 133 | + pe = crt_pe(seq_len, d_model) |
| 134 | + self.register_buffer("K_const", pe) |
| 135 | + self.register_buffer("Q_const", pe) |
| 136 | + self.rng_state = seed + 11 |
| 137 | + |
| 138 | + def forward(self, x): |
| 139 | + scores = self.Q_const @ self.K_const.T |
| 140 | + attn = F.softmax(scores, dim=-1) |
| 141 | + return attn @ x |
| 142 | + |
| 143 | + |
| 144 | +# ---- Full transformer block (same for all variants) ---- |
| 145 | + |
| 146 | + |
| 147 | +class TransformerModel(nn.Module): |
| 148 | + def __init__(self, variant: str, vocab: int, d_model: int, ff_dim: int, |
| 149 | + seq_len: int, seed: int): |
| 150 | + super().__init__() |
| 151 | + s = seed |
| 152 | + E, s = make_matrix(vocab, d_model, 0.3, s) |
| 153 | + self.embedding = nn.Parameter(E) |
| 154 | + |
| 155 | + attn_cls = {"L0": AttentionL0, "L1": AttentionL1, |
| 156 | + "L2": AttentionL2, "L3": AttentionL3}[variant] |
| 157 | + self.attn = attn_cls(d_model, seq_len, s) |
| 158 | + s = self.attn.rng_state |
| 159 | + |
| 160 | + self.ln1_g = nn.Parameter(torch.ones(d_model)) |
| 161 | + self.ln1_b = nn.Parameter(torch.zeros(d_model)) |
| 162 | + |
| 163 | + W_up, s = make_matrix(d_model, ff_dim, 0.3, s + 13) |
| 164 | + W_down, s = make_matrix(ff_dim, d_model, 0.3, s) |
| 165 | + self.ff_up = nn.Parameter(W_up) |
| 166 | + self.ff_up_b = nn.Parameter(torch.zeros(ff_dim)) |
| 167 | + self.ff_down = nn.Parameter(W_down) |
| 168 | + self.ff_down_b = nn.Parameter(torch.zeros(d_model)) |
| 169 | + |
| 170 | + self.ln2_g = nn.Parameter(torch.ones(d_model)) |
| 171 | + self.ln2_b = nn.Parameter(torch.zeros(d_model)) |
| 172 | + |
| 173 | + W_head, _ = make_matrix(d_model, vocab, 0.3, s + 17) |
| 174 | + self.head = nn.Parameter(W_head) |
| 175 | + self.head_b = nn.Parameter(torch.zeros(vocab)) |
| 176 | + |
| 177 | + # Precompute CRT-PE for the embed-side position add. |
| 178 | + self.register_buffer("pe_table", crt_pe(seq_len, d_model)) |
| 179 | + |
| 180 | + def forward(self, token_ids: torch.Tensor) -> torch.Tensor: |
| 181 | + # token_ids: [N] |
| 182 | + x = self.embedding[token_ids] # [N, d] |
| 183 | + x = x + self.pe_table[:x.size(0)] # add CRT-PE |
| 184 | + attn_out = self.attn(x) # [N, d] |
| 185 | + x_post_attn = x + attn_out |
| 186 | + normed1 = F.layer_norm(x_post_attn, (x.size(-1),), |
| 187 | + weight=self.ln1_g, bias=self.ln1_b) |
| 188 | + up = normed1 @ self.ff_up + self.ff_up_b |
| 189 | + activated = F.relu(up) |
| 190 | + down = activated @ self.ff_down + self.ff_down_b |
| 191 | + x_post_ff = x_post_attn + down |
| 192 | + normed2 = F.layer_norm(x_post_ff, (x.size(-1),), |
| 193 | + weight=self.ln2_g, bias=self.ln2_b) |
| 194 | + return normed2 @ self.head + self.head_b # [N, vocab] |
| 195 | + |
| 196 | + |
| 197 | +# ---- Training loop ---- |
| 198 | + |
| 199 | + |
| 200 | +def build_vocab(text: str): |
| 201 | + chars = [] |
| 202 | + lookup = {} |
| 203 | + for ch in text: |
| 204 | + if ch not in lookup: |
| 205 | + lookup[ch] = len(chars) |
| 206 | + chars.append(ch) |
| 207 | + return chars, lookup |
| 208 | + |
| 209 | + |
| 210 | +def train_arm(variant: str, ids: list, vocab_size: int, seq_len: int, |
| 211 | + d_model: int, ff_dim: int, lr: float, steps: int, seed: int): |
| 212 | + torch.manual_seed(seed) |
| 213 | + model = TransformerModel(variant, vocab_size, d_model, ff_dim, seq_len, seed) |
| 214 | + optimizer = torch.optim.AdamW(model.parameters(), lr=lr, |
| 215 | + betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0) |
| 216 | + n_windows = len(ids) - seq_len - 1 |
| 217 | + ids_tensor = torch.tensor(ids, dtype=torch.long) |
| 218 | + tail_losses = [] |
| 219 | + for step in range(steps): |
| 220 | + start = step % n_windows |
| 221 | + window = ids_tensor[start:start + seq_len] |
| 222 | + targets = ids_tensor[start + 1:start + 1 + seq_len] |
| 223 | + logits = model(window) |
| 224 | + loss = F.cross_entropy(logits, targets, reduction="mean") |
| 225 | + optimizer.zero_grad() |
| 226 | + loss.backward() |
| 227 | + optimizer.step() |
| 228 | + if step >= steps - 10: |
| 229 | + tail_losses.append(loss.item()) |
| 230 | + n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) |
| 231 | + return sum(tail_losses) / len(tail_losses), n_params |
| 232 | + |
| 233 | + |
| 234 | +def main(): |
| 235 | + parser = argparse.ArgumentParser() |
| 236 | + parser.add_argument("--seeds", type=str, default="42,7,123") |
| 237 | + parser.add_argument("--steps", type=int, default=250) |
| 238 | + parser.add_argument("--lr", type=float, default=0.02) |
| 239 | + parser.add_argument("--out", type=str, default="results_torch_4way.json") |
| 240 | + args = parser.parse_args() |
| 241 | + |
| 242 | + text = "the quick brown fox jumps over the lazy dog and the dog sleeps in the sun" |
| 243 | + chars, lookup = build_vocab(text) |
| 244 | + vocab_size = len(chars) |
| 245 | + ids = [lookup[c] for c in text] |
| 246 | + seq_len = 8 |
| 247 | + d_model = 16 |
| 248 | + ff_dim = 32 |
| 249 | + seeds = [int(s) for s in args.seeds.split(",")] |
| 250 | + variants = ["L0", "L1", "L2", "L3"] |
| 251 | + |
| 252 | + print("=== PyTorch 4-way attention A/B ===") |
| 253 | + print(f"setup: corpus={len(text)} vocab={vocab_size} seq={seq_len} " |
| 254 | + f"d={d_model} ff={ff_dim}") |
| 255 | + print(f" steps={args.steps} lr={args.lr} seeds={seeds}\n", flush=True) |
| 256 | + |
| 257 | + results = {} |
| 258 | + for v in variants: |
| 259 | + losses = [] |
| 260 | + for seed in seeds: |
| 261 | + loss, n_params = train_arm(v, ids, vocab_size, seq_len, |
| 262 | + d_model, ff_dim, args.lr, args.steps, seed) |
| 263 | + losses.append(loss) |
| 264 | + results[v] = {"losses": losses, "n_params": n_params, |
| 265 | + "mean": sum(losses) / len(losses), |
| 266 | + "std": statistics.stdev(losses) if len(losses) > 1 else 0.0} |
| 267 | + print(f"[{v}] params={n_params:4d} mean={results[v]['mean']:.4f} " |
| 268 | + f"std={results[v]['std']:.4f} per-seed={[f'{x:.3f}' for x in losses]}", |
| 269 | + flush=True) |
| 270 | + |
| 271 | + print("\n=== Summary vs L0 ===") |
| 272 | + base_mean = results["L0"]["mean"] |
| 273 | + base_losses = results["L0"]["losses"] |
| 274 | + for v in variants: |
| 275 | + wins = sum(1 for x, b in zip(results[v]["losses"], base_losses) if x < b) |
| 276 | + rel = (results[v]["mean"] - base_mean) / base_mean * 100 |
| 277 | + marker = "—" if v == "L0" else f"{rel:+.1f}%" |
| 278 | + print(f" {v}: mean={results[v]['mean']:.4f} vs L0: {marker:>8} " |
| 279 | + f"wins={wins}/{len(base_losses)}") |
| 280 | + |
| 281 | + print("\n=== Cross-framework comparison ===") |
| 282 | + print("OMC result (from examples/prometheus_attention_4way.omc):") |
| 283 | + print(" L0=2.576 L1=2.506 (-2.7%) L2=2.157 (-16.3%) L3=2.023 (-21.5%)") |
| 284 | + print("PyTorch result (this run):") |
| 285 | + for v in variants: |
| 286 | + print(f" {v}={results[v]['mean']:.3f}", end=" ") |
| 287 | + print() |
| 288 | + |
| 289 | + # Verdict |
| 290 | + l0 = results["L0"]["mean"] |
| 291 | + l3 = results["L3"]["mean"] |
| 292 | + if l3 < l0: |
| 293 | + delta_pct = (l3 - l0) / l0 * 100 |
| 294 | + print(f"\n[CROSS-FRAMEWORK WIN] L3 beats L0 by {delta_pct:.1f}% in PyTorch too.") |
| 295 | + print(" Substrate-as-attention-replacement validated across runtimes.") |
| 296 | + else: |
| 297 | + delta_pct = (l3 - l0) / l0 * 100 |
| 298 | + print(f"\n[OMC-SPECIFIC] L3 LOSES to L0 by {delta_pct:.1f}% in PyTorch.") |
| 299 | + print(" OMC result didn't replicate — investigate runtime-specific factors.") |
| 300 | + |
| 301 | + out_path = Path(__file__).parent / args.out |
| 302 | + with open(out_path, "w") as f: |
| 303 | + json.dump({ |
| 304 | + "results": {k: {"losses": v["losses"], "n_params": v["n_params"], |
| 305 | + "mean": v["mean"], "std": v["std"]} |
| 306 | + for k, v in results.items()}, |
| 307 | + "config": {"seeds": seeds, "steps": args.steps, "lr": args.lr, |
| 308 | + "vocab": vocab_size, "d_model": d_model, "ff_dim": ff_dim, |
| 309 | + "seq_len": seq_len}, |
| 310 | + }, f, indent=2) |
| 311 | + print(f"\nWrote {out_path}") |
| 312 | + |
| 313 | + |
| 314 | +if __name__ == "__main__": |
| 315 | + main() |
0 commit comments