|
| 1 | +"""Multi-head L0 vs L1 at TinyShakespeare scale. |
| 2 | +
|
| 3 | +The production-shape validation. Yesterday: single-head L1 wins -8.0% val. |
| 4 | +4-block-stacked single-head L1 wins -1.9% val. |
| 5 | +
|
| 6 | +This run: MULTI-HEAD (n_heads=4). Standard transformer pattern. If L1 |
| 7 | +still wins here, substrate-K is the production architecture |
| 8 | +recommendation. If L0 catches up, multi-head's content-keying capacity |
| 9 | +absorbed the substrate's advantage. |
| 10 | +
|
| 11 | +Setup: |
| 12 | + - TinyShakespeare 90/10 train/val |
| 13 | + - d_model=32, n_heads=4 (d_head=8), seq_len=32, ff=64 |
| 14 | + - 1500 steps, AdamW lr=0.005 |
| 15 | + - 3 seeds (matches yesterday's pattern) |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import argparse |
| 21 | +import json |
| 22 | +import random |
| 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 | +from torch_4way import lcg, make_matrix, crt_pe, build_vocab |
| 31 | + |
| 32 | + |
| 33 | +# ---- Multi-head attention variants ---- |
| 34 | + |
| 35 | + |
| 36 | +class AttentionL0_MH(nn.Module): |
| 37 | + """Standard multi-head: learned Q, K, V per head, then output projection.""" |
| 38 | + def __init__(self, d_model: int, n_heads: int, seq_len: int, seed: int): |
| 39 | + super().__init__() |
| 40 | + assert d_model % n_heads == 0 |
| 41 | + self.d_model = d_model |
| 42 | + self.n_heads = n_heads |
| 43 | + self.d_head = d_model // n_heads |
| 44 | + s = seed + 11 |
| 45 | + W_q, s = make_matrix(d_model, d_model, 0.3, s) |
| 46 | + W_k, s = make_matrix(d_model, d_model, 0.3, s) |
| 47 | + W_v, s = make_matrix(d_model, d_model, 0.3, s) |
| 48 | + W_o, s = make_matrix(d_model, d_model, 0.3, s) |
| 49 | + self.W_q = nn.Parameter(W_q) |
| 50 | + self.W_k = nn.Parameter(W_k) |
| 51 | + self.W_v = nn.Parameter(W_v) |
| 52 | + self.W_o = nn.Parameter(W_o) |
| 53 | + self.rng_state = s |
| 54 | + |
| 55 | + def forward(self, x): |
| 56 | + T, D = x.shape |
| 57 | + H, dh = self.n_heads, self.d_head |
| 58 | + q = (x @ self.W_q).view(T, H, dh).transpose(0, 1) # [H, T, dh] |
| 59 | + k = (x @ self.W_k).view(T, H, dh).transpose(0, 1) |
| 60 | + v = (x @ self.W_v).view(T, H, dh).transpose(0, 1) |
| 61 | + scores = (q @ k.transpose(-2, -1)) / (dh ** 0.5) # [H, T, T] |
| 62 | + attn = F.softmax(scores, dim=-1) |
| 63 | + out = attn @ v # [H, T, dh] |
| 64 | + out = out.transpose(0, 1).contiguous().view(T, D) # [T, D] |
| 65 | + return out @ self.W_o |
| 66 | + |
| 67 | + |
| 68 | +class AttentionL1_MH(nn.Module): |
| 69 | + """Multi-head substrate-K: K replaced by CRT-PE (same per-head, shared |
| 70 | + across all heads) + learned Q, V, output projection. Each head still |
| 71 | + has its own Q + V — that's where content-keying happens. K is fixed |
| 72 | + structural prior. |
| 73 | + """ |
| 74 | + def __init__(self, d_model: int, n_heads: int, seq_len: int, seed: int): |
| 75 | + super().__init__() |
| 76 | + assert d_model % n_heads == 0 |
| 77 | + self.d_model = d_model |
| 78 | + self.n_heads = n_heads |
| 79 | + self.d_head = d_model // n_heads |
| 80 | + s = seed + 11 |
| 81 | + W_q, s = make_matrix(d_model, d_model, 0.3, s) |
| 82 | + W_v, s = make_matrix(d_model, d_model, 0.3, s) |
| 83 | + W_o, s = make_matrix(d_model, d_model, 0.3, s) |
| 84 | + self.W_q = nn.Parameter(W_q) |
| 85 | + self.W_v = nn.Parameter(W_v) |
| 86 | + self.W_o = nn.Parameter(W_o) |
| 87 | + # Substrate K: build a per-head [seq_len, d_head] CRT-PE table. |
| 88 | + # Same CRT-PE matrix, sliced by head. |
| 89 | + pe_full = crt_pe(seq_len, d_model) # [T, D] |
| 90 | + pe_per_head = pe_full.view(seq_len, n_heads, |
| 91 | + self.d_head).transpose(0, 1) # [H, T, dh] |
| 92 | + self.register_buffer("K_const_mh", pe_per_head) |
| 93 | + self.rng_state = s |
| 94 | + |
| 95 | + def forward(self, x): |
| 96 | + T, D = x.shape |
| 97 | + H, dh = self.n_heads, self.d_head |
| 98 | + q = (x @ self.W_q).view(T, H, dh).transpose(0, 1) |
| 99 | + v = (x @ self.W_v).view(T, H, dh).transpose(0, 1) |
| 100 | + k = self.K_const_mh # [H, T, dh] |
| 101 | + scores = (q @ k.transpose(-2, -1)) / (dh ** 0.5) |
| 102 | + attn = F.softmax(scores, dim=-1) |
| 103 | + out = attn @ v |
| 104 | + out = out.transpose(0, 1).contiguous().view(T, D) |
| 105 | + return out @ self.W_o |
| 106 | + |
| 107 | + |
| 108 | +# ---- Transformer block + model ---- |
| 109 | + |
| 110 | + |
| 111 | +class TransformerBlockMH(nn.Module): |
| 112 | + def __init__(self, variant: str, d_model: int, n_heads: int, |
| 113 | + ff_dim: int, seq_len: int, seed: int): |
| 114 | + super().__init__() |
| 115 | + attn_cls = {"L0": AttentionL0_MH, "L1": AttentionL1_MH}[variant] |
| 116 | + self.attn = attn_cls(d_model, n_heads, seq_len, seed) |
| 117 | + s = self.attn.rng_state |
| 118 | + self.ln1_g = nn.Parameter(torch.ones(d_model)) |
| 119 | + self.ln1_b = nn.Parameter(torch.zeros(d_model)) |
| 120 | + W_up, s = make_matrix(d_model, ff_dim, 0.3, s + 13) |
| 121 | + W_down, s = make_matrix(ff_dim, d_model, 0.3, s) |
| 122 | + self.ff_up = nn.Parameter(W_up) |
| 123 | + self.ff_up_b = nn.Parameter(torch.zeros(ff_dim)) |
| 124 | + self.ff_down = nn.Parameter(W_down) |
| 125 | + self.ff_down_b = nn.Parameter(torch.zeros(d_model)) |
| 126 | + self.ln2_g = nn.Parameter(torch.ones(d_model)) |
| 127 | + self.ln2_b = nn.Parameter(torch.zeros(d_model)) |
| 128 | + self.rng_state = s |
| 129 | + |
| 130 | + def forward(self, x): |
| 131 | + attn_out = self.attn(x) |
| 132 | + x_post_attn = x + attn_out |
| 133 | + normed1 = F.layer_norm(x_post_attn, (x.size(-1),), |
| 134 | + weight=self.ln1_g, bias=self.ln1_b) |
| 135 | + up = normed1 @ self.ff_up + self.ff_up_b |
| 136 | + activated = F.relu(up) |
| 137 | + down = activated @ self.ff_down + self.ff_down_b |
| 138 | + x_post_ff = x_post_attn + down |
| 139 | + normed2 = F.layer_norm(x_post_ff, (x.size(-1),), |
| 140 | + weight=self.ln2_g, bias=self.ln2_b) |
| 141 | + return normed2 |
| 142 | + |
| 143 | + |
| 144 | +class MultiHeadModel(nn.Module): |
| 145 | + def __init__(self, variant: str, vocab: int, d_model: int, |
| 146 | + n_heads: int, ff_dim: int, seq_len: int, |
| 147 | + n_blocks: int, seed: int): |
| 148 | + super().__init__() |
| 149 | + s = seed |
| 150 | + E, s = make_matrix(vocab, d_model, 0.3, s) |
| 151 | + self.embedding = nn.Parameter(E) |
| 152 | + self.register_buffer("pe_table", crt_pe(seq_len, d_model)) |
| 153 | + self.blocks = nn.ModuleList() |
| 154 | + for i in range(n_blocks): |
| 155 | + block = TransformerBlockMH(variant, d_model, n_heads, ff_dim, |
| 156 | + seq_len, s + 100 * (i + 1)) |
| 157 | + self.blocks.append(block) |
| 158 | + s = block.rng_state |
| 159 | + W_head, _ = make_matrix(d_model, vocab, 0.3, s + 17) |
| 160 | + self.head = nn.Parameter(W_head) |
| 161 | + self.head_b = nn.Parameter(torch.zeros(vocab)) |
| 162 | + |
| 163 | + def forward(self, token_ids): |
| 164 | + x = self.embedding[token_ids] + self.pe_table[:token_ids.size(0)] |
| 165 | + for block in self.blocks: |
| 166 | + x = block(x) |
| 167 | + return x @ self.head + self.head_b |
| 168 | + |
| 169 | + |
| 170 | +# ---- Train with val split ---- |
| 171 | + |
| 172 | + |
| 173 | +def train_with_val(variant, train_ids, val_ids, vocab_size, seq_len, |
| 174 | + d_model, n_heads, ff_dim, n_blocks, lr, steps, seed, |
| 175 | + val_every=200, n_val_batches=30): |
| 176 | + torch.manual_seed(seed) |
| 177 | + random.seed(seed) |
| 178 | + model = MultiHeadModel(variant, vocab_size, d_model, n_heads, ff_dim, |
| 179 | + seq_len, n_blocks, seed) |
| 180 | + optimizer = torch.optim.AdamW(model.parameters(), lr=lr, |
| 181 | + betas=(0.9, 0.999), eps=1e-8) |
| 182 | + n_train = len(train_ids) |
| 183 | + n_val = len(val_ids) |
| 184 | + train_tensor = torch.tensor(train_ids, dtype=torch.long) |
| 185 | + val_tensor = torch.tensor(val_ids, dtype=torch.long) |
| 186 | + val_history = [] |
| 187 | + train_tail = [] |
| 188 | + for step in range(steps): |
| 189 | + start = random.randint(0, n_train - seq_len - 2) |
| 190 | + window = train_tensor[start:start + seq_len] |
| 191 | + targets = train_tensor[start + 1:start + 1 + seq_len] |
| 192 | + logits = model(window) |
| 193 | + loss = F.cross_entropy(logits, targets) |
| 194 | + optimizer.zero_grad() |
| 195 | + loss.backward() |
| 196 | + optimizer.step() |
| 197 | + if step >= steps - 50: |
| 198 | + train_tail.append(loss.item()) |
| 199 | + if (step + 1) % val_every == 0 or step == steps - 1: |
| 200 | + model.eval() |
| 201 | + with torch.no_grad(): |
| 202 | + val_losses = [] |
| 203 | + for _ in range(n_val_batches): |
| 204 | + vs = random.randint(0, n_val - seq_len - 2) |
| 205 | + vw = val_tensor[vs:vs + seq_len] |
| 206 | + vt = val_tensor[vs + 1:vs + 1 + seq_len] |
| 207 | + vl = F.cross_entropy(model(vw), vt) |
| 208 | + val_losses.append(vl.item()) |
| 209 | + val_history.append((step + 1, sum(val_losses) / len(val_losses))) |
| 210 | + model.train() |
| 211 | + train_mean = sum(train_tail) / len(train_tail) |
| 212 | + val_mean = val_history[-1][1] |
| 213 | + n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) |
| 214 | + return train_mean, val_mean, n_params |
| 215 | + |
| 216 | + |
| 217 | +def main(): |
| 218 | + parser = argparse.ArgumentParser() |
| 219 | + parser.add_argument("--seeds", type=str, default="42,7,123") |
| 220 | + parser.add_argument("--steps", type=int, default=1500) |
| 221 | + parser.add_argument("--lr", type=float, default=0.005) |
| 222 | + parser.add_argument("--seq-len", type=int, default=32) |
| 223 | + parser.add_argument("--d-model", type=int, default=32) |
| 224 | + parser.add_argument("--n-heads", type=int, default=4) |
| 225 | + parser.add_argument("--ff-dim", type=int, default=64) |
| 226 | + parser.add_argument("--n-blocks", type=int, default=4) |
| 227 | + parser.add_argument("--out", type=str, |
| 228 | + default="results_torch_multihead_tinyshakespeare.json") |
| 229 | + args = parser.parse_args() |
| 230 | + |
| 231 | + corpus_path = (Path(__file__).parent.parent |
| 232 | + / "transformerless_lm" / "tinyshakespeare.txt") |
| 233 | + text = corpus_path.read_text() |
| 234 | + chars, lookup = build_vocab(text) |
| 235 | + vocab_size = len(chars) |
| 236 | + ids = [lookup[c] for c in text] |
| 237 | + split = int(len(ids) * 0.9) |
| 238 | + train_ids = ids[:split] |
| 239 | + val_ids = ids[split:] |
| 240 | + seeds = [int(s) for s in args.seeds.split(",")] |
| 241 | + variants = ["L0", "L1"] |
| 242 | + |
| 243 | + print(f"=== Multi-head ({args.n_heads}h × {args.n_blocks}b) + TinyShakespeare ===") |
| 244 | + print(f"corpus: {len(text):,} chars; train {len(train_ids):,}; val {len(val_ids):,}") |
| 245 | + print(f"vocab={vocab_size} seq={args.seq_len} d_model={args.d_model} " |
| 246 | + f"n_heads={args.n_heads} d_head={args.d_model // args.n_heads} ff={args.ff_dim}") |
| 247 | + print(f"steps={args.steps} lr={args.lr} seeds={seeds}\n", flush=True) |
| 248 | + |
| 249 | + results = {} |
| 250 | + for v in variants: |
| 251 | + train_means, val_means = [], [] |
| 252 | + n_params = 0 |
| 253 | + for seed in seeds: |
| 254 | + tm, vm, n_params = train_with_val( |
| 255 | + v, train_ids, val_ids, vocab_size, args.seq_len, |
| 256 | + args.d_model, args.n_heads, args.ff_dim, args.n_blocks, |
| 257 | + args.lr, args.steps, seed, |
| 258 | + ) |
| 259 | + train_means.append(tm) |
| 260 | + val_means.append(vm) |
| 261 | + print(f" [{v}] seed={seed} train={tm:.4f} val={vm:.4f}", flush=True) |
| 262 | + results[v] = { |
| 263 | + "train": train_means, "val": val_means, "n_params": n_params, |
| 264 | + "train_mean": sum(train_means) / len(train_means), |
| 265 | + "val_mean": sum(val_means) / len(val_means), |
| 266 | + "val_std": statistics.stdev(val_means) if len(val_means) > 1 else 0.0, |
| 267 | + } |
| 268 | + print(f"[{v}] params={n_params:6d} " |
| 269 | + f"train={results[v]['train_mean']:.4f} " |
| 270 | + f"val={results[v]['val_mean']:.4f} (std={results[v]['val_std']:.4f})\n", |
| 271 | + flush=True) |
| 272 | + |
| 273 | + print("=== Multi-head + TinyShakespeare verdict ===") |
| 274 | + l0 = results["L0"] |
| 275 | + l1 = results["L1"] |
| 276 | + delta_val = l1["val_mean"] - l0["val_mean"] |
| 277 | + rel_val = delta_val / l0["val_mean"] * 100 |
| 278 | + wins = sum(1 for x, b in zip(l1["val"], l0["val"]) if x < b) |
| 279 | + print(f"L0 params={l0['n_params']} train={l0['train_mean']:.4f} val={l0['val_mean']:.4f}") |
| 280 | + print(f"L1 params={l1['n_params']} train={l1['train_mean']:.4f} val={l1['val_mean']:.4f}") |
| 281 | + print(f"L1 vs L0 (val): {rel_val:+.2f}% wins={wins}/{len(l0['val'])}") |
| 282 | + print(f"Param savings: {(l0['n_params'] - l1['n_params']) / l0['n_params'] * 100:.1f}%") |
| 283 | + if l1["val_mean"] < l0["val_mean"]: |
| 284 | + print(f"\n[L1 WINS @ MULTI-HEAD] Substrate-K composes with multi-head at scale.") |
| 285 | + print(f" → Production recommendation: L1 multi-head is the default attention block.") |
| 286 | + else: |
| 287 | + print(f"\n[L0 wins at multi-head scale] — multi-head's per-head content-keying") |
| 288 | + print(" may absorb the substrate's advantage. Worth investigating.") |
| 289 | + |
| 290 | + out_path = Path(__file__).parent / args.out |
| 291 | + with open(out_path, "w") as f: |
| 292 | + json.dump({"results": results, "config": vars(args)}, f, indent=2, default=float) |
| 293 | + print(f"\nWrote {out_path}") |
| 294 | + |
| 295 | + |
| 296 | +if __name__ == "__main__": |
| 297 | + main() |
0 commit comments