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

CodeBERT: A Pre-Trained Model for Programming and Natural Languages

Zhangyin Feng, Daya Guo, Duyu Tang, Nan Duan, Xiaocheng Feng, Ming Gong, Linjun Shou, Bing Qin, Ting Liu, Daxin Jiang

Introduction

While large-scale language models like BERT and RoBERTa have revolutionized Natural Language Processing (NLP), they are primarily designed for human languages. Programming languages (PL), however, possess unique structural and semantic properties that traditional models often fail to capture. Most existing solutions are either unimodal—trained exclusively on text or code—or restricted to a single programming language.

CodeBERT addresses this gap as a bimodal pre-trained model that learns joint representations for both natural language (NL) and programming languages. By training on massive datasets across six programming languages, it establishes a semantic bridge between how developers describe tasks in English (e.g., "sort an array") and their actual implementations in languages like Python, Java, or JavaScript.

Methodology

CodeBERT utilizes a multi-layer bidirectional Transformer architecture based on the RoBERTa-base configuration (125 million parameters). The core innovation lies in its hybrid pre-training approach, which combines bimodal data (NL-PL pairs) with unimodal data (raw code).

Hybrid Pre-training Objectives

The model is trained using two primary objectives:

  1. Masked Language Modeling (MLM): Similar to BERT, the model predicts missing tokens in a sequence of NL-PL pairs.
  2. Replaced Token Detection (RTD): Borrowing from the ELECTRA framework, CodeBERT uses "generators" to create plausible but incorrect code or text tokens. The main model acts as a "discriminator" that must identify which tokens are original and which are synthetic.
The CodeBERT pre-training architecture using Replaced Token Detection (RTD) across NL and PL modalities.

By leveraging vast amounts of raw unimodal code to train better generators for the RTD task, the authors ensure the final discriminator is significantly more robust in understanding code logic.

Datasets and Benchmarks

The authors utilized the CodeSearchNet corpus, encompassing six programming languages: Python, Java, JavaScript, PHP, Ruby, and Go. The training set included approximately 2.1 million bimodal pairs (functions with documentation) and 6.4 million unimodal functions.

To evaluate the model's semantic depth, the authors introduced an NL-PL Probing dataset. This cloze-style benchmark tests if the model understands specific logical concepts, such as distinguishing between max and min operations in both text and code. Furthermore, the model was tested on C# to evaluate its ability to generalize to languages not seen during pre-training.

Key Results and Performance

CodeBERT achieves state-of-the-art performance across several critical tasks, demonstrating the power of treating code as a distinct modality.

In code search tasks, CodeBERT achieved a macro-average Mean Reciprocal Rank (MRR) of 0.7603 across six languages. This significantly outperforms standard RoBERTa (0.6972) and other specialized code models.

Comparison of CodeBERT variants against baseline models on the natural language code search task (MRR).

Documentation Generation and Probing

  • Documentation Generation: In code-to-NL tasks, CodeBERT achieved an overall BLEU score of 17.83, a notable improvement over standard Seq2Seq models.
  • Zero-Shot Probing: In fixed-parameter probing tests, CodeBERT correctly identified logical tokens 85.66% of the time, compared to 62.45% for RoBERTa. This confirms that the model understands the semantic alignment between docstrings and functional logic.
  • Cross-Lingual Generalization: On the unseen C# dataset, CodeBERT improved the BLEU score by 2.55 points over RoBERTa, proving its ability to transfer learned logic across different syntax styles.

Limitations and Future Work

While CodeBERT represents a significant leap forward, the authors identify areas for improvement. Currently, the model treats code as a linear sequence of tokens. Incorporating the Abstract Syntax Tree (AST)—the hierarchical structure of code—could further enhance structural awareness. Additionally, while MLM and RTD are excellent for understanding, future iterations may include generation-specific objectives to further improve automated code synthesis.

Summary updated

BibTeX
@inproceedings{feng2020codebert,
  author = {Zhangyin Feng and Daya Guo and Duyu Tang and Nan Duan and Xiaocheng Feng and Ming Gong and Linjun Shou and Bing Qin and Ting Liu and Daxin Jiang and Ming Zhou},
  title = {{CodeBERT: A Pre-Trained Model for Programming and Natural Languages}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2020},
  doi = {10.18653/v1/2020.findings-emnlp.139},
}
Figure