Paper commentary
Evaluating Large Language Models Trained on Code
- Problem Match-based scores such as BLEU do not reliably show whether generated Python code works, so the study asks whether a GPT model fine-tuned on public code can write functionally correct function bodies from docstrings.
- Method Codex fine-tunes GPT models on public GitHub Python files. Codex-S adds supervised fine-tuning on curated standalone functions, and candidate completions are ranked by mean log-probability or checked against unit tests.
- Results Codex-12B solves 28.8% of HumanEval problems with one sample, and Codex-S reaches 37.7% with the same budget. With 100 samples per problem, Codex-S produces at least one passing function for 77.5% of problems.
Why match-based scores fall short for code
Generative models for code are usually scored by how closely a sample matches a reference solution, either exactly or with a fuzzy measure such as BLEU. That scoring breaks down because a very large set of programs is functionally equivalent to a single reference, so a correct sample can look quite different from it. Ren et al. (2020) found that BLEU misses semantic features specific to code, which is one reason the authors move away from it.
The authors take functional correctness as the target instead: a sample counts as correct when it passes a set of unit tests. The choice rests on how developers judge code, since new code is usually accepted once it creates and passes unit tests.
Scoring samples with unit tests and pass@k
HumanEval contains 164 hand-written problems, and each one has a function signature, a docstring, a body and unit tests, averaging 7.7 tests per problem. Hand-writing the problems matters because the training corpus already contains many public solutions, including more than ten public repositories with solutions to Codeforces problems.
For each problem the authors draw n samples, count the c samples that pass the unit tests, and estimate pass@k with an unbiased estimator in which the probability that k draws all fail is subtracted from one. \[ \mathrm{pass@}k := \mathbb{E}_{\text{Problems}}\left[1-\frac{\binom{n-c}{k}}{\binom{n}{k}}\right] \] The evaluation uses n = 200 samples per task and k up to 100.
Evaluating this expression directly creates very large numbers and numerical instability, so the authors give a numpy version that multiplies the terms one at a time. Estimating pass@k as one minus the failure probability of a single sample raised to the power k is biased, which the authors show in an appendix.
Running untrusted programs in a sandbox
Generated programs are often incorrect and their intent is unknown, and GitHub is known to contain malicious programs, so running them carries a security risk. The sandbox is designed to stop programs from modifying the host, gaining persistence, reaching sensitive resources or leaking data from a host or network.
Fine-tuning GPT into Codex
The training corpus came from 54 million public software repositories on GitHub, collected in May 2020, and held 179 GB of unique Python files under 1 MB. Filters removed files that looked auto-generated, files with very long or very short lines and files with few alphanumeric characters, leaving 159 GB. Codex starts from GPT models rather than from random weights, since the authors expected natural-language prompts to benefit from GPT-3 representations.
The authors found no gain from starting out of a pre-trained language model on this large dataset, but models initialized from GPT converged faster, so they used that start throughout.
The tokenizer is the GPT-3 text tokenizer with extra tokens for runs of whitespace, because whitespace was the largest source of inefficiency for code; this reduces the token count by about 30%.
Each HumanEval problem is turned into a prompt containing a header, a signature and a docstring, and completions are sampled until one of the stop sequences appears. The stop sequences are \nclass, \ndef, \n#, \nif and \nprint, because otherwise the model keeps generating further functions or statements. Sampling uses nucleus sampling with top-p 0.95 for all sampling evaluations.
The example problems show how the prompt and a passing completion are arranged, and the single-sample pass probabilities differ widely across them.
Scaling and sample selection on HumanEval
With one sample per problem, GPT-12B solves no problems, while Codex solves 28.8% and Codex-S solves 37.7%. Among 100 samples per problem, Figure 1 reports 44.5% solved when the sample with the highest mean log-probability is chosen, and 77.5% solved when the sample that passes the unit tests is chosen. All samples in that figure are generated with temperature 0.8.
At k = 1, 10 and 100, Codex-12B reaches 28.81%, 46.81% and 72.31% in the authors' table, and the 6B GPT-J reached 11.62% at k = 1.
The temperature that gives the best pass@k depends on k: higher temperatures help when more samples are drawn, because the set of samples becomes more varied and the metric only needs one correct solution. For a 679M parameter model, the optimal temperature is 0.2 for pass@1 and 0.8 for pass@100. With those temperatures, pass@1 and pass@100 scale smoothly with model size, which Figure 6 plots.
When no unit tests are available, mean token log-probability is a better way to pick one sample than random choice, while sum log-probability can be slightly worse than random. Figure 7 shows the gain from these heuristics on samples at temperature 0.8 from Codex-12B, against an oracle that knows the unit tests as the upper bound.
Comparison with open models and Tabnine
GPT-Neo and GPT-J were trained on The Pile, which includes 8% GitHub code, and the authors measured them on HumanEval themselves. GPT-Neo reaches 6.4% pass@1 and 21.3% pass@100, while GPT models of comparable sizes stay near 0% on both metrics. In Table 1, GPT-Neo 2.7B reaches 6.41% at k = 1 and 21.37% at k = 100. The authors report GPT-Neo-2.7B as roughly equivalent to Codex-85M, a model with 30 times fewer parameters, and GPT-J-6B, at 11.6% pass@1 and 27.7% pass@100, as roughly equivalent to Codex-300M, with 20 times fewer parameters.
For GPT-Neo the best result comes from temperatures 0.2, 0.4 and 0.8, and for GPT-J from 0.2 and 0.8.
In Table 1, TabNine reaches 2.58% at k = 1 and 7.59% at k = 100.
Building standalone-function training problems
Python code on GitHub also includes class implementations, configuration files, scripts and data files, which the authors suspect widens the distribution away from docstring-to-function synthesis. To narrow that gap, the authors build training problems from correctly implemented standalone functions and fine-tune Codex on them, which produces Codex-S.
Programming contest and interview websites contributed 10,000 problems, each turned into a HumanEval-style task with the problem description as docstring. Unit tests came from examples in the statements or from extra cases found by submitting incorrect solutions, because complete test suites are often hidden.
A second source traces function calls with sys.setprofile during the integration tests of open source projects that use travis or tox, and also covers PyPI packages. Tracing records inputs and outputs of every invoked function, so builtin and library calls become problems too, and about 40,000 problems were collected from this source. Codex-12B generates 100 samples per curated problem, and problems with no passing sample are dropped as ambiguous or too difficult, with repeated checks removing stateful or non-deterministic cases.
Training minimizes the negative log-likelihood of the reference solution with prompt loss masked, at a learning rate one tenth of Codex's, until validation loss plateaus, for fewer than 10B tokens.
Codex-S against Codex
Codex-S reaches 37.7% pass@1 with one sample, and with 100 samples per problem it produces at least one passing function for 77.5% of HumanEval problems. Averaged across model sizes, Codex-S beats Codex by 6.5 percentage points at pass@1 and by 15.1 percentage points at pass@100. Choosing among 1 to 100 samples by mean log-probability gives Codex-S an average benefit over random ranking of 11.6 percentage points, which is over 2 percentage points more than the benefit for Codex.
Codex-S prefers slightly higher temperatures than Codex for every k above 1, which the authors link to its narrower distribution; they use temperature 0 for pass@1 and temperature 1 for pass@100.
APPS full-program results
APPS contains 5000 training and 5000 test problems, and most of its tests are full programs that read stdin and print to stdout, unlike Codex's main training data. The authors focus on strict accuracy and report pass@k, but they add two factors from competitive programming: filtered pass@k keeps only solutions that pass the 3 example tests in the statement, and time-outs are counted with a limit of 3 seconds.
Codex is not fine-tuned on APPS, so one input/output example is appended to the docstring as a formatting hint; this 1-shot setting gives Codex-12B results comparable to a GPT-Neo model fine-tuned on APPS.
On introductory problems, 1-shot Codex-12B reaches 4.14% raw pass@1, against 3.90% for the fine-tuned GPT-Neo 2.7B.
Docstring generation judged by hand
Codex-D is trained to minimize the negative log-likelihood of the docstring given the function signature and reference solution. Because no automatic check exists for docstrings, 10 samples were graded by hand for each of 1640 problems, at temperature 0.8, and a docstring counted as correct only when it uniquely and accurately specifies the body.
Docstrings that copy the code body do not count, and the common failures were leaving out important details or inventing unrelated problems from the function name.
Codex-D-12B reaches 20.3% pass@1 and 46.5% pass@10, against 32.2% and 59.5% for Codex-S-12B. The two rows come from different grading procedures: Codex-S is graded by unit tests and Codex-D by hand, so the gap is a comparison of the two protocols rather than of one controlled setting. Back-translation ranking, which scores a sample by the likelihood of the true docstring under Codex-D, beats random ranking but falls below mean log-probability ranking and seems to overfit quickly.
BLEU against functional correctness
BLEU was computed for all Codex-12B HumanEval samples at temperature 0.8 against their reference solutions. For each problem, the distributions of BLEU for correct and incorrect samples overlap considerably, as Figure 8 shows for four random tasks. Incorrect samples are functionally inequivalent to the reference, so the authors conclude that improving BLEU may not raise the rate of functional correctness in practice.
Functionally inequivalent programs that disagree with the reference on some input often have higher BLEU than equivalent ones.
Limits on chained and bound operations
Pass rate falls roughly by a factor of 2-3 with each added component, which Figure 11 plots for Codex-12B on synthetic docstrings built from chained components. The synthetic problems use 13 basic building blocks, each of which changes an input string in a deterministic way, such as converting it to lowercase. The authors argue that this decay is unlike a human programmer, who could handle a chain of arbitrary length after handling a chain of length two.
Codex also makes mistakes binding operations to variables when the docstring holds many operations and variables. In the authors' example, Codex-12B does not decrement the variable w and does not return the product of all numbers. Codex is also not sample efficient to train, since the training corpus covers hundreds of millions of lines, far more than a developer sees over a career.
Hazards of deploying generated code
Over-reliance is a central risk, because Codex can suggest code that looks right but does not do what the user intended, which could especially affect novice programmers. When the prompt contains subtle bugs, Codex tends to write worse code than it can produce, and this gap grows with model size, which the authors treat as an alignment failure.
The authors define misalignment as a case where a system could do a task but chooses not to, and expect it to persist or worsen as models scale.
Generated code that matches training data is rare: occurrences were fewer than 0.1% in a preliminary study, and these were mostly common idioms.
Paper info
- Authors
- Mark Chen, Jerry Tworek, Heewoo Jun, Qiming Yuan, Henrique Ponde de Oliveira Pinto, Jared Kaplan, Harri Edwards, Yuri Burda, Nicholas Joseph, Greg Brockman, Alex Ray, Raul Puri, Gretchen Krueger, Michael Petrov, Heidy Khlaaf, Girish Sastry, Pamela Mishkin, Brooke Chan, Scott Gray, Nick Ryder, Mikhail Pavlov, Alethea Power, Lukasz Kaiser, Mohammad Bavarian, Clemens Winter, Philippe Tillet, Felipe Petroski Such, Dave Cummings, Matthias Plappert, Fotios Chantzis, Elizabeth Barnes, Ariel Herbert-Voss, William Hebgen Guss, Alex Nichol, Alex Paino, Nikolas Tezak, Jie Tang, Igor Babuschkin, Suchir Balaji, Shantanu Jain, William Saunders, Christopher Hesse, Andrew N. Carr, Jan Leike, Josh Achiam, Vedant Misra, Evan Morikawa, Alec Radford, Matthew Knight, Miles Brundage, Mira Murati, Katie Mayer, Peter Welinder, Bob McGrew, Dario Amodei, Sam McCandlish, Ilya Sutskever, Wojciech Zaremba
- Venue
- arXiv · Preprint
- First published
- 2021-7-7
- Added
- 2026-10-20
- Updated
- 2021-7-14
- Version
- v2
- arXiv
- 2107.03374
- Category
- cs.LG
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Mark Chen, Jerry Tworek, Heewoo Jun, Qiming Yuan, Henrique Ponde de Oliveira Pinto, Jared Kaplan, Harri Edwards, Yuri Burda, Nicholas Joseph, Greg Brockman, Alex Ray, Raul Puri, Gretchen Krueger, Michael Petrov, Heidy Khlaaf, Girish Sastry, Pamela Mishkin, Brooke Chan, Scott Gray, Nick Ryder et al. (2021). Evaluating Large Language Models Trained on Code. arXiv:2107.03374v2. https://arxiv.org/abs/2107.03374v2
BibTeX
@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},
url = {https://arxiv.org/abs/2107.03374v2}
}