-
Notifications
You must be signed in to change notification settings - Fork 246
/
run_eval.py
504 lines (442 loc) · 20.4 KB
/
run_eval.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import argparse
import os
import json
import torch
import pandas as pd
import warnings
from eval.utils import (
load_hf_lm,
load_hf_tokenizer,
load_hf_lm_and_tokenizer,
query_openai_chat_model,
query_openai_model,
generate_completions,
score_completions,
dynamic_import_function,
)
from eval.truthfulqa.utilities import (
format_prompt,
split_multi_answer,
format_best,
set_columns,
)
from eval.truthfulqa.metrics import run_gpt_classifier_eval, run_hf_classifier_eval, MC_calcs
from eval.truthfulqa.configs import BEST_COL, ANSWER_COL, INCORRECT_COL
def trim_answer(answer):
# remove spaces at the beginning and end
answer = answer.strip()
# remove the "A:" prefix if it exists
if answer.startswith('A:'):
answer = answer[2:].strip()
# remove everything after "Q:" if it exists
if 'Q:' in answer:
answer = answer.split('Q:')[0].strip()
# reformat line-breaks for long-form answers
answer = answer.replace('\n\n', ' ')
return answer
def run_chatgpt(questions, engine, tag, preset='qa', batch_size=1, cache_path=None, verbose=False):
"""Stores answers from ChatGPT / GPT4 models (requires an API key)"""
if tag not in questions.columns:
questions[tag] = ''
questions[tag].fillna('', inplace=True)
questions[tag] = questions[tag].astype(str)
instances = [
{"prompt": format_prompt(questions.loc[idx], preset, format='general'), "id": idx} for idx in questions.index
]
responses = query_openai_chat_model(
engine=engine,
output_path=cache_path,
instances=instances,
batch_size=batch_size,
temperature=0.0
)
assert len(responses) == len(instances)
return questions
def run_gpt3(questions, engine, tag, preset='qa', batch_size=1, cache_path=None, verbose=False):
"""Stores answers from GPT-3 models (requires an API key)"""
if tag not in questions.columns:
questions[tag] = ''
questions[tag].fillna('', inplace=True)
questions[tag] = questions[tag].astype(str)
instances = [
{"prompt": format_prompt(questions.loc[idx], preset, format='general'), "id": idx} for idx in questions.index
]
responses = query_openai_model(
engine=engine,
instances=instances,
output_path=cache_path,
batch_size=batch_size,
temperature=0.0,
stop=None if preset == 'long' else '\n\n',
max_tokens=50
)
assert len(responses) == len(instances)
for idx, response in zip(questions.index, responses):
questions.loc[idx, tag] = trim_answer(response["output"])
return questions
def run_gpt3_mc(questions, engine, tag, preset='qa', batch_size=1, cache_path=None, verbose=False):
"""Runs multiple-choice metrics for GPT-3 models (requires an API key)"""
set_columns(tag, questions)
examples = []
for idx in questions.index:
# check that candidate answer exists
if pd.isnull(questions.loc[idx, INCORRECT_COL]):
warnings.warn("References missing for {0}!".format(idx), stacklevel=2)
continue
if not len(questions.loc[idx, INCORRECT_COL]):
warnings.warn("References missing for {0}!".format(idx), stacklevel=2)
continue
# reference answers
ref_true = split_multi_answer(questions.loc[idx, ANSWER_COL])
ref_false = split_multi_answer(questions.loc[idx, INCORRECT_COL])
# prompt for all answers
prompt = format_prompt(questions.loc[idx], preset, format='general') + "\nA:"
# candidate completions
examples.append({"prompt": prompt, "completions": ref_true + ref_false})
instances, instance_id = [], 0
for example in examples:
for completion in example["completions"]:
instances.append({"prompt": example["prompt"] + " " + completion, "id": instance_id})
instance_id += 1
responses = query_openai_model(
engine=engine,
instances=instances,
output_path=cache_path,
batch_size=batch_size,
temperature=0.0,
stop=["\n\n"],
max_tokens=0,
echo=True,
logprobs=1
)
assert len(responses) == len(instances)
responses = {response["id"]: response for response in responses}
all_scores, instance_id = {}, 0
for example in examples:
all_scores[example["prompt"]] = {}
for completion in example["completions"]:
response = responses[instance_id]
logprobs = response["response_metadata"]['choices'][0]['logprobs']
# iterate through response to find the indexes of the start / end tokens for the ref answer
idx_start = 0
while idx_start < len(logprobs['text_offset']) - 1:
if (logprobs['text_offset'][idx_start] >= len(example["prompt"])):
break
idx_start += 1
idx_end = idx_start
while idx_end < len(logprobs['text_offset']) - 1:
if (logprobs['text_offset'][idx_end] >= len(example["prompt"] + " " + completion)):
break
idx_end += 1
logprob_vals = logprobs['token_logprobs'][idx_start: idx_end]
text_vals = logprobs['tokens'][idx_start + 3:idx_end]
if True:
print("LOGPROBS AND ANSWER TOKENS")
print(logprob_vals)
print(text_vals)
all_scores[example["prompt"]][completion] = sum(logprob_vals)
instance_id += 1
for idx, example in zip(questions.index, examples):
ref_best = format_best(questions.loc[idx, BEST_COL])
ref_true = split_multi_answer(questions.loc[idx, ANSWER_COL])
ref_false = split_multi_answer(questions.loc[idx, INCORRECT_COL])
completion_scores = all_scores[example["prompt"]]
scores_true = [completion_scores[ref] for ref in ref_true]
scores_false = [completion_scores[ref] for ref in ref_false]
MC_calcs(tag, questions, idx, scores_true, scores_false, ref_true, ref_best)
return questions
def run_hf_model(questions, model, tokenizer, tag, preset="qa", batch_size=1, max_new_tokens=50, chat_formatting_function=None):
"""Stores answers from autoregressive HF models (GPT-2, GPT-Neo)"""
if tag not in questions.columns:
questions[tag] = ''
questions[tag].fillna('', inplace=True)
questions[tag] = questions[tag].astype(str)
prompts = [
format_prompt(questions.loc[idx], preset, format='general') for idx in questions.index
]
if chat_formatting_function is not None:
for idx, prompt in enumerate(prompts):
messages = [{"role": "user", "content": prompt}]
prompts[idx] = chat_formatting_function(messages, tokenizer, add_bos=False)
prompt += "A:" if prompt[-1] in ["\n", " "] else " A:"
# get the last token because the tokenizer may add space tokens at the start.
stop_sequence = tokenizer.encode("\n\n", add_special_tokens=False)[-2:]
completions = generate_completions(
model, tokenizer, prompts, batch_size=batch_size, max_new_tokens=max_new_tokens,
stop_id_sequences=[stop_sequence] if chat_formatting_function is None else None,
do_sample=False,
)
assert len(completions) == len(prompts)
# if it's not a chat format, we will do some post-processing for the answer to make sure it's valid
# otherwise, we will just store the completions as is
for idx, completion in zip(questions.index, completions):
questions.loc[idx, tag] = trim_answer(completion) if not chat_formatting_function else completion
return questions
def run_hf_model_mc(questions, model, tokenizer, tag, batch_size=1, preset='qa', chat_formatting_function=None):
"""Runs multiple-choice metrics for autoregressive HuggingFace models (GPT-2, GPT-Neo)"""
set_columns(tag, questions)
examples = []
for idx in questions.index:
# check that candidate answer exists
if pd.isnull(questions.loc[idx, INCORRECT_COL]):
warnings.warn("References missing for {0}!".format(idx), stacklevel=2)
continue
if not len(questions.loc[idx, INCORRECT_COL]):
warnings.warn("References missing for {0}!".format(idx), stacklevel=2)
continue
# reference answers
ref_true = split_multi_answer(questions.loc[idx, ANSWER_COL])
ref_false = split_multi_answer(questions.loc[idx, INCORRECT_COL])
# prompt for all answers
prompt = format_prompt(questions.loc[idx], preset, format='general')
if chat_formatting_function is not None:
messages = [{"role": "user", "content": prompt}]
prompt = chat_formatting_function(messages, tokenizer, add_bos=False)
prompt += "A:" if prompt[-1] in ["\n", " "] else " A:"
else:
prompt += "\nA:"
# candidate completions
examples.append({"prompt": prompt, "completions": ref_true + ref_false})
all_scores = score_completions(model, tokenizer, examples, batch_size=batch_size, aggregation="sum")
assert len(all_scores) == len(examples)
for idx, example in zip(questions.index, examples):
ref_best = format_best(questions.loc[idx, BEST_COL])
ref_true = split_multi_answer(questions.loc[idx, ANSWER_COL])
ref_false = split_multi_answer(questions.loc[idx, INCORRECT_COL])
completion_scores = all_scores[example["prompt"]]
scores_true = [completion_scores[ref] for ref in ref_true]
scores_false = [completion_scores[ref] for ref in ref_false]
MC_calcs(tag, questions, idx, scores_true, scores_false, ref_true, ref_best)
return questions
def format_frame(results):
results = results[[x for x in results.columns if (x != 'Context') and (results[x].dtype != 'O')]]
new_cols = []
for col in results.columns:
split = col.split(' ')
new_cols.append((split[0], ' '.join(split[1:])))
results.columns = pd.MultiIndex.from_tuples(new_cols)
return results
def main(args):
os.makedirs(args.save_dir, exist_ok=True)
questions = pd.read_csv(os.path.join(args.data_dir, "TruthfulQA.csv"))
if args.num_instances is not None:
questions = questions.sample(args.num_instances, random_state=42)
if args.model_name_or_path:
print("Loading model and tokenizer...")
tokenizer = load_hf_tokenizer(
model_name_or_path=args.model_name_or_path,
tokenizer_name_or_path=args.tokenizer_name_or_path,
use_fast_tokenizer=not args.use_slow_tokenizer,
)
model = load_hf_lm(
model_name_or_path=args.model_name_or_path,
load_in_8bit=args.load_in_8bit,
device_map="balanced_low_0" if torch.cuda.device_count() > 1 else "auto",
gptq_model=args.gptq,
)
from transformers import GPTNeoXForCausalLM, OPTForCausalLM
if isinstance(model, GPTNeoXForCausalLM) or isinstance(model, OPTForCausalLM):
tokenizer.model_max_length = model.config.max_position_embeddings
print("Set tokenizer.model_max_length to model.config.max_position_embeddings: {}".format(model.config.max_position_embeddings))
if "truth" in args.metrics or "info" in args.metrics:
print("Running generations!")
run_hf_model(
questions,
model,
tokenizer,
tag=args.model_name_or_path,
preset=args.preset,
batch_size=args.eval_batch_size,
chat_formatting_function=dynamic_import_function(args.chat_formatting_function) if args.use_chat_format else None
)
if "mc" in args.metrics:
print("Running multiple-choice classification!")
run_hf_model_mc(
questions,
model,
tokenizer,
tag=args.model_name_or_path,
batch_size=args.eval_batch_size,
preset=args.preset,
chat_formatting_function=dynamic_import_function(args.chat_formatting_function) if args.use_chat_format else None
)
elif args.openai_engine:
# gpt-3 language models
cache_path = os.path.join(args.save_dir, "openai_query_cache.jsonl")
if args.openai_engine in ['ada', 'babbage', 'curie', 'davinci', 'text-davinci-003', 'text-davinci-002', 'code-davinci-002']:
if "truth" in args.metrics or "info" in args.metrics:
print("Running generations")
run_gpt3(questions, args.openai_engine, args.openai_engine, cache_path=cache_path, batch_size=args.eval_batch_size, preset=args.preset)
if 'mc' in args.metrics:
print("Running multiple-choice classification!")
run_gpt3_mc(questions, args.openai_engine, args.openai_engine, cache_path=cache_path, batch_size=args.eval_batch_size, preset=args.preset)
# other openai engines
else:
if "truth" in args.metrics or "info" in args.metrics:
print("Running generations")
run_chatgpt(questions, args.openai_engine, args.openai_engine, cache_path=cache_path, batch_size=args.eval_batch_size, preset=args.preset)
if "mc" in args.metrics:
raise ValueError("OpenAI Chat engines does not support MC metrics.")
# run metrics
print("Running metrics!")
model_key = args.model_name_or_path if args.model_name_or_path else args.openai_engine
for metric in args.metrics:
if metric == 'mc':
continue
elif metric in ['truth', 'info']:
if model_key not in questions.columns:
raise ValueError("Answers missing for {0}!".format(model_key))
try:
if metric == 'truth':
if args.gpt_truth_model_name:
questions = run_gpt_classifier_eval(model_key, 'truth', args.gpt_truth_model_name, questions, info=False)
elif args.hf_truth_model_name_or_path:
truth_classifier, truth_tokenizer = load_hf_lm_and_tokenizer(
model_name_or_path=args.hf_truth_model_name_or_path,
tokenizer_name_or_path=args.hf_truth_model_name_or_path,
device_map="balanced_low_0" if torch.cuda.device_count() > 1 else "auto",
)
questions = run_hf_classifier_eval(model_key, 'truth', truth_classifier, truth_tokenizer, questions, info=False)
else:
if args.gpt_info_model_name:
questions = run_gpt_classifier_eval(model_key, 'info', args.gpt_info_model_name, questions, info=True)
elif args.hf_info_model_name_or_path:
info_classifier, info_tokenizer = load_hf_lm_and_tokenizer(
model_name_or_path=args.hf_info_model_name_or_path,
tokenizer_name_or_path=args.hf_info_model_name_or_path,
device_map="balanced_low_0" if torch.cuda.device_count() > 1 else "auto",
)
questions = run_hf_classifier_eval(model_key, 'info', info_classifier, info_tokenizer, questions, info=True)
except Exception as err:
print(err)
else:
warnings.warn("Metric {0} not known, skipping!".format(metric), stacklevel=2)
if "truth" in args.metrics and "info" in args.metrics:
questions["{} truth-info acc".format(model_key)] = questions["{} truth acc".format(model_key)] * questions["{} info acc".format(model_key)]
# save all
questions.to_csv(os.path.join(args.save_dir, "predictions.csv"), index=False)
# format and print basic results
results = format_frame(questions)
results = results.mean(axis=0)
results = results.reset_index().rename(columns={'level_0': 'Model',
'level_1': 'Metric',
0: 'Value'})
# filter to most informative metrics
results = results[results['Metric'].isin(['MC1', 'MC2',
'truth acc',
'info acc',
'truth-info acc'])]
results = pd.pivot_table(results, 'Value', 'Model', 'Metric')
results.to_csv(os.path.join(args.save_dir, 'summary.csv'))
print(results)
results = results.loc[model_key].to_dict()
with open(os.path.join(args.save_dir, 'metrics.json'), 'w') as f:
json.dump(results, f, indent=2)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="The HuggingFace model to be evaluated."
)
parser.add_argument(
"--tokenizer_name_or_path",
type=str,
default=None,
help="If specified, we will load the tokenizer from here."
)
parser.add_argument(
"--use_slow_tokenizer",
action="store_true",
help="If given, we will use the slow tokenizer."
)
parser.add_argument(
"--openai_engine",
type=str,
default=None,
help="If specified, we will evaluate the OpenAI engine."
)
parser.add_argument(
"--data_dir",
type=str,
default="data/eval/truthfulqa",
help="The directory containing the truthfulqa data. Download from https://github.com/sylinrl/TruthfulQA/tree/main/data."
)
parser.add_argument(
"--save_dir",
type=str,
default="results/truthfulqa/",
help="The directory to save the results."
)
parser.add_argument(
"--num_instances",
type=int,
default=None,
help="The number of instances to evaluate. If not given, we will evaluate all instances."
)
parser.add_argument(
"--load_in_8bit",
action="store_true",
help="Load model in 8bit mode, which will reduce memory and speed up inference."
)
parser.add_argument(
"--gptq",
action="store_true",
help="If given, we're evaluating a 4-bit quantized GPTQ model."
)
parser.add_argument(
"--eval_batch_size",
type=int,
default=1,
help="batch size for evaluation."
)
parser.add_argument(
"--use_chat_format",
action="store_true",
help="If given, we will use the chat format for the prompts."
)
parser.add_argument(
"--chat_formatting_function",
type=str,
default="eval.templates.create_prompt_with_tulu_chat_format",
help="The function to use to create the chat format. This function will be dynamically imported. Please see examples in `eval/templates.py`."
)
parser.add_argument(
'--metrics',
nargs='+',
default=['truth', 'info', 'mc'],
choices=['truth', 'info', 'mc'],
help='Metrics to run'
)
parser.add_argument(
'--preset',
type=str,
default='qa',
help='Preset to use for prompt generation. Please see presets.py for options.'
)
parser.add_argument(
'--gpt_truth_model_name',
type=str,
help='A trained GPT judge model name to be used for computing the metrics for `truth` if it is specified.' \
'Either `gpt_truth_model_name` or `hf_truth_model_name_or_path` should be specified for computing the metric.'
)
parser.add_argument(
'--gpt_info_model_name',
type=str,
help='A trained GPT judge model name to be used for computing the metrics for `info` if it is specified.' \
'Either `gpt_info_model_name` or `hf_info_model_name_or_path` should be specified for computing the metric.'
)
parser.add_argument(
'--hf_truth_model_name_or_path',
type=str,
help='A trained HuggingFace judge model name to be used for computing the metrics for `truth` if it is specified.' \
'Either `gpt_truth_model_name` or `hf_truth_model_name_or_path` should be specified for computing the metric.'
)
parser.add_argument(
'--hf_info_model_name_or_path',
type=str,
help='A trained HuggingFace judge model name to be used for computing the metrics for `info` if it is specified.' \
'Either `gpt_info_model_name` or `hf_info_model_name_or_path` should be specified for computing the metric.'
)
args = parser.parse_args()
main(args)