TL;DR
CodeT is a novel framework that significantly boosts the accuracy of code generation by using Large Language Models (LLMs) to automatically generate their own unit tests. By executing candidate solutions against these generated tests and using a "dual execution agreement" to rank them, CodeT achieves a massive jump in performance—for example, raising the pass@1 rate on the HumanEval benchmark from 47.0% to 65.8%.
The Selection Problem in Code Generation
Large Language Models like OpenAI’s Codex have become remarkably good at writing code. However, they suffer from a "selection problem." While a model might generate a correct solution somewhere in a batch of 100 samples (a high pass@100), it often struggles to pick the correct one as its primary choice (a much lower pass@1).
In a real-world setting, we can't expect users to provide exhaustive test suites for every snippet they want to generate. Without tests, how do we know which of the 100 generated samples is actually correct? Previous methods tried to rank solutions based on log-probabilities (how "likely" the code looks) or simple clustering (how "common" a solution is), but these often fail to capture the actual functional correctness of the code.
The CodeT Methodology
The authors introduce CodeT (Code generation with generated Tests), which leverages the LLM's ability to act as both a coder and a tester.
The process follows three main steps:
- Code Generation: The model generates a large set of candidate solutions for a given problem.
- Test Case Generation: Using a specific prompt instruction (e.g., adding a
passplaceholder and anassertstatement), the same LLM generates a set of potential test cases consisting of input-output pairs. - Dual Execution Agreement: This is the core innovation of CodeT. Instead of just checking if a solution passes a test, CodeT looks for a "consensus."
As illustrated above, CodeT groups solutions that behave identically across the generated tests. It calculates a score for each group (consensus set) based on the product of the number of solutions in the group and the number of test cases they all pass. The intuition is inspired by the RANSAC algorithm: correct solutions should consistently pass the same set of valid tests, while incorrect solutions are likely to fail or behave erratically in diverse ways.
Datasets and Key Results
The researchers evaluated CodeT across four major benchmarks that vary in difficulty: HumanEval (164 hand-written Python problems), MBPP (427 entry-level problems), APPS (5,000 problems up to competition level), and CodeContests (highly complex competitive programming).
CodeT achieved state-of-the-art results across the board:
- HumanEval: Using
code-davinci-002, CodeT improved thepass@1from 47.0% to 65.8%—an absolute gain of 18.8%. - MBPP: Improved from 58.1% to 67.7%.
- Competitive Programming: On the difficult APPS Introductory set, it boosted performance from 27.2% to 34.6%.
- Efficiency: Even with only 10 generated test cases, CodeT still outperformed the baseline by a wide margin, showing it doesn't require massive compute to be effective.
Key Insights
- Self-Verification is Powerful: LLMs possess an inherent power to verify their own work. Even if a model isn't sure which code is right, it is often quite good at dreaming up what a valid input and output should look like.
- The "Dual" Matters: Ranking by only the number of solutions (clustering) or only the number of tests passed is insufficient. The product of the two is much more robust against "toxic" (incorrect) test cases.
- Cross-Model Synergy: High-quality test cases generated by a superior model (like
code-davinci-002) can be used to significantly boost the performance of "mediocre" models (likeInCoderorCodeGen). This suggests that testing capability scales differently than coding capability.
Limitations and Future Work
While powerful, CodeT requires code that can be executed in a sandbox, making it difficult to apply to UI code or snippets with complex external dependencies. Furthermore, generating and executing hundreds of samples and tests adds significant latency and API costs compared to a single-shot generation. Finally, if a problem description is ambiguous, the model may generate "correct" code and tests for the wrong interpretation, which CodeT might still rank highly. Future work will likely explore ways to handle non-executable tasks and optimize the generation process to reduce costs.
Reference: Chen, B., et al. (2023). CodeT: Code Generation with Generated Tests. ICLR 2023.
Summary updated