On Contrastive Learning of Semantic Similarity forCode to Code Search

Anthony Saieva, Saikat Chakraborty, Gail E. Kaiser

Abstract

This paper introduces a novel code-to-code search technique that enhances the performance of Large Language Models (LLMs) by including both static and dynamic features as well as utilizing both similar and dissimilar examples during training. We present the first-ever code search method that encodes dynamic runtime information during training without the need to execute either the corpus under search or the search query at inference time and the first code search technique that trains on both positive and negative reference samples. To validate the efficacy of our approach, we perform a set of studies demonstrating the capability of enhanced LLMs to perform cross-language code-to-code search. Our evaluation demonstrates that the effectiveness of our approach is consistent across various model architectures and programming languages. We outperform the state-of-the-art cross-language search tool by up to 44.7%. Moreover, our ablation studies reveal that even a single positive and negative reference sample in the training process results in substantial performance improvements demonstrating both similar and dissimilar references are important parts of code search. Importantly, we show that enhanced well-crafted, fine-tuned models consistently outperform enhanced larger modern LLMs without fine tuning, even when enhancing the largest available LLMs highlighting the importance for open-sourced models. To ensure the reproducibility and extensibility of our research, we present an open-sourced implementation of our tool and training procedures called REINFOREST.

Introduction

Code-to-code search is the task of finding code snippets that perform the same functional operations as a given query. While identifying syntactically similar code (Type 1-3 clones) is a well-studied problem, identifying Type 4 clones—code fragments that are syntactically distinct but functionally identical—remains a significant challenge. This is particularly difficult in cross-lingual scenarios, such as searching for a Python implementation of a Java function.

Existing approaches typically fall into two categories: static methods, which are fast but struggle with semantic depth, and dynamic methods, which are accurate but computationally expensive and difficult to scale due to the requirement of a runtime environment. REINFOREST bridges this gap by training models to recognize functional similarity through runtime execution data during the training phase, enabling execution-free inference.

Overview of the REINFOREST training pipeline, incorporating code execution data into the search model.

The REINFOREST Framework

The core innovation of REINFOREST is a training pipeline that "reinforces" semantic similarity through two main techniques:

Semantic Similarity Score (SSS)

During training, REINFOREST executes code snippets using generated inputs to calculate a Semantic Similarity Score (SSS). This score represents the frequency with which two pieces of code produce identical outputs for the same inputs. This dynamic information is used to supervise the model's understanding of code behavior.

Contrastive Training with Dual Encoders

The framework utilizes a dual-encoder architecture and a specialized loss function. The training process aims to:

  1. Minimize distance in the vector space between code snippets with high SSS (functionally similar).
  2. Maximize distance between snippets with low SSS (functionally dissimilar).
Contrastive learning process: minimizing distance for functionally similar code (high SSS) and maximizing it for dissimilar code.

Execution-Free Inference

Once the model is trained, it can infer the likely behavior of code from its source alone. At search time, the corpus is pre-embedded into a vector space. The search query is converted into a vector, and a similarity search is performed using FAISS. Crucially, no code execution is required during the inference phase, maintaining the speed and scalability of static methods.

Evaluation and Results

The researchers evaluated REINFOREST using the Atcoder dataset, which contains over 18,000 Java and 22,000 Python solutions across 361 distinct programming problems.

Key Performance Metrics

  • State-of-the-Art Performance: REINFOREST outperformed COSAL (the previous state-of-the-art) by up to 44.7%.
  • Cross-Lingual Success: In Java-to-Python searches, it showed a 40.6% improvement over the nearest baseline.
  • Impact of SSS: Training with SSS improved performance by 7.69x for Java-to-Python search and 9.96x for Python-to-Java search compared to untrained models.

Insights

A significant finding is that smaller, open-source models (like CodeBERT) fine-tuned with REINFOREST consistently outperform massive, proprietary models (like OpenAI’s Davinci) that cannot be fine-tuned. This underscores the value of specialized fine-tuning over raw model scale for software engineering tasks. Additionally, ablation studies showed that even a single positive and negative reference sample during training leads to substantial performance gains.

Limitations and Future Work

While REINFOREST shows remarkable performance, it has limitations:

  • Input Generation: The method depends on generating valid inputs for code during training, which can be difficult for code with complex external dependencies.
  • Granularity: The current model assumes a one-to-one function mapping and may struggle with behaviors spread across multiple classes.
  • Generalization: Future work will explore how these techniques generalize from competitive programming datasets to "messy" production codebases.

Conclusion

REINFOREST demonstrates that LLMs can be trained as "mental simulators" of code execution. By baking dynamic behavior into model weights via contrastive learning, it achieves high-accuracy semantic search without the overhead of runtime execution at inference.

Summary updated

BibTeX
@inproceedings{saieva2023contrastive,
  author = {Anthony Saieva and Saikat Chakraborty and Gail E. Kaiser},
  title = {{On Contrastive Learning of Semantic Similarity forCode to Code Search}},
  booktitle = {SCAM},
  year = {2023},
  url = {https://openreview.net/forum?id=Xi0qMx1Zpk},
}
Figure