TL;DR
This paper introduces CodeQueries, a novel dataset for evaluating how well AI models can understand the semantic meaning of code. They found that even advanced language models struggle with this task, highlighting the need for better methods to interpret code beyond just its syntax.
What Problem Does This Paper Address?
Developers often need to understand the "why" and "how" of code, not just what it does. This includes questions about class relationships, attribute conflicts, or the implications of certain code patterns. Answering these questions requires understanding code semantics – how different parts of the code relate to each other and what they mean. While there's been progress in AI understanding natural language and even code syntax, truly grasping code semantics for complex, file-level queries remains a significant challenge. Existing datasets often focus on simpler, localized code snippets (like individual methods) or binary yes/no questions, leaving a gap for more nuanced, real-world developer queries.
Methods
The authors developed a systematic approach to create their dataset. They leveraged CodeQL, a powerful static analysis tool, to automatically identify semantic code patterns. They then translated these patterns into natural language queries. For each query, they extracted "answer spans" (the code that directly answers the question) and "supporting-fact spans" (code that provides evidence for the answer). They also created negative examples, where a query is plausible but doesn't have a valid answer in the given code, to train models to recognize when a pattern is absent.
To evaluate AI models, they tested two main approaches:
- Large Language Models (LLMs) like GPT3.5-Turbo: They explored zero-shot (no examples) and few-shot (with examples) prompting techniques. Few-shot prompting, especially when including supporting facts, showed improvements.
- Fine-tuned Contextual Embedding Models (CuBERT): They fine-tuned a BERT-style model using a two-step process: first, a relevance classifier to identify important code blocks within large files, and then a span prediction model to pinpoint the answer and supporting facts.
Datasets
The primary contribution is the CodeQueries dataset. It contains:
- 52 semantic queries over Python code.
- 34,662 positive examples (code snippets with answers).
- 52,613 negative examples (code snippets without answers).
The dataset is derived from the ETH Py150 dataset of Python code. Unlike existing datasets that often focus on method-level context or simpler question types, CodeQueries uses file-level context and requires understanding of code semantics, including single-hop and multi-hop reasoning.
Key Insights and Ideas
- Code Semantics are Hard: Even state-of-the-art LLMs like GPT3.5-Turbo struggle significantly with accurately identifying semantic code spans, especially for complex, multi-hop reasoning queries. This suggests that current models primarily rely on surface-level patterns rather than deep semantic understanding.
- Supporting Facts Matter: Providing models with "supporting facts" (evidence code snippets) in few-shot prompts significantly improves their ability to predict correct answer spans. This hints that explainability and reasoning are crucial for semantic code understanding.
- Negative Examples are Crucial: The inclusion of negative examples (where a query is plausible but has no answer) is vital for training models to correctly identify the absence of a semantic pattern, not just its presence.
- File-Level Context is a Challenge: Models need to sift through entire files, distinguishing relevant code from irrelevant "distractors," which adds a layer of complexity not present in method-level tasks.
Key Results
- GPT3.5-Turbo (Zero-shot): Achieved only 20.84% exact match on positive examples and 26.77% on negative examples (pass@10).
- GPT3.5-Turbo (Few-shot with BM25): Improved to 32.66% (positive) and 70.08% (negative) exact match (pass@10).
- GPT3.5-Turbo (Few-shot with Supporting Facts): Achieved 39.08% exact match on positive examples (pass@10).
- CuBERT (Two-step, best performing): The best fine-tuned CuBERT model (trained on all data) achieved 52.61% exact match on positive examples and 96.73% on negative examples. However, a more practical setup (trained on limited data) only achieved 3.74% on positive examples.
- The high accuracy on negative examples for CuBERT is attributed to a bias towards predicting "Outside" labels, indicating a potential weakness in distinguishing subtle differences.
Limitations and Future Work
Limitations: * The evaluation was primarily conducted on Python code. * The study focused on two representative model classes (LLMs and fine-tuned encoders). * The dataset is based on queries from a specific static analysis tool (CodeQL), which might not cover all developer needs.
Future Work: * Extend the dataset to include more semantic queries and support for other programming languages. * Develop more sophisticated neural models capable of deeper semantic understanding and multi-hop reasoning. * Investigate how to better handle large code contexts and filter irrelevant information. * Explore methods for generating explanations or justifications for predicted spans.
Summary updated