-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_blochwalk_200k.py
More file actions
191 lines (153 loc) · 5.41 KB
/
Copy pathtrain_blochwalk_200k.py
File metadata and controls
191 lines (153 loc) · 5.41 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
#!/usr/bin/env python3
"""
Bloch Walk training with 200K sequences and 5 epochs.
"""
import matplotlib
matplotlib.use('Agg')
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from pathlib import Path
from belief_geometry.processes import generate_blochwalk_sequence
from belief_geometry.model import ResidualHookedTransformer
class BlochwalkDataset(Dataset):
"""Dataset for Bloch Walk."""
def __init__(self, num_sequences, max_ctx, seed=42):
self.num_sequences = num_sequences
self.max_ctx = max_ctx
self.rng = np.random.default_rng(seed)
self._generate()
def _generate(self):
L = self.max_ctx
N = self.num_sequences
tokens = np.zeros((N, L), dtype=np.int64)
beliefs = np.zeros((N, L, 3), dtype=np.float32)
print(f" Generating {N} sequences of length {L}...")
for i in range(N):
if (i + 1) % 10000 == 0:
print(f" {i+1}/{N}")
s = generate_blochwalk_sequence(L, self.rng)
tokens[i] = s["tokens"]
beliefs[i] = s["beliefs"].astype(np.float32)
x = torch.from_numpy(tokens[:, :-1].copy())
y = torch.from_numpy(tokens[:, 1:].copy())
self.samples = {
"x": x,
"y": y,
"beliefs": torch.from_numpy(beliefs[:, 1:].copy()),
}
def __len__(self):
return len(self.samples["x"])
def __getitem__(self, idx):
return {k: v[idx] for k, v in self.samples.items()}
def train():
"""Train transformer on Bloch Walk."""
print("=" * 70)
print("Bloch Walk Training: 200K sequences, 5 epochs")
print("=" * 70)
print()
# Config
num_sequences = 200_000
max_ctx = 8
vocab_size = 4
batch_size = 256
lr = 3e-4
epochs = 5
device = "cuda" if torch.cuda.is_available() else "cpu"
out_dir = Path("runs/blochwalk_200k")
out_dir.mkdir(parents=True, exist_ok=True)
print(f"Device: {device}")
print(f"Output: {out_dir}")
print()
# Generate data
print("Generating dataset...")
full_ds = BlochwalkDataset(num_sequences, max_ctx, seed=42)
# Split
n_train = int(0.90 * len(full_ds))
n_val = int(0.05 * len(full_ds))
n_test = len(full_ds) - n_train - n_val
train_ds, val_ds, test_ds = torch.utils.data.random_split(
full_ds, [n_train, n_val, n_test],
generator=torch.Generator().manual_seed(42)
)
print(f" Train: {len(train_ds)}, Val: {len(val_ds)}, Test: {len(test_ds)}")
print()
# Model
print("Creating model...")
model = ResidualHookedTransformer(
vocab_size=vocab_size,
d_model=128,
n_heads=4,
d_ff=512,
n_layers=2,
max_ctx=max_ctx - 1,
dropout=0.0,
).to(device)
print(f" Parameters: {sum(p.numel() for p in model.parameters()):,}")
print()
# Training
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
loss_fn = nn.CrossEntropyLoss()
train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_ds, batch_size=batch_size, shuffle=False)
print("Training...")
for epoch in range(epochs):
# Train
model.train()
train_loss = 0.0
for batch in train_loader:
x = batch["x"].to(device)
y = batch["y"].to(device)
optimizer.zero_grad()
logits, _ = model(x)
loss = loss_fn(logits.reshape(-1, vocab_size), y.reshape(-1))
loss.backward()
optimizer.step()
train_loss += loss.item()
train_loss /= len(train_loader)
# Val
model.eval()
val_loss = 0.0
with torch.no_grad():
for batch in val_loader:
x = batch["x"].to(device)
y = batch["y"].to(device)
logits, _ = model(x)
loss = loss_fn(logits.reshape(-1, vocab_size), y.reshape(-1))
val_loss += loss.item()
val_loss /= len(val_loader)
print(f" Epoch {epoch+1}/{epochs}: train_loss={train_loss:.4f}, val_loss={val_loss:.4f}")
print()
print("Saving model...")
torch.save(model.state_dict(), out_dir / "model.pt")
# Export analysis batch
print("Exporting analysis batch...")
model.eval()
export_samples = min(2048, len(val_ds))
loader = DataLoader(val_ds, batch_size=256, shuffle=False)
residuals = []
beliefs = []
tokens_list = []
with torch.no_grad():
for batch in loader:
x = batch["x"].to(device)
logits, res = model(x)
residuals.append(res.cpu())
beliefs.append(batch["beliefs"])
tokens_list.append(batch["x"])
if sum(r.size(0) for r in residuals) >= export_samples:
break
residuals = torch.cat(residuals, dim=0)[:export_samples]
beliefs = torch.cat(beliefs, dim=0)[:export_samples]
tokens_list = torch.cat(tokens_list, dim=0)[:export_samples]
torch.save({
"residuals": residuals,
"beliefs": beliefs,
"tokens": tokens_list,
}, out_dir / "analysis_batch.pt")
print(f" Saved {export_samples} samples")
print()
print("✓ Training complete!")
if __name__ == "__main__":
train()