TL;DR
This paper introduces CodeScore, a novel metric for evaluating code generation that learns to predict functional correctness by observing code execution. Unlike previous metrics that rely on surface-level text matching, CodeScore directly assesses whether generated code works across various input formats (natural language, reference code, or both), achieving state-of-the-art performance and significantly outperforming existing methods.
What Problem Does This Paper Address?
Evaluating the quality of automatically generated code is a critical bottleneck in advancing code generation research. Existing metrics, like BLEU and CodeBLEU, primarily focus on syntactic similarity (e.g., n-grams, abstract syntax trees) rather than functional correctness. This means they can incorrectly score syntactically similar but functionally different code, or penalize functionally correct but syntactically novel code. Furthermore, these metrics are often limited to a "reference-only" input format, ignoring valuable natural language descriptions and struggling when no reference code is available. This paper aims to bridge this gap by developing a more robust and versatile evaluation metric that learns from code execution.
Methods
The core of this work is CodeScore, an LLM-based metric trained using a novel framework called UniCE (Unified Code Evaluation). UniCE fine-tunes pre-trained LLMs to predict two key aspects of code execution:
- PassRatio: A continuous score (0-1) representing the proportion of test cases the generated code passes. This is learned by minimizing the squared difference between the predicted CodeScore and the actual PassRatio.
- Executability: A binary indicator (0 or 1) of whether the code compiles and runs without errors, even if its output is incorrect. This is learned by minimizing the negative log-likelihood of the conditional probability of execution given its executability.
UniCE is designed to be flexible, accepting three input formats:
- Ref-only (g + r): Generated code concatenated with its reference code.
- NL-only (g + n): Generated code concatenated with its natural language description.
- Ref&NL (g + r + n): Generated code concatenated with both reference code and NL description.
The framework uses a pooling layer to effectively combine information from different LLM layers and a unified training objective ($L_{Uni}$) that optimizes for all input formats simultaneously.
Datasets
The authors constructed three new code evaluation datasets:
- APPS-Eval: Based on the APPS benchmark.
- MBPP-Eval: Based on the MBPP benchmark.
- HE-Eval: Based on the HumanEval benchmark.
Each dataset task includes an NL description, reference code(s), multiple generated codes (from various LLMs), and a large number of test cases (over 100 per task). These datasets are designed to provide a comprehensive evaluation of code generation models and metrics.
Key Insights and Ideas
- Functional correctness is paramount: The paper strongly argues that evaluating code based on its execution and functional correctness is far more important than syntactic similarity.
- LLMs can learn to evaluate code: By training LLMs on code execution outcomes (PassRatio and Executability), they can be taught to act as effective code evaluators.
- Versatility in input formats matters: CodeScore's ability to handle Ref-only, NL-only, and Ref&NL inputs significantly broadens its applicability, especially in scenarios where reference code is scarce or unavailable.
- Execution-based metrics are costly but valuable: While execution-based evaluation is computationally expensive, CodeScore offers a way to approximate this without the full overhead, achieving comparable or better results.
- Longer code can be problematic for traditional metrics: The paper notes that CodeBLEU's correlation with ground truth degrades for longer code, suggesting limitations in its ability to handle complex syntactic structures.
Key Results
- State-of-the-art performance: CodeScore achieved significant improvements in correlation with functional correctness across all three datasets, outperforming existing match-based and LLM-based metrics by up to 58.87% (on Pearson R).
- Low Mean Absolute Error (MAE): CodeScore also demonstrated the lowest MAE, indicating high accuracy in its evaluations.
- Efficiency: CodeScore's execution time is comparable to other LLM-based metrics and orders of magnitude faster than traditional execution-based CEMs.
- Exec's effectiveness: The
Execcomponent of CodeScore showed high Precision, F1 Score, and Accuracy in determining code executability. - Human evaluation validation: Human evaluators rated CodeScore significantly higher than other metrics, with an improvement of at least 54.6% in perceived reasonableness.
- Generalizability: CodeScore trained on one dataset (APPS-Eval) showed strong performance on others (MBPP-Eval, HE-Eval) even in zero-shot settings.
Limitations and Future Work
- Data requirements: Training CodeScore requires a substantial amount of data, including test cases, generated code, reference code, and NL descriptions.
- Scope: Currently, CodeScore is primarily suited for function-level Python code. The authors aim to extend it to encompass a wider range of code types and scenarios.
- Computational cost: While more efficient than full execution-based evaluation, CodeScore still incurs additional computation and time compared to simpler metrics.
Future work includes broadening CodeScore's applicability to more complex code structures and scenarios.
Summary updated