2025-02-04 · 进阶 · 机器学习 · 计算机视觉 · 数据科学

Learning to Generate Unit Tests for Automated Debugging

这篇论文提出了UTGEN和UTDEBUG,旨在通过自动生成单元测试来帮助大型语言模型(LLM)进行自动调试。

更新于 2025-02-04。作者:Archiki Prasad、Elias Stengel-Eskin、Justin Chih-Yao Chen、Zaid Khan、Mohit Bansal。

中文摘要

单元测试(UTs)在评估代码正确性以及为大型语言模型(LLM)在迭代调试错误代码时提供反馈方面发挥着关键作用,同时也激励了自动化测试生成。然而,我们发现生成能够揭示错误的单元测试输入与正确预测单元测试输出(在没有访问黄金解决方案的情况下)之间存在权衡。为了解决这一权衡,我们提出了UTGen,它教会LLM根据任务描述和候选代码生成能够揭示错误的单元测试输入及其正确的预期输出。我们将UTGen集成到UTDebug中,这是一个使用生成的测试来帮助LLM有效调试的强大调试流程。由于模型生成的测试可能会提供嘈杂的信号(例如,来自错误预测的输出),UTDebug通过以下方式改进了UTGen的缩放:(i)通过测试时间计算来提高UT输出预测;(ii)基于多个生成的UT进行验证和回溯编辑,以避免过拟合。我们通过一个衡量错误揭示的UT输入和正确UT输出都存在的指标,表明UTGen比UT生成基线表现优异7.59%。当与UTDebug一起使用时,我们发现来自UTGen单元测试的反馈使得Qwen-2.5 7B在HumanEvalFix上的pass@1准确率比其他基于LLM的UT生成基线提高了超过3%,在我们的MBPP+更难调试的子集上提高了12.35%。

英文摘要

Unit tests (UTs) play an instrumental role in assessing code correctness as well as providing feedback to a large language model (LLM) as it iteratively debugs faulty code, motivating automated test generation. However, we uncover a trade-off between generating unit test inputs that reveal errors when given a faulty code and correctly predicting the unit test output without access to the gold solution. To address this trade-off, we propose UTGen, which teaches LLMs to generate unit test inputs that reveal errors along with their correct expected outputs based on task descriptions and candidate code. We integrate UTGen into UTDebug, a robust debugging pipeline that uses generated tests to help LLMs debug effectively. Since model-generated tests can provide noisy signals (e.g., from incorrectly predicted outputs), UTDebug (i) scales UTGen via test-time compute to improve UT output prediction, and (ii) validates and back-tracks edits based on multiple generated UTs to avoid overfitting. We show that UTGen outperforms UT generation baselines by 7.59% based on a metric measuring the presence of both error-revealing UT inputs and correct UT outputs. When used with UTDebug, we find that feedback from UTGen's unit tests improves pass@1 accuracy of Qwen-2.5 7B on HumanEvalFix and our own harder debugging split of MBPP+ by over 3% and 12.35% (respectively) over other LLM-based UT generation baselines.

解读

一句话总结

这篇论文提出了UTGEN和UTDEBUG,旨在通过自动生成单元测试来帮助大型语言模型(LLM)进行自动调试。

问题1:这篇论文想要解决什么具体问题?

• 问题背景:当前LLM在调试错误代码时面临缺乏有效的单元测试反馈的问题,因为手动编写单元测试耗时且效率低,而现有方法无法有效生成可触发错误的单元测试。 • 现有方案不足:现有方法依赖于正确的代码实现来生成单元测试输出,这在测试时间无法获取正确实现,且难以预测错误的代码。 • 研究目标:提出一种新的方法,能够自动生成既能触发错误又能预测正确输出的单元测试,从而帮助LLM更有效地进行调试。

问题2:论文的核心创新点是什么?

• 技术创新:UTGEN通过从代码生成基准中创建训练数据来训练模型,从而生成既能触发错误又能预测正确输出的单元测试。 • 方法改进:UTDEBUG通过测试时间计算来改进生成的单元测试输出预测,并通过多生成单元测试和验证回溯编辑来避免过拟合。 • 优势:与现有方法相比,UTGEN生成的单元测试在攻击率和输出准确性方面均有所提高,而UTDEBUG通过提高输出准确性和防止过拟合来提高调试性能。

问题3:实验结果如何验证了方法的有效性?

• 关键实验:在HumanEvalFix和MBPP+的子集上进行调试实验。 • 性能提升:UTGEN比基线方法在测量错误揭示的单元测试输入和正确单元测试输出的指标上提高了7.59%。 • 对比结果:当使用UTDEBUG时,与基于LLM的单元测试生成基线相比,Qwen-2.5在HumanEval-Fix和MBPP+的更困难的调试分割上分别提高了超过3%和12.35%。

问题4:这个研究的实际应用价值是什么?

• 应用场景:在自动化调试和代码验证领域,尤其是在大型语言模型辅助开发中。 • 实施建议:结合UTGEN和UTDEBUG,为LLM开发提供高效的代码调试工具。 • 局限与展望:尽管取得了显著成果,但模型生成的单元测试仍然可能存在噪声,未来的工作可以进一步改进模型的鲁棒性和准确性。

来源