|
| 1 | +"""Does substrate-Q resample stack on top of the v0.1 K + S-MOD + V win? |
| 2 | +
|
| 3 | +The v0.1 chapter shipped three stacked substrate-attention components: |
| 4 | + - K = CRT-Fibonacci substrate (no learnable W_K) |
| 5 | + - softmax → S-MOD α=1.0 (off-attractor weights dampened) |
| 6 | + - V = substrate_resample(x @ W_v) post-projection (off-attractor V mags dampened) |
| 7 | +
|
| 8 | +Q is the last unmodified component. The V finding's mechanism was |
| 9 | +"modulation > replacement" — keep the learned W, apply substrate as |
| 10 | +post-projection dampening. The natural Q recipe mirrors it: |
| 11 | +
|
| 12 | + Q1 (resample): q = substrate_resample(x @ W_q) |
| 13 | +
|
| 14 | +If the same modulation pattern generalizes to Q, that's a 4th |
| 15 | +stacked substrate-component — every attention primitive now substrate- |
| 16 | +aware. If it doesn't, we learn whether the V recipe was specific to |
| 17 | +the value path or whether it's a general "post-projection modulation" |
| 18 | +principle. |
| 19 | +
|
| 20 | +Three Q variants tested: |
| 21 | + Q0 (baseline): q = x @ W_q (current production) |
| 22 | + Q1 (resample): q = substrate_resample(x @ W_q) (post-projection snap) |
| 23 | + Q2 (modulate): q = (x @ W_q) * (1 + γ·near_attractor_signal(x)) |
| 24 | + (input-conditional) |
| 25 | +
|
| 26 | +3 seeds on TinyShakespeare with S-MOD α=1.0, substrate-V (V1) already |
| 27 | +active. Q is the only thing varying. |
| 28 | +""" |
| 29 | + |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +import argparse |
| 33 | +import json |
| 34 | +import random |
| 35 | +import statistics |
| 36 | +from pathlib import Path |
| 37 | + |
| 38 | +import torch |
| 39 | +import torch.nn as nn |
| 40 | +import torch.nn.functional as F |
| 41 | + |
| 42 | +from torch_4way import lcg, make_matrix, crt_pe, build_vocab |
| 43 | +from torch_substrate_softmax import ( |
| 44 | + attractor_distance, softmax_smod, |
| 45 | +) |
| 46 | +from torch_substrate_v import substrate_resample, near_attractor_signal |
| 47 | + |
| 48 | + |
| 49 | +class AttentionL1QV(nn.Module): |
| 50 | + """L1 multi-head + S-MOD softmax + substrate-V (V1) + pluggable Q variant. |
| 51 | +
|
| 52 | + This is the v0.1 production stack with one variable: how Q is built. |
| 53 | + """ |
| 54 | + def __init__(self, d_model, n_heads, seq_len, seed, |
| 55 | + q_variant="Q0", alpha=1.0, gamma=0.2): |
| 56 | + super().__init__() |
| 57 | + assert d_model % n_heads == 0 |
| 58 | + self.d_model, self.n_heads = d_model, n_heads |
| 59 | + self.d_head = d_model // n_heads |
| 60 | + self.q_variant = q_variant |
| 61 | + self.alpha = alpha |
| 62 | + self.gamma = gamma |
| 63 | + s = seed + 11 |
| 64 | + W_q, s = make_matrix(d_model, d_model, 0.3, s) |
| 65 | + W_v, s = make_matrix(d_model, d_model, 0.3, s) |
| 66 | + W_o, s = make_matrix(d_model, d_model, 0.3, s) |
| 67 | + self.W_q = nn.Parameter(W_q) |
| 68 | + self.W_v = nn.Parameter(W_v) |
| 69 | + self.W_o = nn.Parameter(W_o) |
| 70 | + pe_full = crt_pe(seq_len, d_model) |
| 71 | + pe_per_head = pe_full.view(seq_len, n_heads, |
| 72 | + self.d_head).transpose(0, 1) |
| 73 | + self.register_buffer("K_const_mh", pe_per_head) |
| 74 | + self.rng_state = s |
| 75 | + |
| 76 | + def forward(self, x): |
| 77 | + T, D = x.shape |
| 78 | + H, dh = self.n_heads, self.d_head |
| 79 | + # Q variants — this is the experimental axis. |
| 80 | + q_proj = x @ self.W_q |
| 81 | + if self.q_variant == "Q0": |
| 82 | + q_full = q_proj |
| 83 | + elif self.q_variant == "Q1": |
| 84 | + q_full = substrate_resample(q_proj) |
| 85 | + elif self.q_variant == "Q2": |
| 86 | + gate = near_attractor_signal(x) |
| 87 | + q_full = q_proj * (1.0 + self.gamma * gate) |
| 88 | + else: |
| 89 | + raise ValueError(self.q_variant) |
| 90 | + # V always uses substrate_resample (V1, production default from v0.1). |
| 91 | + v_full = substrate_resample(x @ self.W_v) |
| 92 | + q = q_full.view(T, H, dh).transpose(0, 1) |
| 93 | + v = v_full.view(T, H, dh).transpose(0, 1) |
| 94 | + k = self.K_const_mh |
| 95 | + scores = (q @ k.transpose(-2, -1)) / (dh ** 0.5) |
| 96 | + attn = softmax_smod(scores, dim=-1, alpha=self.alpha) |
| 97 | + out = attn @ v |
| 98 | + out = out.transpose(0, 1).contiguous().view(T, D) |
| 99 | + return out @ self.W_o |
| 100 | + |
| 101 | + |
| 102 | +class BlockQ(nn.Module): |
| 103 | + def __init__(self, d_model, n_heads, ff_dim, seq_len, seed, |
| 104 | + q_variant, alpha, gamma): |
| 105 | + super().__init__() |
| 106 | + self.attn = AttentionL1QV(d_model, n_heads, seq_len, seed, |
| 107 | + q_variant, alpha, gamma) |
| 108 | + s = self.attn.rng_state |
| 109 | + self.ln1_g = nn.Parameter(torch.ones(d_model)) |
| 110 | + self.ln1_b = nn.Parameter(torch.zeros(d_model)) |
| 111 | + W_up, s = make_matrix(d_model, ff_dim, 0.3, s + 13) |
| 112 | + W_down, s = make_matrix(ff_dim, d_model, 0.3, s) |
| 113 | + self.ff_up = nn.Parameter(W_up) |
| 114 | + self.ff_up_b = nn.Parameter(torch.zeros(ff_dim)) |
| 115 | + self.ff_down = nn.Parameter(W_down) |
| 116 | + self.ff_down_b = nn.Parameter(torch.zeros(d_model)) |
| 117 | + self.ln2_g = nn.Parameter(torch.ones(d_model)) |
| 118 | + self.ln2_b = nn.Parameter(torch.zeros(d_model)) |
| 119 | + self.rng_state = s |
| 120 | + |
| 121 | + def forward(self, x): |
| 122 | + attn_out = self.attn(x) |
| 123 | + x_post_attn = x + attn_out |
| 124 | + normed1 = F.layer_norm(x_post_attn, (x.size(-1),), |
| 125 | + weight=self.ln1_g, bias=self.ln1_b) |
| 126 | + up = normed1 @ self.ff_up + self.ff_up_b |
| 127 | + activated = F.relu(up) |
| 128 | + down = activated @ self.ff_down + self.ff_down_b |
| 129 | + x_post_ff = x_post_attn + down |
| 130 | + return F.layer_norm(x_post_ff, (x.size(-1),), |
| 131 | + weight=self.ln2_g, bias=self.ln2_b) |
| 132 | + |
| 133 | + |
| 134 | +class ModelQ(nn.Module): |
| 135 | + def __init__(self, vocab, d_model, n_heads, ff_dim, seq_len, n_blocks, |
| 136 | + seed, q_variant, alpha, gamma): |
| 137 | + super().__init__() |
| 138 | + s = seed |
| 139 | + E, s = make_matrix(vocab, d_model, 0.3, s) |
| 140 | + self.embedding = nn.Parameter(E) |
| 141 | + self.register_buffer("pe_table", crt_pe(seq_len, d_model)) |
| 142 | + self.blocks = nn.ModuleList() |
| 143 | + for i in range(n_blocks): |
| 144 | + b = BlockQ(d_model, n_heads, ff_dim, seq_len, |
| 145 | + s + 100 * (i + 1), q_variant, alpha, gamma) |
| 146 | + self.blocks.append(b) |
| 147 | + s = b.rng_state |
| 148 | + W_head, _ = make_matrix(d_model, vocab, 0.3, s + 17) |
| 149 | + self.head = nn.Parameter(W_head) |
| 150 | + self.head_b = nn.Parameter(torch.zeros(vocab)) |
| 151 | + |
| 152 | + def forward(self, token_ids): |
| 153 | + x = self.embedding[token_ids] + self.pe_table[:token_ids.size(0)] |
| 154 | + for b in self.blocks: |
| 155 | + x = b(x) |
| 156 | + return x @ self.head + self.head_b |
| 157 | + |
| 158 | + |
| 159 | +def train_one(q_variant, train_ids, val_ids, vocab_size, args, seed): |
| 160 | + torch.manual_seed(seed) |
| 161 | + random.seed(seed) |
| 162 | + model = ModelQ(vocab_size, args.d_model, args.n_heads, args.ff_dim, |
| 163 | + args.seq_len, args.n_blocks, seed, q_variant, |
| 164 | + args.alpha, args.gamma) |
| 165 | + opt = torch.optim.AdamW(model.parameters(), lr=args.lr, |
| 166 | + betas=(0.9, 0.999), eps=1e-8) |
| 167 | + n_train, n_val = len(train_ids), len(val_ids) |
| 168 | + train_t = torch.tensor(train_ids, dtype=torch.long) |
| 169 | + val_t = torch.tensor(val_ids, dtype=torch.long) |
| 170 | + for step in range(args.steps): |
| 171 | + start = random.randint(0, n_train - args.seq_len - 2) |
| 172 | + w = train_t[start:start + args.seq_len] |
| 173 | + t = train_t[start + 1:start + 1 + args.seq_len] |
| 174 | + loss = F.cross_entropy(model(w), t) |
| 175 | + opt.zero_grad() |
| 176 | + loss.backward() |
| 177 | + opt.step() |
| 178 | + model.eval() |
| 179 | + vls = [] |
| 180 | + with torch.no_grad(): |
| 181 | + for _ in range(30): |
| 182 | + vs = random.randint(0, n_val - args.seq_len - 2) |
| 183 | + vw = val_t[vs:vs + args.seq_len] |
| 184 | + vt = val_t[vs + 1:vs + 1 + args.seq_len] |
| 185 | + vls.append(F.cross_entropy(model(vw), vt).item()) |
| 186 | + return sum(vls) / len(vls) |
| 187 | + |
| 188 | + |
| 189 | +def main(): |
| 190 | + parser = argparse.ArgumentParser() |
| 191 | + parser.add_argument("--seeds", type=str, default="42,7,123") |
| 192 | + parser.add_argument("--steps", type=int, default=1500) |
| 193 | + parser.add_argument("--lr", type=float, default=0.005) |
| 194 | + parser.add_argument("--seq-len", type=int, default=32) |
| 195 | + parser.add_argument("--d-model", type=int, default=32) |
| 196 | + parser.add_argument("--n-heads", type=int, default=4) |
| 197 | + parser.add_argument("--ff-dim", type=int, default=64) |
| 198 | + parser.add_argument("--n-blocks", type=int, default=4) |
| 199 | + parser.add_argument("--alpha", type=float, default=1.0) |
| 200 | + parser.add_argument("--gamma", type=float, default=0.2) |
| 201 | + parser.add_argument("--variants", type=str, default="Q0,Q1,Q2") |
| 202 | + parser.add_argument("--out", type=str, |
| 203 | + default="results_torch_substrate_q.json") |
| 204 | + args = parser.parse_args() |
| 205 | + |
| 206 | + corpus = (Path(__file__).parent.parent / "transformerless_lm" |
| 207 | + / "tinyshakespeare.txt").read_text() |
| 208 | + chars, lookup = build_vocab(corpus) |
| 209 | + vocab_size = len(chars) |
| 210 | + ids = [lookup[c] for c in corpus] |
| 211 | + split = int(len(ids) * 0.9) |
| 212 | + train_ids, val_ids = ids[:split], ids[split:] |
| 213 | + seeds = [int(s) for s in args.seeds.split(",")] |
| 214 | + variants = args.variants.split(",") |
| 215 | + |
| 216 | + print("=== Substrate-Q on L1-MH + S-MOD + V1 (TinyShakespeare) ===") |
| 217 | + print(f"variants={variants} seeds={seeds} steps={args.steps} " |
| 218 | + f"α={args.alpha} γ={args.gamma}\n", flush=True) |
| 219 | + |
| 220 | + results = {} |
| 221 | + for v in variants: |
| 222 | + vals = [] |
| 223 | + for seed in seeds: |
| 224 | + vm = train_one(v, train_ids, val_ids, vocab_size, args, seed) |
| 225 | + vals.append(vm) |
| 226 | + print(f" {v} seed={seed} val={vm:.4f}", flush=True) |
| 227 | + results[v] = { |
| 228 | + "vals": vals, |
| 229 | + "mean": sum(vals) / len(vals), |
| 230 | + "std": statistics.stdev(vals) if len(vals) > 1 else 0.0, |
| 231 | + } |
| 232 | + print(f"[{v}] mean val={results[v]['mean']:.4f} " |
| 233 | + f"std={results[v]['std']:.4f}\n", flush=True) |
| 234 | + |
| 235 | + print("=== Summary ===") |
| 236 | + base = results[variants[0]]["mean"] |
| 237 | + print(f"{'variant':>8} {'mean val':>10} {'std':>7} {'vs Q0':>8}") |
| 238 | + for v in variants: |
| 239 | + m = results[v]["mean"] |
| 240 | + rel = (m - base) / base * 100 |
| 241 | + marker = "—" if v == variants[0] else f"{rel:+.2f}%" |
| 242 | + print(f"{v:>8} {m:>10.4f} {results[v]['std']:>7.4f} {marker:>8}") |
| 243 | + best = min(variants, key=lambda v: results[v]["mean"]) |
| 244 | + print(f"\nBest: {best} ({results[best]['mean']:.4f})") |
| 245 | + |
| 246 | + out_path = Path(__file__).parent / args.out |
| 247 | + with open(out_path, "w") as f: |
| 248 | + json.dump({"results": results, "config": vars(args), |
| 249 | + "best": best}, f, indent=2, default=float) |
| 250 | + print(f"Wrote {out_path}") |
| 251 | + |
| 252 | + |
| 253 | +if __name__ == "__main__": |
| 254 | + main() |
0 commit comments