-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
577 lines (459 loc) · 19.6 KB
/
utils.py
File metadata and controls
577 lines (459 loc) · 19.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
"""
Adopted from https://github.com/princeton-nlp/DensePhrases/blob/main/densephrases/utils/eval_utils.py
"""
import re
import string
import json
import math
import unicodedata
from math import isclose
from collections import Counter
from rouge_score import rouge_scorer
from functools import partial
from tqdm import tqdm
import torch
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
def normalize_answer(s):
return white_space_fix(remove_articles(remove_punc(lower(s))))
def normalize_answer_with_punc(s):
return white_space_fix(remove_articles(lower(s)))
def remove_citations(sent):
return re.sub(r"\[\d+", "", re.sub(r" \[\d+", "", sent)).replace(" |", "").replace("]", "")
def f1_score(prediction, ground_truth):
normalized_prediction = normalize_answer(prediction)
normalized_ground_truth = normalize_answer(ground_truth)
ZERO_METRIC = (0, 0, 0)
prediction_tokens = normalized_prediction.split()
ground_truth_tokens = normalized_ground_truth.split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return ZERO_METRIC
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1, precision, recall
def drqa_normalize(text):
"""Resolve different type of unicode encodings."""
return unicodedata.normalize('NFD', text)
def drqa_exact_match_score(prediction, ground_truth):
"""Check if the prediction is a (soft) exact match with the ground truth."""
return normalize_answer(prediction) == normalize_answer(ground_truth)
def substring_exact_match_score(prediction, ground_truth):
"""Check if the ground truth is a (soft) exact match substring of the prediction."""
return normalize_answer(ground_truth) in normalize_answer(prediction)
def drqa_metric_max_over_ground_truths(metric_fn, prediction, ground_truths):
"""Given a prediction and multiple valid answers, return the score of
the best prediction-answer_n pair given a metric function.
"""
# ground truth could be a string or a list of strings or a list of list of strings
if isinstance(ground_truths, str):
ground_truths = [ground_truths]
elif isinstance(ground_truths[0], list):
ground_truths = [ground_truth for ground_truths_list in ground_truths for ground_truth in ground_truths_list]
scores_for_ground_truths = []
for ground_truth in ground_truths:
score = metric_fn(prediction, ground_truth)
scores_for_ground_truths.append(score)
return max(scores_for_ground_truths)
def get_max_memory():
"""Get the maximum memory available for the current GPU for loading models."""
free_in_GB = int(torch.cuda.mem_get_info()[0]/1024**3)
max_memory = f'{free_in_GB-6}GB'
n_gpus = torch.cuda.device_count()
max_memory = {i: max_memory for i in range(n_gpus)}
return max_memory
def get_top_tokens(logits, tokenizer, top_k=10):
"""Get the top tokens and their probabilities from the logits."""
top_tokens = []
for logit in logits:
a, b = torch.topk(torch.softmax(logit, dim=-1), top_k, dim=-1)
l = [(y, f"{x*100:.02f}") for x, y in zip(a[0], tokenizer.convert_ids_to_tokens(b[0]))]
top_tokens.append(l)
return top_tokens
def parse_output(output, prefix="Answer:"):
def lstrip_string(s, sub):
return re.sub(f'^{re.escape(sub)}', '', s, flags=re.IGNORECASE)
patterns = [re.compile(f"(?:{prefix})(.*)", flags=re.IGNORECASE | re.DOTALL), # prefix + answer + sentence end
re.compile(r"(?:^)(.*)", flags=re.IGNORECASE | re.DOTALL)] # the beginning + answer + sentence end
for pat in patterns:
matches = pat.search(output)
if matches is not None:
return lstrip_string(matches[1].strip(), prefix).strip() # 0 index includes the non-capturing group # lstrip again because for chat models sometimes it will repeat the prefix
# if still not found, return None, but should actually never get this case...
return None
def extract_binary_label(prediction):
prediction = normalize_answer_with_punc(prediction)
pattern = r"\b(?:yes|no)\b(?!.*\b(?:yes|no)\b)"
match = re.search(pattern, prediction, re.IGNORECASE | re.DOTALL)
label_map = {"yes": 1, "no": 0}
if match:
return label_map[match.group(0)]
else:
if prediction.strip().isdigit():
return int(prediction.strip())
return -1
def turn_int_list(curr_list):
new_list = []
for item in curr_list:
try:
item = int(item)
new_list.append(item)
except:
pass
return new_list
def extract_cnt_list(prediction):
prediction = normalize_answer_with_punc(prediction)
pattern1 = r'\[[\d\s,]+\]' # r'\[[\d\s,]+\](?!.*\[[\d\s,]+\])'
match = re.findall(pattern1, prediction, re.IGNORECASE | re.DOTALL)
if match:
try:
return json.loads(match[-1])
except json.JSONDecodeError:
pass
pattern2 = r'\[.*?\]' # r'\[.*?\](?!.*\[.*\])'
match = re.findall(pattern2, prediction, re.IGNORECASE | re.DOTALL)
if match:
try:
return turn_int_list(json.loads(match[-1]))
except json.JSONDecodeError:
pass
numbers = re.findall(r'\d+', prediction)
num_list = [int(x) for x in numbers]
return num_list
def extract_choice_letter(prediction):
prediction = white_space_fix(prediction)
pattern1 = r"answer is \(?([A-J])\)?"
match = re.search(pattern1, prediction)
if match:
choice = match.group(1)
return ord(choice) - ord("A")
pattern2 = r'[aA]nswer:\s*([A-J])'
match = re.search(pattern2, prediction)
if match:
choice = match.group(1)
return ord(choice) - ord("A")
pattern3 = r"\b[A-J]\b(?!.*\b[A-J]\b)"
match = re.search(pattern3, prediction, re.DOTALL)
if match:
choice = match.group(0)
return ord(choice) - ord("A")
return -1
def extract_class_num(prediction):
prediction = white_space_fix(prediction)
# pattern 1: "label: X" format
pattern1 = r"label:\s*(\d+)"
match = re.search(pattern1, prediction, re.IGNORECASE)
if match:
return int(match.group(1))
# pattern 2: Last number in the text
pattern2 = r"\b(\d+)\b(?!.*\b\d+\b)"
match = re.search(pattern2, prediction)
if match:
return int(match.group(1))
# pattern 3: First number as fallback
numbers = re.findall(r'\d+', prediction)
if numbers:
return int(numbers[0])
return -1
# NOTE: we replace soft_acc with the sum of a list for robust evaluation
# NOTE: we still use the name soft_acc to distinguish it with other metrics in the code
def get_soft_acc(prediction, answer):
return sum(prediction) == sum(answer)
def get_docqa_clean_string(s):
s = str(s).lower().strip()
s = s.replace(",", "")
suffix_list = ["kg", "meters", "acres", "minutes", "miles", "mile",
"feet",
"million", "thousand", "billion", "mm", "m"]
for suffix in suffix_list:
s = re.sub(re.escape(suffix) + r'$', '', s).strip()
# remove parenthesis
# s = re.sub(r'\s*\([^)]*\)', "", s).strip()
# remove quotes
s = re.sub(r"^['\"]|['\"]$", "", s).strip()
s = s.strip().strip("$").strip()
s = s.strip().strip("£").strip()
s = s.strip().strip("%").strip()
return s
def is_float_equal(reference, prediction, include_percentage=False) -> bool:
if include_percentage:
gt_result = [reference / 100, reference, reference * 100]
else:
gt_result = [reference]
for item in gt_result:
if isclose(item, prediction, rel_tol=0.01):
return True
return False
def need_exact_match_check(s):
patterns = [
r'https://',
r'.*\.(py|ipynb)$',
r'^page',
r'^\d+(-\d+|\s\d+)?$',
r'(a\.m\.|p\.m\.)',
r'^\d{4}[-/\s]\d{1,2}[-/\s]\d{1,2}$', # YYYY-MM-DD, YYYY/MM/DD
r'^\d{1,2}[-/\s]\d{1,2}[-/\s]\d{2,4}$', # DD-MM-YYYY, MM/DD/YYYY
r'^\d{4}[-/\s]\d{1,2}$', # YYYY-MM, YYYY/MM
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
]
return any(re.search(pattern, s) for pattern in patterns)
def extract_number_list(pred):
pred_clean = pred.replace(',', '')
num_pattern = r'(-?\d+(\.\d*)?|-?\.\d+)' # r'-?(\d+(\.\d*)?|\.\d+)'
matches = re.findall(num_pattern, pred_clean)
numbers = []
for match_tuple in matches:
match = match_tuple[0]
try: # TODO filter inf number. Note this is added at the end of the experiment. previous is numers.append(float(match))
num = float(match)
if math.isfinite(num):
numbers.append(num)
except ValueError:
continue
return numbers
def get_str_type(num_str):
try:
num = float(get_docqa_clean_string(num_str))
if num == int(num) and "%" not in num_str:
return "Integer"
else:
return "Float"
except:
return "String"
def eval_docqa_score(gt, pred, answer_type):
if answer_type == "Integer":
gt = float(get_docqa_clean_string(str(gt)))
assert int(gt) == gt
gt = int(gt)
pred = get_docqa_clean_string(str(pred))
pred_num_list = [int(num) for num in extract_number_list(pred) if int(num) == num]
score = any(gt == pred_num for pred_num in pred_num_list)
elif answer_type == "Float":
gt = float(get_docqa_clean_string(str(gt)))
pred = get_docqa_clean_string(str(pred))
pred_num_list = extract_number_list(pred)
score = any(is_float_equal(gt, pred_num, include_percentage=True)
for pred_num in pred_num_list)
elif answer_type in ["String", "None"]:
if need_exact_match_check(gt):
score = gt in pred
else:
score = f1_score(pred, gt)[0]
elif answer_type == "List":
gt_list = json.loads(gt)
# merge f1 score text to prevent low precision
merge_flag = [True if isinstance(item, str) and get_str_type(item) == "String" and not
need_exact_match_check(item)
else False for item in gt_list] # we merge all answers that are string and don't need EM for better recall
merged_str = " ".join([item for item, m_flag in zip(gt_list, merge_flag) if m_flag]).strip()
if merged_str:
new_gt_list = [merged_str] + [item for item, m_flag in zip(gt_list, merge_flag) if not m_flag]
gt_list = new_gt_list
gt_score_list = [] # This is the greedy score similar to that used in LongDocURL
for gt in gt_list:
assert not isinstance(gt, list)
if isinstance(gt, int):
gt_type = "Integer"
elif isinstance(gt, float):
gt_type = "Float"
else: # String answers can also represent int and float
gt_type = get_str_type(gt)
gt_score = eval_docqa_score(gt, pred, gt_type)
gt_score_list.append(gt_score)
score = sum(gt_score_list) / len(gt_list)
else:
raise KeyError("Wrong answer type:", answer_type)
return float(score)
def parse_judge_output(model_output):
result = {
"scoring_rationale": "",
"score": None,
"json_data": None,
"raw_output": model_output
}
try:
rationale_match = re.search(r'\[Scoring Rationale\]:(.*?)(?=\[Score\]:|\[JSON\]:|$)',
model_output, re.DOTALL)
if rationale_match:
result["scoring_rationale"] = rationale_match.group(1).strip()
except Exception as e:
print(f"解析rationale错误: {e}")
result["scoring_rationale"] = None
try:
score_match = re.search(r'\[Score\]:\s*(\d+)\s*points?', model_output)
if score_match:
result["score"] = int(score_match.group(1))
except Exception as e:
print(f"解析score错误: {e}")
result["score"] = None
result["json_data"] = robust_json_extraction(model_output, ["answer_score"])
return result
def parse_list_judge_output(model_output):
result = {
"rationale": "",
"json_data": None,
"raw_output": model_output
}
try:
rationale_match = re.search(r'\[Rationale\]:(.*?)(?=\[JSON\]:|$)',
model_output, re.DOTALL)
if rationale_match:
result["rationale"] = rationale_match.group(1).strip()
except Exception as e:
print(f"解析Rationale错误: {e}")
result["rationale"] = None
result["json_data"] = robust_json_extraction(model_output, ["student_answer_count", "covered_count"])
return result
def robust_json_extraction(text, key_list):
try:
match = re.search(r'\[JSON\]:.*?(\{)', text, re.DOTALL)
if match:
start_index = match.start(1)
decoder = json.JSONDecoder()
obj, _ = decoder.raw_decode(text[start_index:])
return obj
except Exception as e:
print(f"标准JSON解析失败: {e}。将尝试按key列表进行正则匹配: {key_list}。")
extracted_data = {}
for key in key_list:
try:
pattern = rf'"{key}"\s*:\s*(-?\d+(?:\.\d+)?)'
match = re.search(pattern, text)
if match:
value_str = match.group(1)
extracted_data[key] = float(value_str)
except Exception as e:
print(f"按key列表正则匹配{key}时发生错误: {e}")
return extracted_data
def cover_key(json_data, key_list):
for key in key_list:
if key not in json_data:
return False
return True
from utils_prompt import DOC_QA_JUDGE_PROMPT, CURRENT_CASE_PROMPT
from utils_prompt import DOC_QA_LIST_F1_JUDGE_PROMPT, CURRENT_LIST_CASE_PROMPT
from vlm_model.model_utils import call_api
def eval_docqa_score_with_llm_judge(gt, pred, extra_info):
question = extra_info["question"]
prompt = DOC_QA_JUDGE_PROMPT + CURRENT_CASE_PROMPT.format(question=question, reference=gt, prediction=pred)
llm_judge_client = extra_info["llm_judge_client"]
func = partial(
llm_judge_client.chat.completions.create,
model=extra_info["llm_judge_model"],
messages=[{"role": "user", "content": prompt}],
max_tokens=256,
)
completion = call_api(func, limit=5, pause=5)
response = completion.choices[0].message.content
judge_result = parse_judge_output(response)
if judge_result["score"] is not None:
score = judge_result["score"]
elif judge_result["json_data"] is not None and "answer_score" in judge_result["json_data"]:
score = judge_result["json_data"]["answer_score"]
else:
score = 0.0
return {"final_score": score, "judge_result": judge_result}
def eval_docqa_list_score_with_llm_judge(gt, pred, extra_info):
question = extra_info["question"]
prompt = DOC_QA_LIST_F1_JUDGE_PROMPT + CURRENT_LIST_CASE_PROMPT.format(question=question, reference=gt, prediction=pred)
llm_judge_client = extra_info["llm_judge_client"]
func = partial(
llm_judge_client.chat.completions.create,
model=extra_info["llm_judge_model"],
messages=[{"role": "user", "content": prompt}],
max_tokens=256,
)
completion = call_api(func, limit=5, pause=5)
response = completion.choices[0].message.content
judge_result = parse_list_judge_output(response)
if judge_result["json_data"] is not None and cover_key(judge_result["json_data"], ["student_answer_count", "covered_count"]):
student_answer_count = judge_result["json_data"]["student_answer_count"]
count = judge_result["json_data"]["covered_count"]
if count > 0:
recall = count / len(json.loads(gt))
precision = count / student_answer_count
score = max(min((2 * recall * precision) / (recall + precision), 1.0), 0.0)
else:
score = 0.0
else:
score = 0.0
return {"final_score": score, "judge_result": judge_result}
def levenshtein_distance(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for i2, c2 in tqdm(enumerate(s2), desc="iterating s2"):
distances_ = [i2 + 1]
for i1, c1 in enumerate(s1):
if c1 == c2:
distances_.append(distances[i1])
else:
distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
distances = distances_
return distances[-1]
def anls_compute(prediction, groundtruth, threshold=-1):
dist = levenshtein_distance(groundtruth, prediction)
length = max(len(groundtruth.upper()), len(prediction.upper()))
value = 0.0 if length == 0 else float(dist) / float(length)
anls = 1.0 - value
if anls<=threshold:
anls = 0.0
return anls
r_scorer = rouge_scorer.RougeScorer(['rougeL', 'rougeLsum'], use_stemmer=True)
def calculate_metrics(prediction, answers, metrics, extra_info=None):
metric_list = [m.strip() for m in metrics.split(",")]
metric_res = {}
if "sub_em" in metric_list:
sub_em = drqa_metric_max_over_ground_truths(substring_exact_match_score, prediction, answers)
metric_res["sub_em"] = sub_em
if "binary_acc" in metric_list:
prediction, default_answer = prediction
label = extract_binary_label(prediction)
if label == -1:
label = default_answer
gt_label = extract_binary_label(answers)
metric_res["acc"] = int(label == gt_label)
# NOTE: We replace soft_acc with the sum of a list for robust evaluation
# NOTE: We still use the name soft_acc to distinguish it with other metrics in the code
if "soft_acc" in metric_list:
cnt_list = extract_cnt_list(prediction)
metric_res["soft_acc"] = get_soft_acc(cnt_list, answers)
if "mc_acc" in metric_list:
choice = extract_choice_letter(prediction)
metric_res["mc_acc"] = int(choice == answers)
if "cls_acc" in metric_list:
class_num = extract_class_num(prediction)
metric_res["cls_acc"] = int(class_num == answers)
if "rouge" in metric_list:
if isinstance(answers, str):
answers = [answers]
elif isinstance(answers[0], list):
answers = [ground_truth for ground_truths_list in answers for ground_truth in ground_truths_list]
rouges = [r_scorer.score(target=a, prediction=prediction) for a in answers]
for k in r_scorer.rouge_types:
metric_res[k + "_f1"] = max([r[k].fmeasure for r in rouges])
metric_res[k + "_recall"] = max([r[k].recall for r in rouges])
if "doc_qa" in metric_list:
metric_res["doc_qa"] = eval_docqa_score(answers, prediction, extra_info["answer_format"])
if "doc_qa_llm" in metric_list:
if extra_info["answer_format"] == "List":
metric_res["doc_qa_llm"] = eval_docqa_list_score_with_llm_judge(answers, prediction, extra_info=extra_info)
else:
metric_res["doc_qa_llm"] = eval_docqa_score_with_llm_judge(answers, prediction, extra_info=extra_info)
if "anls" in metric_list:
metric_res["anls"] = anls_compute(prediction, answers, threshold=-1)
return metric_res