# HumanEval: Decoding the LLM Benchmark for Code Generation

Zian (Andy) Wang

In the era of artificial intelligence and machine learning, evaluating the performance of models is crucial for their development and improvement. Large Language Models (LLMs) have shown incredible capabilities in generating human-like text, and their application has been extended to code generation. However, evaluating the quality of the generated code presents a unique set of challenges. Traditional metrics like BLEU score, which measures text similarity, are only sometimes suitable for assessing the functional correctness of the code, an aspect that is paramount for any programming task.

Enter the HumanEval dataset and the pass@k metric. This hand-crafted dataset, consisting of 164 programming challenges, and the novel evaluation metric, designed to assess the functional correctness of the generated code, have revolutionized how we measure the performance of LLMs in code generation tasks. This article delves into the intricacies of the HumanEval dataset, the limitations of traditional evaluation methods, the workings of the pass@k metric, and the implications of this novel approach on the ongoing development of code generation models.

## The HumanEval Dataset

"HumanEval" refers to a hand-crafted dataset comprising 164 programming challenges. According to the paper, each problem includes "a function signature, docstring, body, and several unit tests, with an average of 7.7 tests per problem". The dataset was meticulously crafted to prevent data leakage, as the Codex model and many more large language models released later contain training data from websites like GitHub.

## Evaluating Generated Code

Before introducing the immensely popular HumanEval benchmark, most evaluation methods for generated code involved comparing the produced solution with the ground-truth code. The "correctness" is usually quantified using the BLEU score or any other metric that measures the similarity between different sets of texts.

However, evaluating text similarity differs significantly from judging whether a piece of code can solve a given problem. In complex problem settings, the presented solution may deviate entirely from the sample solution from a "text similarity" perspective but be functionally correct. Human programmers tend to use test-driven development for evaluating written code. The program can be considered "correct" if it can pass certain unit tests.

## The Pass@k Metric

To address the limitations of traditional text similarity metrics, the paper introduced the pass@k metric, designed to evaluate the functional correctness of generated code samples. The pass@k metric is defined as the probability that at least one of the top k-generated code samples for a problem passes the unit tests. This approach is inspired by the practices of human developers, who judge the correctness of code based on whether it passes a set of unit tests.

The formula for pass@k as derived from basic principles of probability. Let's break it down step by step.

The goal is to estimate the probability that at least one of the top k samples is correct, given that there are c-correct samples in total out of n-generated samples.

The total number of ways to choose k samples out of n is given by the combination formula "n choose k", denoted as C(n, k).

Similarly, the total number of ways to choose k samples out of the n-c incorrect samples is given by C(n−c, k).

So, the probability that all k samples chosen are incorrect is given by:

`C(n−c,k)/C(n,k)`

Therefore, the probability that at least one of the k samples chosen is correct is the complement of the above probability, which is:

`1−(C(n−c,k)/C(n,k))`

This is precisely the formula used to calculate pass@k, with the expectation taken over all problems:

Here, the fancy "E" denotes the expected value over the problems, n is the total number of samples, c is the number of correct samples, and k is the number of top samples consideredThe authors also provide a numerically stable Python implementation of the formula:

```
```
def pass_at_k(n, c, k):
"""
:param n: total number of samples
:param c: number of correct samples
:param k: k in pass@$k$
"""
if n - c < k:
return 1.0
return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1)
```
```

The if statement at the beginning of the function handles the edge case where the number of incorrect samples (n-c) is less than k. In this case, it is inevitable that at least one of the k samples chosen will be correct, so the function returns 1.0.

Otherwise, the function calculates the pass@k using the np.prod function from the NumPy library. The expression 1.0 - k / np.arange(n - c + 1, n + 1) creates an array of values that, when multiplied together, give the same result as the combination formula C(n−c,k)/C(n,k), but in a numerically stable way. This is important because the values of n, c, and k can be very large, and calculating the combinations directly can result in numerical instability or overflow.

By focusing on functional correctness rather than text similarity, the pass@k metric offers a more meaningful and practical assessment of a model's ability to solve programming challenges. This approach aligns more closely with the practices of human developers and provides a valuable benchmark for the ongoing development of code generation models.

## Implications

Since its inception in mid-2021, the HumanEval benchmark has not only become immensely popular but has also emerged as a quintessential evaluation tool for measuring the performance of LLMs in code generation tasks.

The [leaderboard](https://paperswithcode.com/sota/code-generation-on-humaneval) hosted by Papers with Code has become a competitive battleground for various models, ranked by the innovative pass@, pass@10, and pass@100 metrics. This shift from traditional text similarity measurements to functional correctness evaluations has been pivotal in developing LLMs and human-assisting AIs.

As LLMs continue to evolve and their capabilities expand, it is imperative to assess their performance based on their ability to solve problems efficiently and accurately rather than just mimicking human-generated solutions.

Ultimately, AI aims to augment human capabilities and provide innovative and efficient solutions to problems. The HumanEval benchmark and the pass@k metric are significant strides towards achieving this goal by providing a more meaningful and practical assessment of a model's ability to solve programming challenges. This approach aligns more closely with the practices of human developers and sets a valuable benchmark for the ongoing development of code generation models, thereby contributing to the evolution of AI as a tool that genuinely complements human intelligence.