TL;DR
This paper introduces UTGen, a novel method for training Large Language Models (LLMs) to generate effective unit tests that not only reveal bugs in faulty code but also provide correct expected outputs. Combined with UTDEBUG, a debugging framework that handles noisy test feedback, this approach significantly improves LLM-based code debugging and generation.
What Problem Does This Paper Address?
Automated debugging and code generation by LLMs are crucial for improving software development. A key component in this process is the use of unit tests (UTs) to assess code correctness and provide feedback. However, generating high-quality UTs is challenging. Existing methods often face a trade-off: UTs that are good at revealing errors (high "attack rate") are difficult for LLMs to predict the correct output for, and vice-versa. Furthermore, human-written UTs are scarce, and automatically generated UTs can be noisy, leading to ineffective debugging. This paper tackles the problem of generating UTs that are both error-revealing and have accurate outputs, and then effectively using these UTs to improve LLM-based debugging and code generation.
Methods
The paper proposes two main contributions:
- UTGen (Unit Test Generator): This is a training recipe designed to teach LLMs to generate UTs that balance the trade-off between attack rate and output accuracy. UTGen bootstraps training data from existing code generation datasets by perturbing correct code to create faulty versions. It then trains LLMs to generate UT inputs that trigger errors in these faulty codes, along with their correct expected outputs.
- UTDEBUG (Unit Test Debugger): This is a debugging pipeline that leverages UTGen's output. It addresses the issue of noisy UT feedback by:
- Test-time compute scaling (self-consistency): Using multiple samples to predict UT outputs more accurately.
- Backtracking and validation: Accepting code edits only if they improve the overall pass rate across a suite of generated UTs, and discarding edits otherwise. This prevents overfitting to individual noisy tests.
Datasets
- HumanEvalFix: A dataset with human-introduced errors in code, augmented with a larger suite of UTs from EvalPlus for more comprehensive evaluation.
- MBPP+Fix and MBPP+Fix (Hard): Datasets constructed from the MBPP benchmark. MBPP+Fix contains faulty code generated by LLMs, while MBPP+Fix (Hard) focuses on more subtle errors and corner cases, making debugging more challenging.
- Tulu-3 SFT mixture: Used for bootstrapping UTGen's training data.
Key Insights and Ideas
- The UT Generation Trade-off: There's a fundamental tension between generating UTs that effectively expose bugs (high attack rate) and generating UTs whose correct outputs can be reliably predicted by LLMs.
- UTGen's Sweet Spot: UTGen aims to find a "sweet spot" where UTs are challenging enough to reveal errors but not so complex that their correct outputs are unpredictable.
- Noisy UTs are Still Useful: Even with potential inaccuracies in generated UTs, UTDEBUG demonstrates that they can still significantly improve LLM debugging and code generation, provided the noise is handled effectively.
- Quality of UTs Matters for Debugging: The quality of generated UTs is crucial. UTGen-generated UTs, which are designed to be both error-revealing and accurate, lead to substantially better debugging performance compared to UTs generated by simpler prompting methods.
- LLMs Struggle with Self-Correction: The paper highlights that LLMs are generally poor at self-critiquing and debugging without external feedback, underscoring the importance of UT-based feedback.
Key Results
- Intrinsic Evaluation: UTGen outperforms other LLM-based baselines by 7.59% on a metric combining attack rate and output accuracy.
- Debugging Performance: When used with UTDEBUG, UTGen-generated UTs improve the pass@1 accuracy of Qwen2.5 32B on HumanEvalFix by 3.17% and on MBPP+Fix (Hard) by 12.35% compared to other UT generation baselines.
- Frontier LLM Enhancement: UTGen feedback enhances debugging with GPT-4o by 13.8%.
- Code Correctness Judging: UTGen is a better judge for code correctness than a state-of-the-art 8B reward model, outperforming it by 4.43% on HumanEval+.
- MBPP+Fix (Hard) is Challenging: This dataset, with subtle errors, proves significantly harder for LLMs to debug, highlighting the need for robust UT generation and debugging strategies.
Limitations and Future Work
- Computational Cost: While UTGEN and UTDEBUG are effective, the process of generating multiple UTs and performing test-time scaling can be computationally intensive, especially for frontier LLMs.
- Noise Mitigation: While UTDEBUG effectively handles noisy UTs, there's still room for improvement in making the debugging process even more robust.
- Future Work: The authors suggest exploring UT generation for identifying potential issues in new code, similar to how SWEBench addresses known GitHub issues. They also point to the ongoing challenge of LLMs accurately predicting intended program behavior from specifications alone.
Summary updated