Abstract
Semantic code search often relies on joint embedding spaces that overlook fine-grained, local interactions between natural language queries and code snippets. We present MP-CAT, a neural architecture that integrates global sequence-level embeddings with local token-level multi-perspective matching. By incorporating structural information from Abstract Syntax Trees (ASTs) and fusing global and local features, MP-CAT achieves significant performance improvements over standard baselines on the CoNaLa Python dataset, demonstrating the value of capturing both high-level context and low-level token alignments.
Introduction
Semantic code search is the task of retrieving the most relevant code snippet from a repository given a natural language query, such as "sort dictionary by value." Traditionally, this is framed as a cross-modal ranking problem where code and text are encoded independently into a shared vector space. Similarity is then calculated using global metrics like cosine distance. However, this "global-only" approach fails to capture local, token-level alignments—such as matching a specific variable name in code to a specific noun in a query—limiting the model's ability to understand nuanced relationships between the two modalities.
Methodology
To address these limitations, the authors propose MP-CAT, a framework that captures both global context and local token-by-token interactions. The architecture consists of several key modules designed to process code sequences, AST sequences, and description sequences.
The framework was developed through four iterative model variations:
- CT (Baseline): A standard model using bi-directional LSTMs to encode code and text tokens independently, followed by max-pooling and L2 distance computation.
- CAT (Code + AST + Text): Enhances the baseline by incorporating structural syntax. Code is parsed into an Abstract Syntax Tree (AST), flattened into a sequence, and encoded alongside raw code tokens.
- MP (Multi-Perspective): Utilizes Bilateral Multi-Perspective Matching (BiMPM) to compare every token in the code/AST sequence with every token in the text sequence across four matching strategies: Full, Maxpool, Attentive, and Max-Attentive.
- MP-CAT (Combined): The final architecture that concatenates global embeddings (from CAT) with local token-matching features (from MP) to produce a final similarity score.
Experimental Evaluation
The models were evaluated using the CoNaLa Dataset, a benchmark for Python code snippets and English descriptions. The study focused on the manually curated parallel corpus, containing 2,379 training examples and 500 test examples.
Key Insights
- Global Context is Essential: Interestingly, the MP model alone performed worse than the baseline. This suggests that while local matching is powerful, global sequence encoders are necessary to provide the overall context for both code and text.
- Structural Syntax Matters: Integrating AST features (the CAT model) provided a substantial performance boost, as the explicit tree structure reduces the ambiguity inherent in raw code text.
- Synergy of Scales: The best results were achieved by combining global representations with local multi-perspective matching, particularly for complex queries involving multiple data structures.
Results and Discussion
MP-CAT outperformed all other variations across standard retrieval metrics. As shown in the performance table below, it achieved the highest Mean Reciprocal Rank (MRR) and superior Recall at various thresholds.
Quantitatively, MP-CAT reached an MRR of 0.220, a significant improvement over the CT baseline (0.172). Qualitatively, manual reviews indicated that while baselines often identified the correct libraries, they frequently failed to pinpoint the exact command. MP-CAT was more successful in retrieving precise snippets for complex, multi-step queries.
However, this performance comes with a significant computational trade-off. MP-CAT required approximately 240,000 seconds for training, compared to only 4,600 seconds for the baseline CT model—a roughly 50x increase in overhead.
Conclusion
MP-CAT demonstrates that fusing global and local features, supplemented by structural AST information, significantly advances semantic code search. While the computational cost of multi-perspective matching is high, the framework is language-agnostic and holds promise for application across various programming languages where AST parsers are available. Future work may focus on optimizing the efficiency of the local matching modules to make the architecture more scalable for larger datasets.
Summary updated