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

Contrastive Code Representation Learning

Paras Jain, Ajay Jain, Tianjun Zhang, Pieter Abbeel, Joseph Gonzalez, Ion Stoica

Introduction

In Natural Language Processing (NLP), models like BERT and RoBERTa have transformed the field by learning to "fill in the blanks" through Masked Language Modeling (MLM). When these techniques were first applied to source code, researchers expected similar breakthroughs. However, code possesses unique properties that distinguish it from natural language. While a synonym in a sentence might subtly shift a nuance, renaming a variable in a program changes its surface appearance entirely while leaving its underlying logic untouched.

In the paper "Contrastive Code Representation Learning," researchers from UC Berkeley introduce ContraCode, a framework that shifts the focus from predicting tokens to understanding program behavior.

The Problem: Form vs. Function

Most existing "Code-BERT" models rely on MLM, learning by reconstructing missing pieces of code from their context. This approach often leads models to become obsessed with form (syntax and style) rather than function (logic and semantics).

This reliance on surface-level features creates a significant vulnerability: adversarial sensitivity. A model trained via MLM might correctly identify two identical functions as clones, but its confidence often collapses if variables are renamed or minor refactors are applied. For critical tools like malware detectors or bug finders, this fragility is a dealbreaker.

Methodology: The ContraCode Framework

ContraCode treats code representation as a retrieval task rather than a reconstruction task, utilizing three core components:

  1. Compiler-based Augmentation: The researchers used source-to-source compilers (e.g., Babel and Terser for JavaScript) to automatically generate variants of a program. By applying 11 different "correct-by-construction" transforms—such as variable renaming, dead code removal, and constant folding—they created code that looks different but executes identically.
  2. Contrastive Pre-training: Using the Momentum Contrast (MoCo) framework, the model is presented with a "query" program and must identify its "positive" pair (a transformed version of itself) from a large queue of "negatives" (transformed versions of other programs).
  3. The Objective: The model learns to map functionally equivalent programs to the same region in a high-dimensional embedding space while pushing functionally distinct programs apart.

Key Insights: Visualizing the Embedding Space

By forcing the model to recognize a program despite obfuscation, ContraCode learns underlying logic rather than just naming conventions.

UMAP visualizations comparing RoBERTa and ContraCode embeddings. RoBERTa separates functionally identical variants (Program 19 A and B) based on variable names, while ContraCode clusters them together.

As seen in the UMAP visualizations, RoBERTa clusters code based on surface-level features like variable names. In contrast, ContraCode successfully groups functionally identical variants together, demonstrating a "robustness dividend": by learning deeper semantic representations, the model becomes more effective at natural tasks like predicting method names and variable types.

Key Results and Performance

ContraCode demonstrates superior performance across several benchmarks:

  • Adversarial Robustness: In zero-shot code clone detection, ContraCode outperformed RoBERTa by 39% AUROC under adversarial conditions.
  • Downstream Tasks: The model improved TypeScript type prediction accuracy by 9 percentage points and increased the F1 score for code summarization (predicting method names) by up to 8 points compared to MLM baselines.
Average Precision of ContraCode vs. RoBERTa as the number of adversarial code edits increases. ContraCode maintains high precision while RoBERTa's performance drops sharply.

As illustrated above, even when 16 different edits are applied to the code, ContraCode maintains high accuracy while RoBERTa’s performance plummets toward the level of a random classifier.

Conclusion and Future Work

ContraCode proves that compilers can serve as the ultimate data augmentors for code, providing a mathematically grounded way to create diverse training data. While the current implementation is focused on the JavaScript ecosystem, the principles are language-agnostic. Future work may involve "hard negative mining"—selecting programs that look similar but behave differently—to further sharpen the model's semantic boundaries and expanding support to languages like C++, Java, and Python.

Paper Citation: Paras Jain, Ajay Jain, Tianjun Zhang, Pieter Abbeel, Joseph E. Gonzalez, and Ion Stoica.

  1. Contrastive Code Representation Learning. In Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing (EMNLP).

Summary updated

BibTeX
@inproceedings{jain2021contrastive,
  author = {Paras Jain and Ajay Jain and Tianjun Zhang and Pieter Abbeel and Joseph Gonzalez and Ion Stoica},
  title = {{Contrastive Code Representation Learning}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.482},
}
Figure