CRaDLe: Deep code retrieval based on semantic Dependency Learning

Wenchao Gu, Zongjie Li, Cuiyun Gao, Chaozheng Wang, Hongyu Zhang, Zenglin Xu, Michael R. Lyu · DOI

Abstract

Effective code retrieval is hindered by the "semantic gap" between descriptive natural language queries and structured source code. This paper presents CRaDLe (Code Retrieval based on semantic Dependency Learning), a framework that utilizes statement-level dependencies to bridge this gap. By encoding functional relationships through Program Dependency Graphs (PDGs), CRaDLe captures execution logic more effectively than traditional token-based or syntax-tree methods. Experimental results on large-scale Python benchmarks demonstrate that CRaDLe achieves a 36.38% improvement in retrieval accuracy (R@1) over state-of-the-art baselines.

Introduction

Programmers frequently rely on natural language queries to find relevant code snippets. However, the inherent ambiguity of natural language contrasts sharply with the technical structure of source code. Existing neural code retrieval methods often treat code as flat sequences of tokens, losing structural context, or utilize Abstract Syntax Trees (ASTs), which can be overly granular and difficult for models to process efficiently.

Overview of the CRaDLe code retrieval framework showing the interaction between the programmer, the embedding space, and the repository.

CRaDLe addresses these challenges by focusing on statement-level dependencies. It identifies the "sweet spot" of code representation: capturing the logical flow (the how and why) without the noise associated with low-level syntax or exhaustive execution paths.

Methodology

The CRaDLe architecture consists of three primary modules designed to map code and natural language into a shared embedding space.

Code Encoder

The code encoder tokenizes source code into discrete statements. It employs an attention mechanism to weight the importance of tokens within each statement, allowing the model to prioritize unique identifiers and API calls over boilerplate code. Simultaneously, the system extracts a Program Dependency Graph (PDG) to model:

  • Data Dependency: Tracking variable definitions and their subsequent uses.
  • Control Dependency: Identifying conditional logic (e.g., if statements) that governs execution.

These dependencies are processed through a Multi-Layer Perceptron (MLP) and fused with token semantics using a Bi-directional Long Short-Term Memory (Bi-LSTM) network.

Detailed architecture of the CRaDLe model, illustrating the fusion of statement-level tokens and dependency matrices into a unified code vector.

Description Encoder and Similarity Measurement

The natural language query is processed using a Bi-LSTM with max-pooling to capture the global search intent. Both the code and description vectors are projected into a unified vector space. The model is trained using a triplet loss function, which minimizes the cosine distance between a code snippet and its corresponding description while maximizing the distance from mismatched pairs.

Evaluation and Results

CRaDLe was evaluated on two major Python benchmarks: CodeSearchNet (400,000+ pairs) and Code2Seq (330,000+ pairs).

The model demonstrated significant performance gains over baselines such as CODEnn and UNIF. On the CodeSearchNet dataset, CRaDLe achieved an R@1 of 0.791, representing a 36.38% improvement over the best baseline. Furthermore, it achieved a Mean Reciprocal Rank (MRR) of 0.843, indicating that the correct snippet is typically the top result.

Discussion and Conclusion

The success of CRaDLe highlights the importance of structural dependencies in code understanding. By weighting statements based on their data and control flow significance, the model effectively identifies semantically "heavy" code segments.

Despite its strengths, CRaDLe faces limitations in handling complex mathematical logic where domain-specific jargon (e.g., "Cartesian coordinates") does not appear in the code text. Future work will investigate the integration of external knowledge bases and Graph Neural Networks (GNNs) to further refine dependency encoding and handle inter-procedural analysis.

Summary updated

BibTeX
@article{gu2021cradle,
  author = {Wenchao Gu and Zongjie Li and Cuiyun Gao and Chaozheng Wang and Hongyu Zhang and Zenglin Xu and Michael R. Lyu},
  title = {{CRaDLe: Deep code retrieval based on semantic Dependency Learning}},
  journal = {Neural Networks},
  year = {2021},
  doi = {10.1016/j.neunet.2021.04.019},
}
Figure