Reinforest: Reinforcing Semantic Code Similarity for Cross-Lingual Code Search Models

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

Finding code that performs the same function as a query—but is written in a different programming language—is a notoriously difficult task. Traditional methods typically rely on static analysis (textual or structural similarity), which fails when syntax changes drastically, or dynamic analysis (runtime execution), which is accurate but computationally expensive and difficult to scale.

REINFOREST bridges this gap by training Large Language Models (LLMs) to develop "execution intuition." By reinforcing a model's understanding of code behavior during training, REINFOREST allows for highly accurate cross-lingual search using only static embeddings at inference time.

The REINFOREST Framework

The core innovation of REINFOREST is the integration of dynamic runtime behavior into static LLM embeddings. This is achieved through a three-step process:

  1. Semantic Similarity Score (SSS): During the training phase, code snippets are executed with various generated inputs. The SSS is calculated based on the matching outputs between two snippets. This is the only stage where code execution is required.
  2. Contrastive Training: The model employs a dual-encoder architecture. It is trained to minimize the distance between "positive" samples (semantically identical code) and maximize the distance between "negative" samples (functionally different code).
  3. Inference: Once trained, the model generates embeddings for the entire search corpus. When a user submits a query, the model embeds it, and a standard vector similarity search (e.g., using FAISS) identifies the best matches. No code is executed during the search itself.
Overview of the REINFOREST training process, which uses code execution to derive semantic similarity scores for model training.

Contrastive Learning and "Execution Intuition"

A key insight of this research is that LLMs can learn to predict behavioral similarity from source code text if supervised with dynamic execution data. The training process uses the SSS to guide the loss function, forcing the model to learn not just that two snippets are "similar," but the degree of their functional equivalence.

REINFOREST uses contrastive learning to minimize embedding distance for high-SSS pairs and maximize it for low-SSS pairs.

Furthermore, the researchers found that training on "negative" samples—code that solves different problems—is as vital as training on positive ones. This dual pressure creates a more robust and discriminative embedding space.

Evaluation and Results

The researchers evaluated REINFOREST using the Atcoder dataset, which contains thousands of Java and Python solutions to competitive programming problems. Key findings include:

  • State-of-the-Art Performance: REINFOREST outperformed COSAL, the previous leading tool, by up to 44.7%.
  • Impact of SSS: Incorporating the Semantic Similarity Score led to massive performance gains—up to 9.96x better than models trained without dynamic information.
  • Fine-tuning vs. Scale: Smaller, fine-tuned models like CodeBERT consistently outperformed much larger proprietary models (such as GPT-3/Davinci) that lacked specific behavioral tuning. This underscores the importance of open-source models that allow for deep fine-tuning on domain-specific data.

Conclusion

REINFOREST demonstrates that dynamic execution data can be successfully distilled into static embeddings, providing the accuracy of dynamic analysis with the speed of static search. By leveraging both positive and negative samples and a novel similarity metric, REINFOREST sets a new benchmark for cross-lingual code-to-code search, offering a scalable solution for modern software engineering challenges.

Summary updated

BibTeX
@inproceedings{saieva2023reinforest,
  author = {Anthony Saieva and Saikat Chakraborty and Gail E. Kaiser},
  title = {{Reinforest: Reinforcing Semantic Code Similarity for Cross-Lingual Code Search Models}},
  booktitle = {SCAM},
  year = {2023},
  url = {https://openreview.net/forum?id=Xi0qMx1Zpk},
}
Figure