C2LLM Technical Report: A New Frontier in Code Retrieval via Adaptive Cross-Attention Pooling

Jin Qin, Zihan Liao, Ziyin Zhang, Hang Yu, Peng Di, Rui WangarXiv 2025 · DOI

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:

  1. 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.
  2. 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.

Architecture of the C2LLM framework featuring the Pooling by Multihead Attention (PMA) module.

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.

Performance comparison on the MTEB-Code benchmark, showing C2LLM-7B and C2LLM-0.5B relative to other open and closed-source models.

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

BibTeX
@misc{qin2025c,
  author = {Jin Qin and Zihan Liao and Ziyin Zhang and Hang Yu and Peng Di and Rui Wang},
  title = {{C2LLM Technical Report: A New Frontier in Code Retrieval via Adaptive Cross-Attention Pooling}},
  year = {2025},
  eprint = {2512.21332},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2512.21332},
}
Figure