NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

GraphCode2Vec: Generic Code Embedding via Lexical and Program Dependence Analyses

Wei Ma, Mengjie Zhao, Ezekiel O. Soremekun, Qiang Hu, Jie M. Zhang, Mike Papadakis, Maxime Cordy, Xiaofei Xie, Yves Le Traon · DOI

Introduction

Applying machine learning to Software Engineering (SE) tasks—such as method name prediction or bug identification—requires a robust method for "code embedding," the process of converting source code into mathematical vectors. Code presents a unique challenge because it possesses two distinct "faces": lexical syntax (the textual tokens and keywords) and program semantics (the logical flow of data and control).

Most existing models prioritize one aspect over the other. Large language models often treat code as natural language, potentially missing deep logical dependencies. Conversely, models focusing on dependencies frequently rely on imprecise heuristics. GraphCode2Vec addresses this gap by merging precise program dependence analysis with lexical syntax through a self-supervised pre-training approach. The resulting "task-agnostic" embeddings outperform syntax-only models and remain competitive with massive transformer-based models like CodeBERT, despite being 50x smaller.

Methodology

The GraphCode2Vec pipeline integrates lexical and structural information through three primary stages:

  1. Lexical Embedding: Java code is first converted into Jimple, an intermediate representation. To handle "Out-of-Vocabulary" issues common with unique variable names, the model tokenizes instructions into subwords. These are then processed via a Bi-directional LSTM to generate initial instruction-level embeddings.
  2. Dependence Embedding: Using the Soot framework, the model performs rigorous static analysis to construct a Program Dependence Graph (PDG). This graph explicitly maps data flow, control flow, and method calls. The lexical embeddings from the first stage serve as the initial features for the nodes within this PDG.
  3. Graph Neural Networks (GNNs): Graph Attention Networks (GATs) are employed to propagate information across the PDG. This allows the representation of a specific line of code to incorporate the context of the logic that influences it or is influenced by it.
The GraphCode2Vec pipeline, integrating lexical and dependence embeddings.

To ensure the model is generic, it is pre-trained on unlabeled code using three self-supervised strategies: node classification (predicting instruction types), context prediction (predicting masked nodes), and Variational Graph Encoding (VGAE) to reconstruct the graph structure.

Experimental Setup

The model was pre-trained on the Jimple-Graph dataset, which includes approximately 3.5 million methods from 1,976 Java libraries. Evaluation was conducted across several established benchmarks:

  • Java-Small: Method name prediction.
  • Java250: Solution classification.
  • Defects4J: Mutant prediction and automated patch classification.
  • LeetCode & Concurrency datasets: Probing analysis to verify the learning of syntax vs. logic.

Key Results

GraphCode2Vec demonstrates significant improvements over existing baselines while maintaining high efficiency. With only 2.8 million parameters—compared to CodeBERT’s 124 million—it achieves comparable or superior performance on several downstream tasks.

Performance comparison of GraphCode2Vec against other generic code embedding models.

Key performance highlights include:

  • Method Name Prediction: An F1-score of 0.58, representing an 18.03% improvement over the Code2Seq baseline.
  • Solution Classification: A 0.97 F1-score, outperforming task-specific CNN models by approximately 40%.
  • Competitive Performance: The model outperformed GraphCodeBERT in method name prediction and patch classification, remaining within 1% of its performance on other tasks.
  • Pre-training Impact: Self-supervised pre-training enhanced model effectiveness by up to 28% compared to training from scratch.

Probing analysis confirmed that the lexical components excel at syntax-heavy tasks, while the dependence embeddings are crucial for logical understanding, such as identifying concurrency bugs.

Discussion and Future Work

A core strength of GraphCode2Vec is its use of precise static analysis rather than string-matching heuristics. This allows the model to recognize behavioral identity even when code appearance differs significantly. However, the reliance on Jimple and the Soot framework currently limits the implementation to Java. Furthermore, by operating on intermediate representations, the model ignores natural language features like code comments, which are leveraged by models like CodeBERT.

Future research will focus on extending the model to support multiple programming languages and integrating natural language context (comments and documentation) alongside structural and semantic features. While static analysis introduces some computational overhead compared to simple tokenization, the resulting gains in precision and model efficiency offer a compelling path forward for generic code embeddings.

Summary updated

BibTeX
@inproceedings{ma2021graphcode,
  author = {Wei Ma and Mengjie Zhao and Ezekiel O. Soremekun and Qiang Hu and Jie M. Zhang and Mike Papadakis and Maxime Cordy and Xiaofei Xie and Yves Le Traon},
  title = {{GraphCode2Vec: Generic Code Embedding via Lexical and Program Dependence Analyses}},
  booktitle = {MSR},
  year = {2021},
  doi = {10.1145/3524842.3528456},
}
Figure