-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtorch_multiblock.py
More file actions
178 lines (154 loc) · 6.88 KB
/
torch_multiblock.py
File metadata and controls
178 lines (154 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Multi-block transformer A/B: does L3-vs-L0 hold when stacking?
Single attention layer is the easiest test. Stacking exposes whether
the substrate-only attention COMPOSES across depth, or whether
deeper models reveal a need for learned attention that single-block
hid.
Architecture: stack `n_blocks` of (Attn + Residual + LN + FFN +
Residual + LN), same as the single-block model except repeated.
If L3 still beats L0 at depth=4, substrate attention isn't just
useful at the layer level — it's a structurally valid architectural
component.
"""
from __future__ import annotations
import argparse
import json
import math
import statistics
from pathlib import Path
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_4way import (
lcg, make_matrix, crt_pe,
AttentionL0, AttentionL1, AttentionL2, AttentionL3,
build_vocab,
)
class TransformerBlock(nn.Module):
"""One transformer block: Attn → +residual → LN → FFN → +residual → LN."""
def __init__(self, variant: str, d_model: int, ff_dim: int,
seq_len: int, seed: int):
super().__init__()
attn_cls = {"L0": AttentionL0, "L1": AttentionL1,
"L2": AttentionL2, "L3": AttentionL3}[variant]
self.attn = attn_cls(d_model, seq_len, seed)
s = self.attn.rng_state
self.ln1_g = nn.Parameter(torch.ones(d_model))
self.ln1_b = nn.Parameter(torch.zeros(d_model))
W_up, s = make_matrix(d_model, ff_dim, 0.3, s + 13)
W_down, s = make_matrix(ff_dim, d_model, 0.3, s)
self.ff_up = nn.Parameter(W_up)
self.ff_up_b = nn.Parameter(torch.zeros(ff_dim))
self.ff_down = nn.Parameter(W_down)
self.ff_down_b = nn.Parameter(torch.zeros(d_model))
self.ln2_g = nn.Parameter(torch.ones(d_model))
self.ln2_b = nn.Parameter(torch.zeros(d_model))
self.rng_state = s
def forward(self, x):
attn_out = self.attn(x)
x_post_attn = x + attn_out
normed1 = F.layer_norm(x_post_attn, (x.size(-1),),
weight=self.ln1_g, bias=self.ln1_b)
up = normed1 @ self.ff_up + self.ff_up_b
activated = F.relu(up)
down = activated @ self.ff_down + self.ff_down_b
x_post_ff = x_post_attn + down
normed2 = F.layer_norm(x_post_ff, (x.size(-1),),
weight=self.ln2_g, bias=self.ln2_b)
return normed2
class MultiBlockTransformer(nn.Module):
def __init__(self, variant: str, vocab: int, d_model: int, ff_dim: int,
seq_len: int, n_blocks: int, seed: int):
super().__init__()
s = seed
E, s = make_matrix(vocab, d_model, 0.3, s)
self.embedding = nn.Parameter(E)
self.register_buffer("pe_table", crt_pe(seq_len, d_model))
self.blocks = nn.ModuleList()
for i in range(n_blocks):
block = TransformerBlock(variant, d_model, ff_dim, seq_len, s + 100 * (i + 1))
self.blocks.append(block)
s = block.rng_state
W_head, _ = make_matrix(d_model, vocab, 0.3, s + 17)
self.head = nn.Parameter(W_head)
self.head_b = nn.Parameter(torch.zeros(vocab))
def forward(self, token_ids):
x = self.embedding[token_ids] + self.pe_table[:token_ids.size(0)]
for block in self.blocks:
x = block(x)
return x @ self.head + self.head_b
def train_arm(variant: str, ids: list, vocab_size: int, seq_len: int,
d_model: int, ff_dim: int, n_blocks: int, lr: float,
steps: int, seed: int):
torch.manual_seed(seed)
model = MultiBlockTransformer(variant, vocab_size, d_model, ff_dim,
seq_len, n_blocks, seed)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr,
betas=(0.9, 0.999), eps=1e-8)
n_windows = len(ids) - seq_len - 1
ids_tensor = torch.tensor(ids, dtype=torch.long)
tail_losses = []
for step in range(steps):
start = step % n_windows
window = ids_tensor[start:start + seq_len]
targets = ids_tensor[start + 1:start + 1 + seq_len]
logits = model(window)
loss = F.cross_entropy(logits, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step >= steps - 10:
tail_losses.append(loss.item())
n_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
return sum(tail_losses) / len(tail_losses), n_params
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--seeds", type=str, default="42,7,123,2026,1")
parser.add_argument("--steps", type=int, default=300)
parser.add_argument("--lr", type=float, default=0.01)
parser.add_argument("--n-blocks", type=int, default=4)
parser.add_argument("--out", type=str, default="results_torch_multiblock.json")
args = parser.parse_args()
text = "the quick brown fox jumps over the lazy dog and the dog sleeps in the sun"
chars, lookup = build_vocab(text)
vocab_size = len(chars)
ids = [lookup[c] for c in text]
seq_len = 8
d_model = 16
ff_dim = 32
seeds = [int(s) for s in args.seeds.split(",")]
variants = ["L0", "L1", "L2", "L3"]
print(f"=== Multi-block ({args.n_blocks} layers) attention A/B ===")
print(f"setup: corpus={len(text)} vocab={vocab_size} seq={seq_len} "
f"d={d_model} ff={ff_dim} n_blocks={args.n_blocks}")
print(f" steps={args.steps} lr={args.lr} seeds={seeds}\n", flush=True)
results = {}
for v in variants:
losses = []
n_params = 0
for seed in seeds:
loss, n_params = train_arm(v, ids, vocab_size, seq_len, d_model,
ff_dim, args.n_blocks, args.lr,
args.steps, seed)
losses.append(loss)
results[v] = {"losses": losses, "n_params": n_params,
"mean": sum(losses) / len(losses),
"std": statistics.stdev(losses) if len(losses) > 1 else 0.0}
print(f"[{v}] params={n_params:5d} mean={results[v]['mean']:.4f} "
f"std={results[v]['std']:.4f}", flush=True)
print("\n=== Summary vs L0 ===")
base_mean = results["L0"]["mean"]
base_losses = results["L0"]["losses"]
for v in variants:
wins = sum(1 for x, b in zip(results[v]["losses"], base_losses) if x < b)
rel = (results[v]["mean"] - base_mean) / base_mean * 100
marker = "—" if v == "L0" else f"{rel:+.1f}%"
print(f" {v}: mean={results[v]['mean']:.4f} vs L0: {marker:>8} "
f"wins={wins}/{len(base_losses)}")
out = {"n_blocks": args.n_blocks, "seeds": seeds, "steps": args.steps,
"results": results}
out_path = Path(__file__).parent / args.out
with open(out_path, "w") as f:
json.dump(out, f, indent=2, default=float)
print(f"\nWrote {out_path}")
if __name__ == "__main__":
main()