Spaces:
Runtime error
Runtime error
File size: 11,654 Bytes
1359055 6ac8164 1359055 419ab80 1359055 c2da054 1359055 ca7f85b 1359055 419ab80 1359055 419ab80 1359055 0a274eb 1359055 0a274eb 1359055 419ab80 1359055 fb86293 419ab80 1359055 419ab80 1359055 2071b6f 1359055 2071b6f 1359055 419ab80 1359055 419ab80 1359055 419ab80 1359055 bf34e9e 1359055 419ab80 1359055 419ab80 1359055 419ab80 1359055 419ab80 1359055 419ab80 84e44d7 419ab80 1359055 419ab80 1359055 419ab80 1359055 1b70784 |
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 |
import dataclasses
import itertools
import os
import re
import tempfile
from collections import defaultdict
from pathlib import Path
import datasets
import evaluate
import numpy as np
from tqdm import tqdm
from .execution import execute_predictions
STDOUT_PARSE_REGEX = re.compile(r"^TEST-(.+)\.\.\.(.+)$", flags=re.MULTILINE)
_CITATION = """\
@article{orlanski2023measuring,
title={Measuring The Impact Of Programming Language Distribution},
author={Orlanski, Gabriel and Xiao, Kefan and Garcia, Xavier and Hui, Jeffrey and Howland, Joshua and Malmaud, Jonathan and Austin, Jacob and Singh, Rishah and Catasta, Michele},
journal={arXiv preprint arXiv:2302.01973},
year={2023}
}
"""
_DESCRIPTION = """\
This metric implements the evaluation harness for datasets translated with the BabelCode framework as described in the paper "Measuring The Impact Of Programming Language Distribution" (https://arxiv.org/abs/2302.01973).
"""
_KWARGS_DESCRIPTION = """
Calculates how many predictions per question pass a set of tests for the given problem.
Args:
predictions: The list of predictions for each question to execute.
languages: The language to use for each question.
question_dicts: The information for each question.
k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])
num_workers: number of workers used to evaluate the candidate programs (Default: 4).
language_timeout: Timeouts to use for each language. If it is not set, will default to the one in the question dict (Default: None).
Returns:
pass_at_k: dict with pass rates for each k
results: dict with granular results of each unittest
Examples:
>>> bc_eval = evaluate.load("bc_eval")
>>> predictions = [["def add(a,b):\n\treturn a+b", "def add(a,b):\n\treturn a-b"]]
>>> languages = ["Python"]
>>> question_dicts = [{"test_code": "...", "entry_fn_name": "add","entry_cls_name":"Solution", "test_case_ids":["0","1"],"test_list":"..."}]
>>> pass_at_k, results = code_eval.compute(predictions=predictions,languages=languages, question_dicts=question_dicts, k=[1, 2])
>>> print(pass_at_k)
{'pass@1': 0.5, 'pass@2': 1.0}
"""
_WARNING = """
################################################################################
!!!WARNING!!!
################################################################################
The "bc_eval" metric executes untrusted model-generated code in Python.
Although it is highly unlikely that model-generated code will do something
overtly malicious in response to this test suite, model-generated code may act
destructively due to a lack of model capability or alignment.
Users are strongly encouraged to sandbox this evaluation suite so that it
does not perform destructive actions on their host or network. For more
information on how OpenAI sandboxes its code, see the paper "Evaluating Large
Language Models Trained on Code" (https://arxiv.org/abs/2107.03374).
Once you have read this disclaimer and taken appropriate precautions,
set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this
with:
>>> import os
>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"
################################################################################\
"""
_QUESTION_INFO_KEYS = {
"entry_fn_name",
"entry_cls_name",
"test_code",
"test_list",
"test_case_ids",
"extension"
}
def make_file_and_command(qid, idx, pred, question, working_dir, timeout_override=None):
file_name = f"pred.{question['extension']}"
pred_dir = working_dir.joinpath(f'{qid}_{idx}')
pred_dir.mkdir(parents=True)
pred_file = pred_dir.joinpath(file_name)
with pred_file.open("w",encoding='utf-8') as f:
code = question["test_code"]
code = question["test_code"].replace("PLACEHOLDER_CODE_BODY", pred)
code = code.replace("PLACEHOLDER_FN_NAME", question["entry_fn_name"])
code = code.replace("PLACEHOLDER_CLS_NAME", question["entry_cls_name"])
f.write(code)
commands = []
for cmd, t in zip(question["commands"], question["timeouts"]):
commands.append(
{
"timeout": t if timeout_override is None else timeout_override,
"command": [c if c != "__FILENAME__" else file_name for c in cmd],
}
)
return {"qid": qid, "idx": idx, "commands": commands, "cwd": pred_dir}
def _write_preds(
preds,
languages,
language_timeout,
question_dicts,
tmp_dir,
):
commands = []
question_id_to_dict = {}
for pred_list, l, q_dict in tqdm(
zip(preds, languages, question_dicts), desc="Setup", total=len(preds)
):
qid = len(question_id_to_dict)
q_dict["language"] = l
question_id_to_dict[qid] = q_dict
for p_idx, p in enumerate(pred_list):
commands.append(
make_file_and_command(
qid=qid,
idx=str(p_idx),
pred=p,
question=q_dict,
timeout_override=language_timeout.get(l),
working_dir=tmp_dir,
)
)
return question_id_to_dict, commands
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class BabelCodeEval(evaluate.Metric):
def _info(self):
list_keys = ["timeouts", "commands", "test_case_ids"]
question_info_type = {
k: datasets.Value(dtype="string")
for k in _QUESTION_INFO_KEYS
if k not in list_keys
}
question_info_type["test_case_ids"] = datasets.Sequence(datasets.Value("string"))
question_info_type["commands"] = datasets.Sequence(datasets.Sequence(datasets.Value("string")))
question_info_type["timeouts"] = datasets.Sequence(datasets.Value("int32"))
return evaluate.MetricInfo(
# This is the description that will appear on the metrics page.
description=_DESCRIPTION,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION,
# This defines the format of each prediction and reference
features=datasets.Features(
{
"predictions": datasets.Sequence(datasets.Value("string")),
"languages": datasets.Value("string"),
"question_dicts": question_info_type,
}
),
homepage="https://github.com/google-research/babelcode",
codebase_urls=["https://github.com/google-research/babelcode"],
reference_urls=["https://github.com/google-research/babelcode"],
)
def _compute(
self,
predictions,
languages,
question_dicts,
k=[1, 10, 100],
num_workers=4,
language_timeout=None,
max_task_per_child=10,
gc_freq=500
):
"""Returns the scores"""
if os.getenv("HF_ALLOW_CODE_EVAL", 0) != "1":
raise ValueError(_WARNING)
language_timeout = language_timeout or {}
with tempfile.TemporaryDirectory() as tmp_dir:
working_dir = Path(tmp_dir)
question_map, pred_commands = _write_preds(
preds=predictions,
languages=languages,
language_timeout=language_timeout,
question_dicts=question_dicts,
tmp_dir=working_dir,
)
results = execute_predictions(
pred_commands,
num_workers=num_workers,
max_task_per_child=max_task_per_child,
garbage_collection_freq=gc_freq,
)
all_results, q_passes, q_pct = _eval_predictions(results, question_map)
assert len(q_passes) == len(q_pct)
metrics = {}
for lang in q_passes:
metrics.update(
_calculate_metrics(lang, q_passes[lang], q_pct[lang], k_vals=k)
)
return metrics, all_results
def _eval_single_pred(result, test_ids, num_expected_commands):
test_case_results = {k: "MISSING" for k in test_ids}
if len(result["results"]) != num_expected_commands:
return "HAD_ERROR", 0, test_case_results
last_result = result["results"][-1]
if last_result.timed_out:
return "TIMED_OUT", 0, test_case_results
elif last_result.return_code != 0:
return "HAD_ERROR", 0, test_case_results
elif not last_result.stdout:
return "HAD_ERROR", 0, test_case_results
for match in STDOUT_PARSE_REGEX.findall(last_result.stdout):
idx, test_result = match
if idx in test_ids:
if test_case_results[idx] != "MISSING":
return "UNKNOWN_ERROR", 0, test_case_results
test_case_results[idx] = test_result.strip()
did_test_fail = False
had_error = False
num_passed = 0
for r in test_case_results.values():
if r == "PASSED":
num_passed += 1
elif r == "FAILED":
did_test_fail = True
else:
had_error = True
if had_error:
return "HAD_RUNTIME_ERROR", num_passed, test_case_results
if did_test_fail:
return "FAILED", num_passed, test_case_results
return "PASSED", num_passed, test_case_results
def _eval_predictions(pred_results, question_map):
out = []
question_results = defaultdict(lambda: defaultdict(list))
question_pct_pass = defaultdict(lambda: defaultdict(list))
for p in pred_results:
question = question_map[p["qid"]]
test_cases = question["test_case_ids"]
num_expected_commands = len(question["commands"])
outcome, num_passed, test_case_results = _eval_single_pred(
p, test_ids=test_cases, num_expected_commands=num_expected_commands
)
p["results"] = [dataclasses.asdict(r) for r in p["results"]]
p["test_cases"] = test_case_results
p["outcome"] = outcome
lang = question["language"]
question_results[lang][p["qid"]].append(num_passed == len(test_case_results))
question_pct_pass[lang][p["qid"]].append(num_passed / len(test_case_results))
out.append(p)
return out, question_results, question_pct_pass
def _calculate_metrics(lang, q_passed, q_pcts, k_vals):
assert len(q_passed) == len(q_pcts)
num_samples = np.zeros(len(q_passed))
num_correct = np.zeros(len(q_passed))
pcts_passed = np.zeros(len(q_passed))
for i, (k, v) in enumerate(q_passed.items()):
num_samples[i] = len(v)
num_correct[i] = sum(v)
pcts_passed[i] = np.mean(q_pcts[k])
out = {
f"{lang}/pass@{k}": estimate_pass_at_k(num_samples, num_correct, k).mean()
for k in k_vals
}
out[f"{lang}/mean_pct_pass"] = np.mean(pcts_passed)
out[f"{lang}/median_pct_pass"] = np.median(pcts_passed)
return out
def estimate_pass_at_k(num_samples, num_correct, k):
"""Estimates pass@k of each problem and returns them in an array."""
def estimator(n: int, c: int, k: int) -> float:
"""Calculates 1 - comb(n - c, k) / comb(n, k)."""
if n - c < k:
return 1.0
return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1))
if isinstance(num_samples, int):
num_samples_it = itertools.repeat(num_samples, len(num_correct))
else:
assert len(num_samples) == len(num_correct)
num_samples_it = iter(num_samples)
return np.array(
[estimator(int(n), int(c), k) for n, c in zip(num_samples_it, num_correct)]
)
|