Multi-modal Attention Network Learning for Semantic Source Code Retrieval

Yao Wan, Jingdong Shu, Yulei Sui, Guandong Xu, Zhou Zhao, Jian Wu, Philip S. Yu

Abstract

Semantic code retrieval is essential for efficient software development and reuse. However, existing models often treat source code as plain text, neglecting its complex structural and logical properties. We present the Multi-Modal Attention Network (MMAN), a novel approach that captures code semantics across three distinct levels: lexical (tokens), syntactic (Abstract Syntax Trees), and structural (Control-Flow Graphs). MMAN utilizes a specialized multi-modal attention fusion layer to weigh these features dynamically, projecting them into a unified semantic space alongside natural language queries. Evaluation on a large-scale GitHub C dataset shows that MMAN achieves state-of-the-art performance, with a 26% relative improvement in SuccessRate@1 over previous benchmarks.

Introduction

Software developers frequently search for existing code fragments to accelerate development. Effective semantic retrieval requires understanding the intent behind a query and matching it with the functional logic of code snippets. Traditional approaches primarily focus on lexical features, such as method names and tokens, treating code similarly to natural language. This "shallow" representation fails to capture the rich semantics embedded in a program's structure and execution flow. Furthermore, many deep learning models operate as "black boxes," offering little insight into why specific code snippets are retrieved.

To address these challenges, we propose MMAN, a multi-modal framework that leverages the inherent structure of source code to improve retrieval accuracy and explainability.

Methodology

MMAN represents source code through three modalities, each capturing a different facet of program semantics:

  1. Lexical Level (Tokens): A standard Long Short-Term Memory (LSTM) network processes the sequential stream of code tokens to capture textual patterns.
  2. Syntactic Level (AST): A Tree-LSTM is employed to model the hierarchical, nested structure of the code's Abstract Syntax Tree.
  3. Structural Level (CFG): A Gated Graph Neural Network (GGNN) represents the Control-Flow Graph, capturing the logical execution paths of the program.
Figure 1: Illustration of a code snippet and its corresponding Abstract Syntax Tree (AST) and Control-Flow Graph (CFG) representations.

Multi-Modal Attention Fusion

The core innovation of MMAN is the Multi-Modal Attention Fusion Layer. Rather than simple concatenation, this layer uses an attention mechanism to assign importance weights to different parts of each modality based on the context. For instance, the model may prioritize a specific function name in the token stream while focusing on a particular conditional branch in the CFG.

Figure 2: The MMAN architecture featuring joint representation learning and coordinate mapping into a shared semantic space.

The model is trained using a ranking loss function that projects both the multi-modal code representation and the natural language query into a shared "semantic space." In this space, code snippets that are functionally similar to a query are positioned closer together.

Experimental Setup

The model was evaluated using a large-scale real-world dataset:

  • GitHub C Corpus: Comprising 28,527 C code snippets harvested from GitHub repositories with at least two stars.
  • Preprocessing: The Clang compiler was used to generate ASTs, and the SVF static analysis tool was utilized to extract CFGs.
  • Evaluation: The dataset was split into 27,527 training samples and 1,000 evaluation samples. Documentation comments served as the ground truth for natural language queries.

Results and Analysis

MMAN demonstrated superior performance across standard retrieval metrics compared to state-of-the-art methods like DeepCS and CodeHow. MMAN achieved a SuccessRate@1 of 0.347 and a Mean Reciprocal Rank (MRR) of 0.452, representing significant gains over the DeepCS baseline (0.275 and 0.377, respectively).

Key Insights

  • Structural Synergy: Ablation studies confirmed that removing either the AST or CFG modalities significantly degraded performance, proving that structural and logical features provide complementary information to lexical tokens.
  • Explainability through Attention: Visualizing attention weights revealed that the model focuses on semantically relevant code elements. For a query regarding "printing," the model correctly highlighted printf calls and related function names.
  • Attention Mechanisms: The authors found that using a sigmoid function for CFG attention outperformed the standard softmax. This suggests that multiple nodes in a graph may be equally relevant to a query, a scenario better handled by sigmoid's non-competitive weighting.

Conclusion

MMAN advances semantic code retrieval by integrating lexical, syntactic, and structural code features through an attention-based fusion mechanism. While the model shows significant promise, its current reliance on compilable code for CFG extraction limits its applicability to partial snippets (e.g., from StackOverflow). Future research will explore extending this multi-modal approach to other programming languages like Java and Python and conducting human-centric studies to evaluate developer satisfaction in real-world scenarios.

Summary updated

BibTeX
@inproceedings{wan2019multi,
  author = {Yao Wan and Jingdong Shu and Yulei Sui and Guandong Xu and Zhou Zhao and Jian Wu and Philip S. Yu},
  title = {{Multi-modal Attention Network Learning for Semantic Source Code Retrieval}},
  booktitle = {ASE},
  year = {2019},
  doi = {10.1109/ase.2019.00012},
}
Figure