Abstract
Recent advances in large-scale code generation models have led to remarkable progress in producing high-quality code. These models are trained in a self-supervised manner on extensive unlabeled code corpora using a decoder-only architecture. However, despite their generative strength, decoder-only models often exhibit limited performance on code understanding tasks such as code search and clone detection, primarily due to their generation-oriented training objectives. While training large encoder-only models from scratch on massive code datasets can improve understanding ability but remains computationally expensive and time-consuming. In this paper, we explore a more efficient alternative by transferring knowledge from pre-trained decoder-only code generation models to code understanding tasks. We investigate how decoder-only architectures can be effectively adapted to learn discriminative and semantically meaningful code representations. To this end, we propose CL4D, a contrastive learning framework tailored to strengthen the representation capabilities of decoder-only models. Extensive experiments on multiple benchmark datasets demonstrate that CL4D achieves competitive or superior performance compared to existing methods on representative code understanding tasks, including code search and clone detection. Further analysis reveals that CL4D substantially improves the semantic alignment of code representations by reducing the distance between semantically similar code snippets. These findings highlight the feasibility of leveraging decoder-only models as a unified backbone for both code generation and understanding.
Introduction
The landscape of AI for code is currently bifurcated. On one side, decoder-only models like CodeLlama and StarCoder have set new benchmarks in code generation. On the other, encoder-only models like CodeBERT remain the standard for code understanding tasks such as semantic search and clone detection. This divide stems from the unidirectional attention mechanism of decoders, which, while excellent for predicting the next token, often fails to capture the global semantic context required for discriminative tasks.
Training massive encoder-only models from scratch is a resource-intensive endeavor. This paper proposes a more efficient path: repurposing the vast knowledge already embedded in pre-trained decoder-only models. We introduce CL4D (Contrastive Learning for Decoders), a framework that adapts these generative models for high-performance code understanding.
The CL4D Framework
CL4D transforms a generative decoder into a discriminative encoder using a dual-encoder architecture and a contrastive learning objective.
Methodology Highlights
- Contrastive Learning: The model is trained to map natural language queries and their corresponding code snippets into a shared vector space. The objective is to minimize the distance between positive pairs while maximizing the distance from "negative" samples.
- Hard Negative Mining: To improve discriminative power, the framework identifies "hard negatives"—code snippets that are syntactically similar but semantically distinct—using a pre-trained UniXcoder model to filter and select challenging examples.
- Optimal Representation Extraction: Unlike encoder models that use a special
[CLS]token, decoders require different strategies. The authors found that averaging the embeddings of all tokens, combined with right-padding, yields the most semantically rich representations for understanding tasks.
Experimental Setup
The framework was trained on millions of (query, code) pairs extracted from The Stack, covering six major programming languages: Python, Java, Go, PHP, JavaScript, and Ruby. Evaluation was conducted across several key benchmarks:
- CodeSearchNet (CSN) & CoSQA: Benchmarks for natural language code search.
- POJ-104: A dataset designed for semantic clone detection (identifying code that performs the same function but differs in implementation).
Key Results and Analysis
CL4D-enhanced models consistently outperformed specialized encoder-only models of similar sizes. Notably, DeepSeek-Coder (1.3B) + CL4D achieved a score of 77.57 on the CSN benchmark, surpassing the previous state-of-the-art encoder, CodeSage.
Zero-Shot Capabilities
One of the most striking findings was the improvement in zero-shot performance. Base decoder models often perform poorly on understanding tasks without specific fine-tuning. After CL4D training, DeepSeek-Coder’s performance on CSN jumped from a baseline of 0.12 to 76.02, demonstrating that the model's latent knowledge was successfully surfaced and aligned.
Semantic Alignment
Visualization of the embedding space confirms the effectiveness of the contrastive approach. As shown in the t-SNE plot below, CL4D successfully clusters semantically similar code snippets in the vector space, a feat that base decoder-only models fail to achieve as their representations are typically scattered.
Conclusion
CL4D demonstrates that decoder-only models can serve as a unified backbone for both code generation and understanding. By leveraging contrastive learning and efficient representation extraction, we can bridge the gap between "reading" and "writing" code without the prohibitive cost of training new architectures from scratch. This research paves the way for more versatile and efficient AI tools that can both generate and reason about code within a single, powerful framework.
Summary updated