-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtorch_4way.py
More file actions
315 lines (257 loc) · 11 KB
/
torch_4way.py
File metadata and controls
315 lines (257 loc) · 11 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""4-way attention A/B in PyTorch.
Reproduce the substrate-attention experiment from
examples/prometheus_attention_4way.omc. Same architecture, same
task, same seed semantics (LCG-ported init for fair comparison).
If PyTorch shows the same monotonic substrate-ladder result (L3 >
L2 > L1 > L0), the win is cross-framework. If it doesn't, the OMC
result was specific to our implementation.
Variants:
L0: standard QKV (learned matrices)
L1: K = CRT-PE (substrate), Q + V learned
L2: K, Q = CRT-PE; V learned
L3: K, Q = CRT-PE; V = identity (parameter-free attention block)
"""
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
# ---- Reproduce OMC's LCG init for fair comparison ----
def lcg(state: int) -> int:
return (state * 1103515245 + 12345) % 2147483648
def make_matrix(rows: int, cols: int, bound: float, state: int):
"""Bit-identical port of _prom_random_matrix from prometheus.omc."""
m = torch.empty(rows, cols)
s = state
for i in range(rows):
for j in range(cols):
s = lcg(s)
r = s / 2147483648.0
m[i, j] = (r * 2.0 - 1.0) * bound
return m, s
# ---- CRT-Fibonacci positional encoding (same moduli as OMC) ----
FIB_MODULI = [5, 8, 13, 21, 34, 55, 89, 144]
def crt_pe(seq_len: int, d_model: int) -> torch.Tensor:
pe = torch.zeros(seq_len, d_model)
n_pairs = d_model // 2
for i in range(n_pairs):
m = FIB_MODULI[i % len(FIB_MODULI)]
for pos in range(seq_len):
residue = pos % m
angle = 2.0 * math.pi * residue / m
pe[pos, 2 * i] = math.sin(angle)
pe[pos, 2 * i + 1] = math.cos(angle)
return pe
# ---- Attention variants ----
class AttentionL0(nn.Module):
"""Standard QKV — learned matrices."""
def __init__(self, d_model: int, seq_len: int, seed: int):
super().__init__()
W_q, s = make_matrix(d_model, d_model, 0.3, seed + 11)
W_k, s = make_matrix(d_model, d_model, 0.3, s)
W_v, s = make_matrix(d_model, d_model, 0.3, s)
self.W_q = nn.Parameter(W_q)
self.W_k = nn.Parameter(W_k)
self.W_v = nn.Parameter(W_v)
self.rng_state = s
def forward(self, x):
q = x @ self.W_q
k = x @ self.W_k
v = x @ self.W_v
scores = q @ k.T
attn = F.softmax(scores, dim=-1)
return attn @ v
class AttentionL1(nn.Module):
"""K = CRT-PE; Q + V learned."""
def __init__(self, d_model: int, seq_len: int, seed: int):
super().__init__()
W_q, s = make_matrix(d_model, d_model, 0.3, seed + 11)
W_v, s = make_matrix(d_model, d_model, 0.3, s)
self.W_q = nn.Parameter(W_q)
self.W_v = nn.Parameter(W_v)
self.register_buffer("K_const", crt_pe(seq_len, d_model))
self.rng_state = s
def forward(self, x):
q = x @ self.W_q
v = x @ self.W_v
k = self.K_const
scores = q @ k.T
attn = F.softmax(scores, dim=-1)
return attn @ v
class AttentionL2(nn.Module):
"""K, Q = CRT-PE; only V learned."""
def __init__(self, d_model: int, seq_len: int, seed: int):
super().__init__()
W_v, s = make_matrix(d_model, d_model, 0.3, seed + 11)
self.W_v = nn.Parameter(W_v)
pe = crt_pe(seq_len, d_model)
self.register_buffer("K_const", pe)
self.register_buffer("Q_const", pe)
self.rng_state = s
def forward(self, x):
v = x @ self.W_v
scores = self.Q_const @ self.K_const.T
attn = F.softmax(scores, dim=-1)
return attn @ v
class AttentionL3(nn.Module):
"""K, Q = CRT-PE; V = identity (parameter-free)."""
def __init__(self, d_model: int, seq_len: int, seed: int):
super().__init__()
pe = crt_pe(seq_len, d_model)
self.register_buffer("K_const", pe)
self.register_buffer("Q_const", pe)
self.rng_state = seed + 11
def forward(self, x):
scores = self.Q_const @ self.K_const.T
attn = F.softmax(scores, dim=-1)
return attn @ x
# ---- Full transformer block (same for all variants) ----
class TransformerModel(nn.Module):
def __init__(self, variant: str, vocab: int, d_model: int, ff_dim: int,
seq_len: int, seed: int):
super().__init__()
s = seed
E, s = make_matrix(vocab, d_model, 0.3, s)
self.embedding = nn.Parameter(E)
attn_cls = {"L0": AttentionL0, "L1": AttentionL1,
"L2": AttentionL2, "L3": AttentionL3}[variant]
self.attn = attn_cls(d_model, seq_len, s)
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))
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))
# Precompute CRT-PE for the embed-side position add.
self.register_buffer("pe_table", crt_pe(seq_len, d_model))
def forward(self, token_ids: torch.Tensor) -> torch.Tensor:
# token_ids: [N]
x = self.embedding[token_ids] # [N, d]
x = x + self.pe_table[:x.size(0)] # add CRT-PE
attn_out = self.attn(x) # [N, d]
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 @ self.head + self.head_b # [N, vocab]
# ---- Training loop ----
def build_vocab(text: str):
chars = []
lookup = {}
for ch in text:
if ch not in lookup:
lookup[ch] = len(chars)
chars.append(ch)
return chars, lookup
def train_arm(variant: str, ids: list, vocab_size: int, seq_len: int,
d_model: int, ff_dim: int, lr: float, steps: int, seed: int):
torch.manual_seed(seed)
model = TransformerModel(variant, vocab_size, d_model, ff_dim, seq_len, seed)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr,
betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0)
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, reduction="mean")
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")
parser.add_argument("--steps", type=int, default=250)
parser.add_argument("--lr", type=float, default=0.02)
parser.add_argument("--out", type=str, default="results_torch_4way.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("=== PyTorch 4-way attention A/B ===")
print(f"setup: corpus={len(text)} vocab={vocab_size} seq={seq_len} "
f"d={d_model} ff={ff_dim}")
print(f" steps={args.steps} lr={args.lr} seeds={seeds}\n", flush=True)
results = {}
for v in variants:
losses = []
for seed in seeds:
loss, n_params = train_arm(v, ids, vocab_size, seq_len,
d_model, ff_dim, 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:4d} mean={results[v]['mean']:.4f} "
f"std={results[v]['std']:.4f} per-seed={[f'{x:.3f}' for x in losses]}",
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)}")
print("\n=== Cross-framework comparison ===")
print("OMC result (from examples/prometheus_attention_4way.omc):")
print(" L0=2.576 L1=2.506 (-2.7%) L2=2.157 (-16.3%) L3=2.023 (-21.5%)")
print("PyTorch result (this run):")
for v in variants:
print(f" {v}={results[v]['mean']:.3f}", end=" ")
print()
# Verdict
l0 = results["L0"]["mean"]
l3 = results["L3"]["mean"]
if l3 < l0:
delta_pct = (l3 - l0) / l0 * 100
print(f"\n[CROSS-FRAMEWORK WIN] L3 beats L0 by {delta_pct:.1f}% in PyTorch too.")
print(" Substrate-as-attention-replacement validated across runtimes.")
else:
delta_pct = (l3 - l0) / l0 * 100
print(f"\n[OMC-SPECIFIC] L3 LOSES to L0 by {delta_pct:.1f}% in PyTorch.")
print(" OMC result didn't replicate — investigate runtime-specific factors.")
out_path = Path(__file__).parent / args.out
with open(out_path, "w") as f:
json.dump({
"results": {k: {"losses": v["losses"], "n_params": v["n_params"],
"mean": v["mean"], "std": v["std"]}
for k, v in results.items()},
"config": {"seeds": seeds, "steps": args.steps, "lr": args.lr,
"vocab": vocab_size, "d_model": d_model, "ff_dim": ff_dim,
"seq_len": seq_len},
}, f, indent=2)
print(f"\nWrote {out_path}")
if __name__ == "__main__":
main()