forked from epfl-dlab/understanding-decoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_figure5.py
361 lines (301 loc) · 12.5 KB
/
visualize_figure5.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
import argparse
import io
import os
from datetime import datetime
from itertools import cycle
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import pandas as pd
import seaborn as sns
import torch
import wandb
from PIL import Image
sns.set_theme(style="whitegrid", font_scale=1.4)
sns.set_style("ticks")
sns.set_palette("flare")
def log_plot_to_wandb(wandb_logger, log_key, backend=plt, dpi=200):
with io.BytesIO() as f:
backend.savefig(f, dpi=dpi, format="png")
im = Image.open(f)
wandb_logger.log({log_key: wandb.Image(im)})
def load_list_from_file(path, post_processing_fn=float):
"""File contains floats separated by a new line."""
values = []
with open(path, "r") as f:
for line in f.readlines():
line = line.strip()
if len(line) == 0:
continue
value = post_processing_fn(line)
values.append(value)
return values
def is_it_a_yes(string):
if string.strip().lower() == "yes":
return 1.0
if string.strip().lower() == "no":
return 0.0
raise ValueError("Could not determine whether or not it was a yes.")
def parsing_sports(targets_path, outputs_path, ll_path, all_probs_path, predictions_path):
outputs = []
probs_counter = 0
with open(outputs_path, "r") as f:
for line in f.readlines():
line = line.strip()
if len(line) == 0:
continue
results_dict = eval(line)
assert isinstance(results_dict, dict)
assert len(results_dict["all_probs"]) > probs_counter
probs = results_dict["all_probs"][probs_counter:]
probs_counter += len(probs)
results_dict["all_probs"] = probs
outputs.append(results_dict)
targets = load_list_from_file(targets_path, post_processing_fn=is_it_a_yes)
assert len(outputs) == len(targets)
for i in range(len(outputs)):
outputs[i]["id"] = i
outputs[i]["target"] = targets[i]
print("Outputs (and targets) files preprocessed:")
print(f" outputs[0]={outputs[0]}")
print(f" outputs[-1]={outputs[-1]}")
print(f" len(outputs)={len(outputs)}")
print()
with open(predictions_path, "w") as f:
for o in outputs:
completion = o["completion"].lower().strip()
pred = None
if completion.endswith("the answer is yes."):
pred = 1.0
elif completion.endswith("the answer is no."):
pred = 0.0
elif len(completion) == 0:
pred = -1.0
if pred == None:
pred = completion
print(o["prompt"])
print(f'id+1={o["id"] + 1}')
print(o["completion"])
print(pred)
print(o["target"])
print(f"\n\n\n\n\n")
f.write(str(pred))
f.write("\n")
with open(all_probs_path, "w") as f:
for o in outputs:
f.write(str(o["all_probs"]))
f.write("\n")
with open(ll_path, "w") as f:
for o in outputs:
normalized_ll = -torch.tensor(o["all_probs"]).log().mean().item()
f.write(str(normalized_ll))
f.write("\n")
def parsing_zeroshot_sports(targets_path, model_outputs_path, ll_path, all_probs_path, predictions_path, stream=False):
import jsonlines
with jsonlines.open(model_outputs_path, "r") as reader:
outputs = [e for e in reader]
targets = load_list_from_file(targets_path, post_processing_fn=is_it_a_yes)
assert len(outputs) > 0
assert len(outputs) == len(targets)
for i in range(len(outputs)):
outputs[i]["id"] = i
outputs[i]["target"] = targets[i]
outputs[i]["all_logprobs"] = outputs[i]["logprobs"]["token_logprobs"]
outputs[i]["all_probs"] = np.exp(np.array(outputs[i]["all_logprobs"])).reshape(-1, 1, 1).tolist()
print("Outputs (and targets) files preprocessed:")
print(f" outputs[0]={outputs[0]}")
print(f" outputs[-1]={outputs[-1]}")
print(f" len(outputs)={len(outputs)}")
print()
with open(predictions_path, "w") as f:
for o in outputs:
completion = o["text"].lower().strip()
pred = None
if completion.startswith("yes"):
pred = 1
elif completion.startswith("no"):
pred = 0
elif len(completion) == 0:
pred = -1
# Automatic labeling will not work, we annotate the outputs by hand
if pred == None:
pred = f"`{completion}`".encode('unicode_escape')
else:
pred = f"{pred} " + str(f"`{completion}`".encode('unicode_escape'))
print(f'id+1={o["id"] + 1}')
print(o["text"])
print(pred)
print(o["target"])
print(f"\n\n\n\n\n")
f.write(str(pred))
f.write("\n")
with open(all_probs_path, "w") as f:
for o in outputs:
f.write(str(o["all_probs"]))
f.write("\n")
with open(ll_path, "w") as f:
for o in outputs:
normalized_ll = -torch.tensor(o["all_logprobs"]).mean().item()
f.write(str(normalized_ll))
f.write("\n")
def load_sports_prompt_dataset_dicts(targets_path, ll_path, predictions_path):
dicts = []
targets = load_list_from_file(targets_path, post_processing_fn=is_it_a_yes)
lls = load_list_from_file(ll_path)
predictions = load_list_from_file(predictions_path)
assert len(targets) == len(lls) == len(predictions)
for id, (t, ll, pred) in enumerate(zip(targets, lls, predictions)):
d = {
"id": id,
"prediction": pred,
"target": t,
"score": -ll,
}
dicts.append(d)
matches = 0
for d in dicts:
d["match"] = d["target"] == d["prediction"]
matches += d["match"]
if d["match"]:
print(d)
d["utility"] = float(d["match"])
print(f"Acc: {matches / len(dicts)}")
print()
print()
return dicts
def load_sports_prompt_dataset(parse=False):
TARGETS_PATH = "data/prompts_sports/sports_understanding_8shot_stream_s0_targets"
# RAW
OUTPUTS_PATH_1 = "data/prompts_sports/sports_understanding_8shot_direct_s0_inputs.preprocessed.out"
# PROCESSED
ALL_PROBS_PATH_1 = "data/prompts_sports/sports_understanding_8shot_direct_s0_all_probs"
PREDICTIONS_PATH_1 = "data/prompts_sports/sports_understanding_8shot_direct_s0_predictions"
LL_PATH_1 = "data/prompts_sports/sports_understanding_8shot_direct_s0_ll"
# RAW
OUTPUTS_PATH_2 = "data/prompts_sports/sports_understanding_8shot_stream_s0_inputs.preprocessed.out"
# PROCESSED
ALL_PROBS_PATH_2 = "data/prompts_sports/sports_understanding_8shot_stream_s0_all_probs"
PREDICTIONS_PATH_2 = "data/prompts_sports/sports_understanding_8shot_stream_s0_predictions"
LL_PATH_2 = "data/prompts_sports/sports_understanding_8shot_stream_s0_ll"
# RAW
OUTPUTS_PATH_3 = "data/prompts_sports/tnlgv2_zero-shot_sports.jsonl"
# PROCESSED
ALL_PROBS_PATH_3 = "data/prompts_sports/tnlgv2_zero-shot_sports_all_probs"
PREDICTIONS_PATH_3 = "data/prompts_sports/tnlgv2_zero-shot_sports_predictions"
LL_PATH_3 = "data/prompts_sports/tnlgv2_zero-shot_sports_ll"
if parse:
parsing_sports(TARGETS_PATH, OUTPUTS_PATH_1, LL_PATH_1, ALL_PROBS_PATH_1, PREDICTIONS_PATH_1)
parsing_sports(TARGETS_PATH, OUTPUTS_PATH_2, LL_PATH_2, ALL_PROBS_PATH_2, PREDICTIONS_PATH_2)
parsing_zeroshot_sports(TARGETS_PATH, OUTPUTS_PATH_3, LL_PATH_3, ALL_PROBS_PATH_3, PREDICTIONS_PATH_3)
print(f"The sports dataset results have been parsed. The predictions need to be curated by hand:")
print(f" - {PREDICTIONS_PATH_1}")
print(f" - {PREDICTIONS_PATH_2}")
print(f" - {PREDICTIONS_PATH_3}")
return
std_dicts = load_sports_prompt_dataset_dicts(TARGETS_PATH, LL_PATH_1, PREDICTIONS_PATH_1)
stream_dicts = load_sports_prompt_dataset_dicts(TARGETS_PATH, LL_PATH_2, PREDICTIONS_PATH_2)
zeroshot_dicts = load_sports_prompt_dataset_dicts(TARGETS_PATH, LL_PATH_3, PREDICTIONS_PATH_3)
std_df = pd.DataFrame(std_dicts)
stream_df = pd.DataFrame(stream_dicts)
zeroshot_df = pd.DataFrame(zeroshot_dicts)
zeroshot_df = zeroshot_df.dropna()
return std_df, stream_df, zeroshot_df
def _plot_join_q_plot(
df_label_tuples,
num_bins,
folder,
name,
ax_lim=(0, 1),
y_label="Utility (Solve Rate)",
x_label="Percentile",
):
c = cycle(["r", "GoldenRod", "forestgreen", "yellow"])
fig = plt.figure(figsize=(5.2, 5))
ax = fig.gca()
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
for df, label in df_label_tuples:
df['bin_id'] = pd.qcut(df.score, num_bins, precision=3, labels=False)
df['bin_labels'] = pd.qcut(df.score, num_bins, precision=3)
sns.lineplot(data=df,
x='bin_id',
y='utility',
color=next(c),
linestyle="-",
linewidth=2,
err_style="bars", markers=True, dashes=False, ci=95,
err_kws={"fmt": 'o', "linewidth": 2, "capsize": 6},
ax=ax, alpha=1, label=label)
print(f"{label} range per bin")
print(df[['bin_id', 'bin_labels']].value_counts(ascending=True).reset_index(name='count'))
positions = np.arange(num_bins)
labels = ((positions + 1) * 100 / num_bins).astype(int)
ax.xaxis.set_major_locator(mticker.FixedLocator(positions))
ax.xaxis.set_major_formatter(mticker.FixedFormatter(labels))
ax.set_ylabel(y_label)
ax.set_xlabel(x_label)
ax.set_ylim(ax_lim)
plt.legend(frameon=False)
plt.tight_layout(rect=(-0.03, -0.03, 1.05, 1.03))
log_plot_to_wandb(wandb_logger, f"Prompting/{name}", backend=plt, dpi=args.dpi)
if not os.path.isdir(folder):
os.makedirs(folder)
path = os.path.join(folder, f"{name}")
plt.savefig(f"{path}.pdf", bbox_inches="tight")
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Visualise a list of evaluation experiments.")
parser.add_argument("--dpi", type=float, default=200, help="The DPI of the plots logged to wandb.")
parser.add_argument("--parse_dataset", action="store_true",
help="Whether parse to parse the raw dataset outputs into the intermediary format "
"we use (i.e., extract `all_probs`, `ll` and `predictions` from the raw `.out` file. "
"Note that `predictions` need to be curated by hand before the results can be "
"computed/plotted.")
parser.add_argument(
"--wandb_entity_for_report",
type=str,
default="user72",
help="The wandb username or team name where final logs will be reported.",
)
parser.add_argument(
"--wandb_project_for_report",
type=str,
default="understanding-decoding",
help="The name of the wandb project where final logs will be reported.",
)
parser.add_argument(
"--wandb_id_for_report",
type=str,
default=f"report_{datetime.now().strftime('%Y.%m.%d_%H.%M.%S')}",
help="ID of the wandb run to be used (or created) for reporting final logs.",
)
args = parser.parse_args()
if os.getcwd().endswith("scripts"):
work_dir = ".."
else:
work_dir = "."
# 1. GET THE DATA
if args.parse_dataset:
load_sports_prompt_dataset(parse=True)
print(f"The raw dataset data has been parsed and needs to be post-processed by hand "
f"to curate the predictions. Once this is done, re-run the script with the `parse_dataset` flag "
f"removed. Exiting now.")
exit()
else:
fs_df, cot_df, zeroshot_df = load_sports_prompt_dataset()
df_label_tuples = [
(zeroshot_df, "Zero-Shot"),
(fs_df, "Few-Shot"),
(cot_df, "Chain-of-Thought"),
]
# 2. GET THA LOGGER
wandb_logger = wandb.init(
entity=args.wandb_entity_for_report,
project=args.wandb_project_for_report,
id=args.wandb_id_for_report,
resume="allow"
)
# 3. THE PLOT
_plot_join_q_plot(df_label_tuples, 5, folder=f"{work_dir}/images/fig5", name="Prompting")
wandb_logger.finish()
print(wandb_logger.get_url())