forked from sebastianGehrmann/CausalMediationAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron_intervention_subset_selection.py
456 lines (376 loc) · 17.6 KB
/
neuron_intervention_subset_selection.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# -*- coding: utf-8 -*-
import gc
import os
import pickle
import random
from argparse import ArgumentParser
import numpy as np
import pandas as pd
import torch
from transformers import GPT2Tokenizer
from experiment import Intervention, Model
from utils import convert_results_to_pd
np.random.seed(1)
torch.manual_seed(1)
def get_template_list():
# Get list of all considered templates
# "That" sentences are ours
# "Because" sentences are a subset
# from https://arxiv.org/pdf/1807.11714.pdf (Lu et al.)
return ["The {} said that",
"The {} yelled that",
"The {} whispered that",
"The {} wanted that",
"The {} desired that",
"The {} wished that",
"The {} ate because",
"The {} ran because",
"The {} drove because",
"The {} slept because",
"The {} cried because",
"The {} laughed because",
"The {} went home because",
"The {} stayed up because",
"The {} was fired because",
"The {} was promoted because",
"The {} yelled because"]
def get_intervention_types():
return ['man_indirect',
'woman_indirect']
def construct_interventions(base_sent, tokenizer, DEVICE, gender='female'):
interventions = {}
if gender == 'female':
filename = 'experiment_data/professions_female_stereo.json'
else:
filename = 'experiment_data/professions_male_stereo.json'
with open(filename, 'r') as f:
all_word_count = 0
used_word_count = 0
for l in f:
# there is only one line that eval's to an array
for j in eval(l):
all_word_count += 1
biased_word = j[0]
try:
interventions[biased_word] = Intervention(
tokenizer,
base_sent,
[biased_word, "man", "woman"],
["he", "she"],
device=DEVICE)
used_word_count += 1
except:
pass
# print("excepted {} due to tokenizer splitting.".format(
# biased_word))
print("Only used {}/{} neutral words due to tokenizer".format(
used_word_count, all_word_count))
return interventions
def compute_odds_ratio(df, gender='female', col='odds_ratio'):
# filter some stuff out
df['profession'] = df['base_string'].apply(lambda s: s.split()[1])
df['definitional'] = df['profession'].apply(get_stereotypicality)
df = df.loc[df['definitional'] < 0.75, :]
df = df[df['candidate2_base_prob'] > 0.01]
df = df[df['candidate1_base_prob'] > 0.01]
if gender == 'female':
odds_base = df['candidate1_base_prob'] / df['candidate2_base_prob']
odds_intervention = df['candidate1_prob'] / df['candidate2_prob']
else:
odds_base = df['candidate2_base_prob'] / df['candidate1_base_prob']
odds_intervention = df['candidate2_prob'] / df['candidate1_prob']
odds_ratio = odds_intervention / odds_base
df[col] = odds_ratio
return df
def sort_odds_obj(df):
df['odds_diff'] = df['odds_ratio'].apply(lambda x: x-1)
df_sorted = df.sort_values(by=['odds_diff'], ascending=False)
return df_sorted
def get_stereotypicality(vals):
return abs(profession_stereotypicality[vals]['definitional'])
profession_stereotypicality = {}
with open("experiment_data/professions.json") as f:
for l in f:
for p in eval(l):
profession_stereotypicality[p[0]] = {
'stereotypicality': p[2],
'definitional': p[1],
'total': p[2]+p[1],
'max': max([p[2],p[1]], key=abs)}
# get global list
def get_all_contrib(templates, tokenizer, out_dir=''):
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female')
male_df = get_intervention_results(templates, tokenizer, gender='male')
gc.collect()
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
female_df = female_df[['layer','neuron', 'odds_ratio']]
male_df = male_df[['layer','neuron', 'odds_ratio']]
gc.collect()
# merge and average
df = pd.concat([female_df, male_df])
df = df.groupby(['layer','neuron'], as_index=False).mean()
df_sorted = sort_odds_obj(df)
layer_list = df_sorted['layer'].values
neuron_list = df_sorted['neuron'].values
odds_list = df_sorted['odds_ratio'].values
marg_contrib = {}
marg_contrib['layer'] = layer_list
marg_contrib['neuron'] = neuron_list
marg_contrib['val'] = odds_list
pickle.dump(marg_contrib, open(out_dir + "/marg_contrib_" + model_type + ".pickle", "wb" ))
return layer_list, neuron_list
def get_intervention_results(templates, tokenizer, DEVICE='cuda', gender='female',
layers_to_adj=[], neurons_to_adj=[], intervention_loc='all',
df_layer=None, df_neuron=None):
if gender == 'female':
intervention_type = 'man_indirect'
else:
intervention_type = 'woman_indirect'
df = []
for template in templates:
# pickle.dump(template + "_" + gender, open("results/log.pickle", "wb" ) )
interventions = construct_interventions(template, tokenizer, DEVICE, gender)
intervention_results = model.neuron_intervention_experiment(interventions, intervention_type,
layers_to_adj=layers_to_adj, neurons_to_adj=neurons_to_adj,
intervention_loc=intervention_loc)
df_template = convert_results_to_pd(interventions, intervention_results, df_layer, df_neuron)
# calc odds ratio and odds-abs
df.append(df_template)
gc.collect()
return pd.concat(df)
def get_neuron_intervention_results(templates, tokenizer, layers, neurons):
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=layers, neurons_to_adj=[neurons], intervention_loc='neuron',
df_layer=layers, df_neuron=neurons[0])
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=layers, neurons_to_adj=[neurons], intervention_loc='neuron',
df_layer=layers, df_neuron=neurons[0])
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
df = pd.concat([female_df, male_df])
return df['odds_ratio'].mean()
def top_k_by_layer(model, model_type, tokenizer, templates, layer, layer_list, neuron_list, k=50, out_dir=''):
layer_2_ind = np.where(layer_list == layer)[0]
neuron_2 = neuron_list[layer_2_ind]
odd_abs_list = []
for i in range(k):
print(i)
temp_list = list(neuron_2[:i+1])
neurons = [temp_list]
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=len(temp_list)*[layer], neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=len(temp_list)*[layer], neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
gc.collect()
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
# merge and average
df = pd.concat([female_df, male_df])
odd_abs_list.append(df['odds_ratio'].mean()-1)
pickle.dump(odd_abs_list, open(out_dir + "/topk_" + model_type + '_' + str(layer) + ".pickle", "wb" ) )
def top_k(model, model_type, tokenizer, templates, layer_list, neuron_list, k=50, out_dir=''):
odd_abs_list = []
for i in range(k):
print(i)
n_list = list(neuron_list[:i+1])
l_list = list(layer_list[:i+1])
neurons = [n_list]
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=l_list, neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=l_list, df_neuron=neurons[0])
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=l_list, neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=l_list, df_neuron=neurons[0])
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
# merge and average
df = pd.concat([female_df, male_df])
odd_abs_list.append(df['odds_ratio'].mean()-1)
pickle.dump(odd_abs_list, open(out_dir + "/topk_" + model_type + ".pickle", "wb" ))
def greedy_by_layer(model, model_type, tokenizer, templates, layer, k=50, out_dir=''):
neurons = []
odd_abs_list = []
neurons = []
for i in range(k):
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=layer, neurons_to_adj=neurons, intervention_loc='layer',
df_layer=layer, df_neuron=None)
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=layer, neurons_to_adj=neurons, intervention_loc='layer',
df_layer=layer, df_neuron=None)
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
gc.collect()
# merge and average
df = pd.concat([female_df, male_df])
df = df.groupby(['layer', 'neuron'], as_index=False).mean()
df_sorted = sort_odds_obj(df)
neurons.append(df_sorted.head(1)['neuron'].values[0])
odd_abs_list.append(df_sorted['odds_diff'].values[0])
greedy_res = {}
greedy_res['neuron'] = neurons
greedy_res['val'] = odd_abs_list
pickle.dump(greedy_res, open(out_dir + "/greedy_" + model_type + "_" + str(layer) + ".pickle", "wb" ))
def greedy(model, model_type, tokenizer, templates, k=50, out_dir=''):
neurons = []
odd_abs_list = []
layers = []
greedy_filename = out_dir + "/greedy_" + model_type + ".pickle"
if os.path.exists(greedy_filename):
print('loading precomputed greedy values')
res = pickle.load( open(greedy_filename, "rb" ))
odd_abs_list = res['val']
layers = res['layer']
neurons = res['neuron']
k = k - len(odd_abs_list)
else:
neurons = []
odd_abs_list = []
layers = []
for i in range(k):
print(i)
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=layers, neurons_to_adj=neurons, intervention_loc='all',
df_layer=None, df_neuron=None)
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=layers, neurons_to_adj=neurons, intervention_loc='all',
df_layer=None, df_neuron=None)
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
gc.collect()
# merge and average
df = pd.concat([female_df, male_df])
df = df.groupby(['layer', 'neuron'], as_index=False).mean()
df_sorted = sort_odds_obj(df)
neurons.append(df_sorted.head(1)['neuron'].values[0])
layers.append(df_sorted.head(1)['layer'].values[0])
odd_abs_list.append(df_sorted['odds_diff'].values[0])
# memory issue
del df
del female_df
del male_df
gc.collect()
greedy_res = {}
greedy_res['layer'] = layers
greedy_res['neuron'] = neurons
greedy_res['val'] = odd_abs_list
pickle.dump(greedy_res, open(greedy_filename, "wb" ))
def random_greedy_by_layer(layer, k=50, out_dir=''):
neurons = []
odd_abs_list = []
neurons = []
el_list = list(range(1,k+1))
df = []
for i in range(k):
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=layer, neurons_to_adj=neurons, intervention_loc='layer',
df_layer=layer, df_neuron=None)
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=layer, neurons_to_adj=neurons, intervention_loc='layer',
df_layer=layer, df_neuron=None)
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
# merge and average
df = pd.concat([female_df, male_df])
df = df.groupby(['layer', 'neuron'], as_index=False).mean()
df_sorted = sort_odds_obj(df)
j = random.choice(el_list)
neurons.append(df_sorted.head(j)['neuron'].values[-1])
odd_abs_list.append(df_sorted.head(j)['odds_abs'].values[-1])
pickle.dump(odd_abs_list, open("rand_greedy_" + str(layer) + ".pickle", "wb" ))
pickle.dump(neurons, open("rand_greedy_neurons_" + str(layer) + ".pickle", "wb" ))
def test():
layer_obj = []
for layer in range(12):
print(layer)
neurons = [list(range(768))]
# get marginal contrib to empty set
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=768*[layer], neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=768*[layer], neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
# merge and average
df = pd.concat([female_df, male_df])
print(layer)
# print(df_sorted['odds_abs'].values[0])
layer_obj.append(abs(df['odds_ratio'].mean()-1))
neurons = [12*list(range(768))]
# get marginal contrib to empty set
layer_list = []
for l in range(12):
layer_list += (768 * [l])
female_df = get_intervention_results(templates, tokenizer, gender='female',
layers_to_adj=layer_list, neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
male_df = get_intervention_results(templates, tokenizer, gender='male',
layers_to_adj=layer_list, neurons_to_adj=neurons, intervention_loc='neuron',
df_layer=layer, df_neuron=neurons[0])
# compute odds ratio differently for each gender
female_df = compute_odds_ratio(female_df, gender='female')
male_df = compute_odds_ratio(male_df, gender='male')
# merge and average
df = pd.concat([female_df, male_df])
print(layer_obj)
print('all')
print(abs(df['odds_ratio'].mean()-1))
if __name__ == '__main__':
ap = ArgumentParser(description="Neuron subset selection.")
ap.add_argument('--model_type', type=str, choices=['distil-gpt2', 'gpt2', 'gpt2-medium', 'gpt2-large'], default='gpt2')
ap.add_argument('--algo', type=str, choices=['topk', 'greedy', 'random_greedy', 'test'], default='topk')
ap.add_argument('--k', type=int, default=1)
ap.add_argument('--layer', type=int, default=-1)
ap.add_argument('--out_dir', type=str, default='results')
args = ap.parse_args()
algo = args.algo
k = args.k
layer = args.layer
out_dir = args.out_dir
model_type = args.model_type
if not os.path.exists(out_dir):
os.makedirs(out_dir)
tokenizer = GPT2Tokenizer.from_pretrained(model_type)
model = Model(device='cuda')
DEVICE = 'cuda'
templates = get_template_list()
if args.algo == 'topk':
marg_contrib_path = out_dir + "/marg_contrib.pickle"
if os.path.exists(marg_contrib_path):
print('Using cached marginal contribution')
marg_contrib = pickle.load( open(marg_contrib_path, "rb" ))
layer_list = marg_contrib['layer']
neuron_list = marg_contrib['neuron']
else:
print('Computing marginal contribution')
layer_list, neuron_list = get_all_contrib(templates, tokenizer, out_dir)
if layer == -1:
top_k(model, model_type, tokenizer, templates, layer_list, neuron_list, k, out_dir)
elif layer != -1:
top_k_by_layer(model, model_type, tokenizer, templates, layer, layer_list, neuron_list, k, out_dir)
elif (args.algo == 'greedy') and (layer == -1):
greedy(model, model_type, tokenizer, templates, k, out_dir)
elif (args.algo == 'greedy') and (layer != -1):
greedy_by_layer(model, model_type, tokenizer, templates, layer, k, out_dir)
elif (args.algo == 'test'):
test()
else:
random_greedy_by_layer(layer, k, out_dir)