Introduction
In modern software engineering, code retrieval—the ability to locate specific code snippets from vast repositories using natural language queries—is a critical capability. High-quality code retrieval relies on embedding models that can compress complex, multi-line logic into a single dense vector. C2LLM (Contrastive Code Large Language Models) introduces a novel family of embedding models that achieve state-of-the-art performance by addressing the fundamental "pooling dilemma" inherent in causal Large Language Models (LLMs).
The Pooling Dilemma
Most contemporary embedding models utilize one of two sub-optimal strategies to generate sequence representations from causal LLMs like Qwen or Llama:
- Mean Pooling: This involves averaging the hidden states of all tokens. While comprehensive, it typically requires bidirectional attention, which conflicts with the unidirectional (causal) pretraining of most modern LLMs.
- EOS Pooling: This uses only the final "End of Sequence" token's representation. In long code files, this creates a significant information bottleneck, as a single token struggles to capture the full semantic nuance of the entire snippet.
C2LLM resolves this by introducing an architecture that preserves causal pretraining while enabling global context aggregation.
Methodology: Pooling by Multihead Attention (PMA)
The core innovation of C2LLM is the Pooling by Multihead Attention (PMA) module. Built upon the Qwen-2.5-Coder backbone, the PMA module replaces traditional pooling with a lightweight cross-attention layer.
As shown in the architecture diagram, a "learnable query" vector attends to the hidden states produced by the LLM. This allows the model to dynamically weight different parts of the code—such as function signatures, variable declarations, or specific algorithmic logic—based on their importance to the final embedding. Furthermore, the PMA module's projection matrix allows for Dimensionality Adaptation, enabling the model to output embeddings of varying sizes to optimize vector database storage without the complexity of Matryoshka Representation Learning (MRL).
Training and Datasets
C2LLM models were trained on a massive dataset of 3 million samples covering a wide spectrum of code-related tasks, including:
- Retrieval & Search: CodeSearchNet, CosQA, and CodeEditSearch.
- Reasoning & Dialogue: CodeFeedback (Single and Multi-turn) and StackOverflowQA.
- Programming Challenges: APPS.
- Translation & SQL: CodeTransOcean and SyntheticText2SQL.
The training process employed contrastive learning with a global batch strategy to maximize the diversity of negative examples. Efficiency was maintained through the use of LoRA (Low-Rank Adaptation) and Flash Attention 2.
Evaluation and Results
The models were evaluated on the MTEB-Code benchmark, a comprehensive suite of 12 retrieval tasks. C2LLM demonstrated dominant performance across various model sizes.
Key highlights include:
- Overall #1 Ranking: C2LLM-7B achieved an average score of 80.75, surpassing all other models on the leaderboard, including Seed1.6-Embed and Qwen3-Embed-8B.
- Efficiency at Scale: The C2LLM-0.5B variant is the top-performing model in the sub-1B parameter category, scoring 75.46. It notably outperforms much larger models like the INF-Retriever 7B (69.70).
- Superior Reasoning: On the CodeFeedback-MT task, C2LLM-7B reached a score of 94.32, showcasing its ability to handle complex, multi-turn conversational context in code retrieval.
Conclusion
C2LLM demonstrates that preserving causal attention while utilizing a global aggregator like the PMA module is a superior approach for code embeddings. By breaking the information bottleneck of EOS pooling and the architectural mismatch of Mean pooling, C2LLM sets a new standard for code retrieval. Future work will focus on expanding these capabilities into multilingual and multi-domain scenarios, further integrating C2LLM into the broader CodeFuse ecosystem of developer tools.
Summary updated