-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathstandard.py
289 lines (240 loc) · 10.7 KB
/
standard.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
"""
Implements the standard SAE training scheme.
"""
import torch as t
from typing import Optional
from ..trainers.trainer import SAETrainer, get_lr_schedule, get_sparsity_warmup_fn, ConstrainedAdam
from ..config import DEBUG
from ..dictionary import AutoEncoder
from collections import namedtuple
class StandardTrainer(SAETrainer):
"""
Standard SAE training scheme following Towards Monosemanticity. Decoder column norms are constrained to 1.
"""
def __init__(self,
steps: int, # total number of steps to train for
activation_dim: int,
dict_size: int,
layer: int,
lm_name: str,
dict_class=AutoEncoder,
lr:float=1e-3,
l1_penalty:float=1e-1,
warmup_steps:int=1000, # lr warmup period at start of training and after each resample
sparsity_warmup_steps:Optional[int]=2000, # sparsity warmup period at start of training
decay_start:Optional[int]=None, # decay learning rate after this many steps
resample_steps:Optional[int]=None, # how often to resample neurons
seed:Optional[int]=None,
device=None,
wandb_name:Optional[str]='StandardTrainer',
submodule_name:Optional[str]=None,
):
super().__init__(seed)
assert layer is not None and lm_name is not None
self.layer = layer
self.lm_name = lm_name
self.submodule_name = submodule_name
if seed is not None:
t.manual_seed(seed)
t.cuda.manual_seed_all(seed)
# initialize dictionary
self.ae = dict_class(activation_dim, dict_size)
self.lr = lr
self.l1_penalty=l1_penalty
self.warmup_steps = warmup_steps
self.sparsity_warmup_steps = sparsity_warmup_steps
self.steps = steps
self.decay_start = decay_start
self.wandb_name = wandb_name
if device is None:
self.device = 'cuda' if t.cuda.is_available() else 'cpu'
else:
self.device = device
self.ae.to(self.device)
self.resample_steps = resample_steps
if self.resample_steps is not None:
# how many steps since each neuron was last activated?
self.steps_since_active = t.zeros(self.ae.dict_size, dtype=int).to(self.device)
else:
self.steps_since_active = None
self.optimizer = ConstrainedAdam(self.ae.parameters(), self.ae.decoder.parameters(), lr=lr)
lr_fn = get_lr_schedule(steps, warmup_steps, decay_start, resample_steps, sparsity_warmup_steps)
self.scheduler = t.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lr_fn)
self.sparsity_warmup_fn = get_sparsity_warmup_fn(steps, sparsity_warmup_steps)
def resample_neurons(self, deads, activations):
with t.no_grad():
if deads.sum() == 0: return
print(f"resampling {deads.sum().item()} neurons")
# compute loss for each activation
losses = (activations - self.ae(activations)).norm(dim=-1)
# sample input to create encoder/decoder weights from
n_resample = min([deads.sum(), losses.shape[0]])
indices = t.multinomial(losses, num_samples=n_resample, replacement=False)
sampled_vecs = activations[indices]
# get norm of the living neurons
alive_norm = self.ae.encoder.weight[~deads].norm(dim=-1).mean()
# resample first n_resample dead neurons
deads[deads.nonzero()[n_resample:]] = False
self.ae.encoder.weight[deads] = sampled_vecs * alive_norm * 0.2
self.ae.decoder.weight[:,deads] = (sampled_vecs / sampled_vecs.norm(dim=-1, keepdim=True)).T
self.ae.encoder.bias[deads] = 0.
# reset Adam parameters for dead neurons
state_dict = self.optimizer.state_dict()['state']
## encoder weight
state_dict[1]['exp_avg'][deads] = 0.
state_dict[1]['exp_avg_sq'][deads] = 0.
## encoder bias
state_dict[2]['exp_avg'][deads] = 0.
state_dict[2]['exp_avg_sq'][deads] = 0.
## decoder weight
state_dict[3]['exp_avg'][:,deads] = 0.
state_dict[3]['exp_avg_sq'][:,deads] = 0.
def loss(self, x, step: int, logging=False, **kwargs):
sparsity_scale = self.sparsity_warmup_fn(step)
x_hat, f = self.ae(x, output_features=True)
l2_loss = t.linalg.norm(x - x_hat, dim=-1).mean()
recon_loss = (x - x_hat).pow(2).sum(dim=-1).mean()
l1_loss = f.norm(p=1, dim=-1).mean()
if self.steps_since_active is not None:
# update steps_since_active
deads = (f == 0).all(dim=0)
self.steps_since_active[deads] += 1
self.steps_since_active[~deads] = 0
loss = recon_loss + self.l1_penalty * sparsity_scale * l1_loss
if not logging:
return loss
else:
return namedtuple('LossLog', ['x', 'x_hat', 'f', 'losses'])(
x, x_hat, f,
{
'l2_loss' : l2_loss.item(),
'mse_loss' : recon_loss.item(),
'sparsity_loss' : l1_loss.item(),
'loss' : loss.item()
}
)
def update(self, step, activations):
activations = activations.to(self.device)
self.optimizer.zero_grad()
loss = self.loss(activations, step=step)
loss.backward()
self.optimizer.step()
self.scheduler.step()
if self.resample_steps is not None and step % self.resample_steps == 0:
self.resample_neurons(self.steps_since_active > self.resample_steps / 2, activations)
@property
def config(self):
return {
'dict_class': 'AutoEncoder',
'trainer_class' : 'StandardTrainer',
'activation_dim': self.ae.activation_dim,
'dict_size': self.ae.dict_size,
'lr' : self.lr,
'l1_penalty' : self.l1_penalty,
'warmup_steps' : self.warmup_steps,
'resample_steps' : self.resample_steps,
'sparsity_warmup_steps' : self.sparsity_warmup_steps,
'steps' : self.steps,
'decay_start' : self.decay_start,
'seed' : self.seed,
'device' : self.device,
'layer' : self.layer,
'lm_name' : self.lm_name,
'wandb_name': self.wandb_name,
'submodule_name': self.submodule_name,
}
class StandardTrainerAprilUpdate(SAETrainer):
"""
Standard SAE training scheme following the Anthropic April update. Decoder column norms are NOT constrained to 1.
This trainer does not support resampling or ghost gradients. This trainer will have fewer dead neurons than the standard trainer.
"""
def __init__(self,
steps: int, # total number of steps to train for
activation_dim: int,
dict_size: int,
layer: int,
lm_name: str,
dict_class=AutoEncoder,
lr:float=1e-3,
l1_penalty:float=1e-1,
warmup_steps:int=1000, # lr warmup period at start of training
sparsity_warmup_steps:Optional[int]=2000, # sparsity warmup period at start of training
decay_start:Optional[int]=None, # decay learning rate after this many steps
seed:Optional[int]=None,
device=None,
wandb_name:Optional[str]='StandardTrainerAprilUpdate',
submodule_name:Optional[str]=None,
):
super().__init__(seed)
assert layer is not None and lm_name is not None
self.layer = layer
self.lm_name = lm_name
self.submodule_name = submodule_name
if seed is not None:
t.manual_seed(seed)
t.cuda.manual_seed_all(seed)
# initialize dictionary
self.ae = dict_class(activation_dim, dict_size)
self.lr = lr
self.l1_penalty=l1_penalty
self.warmup_steps = warmup_steps
self.sparsity_warmup_steps = sparsity_warmup_steps
self.steps = steps
self.decay_start = decay_start
self.wandb_name = wandb_name
if device is None:
self.device = 'cuda' if t.cuda.is_available() else 'cpu'
else:
self.device = device
self.ae.to(self.device)
self.optimizer = t.optim.Adam(self.ae.parameters(), lr=lr)
lr_fn = get_lr_schedule(steps, warmup_steps, decay_start, resample_steps=None, sparsity_warmup_steps=sparsity_warmup_steps)
self.scheduler = t.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lr_fn)
self.sparsity_warmup_fn = get_sparsity_warmup_fn(steps, sparsity_warmup_steps)
def loss(self, x, step: int, logging=False, **kwargs):
sparsity_scale = self.sparsity_warmup_fn(step)
x_hat, f = self.ae(x, output_features=True)
l2_loss = t.linalg.norm(x - x_hat, dim=-1).mean()
recon_loss = (x - x_hat).pow(2).sum(dim=-1).mean()
l1_loss = (f * self.ae.decoder.weight.norm(p=2, dim=0)).sum(dim=-1).mean()
loss = recon_loss + self.l1_penalty * sparsity_scale * l1_loss
if not logging:
return loss
else:
return namedtuple('LossLog', ['x', 'x_hat', 'f', 'losses'])(
x, x_hat, f,
{
'l2_loss' : l2_loss.item(),
'mse_loss' : recon_loss.item(),
'sparsity_loss' : l1_loss.item(),
'loss' : loss.item()
}
)
def update(self, step, activations):
activations = activations.to(self.device)
self.optimizer.zero_grad()
loss = self.loss(activations, step=step)
loss.backward()
t.nn.utils.clip_grad_norm_(self.ae.parameters(), 1.0)
self.optimizer.step()
self.scheduler.step()
@property
def config(self):
return {
'dict_class': 'AutoEncoder',
'trainer_class' : 'StandardTrainerAprilUpdate',
'activation_dim': self.ae.activation_dim,
'dict_size': self.ae.dict_size,
'lr' : self.lr,
'l1_penalty' : self.l1_penalty,
'warmup_steps' : self.warmup_steps,
'sparsity_warmup_steps' : self.sparsity_warmup_steps,
'steps' : self.steps,
'decay_start' : self.decay_start,
'seed' : self.seed,
'device' : self.device,
'layer' : self.layer,
'lm_name' : self.lm_name,
'wandb_name': self.wandb_name,
'submodule_name': self.submodule_name,
}