Source-linked AI summary
Self-Supervised Contrastive Learning for Code Retrieval and Summarization via Semantic-Preserving Transformations
Nghi D. Q. Bui, Yijun Yu, Lingxiao Jiang
TL;DR
Code retrieval and other code-modeling tasks need semantic training examples, but collecting and labeling them is difficult and heuristic labels can be noisy. Corder uses contrastive learning on semantically equivalent program transformations, and its pretrained models outperform most baselines across code-to-code retrieval, text-to-code retrieval, and code summarization.
Problem
Building code-retrieval models needs many labeled examples of semantically equivalent or nonequivalent snippets, but manual labeling is costly and heuristics based on names or comments can be noisy.
Source: §1 · p. 1Method
Corder trains code models contrastively on syntactically diverse, semantically equivalent snippets generated by program transformations.
Source: §1 · pp. 1–2, §3.2.1 · p. 3Results
Corder outperforms most baselines across code-to-code retrieval, text-to-code retrieval, and code summarization.
Source: §5.6 · p. 8Takeaways & Limitations
Corder pretraining can produce code representations for retrieval and support fine-tuning for downstream tasks including text-to-code retrieval and code summarization.
Source: §1 · p. 2, §7 · p. 9Comparisons across pretext tasks are constrained because adapting some tasks to all encoders requires configuration choices, and the authors cannot guarantee unbiased settings.
Source: §5.6 · p. 8
Abstract
from arXiv · showhide
We propose Corder, a self-supervised contrastive learning framework for source code model. Corder is designed to alleviate the need of labeled data for code retrieval and code summarization tasks. The pre-trained model of Corder can be used in two ways: (1) it can produce vector representation of code which can be applied to code retrieval tasks that do not have labeled data; (2) it can be used in a fine-tuning process for tasks that might still require label data such as code summarization. The key innovation is that we train the source code model by asking it to recognize similar and dissimilar code snippets through a contrastive learning objective. To do so, we use a set of semantic-preserving transformation operators to generate code snippets that are syntactically diverse but semantically equivalent. Through extensive experiments, we have shown that the code models pretrained by Corder substantially outperform the other baselines for code-to-code retrieval, text-to-code retrieval, and code-to-text summarization tasks.
1 Introduction
Corder addresses the need for labeled code data by learning from semantically equivalent program transformations rather than noisy heuristics. Its pretraining supports code retrieval and fine-tuning for downstream tasks, and the paper reports gains over other pretraining approaches and baselines.
1 Introduction
- Code retrieval needs large collections of semantically equivalent or nonequivalent snippets, but labeling them requires substantial human effort or noisy heuristics.Test-based labeling incurs code-execution costs, while shared comments, method names, or documents do not guarantee semantic equivalence.Source: §1 · p. 1
- Corder trains neural networks to distinguish semantically equivalent snippets generated by program transformations from dissimilar code.Transformations create syntactically different versions of a snippet while preserving its semantics.Source: §1 · pp. 1–2
- Corder’s pretrained encoder produces code vectors for retrieval and can be fine-tuned with a small amount of labeled data for downstream tasks.The paper evaluates code-to-code retrieval, text-to-code retrieval, and code summarization.Source: §1 · p. 2
- Corder-pretrained models outperform other pretext-task models for unsupervised code-to-code retrieval and perform better than scratch-trained models and other pretext tasks for downstream tasks.The introduction names text-to-code retrieval and code summarization as downstream tasks.Source: §1 · p. 2
2 Related Work
Prior code models address many software-engineering tasks, but the related work describes limited efforts to learn source-code models from unlabeled data. Existing approaches include task-specific self-supervision and language-model pretraining that, the authors argue, misses code syntax and semantics.
Deep Learning Models of Code
- Earlier code models use graph neural networks over dependency-enriched ASTs or recurrent networks over paths and flattened subtrees to learn code representations.The related work names Allamanis et al. [3], Code2vec [6], Code2seq [4], and ASTNN.Source: §2 · p. 2
- Surveys on code embeddings [17] report a need to reduce labeled-data requirements and encourage learning source-code models from unlabeled data.Source: §2 · p. 2
- The cited self-supervised program-repair approach [73] is task-specific, while language-model pretraining [23] [34] is described as missing code syntax and semantic features.Source: §2 · p. 2
3 Approach
Corder trains a code encoder to bring together transformed versions of the same snippet while separating snippets from different originals. Its pretext task uses semantic-preserving program transformations and a contrastive objective.
3.1 Approach Overview
- Corder transforms unlabeled snippets into positive pairs, encodes them as vectors, and uses contrastive prediction to distinguish same-origin variants from other snippets.The framework combines a program transformation module, a shared encoder, and a contrastive loss.Source: §3.1 · pp. 2–3, Figure 2 · p. 3
3.2 Approach Details
- For each minibatch, two randomly selected transformations generate variants of each snippet; the shared encoder maps them to vectors, and NCE trains on the resulting pairs.Other transformed snippets in the minibatch serve as negatives, and pair losses are computed in both directions.Source: §3.2 · p. 3
3.2.1 Program Transformation Operators.
- Corder uses variable renaming, dead-code insertion, independent statement permutation, loop exchange, and switch-to-if conversion to create semantically preserved variants with different code structures.The operators alter textual information or AST structure in different ways; statement permutation is restricted to statements without dependencies.Source: §3.2.1 · pp. 3–4
4 Use Cases
Corder’s pretrained encoders support direct code-vector retrieval and initialization of code encoders for supervised downstream tasks. The section describes their use in code-to-code search, text-to-code retrieval, and code summarization.
4 Use Cases
- The pretrained Corder models are intended for three downstream tasks: code-to-code retrieval, text-to-code retrieval, and code summarization.The encoders are used either to produce code vectors or to initialize supervised models.Source: §4 · p. 4
4.1 Using the Pre-trained Encoders to Produce Code Vectors for Downstream Task
- For code-to-code search, Corder maps codebase snippets and a code query to vectors, then retrieves the query’s top-k nearest neighbors by cosine similarity.The retrieved candidates are intended to be semantically equivalent to the query.Source: §4.1.1 · p. 4, §4.1 · p. 4
4.2 Fine-Tuning the Encoders for Supervised Learning Downstream Tasks
- For supervised downstream tasks, the Corder-pretrained encoder weights initialize the model, rather than starting the code encoder from random weights.The paper describes this transfer as a use of pretraining followed by fine-tuning.Source: §4.2 · p. 4
4.2.1 Text-to-Code Retrieval
- For text-to-code retrieval, the pretrained code encoder is paired with a randomly initialized text encoder; for code summarization, the pretrained encoder initializes the source-code encoder.Text and code embeddings are mapped into a shared space for retrieval.Source: §4.2.1 · pp. 4–5, §4.2.2 · p. 5
5 Empirical Evaluation
The evaluation is designed to test pretrained Corder models on code-to-code search and their use in fine-tuning for text-to-code search and code summarization. It describes the pretraining data, encoder choices, and research questions.
5.1.1 Data Preparation
- JavaSmall contains about 700k examples from 11 GitHub Java projects, and JavaMed contains about 4M examples from 1,000 top-starred GitHub Java projects.These datasets train the Corder pretext task; they are not the evaluation datasets.Source: §5.1.1 · p. 5
5.1.2 Encoders
- The evaluation compares AST-based Code2vec and TBCNN encoders with token-based two-layer Bi-LSTM and Transformer encoders.All techniques use node-type and token information to initialize AST nodes; the described embedding dimensions are 128.Source: §5.1.2 · p. 5
5.1.3 Research Questions
- The research questions ask whether pretrained code vectors help unsupervised code-to-code search and whether pretraining improves fine-tuned text-to-code search and code summarization.The latter comparison is against training downstream models from scratch.Source: §5.1.3 · p. 5
5.2 Using Pre-trained Encoders to Produce Code Representations for Code-to-Code Retrieval
This section evaluates Corder-pretrained code representations for code-to-code retrieval against retrieval systems and alternative pretext tasks. Code2vec and BiLSTM pretrained with Corder on JavaMed outperform FaCoy, while Corder also outperforms MNP and Masked-LM pretraining.
5.2.1 Datasets, Metrics, and Baselines
- Code-to-code retrieval takes a code snippet as query and seeks semantically related snippets from a candidate collection; OJ and BigCloneBench are used for evaluation.The paper motivates both datasets by their use in detecting semantically duplicated or similar code.Source: §5.2.1 · pp. 5–6
- OJ queries are sampled as 50 programs from each of 104 classes, while BigCloneBench evaluation samples clone and non-clone pairs and uses 5,000 clone-pair queries.OJ has 5,200 total queries; results are limited to the top 10 for both datasets.Source: §5.2.1 · p. 6
- The comparison includes Word2vec, Doc2vec, ElasticSearch, FaCoy, and encoders pretrained with masked language modeling or method-name prediction.The Transformer and Code2vec pretraining baselines use the JavaMed dataset.Source: §5.2.1 · p. 6, §5.2.2 · p. 6
5.2.2 Results
- Corder-pretrained Code2vec and BiLSTM on JavaMed outperform FaCoy, and Corder pretraining beats MNP and Masked-LM for code retrieval.Table 1 reports code-to-code retrieval results; the authors describe Corder as better than the alternative pretext tasks.Source: Table 1 · p. 6, §5.2.2 · p. 6
5.3 Fine-tuning Pre-trained Encoders for Text-to-code Retrieval
The section evaluates fine-tuned encoders for retrieving code from natural-language queries using the DeepCS Java methods and docstrings. Corder pretraining improves performance across encoders, and larger pretraining datasets yield larger gains.
5.3 Fine-tuning Pre-trained Encoders for Text-to-code Retrieval
- The evaluation uses approximately 16 million preprocessed Java methods paired with docstrings from DeepCS.The retrieval task takes natural language as input and searches candidate code snippets.Source: §5.3.1 · p. 7
5.3.1 Datasets, Metrics, and Baselines
- Retrieval is evaluated with Precision@k for k=1, 5, and 10, and Mean Reciprocal Rank.The chosen Precision@k values reflect typical result-list sizes that users inspect.Source: §5.3.1 · p. 7
- The study compares models trained from scratch with fine-tuned Corder-pretrained encoders, including neural bag-of-words, BiLSTM, Transformer, TBCNN, and Code2vec.A Code2vec model pretrained with method-name prediction is also included as a fine-tuning baseline.Source: §5.3.1 · p. 7
5.3.2 Results
- Corder pretraining significantly improves text-to-code retrieval across encoders, with larger pretraining datasets producing higher-margin gains and Corder-pretrained Code2vec outperforming MNP-pretrained Code2vec.Table 2 reports text-to-code retrieval performance.Source: §5.3.2 · p. 7
5.4 Fine-tuning Pre-trained Encoders for Code Summarization
This section evaluates code summarization by fine-tuning encoders to generate natural-language descriptions from code snippets. Corder-pretrained encoders outperform MNP- and Masked-LM-pretrained alternatives in BLEU across encoders.
5.4 Fine-tuning Pre-trained Encoders for Code Summarization
- The task predicts a full natural-language sentence from a short code snippet, using DeepCS data of approximately 16 million Java methods and corresponding docstrings.The target sequence averages about 12.3 in length.Source: §5.4.1 · p. 7
5.4.1 Dataset, Metric, and Baselines
- Performance is measured with BLEU and compared against 2-layer bidirectional LSTMs, Transformer, Code2seq, MOSES, and an information-retrieval baseline.The comparison also uses token splitting into subtokens and UNK replacement during inference.Source: §5.4.1 · p. 7
- Corder pretraining outperforms MNP and Masked-LM pretraining in BLEU for every encoder on code summarization.The authors describe the margin as significant.Source: §5.4.2 · p. 7
5.5 Compared Against Supervised Methods
This section tests whether Corder embeddings compare favorably with representations learned by supervised methods, using code classification on OJ. Corder-trained encoders significantly outperform the other supervised learning methods in the reported comparison.
5.5 Compared Against Supervised Methods
- Corder-trained encoders significantly outperform the other supervised representation-learning methods on code classification.Table 4 reports the comparison against supervised methods.Source: Table 4 · p. 8, §5.5 · p. 8
- The evaluation follows a representation-quality protocol: embeddings are extracted for training samples and a Logistic Regression classifier is trained on top.Accuracy is the metric, and the OJ task classifies each code snippet by functionality.Source: §5.5 · p. 8
- The supervised representation baselines include AdaSent and InferSent, whose encoders are trained on supervised tasks, plus Code2vec pretrained with method-name prediction.All are evaluated through the classifier trained on their embeddings.Source: §5.5 · p. 8
5.6 Summary & Threats to Validity
Corder outperforms most baselines across three code tasks, while comparisons among pretraining methods are constrained by difficulties adapting tasks across encoders.
- Corder outperforms most baselines on code-to-code retrieval, text-to-code retrieval, and code summarization.Source: §5.6 · p. 8
- Corder embeddings also perform better than embeddings from other supervised methods on code classification.Source: §5.6 · p. 8
- Pretraining comparisons are limited because some tasks are difficult to adapt across encoders, and unbiased settings are not guaranteed.The study therefore adapts MNP for Code2vec and Masked-LM for Transformer.Source: §5.6 · p. 8
6 Analysis and Ablation Study
The analysis tests individual transformation operators and compares code embeddings from Corder and method-name prediction; the ablation findings favor structural changes over variable renaming.
6 Analysis and Ablation Study
- The analysis includes an ablation study of individual operators and a visualization of code embeddings.Source: §6 · p. 8, §6.1 · p. 8
6.1 Impact of Transformation Operators
- With downsampled data, SF, PS, and US outperform VR on text-to-code retrieval and code summarization, and SF leads text-to-code retrieval.With the original data, PS and US perform best most of the time, while SF is comparable to VR despite applying to fewer snippets.Source: §6.1 · p. 9
- The operator results support the conclusion that changing code structure is crucial; VR changes textual information, whereas PS, US, and SF modify structure extensively.Source: §6.1 · p. 9
6.2 Visualization for Code Embeddings
- The embedding visualization compares Corder-Code2vec with MNP-Code2vec on vectors from six OJ Dataset classes.The authors conclude that instance discrimination is a better pretext task than method-name prediction for the same Code2vec encoder.Source: Figure 5 · p. 9, §6.2 · p. 9
7 Conclusion
Corder uses program transformations to create syntactically diverse but semantically equivalent code, then trains representations to bring similar snippets together and separate dissimilar ones. The approach significantly outperforms non-contrastive models across code-to-code retrieval, text-to-code retrieval, and code summarization.
- Corder applies program transformation operators to create syntactically diverse, semantically equivalent code snippets.Source: §7 · p. 9
- Its contrastive objective minimizes distances between representations of similar snippets and maximizes distances between dissimilar snippets.Source: §7 · p. 9
- Corder pretraining significantly outperforms models without contrastive learning on code-to-code retrieval, text-to-code retrieval, and code summarization.Source: §7 · p. 9
Related papers
This paper cites
- Attention Is All You Need2017
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding2018
- Sequence to Sequence Learning with Neural Networks2014
- A Simple Framework for Contrastive Learning of Visual Representations2020
- Distributed Representations of Words and Phrases and their Compositionality2013
- Representation Learning with Contrastive Predictive Coding2018
Cited by
- CIVER: Causal Intervention Verification for Reliable Code Review2026
- Type-IV Code Clone Detection via Layer-Wise Non-Contrastive Representation Learning2026
- Synthetic Semantic Supervision for Contrastive Code Representation Learning in Small Transformers: An Empirical Study2026
- Learning Spectral Representations of Code through Latent Graph Learning for Generalizable Cross-Language Code Clone Detection2026