forked from wesg52/world-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_experiment.py
379 lines (295 loc) · 12 KB
/
probe_experiment.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
import os
import pickle
import argparse
import tqdm
import numpy as np
import pandas as pd
from utils import timestamp, MODEL_N_LAYERS, get_model_family
from make_prompt_datasets import ENTITY_PROMPTS
from save_activations import load_activation_probing_dataset, load_activation_probing_dataset_args
from probes import baseline
from probes import rank
from probes.evaluation import *
from feature_datasets import common
from sklearn.linear_model import Ridge, RidgeCV
from sklearn import metrics
from scipy import stats
import warnings
warnings.filterwarnings(
'ignore', category=UserWarning,
message='TypedStorage is deprecated')
def save_probe_results(args, probe_results, prompt, pca=False):
save_path = os.path.join(
os.getenv('RESULTS_DIR', 'results'),
args.experiment_name,
args.model,
args.entity_type,
args.feature_name,
'probes' if not pca else 'pca_probes'
)
os.makedirs(save_path, exist_ok=True)
probe_metadata = [
args.experiment_type,
args.normalization_type,
args.label_processing,
args.activation_aggregation,
prompt
]
probe_name = '.'.join(probe_metadata) + '.p'
pickle.dump(
probe_results,
open(os.path.join(save_path, probe_name), 'wb')
)
def load_probe_results(
experiment_name, model, entity_type, feature_name, prompt,
experiment_type='lsq_train',
normalization_type='none',
label_processing='none',
activation_aggregation='last',
pca=False):
save_path = os.path.join(
os.getenv('RESULTS_DIR', 'results'),
experiment_name,
model,
entity_type,
feature_name,
'probes' if not pca else 'pca_probes'
)
os.makedirs(save_path, exist_ok=True)
probe_metadata = [
experiment_type,
normalization_type,
label_processing,
activation_aggregation,
prompt
]
probe_name = '.'.join(probe_metadata) + '.p'
probe_results = pickle.load(
open(os.path.join(save_path, probe_name), 'rb'))
return probe_results
def place_probe_experiment(activations, target, is_test, probe=None, is_lat_lon=True):
train_activations = activations[~is_test]
train_target = target[~is_test]
test_activations = activations[is_test]
test_target = target[is_test]
norm_train_target = (
train_target - train_target.mean(axis=0)) / train_target.std(axis=0)
if probe is None:
probe = Ridge(alpha=activations.shape[1])
probe.fit(train_activations, norm_train_target)
train_pred = probe.predict(train_activations)
test_pred = probe.predict(test_activations)
train_pred_unnorm = train_pred * \
train_target.std(axis=0) + train_target.mean(axis=0)
test_pred_unnorm = test_pred * \
train_target.std(axis=0) + train_target.mean(axis=0)
projection = probe.predict(activations) * \
train_target.std(axis=0) + train_target.mean(axis=0)
train_scores = score_place_probe(
train_target, train_pred_unnorm, use_haversine=is_lat_lon)
test_scores = score_place_probe(
test_target, test_pred_unnorm, use_haversine=is_lat_lon)
scores = {
**{('train', k): v for k, v in train_scores.items()},
**{('test', k): v for k, v in test_scores.items()},
}
error_matrix = compute_proximity_error_matrix(
target, projection, pairwise_haversine_distance)
train_error, test_error, combined_error = proximity_scores(
error_matrix, is_test)
scores['train', 'prox_error'] = train_error.mean()
scores['test', 'prox_error'] = test_error.mean()
projection_df = pd.DataFrame({
'x': projection[:, 0],
'y': projection[:, 1],
'is_test': is_test,
'x_error': projection[:, 0] - target[:, 0],
'y_error': projection[:, 1] - target[:, 1],
'prox_error': combined_error,
})
return probe, scores, projection_df
def time_probe_experiment(activations, target, is_test, probe=None):
train_activations = activations[~is_test]
train_target = target[~is_test]
test_activations = activations[is_test]
test_target = target[is_test]
norm_train_target = (train_target - train_target.mean()
) / train_target.std()
if probe is None:
probe = Ridge(alpha=activations.shape[1])
probe.fit(train_activations, norm_train_target)
train_pred = probe.predict(train_activations)
test_pred = probe.predict(test_activations)
train_pred_unnorm = train_pred * train_target.std() + train_target.mean()
test_pred_unnorm = test_pred * train_target.std() + train_target.mean()
projection = probe.predict(activations) * \
train_target.std() + train_target.mean()
train_scores = score_time_probe(train_target, train_pred_unnorm)
test_scores = score_time_probe(test_target, test_pred_unnorm)
scores = {
**{('train', k): v for k, v in train_scores.items()},
**{('test', k): v for k, v in test_scores.items()},
}
error_matrix = compute_proximity_error_matrix(
target, projection, pairwise_abs_distance_fn)
train_error, test_error, combined_error = proximity_scores(
error_matrix, is_test)
scores['train', 'prox_error'] = train_error.mean()
scores['test', 'prox_error'] = test_error.mean()
projection_df = pd.DataFrame({
'projection': projection,
'is_test': is_test,
'error': projection - target,
'prox_error': combined_error,
})
return probe, scores, projection_df
def get_target_values(entity_df, feature_name):
if feature_name == 'coords':
target = entity_df[['longitude', 'latitude']].values
elif feature_name.endswith('date') or feature_name.endswith('year'):
if feature_name == 'death_year':
target = entity_df[feature_name].values
else:
NS_PER_YEAR = 1e9 * 60 * 60 * 24 * 365.25
target = pd.to_datetime(entity_df[feature_name]).values
target = target.astype(np.int64) / NS_PER_YEAR
else:
raise ValueError(f'Unrecognized feature name: {feature_name}')
return target
MODEL_ALPHA = {
'Llama-2-7b-hf': 5000,
'Llama-2-13b-hf': 10000,
'Llama-2-70b-hf': 20000,
}
RIDGE_ALPHAS = {
'Llama-2-7b-hf': np.logspace(0.8, 4.1, 12),
'Llama-2-13b-hf': np.logspace(0.8, 4.3, 12),
'Llama-2-70b-hf': np.logspace(0.8, 4.5, 12),
}
def main_probe_experiment(args):
n_layers = MODEL_N_LAYERS[args.model]
entity_df = common.load_entity_data(args.entity_type)
is_test = entity_df.is_test.values
print(timestamp(),
f'running probe on {args.model} {args.experiment_type}.{args.feature_name}.{args.prompt_name}')
results = {
'scores': {},
'projections': {},
'probe_directions': {},
'probe_biases': {},
'probe_alphas': {},
}
for layer in tqdm.tqdm(range(n_layers)):
# load data
activations = load_activation_probing_dataset_args(
args, args.prompt_name, layer).dequantize()
if activations.isnan().any():
print(timestamp(), 'WARNING: nan activations, skipping layer', layer)
continue
target = get_target_values(entity_df, args.feature_name)
probe = RidgeCV(alphas=RIDGE_ALPHAS[args.model], store_cv_values=True)
is_place = args.entity_type.endswith('place')
if is_place:
probe, scores, projection = place_probe_experiment(
activations, target, is_test, probe=probe)
else:
probe, scores, projection = time_probe_experiment(
activations, target, is_test, probe=probe)
probe_direction = probe.coef_.T.astype(np.float16)
probe_alphas = probe.cv_values_.mean(axis=(0, 1) if is_place else 0)
results['scores'][layer] = scores
results['projections'][layer] = projection
results['probe_directions'][layer] = probe_direction
results['probe_biases'][layer] = probe.intercept_
results['probe_alphas'][layer] = probe_alphas
save_probe_results(args, results, args.prompt_name)
def pca_probe_experiment(args):
MODEL_LAYER = {
'Llama-2-7b-hf': 20,
'Llama-2-13b-hf': 24,
'Llama-2-70b-hf': 48,
}
layer = MODEL_LAYER[args.model]
entity_df = common.load_entity_data(args.entity_type)
is_test = entity_df.is_test.values
activations = load_activation_probing_dataset_args(
args, args.prompt_name, layer).dequantize()
PCA_DIMS = [2, 4, 6, 8, 10, 15, 20, 30,
40, 50, 75, 100, activations.shape[1]]
print(timestamp(),
f'running probe on {args.model} {args.experiment_type}.{args.feature_name}.{args.prompt_name}')
U, S, V = torch.pca_lowrank(activations, q=PCA_DIMS[-2])
results = {
'scores': {},
'projections': {},
'probe_directions': {},
'probe_biases': {},
'probe_alphas': {},
}
for pca_dim in tqdm.tqdm(PCA_DIMS):
# load data
if activations.isnan().any():
print(timestamp(), 'WARNING: nan activations, skipping layer', pca_dim)
continue
if pca_dim != PCA_DIMS[-1]:
pca_activations = activations @ V[:, :pca_dim]
else:
pca_activations = activations
target = get_target_values(entity_df, args.feature_name)
probe = RidgeCV(alphas=RIDGE_ALPHAS[args.model], store_cv_values=True)
is_place = args.entity_type.endswith('place')
if is_place:
probe, scores, projection = place_probe_experiment(
pca_activations, target, is_test, probe=probe)
else:
probe, scores, projection = time_probe_experiment(
pca_activations, target, is_test, probe=probe)
probe_direction = probe.coef_.T.astype(np.float16)
probe_alphas = probe.cv_values_.mean(axis=(0, 1) if is_place else 0)
results['scores'][pca_dim] = scores
results['projections'][pca_dim] = projection
results['probe_directions'][pca_dim] = probe_direction
results['probe_biases'][pca_dim] = probe.intercept_
results['probe_alphas'][pca_dim] = probe_alphas
save_probe_results(args, results, args.prompt_name, pca=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# experiment params
parser.add_argument(
'--experiment_name', type=str, help='Name of experiment for save dir')
parser.add_argument(
'--experiment_type', type=str, default='lsq_train',
help='Type of experiment: spearman_train, kendall_train, lsq_train')
parser.add_argument(
'--model', default='pythia-70m',
help='Name of model from TransformerLens')
parser.add_argument(
'--entity_type',
help='Name of feature collection (should be dir under processed_datasets/)')
parser.add_argument(
'--feature_name', type=str, default='coords',
help='Name of feature to probe, must be in FEATURE_PROMPT_MAPPINGS')
parser.add_argument(
'--prompt_name', type=str,
help='Name of prompt to use for probing, must key in <ENTITY>_PROMPTS')
parser.add_argument(
'--normalization_type', type=str, default='none',
help='Type of normalization to apply to activations before training')
parser.add_argument(
'--label_processing', type=str, default='none',
help='Type of weighting to apply to labels before training')
parser.add_argument(
'--activation_aggregation', default='last',
help='Average activations across all tokens in a sequence')
parser.add_argument(
'--pca', action='store_true')
# parser.add_argument(
# '--probe_device', default='cpu',
# help='Device to run probe training on (only relevant for kendall tau)')
# TODO: potentially add more probe type experiments
args = parser.parse_args()
if args.pca:
pca_probe_experiment(args)
else:
main_probe_experiment(args)