TL;DR
The paper introduces Codex, a version of GPT-3 that was further trained on public Python code from GitHub, and asks how well it can write a whole function from a docstring. To measure this, the authors wrote 164 new programming problems and checked each answer by running unit tests. A single sample from the largest model passes 28.8 percent of them, and sampling many times and counting any success pushes that much higher. They also gave an unbiased formula for the "pass@k" score, which became a standard way to report code models.
Builds on: Language Model, Transformer, Fine-Tuning, Pass@k · Leads to: Language Models are Few-Shot Learners (GPT-3)
Why it mattered
Before this paper, code models were judged by text-matching scores such as BLEU, which reward a program for looking like a reference solution even when it has a bug. The paper made functional correctness the test: a program counts only if it runs and passes unit tests. The HumanEval set, released with the paper, became a common benchmark for later code models, and the pass@k estimator described here is the one most people use to report results on it.
The paper also describes a production version of Codex that powered GitHub Copilot, the coding assistant that suggests code inside editors. So a research question about docstring-to-function generation turned into a product, which is one reason the paper is read for its method as well as its numbers.
The problem
By 2021 large language models could write fluent text, and GPT-3 could produce simple programs from docstrings, but nobody had a reliable way to say how often the output was right. The usual metrics compare the output with one reference string. Two correct programs can look nothing alike, and a program can look very close to the reference yet fail on an edge case. So the field lacked a way to ask: does this code do what the docstring says?
The data made this harder. A benchmark drawn from public problem sites would overlap with GitHub, where the model was trained, so the model could simply have memorised answers. The authors therefore wrote their own problems. A second issue was variance: scoring one sample per problem is noisy, and reporting "solved with k samples" by drawing k samples at random gives unstable numbers. The paper needed a metric that is both fair and stable.
How it works
The pipeline runs in one order: write problems, train the model, sample answers, run them, and score the results. Here is the path for one problem.
- Write the problem. The authors hand-write a function signature and a docstring that says what the function should do, plus several unit tests. On average each problem has 7.7 tests. The docstring is the prompt.
- Train the model. Codex starts from GPT-3 weights and is trained on about 179 GB of unique Python files collected from 54 million public GitHub repositories. Starting from GPT-3 converged faster, although the authors did not see a gain in final quality.
- Sample an answer. The model, an autoregressive model, reads the prompt and writes tokens one at a time. Sampling stops when it reaches a stop sequence such as a new
defor a new class, because otherwise it keeps writing extra functions. - Repeat sampling. For each problem the authors draw n = 200 samples. Each sample is a different possible program, because the sampling is random.
- Run the tests. Each program is executed in a sandbox against the unit tests. It counts as correct only if every test passes.
- Count and score. Let c be the number of samples that passed. The pass@k score for one problem is the chance that a random set of k samples contains at least one correct program, computed from c and n as described in "The math".
- Average over problems. The final number is the average over all 164 problems.
flowchart LR A["Docstring and signature"] --> B["Codex samples n = 200 programs"] B --> C["Sandbox runs each program on unit tests"] C --> D["Count correct samples c"] D --> E["Unbiased pass@k per problem"] E --> F["Average over 164 problems"]
The math
Let n be the number of samples per problem, c the number that pass, and k the number we would like to be able to pick. The paper defines the score for one problem as the chance that a random set of k samples, drawn from the n, contains at least one passing program:
Here is the number of ways to choose b items from a. The fraction is the chance that all k drawn samples fail. Subtracting it from 1 gives the chance that at least one passes. The overall score is the average of this over all problems.
Step 5 in symbols
If we take k = 1, the formula reduces to the fraction of samples that passed, because . So pass@1 is just c over n, which is the plain success rate.
Step 6 in symbols
Why not simply estimate pass@k as with the observed pass@1? The paper shows this is biased: it underestimates the true value, and the gap does not fully close even when n is more than five times k. The shortcut treats the k draws as if they were independent. They are not: they are drawn without replacement from a fixed pool of n samples, and the combinatorial formula above matches that sampling exactly, which is why it is unbiased.
Worked examples
A problem gets n = 5 samples, and c = 2 of them pass.
- The formula with k = 1 is .
- and .
- The fraction is 3 over 5, which is 0.6.
- Subtract from 1: 1 minus 0.6 is 0.4.
Interpretation: 2 of 5 passed, so the pass rate is 0.4, the same as c over n. This is the sanity check for the formula.
Same problem, n = 5, c = 2, but now k = 2.
- The unbiased formula: .
- and .
- The fraction is 3 over 10, which is 0.3.
- Subtract from 1: 0.7.
- The biased shortcut uses : .
Interpretation: the unbiased answer is 0.70 and the shortcut gives 0.64. The shortcut is too low, and the gap is exactly the kind of error the paper warns about. A paper that reports pass@k with the shortcut would look worse than it is, and comparisons across different values of n would be unfair.
The paper's results table gives Codex-12B scores of 28.81 percent at k = 1, 46.81 percent at k = 10 and 72.31 percent at k = 100. The Codex-300M model scores 13.17 percent at k = 1, and the GPT-J 6B model scores 11.62 percent at k = 1.
- Ratio of the two 1-sample scores: 28.81 divided by 13.17 is about 2.19. The 12B model is roughly twice as likely as the 300M model to produce a working program on one try, although it is about 40 times larger.
- Gain from more samples for Codex-12B: 72.31 minus 28.81 is 43.50 percentage points between k = 1 and k = 100.
- Compare with GPT-J: the 6B GPT-J model is only slightly below the 300M Codex on k = 1.
Interpretation: fine-tuning on code is worth more than size alone at this scale. Repeated sampling adds a large gain, but it is paid for with 100 times the sampling at test time, so a score at k = 100 does not mean one answer is reliable.
Try it: a problem gets n = 10 samples and c = 3 pass. Find pass@1 and pass@2.
Answer: pass@1 is 3 over 10, which is 0.3. For pass@2, compute , which is .
Results
The main result is for a single sample. Codex-12B solves 28.81 percent of the problems, against no problems for the GPT-12B baseline in the paper's Figure 1 and 11.62 percent for GPT-J 6B in its results table.
The scores rise with size on a smooth curve. Codex-85M gets 8.22 percent at k = 1, Codex-300M 13.17, Codex-679M 16.22, Codex-2.5B 21.36 and Codex-12B 28.81. Fine-tuning a further model on standalone, correctly implemented functions (Codex-S) raised the one-sample score to 37.7 percent, as stated in the paper's introduction.
Repeated sampling changes the picture. At k = 10 the 12B model reaches 46.81 percent, and at k = 100 it reaches 72.31 percent. The authors chose the sampling temperature separately for each k, using about 0.2 for k = 1 and about 0.8 for k = 100 on the 679M model, because a lower temperature gives more reliable single samples and a higher one gives more variety across many samples.
The limits matter as much as the numbers. The benchmark has only 164 problems, so one problem is worth about 0.6 percentage points, and small gaps between models may be noise. The paper also reports that performance falls off quickly as a docstring chains more operations: pass rate drops by roughly a factor of two to three for each added component in its synthetic test.
Reading guide
The paper runs in this order, with the prerequisite lessons that each part needs.
- Section 1, Introduction. Needs Language Model and Language Models are Few-Shot Learners (GPT-3) for the idea that a model trained on text can be asked to write code.
- Section 2, Evaluation framework. The pass@k definition and the unbiased estimator (Appendix A) need Pass@k and the binomial coefficients that the formula uses. The sandbox subsection needs nothing beyond the idea of running untrusted code.
- Section 3, Code fine-tuning. The training recipe (Adam, warmup, cosine decay, weight decay) needs Fine-Tuning, Transformer and Adam Optimizer. The tokenizer subsection needs the idea of a token from Language Model.
- Section 3.3 to 3.5, Results. Read these with Table 1 and the scaling curves; the reasoning about temperature uses Softmax.
- Section 4 and 5, Supervised fine-tuning and docstring generation. These extend the method; read them after the main results.
- Section 6, Limitations. Read with Overfitting in mind when you think about what a benchmark can and cannot show.
Check yourself
QuestionRecall: what does pass@k measure in words?Show answer
pass@k is the chance that at least one of k samples drawn from the n generated for a problem passes the unit tests. Averaged over problems, it is the paper's score for a model's coding success when it is allowed k tries.
QuestionCompute: a problem has n = 4 samples and c = 1 passes. What is pass@1, and what is pass@2?Show answer
pass@1 is c over n, which is 1 over 4, or 0.25. For pass@2, the formula gives . Check: there are 6 pairs of samples, and the passing sample appears in 3 of them, so half the pairs contain a correct program.
QuestionWhy do the authors use hand-written problems instead of a public benchmark?Show answer
Codex was trained on a large fraction of public GitHub code, which already contains solutions to many public programming problems. A public benchmark could therefore reward memorisation rather than the ability to write a function from a new description. Hand-written problems make the test a fairer measure of generalisation.
QuestionScenario: a team reports that its code model reaches 60 percent pass@100 on HumanEval-style tests. A user says the model is reliable. What would you tell them?Show answer
The 60 percent is the chance that at least one of 100 samples is correct, so a user who reads one answer has a much lower chance of getting a working program. The score needs the sample count and the temperature to interpret, and pass@1 shows single-try reliability. The honest report gives both numbers, and the user should expect to run tests on the output.
Sources
- Chen et al., Evaluating Large Language Models Trained on Code, arXiv:2107.03374: https://arxiv.org/abs/2107.03374
- GitHub Copilot, Wikipedia: https://en.wikipedia.org/wiki/GitHub_Copilot