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

GraphCodeBERT: Pre-training Code Representations with Data Flow

Daya Guo, Shuo Ren, Shuai Lu, Zhangyin Feng, Duyu Tang, Shujie Liu, Long Zhou, Nan Duan, Alexey Svyatkovskiy, Shengyu Fu, Michele Tufano, Shao Kun Deng, Colin Clement, Dawn Drain, Neel Sundaresan, Jian Yin, Daxin Jiang, Ming Zhou

Introduction

In the world of Natural Language Processing (NLP), models like BERT and GPT have revolutionized how machines understand human language. Naturally, researchers have applied these techniques to programming languages. However, code isn't just "text." While a sentence's meaning is derived from word order, a program's meaning is fundamentally derived from its structure and logic.

Previous pre-trained models for code, such as CodeBERT, largely treated source code as a simple sequence of tokens. Some attempts were made to incorporate Abstract Syntax Trees (ASTs), but ASTs are often unnecessarily deep and complex, containing "syntactic noise" that doesn't always aid semantic understanding.

GraphCodeBERT is a Transformer-based pre-trained model that moves beyond treating source code as simple text by incorporating data flow—the semantic graph of how values move between variables. By bridging the gap between literal code tokens and the underlying logic of "where a variable's value comes from," GraphCodeBERT achieves state-of-the-art performance across diverse tasks, proving that "understanding" code requires looking at how it functions, not just how it's written.

Methods: Incorporating Code Structure

GraphCodeBERT is built on the Transformer architecture but introduces key innovations to handle the inherent structure of code efficiently.

1. Data Flow Integration

Instead of using a bulky AST, the model extracts a data flow graph where nodes are variables and edges represent the "value-comes-from" relationship. This graph is flatter, more efficient, and less prone to the vanishing gradient issues associated with deep trees.

Extracting data flow from source code

2. Graph-Guided Masked Attention

To incorporate this graph into the Transformer, the authors modified the standard attention mechanism. A graph-guided "mask" ensures that nodes in the data flow only attend to the specific code tokens they represent or other variables they are logically connected to.

3. Structure-Aware Pre-training Tasks

GraphCodeBERT employs three pre-training tasks to learn both the text and the structure of the code:

GraphCodeBERT architecture and pre-training tasks
  • Masked Language Modeling (MLM): The standard task of predicting missing code tokens based on context.
  • Edge Prediction: The model is forced to predict missing edges in the data flow graph, encouraging it to learn the logical structure and dependencies within the code.
  • Node Alignment: The model predicts which code tokens correspond to which nodes in the data flow graph, aligning the literal "text" of the code with its underlying "logic."

Datasets

  • Pre-training: The model was trained on the CodeSearchNet dataset, which contains 2.3 million functions paired with natural language comments across six programming languages (Python, Java, JavaScript, PHP, Ruby, and Go).
  • Evaluation:
    • Code Search: Evaluated using the filtered CodeSearchNet corpus.
  • Clone Detection: Evaluated on BigCloneBench, a massive dataset of over 6 million true/false clone pairs.
  • Code Translation: Tested on a dataset of Java and C# project pairs.
  • Code Refinement: Evaluated on a bug-fixing dataset released by Tufano et al.

Key Insights

  • Semantics over Syntax: Data flow is a "semantic-level" structure. Unlike an AST, which changes if you add unnecessary parentheses or change a loop type, the data flow remains relatively stable for the same logic. This makes it a much more robust feature for a model to learn.
  • Attention Preference: One of the most striking findings was that when performing code search, the model's internal attention mechanism actually preferred looking at the data flow nodes over the raw code tokens. This confirms that the model actively uses logical structure to "understand" queries.

Key Results

GraphCodeBERT consistently outperformed previous state-of-the-art models (like CodeBERT and RoBERTa) across all four downstream tasks:

  • Code Search: Achieved an overall Mean Reciprocal Rank (MRR) of 0.713, a significant improvement over CodeBERT's 0.693.
  • Clone Detection: Reached an F1 score of 0.971 on BigCloneBench, outperforming specialized graph neural network models.
  • Code Translation: In Java to C# translation, it achieved a BLEU score of 80.58, showing it can better handle the mapping of logic between different languages.
  • Code Refinement: It was significantly more successful at fixing bugs (e.g., changing a * to a + in an add function) because it understood the dependency between the function name and the return variable.

Limitations and Future Work

The authors identified two primary areas for improvement:

  1. Library Knowledge: The model struggles when code uses specific external libraries (like TensorFlow). If the model hasn't seen the definition of an API like tf.io.read_file, it cannot fully grasp the data flow. Future work could involve incorporating library documentation or definitions into the pre-training phase.
  2. Generation Errors: While great at understanding, the model still makes "hallucination" errors during code generation (like translation), such as inventing variable names or missing closing brackets. The authors suggest that a dedicated, grammar-aware decoder could solve these syntax-level mistakes.

Summary updated

BibTeX
@inproceedings{guo2021graphcodebert,
  author = {Daya Guo and Shuo Ren and Shuai Lu and Zhangyin Feng and Duyu Tang and Shujie LIU and Long Zhou and Nan Duan and Alexey Svyatkovskiy and Shengyu Fu and Michele Tufano and Shao Kun Deng and Colin Clement and Dawn Drain and Neel Sundaresan and Jian Yin and Daxin Jiang and Ming Zhou},
  title = {{GraphCodeBERT: Pre-training Code Representations with Data Flow}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
  url = {https://openreview.net/forum?id=jLoC4ez43PZ},
}
Figure