From 5fb578aa0030dc1112b6e3e9cb2316a2480e5f1d Mon Sep 17 00:00:00 2001 From: Francesco Bertolotti Date: Wed, 21 May 2025 13:08:48 +0200 Subject: [PATCH 1/2] timed re.search and _executor made global --- opencompass/datasets/mbpp.py | 39 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/opencompass/datasets/mbpp.py b/opencompass/datasets/mbpp.py index fca83b31..99a50ddd 100644 --- a/opencompass/datasets/mbpp.py +++ b/opencompass/datasets/mbpp.py @@ -4,7 +4,7 @@ import itertools import json import multiprocessing import os.path as osp -import re +import regex as re import signal import tempfile from collections import defaultdict @@ -330,7 +330,7 @@ class MBPPEvaluator(BaseEvaluator): r"'(.*)'\s*\[DONE\]", ] for p in patterns: - match = re.search(p, text, re.DOTALL) + match = re.search(p, text, re.DOTALL, timeout=10.0) if match: text = match.group(1) break @@ -383,6 +383,22 @@ class MBPPEvaluator2(MBPPEvaluator): text = text[1:] return text +def _execution(programs, timeout, key): + try: + # Add exec globals to prevent the exec to raise + # unnecessary NameError for correct answer + exec_globals = {} + with swallow_io(): + with time_limit(timeout): + exec(programs, exec_globals) + key.append('pass') + except TimeOutException: + key.append('timeout') + except AssertionError: + key.append('wrong_answer') + except BaseException as e: + print(e) + key.append('failed') def execution(programs, task_id, timeout): """Execution function for running generation code. @@ -400,29 +416,12 @@ def execution(programs, task_id, timeout): control the process. """ - def _execution(programs, timeout): - try: - # Add exec globals to prevent the exec to raise - # unnecessary NameError for correct answer - exec_globals = {} - with swallow_io(): - with time_limit(timeout): - exec(programs, exec_globals) - key.append('pass') - except TimeOutException: - key.append('timeout') - except AssertionError: - key.append('wrong_answer') - except BaseException as e: - print(e) - key.append('failed') - manager = multiprocessing.Manager() key = manager.list() # `signal` cannot be used in child thread, therefore, we # need to create a process in the thread. p = multiprocessing.Process(target=_execution, - args=(programs, timeout - 1)) + args=(programs, timeout - 1, key)) p.start() p.join(timeout=timeout) if p.is_alive(): From 2fc30a92ec76ef2cd4e6e736b640b3d9c015b0d9 Mon Sep 17 00:00:00 2001 From: Francesco Bertolotti Date: Wed, 28 May 2025 10:42:32 +0200 Subject: [PATCH 2/2] TimeOutError exception handling --- opencompass/datasets/mbpp.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/opencompass/datasets/mbpp.py b/opencompass/datasets/mbpp.py index 99a50ddd..12a6865e 100644 --- a/opencompass/datasets/mbpp.py +++ b/opencompass/datasets/mbpp.py @@ -330,7 +330,11 @@ class MBPPEvaluator(BaseEvaluator): r"'(.*)'\s*\[DONE\]", ] for p in patterns: - match = re.search(p, text, re.DOTALL, timeout=10.0) + try: + match = re.search(p, text, re.DOTALL, timeout=10.0) + except TimeoutError: + match = None + if match: text = match.group(1) break