File size: 8,092 Bytes
6bd9122 9997d13 6bd9122 9997d13 2ed1c06 6bd9122 9997d13 6bd9122 85ee43a 9997d13 6bd9122 9997d13 6bd9122 9997d13 6bd9122 9997d13 6bd9122 02ebdd5 6bd9122 9997d13 6bd9122 9997d13 2ed1c06 9997d13 75ece7c 6bd9122 c503e24 6bd9122 2ed1c06 6bd9122 85ee43a c503e24 85ee43a 6bd9122 2ed1c06 6bd9122 9fdbbcb 2ed1c06 6bd9122 9fdbbcb 2ed1c06 6bd9122 9fdbbcb 2ed1c06 6bd9122 |
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 |
# Heavily adapted from `Muennighoff/code_eval_octopack`
"""TODO: Add a description here."""
import collections
import os
from typing import Literal
import concurrent.futures
import datasets
import evaluate
import itertools
import numpy as np
from tqdm import tqdm
from .execute import check_correctness
# TODO: Add BibTeX citation
_CITATION = """\
@InProceedings{huggingface:module,
title = {A great new module},
authors={huggingface, Inc.},
year={2020}
}
"""
# TODO: Add description of the module here
_DESCRIPTION = """\
This new module is designed to solve this great ML task and is crafted with a lot of care.
"""
# TODO: Add description of the arguments of the module here
_KWARGS_DESCRIPTION = """
Calculates how good are predictions given some references, using certain scores
Args:
predictions: list of predictions to score. Each predictions
should be a string with tokens separated by spaces.
references: list of reference for each prediction. Each
reference should be a string with tokens separated by spaces.
Returns:
accuracy: description of the first score,
another_score: description of the second score,
Examples:
Examples should be written in doctest format, and should illustrate how
to use the function.
>>> my_new_module = evaluate.load("my_new_module")
>>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
>>> print(results)
{'accuracy': 1.0}
"""
_WARNING = """
################################################################################
!!!WARNING!!!
################################################################################
The "code_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"
################################################################################\
"""
_CLANG_WARNING = """
Please provide the environment variable 'GENERICIFY_CLANG' with the path of the
clang++ compiler. Version 15+ is required. Within Python you can to this
with:
>>> import os
>>> os.environ["GENERICIFY_CLANG"] = "/path/to/clang++"
"""
# TODO: Define external resources urls if needed
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
@evaluate.utils.file_utils.add_start_docstrings(
_DESCRIPTION, _KWARGS_DESCRIPTION
)
class EvaluateGenericifyCpp(evaluate.Metric):
"""TODO: Short description of my evaluation module."""
def _info(self):
# TODO: Specifies the evaluate.EvaluationModuleInfo object
return evaluate.MetricInfo(
# This is the description that will appear on the modules 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")),
"references": datasets.Features(
{
"tests": datasets.Value("string"),
"invalids": datasets.Value("string"),
}
),
}
),
# Homepage of the module for documentation
homepage="http://module.homepage",
# Additional links to the codebase or references
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
reference_urls=["http://path.to.reference.url/new_module"],
)
def _compute(
self,
*,
predictions,
references,
cpp_type: Literal["base", "sfinae", "concepts"],
k=[1, 10, 100],
):
"""Returns the scores"""
num_workers = os.cpu_count()
num_workers = num_workers if not num_workers else 8
if os.getenv("HF_ALLOW_CODE_EVAL", default=0) != "1":
raise ValueError(_WARNING)
if os.getenv("GENERICIFY_CLANG", default=0) == 0:
raise ValueError(_CLANG_WARNING)
if os.name == "nt":
raise NotImplementedError(
"This metric is currently not supported on Windows."
)
total_predictions = sum(map(len, predictions))
with concurrent.futures.ThreadPoolExecutor(
max_workers=num_workers
) as executor:
futures = []
completion_id = collections.Counter()
results = collections.defaultdict(list)
for task_id, (candidates, reference) in enumerate(
zip(predictions, references)
):
for candidate in candidates:
args = (
candidate,
reference,
cpp_type,
task_id,
completion_id[task_id],
)
future = executor.submit(check_correctness, *args)
futures.append(future)
completion_id[task_id] += 1
for future in tqdm(
concurrent.futures.as_completed(futures),
desc="Evaluating",
total=total_predictions,
):
result = future.result()
results[result["task_id"]].append(
(result["completion_id"], result)
)
totals = collections.defaultdict(list)
corrects = collections.defaultdict(list)
keys = {
"base": [
"base_run_passed",
"base_run_compiled",
],
"sfinae": [
"sfinae_run_passed",
"sfinae_run_compiled",
"sfinae_constrain_passed",
],
"concepts": [
"concepts_run_passed",
"concepts_run_compiled",
"concepts_constrain_passed",
],
}[cpp_type]
for result in results.values():
result.sort()
for pt in keys:
passed = [r[1][pt] for r in result]
totals[pt].append(len(passed))
corrects[pt].append(sum(passed))
totals = {k: np.array(v) for k, v in totals.items()}
corrects = {k: np.array(v) for k, v in corrects.items()}
ks = k
pass_at_k = {
f"{key}@{k}": estimate_pass_at_k(
totals[key],
corrects[key],
k,
).mean()
for key in totals.keys()
for k in ks
if (totals[key] >= k).all()
}
return pass_at_k, results
def estimate_pass_at_k(num_samples, num_correct, k) -> np.array:
"""Estimates pass@k of each problem and returns them in an array."""
def estimator(n: int, c: 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)) for n, c in zip(num_samples_it, num_correct)]
)
|