TL;DR
This paper introduces CAST, a novel method for improving code translation in large language models (LLMs) by incorporating code's structural knowledge after pre-training, using in-context learning (ICL). CAST effectively selects relevant examples by measuring how well their Abstract Syntax Trees (ASTs) cover the test code's AST, leading to significant performance gains without model retraining.
What Problem Does This Paper Address?
Large Language Models (LLMs) have shown impressive capabilities in various software mining tasks, but effectively handling the strict and complex syntactic structure of source code remains a challenge. Traditional methods that incorporate code structure often require intricate model architectures and resource-intensive training, making them impractical for off-the-shelf LLMs or proprietary models. In-context learning (ICL) offers a way to inject knowledge without retraining by providing examples in the prompt, but selecting the right examples for code translation, which heavily relies on syntactic structure, is difficult. Existing ICL exemplar selection methods often focus on simple similarity or diversity, neglecting the compositional nature of code structure and its impact on translation quality.
Methods
The core contribution is the Coverage of Abstract Syntax Tree (CAST) metric and a corresponding greedy submodular maximization algorithm for exemplar selection.
- CAST Metric: Instead of relying on simple similarity or diversity, CAST quantifies information coverage by measuring the proportion of nodes in the test source code's AST that are covered by subtrees from selected exemplars' ASTs. This focuses on structural relevance and compositional knowledge.
- AST Fingerprinting: To efficiently compare ASTs, the authors use tree-sitter to extract ASTs and then generate unique fingerprints for each subtree, retaining only node type attributes (syntactic information) and discarding value attributes.
- Greedy Submodular Maximization: The problem of selecting exemplars to maximize CAST is formulated as a submodular maximization problem. A polynomial-time greedy algorithm is proposed, which theoretically guarantees a (1 - 1/e)-approximate solution. This algorithm iteratively selects exemplars that provide the maximum marginal gain in CAST coverage.
- Framework: The overall approach involves:
- (Optional) Coarse retrieval of candidate exemplars using lexical similarity.
- Extraction of AST fingerprints for test code and candidate exemplars.
- Construction of a co-occurrence matrix.
- Application of the greedy CAST maximization algorithm to select the top-k exemplars.
- Concatenation of selected exemplars with the test code to form an ICL prompt for the LLM.
Datasets
The paper uses the dataset from [31], which contains 948 parallel functions in Python, Java, and C++. The experiments focus on a subset of 568 functions that include unit tests for at least one language. They also evaluate on AVATAR [35], a more realistic benchmark from competitive programming sites. For code summarization, they use a dataset from CodeXGLUE [36].
Key Insights and Ideas
- Post-hoc structural knowledge injection is effective: The most significant insight is that code structural knowledge can be effectively incorporated into pre-trained LLMs during inference (post-hoc) via ICL, even if it was overlooked during the initial training phase.
- Scaling isn't enough for structural understanding: Simply scaling up LLM size or training data does not automatically lead to the emergence of sufficient code structural understanding. Explicitly incorporating this knowledge is necessary.
- Information coverage is a superior objective: The paper argues that information coverage, as approximated by CAST, is a more precise and general objective for ICL exemplar selection than traditional similarity and diversity metrics.
- Compositionality matters: CAST's focus on covering subtrees from multiple exemplars captures compositional relationships, which is crucial for understanding how different code segments contribute to the overall translation.
- CAST's robustness: CAST-F (fixed exemplar count) and CAST-A (adaptive exemplar count) consistently outperform baseline strategies across various LLMs, translation tasks, and metrics, demonstrating its generalizability and effectiveness.
Key Results
- Significant Performance Improvement: CAST-F consistently outperforms other ICL exemplar selection strategies (Random, LD, BM25, CodeBERT, Diversity) across various LLMs (Qwen2-7B, Gemma2-27B, Qwen2.5-72B) and translation tasks, achieving higher Computational Accuracy (CA) and Exact Match Accuracy (EM). For example, on Qwen2-7B, CAST-F surpasses the second-best strategy by 1.6% to 1.14%.
- Robustness across LLMs and Tasks: CAST-F shows strong performance on the AVATAR benchmark and in code summarization tasks, indicating its versatility beyond code translation.
- Scalability: CAST-F demonstrates superior scalability as the number of exemplars (shots) increases, maintaining its advantage over other methods.
- Efficiency: The greedy CAST maximization algorithm runs in polynomial time, making it practical for real-world applications.
- Reduced Failures: CAST-F leads to a lower proportion of "Wrong Answer" outcomes and a higher "Accept" rate in code translation compared to baseline strategies.
Limitations and Future Work
- Database Size and Noise Sensitivity: Performance degrades with reduced database size and structural noise (e.g., statement shuffling), indicating the importance of adequate data quality and scale.
- Future Work: The authors suggest exploring more advanced code structures beyond ASTs (e.g., CFGs, DFGs) and introducing a feedback loop during inference to dynamically refine exemplar selection based on intermediate model outputs.
Summary updated