forked from saprmarks/dictionary_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.py
308 lines (264 loc) · 10.3 KB
/
dictionary.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
"""
Defines the dictionary classes
"""
from abc import ABC, abstractmethod
import torch as t
import torch.nn as nn
import torch.nn.init as init
class Dictionary(ABC):
"""
A dictionary consists of a collection of vectors, an encoder, and a decoder.
"""
dict_size : int # number of features in the dictionary
activation_dim : int # dimension of the activation vectors
@abstractmethod
def encode(self, x):
"""
Encode a vector x in the activation space.
"""
pass
@abstractmethod
def decode(self, f):
"""
Decode a dictionary vector f (i.e. a linear combination of dictionary elements)
"""
pass
class AutoEncoder(Dictionary, nn.Module):
"""
A one-layer autoencoder.
"""
def __init__(self, activation_dim, dict_size):
super().__init__()
self.activation_dim = activation_dim
self.dict_size = dict_size
self.bias = nn.Parameter(t.zeros(activation_dim))
self.encoder = nn.Linear(activation_dim, dict_size, bias=True)
# rows of decoder weight matrix are unit vectors
self.decoder = nn.Linear(dict_size, activation_dim, bias=False)
dec_weight = t.randn_like(self.decoder.weight)
dec_weight = dec_weight / dec_weight.norm(dim=0, keepdim=True)
self.decoder.weight = nn.Parameter(dec_weight)
def encode(self, x):
return nn.ReLU()(self.encoder(x - self.bias))
def decode(self, f):
return self.decoder(f) + self.bias
def forward(self, x, output_features=False, ghost_mask=None):
"""
Forward pass of an autoencoder.
x : activations to be autoencoded
output_features : if True, return the encoded features as well as the decoded x
ghost_mask : if not None, run this autoencoder in "ghost mode" where features are masked
"""
if ghost_mask is None: # normal mode
f = self.encode(x)
x_hat = self.decode(f)
if output_features:
return x_hat, f
else:
return x_hat
else: # ghost mode
f_pre = self.encoder(x - self.bias)
f_ghost = t.exp(f_pre) * ghost_mask.to(f_pre)
f = nn.ReLU()(f_pre)
x_ghost = self.decoder(f_ghost) # note that this only applies the decoder weight matrix, no bias
x_hat = self.decode(f)
if output_features:
return x_hat, x_ghost, f
else:
return x_hat, x_ghost
def from_pretrained(path, device=None):
"""
Load a pretrained autoencoder from a file.
"""
state_dict = t.load(path)
dict_size, activation_dim = state_dict['encoder.weight'].shape
autoencoder = AutoEncoder(activation_dim, dict_size)
autoencoder.load_state_dict(state_dict)
if device is not None:
autoencoder.to(device)
return autoencoder
class IdentityDict(Dictionary, nn.Module):
"""
An identity dictionary, i.e. the identity function.
"""
def __init__(self, activation_dim=None):
super().__init__()
self.activation_dim = activation_dim
self.dict_size = activation_dim
def encode(self, x):
return x
def decode(self, f):
return f
def forward(self, x, output_features=False, ghost_mask=None):
if output_features:
return x, x
else:
return x
class GatedAutoEncoder(Dictionary, nn.Module):
"""
An autoencoder with separate gating and magnitude networks.
"""
def __init__(self, activation_dim, dict_size, initialization='default', device=None):
super().__init__()
self.activation_dim = activation_dim
self.dict_size = dict_size
self.decoder_bias = nn.Parameter(t.empty(activation_dim, device=device))
self.encoder = nn.Linear(activation_dim, dict_size, bias=False, device=device)
self.r_mag = nn.Parameter(t.empty(dict_size, device=device))
self.gate_bias = nn.Parameter(t.empty(dict_size, device=device))
self.mag_bias = nn.Parameter(t.empty(dict_size, device=device))
self.decoder = nn.Linear(dict_size, activation_dim, bias=False, device=device)
if initialization == 'default':
self._reset_parameters()
else:
initialization(self)
def _reset_parameters(self):
"""
Default method for initializing GatedSAE weights.
"""
# biases are initialized to zero
init.zeros_(self.decoder_bias)
init.zeros_(self.r_mag)
init.zeros_(self.gate_bias)
init.zeros_(self.mag_bias)
# decoder weights are initialized to random unit vectors
dec_weight = t.randn_like(self.decoder.weight)
dec_weight = dec_weight / dec_weight.norm(dim=0, keepdim=True)
self.decoder.weight = nn.Parameter(dec_weight)
def encode(self, x, return_gate=False):
"""
Returns features, gate value (pre-Heavyside)
"""
x_enc = self.encoder(x - self.decoder_bias)
# gating network
pi_gate = x_enc + self.gate_bias
f_gate = (pi_gate > 0).float()
# magnitude network
pi_mag = self.r_mag.exp() * x_enc + self.mag_bias
f_mag = nn.ReLU()(pi_mag)
f = f_gate * f_mag
if return_gate:
return f, nn.ReLU()(pi_gate)
return f
def decode(self, f):
return self.decoder(f) + self.decoder_bias
def forward(self, x, output_features=False):
f = self.encode(x)
x_hat = self.decode(f)
# TODO: modify so that x_hat depends on f
f = f * self.decoder.weight.norm(dim=0, keepdim=True)
if output_features:
return x_hat, f
else:
return x_hat
def from_pretrained(path, device=None):
"""
Load a pretrained autoencoder from a file.
"""
state_dict = t.load(path)
dict_size, activation_dim = state_dict['encoder.weight'].shape
autoencoder = GatedAutoEncoder(activation_dim, dict_size)
autoencoder.load_state_dict(state_dict)
if device is not None:
autoencoder.to(device)
return autoencoder
class JumpAutoEncoder(Dictionary, nn.Module):
"""
An autoencoder with jump ReLUs. Replacement for GatedAutoEncoder.
"""
def __init__(self, activation_dim, dict_size):
super().__init__()
self.activation_dim = activation_dim
self.dict_size = dict_size
self.bias = nn.Parameter(t.zeros(activation_dim))
self.encoder = nn.Linear(activation_dim, dict_size, bias=True)
# jump values added to activated features
self.jump = nn.Parameter(t.zeros(dict_size))
# rows of decoder weight matrix are unit vectors
self.decoder = nn.Linear(dict_size, activation_dim, bias=False)
dec_weight = t.randn_like(self.decoder.weight)
dec_weight = dec_weight / dec_weight.norm(dim=0, keepdim=True)
self.decoder.weight = nn.Parameter(dec_weight)
def encode(self, x, output_pre_jump=False):
pre_jump = nn.ReLU()(self.encoder(x - self.bias))
f = pre_jump + self.jump * (pre_jump > 0)
if output_pre_jump:
return f, pre_jump
else:
return f
def decode(self, f):
return self.decoder(f) + self.bias
def forward(self, x, output_features=False, output_pre_jump=False):
"""
Forward pass of an autoencoder.
x : activations to be autoencoded
output_features : if True, return the encoded features (and their pre-jump version) as well as the decoded x
"""
f, pre_jump = self.encode(x, output_pre_jump=True)
x_hat = self.decode(f)
if output_pre_jump:
return x_hat, f, pre_jump
elif output_features:
return x_hat, f
else:
return x_hat
def from_pretrained(path, device=None):
"""
Load a pretrained autoencoder from a file.
"""
state_dict = t.load(path)
dict_size, activation_dim = state_dict['encoder.weight'].shape
autoencoder = JumpAutoEncoder(activation_dim, dict_size)
autoencoder.load_state_dict(state_dict)
if device is not None:
autoencoder.to(device)
return autoencoder
# TODO merge this with AutoEncoder
class AutoEncoderNew(Dictionary, nn.Module):
"""
The autoencoder architecture and initialization used in https://transformer-circuits.pub/2024/april-update/index.html#training-saes
"""
def __init__(self, activation_dim, dict_size):
super().__init__()
self.activation_dim = activation_dim
self.dict_size = dict_size
self.encoder = nn.Linear(activation_dim, dict_size, bias=True)
self.decoder = nn.Linear(dict_size, activation_dim, bias=True)
# initialize encoder and decoder weights
w = t.randn(activation_dim, dict_size)
## normalize columns of w
w = w / w.norm(dim=0, keepdim=True) * 0.1
## set encoder and decoder weights
self.encoder.weight = nn.Parameter(w.clone().T)
self.decoder.weight = nn.Parameter(w.clone())
# initialize biases to zeros
init.zeros_(self.encoder.bias)
init.zeros_(self.decoder.bias)
def encode(self, x):
return nn.ReLU()(self.encoder(x))
def decode(self, f):
return self.decoder(f)
def forward(self, x, output_features=False):
"""
Forward pass of an autoencoder.
x : activations to be autoencoded
"""
if not output_features:
return self.decode(self.encode(x))
else: # TODO rewrite so that x_hat depends on f
f = self.encode(x)
x_hat = self.decode(f)
# multiply f by decoder column norms
f = f * self.decoder.weight.norm(dim=0, keepdim=True)
return x_hat, f
def from_pretrained(path, device=None):
"""
Load a pretrained autoencoder from a file.
"""
state_dict = t.load(path)
dict_size, activation_dim = state_dict['encoder.weight'].shape
autoencoder = AutoEncoderNew(activation_dim, dict_size)
autoencoder.load_state_dict(state_dict)
if device is not None:
autoencoder.to(device)
return autoencoder