发现大模型内部能编码代码正确性,提升生成质量
On LLMs' Internal Representation of Code Correctness
- 通过对比正确与错误代码的隐藏状态,提取内部正确性信号
- 新方法比传统概率排序和模型自述信心更准确筛选优质代码
- 无需运行测试即可判断代码质量,适合提升生成可靠性
尽管大型语言模型(LLMs)在代码生成中表现有效,但常产生错误代码。原因之一是模型输出概率与代码正确性相关性弱,仅反映生成结果。受大模型内编码真实性的启发,本文探索其是否也编码代码正确性。通过对比相同编程任务下正确与错误代码的隐藏状态,我们识别出模型内部的正确性表示。在四个LLM上实验表明,利用该提取的正确性表示优于标准对数似然排名及模型自述置信度。进一步研究发现,该内部信号可用于选择更高质量的代码样本,无需实际执行测试。本工作展示了利用内部表征增强代码生成系统的方法,提升大模型生成代码的可靠性。
原文摘要 · Abstract (English)
Despite the effectiveness of large language models (LLMs) for code generation, they often output incorrect code. One reason is that model output probabilities are often not well-correlated with correctness, and reflect only the final output of the generation process. Inspired by findings that LLMs internally encode concepts like truthfulness, this paper explores if LLMs similarly represent code correctness. Specifically, we identify a correctness representation inside LLMs by contrasting the hidden states between pairs of correct and incorrect code for the same programming tasks. By experimenting on four LLMs, we show that exploiting this extracted correctness representation outperforms standard log-likelihood ranking, as well as verbalized model confidence. Furthermore, we explore how this internal correctness signal can be used to select higher-quality code samples, without requiring test execution. Ultimately, this work demonstrates how leveraging internal representations can enhance code generation systems and make LLMs more reliable, thus improving confidence in automatically generated code.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。