forked from wesg52/world-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_gen_experiment.py
195 lines (154 loc) · 6.84 KB
/
prompt_gen_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
import os
import pickle
import argparse
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.evaluation import *
from feature_datasets import common
from probe_experiment import *
import warnings
warnings.filterwarnings(
'ignore', category=UserWarning,
message='TypedStorage is deprecated')
def save_experiment(args, results, layer):
save_path = os.path.join(
os.getenv('RESULTS_DIR', 'results'),
args.experiment_name,
args.model,
args.entity_type,
args.feature_name,
'prompt_gen_eval'
)
os.makedirs(save_path, exist_ok=True)
result_name = f'{layer}.p'
pickle.dump(
results,
open(os.path.join(save_path, result_name), 'wb')
)
def place_probe_generalization_experiment(activation_dict, probe_dict, target, is_test):
train_target = target[~is_test]
test_target = target[is_test]
y_mean = train_target.mean(axis=0)
y_std = train_target.std(axis=0)
scores = {}
for prompt_a, activations_a in activation_dict.items():
for prompt_b, (probe_b, bias_b) in probe_dict.items():
activations_b = activation_dict[prompt_b]
a_minus_b_dir = activations_a.mean(
axis=0) - activations_b.mean(axis=0)
a_proj_b = activations_a @ probe_b + bias_b
a_proj_b_centered = (
activations_a - a_minus_b_dir) @ probe_b + bias_b
a_proj_b_unnorm = a_proj_b * y_std + y_mean
a_proj_b_centered_unnorm = a_proj_b_centered * y_std + y_mean
train_scores = score_place_probe(
train_target, a_proj_b_unnorm[~is_test], use_haversine=True)
test_scores = score_place_probe(
test_target, a_proj_b_unnorm[is_test], use_haversine=True)
train_scores_centered = score_place_probe(
train_target, a_proj_b_centered_unnorm[~is_test], use_haversine=True)
test_scores_centered = score_place_probe(
test_target, a_proj_b_centered_unnorm[is_test], use_haversine=True)
scores[prompt_a, prompt_b, 'train', 'uncentered'] = train_scores
scores[prompt_a, prompt_b, 'test', 'uncentered'] = test_scores
scores[prompt_a, prompt_b, 'train',
'centered'] = train_scores_centered
scores[prompt_a, prompt_b, 'test',
'centered'] = test_scores_centered
return scores
def time_probe_generalization_experiment(activation_dict, probe_dict, target, is_test):
train_target = target[~is_test]
test_target = target[is_test]
y_mean = train_target.mean()
y_std = train_target.std()
scores = {}
for prompt_a, activations_a in activation_dict.items():
for prompt_b, (probe_b, bias_b) in probe_dict.items():
activations_b = activation_dict[prompt_b]
a_minus_b_dir = activations_a.mean(
axis=0) - activations_b.mean(axis=0)
a_proj_b = activations_a @ probe_b + bias_b
a_proj_b_centered = (
activations_a - a_minus_b_dir) @ probe_b + bias_b
a_proj_b_unnorm = a_proj_b * y_std + y_mean
a_proj_b_centered_unnorm = a_proj_b_centered * y_std + y_mean
train_scores = score_time_probe(
train_target, a_proj_b_unnorm[~is_test])
test_scores = score_time_probe(
test_target, a_proj_b_unnorm[is_test])
train_scores_centered = score_time_probe(
train_target, a_proj_b_centered_unnorm[~is_test])
test_scores_centered = score_time_probe(
test_target, a_proj_b_centered_unnorm[is_test])
scores[prompt_a, prompt_b, 'train', 'uncentered'] = train_scores
scores[prompt_a, prompt_b, 'test', 'uncentered'] = test_scores
scores[prompt_a, prompt_b, 'train',
'centered'] = train_scores_centered
scores[prompt_a, prompt_b, 'test',
'centered'] = test_scores_centered
return scores
MODEL_LAYER = {
'Llama-2-7b-hf': 20,
'Llama-2-13b-hf': 22,
'Llama-2-70b-hf': 50,
}
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(
'--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_names', type=str, nargs='+',
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(
'--layer', type=int, default=20)
args = parser.parse_args()
# n_layers = MODEL_N_LAYERS[args.model]
layer = MODEL_LAYER[args.model]
entity_df = common.load_entity_data(args.entity_type)
is_test = entity_df.is_test.values
print(timestamp(),
f'running prompt exp on {args.model} {args.entity_type}.{args.feature_name}.{layer}')
target = get_target_values(entity_df, args.feature_name)
activation_dict = {}
probe_dict = {}
for prompt in args.prompt_names:
# load data
activations = load_activation_probing_dataset_args(
args, prompt, layer).dequantize().numpy()
probe_result = load_probe_results(
args.experiment_name, args.model, args.entity_type, args.feature_name, prompt)
activation_dict[prompt] = activations
probe_dict[prompt] = (
probe_result['probe_directions'][layer],
probe_result['probe_biases'][layer]
)
is_place = args.entity_type in set(
['world_place', 'us_place', 'nyc_place'])
if is_place:
results = place_probe_generalization_experiment(
activation_dict, probe_dict, target, is_test)
else:
results = time_probe_generalization_experiment(
activation_dict, probe_dict, target, is_test)
save_experiment(args, results, layer)