arXiv(opens in a new tab)Computer ScienceApr 2023Cited by 37 papers on PaperVantageClassic Paper

Manuscript Offprint

arXiv:2304.05128

Teaching Large Language Models to Self-Debug

Xinyun Chen, Maxwell Lin, Nathanael Schärli, Denny Zhou

Abstract

Large language models (LLMs) have achieved impressive performance on code generation. However, for complex programming tasks, generating the correct solution in one go becomes challenging, thus some prior works have designed program repair approaches to improve code generation performance. In this work, we propose Self-Debugging, which teaches a large language model to debug its predicted program via few-shot demonstrations. In particular, we demonstrate that Self-Debugging can teach the large language model to perform rubber duck debugging; i.e., without any human feedback on the code correctness or error messages, the model is able to identify its mistakes by investigating the execution results and explaining the generated code in natural language. Self-Debugging achieves the state-of-the-art performance on several code generation benchmarks, including the Spider dataset for text-to-SQL generation, TransCoder for C++-to-Python translation, and MBPP for text-to-Python generation. On the Spider benchmark where there are no unit tests to verify the correctness of predictions, Self-Debugging with code explanation consistently improves the baseline by 2−3%2-3\%, and improves the prediction accuracy on problems of the hardest level by 9%9\%. On TransCoder and MBPP where unit tests are available, Self-Debugging improves the baseline accuracy by up to 12%12\%. Meanwhile, by leveraging feedback messages and reusing failed predictions, Self-Debugging notably improves sample efficiency, and can match or outperform baseline models that generate more than 10×\times candidate programs.

1 Introduction

Code generation has been a long-standing challenge with a variety of applications, such as code synthesis from natural languages (Yu et al., 2018; Chen et al., 2021a; Austin et al., 2021; Li et al., 2022), programming by examples (Devlin et al., 2017; Bunel et al., 2018; Chen et al., 2019), and code translation (Roziere et al., 2020; Chen et al., 2018). In particular, recent large language models have demonstrated a significant leap in improvement over prior deep neural networks (Chen et al., 2021a; Nijkamp et al., 2023; Zheng et al., 2023; Xu et al., 2022). However, for many programming tasks, generating correct code with a single attempt is challenging. Inspired by observations that correct code is much more likely to be predicted when multiple programs are sampled from the model (Chen et al., 2021a; Chowdhery et al., 2022; Li et al., 2022), one line of work has designed reranking techniques to select the best candidate from multiple samples, which typically requires tens of samples to start with (Shi et al., 2022; Zhang et al., 2022; Ni et al., 2023; Li et al., 2022).

Intuitively, even for human programmers, there is no guarantee that the code written on the first try is always accurate. Instead of completely discarding the incorrect code, humans typically look into the code and investigate execution results, then make changes to resolve the implementation errors. Therefore, prior works propose deep learning techniques to repair the predicted code, which demonstrate notable performance gains on various coding tasks (Gupta et al., 2020; Wang et al., 2018; Fu et al., 2019; Chen et al., 2023a). However, these approaches require additional training for the code repair model. While some recent works show that large language models have potential for generating feedback messages to critique and refine their outputs for some natural language and reasoning domains (Shinn et al., 2023; Madaan et al., 2023b; Kim et al., 2023; Bai et al., 2022), prior works suggest that such large language models are not yet capable of correcting code when lacking external feedback, such as unit tests or human instructions (Chen et al., 2023a).

In this work, we propose Self-Debugging, where we teach the large language model to debug its own predicted code via few-shot prompting. Without any additional model training, Self-Debugging instructs the model to execute the code, then generate a feedback message based on the code and its execution result. Different from prior works on utilizing human feedback for code repair, where the feedback message explains the code errors and how to fix them (Chen et al., 2023a; Austin et al., 2021), Self-Debugging teaches the model to identify the implementation errors via investigating into execution results and explaining the code by itself. This debugging process is reminiscent of rubber duck debugging for human programmers, where explaining the code line-by-line in natural language to a rubber duck significantly boosts debugging efficiency without expert guidance (Hunt & Thomas, 2000). Figure 1 illustrates the full procedure of Self-Debugging.

We evaluate Self-Debugging on a variety of models, including code-davinci-002 (Chen et al., 2021a), gpt-3.5-turbo, gpt-4 (OpenAI, 2023) in the GPT model family, as well as StarCoder (Li et al., 2023b), a strong open-source LLM for code generation. Self-Debugging achieves the state-of-the-art performance on different types of code generation tasks, including text-to-SQL generation, code translation and text-to-Python generation. On the Spider benchmark (Yu et al., 2018) for text-to-SQL generation where there are no unit tests in the problem description, with code explanation, Self-Debugging consistently improves the baseline by 2−3%2-3\% with different numbers of initial programs, and improves the prediction accuracy on the most complicated SQL queries by 9%9\%. On both TransCoder for code translation (Roziere et al., 2020) and MBPP for text-to-Python generation (Austin et al., 2021), utilizing unit tests along with code explanation boosts the accuracy by up to 12%12\%, and code explanation alone without debugging also consistently improves the code translation performance by 2−3%2-3\%. Meanwhile, Self-Debugging improves sample efficiency, and can match or outperform baseline models that sample more than 10×\times predictions. Our work indicates that besides improving their ability to generate code from scratch, teaching large language models to perform Self-Debugging without human guidance is another promising path to enhance coding capability and reduce the sampling cost required to accomplish challenging tasks.

Figure

Figure 1: Self-Debugging for iterative debugging using a large language model. At each debugging step, the model first generates new code, then the code is executed and the model explains the code. The code explanation along with the execution results constitute the feedback message, based on which the model infers the code correctness and then adds this message to the feedback. The feedback message is then sent back to the model to perform more debugging steps. When unit tests are not available, the feedback can be purely based on code explanation.

References (71)

Cited by (37)