CODE REPRESENTATION LEARNING AT SCALE

Dejiao Zhang, Wasi Uddin Ahmad, Ming Tan, Hantian Ding, Ramesh Nallapati, Dan Roth, Xiaofei Ma, Bing Xiang

Introduction

While Large Language Models (LLMs) have revolutionized code generation, the field of code representation—creating vector embeddings for tasks like semantic search, clone detection, and code classification—has historically lagged behind. Most existing code encoders are relatively small and rely on pretraining objectives borrowed directly from Natural Language Processing (NLP), such as the standard BERT "80-10-10" masking rule. However, these methods are often suboptimal for source code; random token replacement can disrupt the fragile syntax and structural integrity of a program, complicating the learning task and yielding inferior embeddings. This paper introduces CodeSage, a new family of high-performance code embedding models (up to 1.3B parameters) designed to address these challenges through a specialized two-stage training scheme.

Methodology

CodeSage utilizes a two-stage pretraining pipeline designed specifically for the unique structural nature of source code.

Stage 1: Token-level Denoising

The model first learns the underlying grammar and structure of code through two tasks:

  • Identifier Deobfuscation (DOBF): The model predicts the original names of variables and functions that have been replaced with generic placeholders.
  • Full Mask MLM: Unlike standard BERT, which replaces some masked tokens with random words, CodeSage employs a "Full Mask" approach—replacing all selected tokens with a [MASK] tag to avoid introducing syntactically nonsensical noise.

Stage 2: Sequence-level Bimodal Contrastive Learning

The model is then refined using pairs of natural language text and code (e.g., a function and its docstring). To prevent the model from relying on superficial shortcuts like function name matching, the authors introduce:

  • Hard Positives: Removing function signatures and return statements from the code, forcing the model to understand the internal logic of the function body.
  • Hard Negatives: Utilizing an unsupervised, distance-based method to identify challenging incorrect matches in the embedding space during training.
Overview of the CodeSage training pipeline and dataset statistics across major programming languages.

Experimental Setup

The models were trained on The Stack, a massive 6TB dataset of permissively licensed source code, filtered to 237 million code files and 75 million (text, code) pairs across nine major programming languages. Evaluation was conducted on Code2Code search (extending CodeNet), NL2Code search (CoSQA, AdvTest, CodeSearchNet), and various classification tasks including defect detection and runtime error prediction.

Results and Discussion

CodeSage sets new benchmarks for code embeddings across multiple tasks. In Code2Code search, CodeSage-Small (130M) outperformed UnixCoder by 23% relatively, while CodeSage-Large (1.3B) achieved a MAP score of 38.51, significantly exceeding OpenAI’s text-embedding-ada-002 (27.33).

The Suboptimality of the 80-10-10 Rule

A key insight of this work is that the standard NLP "80-10-10" masking rule is detrimental to code representation. Because code syntax is highly structured, replacing a keyword like if with while breaks the Abstract Syntax Tree (AST). As shown in the figure below, the "Full Masking" strategy consistently outperforms the standard rule across all model scales and evaluation metrics, preserving the structural integrity of the context during training.

Performance comparison of the standard 80-10-10 masking rule versus the proposed Full Mask strategy across different model sizes and tasks.

Scaling and Generalization

CodeSage demonstrates strong scaling laws; performance on downstream tasks improves consistently as model size increases from 130M to 1.3B parameters. Furthermore, the model shows exceptional generalization on the AdvTest benchmark—which uses obfuscated code to prevent simple keyword matching—achieving an MRR of 52.67, compared to 27.32 for UnixCoder and 38.08 for OpenAI's model.

Conclusion

CodeSage represents a significant advancement in code representation learning. By combining large-scale pretraining with code-specific denoising and contrastive learning strategies, the authors demonstrate that encoders can achieve state-of-the-art performance that scales with model size. While CodeSage is not designed for generative tasks, its success in semantic search and classification highlights the importance of structural awareness in code embeddings.

Summary updated

BibTeX
@inproceedings{zhang2024code,
  author = {Dejiao Zhang and Wasi Uddin Ahmad and Ming Tan and Hantian Ding and Ramesh Nallapati and Dan Roth and Xiaofei Ma and Bing Xiang},
  title = {{CODE REPRESENTATION LEARNING AT SCALE}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=vfzRRjumpX},
}
Figure