On LLMs' Internal Representation of Code Correctness
Francisco Ribeiro, Claudio Spiess, Prem Devanbu, Sarah Nadi

TL;DR
This paper investigates whether large language models internally encode code correctness, and demonstrates that exploiting these internal signals can improve code quality selection without test execution.
Contribution
It introduces a method to identify and utilize internal correctness representations in LLMs, surpassing traditional ranking methods for code quality.
Findings
Internal correctness signals can be extracted from LLMs' hidden states.
Using internal signals improves code selection over log-likelihood ranking.
The approach enhances the reliability of automatically generated code.
Abstract
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…
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Taxonomy
TopicsSoftware Engineering Research · Topic Modeling · Natural Language Processing Techniques
