Skip to main content
QUICK REVIEW

[Paper Review] CoCoSoDa: Effective Contrastive Learning for Code Search

Ensheng Shi, Wenchao Gub|arXiv (Cornell University)|Apr 7, 2022
Topic ModelingComputer Science9 citations
TL;DR

CoCoSoDa proposes a contrastive learning framework for code search that combines soft data augmentation and a momentum-based negative sample queue to improve code and query representation learning. It achieves state-of-the-art performance, outperforming models like CodeBERT, GraphCodeBERT, and UniXcoder by 13.3%, 10.5%, and 5.9% on average MRR, respectively, across six programming languages.

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.

Motivation & Objective

  • To improve code search performance by effectively leveraging contrastive learning through enhanced data augmentation and negative sample mining.
  • To address the limitations of existing contrastive learning methods in code search, such as static augmentation and limited negative sample diversity.
  • To enable pre-trained models to learn more robust and aligned code-query representations without requiring extensive fine-tuning.
  • To demonstrate the generalization of the proposed method across diverse pre-trained models, including RoBERTa, CodeBERT, GraphCodeBERT, and UniXcoder.
  • To validate the robustness and effectiveness of the framework under varying hyperparameters and zero-shot settings.

Proposed method

  • Introduces four soft data augmentation (SoDa) techniques that dynamically mask or replace code tokens with their types to generate semantically similar positive samples.
  • Employs a momentum encoder and a momentum-updated queue to maintain a large, consistent set of negative samples per mini-batch, improving contrastive learning stability.
  • Applies multimodal contrastive learning to align representations of paired code-query instances while pushing apart unpaired code snippets and queries.
  • Integrates the contrastive learning objective into the training of various pre-trained models, including RoBERTa, CodeBERT, GraphCodeBERT, and UniXcoder, to boost their code search performance.
  • Uses a dual-contrastive learning strategy combining intra-modal (code-code, query-query) and inter-modal (code-query) contrastive objectives to enhance representation alignment.
  • Adapts the framework to zero-shot evaluation by evaluating pre-trained models without fine-tuning, demonstrating its ability to learn robust representations from pre-training alone.

Experimental results

Research questions

  • RQ1Can soft data augmentation that replaces or masks tokens with their types improve code representation learning for code search?
  • RQ2Does using a momentum-based queue of negative samples enhance the discriminative power of contrastive learning in code search?
  • RQ3To what extent does multimodal contrastive learning (between code and queries) improve alignment and performance compared to unimodal contrastive learning?
  • RQ4How effective is CoCoSoDa when applied to different pre-trained models, including non-code-specific models like RoBERTa?
  • RQ5Can CoCoSoDa achieve strong performance without fine-tuning, indicating robustness and generalization of learned representations?

Key findings

  • CoCoSoDa achieves state-of-the-art performance on the CodeSearchNet benchmark, outperforming 18 SOTA baselines, with an average MRR of 0.738 across six programming languages.
  • CoCoSoDa exceeds CodeBERT, GraphCodeBERT, and UniXcoder by 13.3%, 10.5%, and 5.9% on average MRR, respectively, demonstrating significant performance gains.
  • In zero-shot evaluation, CoCoSoDa’s performance surpasses fine-tuned versions of CodeBERT, CodeT5, and GraphCodeBERT, with gains exceeding 50% on average MRR and up to 70% on PHP.
  • Ablation studies confirm that both soft data augmentation and the momentum-based negative queue are essential components, with each contributing significantly to performance gains.
  • The method consistently improves performance across different pre-trained models (RoBERTa, CodeBERT, GraphCodeBERT, UniXcoder), showing its orthogonality and generalizability to various architectures.
  • The model demonstrates robustness under various hyperparameter settings, maintaining strong performance across different configurations.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.