TL;DR
This paper introduces PtTrust, a novel two-stage framework that leverages the internal states of code Large Language Models (LLMs) to identify risky code lines. By pre-training a risk assessment model on internal states and then fine-tuning it with minimal labeled data, PtTrust offers a scalable and interpretable solution for enhancing the trustworthiness of AI-generated code.
What Problem Does This Paper Address?
The rapid integration of code LLMs into software development workflows has brought immense benefits, but a significant challenge remains: their trustworthiness. These models can generate code that is incorrect, insecure, or unreliable, leading to potential issues for developers and organizations. While analyzing LLM internal states offers a promising avenue for detecting such risks, existing methods are often limited to narrow domains, lack scalability, and struggle to generalize across different tasks and programming languages. This paper aims to bridge this gap by proposing a practical and scalable framework for fine-grained risk assessment of code LLMs.
Methods
PtTrust employs a two-stage approach:
- Pre-training Stage (Unsupervised): This stage focuses on extracting internal states from a target code LLM. It utilizes Sparse Autoencoders (SAEs), specifically a variant known as TopK autoencoders, to learn a compressed, low-dimensional representation of these internal states. This process is unsupervised and designed to scale efficiently with large code corpora, making it suitable for industrial use. Contrastive learning is incorporated to encourage the model to distinguish between representations of correct and incorrect code lines. The focus is on the newline token (
\n) to reduce computational costs and mitigate bias. - Semantic Binding Stage (Supervised): In this stage, the learned representations from the SAE are mapped to specific risks. A small, labeled dataset of code lines (correct vs. incorrect) is used to train a downstream classifier. Instead of simply predicting correctness, the framework is designed to output rankings of code lines by risk level, providing a more intuitive and user-friendly interface for developers. This stage leverages learning-to-rank (LTR) techniques, specifically NeuralNDCG loss, to prioritize the most critical lines.
Datasets
The paper utilizes several publicly available datasets for training and evaluation:
- Pre-training: LeetCode Dataset, HumanEval-Pack (EvalPack), and DebugBench. For LeetCode, which lacks bug annotations, mutation techniques (switch inside, switch outside, delete line) were used to generate incorrect code.
- Semantic Binding & Evaluation: HumanEval (for code generation), EvalPack and Quixbug (for code repair), EditEval (for code editing), Code Lingua (for code translation), and Defects4j (for code repair).
The authors note that these datasets can be substituted with internal company code repositories for pre-training and commit histories for semantic binding.
Key Insights and Ideas
- Internal States as Risk Indicators: The core idea that LLMs' internal states, much like human cognitive signals, can reveal potential errors in generated code is a powerful concept.
- Two-Stage Pre-training for Scalability: The proposed two-stage approach (unsupervised representation learning followed by supervised semantic binding) is crucial for achieving scalability and practicality in industrial settings where labeled data is scarce.
- Fine-grained, Line-Level Risk Assessment: Moving beyond snippet-level correctness to identifying specific risky lines significantly enhances developer efficiency by allowing them to focus on critical areas.
- Interpretable Features: The use of SAEs not only compresses high-dimensional states but also yields interpretable features, fostering user trust by providing insights into why a piece of code is flagged as risky.
- Cross-Language and Cross-Task Generalization: The framework demonstrates a remarkable ability to generalize its risk assessment capabilities across different programming languages and diverse software engineering tasks, a significant advantage over previous methods.
Key Results
- State-of-the-Art Performance: PtTrust achieved state-of-the-art performance in identifying incorrect code lines (RQ1) across multiple code LLMs (Code Llama, StarCoder2, Qwen2.5-Coder) and tasks (EditEval, Code Lingua, Defects4j), often outperforming baselines by a significant margin (e.g., 5.5x more buggy lines identified on Code Lingua for Code Llama).
- Robust Snippet-Level Prediction: PtTrust also demonstrated strong performance in identifying incorrect code snippets (RQ2), achieving SOTA in 7 out of 9 evaluated cases.
- Interpretable Features: The learned latent representations from SAEs showed consistent activation patterns for buggy code lines across different tasks and languages, mirroring human error-related negativity signals.
- Uncertainty-Based Methods Limitations: While simple, uncertainty-based methods struggled with fine-grained risk assessment and snippet-level prediction, highlighting the need for more sophisticated approaches.
Limitations and Future Work
- Labeling Quality: The reliance on GPT-40 for generating fine-grained error labels introduces potential uncertainty. While mitigated by manual review and sampling, future work could explore fully human-labeled data from feedback platforms.
- Limited Layer Exploration: The study focused on early layers of LLMs. Future work could explore other layers and even attention mechanisms to gain deeper insights.
- Broader Trustworthiness Properties: The framework is extensible to other trustworthiness properties like security and performance, provided relevant labels can be obtained.
- Evaluating Latent Representations: Developing methods to directly assess the quality of learned latent representations without relying on downstream tasks is a promising direction for accelerating model improvement.
- External Validity: While evaluated on multiple LLMs and tasks, further validation with a wider variety of models and tasks would strengthen generalizability claims.
Summary updated