|
| 1 | +# Using the HumanEval accuracy test tools |
| 2 | + |
| 3 | +The HumanEval benchmark is a code generation and functional correctness evaluation framework designed to assess language models' ability to generate Python code. It consists of 164 handwritten programming problems, each containing a function signature, docstring, body, and several unit tests. This benchmark focuses on evaluating a model's capability to generate functionally correct code that passes the test cases, making it particularly useful for assessing code generation capabilities. |
| 4 | + |
| 5 | +This tool provides an automated way to evaluate language models on the HumanEval benchmark. It handles the process of downloading the dataset, generating code completions, executing them in a secure environment, and calculating pass@k metrics. |
| 6 | + |
| 7 | +## Dataset |
| 8 | + |
| 9 | +The HumanEval dataset is automatically downloaded from [OpenAI's human-eval repository](https://github.com/openai/human-eval) when you first run the benchmark. The dataset contains programming problems that test various aspects of Python programming, including: |
| 10 | + |
| 11 | +- Basic programming operations |
| 12 | +- String manipulation |
| 13 | +- Mathematical computations |
| 14 | +- List operations |
| 15 | +- Algorithm implementation |
| 16 | +- Data structure manipulation |
| 17 | + |
| 18 | +## Running the Benchmark |
| 19 | + |
| 20 | +```bash |
| 21 | +lemonade -i meta-llama/Llama-3.2-1B oga-load --device igpu --dtype int4 accuracy-humaneval --k-samples 1 --first-n-samples 5 --timeout 30.0 |
| 22 | +``` |
| 23 | + |
| 24 | +### Optional arguments: |
| 25 | + |
| 26 | +`--k-samples`: Number of completions to generate per prompt (default: 1). This parameter determines the k in pass@k metrics. For example: |
| 27 | +- `--k-samples 1`: Calculates pass@1 (single attempt per problem) |
| 28 | +- `--k-samples 10`: Calculates pass@10 (ten attempts per problem) |
| 29 | +- `--k-samples 100`: Calculates pass@100 (hundred attempts per problem) |
| 30 | + |
| 31 | +Higher k values provide more robust evaluation but take longer to run. |
| 32 | + |
| 33 | +`--first-n-samples`: Evaluate only the first N problems from the dataset (default: entire dataset). Useful for quick testing or when you want to evaluate a subset of problems. |
| 34 | + |
| 35 | +`--timeout`: Maximum time in seconds allowed for each test case execution (default: 30.0). This prevents infinite loops or long-running code from blocking the evaluation. |
| 36 | + |
| 37 | +`--data-dir`: Custom directory for storing the HumanEval dataset (default: "<lemonade_cache_dir>/data/humaneval"). |
| 38 | + |
| 39 | +## How It Works |
| 40 | + |
| 41 | +1. **Dataset Preparation:** |
| 42 | + - On first run, the tool downloads the HumanEval dataset (HumanEval.jsonl.gz) |
| 43 | + - The dataset contains function signatures, docstrings, and test cases |
| 44 | + - Each problem is structured to test specific programming capabilities |
| 45 | + - You can evaluate only the first N problems using `--first-n-samples` |
| 46 | + |
| 47 | +2. **Code Generation:** |
| 48 | + - For each programming problem, the model is provided with a prompt containing: |
| 49 | + - Function signature (e.g., `def sort_numbers(numbers):`) |
| 50 | + - Docstring describing the function's purpose and requirements |
| 51 | + - The model generates k code completions for the function body (controlled by `--k-samples`) |
| 52 | + - These k samples are used to calculate the pass@k metric |
| 53 | + |
| 54 | +3. **Secure Execution:** |
| 55 | + - Generated code is executed in a secure sandbox environment maintained by OpenAI's human-eval library. For your awareness, OpenAI's policy is to disable code execution by default, however lemonade enables code execution by default by automatically setting the environment variable `HF_ALLOW_CODE_EVAL=1`. OpenAI provides the following code execution protections: |
| 56 | + - **Process Isolation**: Each code sample runs in a separate process to prevent interference |
| 57 | + - **Resource Limits**: |
| 58 | + - CPU time limit (controlled by `--timeout`) |
| 59 | + - Memory usage restrictions |
| 60 | + - Maximum output size restrictions |
| 61 | + - **Restricted Access**: |
| 62 | + - No network access |
| 63 | + - No file system access outside test directory |
| 64 | + - No subprocess creation |
| 65 | + - No system calls |
| 66 | + - **Module Restrictions**: |
| 67 | + - Only allows importing standard Python libraries needed for testing |
| 68 | + - Blocks potentially dangerous modules (os, sys, subprocess, etc.) |
| 69 | + These security measures are implemented through: |
| 70 | + - Python's built-in `resource` module for resource limits |
| 71 | + - AST (Abstract Syntax Tree) analysis for code validation |
| 72 | + - Process-level isolation using `multiprocessing` |
| 73 | + - Custom import hooks to restrict module access |
| 74 | + |
| 75 | +4. **Evaluation Metrics:** |
| 76 | + - **pass@k**: Percentage of problems solved with k attempts |
| 77 | + - pass@1: Success rate with single attempt |
| 78 | + - pass@10: Success rate within 10 attempts |
| 79 | + - pass@100: Success rate within 100 attempts |
| 80 | + - A problem is considered solved if all test cases pass |
| 81 | + - Results are normalized to percentages |
| 82 | + |
| 83 | +5. **Output Files:** |
| 84 | + The tool generates several output files in the results directory: |
| 85 | + - `evaluation_results.csv`: Contains prompts, completions, and expected answers |
| 86 | + - `humaneval_predictions.jsonl`: Raw model predictions in JSONL format |
| 87 | + - `humaneval_predictions.jsonl_results.jsonl`: Detailed evaluation results |
| 88 | + |
| 89 | +## Example Results Format |
| 90 | + |
| 91 | +The evaluation produces metrics in the following format: |
| 92 | +```json |
| 93 | +{ |
| 94 | + "pass@1": 0.25, // 25% success rate with 1 attempt |
| 95 | + "pass@10": 0.45, // 45% success rate within 10 attempts |
| 96 | + "pass@100": 0.65 // 65% success rate within 100 attempts |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Limitations |
| 101 | + |
| 102 | +1. **Resource Requirements**: Generating multiple samples per problem (high k values) can be computationally intensive and time-consuming. |
| 103 | +2. **Memory Usage**: Large language models may require significant memory, especially when generating multiple samples. |
| 104 | + |
| 105 | +## References |
| 106 | + |
| 107 | +1. [Evaluating Large Language Models Trained on Code](https://arxiv.org/abs/2107.03374) |
| 108 | +2. [OpenAI HumanEval Repository](https://github.com/openai/human-eval) |
0 commit comments