-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcorrelations.py
362 lines (295 loc) · 12.9 KB
/
correlations.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
import os
import time
import tqdm
import torch as t
import einops
import datasets
import argparse
from utils import *
from functools import partial
from torch.utils.data import DataLoader
from transformer_lens import HookedTransformer
from transformer_lens.utils import gelu_fast
from analysis.correlations import summarize_correlation_matrix, flatten_layers
class StreamingCosineSimComputer:
def __init__(self, model_1, model_2, device='cpu'):
m1_layers = model_1.cfg.n_layers
m2_layers = model_2.cfg.n_layers
m1_dmlp = model_1.cfg.d_mlp
m2_dmlp = model_2.cfg.d_mlp
self.m1_sum_sq = t.zeros(
(m1_layers, m1_dmlp), dtype=t.float64, device=device)
self.m2_sum_sq = t.zeros(
(m2_layers, m2_dmlp), dtype=t.float64, device=device)
self.m1_m2_sum = t.zeros(
(m1_layers, m1_dmlp, m2_layers, m2_dmlp),
dtype=t.float64, device=device
)
def update_correlation_data(self, batch_1_acts, batch_2_acts):
self.m1_sum_sq += (batch_1_acts**2).sum(dim=-1)
self.m2_sum_sq += (batch_2_acts**2).sum(dim=-1)
self.m1_m2_sum += einops.einsum(
batch_1_acts, batch_2_acts, 'l1 n1 t, l2 n2 t -> l1 n1 l2 n2'
)
def compute_correlation(self):
layer_correlations = []
# compute layerwise for memory efficiency
for l1 in range(self.m1_sum_sq.shape[0]):
numerator = self.m1_m2_sum[l1, :, :, :]
denominator = einops.einsum(
self.m1_sum_sq[l1, :]**0.5,
self.m2_sum_sq**0.5,
'n1, l2 n2 -> n1 l2 n2'
)
l_correlation = numerator / denominator
layer_correlations.append(l_correlation.to(t.float16))
correlation = t.stack(layer_correlations, dim=0)
return correlation
class StreamingPearsonComputer:
def __init__(self, model_1, model_2, device='cpu'):
m1_layers = model_1.cfg.n_layers
m2_layers = model_2.cfg.n_layers
m1_dmlp = model_1.cfg.d_mlp
m2_dmlp = model_2.cfg.d_mlp
self.m1_sum = t.zeros(
(m1_layers, m1_dmlp), dtype=t.float32, device=device)
self.m1_sum_sq = t.zeros(
(m1_layers, m1_dmlp), dtype=t.float32, device=device)
self.m2_sum = t.zeros(
(m2_layers, m2_dmlp), dtype=t.float32, device=device)
self.m2_sum_sq = t.zeros(
(m2_layers, m2_dmlp), dtype=t.float32, device=device)
self.m1_m2_sum = t.zeros(
(m1_layers, m1_dmlp, m2_layers, m2_dmlp),
dtype=t.float32, device=device
)
self.n = 0
def update_correlation_data(self, batch_1_acts, batch_2_acts):
self.m1_sum += batch_1_acts.sum(dim=-1)
self.m1_sum_sq += (batch_1_acts**2).sum(dim=-1)
self.m2_sum += batch_2_acts.sum(dim=-1)
self.m2_sum_sq += (batch_2_acts**2).sum(dim=-1)
# TODO: reduce memory consumption (consider doing layerwise)
# for large models may need to do disk caching
self.m1_m2_sum += einops.einsum(
batch_1_acts, batch_2_acts, 'l1 n1 t, l2 n2 t -> l1 n1 l2 n2'
)
self.n += batch_1_acts.shape[-1]
def compute_correlation(self):
layer_correlations = []
# compute layerwise for memory efficiency
for l1 in range(self.m1_sum.shape[0]):
numerator = self.m1_m2_sum[l1, :, :, :] - (1 / self.n) * einops.einsum(
self.m1_sum[l1, :], self.m2_sum, 'n1, l2 n2 -> n1 l2 n2')
m1_norm = (self.m1_sum_sq[l1, :] -
(1 / self.n) * self.m1_sum[l1, :]**2)**0.5
m2_norm = (self.m2_sum_sq - (1 / self.n) * self.m2_sum**2)**0.5
l_correlation = numerator / einops.einsum(
m1_norm, m2_norm, 'n1, l2 n2 -> n1 l2 n2'
)
layer_correlations.append(l_correlation.to(t.float16))
correlation = t.stack(layer_correlations, dim=0)
return correlation
class StreamingJaccardComputer:
def __init__(self, model_1, model_2, threshold=0, device='cpu'):
m1_layers = model_1.cfg.n_layers
m2_layers = model_2.cfg.n_layers
m1_dmlp = model_1.cfg.d_mlp
m2_dmlp = model_2.cfg.d_mlp
self.threshold = threshold
self.m1_intersection_m2_sum = t.zeros(
(m1_layers, m1_dmlp, m2_layers, m2_dmlp),
dtype=t.int32, device=device
)
self.m1_sum = t.zeros(
(m1_layers, m1_dmlp), dtype=t.int32, device=device)
self.m2_sum = t.zeros(
(m2_layers, m2_dmlp), dtype=t.int32, device=device)
def update_correlation_data(self, batch_1_acts, batch_2_acts):
# casting to float rather than int because pytorch doesn't parallelize int ops
batch_1_acts = (batch_1_acts > self.threshold).float()
batch_2_acts = (batch_2_acts > self.threshold).float()
self.m1_intersection_m2_sum += einops.einsum(
batch_1_acts, batch_2_acts, 'l1 n1 t, l2 n2 t -> l1 n1 l2 n2').int()
self.m1_sum += batch_1_acts.sum(dim=-1).int()
self.m2_sum += batch_2_acts.sum(dim=-1).int()
def compute_correlation(self):
layer_correlations = []
# compute layerwise for memory efficiency
for l1 in range(self.m1_sum.shape[0]):
l_correlation = self.m1_intersection_m2_sum[l1, :, :, :] / \
(self.m1_sum[l1, :][:, None, None] + self.m2_sum -
self.m1_intersection_m2_sum[l1, :, :, :])
layer_correlations.append(l_correlation.to(t.float16))
correlation = t.stack(layer_correlations, dim=0)
return correlation
def save_activation_hook(tensor, hook, device='cpu'):
hook.ctx['activation'] = tensor.detach().to(device)
def get_activations(model, inputs, filter_padding=True):
"""Get the activations for a given model and dataset.
Inputs should already be appropriately batched
inputs: (n_tokens, n_sequences) 512 x 32 by default
out: (n_tokens, n_sequences, (n_layers * d_mlp))
"""
hooks = [
(f'blocks.{layer_ix}.mlp.hook_post',
partial(save_activation_hook, device=args.correlation_device))
for layer_ix in range(model.cfg.n_layers)
]
with t.no_grad():
model.run_with_hooks(
inputs,
fwd_hooks=hooks,
stop_at_layer=model.cfg.n_layers+1 # don't compute logits to save memory
)
activations = torch.stack(
[model.hook_dict[hook_pt[0]].ctx['activation'] for hook_pt in hooks], dim=2)
model.reset_hooks()
activations = einops.rearrange(
activations, 'batch context l n -> l n (batch context)')
if filter_padding:
# In Pythia and GPT2, pad and bos tokens are the same id
pad_token = model.tokenizer.pad_token_id
not_padding = (inputs.flatten() != pad_token).to(activations.device)
activations = activations[:, :, not_padding]
return activations
def run_correlation_experiment(args, model_1, model_2, token_dataset):
# set up the streaming correlation data structures
if args.similarity_type == 'pearson':
corr_computer = StreamingPearsonComputer(
model_1, model_2, device=args.correlation_device)
elif args.similarity_type == 'jaccard':
corr_computer = StreamingJaccardComputer(
model_1, model_2, device=args.correlation_device)
elif args.similarity_type == 'cosine':
corr_computer = StreamingCosineSimComputer(
model_1, model_2, device=args.correlation_device)
else:
raise ValueError(f'Invalid similarity type: {args.similarity_type}')
if args.baseline == 'rotation':
# TODO: consider making this actually orthogonal
# eg, scipy.stats.special_ortho_group
# see https://math.stackexchange.com/questions/3839152/sample-a-random-rotation-in-n-dimensions
rotation_matrix = t.randn(
(model_2.cfg.n_layers, model_2.cfg.d_mlp, model_2.cfg.d_mlp))
rotation_matrix /= rotation_matrix.norm(dim=-1, keepdim=True)
rotation_matrix = rotation_matrix.to(args.model_2_device)
dataloader = DataLoader(
token_dataset['tokens'], batch_size=args.batch_size, shuffle=False)
start_time = time.time()
# run models
for step, batch in enumerate(tqdm.tqdm(dataloader)):
m1_activations = get_activations(
model_1, batch.to(args.model_1_device))
# do special processing for the baselines
if args.baseline != 'gaussian':
m2_activations = get_activations(
model_2, batch.to(args.model_2_device))
else:
m2_activations = t.randn(
(model_2.cfg.n_layers, model_2.cfg.d_mlp,
m1_activations.shape[-1]),
device=args.model_2_device)
m2_activations = gelu_fast(m2_activations)
if args.baseline == 'permutation':
# shuffle the activations along the token dimension
perm = t.randperm(m2_activations.shape[-1]).to(args.model_2_device)
m2_activations = m2_activations[:, :, perm]
if args.baseline == 'rotation':
# rotate the neuron basis
rotated_acts = []
for l in range(m2_activations.shape[0]):
rotated_acts.append(
t.einsum(
'n t, m n -> m t',
m2_activations[l, :, :].to(rotation_matrix.device),
rotation_matrix[l, :, :]
).to(args.correlation_device)
)
m2_activations = t.stack(rotated_acts, dim=0)
corr_computer.update_correlation_data(m1_activations, m2_activations)
correlation = corr_computer.compute_correlation()
end_time = time.time()
print(f'Correlation computation took {end_time - start_time:.2f} seconds')
print(correlation)
return correlation
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model_1_name', default='pythia-70m',
help='Name of model from TransformerLens')
parser.add_argument(
'--model_2_name', default='pythia-70m-v0')
parser.add_argument(
'--token_dataset', type=str)
parser.add_argument(
'--baseline', type=str, default='none',
choices=['none', 'gaussian', 'permutation', 'rotation'])
parser.add_argument(
'--similarity_type', type=str, default='pearson',
choices=['pearson', 'jaccard', 'cosine'])
parser.add_argument(
'--jaccard_threshold', type=float, default=0)
parser.add_argument(
'--batch_size', default=32, type=int)
parser.add_argument(
'--model_1_device', type=str, default='cpu')
parser.add_argument(
'--model_2_device', type=str, default='cpu')
parser.add_argument(
'--correlation_device', type=str, default='cpu')
parser.add_argument(
'--save_full_correlation_matrix', action='store_true',
help='Whether to save the full correlation matrix (always save the summary)')
parser.add_argument(
'--save_precision', type=int, default=16, choices=[8, 16, 32],
help='Number of bits to use for saving full correlation matrix')
args = parser.parse_args()
t.autograd.set_grad_enabled(False)
print(f"Visible CUDA devices: {t.cuda.device_count()}")
model_1 = HookedTransformer.from_pretrained(
args.model_1_name, device='cpu')
model_1.to(args.model_1_device)
model_1.eval()
model_2 = HookedTransformer.from_pretrained(
args.model_2_name, device='cpu')
model_2.to(args.model_2_device)
model_2.eval()
model_family = get_model_family(args.model_1_name)
tokenized_dataset = datasets.load_from_disk(
os.path.join(
os.getenv('DATASET_DIR', 'token_datasets'),
model_family,
args.token_dataset
)
)
correlation = run_correlation_experiment(
args, model_1, model_2, tokenized_dataset)
similarity_type = f'jaccard-{args.jaccard_threshold:.2f}'\
if args.similarity_type == 'jaccard' \
else args.similarity_type
save_path = os.path.join(
os.getenv('RESULTS_DIR', 'correlation_results'),
args.model_1_name + '+' + args.model_2_name,
args.token_dataset,
f'{similarity_type}.{args.baseline}'
)
os.makedirs(save_path, exist_ok=True)
if args.save_full_correlation_matrix:
torch.save(
adjust_precision(correlation, args.save_precision),
os.path.join(save_path, 'correlation.pt')
)
# save both the summary and the summary on the transpose
correlation = flatten_layers(correlation.cpu()).to(torch.float32)
corr_summary = summarize_correlation_matrix(correlation)
corr_summary_T = summarize_correlation_matrix(correlation.T)
torch.save(
corr_summary,
os.path.join(save_path, 'correlation_summary.pt')
)
torch.save(
corr_summary_T,
os.path.join(save_path, 'correlation_summary_T.pt')
)