-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathhumaneval.py
85 lines (70 loc) · 3.85 KB
/
humaneval.py
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
"""Evaluating Large Language Models Trained on Code
https://arxiv.org/abs/2107.03374
The HumanEval dataset released by OpenAI includes 164 programming problems with a function signature,
docstring, body, and several unit tests.
They were handwritten to ensure not to be included in the training set of code generation models.
Homepage: https://github.com/openai/human-eval
"""
import re
from evaluate import load
from lm_eval.base import Task
_CITATION = """
@misc{chen2021evaluating,
title={Evaluating Large Language Models Trained on Code},
author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser and Mohammad Bavarian and Clemens Winter and Philippe Tillet and Felipe Petroski Such and Dave Cummings and Matthias Plappert and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain and William Saunders and Christopher Hesse and Andrew N. Carr and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},
year={2021},
eprint={2107.03374},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
"""
class HumanEval(Task):
"""A task represents an entire benchmark including its dataset, problems,
answers, generation settings and evaluation methods.
"""
DATASET_PATH = "openai_humaneval"
def __init__(self):
super().__init__(
stop_words=["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"],
requires_execution=True,
)
def get_dataset(self):
"""Returns dataset for the task or an iterable of any object, that get_prompt can handle"""
return self.dataset["test"]
def get_prompt(self, doc):
"""Builds the prompt for the LM to generate from."""
return doc["prompt"].strip()
def get_reference(self, doc):
"""Builds the reference solution for the doc (sample from the test dataset)."""
test_func = doc["test"]
entry_point = f"check({doc['entry_point']})"
return "\n" + test_func + "\n" + entry_point
@staticmethod
def remove_last_block(string, stop_words):
# Remove the last block of the code containing stop_words for HumanEval
string_list = re.split("(%s)" % "|".join(stop_words), string)
# last string should be ""
return "".join(string_list[:-2])
def postprocess_generation(self, generation, idx):
"""Defines the postprocessing for a LM generation.
:param generation: str
code generation from LM
:param idx: int
index of doc in the dataset to which the generation belongs
(not used for Humaneval-Task)
"""
return self.remove_last_block(generation, self.stop_words)
def process_results(self, generations, references):
"""Takes the list of LM generations and evaluates them against ground truth references,
returning the metric for the generations.
:param generations: list(list(str))
list of lists containing generations
:param references: list(str)
list of str containing refrences
"""
code_metric = load("code_eval")
results, _ = code_metric.compute(
references=references,
predictions=generations,
)
return results