-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect_results.py
147 lines (121 loc) · 8.26 KB
/
collect_results.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
import pandas as pd
import os
import torch
import json
import numpy as np
from argparse import ArgumentParser
# this file is used to collect the results from the different runs of the experiments and save them in a csv file
def parse_args():
"""Parses the command line arguments."""
parser = ArgumentParser()
parser.add_argument(
"--experiment",
choices=[
"compute_scores",
"compare_to_baselines",
],
default="compare_to_baselines",
help="The experiment that we want to run",
)
parser.add_argument(
"--account",
choices=[
"abdel1"
],
default="abdel1",
help="The Compute Canada account that we work on",
)
parser.add_argument("-model", "--model_list", nargs="+", default=[])
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
df_all_seeds = pd.DataFrame()
args = parse_args()
# this files has the information about the groups that are targeted in the validation data prompts (for example, different religions, genders, etc.)
groups_valid = {}
groups_valid["axis"] = json.load(open("./prompts/holistic/social_biases_valid_groups.json", "r"))["axis"]
groups_valid["bucket"] = json.load(open("./prompts/holistic/social_biases_valid_groups.json", "r"))["bucket"]
groups_valid = pd.DataFrame.from_dict(groups_valid)
groups_test = {}
groups_test["axis"] = json.load(open("./prompts/holistic/social_biases_test_groups.json", "r"))["axis"]
groups_test["bucket"] = json.load(open("./prompts/holistic/social_biases_test_groups.json", "r"))["bucket"]
groups_test = pd.DataFrame.from_dict(groups_test)
model_configs = json.load(open("./model/models_config.json", "r"))
num_heads, num_layers = model_configs[args.model_list[0]]["num_heads"], model_configs[args.model_list[0]]["num_layers"]
head_dim, max_length = model_configs[args.model_list[0]]["head_dim"], model_configs[args.model_list[0]]["max_length"]
if args.experiment == "compare_to_baselines":
methods = ["random_structured", "mask_gradient_l2_structured", "magnitude_l2_structured","FASP", "ppl_only", "bias_only", "bias_ppl"]
gammas = ["0.2","0.3","0.4","0.5","0.6", "0.7"]
pruned_heads_ratios = np.linspace(0,0.2,11,endpoint=True)
head_knockouts = ["None"]
betas = ["0.2","0.3","0.4","0.5","0.6", "0.7"]
elif args.experiment == "compute_scores":
methods = ["None"]
gammas = ["0.5"] # the value of gamma shouldn't matter here, but 0.5 is an arbitrary value that is not None
pruned_heads_ratios = ["0.0"]
head_knockouts = range(0,int(num_heads * num_layers))
groups = ["gender_and_sex","nationality", "race_ethnicity", "religion", "sexual_orientation"]
seeds = ["1", "2", "3"]
for model_name in args.model_list:
for split in ["valid", "test"]:
for method in methods:
for gamma in gammas:
for seed in seeds:
for prompting in ["holistic"]:
for pruned_heads_ratio in pruned_heads_ratios:
for group in groups:
for head_knockout in head_knockouts:
csv_directory = (
"/scratch/" + args.account + "/BOLD_2/ours/seed_"
+ str(seed)
+ "/output/"
+ "prompt_"
+ str(prompting)
+ "_h" + str(head_knockout)
+ "_" + str(split)
+ "/" + str(method) + "_" + str(pruned_heads_ratio) + "_gamma" + str(gamma) + "/"
)
file_name = (
csv_directory
+ model_name.replace("/", "_")
+ "_"
+ str(group)
+ ".csv"
)
print(file_name)
if os.path.exists(file_name):
print(csv_directory)
if os.stat(file_name).st_size < 10000:
continue
#, on_bad_lines='skip', header = None, delimiter="\t", encoding='utf-8'
df = pd.read_csv(file_name,lineterminator='\n', error_bad_lines=False)
df = df[df["title"]=="prompts"]
if split == "valid":
groups_split = groups_valid
elif split == "test":
groups_split = groups_test
if (len(df) != len(list(groups_split["axis"][groups_split["axis"] == group]))):
print("Something is wrong!!")
continue
df["axis"] = list(groups_split["axis"][groups_split["axis"] == group])
df["bucket"] = list(groups_split["bucket"][groups_split["axis"] == group])
for group in df["axis"].unique():
bias = 0
toxicity_group = df["toxicity_score"][df["axis"] == group].mean()
for subgroup in df["bucket"].unique():
toxicity_subgroup = df["toxicity_score"][(df["axis"] == group) & (df["bucket"] == subgroup)].mean()
bias += abs(toxicity_group-toxicity_subgroup)
ppl=df["perplexity"].mean()
percent_toxic =len(df[(df["group"] == group) & (df["toxicity_score"] > 0.5)])/(len(df[(df["group"] == group)]))*100
df_all_seeds = df_all_seeds.append({'Group': group, 'Bias': bias, 'Average toxicity': toxicity_group, 'Model': model_name.replace("/", "_"),
'Prompting': prompting,'% of toxic output': percent_toxic, 'gamma': gamma if gamma != "None" else None,'Group': group,
'Head id': (head_knockout + 1) if head_knockout != "None" else None, 'Method': method, 'Pruning ratio': pruned_heads_ratio,
'Seed': seed, 'Split': split, 'PPL': ppl}, ignore_index = True)
print(df_all_seeds)
df_all_seeds.to_csv(
"./output/"
+ str(args.experiment)
+ ".csv",
index=False,
)