-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmlu_eval.py
461 lines (433 loc) · 15.8 KB
/
tmlu_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
import argparse
import configparser
import json
import logging
import os
from collections.abc import Callable
from pprint import pprint
from datasets import load_dataset
from model import Anthropic_LM, Google_LM, HFLM_transformers, HFLM_vLLM, OpenAI_LM
from template import anthropic_template, hf_template, openai_template
from utils import check_ans, check_ans_cot
config = configparser.ConfigParser()
config.read('config.ini')
logging.basicConfig(
level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
SUBSETS = [
'AST_chinese',
'AST_mathematics',
'AST_biology',
'AST_chemistry',
'AST_physics',
'AST_civics',
'AST_geography',
'AST_history',
'GSAT_chinese',
'GSAT_chemistry',
'GSAT_biology',
'GSAT_physics',
'GSAT_earth_science',
'GSAT_mathematics',
'GSAT_geography',
'GSAT_history',
'GSAT_civics',
'CAP_chinese',
'CAP_mathematics',
'CAP_biology',
'CAP_history',
'CAP_civics',
'CAP_geography',
'CAP_physics',
'CAP_chemistry',
'CAP_earth_science',
'driving_rule',
'basic_traditional_chinese_medicine',
'clinical_traditional_chinese_medicine',
'lawyer_qualification',
'nutritionist',
'tour_guide',
'tour_leader',
'taiwan_tourist_resources',
'clinical_psychologist',
'teacher_qualification',
'accountant',
]
def parse_args():
parser = argparse.ArgumentParser(description='Run TMLU-Eval')
parser.add_argument(
'--backend',
choices=['hf', 'vllm', 'openai', 'anthropic', 'google', 'custom_api'],
required=True,
help='The backend type. '
"Options: ['hf', 'vllm', 'openai', 'anthropic', 'google', 'custom_api']",
)
parser.add_argument(
'--model',
type=str,
required=True,
help='Model name.',
)
parser.add_argument(
'--cache_dir',
type=str,
default=None,
help='The dir to store the pretrained models downloaded from huggingface.co.',
)
parser.add_argument(
'--revision', type=str, default=None, help='The revision of the huggingface model.'
)
parser.add_argument('--dtype', type=str, default='bfloat16', help='The dtype of the model.')
parser.add_argument('--temperature', type=float, default=0.0, help='Sampling temperature.')
parser.add_argument(
'--max_length',
type=int,
default=None,
help='Max input length for the open source model. '
'Use max_length in generation_config or max_position_embeddings in config '
'if this arg is not provided',
)
parser.add_argument(
'--max_tokens',
type=int,
default=128,
help='Max new tokens to generate for generation based evalutaion.',
)
parser.add_argument(
'--subsets',
type=str,
default='ALL',
help="The subsets of TMLU (splited by comma). Default is 'ALL'.",
)
parser.add_argument(
'--base_url', type=str, default=None, help='The base url for OpenAI python API library.'
)
parser.add_argument(
'--tensor_parallel_size', type=int, default=1, help='Tensor parallel size for vllm.'
)
parser.add_argument(
'--log_dir', type=str, default=None, help='Directory for saving evaluation log.'
)
parser.add_argument(
'--overwrite_log_dir', action='store_true', help='Overwrite logs in the directory.'
)
parser.add_argument(
'--few_shot_num',
type=int,
default=5,
help='The number for few shot example. Range: [0, 5]. Default is 5.',
)
parser.add_argument(
'--timeout', type=float, default=20.0, help='Timeout for API based backend.'
)
parser.add_argument(
'--max_retries', type=int, default=100, help='Max retries for API based backend.'
)
parser.add_argument('--cot', action='store_true', help='Use CoT evaluation.')
parser.add_argument(
'--reduce_few_shot',
action='store_true',
help='Reduce few-shot example number when prompt exceed the model length limit.',
)
parser.add_argument(
'--apply_chat_template', action='store_true', help='Apply chat template on the prompts.'
)
return parser.parse_args()
def format_problem(
example: dict[str, str],
model_template: Callable,
topic_line: str = '以下選擇題為出自臺灣的考題,答案為其中一個選項。',
few_shot_examples: list[dict[str, str]] = None,
few_shot_num: int = 0,
cot: bool = False,
tokenizer=None,
max_length=None,
prefill=None,
apply_chat_template=True,
) -> tuple[str, list[str]]:
if tokenizer is None:
prompt = topic_line + '\n\n'
if few_shot_examples and few_shot_num:
for i in range(few_shot_num):
fs_ex = few_shot_examples[i]
fs_ex_prompt = model_template(fs_ex, use_cot=cot, include_ans=True)
prompt += fs_ex_prompt + '\n\n'
example_prompt = model_template(example, use_cot=cot, include_ans=False)
prompt += example_prompt
else:
prompt = topic_line + '\n\n'
example_prompt = model_template(example, use_cot=cot, include_ans=False)
if apply_chat_template:
template = (
tokenizer.apply_chat_template(
[{'role': 'user', 'content': ''}], tokenize=False, add_generation_prompt=True
)
+ prefill
)
else:
template = prefill
left_length = max_length
left_length -= len(tokenizer.encode(prompt, add_special_tokens=False))
left_length -= len(tokenizer.encode(example_prompt, add_special_tokens=False))
left_length -= len(tokenizer.encode(template, add_special_tokens=False))
if few_shot_examples and few_shot_num:
for i in range(few_shot_num):
fs_ex = few_shot_examples[i]
fs_ex_prompt = model_template(fs_ex, use_cot=cot, include_ans=True)
fs_ex_prompt += '\n\n'
fs_ex_prompt_length = len(tokenizer.encode(fs_ex_prompt, add_special_tokens=False))
if left_length - fs_ex_prompt_length > 0:
prompt += fs_ex_prompt
left_length -= fs_ex_prompt_length
prompt += example_prompt
example['prompt'] = prompt
return example
if __name__ == '__main__':
args = parse_args()
if args.backend == 'openai':
api_key = os.environ.get('OPENAI_API_KEY')
model = OpenAI_LM(
model_name=args.model,
max_tokens=args.max_tokens,
temperature=args.temperature,
api_key=api_key,
base_url=args.base_url,
timeout=args.timeout,
max_retries=args.max_retries,
)
elif args.backend == 'anthropic':
api_key = os.environ.get('ANTHROPIC_API_KEY')
model = Anthropic_LM(
model_name=args.model,
max_tokens=args.max_tokens,
temperature=args.temperature,
api_key=api_key,
timeout=args.timeout,
max_retries=args.max_retries,
)
elif args.backend == 'google':
api_key = os.environ.get('GOOGLE_API_KEY')
model = Google_LM(
model_name=args.model,
max_tokens=args.max_tokens,
temperature=args.temperature,
api_key=api_key,
timeout=args.timeout,
max_retries=args.max_retries,
)
elif args.backend == 'custom_api':
api_key = 'EMPTY'
model = OpenAI_LM(
model_name=args.model,
max_tokens=args.max_tokens,
temperature=args.temperature,
api_key=api_key,
base_url=args.base_url,
timeout=args.timeout,
max_retries=args.max_retries,
)
elif args.backend == 'hf':
if args.cot:
raise ValueError('CoT evaluation is not supported with HF backend now.')
model = HFLM_transformers(
model_name=args.model,
max_tokens=args.max_tokens,
max_length=args.max_length,
temperature=args.temperature,
revision=args.revision,
dtype=args.dtype,
cache_dir=args.cache_dir,
)
else:
model = HFLM_vLLM(
model_name=args.model,
tensor_parallel_size=args.tensor_parallel_size,
max_tokens=args.max_tokens,
max_length=args.max_length,
temperature=args.temperature,
revision=args.revision,
dtype=args.dtype,
cache_dir=args.cache_dir,
)
if args.subsets == 'ALL':
subsets = SUBSETS
else:
subsets = [x.strip() for x in args.subsets.split(',')]
for subset in subsets:
if subset not in SUBSETS:
raise ValueError(f'{subset} is not an available subset of TMLU.')
results = {}
if args.log_dir:
log_root = args.log_dir
else:
if args.backend == 'hf':
log_root = os.path.join('log', f"{args.model.replace('/', '_')}_logits")
elif args.cot:
log_root = os.path.join('log', f"{args.model.replace('/', '_')}_cot")
else:
log_root = os.path.join('log', args.model.replace('/', '_'))
os.makedirs(log_root, exist_ok=True)
if args.cot:
score_func = check_ans_cot
else:
score_func = check_ans
for subset_name in subsets:
logging.info(f'Evaluating {subset_name}')
test_data = load_dataset(
'miulab/tmlu',
subset_name,
split='test',
)
fs_data = load_dataset(
'miulab/tmlu',
subset_name,
split='dev',
)
past_scores = []
past_ids = set()
if (
os.path.isfile(os.path.join(log_root, f'{subset_name}_out.jsonl'))
and not args.overwrite_log_dir
):
with open(os.path.join(log_root, f'{subset_name}_out.jsonl')) as f:
lines = f.readlines()
for line in lines:
example = json.loads(line)
past_ids.add(example['id'])
score = 1 if score_func(example['full_response'], example['gold_answer']) else 0
past_scores.append(score)
test_data = test_data.filter(lambda example: example['id'] not in past_ids)
if args.backend == 'openai' or args.backend == 'custom_api':
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': openai_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
},
)
if args.cot:
outputs = model.generate(test_data, prefill='')
else:
outputs = model.generate(test_data, prefill='')
elif args.backend == 'anthropic':
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': anthropic_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
},
)
if args.cot:
outputs = model.generate(test_data, prefill='\n讓我們一步一步思考。\n')
else:
outputs = model.generate(test_data, prefill='\n正確答案:(')
elif args.backend == 'google':
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': openai_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
},
)
if args.cot:
outputs = model.generate(test_data, prefill='')
else:
outputs = model.generate(test_data, prefill='')
elif args.backend == 'hf':
if args.reduce_few_shot:
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': hf_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
'tokenizer': model.tokenizer,
'max_length': model.model_max_length,
'prefill': '\n正確答案:(',
'apply_chat_template': args.apply_chat_template,
},
)
else:
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': hf_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
},
)
outputs = model.generate(
test_data, prefill='\n正確答案:(', apply_chat_template=args.apply_chat_template
)
else:
if args.cot:
prefill = '\n讓我們一步一步思考。\n'
else:
prefill = '\n正確答案:('
if args.reduce_few_shot:
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': hf_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
'tokenizer': model.tokenizer,
'max_length': model.model_max_length,
'prefill': prefill,
'apply_chat_template': args.apply_chat_template,
},
)
else:
test_data = test_data.map(
format_problem,
load_from_cache_file=False,
fn_kwargs={
'model_template': hf_template,
'few_shot_examples': fs_data,
'few_shot_num': args.few_shot_num,
'cot': args.cot,
},
)
outputs = model.generate(
test_data, prefill=prefill, apply_chat_template=args.apply_chat_template
)
if args.overwrite_log_dir:
output_file_open_type = 'w'
else:
output_file_open_type = 'a'
with open(os.path.join(log_root, f'{subset_name}_out.jsonl'), output_file_open_type) as f:
for i in range(len(outputs)):
line = {
'id': test_data[i]['id'],
'prompt': test_data[i]['prompt'],
'full_response': outputs[i],
'gold_answer': test_data[i]['answer'],
}
f.write(json.dumps(line, ensure_ascii=False) + '\n')
if len(outputs) != len(test_data):
raise ValueError(
f'Error occurred when evaluating {subset_name}: output length mismatch'
)
scores = [
1 if score_func(output, row['answer']) else 0 for output, row in zip(outputs, test_data)
]
scores += past_scores
avg_score = sum(scores) / len(scores)
results[subset_name] = avg_score
pprint(results)