Abstract
Code representation learning, which aims to encode the semantics of source code into distributed vectors, plays an important role in recent deep-learning-based models for code intelligence. Recently, many pre-trained language models for source code (e.g., CuBERT and CodeBERT) have been proposed to model the context of code and serve as a basis for downstream code intelligence tasks such as code search, code clone detection, and program translation. Current approaches typically consider the source code as a plain sequence of tokens, or inject the structure information (e.g., AST and data-flow) into the sequential model pre-training. To further explore the properties of programming languages, this paper proposes SynCoBERT, a syntax-guided multi-modal contrastive pre-training approach for better code representations. Specially, we design two novel pre-training objectives originating from the symbolic and syntactic properties of source code, i.e., Identifier Prediction (IP) and AST Edge Prediction (TEP), which are designed to predict identifiers, and edges between two nodes of AST, respectively. Meanwhile, to exploit the complementary information in semantically equivalent modalities (i.e., code, comment, AST) of the code, we propose a multi-modal contrastive learning strategy to maximize the mutual information among different modalities. Extensive experiments on four downstream tasks related to code intelligence show that SynCoBERT advances the state-of-the-art with the same pre-training corpus and model size.
Introduction
While large-scale pre-trained models like BERT have revolutionized Natural Language Processing (NLP), applying them to source code presents unique challenges. Unlike natural language, source code is highly structured and symbolic. Existing models often treat code as a flat sequence of tokens, failing to capture two critical aspects: symbolic importance and structural connectivity.
Identifiers, such as variable and function names, carry significant semantic weight but are often treated identically to language keywords. Furthermore, while some models incorporate Abstract Syntax Trees (ASTs), they frequently overlook the explicit edges between nodes that define program logic and flow.
SynCoBERT addresses these gaps by treating code as a multi-modal entity, incorporating raw source code, developer comments, and the underlying AST. By exploiting the mutual information shared between these views, the model learns a more robust representation of code semantics.
Methodology
SynCoBERT utilizes a standard Transformer encoder enhanced with three specialized pre-training objectives designed to capture the symbolic and structural properties of code.
Pre-training Objectives
- Identifier Prediction (IP): The model performs a binary classification for every token to determine if it is an identifier. This forces the model to distinguish between fixed language keywords and user-defined symbolic variables.
- AST Edge Prediction (TEP): To integrate structural knowledge without specialized graph architectures, the model predicts whether a connection exists between two masked nodes in the AST.
- Multi-Modal Masked Language Modeling (MMLM): Tokens from code, comments, and AST sequences are randomly masked and predicted, similar to standard BERT training but across multiple modalities.
Multi-Modal Contrastive Learning (MCL)
The core innovation of SynCoBERT is the MCL strategy, which maximizes the similarity between different "views" of the same code snippet. The model is trained to align the vector representations of natural language comments, source code, and ASTs. This alignment helps bridge the "semantic gap" between human descriptions and machine-executable logic.
Experimental Setup
The model was pre-trained on the CodeSearchNet dataset, comprising 2.1 million bimodal data points (code-comment pairs) and 6.4 million unimodal code snippets across six programming languages (Ruby, JavaScript, Go, Python, Java, and PHP).
Evaluation was conducted on several industry-standard benchmarks:
- Code Search: AdvTest and CodeSearchNet.
- Clone Detection: BigCloneBench and POJ-104.
- Defect Detection: Defects4J (C language).
- Program Translation: CodeTrans (e.g., C# to Java).
Results and Discussion
SynCoBERT consistently outperformed established baselines like CodeBERT and GraphCodeBERT across all tasks.
- Code Search: Achieved an average Mean Reciprocal Rank (MRR) of 74.0, compared to CodeBERT's 69.3.
- Clone Detection: Reached a MAP@R of 88.24 on POJ-104, surpassing GraphCodeBERT (85.16).
- Defect Detection: Set a new state-of-the-art with 64.50% accuracy.
- Program Translation: Improved the BLEU score for C# to Java translation by nearly 4 points over CodeBERT.
Key Insights
Ablation studies highlighted that MCL (Contrastive Learning) was the most impactful component, as it effectively aligns disparate modalities. Furthermore, the TEP objective allows the model to internalize syntax rules more deeply than simply providing the AST as input. Notably, SynCoBERT demonstrated strong zero-shot generalization on C and C# tasks, suggesting it learns generic programming semantics that transcend specific language syntax.
Conclusion
SynCoBERT demonstrates that explicitly modeling the symbolic and structural properties of code through multi-modal contrastive learning significantly enhances pre-trained representations. While currently dependent on language-specific AST parsers, future work may explore additional modalities like Data-Flow or Control-Flow Graphs and scale the model to even larger corpora.
Summary updated