Abstract
Code search aims to retrieve semantically relevant code snippets for a given natural language query. Recently, many approaches employing contrastive learning have shown promising results on code representation learning and greatly improved the performance of code search. However, there is still a lot of room for improvement in using contrastive learning for code search. In this paper, we propose CoCoSoDa to effectively utilize contrastive learning for code search via two key factors in contrastive learning: data augmentation and negative samples. Specifically, soft data augmentation is to dynamically masking or replacing some tokens with their types for input sequences to generate positive samples. Momentum mechanism is used to generate large and consistent representations of negative samples in a mini-batch through maintaining a queue and a momentum encoder. In addition, multimodal contrastive learning is used to pull together representations of code-query pairs and push apart the unpaired code snippets and queries. We conduct extensive experiments to evaluate the effectiveness of our approach on a large-scale dataset with six programming languages. Experimental results show that: (1) CoCoSoDa outperforms 14 baselines and especially exceeds CodeBERT, GraphCodeBERT, and UniXcoder by 13.3%, 10.5%, and 5.9% on average MRR scores, respectively. (2) The ablation studies show the effectiveness of each component of our approach. (3) We adapt our techniques to several different pre-trained models such as RoBERTa, CodeBERT, and GraphCodeBERT and observe a significant boost in their performance in code search. (4) Our model performs robustly under different hyper-parameters. Furthermore, we perform qualitative and quantitative analyses to explore reasons behind the good performance of our model.
Introduction
The primary objective of code search is to enable developers to find relevant code snippets using natural language (NL) queries. Modern systems often leverage Contrastive Learning (CL) to map code and NL into a shared embedding space, where semantically matching pairs are positioned closely together. Despite their success, existing CL methods face challenges in efficient data augmentation and the scalability of negative samples due to GPU memory constraints.
To address these limitations, we present CoCoSoDa (Code search with multimodal momentum Contrastive learning and Soft Data augmentation). This framework introduces "soft" data augmentation techniques and a momentum-based queue to significantly enhance the accuracy and robustness of semantic code search.
Methodology
CoCoSoDa incorporates three primary innovations to optimize the contrastive learning process:
- Soft Data Augmentation (SoDa): Unlike traditional "hard" transformations that require complex parsers, SoDa uses lightweight operations such as Dynamic Masking (DM) and Dynamic Replacement (DR). DR replaces tokens with their general types (e.g., variable names become
<Identifier>). These methods preserve sequence structure while forcing the model to learn context-dependent semantics. - Momentum Mechanism: Inspired by the MoCo framework, CoCoSoDa maintains a queue of representations from previous mini-batches. This allows the model to utilize thousands of negative samples (e.g., 4,096) without increasing the batch size, ensuring a large and consistent set of negative examples.
- Multimodal Contrastive Loss: The framework optimizes both inter-modal (code-query) and intra-modal (augmented versions of the same snippet or query) alignment, ensuring that functionally similar inputs remain close in the embedding space.
Experimental Setup
We evaluated CoCoSoDa using the CodeSearchNet benchmark, which includes six programming languages: Python, Java, JavaScript, PHP, Ruby, and Go. The framework was benchmarked against 18 baseline models, including state-of-the-art pre-trained models like CodeBERT, GraphCodeBERT, and UniXcoder.
Results and Discussion
CoCoSoDa achieves state-of-the-art performance, reaching an average Mean Reciprocal Rank (MRR) of 0.788 across all six languages.
Key Findings:
- Performance Gains: CoCoSoDa significantly outperforms industry standards, exceeding CodeBERT by 13.3%, GraphCodeBERT by 10.5%, and UniXcoder by 5.9% on average MRR.
- Orthogonality: The training strategy is model-agnostic. Applying CoCoSoDa's techniques to existing models like RoBERTa and CodeBERT consistently yielded performance improvements.
- Semantic Depth: In zero-shot settings, CoCoSoDa outperformed UniXcoder by 50%, indicating a deeper capture of semantic relationships beyond simple keyword matching.
- Representation Uniformity: Analysis via t-SNE visualizations suggests that CoCoSoDa produces a more uniform distribution of embeddings, mitigating the "representation degeneration" problem common in dense retrieval models.
Conclusion
CoCoSoDa demonstrates that combining soft data augmentation with a momentum-based contrastive learning framework effectively addresses the bottlenecks of traditional code search models. While currently focused on local code snippets, future work will explore incorporating broader contextual information, such as class and project-level structures, to further refine retrieval accuracy.
Summary updated