-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_test.py
More file actions
388 lines (278 loc) · 13.8 KB
/
batch_test.py
File metadata and controls
388 lines (278 loc) · 13.8 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
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
import json
import os
import torch
import argparse
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
import transformers
import time
import random
import numpy as np
from transformers import set_seed
import re
import copy
from difflib import SequenceMatcher
seed = 42
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
set_seed(seed)
parser = argparse.ArgumentParser(description="Test a language model on a dataset")
parser.add_argument('--model_name', type=str, required=True, help="The name of the model to load")
parser.add_argument('--chat', action='store_true', help="Is chat model (default: False)")
parser.add_argument('--batch_size', type=int, default=2, help="Batch size for generation")
parser.add_argument('--cot', action='store_true', help="cot")
parser.add_argument('--cot_type', type=str, default='zero', help="The name of the model to load")
parser.add_argument('--dataset', type=str, default='dataset_1113', help="dataset")
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_name = args.model_name
batch_size = args.batch_size
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left")
tokenizer.pad_token = tokenizer.eos_token
model.eval()
print("load model: ", model_name)
generation_config = {
"temperature": 1e-7,
"max_new_tokens": 4096,
"batch_size": batch_size
}
import re
def extract_answer(text):
pattern = r"The answer is (.*?)(?:\.\s|\.\n|$)"
match = re.search(pattern, text)
if match:
answer = match.group(1)
answer = re.sub(r'[^\w\s\.,\{\}\[\]-]', '', answer)
answer = answer.rstrip('.')
return answer
return ''
def extract_last_num(text: str) -> float:
text = re.sub(r"(\d),(\d)", "\g<1>\g<2>", text)
res = re.findall(r"(\d+(\.\d+)?)", text)
if len(res) > 0:
num_str = res[-1][0]
return float(num_str)
else:
return None
def is_semantically_similar(answer, output, threshold=0.9):
ratio = SequenceMatcher(None, answer, output).ratio()
return ratio >= threshold
def learning_gen(batch_prompts, generator, tokenizer):
res = []
total_input_tokens, total_output_tokens = 0, 0
choose_prompt = "Answer the question if possible using the format: 'The answer is {your answer}.' If you cannot answer, respond with 'Need more samples.'\n"
final_prompt = "These are all the examples. Now you must answer this question, using the format: 'The answer is {your answer}.'\n"
example_num = 0
for prompt in batch_prompts:
i = 0
sys = [prompt[0]]
q = prompt[-1]['content']
prompt_copy = copy.deepcopy(prompt)
prompt_copy[-1]['content'] = choose_prompt + q
new_q = [prompt_copy[-1]]
prompt[-1]['content'] = final_prompt + q
final_q = [prompt[-1]]
example = prompt[1:-1]
new_prompt = sys + example[i:i + 10] + new_q
while i < len(example):
input_tokens = sum(count_tokens(p['content'], tokenizer) for p in new_prompt)
total_input_tokens += input_tokens
text_gen = generator(new_prompt)
generated_text = text_gen[0]['generated_text'][-1]['content']
output_tokens = count_tokens(generated_text, tokenizer)
total_output_tokens += output_tokens
if "more samples" not in generated_text:
break
else:
i += 10
example_num += len(example[i:i + 10])
if(i + 10 >= len(example)):
new_prompt = text_gen[0]['generated_text'] + example[i:i + 10] + final_q
else:
new_prompt = text_gen[0]['generated_text'] + example[i:i + 10] + new_q
example_num += len(example[i:])
res.append(text_gen)
return res, total_input_tokens, total_output_tokens, example_num
def count_tokens(text, tokenizer):
tokens = tokenizer.encode(text, add_special_tokens=False)
return len(tokens)
def gen_qwen(model, tokenizer, prompts):
texts = [
tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
for prompt in prompts
]
model_inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True).to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=generation_config["max_new_tokens"],
temperature=generation_config["temperature"]
)
input_lengths = [len(input_ids) for input_ids in model_inputs.input_ids]
gen_text = [
tokenizer.decode(output_ids[input_len:], skip_special_tokens=True)
for output_ids, input_len in zip(generated_ids, input_lengths)
]
return gen_text
def test_model_on_reason_dataset(file_path, level='l0', batch_size=1):
with open(file_path, 'r') as f:
data = json.load(f)
if 'l5' in level:
batch_size = 1
prompts = [item["prompt"] for item in data]
answers = [item["answer"] for item in data]
for prompt in prompts:
for p in prompt:
p['type'] = 'text'
if(p['role'] == 'bot'):
p['role'] = 'assistant'
if(p['role'] == 'human'):
p['role'] = 'user'
if args.cot:
if args.cot_type == 'zero':
for i in range(len(prompts)):
for j in range(len(prompts[i])):
if(prompts[i][j]['role'] == 'system'):
prompts[i][j]['content'] = "The task is to identify patterns and discover rules from the provided examples, then answer a question. The symbols in the question may not have their usual meanings, so carefully analyze the rules and expressions before providing your final answer in the format: 'Answer: The answer is {your answer}.'. Use step-by-step reasoning to explain your answer, even if examples only provide direct answers."
prompts[i][-1]['content'] = prompts[i][-1]['content'] + "Let's think step by step. \n\n"
acc, cnt, total_input_tokens, total_output_tokens, total_example_num = 0, 0, 0, 0, 0
if args.chat:
generator = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
pad_token_id=tokenizer.eos_token_id,
max_new_tokens=generation_config["max_new_tokens"],
batch_size=generation_config["batch_size"],
temperature=generation_config["temperature"]
)
for i in tqdm(range(0, len(prompts), batch_size), desc=f"Processing {file_path}"):
batch_prompts = prompts[i:i + batch_size]
batch_answers = answers[i:i + batch_size]
if 'l5' in level:
gen_texts, input_tokens, output_tokens, example_num = learning_gen(batch_prompts, generator, tokenizer)
total_input_tokens += input_tokens
total_output_tokens += output_tokens
total_example_num += example_num
elif 'qwen' in model_name.lower():
gen_texts = gen_qwen(model, tokenizer, batch_prompts)
else:
gen_texts = generator(batch_prompts)
batch_prompts = [p[0]['generated_text'][:-1] for p in gen_texts]
for j, output in enumerate(gen_texts):
if 'qwen' in model_name.lower():
generated_text = output
else:
generated_text = output[0]['generated_text'][-1]['content']
output_text = extract_answer(generated_text)
if 'l5' not in level:
input_tokens = sum(count_tokens(p['content'], tokenizer) for p in batch_prompts[j])
output_tokens = count_tokens(generated_text, tokenizer)
total_input_tokens += input_tokens
total_output_tokens += output_tokens
if extract_last_num(batch_answers[j]) is not None and ('l0' in level or 'l3' in level or 'l6' in level):
if (extract_last_num(output_text)) != None:
is_correct = abs(extract_last_num(output_text) - extract_last_num(batch_answers[j])) < 1e-2
else:
is_correct = 0
else:
is_correct = (batch_answers[j] == output_text)
acc += is_correct
cnt += 1
output_records.append({
"prompt": batch_prompts[j],
'raw_output': generated_text,
"output": output_text,
"answer": batch_answers[j],
'correct': is_correct,
'input_tokens': input_tokens,
'output_tokens': output_tokens
})
else:
for i in tqdm(range(0, len(prompts), batch_size), desc=f"Processing {file_path}"):
batch_prompts = prompts[i:i + batch_size]
batch_answers = answers[i:i + batch_size]
inputs = tokenizer(batch_prompts, return_tensors="pt", padding=True).to(device)
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=25, temperature=0)
for j, output in enumerate(outputs):
output_text = tokenizer.decode(output, skip_special_tokens=True)
output_text = output_text[len(batch_prompts[j]):].split('\n')[0]
input_tokens = count_tokens(batch_prompts[j], tokenizer)
output_tokens = count_tokens(output_text, tokenizer)
total_input_tokens += input_tokens
total_output_tokens += output_tokens
if extract_last_num(batch_answers[j]) != None:
is_correct = abs(extract_last_num(output_text) - extract_last_num(batch_answers[j])) < 1e-2
else:
is_correct = batch_answers[j] in output_text
acc += is_correct
cnt += 1
output_records.append({
"prompt": batch_prompts[j],
"output": output_text,
"answer": batch_answers[j],
'correct': is_correct,
'input_tokens': input_tokens,
'output_tokens': output_tokens,
'total_example_num': total_example_num
})
accuracy = acc / cnt if cnt > 0 else 0
total_tokens = {"total_input_tokens": total_input_tokens, "total_output_tokens": total_output_tokens, 'total_example_num': total_example_num}
return accuracy, total_tokens
levels = ['l0', 'l1', 'l2', 'l3', 'l4', 'l6']
for level in levels:
if args.chat:
level = level + '_chat'
results_dir = f'result_chat_{args.dataset}/{model_name}/result'
else:
results_dir = f'result_{args.dataset}/{model_name}/result'
if args.cot:
results_dir = results_dir + '_' + args.cot_type
dataset_dir = args.dataset + f'/{level}/'
datasets = sorted(os.listdir(dataset_dir))
config_file = os.path.join(results_dir, 'config.json')
os.makedirs(results_dir, exist_ok=True)
with open(config_file, 'w') as f:
json.dump(generation_config, f, indent=4)
print(dataset_dir)
for filename in datasets:
if 'dataset' in filename and filename.endswith('.json'):
if 'gsm8k_cot.json' in filename and not args.cot:
continue
if 'gsm8k.json' in filename and args.cot:
continue
res_dir = os.path.join(results_dir, level)
file_results_dir = os.path.join(res_dir, filename.replace('.json', ''))
os.makedirs(file_results_dir, exist_ok=True)
results_file = os.path.join(file_results_dir, 'result.json')
if os.path.exists(results_file):
with open(results_file, 'r') as f:
previous_results = json.load(f)
print(f"Results for {filename} already exist:")
continue
output_records = []
file_path = os.path.join(dataset_dir, filename)
start_time = time.time()
accuracy, total_tokens = test_model_on_reason_dataset(file_path, level=level, batch_size=batch_size)
end_time = time.time()
run_time = end_time - start_time
with open(results_file, 'w') as f:
json.dump({
filename: accuracy,
"token_usage": total_tokens,
"run_time_seconds": run_time
}, f, indent=4)
print("accuracy: ", accuracy)
print("Token usage:", total_tokens)
print("Run time (seconds):", run_time)
output_file = os.path.join(file_results_dir, 'output.json')
with open(output_file, 'w') as f:
json.dump(output_records, f, indent=4)
print(f"Results saved to {results_file}")
print(f"Test outputs saved to {output_file}")