-
Notifications
You must be signed in to change notification settings - Fork 0
/
sae_variants.py
450 lines (373 loc) · 17.7 KB
/
sae_variants.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import torch
from torch import nn, Tensor
from fancy_einsum import einsum
from torch.nn import functional as F
from datasets import load_dataset
import torch
from torch.utils.data import DataLoader
from einops import rearrange
from transformer_lens import utils
from mandala._next.imports import op, NewArgDefault, Storage
from typing import Tuple
from ioi_utils import *
@op
def normalize_activations(A: Tensor, scale: Optional[float] = NewArgDefault()) -> Tuple[Tensor, float]:
"""
Normalize activations following
https://transformer-circuits.pub/2024/april-update/index.html#training-saes:
multiply by a scalar so that the average norm is sqrt(dimension)
"""
assert len(A.shape) == 2
if isinstance(scale, NewArgDefault):
d_activation = A.shape[1]
avg_norm = A.norm(p=2, dim=1).mean()
normalization_scale = avg_norm / d_activation ** 0.5
return A / normalization_scale, normalization_scale
else:
return A / scale, scale
@op
def normalize_grad(A_grad: Tensor, scale: float) -> Tensor:
"""
To be used w/ attribution SAEs
"""
return A_grad / scale
class VanillaAutoEncoder(nn.Module):
def __init__(self,
d_activation: int,
d_hidden: int,
enc_dtype: str = "fp32",
freeze_decoder: bool = False,
random_seed: int = 0,
):
super().__init__()
self.d_activation = d_activation
self.d_hidden = d_hidden
self.freeze_decoder = freeze_decoder
dtype = torch.float32 if enc_dtype == "fp32" else torch.float16
# set the random seed before initializing the weights
torch.manual_seed(random_seed)
self.W_dec = nn.Parameter(torch.nn.init.kaiming_uniform_(torch.empty(self.d_hidden, d_activation, dtype=dtype)))
if freeze_decoder:
self.W_dec.requires_grad = False
self.b_enc = nn.Parameter(torch.zeros(self.d_hidden, dtype=dtype))
self.b_dec = nn.Parameter(torch.zeros(d_activation, dtype=dtype))
self.W_dec.data[:] = self.W_dec / self.W_dec.norm(dim=-1, keepdim=True)
self.W_enc = nn.Parameter(torch.empty(d_activation, d_hidden, dtype=dtype))
# initialize W_enc from W_dec, following https://transformer-circuits.pub/2024/april-update/index.html#training-saes
self.W_enc.data = self.W_dec.data.T.detach().clone()
def forward_detailed(self, A: Tensor):
x_cent = A - self.b_dec
acts = F.relu(x_cent @ self.W_enc + self.b_enc)
x_reconstruct = acts @ self.W_dec + self.b_dec
l2_losses = (x_reconstruct.float() - A.float()).pow(2).sum(-1)
l1_losses = acts.float().abs().sum(-1)
return x_reconstruct, acts, l2_losses, l1_losses
def forward(self, A: Tensor):
x_reconstruct, acts, l2_losses, l1_losses = self.forward_detailed(A)
l2_loss = l2_losses.mean()
l1_loss = l1_losses.mean()
loss = l2_loss + l1_loss
return x_reconstruct, acts, l2_loss, l1_loss
@torch.no_grad()
def make_decoder_weights_and_grad_unit_norm(self):
if self.freeze_decoder:
return
W_dec_normed = self.W_dec / self.W_dec.norm(dim=-1, keepdim=True)
W_dec_grad_proj = (self.W_dec.grad * W_dec_normed).sum(-1, keepdim=True) * W_dec_normed
self.W_dec.grad -= W_dec_grad_proj
# Bugfix(?) for ensuring W_dec retains unit norm, this was not there when I trained my original autoencoders.
self.W_dec.data = W_dec_normed
### encapsulate some pieces of logic here w/ no_grad to avoid bugs
@torch.no_grad()
def get_activation_pattern(self, A: Tensor) -> Tensor:
_, acts, _, _ = self.forward_detailed(A)
return (acts > 0).bool()
@torch.no_grad()
def get_feature_magnitudes(self, A: Tensor) -> Tensor:
_, acts, _, _ = self.forward_detailed(A)
return acts
@torch.no_grad()
def get_reconstructions(self, A: Tensor) -> Tensor:
x_reconstruct, _, _, _ = self.forward_detailed(A)
return x_reconstruct
class GatedAutoEncoder(nn.Module):
"""
Following the Gated SAE paper https://arxiv.org/pdf/2404.16014
"""
def __init__(self, d_activation: int, d_hidden: int):
super().__init__()
self.d_activation = d_activation
self.d_hidden = d_hidden
# the decoder matrix
W_dec = torch.randn(d_hidden, d_activation)
# normalize so that each row (i.e., decoder vector) has unit norm
W_dec = W_dec / torch.norm(W_dec, dim=1, keepdim=True)
self.W_dec = nn.Parameter(W_dec)
# the gating matrix (also denoted W_enc in the paper figure 3)
self.W_gate = nn.Parameter(nn.init.kaiming_uniform_(torch.empty(d_activation, d_hidden)))
self.r_mag = nn.Parameter(torch.zeros(d_hidden))
self.b_gate = nn.Parameter(torch.zeros(d_hidden))
self.b_mag = nn.Parameter(torch.zeros(d_hidden))
self.b_dec = nn.Parameter(torch.zeros(d_activation))
self.relu = nn.ReLU()
def encode(self, X: Tensor):
# using the paper's notation
X_centered = X - self.b_dec
pi_gate = einsum( 'dim hidden, batch dim -> batch hidden', self.W_gate, X_centered,) + self.b_gate
# f_gate gives the activation pattern for the hidden layer
f_gate = (pi_gate > 0).float()
W_mag = einsum( 'hidden, dim hidden -> dim hidden', torch.exp(self.r_mag), self.W_gate,)
f_mag = einsum('dim hidden, batch dim -> batch hidden', W_mag, X_centered, ) + self.b_mag
f_tilde = f_gate * f_mag
L_sparsity = nn.ReLU()(pi_gate).norm(dim=1, p=1).mean()
return f_tilde, pi_gate, L_sparsity
def decode(self, f_tilde: Tensor, pi_gate: Tensor, X: Tensor):
X_hat = einsum('batch hidden, hidden dim -> batch dim', f_tilde, self.W_dec, ) + self.b_dec
L_reconstruct = (X_hat - X).pow(2).sum(dim=1).mean()
# compute the auxiliary loss
W_dec_clone = self.W_dec.clone().detach()
b_dec_clone = self.b_dec.clone().detach()
x_hat_frozen = einsum(
'batch hidden, hidden dim -> batch dim', nn.ReLU()(pi_gate), W_dec_clone,
) + b_dec_clone
L_aux = (X - x_hat_frozen).pow(2).sum(dim=1).mean()
return X_hat, L_reconstruct, L_aux
@torch.no_grad()
def make_decoder_weights_and_grad_unit_norm(self):
W_dec_normed = self.W_dec / self.W_dec.norm(dim=-1, keepdim=True)
W_dec_grad_proj = (self.W_dec.grad * W_dec_normed).sum(-1, keepdim=True) * W_dec_normed
self.W_dec.grad -= W_dec_grad_proj
self.W_dec.data = W_dec_normed
### encapsulate some pieces of logic here w/ no_grad to avoid bugs
@torch.no_grad()
def get_activation_pattern(self, A: Tensor):
_, pi_gate, _ = self.encode(A)
return (pi_gate > 0).bool()
@torch.no_grad()
def get_feature_magnitudes(self, X: Tensor) -> Tensor:
"""
This returns the actual magnitudes in the decomposition x_hat = sum_j
f_j W_dec_j + b_dec
"""
# use f_tilde, because X_hat = f_tilde @ W_dec + b_dec
f_tilde, _, _ = self.encode(X)
return f_tilde
@torch.no_grad()
def get_reconstructions(self, X: Tensor) -> Tensor:
f_tilde, pi_gate, _ = self.encode(X)
X_hat, _, _ = self.decode(f_tilde, pi_gate, X)
return X_hat
################################################################################
### Attribution SAEs
################################################################################
# attribution SAEs are basically vanilla SAEs, but with a different loss
# function
class AttributionAutoEncoder(VanillaAutoEncoder):
"""
Following https://transformer-circuits.pub/2024/april-update/index.html#attr-dl
"""
def forward_detailed(self, A: Tensor, A_grad: Optional[Tensor] = None):
A_centered = A - self.b_dec
acts = F.relu(A_centered @ self.W_enc + self.b_enc)
A_hat = acts @ self.W_dec + self.b_dec
l2_losses = (A_hat.float() - A.float()).pow(2).sum(-1)
l1_losses = acts.float().abs().sum(-1)
# and now, two new losses
# in the notation of the blog post,
# hidden activations are y (d_hidden,)
# the reconstructions are x_hat = y @ W_dec + b_dec
# the gradient w.r.t. the *hidden* activations is
# grad_y (metric) = grad_x_hat (metric) @ W_dec.T
# and b/c we don't want to compute all the gradients for reconstructions
# we just use grad_x (metric), lol
if A_grad is not None:
attribution_sparsity_losses = (acts * (einsum('batch act_dim, hidden act_dim -> batch hidden', A_grad, self.W_dec))).abs().sum(-1)
unexplained_attribution_losses = einsum("batch act_dim, batch act_dim -> batch", A - A_hat, A_grad).abs()
else:
attribution_sparsity_losses = None
unexplained_attribution_losses = None
return A_hat, acts, l2_losses, l1_losses, attribution_sparsity_losses, unexplained_attribution_losses
# def get_reconstruction(self, A: Tensor) -> Tensor:
# x_reconstruct, _, _, _ = self.forward_detailed(A)
# return x_reconstruct
def forward(self, A: Tensor, A_grad: Optional[Tensor] = None):
x_reconstruct, acts, l2_losses, l1_losses, attribution_sparsity_losses, unexplained_attribution_losses = self.forward_detailed(A, A_grad)
l2_loss = l2_losses.mean()
l1_loss = l1_losses.mean()
if A_grad is not None:
attribution_sparsity_loss = attribution_sparsity_losses.mean()
unexplained_attribution_loss = unexplained_attribution_losses.mean()
else:
attribution_sparsity_loss = None
unexplained_attribution_loss = None
return x_reconstruct, acts, l2_loss, l1_loss, attribution_sparsity_loss, unexplained_attribution_loss
@torch.no_grad()
def make_decoder_weights_and_grad_unit_norm(self):
if self.freeze_decoder:
return
W_dec_normed = self.W_dec / self.W_dec.norm(dim=-1, keepdim=True)
W_dec_grad_proj = (self.W_dec.grad * W_dec_normed).sum(-1, keepdim=True) * W_dec_normed
self.W_dec.grad -= W_dec_grad_proj
# Bugfix(?) for ensuring W_dec retains unit norm, this was not there when I trained my original autoencoders.
self.W_dec.data = W_dec_normed
### encapsulate some pieces of logic here w/ no_grad to avoid bugs
@torch.no_grad()
def get_activation_pattern(self, A: Tensor) -> Tensor:
_, acts, _, _, _, _ = self.forward_detailed(A)
return (acts > 0).bool()
@torch.no_grad()
def get_feature_magnitudes(self, A: Tensor) -> Tensor:
_, acts, _, _, _, _ = self.forward_detailed(A)
return acts
@torch.no_grad()
def get_reconstructions(self, A: Tensor) -> Tensor:
x_reconstruct, _, _, _, _, _ = self.forward_detailed(A)
return x_reconstruct
@op
@batched(args=['prompts'], n_outputs=1, reducer='cat')
def collect_gradients(
prompts: List[Prompt],
# layers_and_activations: List[Tuple[int, Literal['z', 'q', 'k']]],
nodes: List[Node],
batch_size: Optional[int] = None,
) -> Dict[Node, Tensor]:
"""
Given a list of prompts, collect the gradients of the logit difference with
respect to the given activation in the given layer.
This will return a tensor of shape (n_prompts, seq_len, n_heads, head_dim)
from which you can then select gradients for desired heads and positions.
"""
model: HookedTransformer = MODELS[MODEL_ID]
model.requires_grad_(True)
prompt_dataset = PromptDataset(prompts, model=model)
layers_and_activations = list({(node.layer, node.component_name) for node in nodes})
activations = {}
grads = {}
def get_forward_hook(location: Tuple[int, str]):
def hook(model, input, output):
activations[location] = output
output.retain_grad()
return hook
def get_backward_hook(location: Tuple[int, str]):
def hook(grad):
grads[location] = grad
return hook
activation_attrs = {
'z': 'hook_z',
'q': 'hook_q',
'k': 'hook_k',
'v': 'hook_v',
}
forward_handles = {}
for layer, activation_name in layers_and_activations:
location = (layer, activation_name)
forward_hook_handle = getattr(model.blocks[layer].attn, activation_attrs[activation_name]).register_forward_hook(get_forward_hook(location))
forward_handles[location] = forward_hook_handle
input_tensor = prompt_dataset.tokens
# forward all the inputs ONCE
output = model(input_tensor)[:, -1, :]
# compute the tensor of logit differences that we want to take the gradient of
answer_logits = torch.gather(output, dim=1, index=prompt_dataset.answer_tokens.cuda())
ld = answer_logits[:, 0] - answer_logits[:, 1]
backward_handles = {}
for layer, activation_name in layers_and_activations:
activation = activations[(layer, activation_name)]
backward_hook_handle = activation.register_hook(get_backward_hook((layer, activation_name)))
backward_handles[(layer, activation_name)] = backward_hook_handle
individual_gradients = {}
for i in range(len(prompt_dataset)): # iterate over the batch
# lol why isn't there (?) a way to get the batch of gradients in one go :(
# Backward pass
model.zero_grad() # make sure the model is zeroed out
ld[i].backward(retain_graph=True)
# The gradient of the loss with respect to the internal activation
for layer, activation_name in layers_and_activations:
if (layer, activation_name) not in individual_gradients:
individual_gradients[(layer, activation_name)] = []
internal_activation_grad = grads[(layer, activation_name)][i].detach().clone()
individual_gradients[(layer, activation_name)].append(internal_activation_grad)
#! undo all the things we've done to the model
for layer, activation_name in layers_and_activations:
forward_hook_handle = forward_handles[(layer, activation_name)]
forward_hook_handle.remove()
backward_hook_handle = backward_handles[(layer, activation_name)]
backward_hook_handle.remove()
model.requires_grad_(False)
for key in individual_gradients:
individual_gradients[key] = torch.stack(individual_gradients[key], dim=0).cpu()
# now, turn into a dict keyed by the nodes
node = nodes[0]
nodes_result = {}
for node in nodes:
layer, component_name = node.layer, node.component_name
full_grad = individual_gradients[(layer, component_name)]
nodes_result[node] = full_grad[node.idx(prompts=prompts)]
return nodes_result
def get_gradients(storage: Storage, nodes: List[Node], prompts: Any, computing: bool, n_batches: int) -> Dict[Node, Tensor]:
with storage:
prompts_raw = storage.unwrap(prompts)
n_total = len(prompts_raw)
grads_parts = []
result = {node: [] for node in nodes}
for i in tqdm(list(range(n_batches))):
# print(f'Batch {i}/{n_batches}')
start = i * (n_total // n_batches)
end = (i + 1) * (n_total // n_batches)
prompts = prompts_raw[start:end]
grads_part = collect_gradients(prompts=prompts, nodes=nodes, batch_size=20,)
if computing:
storage.commit()
storage.atoms.clear()
else:
grads_part = storage.unwrap(grads_part)
for node in nodes:
result[node].append(grads_part[node])
if computing:
return None
else:
return {k: torch.cat(v, dim=0).cuda() for k, v in result.items()}
################################################################################
### top-k SAEs
################################################################################
class TopKAutoEncoder(nn.Module):
def __init__(self, d_activation: int, d_hidden: int, k: int):
super().__init__()
self.d_activation = d_activation
self.d_hidden = d_hidden
self.k = k
self.W_enc = nn.Parameter(
torch.nn.init.kaiming_uniform_(
torch.empty(self.d_activation, self.d_hidden, ).cuda(),
nonlinearity="relu",
)
)
self.W_dec = nn.Parameter(self.W_enc.data.mT.contiguous())
self.b_pre = nn.Parameter(torch.zeros(self.d_activation).cuda())
def forward(self, A: Tensor):
acts = einsum('batch d_activation, d_activation d_hidden -> batch d_hidden', A - self.b_pre, self.W_enc)
top_acts, top_indices = acts.topk(self.k, dim=-1,
sorted=False # we don't care about the order
)
# top_indices is (batch, k)
# we want to get a tensor of shape (batch, d_hidden) out of this
# by putting the top_k indices in the right places
# top_indices is (batch, k)
z = torch.zeros_like(acts)
z.scatter_(dim=-1, index=top_indices, src=top_acts)
A_reconstruct = z @ self.W_dec + self.b_pre
return A_reconstruct, acts, z
@property
def b_dec(self): # a trick for compatibility with the other SAEs
return self.b_pre
@torch.no_grad()
def get_activation_pattern(self, A: Tensor) -> Tensor:
_, acts, z = self.forward(A)
return (z != 0).bool()
@torch.no_grad()
def get_feature_magnitudes(self, A: Tensor) -> Tensor:
_, _, z = self.forward(A)
return z
@torch.no_grad()
def get_reconstructions(self, A: Tensor) -> Tensor:
A_reconstruct, _, _ = self.forward(A)
return A_reconstruct