NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Soft-Labeled Contrastive Pre-Training for Function-Level Code Representation

Xiaonan Li, Daya Guo, Yeyun Gong, Yun Lin, Yelong Shen, Xipeng Qiu, Daxin Jiang, Weizhu Chen, Nan Duan

Introduction

Contrastive pre-training has emerged as a cornerstone for learning robust code representations. By pulling semantically similar code snippets (positives) together and pushing dissimilar ones (negatives) apart, models learn to map code into a meaningful vector space. However, traditional binary contrastive learning suffers from two critical limitations: the "False Negative" problem and a reliance on "Superficial Positives."

The false negative problem occurs when semantically similar snippets (e.g., two different implementations of a sorting algorithm) are treated as hard negatives, forcing the model to ignore shared logic. Conversely, superficial positives arise when models use simple transformations like variable renaming to create positive pairs, allowing the model to "cheat" by matching syntax rather than deep semantics. To address these issues, we introduce SCodeR (Soft-labeled Code Representation).

The SCodeR Framework

SCodeR moves beyond the rigid binary nature of standard contrastive learning by introducing two primary innovations: an iterative adversarial framework for soft labeling and enhanced positive sample construction via structural splitting.

Soft-Labeled Contrastive Learning

Instead of assigning a hard 0 (negative) or 1 (positive) label, SCodeR utilizes an iterative adversarial framework. This system consists of a dual-encoder (the primary model) and cross-encoder discriminators. During training, the dual-encoder identifies "hard negatives"—samples it perceives as similar despite their negative labels. The discriminators then evaluate these pairs to determine if they actually share underlying semantics. If they do, the pair is assigned a "soft label" (a relevance score between 0 and 1), allowing the model to maintain proximity between logically related snippets.

A case study of soft label assignment: Code 2 is a "false negative" that performs the same function as the positive sample, receiving a high relevance score.

Enhanced Positive Sample Construction

To force the model to learn logic rather than just syntax, SCodeR employs two strategies for positive pair creation:

  1. Code Comments: Pairing a function with its natural language description to align code logic with human intent.
  2. Abstract Syntax Sub-Tree (ASST) Extraction: Rather than simple text-based transformations, SCodeR uses an AST parser to split a function into a specific sub-tree (such as an if statement or a loop) and its surrounding context. This creates positive pairs that are structurally distinct but logically coupled.
The ASST extraction process: breaking a function into structural components to create logically coupled but structurally distinct positive pairs.

Experimental Evaluation

SCodeR was pre-trained on the CodeSearchNet (CSN) corpus, which includes 2.3 million functions across six programming languages. The model was evaluated across a diverse set of benchmarks, including code search, clone detection, and zero-shot cross-language search.

Key Results

  • Code Search: On the CSN dataset, SCodeR outperformed the previous state-of-the-art (UniXcoder) by an average of 2.3 points. On the more challenging AdvTest dataset, it demonstrated even larger gains of approximately 4 points.
  • Clone Detection: The model achieved a MAP@R of 92.45 on the POJ-104 benchmark, significantly outperforming specialized models like Corder and DISCO.
  • Zero-Shot Generalization: In cross-language code search tasks (e.g., using a Ruby query to find Python code), SCodeR improved performance by an average of 3.12 absolute points, highlighting its ability to capture language-agnostic logic.

Conclusion and Future Work

SCodeR demonstrates that embracing the inherent ambiguity of code similarity through soft labels leads to more nuanced and effective representations. By bridging the gap between structural sub-components and their broader contexts, the model achieves superior performance across multiple downstream tasks.

While the iterative adversarial training introduces additional computational overhead—requiring approximately 37 hours on 8x A100 GPUs—future iterations may explore offline soft-label generation to streamline the data pipeline. Additionally, applying the SCodeR framework to larger backbone architectures remains a promising avenue for further research.

Summary updated

BibTeX
@inproceedings{li2022soft,
  author = {Xiaonan Li and Daya Guo and Yeyun Gong and Yun Lin and Yelong Shen and Xipeng Qiu and Daxin Jiang and Weizhu Chen and Nan Duan},
  title = {{Soft-Labeled Contrastive Pre-Training for Function-Level Code Representation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2022},
  doi = {10.18653/v1/2022.findings-emnlp.9},
}
Figure