Are Decoder-Only Large Language Models the Silver Bullet for Code Search?

Yuxuan Chen, Guangsheng Ou, Mingwei Liu, Yanlin Wang, Zibin Zheng · DOI

Introduction

Code search serves as the "Google for developers," allowing users to retrieve relevant code snippets from natural language queries. For years, the industry standard has been encoder-only models like CodeBERT and UniXcoder, which are specifically designed to align text and code in a dense vector embedding space. However, the meteoric rise of decoder-only Large Language Models (LLMs) like GPT-4 and CodeGemma has revolutionized code generation, leaving their effectiveness for retrieval tasks as an open research question. This paper investigates whether the massive scale and reasoning capabilities of these newer LLMs can overcome their architectural focus on next-token prediction to set new benchmarks in code search.

Methodology

The researchers conducted a systematic evaluation of 11 decoder-only LLMs, ranging from general-purpose models like Llama 3 to code-specific variants like Qwen2.5-Coder. To bridge the gap between generation and retrieval, several key technical strategies were employed:

Supervised Contrastive Learning (SupCon)

Models were fine-tuned to pull "positive" pairs (matching query and code) together while pushing "negative" pairs apart in the embedding space. This process is essential because, out of the box, decoder-only models are optimized for continuation rather than similarity measurement.

Bidirectional Attention

Standard decoder-only models use causal (left-to-right) attention. During fine-tuning, the authors modified this mechanism to allow tokens to "see" the entire context, which is critical for generating high-quality, context-aware embeddings.

Comparison of Causal Attention vs. Bidirectional Attention mechanisms.

Mean Pooling

While encoder-only models typically use a single [CLS] token to represent a sequence, the authors found that mean pooling—averaging all token embeddings—yielded superior results for decoder-only architectures.

Comparison of pooling strategies: (a) CLS pooling in encoder-only models vs. (b) Mean pooling in decoder-only models.

Datasets and Benchmarks

The study utilized two primary benchmarks to ensure a robust evaluation:

  1. CodeSearchNet (CSN): A standard dataset covering six programming languages (Ruby, JavaScript, Go, Python, Java, PHP).
  2. CoSQA+: A high-quality Python benchmark designed to reflect real-world scenarios, featuring multiple correct code snippets for a single query to test generalization.

Key Insights and Results

The Fine-Tuning Breakthrough

Without fine-tuning, decoder-only LLMs perform poorly in zero-shot settings, often trailing behind older encoder-only models. However, fine-tuning on the CSN dataset resulted in a staggering 453.1% increase in Average Mean Reciprocal Rank (MRR). A fine-tuned CodeGemma model eventually achieved a 40.4% higher Mean Average Precision (MAP) on the CoSQA+ benchmark than UniXcoder, establishing a new state-of-the-art.

Scaling and Robustness

  • Optimal Size: Surprisingly, larger models are not always better for embedding. In the Qwen2.5-Coder family, the 1.5B parameter model outperformed the 7B and 14B versions, suggesting an optimal density for retrieval tasks.
  • Resistance to Collapse: Unlike smaller encoder-only models, which often see performance plummet when tested on unseen datasets ("embedding space collapse"), larger decoder-only models proved highly robust and maintained high performance across varying query styles.
  • Multilingual Advantage: Training on a mix of languages improved results across the board, as the model learned more abstract and robust semantic representations of code.

Limitations and Future Directions

Despite their strengths, decoder-only models struggle with "ultra-short" queries (fewer than 10 tokens), a phenomenon attributed to the "curse of dimensionality" where minimal input provides insufficient signal. Future work may explore query reformulation to expand short inputs. Additionally, while these models are large, the per-query embedding time for a 7B model is approximately 14.7ms, making them highly viable for real-time production search systems.

Summary updated

BibTeX
@article{chen2026are,
  author = {Yuxuan Chen and Guangsheng Ou and Mingwei Liu and Yanlin Wang and Zibin Zheng},
  title = {{Are Decoder-Only Large Language Models the Silver Bullet for Code Search?}},
  journal = {IEEE Trans. Software Eng.},
  year = {2026},
  doi = {10.1109/tse.2026.3657353},
}
Figure