forked from google-research/t5x
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplot_eai_eval.py
More file actions
311 lines (274 loc) · 11.6 KB
/
plot_eai_eval.py
File metadata and controls
311 lines (274 loc) · 11.6 KB
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
import json
import re
import subprocess
from argparse import ArgumentParser
from collections import namedtuple
from pathlib import Path
import numpy as np
from matplotlib import pyplot as plt
def get_args():
parser = ArgumentParser()
parser.add_argument('--all', action="store_true", help="Plot all results in a single plot")
parser.add_argument('--per-arch', action="store_true", help="Plot results grouped by architectures")
parser.add_argument('--per-objective', action="store_true", help="Plots results grouped by objectives")
parser.add_argument('--per-t0-adapted', action="store_true", help="Plots only T0 adapted models")
parser.add_argument('--normalised', action="store_true", help="Whether to plot normalised scores or not. Each task has a random baseline and we compute how far away we're from that baseline")
parser.add_argument('--only-t0-eval', action="store_true", help="Only plot the results from the t0 evaluation set")
args = parser.parse_args()
assert args.all or args.per_arch or args.per_objective or args.per_t0_adapted
return args
def load_data(dir_path: Path):
def remove_eai_eval(filename):
name, empty = filename.rsplit(".json", maxsplit=1)
assert empty == ""
name = name.replace("_eai_eval", "")
name = name.replace("_bs2048", "")
name = name.replace("_c4", "")
return name
all_results = {}
for child in dir_path.iterdir():
child_name = remove_eai_eval(child.name)
with open(child, "r") as fi:
results = json.load(fi)["results"]
all_results[child_name] = results
print(all_results.keys())
return all_results
def get_experiment_name(key: str):
name = re.sub(r"_([0-9]*)$", r" [\1]", key)
name = name.replace("span_corruption", "SC")
name = re.sub(r"^enc_dec", "ED", name)
name = re.sub(r"^nc_dec", "NCD", name)
name = re.sub(r"^c_dec", 'CD', name)
name = name.replace("full_lm", "FLM")
name = name.replace("prefix_lm", "PLM")
name = re.sub(r"t0_adapt_([0-9]+)", r"T0(\1)", name)
if name[:3] == "CD_":
name = re.sub(r"lm_adapt_([0-9]+)", r"FLM(\1)", name)
name = re.sub(r"t0_adapt_nc_([0-9]+)", r"T0 AS NC (\1)", name)
name = re.sub(r"nc_sc_([0-9]+)", r"SC as NC(\1)", name)
name = re.sub(r"nc_t0_([0-9]+)", r"T0 as NC(\1)", name)
elif name[:4] == "NCD_" or name[:3] == "ED_":
if "flm_adapt" in name:
name = re.sub(r"flm_adapt_([0-9]+)", r"FLM AS CD(\1)", name)
else:
name = re.sub(r"lm_adapt_([0-9]+)", r"PLM(\1)", name)
else:
raise NotImplementedError
name = name.replace("_", " + ")
return name
RANDOM_BASELINE={
"anli_r1_acc": 1/3,
"anli_r2_acc": 1 / 3,
"anli_r3_acc": 1 / 3,
"arc_challenge_acc": 0.2502, # Source: https://arxiv.org/pdf/1803.05457.pdf table 6
"arc_easy_acc": 0.2502, # Source: https://arxiv.org/pdf/1803.05457.pdf table 6
"boolq_acc": 0.5,
"cb_acc": 0.5,
"copa_acc": 0.5,
"headqa_acc": 0.25,
"headqa_en_acc": 0.25,
"hellaswag_acc": 0.25,
"lambada_acc": 0., # Safe to say that random models won't perform well at all.
"logiqa_acc": 0.25,
"mathqa_acc": (4360 * 1/ 5 - (4475 - 4360) * 1/ 4) / 4475,
"mrpc_acc": 0.5,
"multirc_acc": 0., # TODO: I couldn't figure it out
"openbookqa_acc": 0.25,
"piqa_acc": 0.5,
"prost_acc": 0.25,
"pubmedqa_acc": 1/3,
"qnli_acc": 0.5,
"qqp_acc": 0.5,
"race_acc": 0.25, # Source: https://arxiv.org/pdf/1704.04683.pdf table 5
"rte_acc": 0.5,
"sciq_acc": 0.25,
"sst_acc": 0.5,
"triviaqa_acc": 0.,
"webqs_acc": 0.,
"wic_acc": 0.5,
"winogrande_acc": 0.5,
"wnli_acc": 0.5,
"wsc_acc": 0.5
}
def normalise_score(score, evaluation_name, metric_name):
key = f"{evaluation_name}_{metric_name}"
if key not in RANDOM_BASELINE:
raise ValueError(f"{key} doesn't have a random baseline set yet.")
return (score - RANDOM_BASELINE[key]) / (1 - RANDOM_BASELINE[key])
def plot_tasks(data, evaluation_metrics):
data, sorted_experiment_keys = data
fig, axs = plt.subplots(3, 11)
agg_fig, agg_axs = plt.subplots(1,3)
axs = axs.flatten()
agg_axs = agg_axs.flatten()
assert len(axs) >= len(evaluation_metrics)
for (evaluation_name, metric_name, _), ax in zip(evaluation_metrics, axs):
key = f"{evaluation_name}_{metric_name}"
ax.set_title(key)
ax.axhline(
RANDOM_BASELINE[key],
0, len(data),
label="Random"
)
agg_axs[0].set_title("Average")
agg_axs[0].axhline(
np.mean(list(RANDOM_BASELINE.values())),
0, len(data),
label="Random"
)
agg_axs[1].set_title("Normalised average")
agg_axs[1].axhline(
0,
0, len(data),
label="Random"
)
for i, experiment_key in enumerate(sorted_experiment_keys):
experiment_name = get_experiment_name(experiment_key)
experiment = data[experiment_key]
scores = [experiment[evaluation_name][metric_name] for (evaluation_name, metric_name, _) in evaluation_metrics]
normalised_score = [
normalise_score(experiment[evaluation_name][metric_name], evaluation_name, metric_name)
for (evaluation_name, metric_name, _) in evaluation_metrics
]
for j, score in enumerate(scores):
axs[j].scatter(i, score, s=50, alpha=0.4, label=experiment_name)
agg_axs[0].scatter(i, np.mean(scores), s=50, alpha=0.4, label=experiment_name)
agg_axs[1].scatter(i, np.mean(normalised_score), s=50, alpha=0.4, label=experiment_name)
last_ax_id = len(evaluation_metrics) -1
axs[last_ax_id].legend(bbox_to_anchor=(1, 1), loc="upper left")
for ax in axs[last_ax_id + 1:]:
ax.set_visible(False)
agg_axs[1].legend(bbox_to_anchor=(1, 1), loc="upper left")
for ax in agg_axs[2:]:
ax.set_visible(False)
def plot_bar(data, evaluation_metrics, normalised):
ind = np.arange(len(evaluation_metrics))
width = 1 / (len(data.keys()) + 1) # the width of the bars
fig, ax = plt.subplots()
for i, (experiment_name, experiment) in enumerate(data.items()):
if normalised:
normalised_scores = [
normalise_score(experiment[evaluation_name][metric_name], evaluation_name, metric_name)
for (evaluation_name, metric_name, _) in evaluation_metrics
]
ax.bar(ind + i * width, normalised_scores, width, label=experiment_name)
else:
scores = [experiment[evaluation_name][metric_name] for (evaluation_name, metric_name, _) in evaluation_metrics]
ax.bar(ind + i * width, scores, width, label=experiment_name)
# add some text for labels, title and axes ticks
if normalised:
ax.set_ylabel('Normalised scores')
else:
ax.set_ylabel('Scores')
ax.set_title('EAI harness')
ax.set_xticks(ind + len(data.keys()) / 2 * width)
ax.set_xticklabels(
(f"{evaluation_name}_{metric_name}" for evaluation_name, metric_name, _ in evaluation_metrics),
rotation=80,
ha="right"
)
ax.legend()
# All evaluation tasks available: (dataset_name, metric_name, is_t0_eval)
Evaluation = namedtuple('Evaluation', ['task', 'metric', "is_t0_eval"])
ALL_EVALUATION = [
Evaluation("anli_r1", "acc", True),
Evaluation("anli_r2", "acc", True),
Evaluation("anli_r3", "acc", True),
Evaluation("arc_challenge", "acc", False),
Evaluation("arc_easy", "acc", False),
Evaluation("boolq", "acc", False),
Evaluation("cb", "acc", True), # https://github.com/EleutherAI/lm-evaluation-harness/pull/254
Evaluation("copa", "acc", True),
Evaluation("headqa_en", "acc", False),
Evaluation("hellaswag", "acc", True),
Evaluation("lambada", "acc", False),
Evaluation("logiqa", "acc", False),
Evaluation("mathqa", "acc", False),
Evaluation("mrpc", "acc", False),
Evaluation("multirc", "acc", False),
Evaluation("openbookqa", "acc", False),
Evaluation("piqa", "acc", False),
Evaluation("prost", "acc", False),
Evaluation("pubmedqa", "acc", False),
Evaluation("qnli", "acc", False),
Evaluation("qqp", "acc", False),
Evaluation("race", "acc", False),
Evaluation("rte", "acc", True),
Evaluation("sciq", "acc", False),
Evaluation("sst", "acc", False),
Evaluation("triviaqa", "acc", False),
Evaluation("webqs", "acc", False),
Evaluation("wic", "acc", True),
Evaluation("winogrande", "acc", True),
Evaluation("wnli", "acc", False),
Evaluation("wsc", "acc", True),
]
def main():
args = get_args()
# Define directories
results_dir = Path(__file__).resolve().parent.parent / "results" / "eai_eval"
subprocess.run(["mkdir", "-p", results_dir])
# Update data locally
# gsutil rsync -rd gs://bigscience-t5x/arch_objective_exps_v2/eai_eval ../results/eai_eval
subprocess.run(["gsutil", "-m", "rsync", "-rd", "gs://bigscience-t5x/arch_objective_exps_v2/eai_eval", results_dir])
# Load data
data = load_data(results_dir)
# Get evaluation_metric
if args.only_t0_eval:
evaluation_metrics = [elt for elt in ALL_EVALUATION if elt.is_t0_eval]
else:
evaluation_metrics = ALL_EVALUATION
# Plot data
# plot_bar(data, evaluation_metrics, args.normalised)
# sort experiments
LM_ADAPT_FROM = [28000, 30000, 58768]
PRETRAIN_AND_T0_ADAPT_STEPS = [(32768, 37768), (65536, 70536), (131072, 141072), (169984, 179984), (196608, 206608)]
def key_architecture(experiment_name):
if experiment_name[0] == 'c':
return 0
elif experiment_name[0] == 'n':
return 1
elif experiment_name[0] == 'e':
return 2
else:
raise NotImplementedError
def key_objective(experiment_name):
suffixes = []
for max_steps, _ in PRETRAIN_AND_T0_ADAPT_STEPS:
suffixes += [
f"lm_{max_steps}",
f"span_corruption_{max_steps}",
*[f"{lm_type}_adapt_{lm_adapt}_{max_steps}" for lm_adapt in LM_ADAPT_FROM for
lm_type in ["_lm", "_flm", "_plm"]]
]
for t0_adapt_from, max_steps in PRETRAIN_AND_T0_ADAPT_STEPS:
suffixes += [
f"lm_t0_adapt_{t0_adapt_from}_{max_steps}",
f"lm_t0_adapt_nc_{t0_adapt_from}_{max_steps}",
f"span_corruption_t0_adapt_{t0_adapt_from}_{max_steps}",
*[f"{lm_type}_adapt_{lm_adapt}_t0_adapt_{t0_adapt_from}_{max_steps}" for lm_adapt in LM_ADAPT_FROM for lm_type in ["_lm", "_flm", "_plm"]],
f"-nc_sc_{t0_adapt_from}-nc_t0_{max_steps}"
]
for i, suffix in enumerate(suffixes):
if experiment_name.endswith(suffix):
return i
raise NotImplementedError(f"{experiment_name}")
sorted_experiment_keys = sorted(data.keys(), key=lambda x: (key_objective(x), key_architecture(x)))
if args.all:
plot_tasks((data, sorted_experiment_keys), evaluation_metrics)
def plot_per_group(group_fn):
t5x_objective_keys = set(group_fn(x) for x in sorted_experiment_keys)
for group_id in t5x_objective_keys:
t5x_experiments_per_group = [x for x in sorted_experiment_keys if group_id == group_fn(x)]
plot_tasks((data, t5x_experiments_per_group), evaluation_metrics)
if args.per_objective:
plot_per_group(key_objective)
if args.per_arch:
plot_per_group(key_architecture)
if args.per_t0_adapted:
def key_is_t0_adapted(experiment_name):
return "_t0" in experiment_name
plot_per_group(key_is_t0_adapted)
plt.show()
if __name__ == "__main__":
main()