-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
260 lines (216 loc) · 8.04 KB
/
model.py
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
import einops
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from hook_point import HookPoint
# From Neel Nanda's A Mechanistic Interpretability Analysis of Grokking
class Embed(nn.Module):
def __init__(self, d_vocab, d_model):
super().__init__()
self.W_E = nn.Parameter(torch.randn(d_model, d_vocab) / np.sqrt(d_model))
def forward(self, x):
embedded = self.W_E[:, x]
result = torch.einsum("dbp -> bpd", embedded)
return result
class Unembed(nn.Module):
def __init__(self, d_vocab, d_model):
super().__init__()
self.W_U = nn.Parameter(torch.randn(d_model, d_vocab) / np.sqrt(d_vocab))
def forward(self, x):
result = x @ self.W_U
return result
class PosEmbed(nn.Module):
def __init__(self, max_ctx, d_model):
super().__init__()
self.W_pos = nn.Parameter(torch.randn(max_ctx, d_model) / np.sqrt(d_model))
def forward(self, x):
result = x + self.W_pos[: x.shape[-2]]
return result
class LayerNorm(nn.Module):
def __init__(self, d_model, epsilon=1e-4, model=[None]):
super().__init__()
self.model = model
self.w_ln = nn.Parameter(torch.ones(d_model))
self.b_ln = nn.Parameter(torch.zeros(d_model))
self.epsilon = epsilon
def forward(self, x):
if self.model[0].use_ln:
x = x - x.mean(axis=-1)[..., None]
x = x / (x.std(axis=-1)[..., None] + self.epsilon)
x = x * self.w_ln
x = x + self.b_ln
return x
else:
return x
class Attention(nn.Module):
def __init__(self, d_model, num_heads, d_head, n_ctx):
super().__init__()
self.W_K = nn.Parameter(
torch.randn(num_heads, d_head, d_model) / np.sqrt(d_model)
)
self.W_Q = nn.Parameter(
torch.randn(num_heads, d_head, d_model) / np.sqrt(d_model)
)
self.W_V = nn.Parameter(
torch.randn(num_heads, d_head, d_model) / np.sqrt(d_model)
)
self.W_O = nn.Parameter(
torch.randn(d_model, d_head * num_heads) / np.sqrt(d_model)
)
self.register_buffer("mask", torch.tril(torch.ones((n_ctx, n_ctx))))
self.d_head = d_head
self.hook_k = HookPoint()
self.hook_q = HookPoint()
self.hook_v = HookPoint()
self.hook_z = HookPoint()
self.hook_attn = HookPoint()
self.hook_attn_pre = HookPoint()
def forward(self, x):
k = self.hook_k(torch.einsum("ihd,bpd->biph", self.W_K, x))
q = self.hook_q(torch.einsum("ihd,bpd->biph", self.W_Q, x))
v = self.hook_v(torch.einsum("ihd,bpd->biph", self.W_V, x))
attn_scores_pre = torch.einsum("biph,biqh->biqp", k, q)
attn_scores_masked = torch.tril(attn_scores_pre) - 1e10 * (
1 - self.mask[: x.shape[-2], : x.shape[-2]]
)
attn_matrix = self.hook_attn(
F.softmax(
self.hook_attn_pre(attn_scores_masked / np.sqrt(self.d_head)), dim=-1
)
)
z = self.hook_z(torch.einsum("biph,biqp->biqh", v, attn_matrix))
z_flat = einops.rearrange(z, "b i q h -> b q (i h)")
out = torch.einsum("df,bqf->bqd", self.W_O, z_flat)
return out
class MLP(nn.Module):
def __init__(self, d_model, d_mlp, act_type):
super().__init__()
self.W_in = nn.Parameter(torch.randn(d_mlp, d_model) / np.sqrt(d_model))
self.b_in = nn.Parameter(torch.zeros(d_mlp))
self.W_out = nn.Parameter(torch.randn(d_model, d_mlp) / np.sqrt(d_model))
self.b_out = nn.Parameter(torch.zeros(d_model))
self.act_type = act_type
# self.ln = LayerNorm(d_mlp, model=self.model)
self.hook_pre = HookPoint()
self.hook_post = HookPoint()
assert act_type in ["ReLU", "GeLU"]
def forward(self, x):
x = self.hook_pre(torch.einsum("md,bpd->bpm", self.W_in, x) + self.b_in)
if self.act_type == "ReLU":
x = F.relu(x)
elif self.act_type == "GeLU":
x = F.gelu(x)
x = self.hook_post(x)
x = torch.einsum("dm,bpm->bpd", self.W_out, x) + self.b_out
return x
class TransformerBlock(nn.Module):
def __init__(self, d_model, d_mlp, d_head, num_heads, n_ctx, act_type):
super().__init__()
# self.ln1 = LayerNorm(d_model, model=self.model)
self.attn = Attention(d_model, num_heads, d_head, n_ctx)
# self.ln2 = LayerNorm(d_model, model=self.model)
self.mlp = MLP(d_model, d_mlp, act_type)
self.hook_attn_out = HookPoint()
self.hook_mlp_out = HookPoint()
self.hook_resid_pre = HookPoint()
self.hook_resid_mid = HookPoint()
self.hook_resid_post = HookPoint()
def forward(self, x):
x = self.hook_resid_pre(x)
x = self.hook_resid_mid(x + self.hook_attn_out(self.attn(x)))
x = self.hook_resid_post(x + self.hook_mlp_out(self.mlp(x)))
return x
class Transformer(nn.Module):
def __init__(
self,
num_layers,
d_vocab,
d_model,
d_mlp,
d_head,
num_heads,
n_ctx,
act_type,
use_cache=False,
use_ln=True,
):
super().__init__()
self.cache = {}
self.use_cache = use_cache
self.embed = Embed(d_vocab, d_model)
self.pos_embed = PosEmbed(n_ctx, d_model)
self.blocks = nn.ModuleList(
[
TransformerBlock(d_model, d_mlp, d_head, num_heads, n_ctx, act_type)
for i in range(num_layers)
]
)
# self.ln = LayerNorm(d_model, model=[self])
self.unembed = Unembed(d_vocab, d_model)
self.use_ln = use_ln
for name, module in self.named_modules():
if type(module) == HookPoint:
module.give_name(name)
def forward(self, x):
x = self.embed(x)
x = self.pos_embed(x)
for block in self.blocks:
x = block(x)
# x = self.ln(x)
x = self.unembed(x)
return x
def set_use_cache(self, use_cache):
self.use_cache = use_cache
def hook_points(self):
return [module for name, module in self.named_modules() if "hook" in name]
def remove_all_hooks(self):
for hp in self.hook_points():
hp.remove_hooks("fwd")
hp.remove_hooks("bwd")
def cache_all(self, cache, incl_bwd=False):
# Caches all activations wrapped in a HookPoint
def save_hook(tensor, name):
cache[name] = tensor.detach()
def save_hook_back(tensor, name):
cache[name + "_grad"] = tensor[0].detach()
for hp in self.hook_points():
hp.add_hook(save_hook, "fwd")
if incl_bwd:
hp.add_hook(save_hook_back, "bwd")
class Mlps(nn.Module):
def __init__(
self, num_layers, d_vocab, d_model, d_mlp, n_ctx, act_type, use_cache=False
):
super().__init__()
self.cache = {}
self.use_cache = use_cache
self.embed = Embed(d_vocab, d_model)
self.pos_embed = PosEmbed(n_ctx, d_model)
self.blocks = nn.ModuleList(
[MLP(d_model, d_mlp, act_type) for i in range(num_layers)]
)
self.unembed = Unembed(d_vocab, d_model)
for name, module in self.named_modules():
if type(module) == HookPoint:
module.give_name(name)
def forward(self, x):
x = self.embed(x)
x = self.pos_embed(x)
for block in self.blocks:
x = block(x)
x = self.unembed(x)
return x
class NoMlp(nn.Module):
def __init__(self, d_head, num_heads, d_vocab, d_model, n_ctx):
super().__init__()
self.embed = Embed(d_vocab, d_model)
self.pos_embed = PosEmbed(n_ctx, d_model)
self.attn = Attention(d_model, num_heads, d_head, n_ctx)
self.unembed = Unembed(d_vocab, d_model)
def forward(self, x):
x = self.embed(x)
x = self.pos_embed(x)
x = x + self.attn(x)
x = self.unembed(x)
return x