Datasets:
Download cc_valid/evaluator.py from Tommysha/zero-code-l1-val: direct link, hf CLI and curl.
- Browser
- Download file 22.9 kB
-
https://huggingface.co/datasets/Tommysha/zero-code-l1-val/resolve/main/cc_valid/evaluator.py
- Command line
-
hf download hf://datasets/Tommysha/zero-code-l1-val/cc_valid/evaluator.py
-
curl -L -o evaluator.py https://huggingface.co/datasets/Tommysha/zero-code-l1-val/resolve/main/cc_valid/evaluator.py
22.9 kB
| #!/usr/bin/env python3 | |
| """Zevo custom evaluator for zero-code L1 CodeContests valid (117 problems), pass@1. | |
| Schema matches LiveCodeBench rows so the same testing_util grades stdin tests. | |
| Usage (Zevo protocol): python3 evaluator.py PREDICTIONS_CSV SCORING_SET METRICS_JSON | |
| PREDICTIONS_CSV: columns id,prediction (raw model output; the last ``` fenced block is the program) | |
| SCORING_SET: the Test set rows (parquet / jsonl / json / csv) with id, public_test_cases, | |
| private_test_cases, metadata | |
| METRICS_JSON: written as {"score": pass@1, "correct": ..., "total": ..., "by_difficulty": {...}} | |
| Grading is LiveCodeBench's own testing_util (vendored verbatim below, numpy import dropped: it was unused), | |
| with LCB's defaults: timeout 6 s per test, all public + private tests must pass, missing prediction = fail. | |
| Stdlib only (pyarrow/pandas needed only if SCORING_SET is parquet). | |
| """ | |
| # ---------------- vendored: lcb_runner/evaluation/testing_util.py @ 28fef95 ---------------- | |
| import ast | |
| import json | |
| import sys | |
| import faulthandler | |
| import platform | |
| # used for debugging to time steps | |
| from datetime import datetime | |
| # to run the solution files we're using a timing based approach | |
| import signal | |
| from io import StringIO | |
| # used for testing the code that reads from input | |
| from unittest.mock import patch, mock_open | |
| # from pyext import RuntimeModule | |
| from types import ModuleType | |
| from enum import Enum | |
| from decimal import Decimal | |
| import time | |
| import_string = "from string import *\nfrom re import *\nfrom datetime import *\nfrom collections import *\nfrom heapq import *\nfrom bisect import *\nfrom copy import *\nfrom math import *\nfrom random import *\nfrom statistics import *\nfrom itertools import *\nfrom functools import *\nfrom operator import *\nfrom io import *\nfrom sys import *\nfrom json import *\nfrom builtins import *\nfrom typing import *\nimport string\nimport re\nimport datetime\nimport collections\nimport heapq\nimport bisect\nimport copy\nimport math\nimport random\nimport statistics\nimport itertools\nimport functools\nimport operator\nimport io\nimport sys\nimport json\nsys.setrecursionlimit(50000)\n" | |
| def truncatefn(s, length=300): | |
| if isinstance(s, str): | |
| pass | |
| else: | |
| s = str(s) | |
| if len(s) <= length: | |
| return s | |
| return s[: length // 2] + "...(truncated) ..." + s[-length // 2 :] | |
| class CODE_TYPE(Enum): | |
| call_based = 0 | |
| standard_input = 1 | |
| # stuff for setting up signal timer | |
| class TimeoutException(Exception): | |
| pass | |
| def timeout_handler(signum, frame): | |
| print("timeout occured: alarm went off") | |
| raise TimeoutException | |
| # used to capture stdout as a list | |
| # from https://stackoverflow.com/a/16571630/6416660 | |
| # alternative use redirect_stdout() from contextlib | |
| class Capturing(list): | |
| def __enter__(self): | |
| self._stdout = sys.stdout | |
| sys.stdout = self._stringio = StringIO() | |
| # Make closing the StringIO a no-op | |
| self._stringio.close = lambda x: 1 | |
| return self | |
| def __exit__(self, *args): | |
| self.append(self._stringio.getvalue()) | |
| del self._stringio # free up some memory | |
| sys.stdout = self._stdout | |
| # Custom mock for sys.stdin that supports buffer attribute | |
| class MockStdinWithBuffer: | |
| def __init__(self, inputs: str): | |
| self.inputs = inputs | |
| self._stringio = StringIO(inputs) | |
| self.buffer = MockBuffer(inputs) | |
| def read(self, *args): | |
| return self.inputs | |
| def readline(self, *args): | |
| return self._stringio.readline(*args) | |
| def readlines(self, *args): | |
| return self.inputs.split("\n") | |
| def __getattr__(self, name): | |
| # Delegate other attributes to StringIO | |
| return getattr(self._stringio, name) | |
| class MockBuffer: | |
| def __init__(self, inputs: str): | |
| self.inputs = inputs.encode("utf-8") # Convert to bytes | |
| def read(self, *args): | |
| # Return as byte strings that can be split | |
| return self.inputs | |
| def readline(self, *args): | |
| return self.inputs.split(b"\n")[0] + b"\n" | |
| def clean_if_name(code: str) -> str: | |
| try: | |
| astree = ast.parse(code) | |
| last_block = astree.body[-1] | |
| if isinstance(last_block, ast.If): | |
| condition = last_block.test | |
| if ast.unparse(condition).strip() == "__name__ == '__main__'": | |
| code = ( | |
| ast.unparse(astree.body[:-1]) + "\n" + ast.unparse(last_block.body) # type: ignore | |
| ) | |
| except: | |
| pass | |
| return code | |
| def make_function(code: str) -> str: | |
| try: | |
| import_stmts = [] | |
| all_other_stmts = [] | |
| astree = ast.parse(code) | |
| for stmt in astree.body: | |
| if isinstance(stmt, (ast.Import, ast.ImportFrom)): | |
| import_stmts.append(stmt) | |
| else: | |
| all_other_stmts.append(stmt) | |
| function_ast = ast.FunctionDef( | |
| name="wrapped_function", | |
| args=ast.arguments( | |
| posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[] | |
| ), | |
| body=all_other_stmts, | |
| decorator_list=[], | |
| lineno=-1, | |
| ) | |
| main_code = ( | |
| import_string | |
| + "\n" | |
| + ast.unparse(import_stmts) # type: ignore | |
| + "\n" | |
| + ast.unparse(function_ast) # type: ignore | |
| ) | |
| return main_code | |
| except Exception as e: | |
| return code | |
| def call_method(method, inputs): | |
| if isinstance(inputs, list): | |
| inputs = "\n".join(inputs) | |
| inputs_line_iterator = iter(inputs.split("\n")) | |
| # Create custom stdin mock with buffer support | |
| mock_stdin = MockStdinWithBuffer(inputs) | |
| # sys.setrecursionlimit(10000) | |
| # @patch('builtins.input', side_effect=inputs.split("\n")) | |
| # Use our custom mock instead of StringIO | |
| # @patch('sys.stdout.write', print) | |
| def _inner_call_method(_method): | |
| try: | |
| return _method() | |
| except SystemExit as e: | |
| pass | |
| finally: | |
| pass | |
| return _inner_call_method(method) | |
| def get_function(compiled_sol, fn_name: str): # type: ignore | |
| try: | |
| assert hasattr(compiled_sol, fn_name) | |
| return getattr(compiled_sol, fn_name) | |
| except Exception as e: | |
| return | |
| def compile_code(code: str, timeout: int): | |
| signal.alarm(timeout) | |
| try: | |
| tmp_sol = ModuleType("tmp_sol", "") | |
| exec(code, tmp_sol.__dict__) | |
| if "class Solution" in code: | |
| # leetcode wraps solutions in `Solution` | |
| # this is a hack to check if it is leetcode solution or not | |
| # currently livecodebench only supports LeetCode but | |
| # else condition allows future extensibility to other platforms | |
| compiled_sol = tmp_sol.Solution() | |
| else: | |
| # do nothing in the other case since function is accesible | |
| compiled_sol = tmp_sol | |
| assert compiled_sol is not None | |
| finally: | |
| signal.alarm(0) | |
| return compiled_sol | |
| def convert_line_to_decimals(line: str) -> tuple[bool, list[Decimal]]: | |
| try: | |
| decimal_line = [Decimal(elem) for elem in line.split()] | |
| except: | |
| return False, [] | |
| return True, decimal_line | |
| def get_stripped_lines(val: str): | |
| ## you don't want empty lines to add empty list after splitlines! | |
| val = val.strip() | |
| return [val_line.strip() for val_line in val.split("\n")] | |
| def grade_call_based( | |
| code: str, all_inputs: list, all_outputs: list, fn_name: str, timeout: int | |
| ): | |
| # call-based clean up logic | |
| # need to wrap in try-catch logic after to catch the correct errors, but for now this is fine. | |
| code = import_string + "\n\n" + code | |
| compiled_sol = compile_code(code, timeout) | |
| if compiled_sol is None: | |
| return | |
| method = get_function(compiled_sol, fn_name) | |
| if method is None: | |
| return | |
| all_inputs = [ | |
| [json.loads(line) for line in inputs.split("\n")] for inputs in all_inputs | |
| ] | |
| all_outputs = [json.loads(output) for output in all_outputs] | |
| total_execution = 0 | |
| all_results = [] | |
| for idx, (gt_inp, gt_out) in enumerate(zip(all_inputs, all_outputs)): | |
| signal.alarm(timeout) | |
| faulthandler.enable() | |
| try: | |
| # can lock here so time is useful | |
| start = time.time() | |
| prediction = method(*gt_inp) | |
| total_execution += time.time() - start | |
| signal.alarm(0) | |
| # don't penalize model if it produces tuples instead of lists | |
| # ground truth sequences are not tuples | |
| if isinstance(prediction, tuple): | |
| prediction = list(prediction) | |
| tmp_result = prediction == gt_out | |
| # handle floating point comparisons | |
| all_results.append(tmp_result) | |
| if not tmp_result: | |
| return all_results, { | |
| "output": truncatefn(prediction), | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| "error_code": -2, | |
| "error_message": "Wrong Answer", | |
| } | |
| except Exception as e: | |
| signal.alarm(0) | |
| if "timeoutexception" in repr(e).lower(): | |
| all_results.append(-3) | |
| return all_results, { | |
| "error": repr(e), | |
| "error_code": -3, | |
| "error_message": "Time Limit Exceeded", | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| } | |
| else: | |
| all_results.append(-4) | |
| return all_results, { | |
| "error": repr(e), | |
| "error_code": -4, | |
| "error_message": "Runtime Error", | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| } | |
| finally: | |
| signal.alarm(0) | |
| faulthandler.disable() | |
| return all_results, {"execution time": total_execution} | |
| def grade_stdio( | |
| code: str, | |
| all_inputs: list, | |
| all_outputs: list, | |
| timeout: int, | |
| ): | |
| ## runtime doesn't interact well with __name__ == '__main__' | |
| code = clean_if_name(code) | |
| ## we wrap the given code inside another function | |
| code = make_function(code) | |
| compiled_sol = compile_code(code, timeout) | |
| if compiled_sol is None: | |
| return | |
| method = get_function(compiled_sol, "wrapped_function") | |
| if method is None: | |
| return | |
| all_results = [] | |
| total_execution_time = 0 | |
| for idx, (gt_inp, gt_out) in enumerate(zip(all_inputs, all_outputs)): | |
| signal.alarm(timeout) | |
| faulthandler.enable() | |
| signal.alarm(timeout) | |
| with Capturing() as captured_output: | |
| try: | |
| start = time.time() | |
| call_method(method, gt_inp) | |
| total_execution_time += time.time() - start | |
| # reset the alarm | |
| signal.alarm(0) | |
| except Exception as e: | |
| signal.alarm(0) | |
| if "timeoutexception" in repr(e).lower(): | |
| all_results.append(-3) | |
| return all_results, { | |
| "error": repr(e), | |
| "error_code": -3, | |
| "error_message": "Time Limit Exceeded", | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| } | |
| else: | |
| all_results.append(-4) | |
| return all_results, { | |
| "error": repr(e), | |
| "error_code": -4, | |
| "error_message": "Runtime Error", | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| } | |
| finally: | |
| signal.alarm(0) | |
| faulthandler.disable() | |
| prediction = captured_output[0] | |
| stripped_prediction_lines = get_stripped_lines(prediction) | |
| stripped_gt_out_lines = get_stripped_lines(gt_out) | |
| ## WA happens in multiple circumstances | |
| ## so cache the return to make it clean! | |
| WA_send_args = { | |
| "output": truncatefn(prediction), | |
| "inputs": truncatefn(gt_inp), | |
| "expected": truncatefn(gt_out), | |
| "error_code": -2, | |
| } | |
| if len(stripped_prediction_lines) != len(stripped_gt_out_lines): | |
| all_results.append(-2) | |
| WA_send_args["error_message"] = "Wrong answer: mismatched output length" | |
| return all_results, WA_send_args | |
| for output_line_idx, ( | |
| stripped_prediction_line, | |
| stripped_gt_out_line, | |
| ) in enumerate(zip(stripped_prediction_lines, stripped_gt_out_lines)): | |
| WA_send_args["error_message"] = ( | |
| f"Wrong answer at {output_line_idx=}: {truncatefn(stripped_prediction_line)} != {truncatefn(stripped_gt_out_line)}" | |
| ) | |
| ## CASE 1: exact match | |
| if stripped_prediction_line == stripped_gt_out_line: | |
| continue | |
| ## CASE 2: element-wise comparision | |
| ## if there are floating elements | |
| ## use `decimal` library for good floating point comparision | |
| ## otherwise gotcha: np.isclose(50000000000000000, 50000000000000001) = True | |
| ## note that we should always be able to convert to decimals | |
| success, decimal_prediction_line = convert_line_to_decimals( | |
| stripped_prediction_line | |
| ) | |
| if not success: | |
| all_results.append(-2) | |
| return all_results, WA_send_args | |
| success, decimal_gtout_line = convert_line_to_decimals(stripped_gt_out_line) | |
| if not success: | |
| all_results.append(-2) | |
| return all_results, WA_send_args | |
| if decimal_prediction_line == decimal_gtout_line: | |
| continue | |
| all_results.append(-2) | |
| return all_results, WA_send_args | |
| all_results.append(True) | |
| return all_results, {"execution time": total_execution_time} | |
| def run_test(sample, test=None, debug=False, timeout=6): | |
| """ | |
| if test(generated_code) is not None it'll try to run the code. | |
| otherwise it'll just return an input and output pair. | |
| """ | |
| signal.signal(signal.SIGALRM, timeout_handler) | |
| # Disable functionalities that can make destructive changes to the test. | |
| # max memory is set to 4GB | |
| reliability_guard() | |
| if debug: | |
| print(f"start = {datetime.now().time()}") | |
| try: | |
| in_outs = json.loads(sample["input_output"]) | |
| except ValueError as e: | |
| raise e | |
| in_outs = None | |
| if in_outs: | |
| if in_outs.get("fn_name") is None: | |
| which_type = CODE_TYPE.standard_input # Standard input | |
| method_name = None | |
| else: | |
| which_type = CODE_TYPE.call_based # Call-based | |
| method_name = in_outs["fn_name"] | |
| if debug: | |
| print(f"loaded input_output = {datetime.now().time()}") | |
| if test is None: | |
| assert False, "should not happen: test code is none" | |
| return in_outs, {"error": "no test code provided"} | |
| elif test is not None: | |
| results = [] | |
| sol = import_string | |
| if debug: | |
| print(f"loading test code = {datetime.now().time()}") | |
| if which_type == CODE_TYPE.call_based: | |
| signal.alarm(timeout) | |
| try: | |
| results, metadata = grade_call_based( | |
| code=test, | |
| all_inputs=in_outs["inputs"], | |
| all_outputs=in_outs["outputs"], | |
| fn_name=method_name, | |
| timeout=timeout, | |
| ) | |
| return results, metadata | |
| except Exception as e: | |
| return [-4], { | |
| "error_code": -4, | |
| "error_message": f"Error during testing: {e}", | |
| } | |
| finally: | |
| signal.alarm(0) | |
| elif which_type == CODE_TYPE.standard_input: | |
| # sol | |
| # if code has if __name__ == "__main__": then remove it | |
| signal.alarm(timeout) | |
| try: | |
| results, metadata = grade_stdio( | |
| code=test, | |
| all_inputs=in_outs["inputs"], | |
| all_outputs=in_outs["outputs"], | |
| timeout=timeout, | |
| ) | |
| return results, metadata | |
| except Exception as e: | |
| return [-4], { | |
| "error_code": -4, | |
| "error_message": f"Error during testing: {e}", | |
| } | |
| finally: | |
| signal.alarm(0) | |
| def reliability_guard(maximum_memory_bytes=None): | |
| """ | |
| This disables various destructive functions and prevents the generated code | |
| from interfering with the test (e.g. fork bomb, killing other processes, | |
| removing filesystem files, etc.) | |
| WARNING | |
| This function is NOT a security sandbox. Untrusted code, including, model- | |
| generated code, should not be blindly executed outside of one. See the | |
| Codex paper for more information about OpenAI's code sandbox, and proceed | |
| with caution. | |
| """ | |
| if maximum_memory_bytes is not None: | |
| import resource | |
| resource.setrlimit( | |
| resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes) | |
| ) | |
| resource.setrlimit( | |
| resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes) | |
| ) | |
| if not platform.uname().system == "Darwin": | |
| resource.setrlimit( | |
| resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes) | |
| ) | |
| faulthandler.disable() | |
| import builtins | |
| # builtins.exit = None | |
| builtins.quit = None | |
| import os | |
| os.environ["OMP_NUM_THREADS"] = "1" | |
| os.kill = None | |
| os.system = None | |
| os.putenv = None | |
| os.remove = None | |
| os.removedirs = None | |
| os.rmdir = None | |
| os.fchdir = None | |
| os.setuid = None | |
| os.fork = None | |
| os.forkpty = None | |
| os.killpg = None | |
| os.rename = None | |
| os.renames = None | |
| os.truncate = None | |
| os.replace = None | |
| os.unlink = None | |
| os.fchmod = None | |
| os.fchown = None | |
| os.chmod = None | |
| os.chown = None | |
| os.chroot = None | |
| os.fchdir = None | |
| os.lchflags = None | |
| os.lchmod = None | |
| os.lchown = None | |
| os.getcwd = None | |
| os.chdir = None | |
| import shutil | |
| shutil.rmtree = None | |
| shutil.move = None | |
| shutil.chown = None | |
| import subprocess | |
| subprocess.Popen = None # type: ignore | |
| # run as __main__ (Zevo), __builtins__ is the module, not its dict | |
| (__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)["help"] = None | |
| import sys | |
| sys.modules["ipdb"] = None | |
| sys.modules["joblib"] = None | |
| sys.modules["resource"] = None | |
| sys.modules["psutil"] = None | |
| sys.modules["tkinter"] = None | |
| # ---------------- Zevo entry point ---------------- | |
| import base64, csv, multiprocessing, pickle, zlib, os | |
| from concurrent.futures import ProcessPoolExecutor | |
| def extract_code(output: str) -> str: | |
| # LCB extract_code for chat/R1 styles: text between the last two ``` lines. | |
| lines = output.split("\n") | |
| idx = [i for i, line in enumerate(lines) if "```" in line] | |
| if len(idx) < 2: | |
| return "" | |
| return "\n".join(lines[idx[-2] + 1: idx[-1]]) | |
| def decode_tests(row): | |
| pub = json.loads(row["public_test_cases"]) | |
| priv = row["private_test_cases"] | |
| try: | |
| priv = json.loads(priv) | |
| except Exception: | |
| priv = json.loads(pickle.loads(zlib.decompress(base64.b64decode(priv.encode("utf-8"))))) | |
| meta = row["metadata"] if isinstance(row["metadata"], dict) else json.loads(row["metadata"] or "{}") | |
| tests = pub + priv | |
| return {"input_output": json.dumps({"inputs": [t["input"] for t in tests], | |
| "outputs": [t["output"] for t in tests], | |
| "fn_name": meta.get("func_name")})} | |
| def _run(sample, code, result, timeout): | |
| res, _ = run_test(sample, test=code, debug=False, timeout=timeout) | |
| result.append(res) | |
| def check(args): | |
| # LCB check_correctness: run in a child process with a global timeout. | |
| sample, code, timeout = args | |
| if not code.strip(): | |
| return False | |
| manager = multiprocessing.Manager() | |
| result = manager.list() | |
| p = multiprocessing.Process(target=_run, args=(sample, code, result, timeout)) | |
| p.start() | |
| n = len(json.loads(sample["input_output"])["inputs"]) | |
| p.join(timeout=(timeout + 1) * n + 5) | |
| if p.is_alive(): | |
| p.kill() | |
| if not result: | |
| return False | |
| res = result[0] | |
| return bool(res) and all(r is True or (not isinstance(r, bool) and r == 1) for r in res) | |
| def load_rows(path): | |
| ext = os.path.splitext(path)[1].lower() | |
| if ext == ".parquet": | |
| import pyarrow.parquet as pq | |
| return pq.read_table(path).to_pylist() | |
| if ext == ".jsonl": | |
| return [json.loads(l) for l in open(path, encoding="utf-8") if l.strip()] | |
| if ext == ".json": | |
| d = json.load(open(path, encoding="utf-8")) | |
| return d if isinstance(d, list) else d.get("data", d.get("rows")) | |
| csv.field_size_limit(sys.maxsize) | |
| return list(csv.DictReader(open(path, newline="", encoding="utf-8"))) | |
| def main(): | |
| pred_path, set_path, out_path = sys.argv[1:4] | |
| csv.field_size_limit(sys.maxsize) | |
| preds = {r["id"]: r.get("prediction") or "" for r in csv.DictReader(open(pred_path, newline="", encoding="utf-8"))} | |
| rows = load_rows(set_path) | |
| jobs = [(decode_tests(r), extract_code(preds.get(str(r["id"]), "")), 6) for r in rows] | |
| workers = int(os.environ.get("LCB_EVAL_WORKERS", min(16, os.cpu_count() or 1))) | |
| with ProcessPoolExecutor(max_workers=workers) as ex: | |
| ok = list(ex.map(check, jobs)) | |
| by = {} | |
| for r, k in zip(rows, ok): | |
| d = by.setdefault(r.get("difficulty", "all"), [0, 0]) | |
| d[0] += k; d[1] += 1 | |
| total = len(rows); correct = sum(ok) | |
| out = {"score": correct / total if total else 0.0, "correct": correct, "total": total, | |
| "missing_predictions": sum(str(r["id"]) not in preds for r in rows), | |
| "by_difficulty": {k: round(v[0] / v[1], 4) for k, v in by.items()}} | |
| json.dump(out, open(out_path, "w", encoding="utf-8")) | |
| print(json.dumps(out)) | |
| if __name__ == "__main__": | |
| main() | |