Manuscript Offprint

arXiv:2609.03702

Synthetic Semantic Supervision for Contrastive Code Representation Learning in Small Transformers: An Empirical Study

Kenneth Paulsen, Florian Tambon, Mike Papadakis, Shin Yoo

Abstract

General-purpose code embeddings power tools for code search, classification, and retrieval. Compact transformer encoders for code typically rely on either human-written docstrings (labor-intensive and inconsistent) or mined structural signals such as execution traces (setting-specific and costly to collect). We empirically study an alternative: contrastive pretraining of small encoders with synthetically generated natural-language descriptions emphasizing code functionality and intent, paired with code in a dual-encoder framework at training and discarded at inference. We benchmark this approach against pretraining-based baselines, generalist LLMs, and embedding-specific models on eight retrieval, classification, and generation tasks across C, C++, and Java. Synthetic semantic supervision yields statistically significant gains over pretraining baselines of the same inference-time size on five of eight tasks, with parity on two more; once fine-tuned, it matches or exceeds zero-shot models two orders of magnitude larger on classification, and it stays on par with execution-aware supervision at matched pretraining data, suggesting a scalable, effective alternative to existing code-representation paradigms.

1 Introduction

In recent years, a growing number of automated tools for code search, documentation, analysis, and coding assistance has been proposed (Microsoft; Guo et al., 2022; Huang et al., 2020). These tools rely on effective code representations: abstractions that translate human-written source code into formats suitable for automated processing.¹

A common approach is to use learned embeddings derived from token sequences or structured program elements (Xie et al., 2023). While

Figure

Figure 1: Comparison across eight downstream tasks. Methods grouped by paradigm (left band). Cell color indicates rank within each panel and task column: 1st (dark), 2nd (medium), 3rd (light); cells in columns with a single evaluated method are unshaded; em-dashes indicate the method was not evaluated. (a) Against pretraining baselines of the same inference-time size, SyncDesc improves significantly on five of eight tasks and is at statistical parity on two more (paired bootstrap tests in Table 2); ContraCode's ranking carries a cross-language transfer penalty (JavaScript pretraining; Section 3). (b) SyncDesc, fine-tuned per task, remains competitive against zero-shot LLM-scale models on algorithmic retrieval and classification. SyncDesc is best on single-source / dual-source variants (per-variant breakdown in Appendix B). Full results in Appendix A.

Large Language Models (LLMs) such as GPT-4o mini (OpenAI, 2024a) can generate such embeddings, and specialized embedding LLMs such as Nomic (Nussbaum et al., 2025) can do so as well, their use incurs high computational and financial costs (e.g., API calls, infrastructure) and scales

¹Code, data, the generated descriptions, and the generation logs are available at https://zenodo.org/records/22130652.

poorly for many practical applications. This holds in particular for embedding workloads (code search, retrieval, and clone detection over large candidate pools), where ranking NN candidates requires NN precomputed, mutually comparable vectors rather than NN prompts per query, and per-query API calls add latency, rate limits, and code egress that private codebases often prohibit. Prompted LLMs retain the complementary advantage of zero-shot generalization; the deployment class we target is the former. In contrast, smaller, specialized transformer models have emerged as a cost-effective alternative, achieving strong performance on targeted coding tasks through fine-tuning (Zeng et al., 2022; Giagnorio et al., 2025; Wang et al., 2025).

Prior work on training code embedding models generally falls into two paradigms. The first, human supervision, uses manually written specifications (e.g., docstrings) as supervision signals to capture program semantics, as in UnixCoder (Guo et al., 2022) or CodeBERT (Feng et al., 2020). While effective, this approach is labor-intensive and struggles to produce representations that generalize across tasks, languages, or evaluation settings (Li et al., 2023). Moreover, existing annotations often rely on surface-level lexical or syntactic cues, providing only indirect access to semantic intent (Panthaplackel et al., 2020), and may contain inconsistencies or errors (Siddiq et al., 2024). The second paradigm, structural supervision, leverages dynamic or control-flow information mined directly from code (e.g., traces, instrumentation, or program analysis), as in TRACED (Ding et al., 2024). While more automated, these approaches can be expensive to collect and must be tailored to specific languages or settings. Recent work has also explored synthetic supervision for code. For example, ContraCode (Jain et al., 2021) proposed generating synthetic equivalent code to pre-train a neural model, while the Qwen2.5-coder white paper (Qwen et al., 2025) demonstrated the use of synthetic data for pretraining code models. These efforts focus primarily on synthetic equivalent code, which limits the diversity of tasks (i.e., they require equivalence), or they train large models, raising the question of the benefit compared to the increase in size. Finally, they focus mainly on general code generation tasks rather than representation learning for downstream understanding tasks.

Despite a wealth of approaches, evaluations and direct comparisons of these approaches remain scarce when it comes to downstream code-oriented tasks. Moreover, while synthetic datasets have proven useful for training LLMs, they remain underexplored for smaller code-oriented transformers. Starting from this observation, we ask: how do existing code-embedding methods fare, and could a simple, scalable approach (using synthetic semantic descriptions and contrastive learning) produce high-quality code embeddings that rival or exceed existing methods? In this paper, we empirically evaluate the following hypothesis: contrastive pretraining with synthetic semantic descriptions can yield code embeddings that (1) outperform traditional pretrained baselines, (2) match or exceed the performance of larger models on downstream tasks, and (3) generalize across languages and tasks, all while being computationally efficient.

To test these hypotheses, we select several baseline approaches from the literature covering a broad spectrum of methods, namely pretraining-based methods (TRACED (Ding et al., 2024), ContraCode (Jain et al., 2021)) applied to a small baseline transformer (UnixCoder (Guo et al., 2022)), large generalist models (GPT-4o (OpenAI, 2024b)), and large generalist embedding models (Nomic (Nussbaum et al., 2025)). We further propose a simple approach, SyncDesc, relying on synthetically generated semantic descriptions at scale, paired with code snippets to form aligned supervision for a dual-encoder contrastive objective (Radford et al., 2021), exploiting research highlighting their individual effectiveness (Qwen et al., 2025; Guo et al., 2022). SyncDesc is best understood as lightweight knowledge distillation: it transfers the semantic-extraction ability of a large teacher model into a compact encoder through generated descriptions, rather than introducing a new mechanism. In this respect, none of the compared pretraining paradigms is information-free: TRACED injects privileged execution information, ContraCode injects compiler-verified equivalence, and SyncDesc injects teacher-model semantics; the empirical question this paper answers is which external signal is most effective per unit cost at a fixed inference-time size. Notably, the fine-tuned 125M student exceeds its own teacher on the one task where the teacher is evaluated (Section 3). We further conduct an ablation study that isolates the two ingredients of SyncDesc (the synthetic semantic descriptions and the contrastive objective), comparing against variants supervised with human-written docstrings and trained non-contrastively, to quantify how much each contributes to the observed gains.

Our results show that contrastively pretrained models with synthetic semantic descriptions improve significantly over pretrained baselines of the same inference-time size on five of eight tasks, with parity on two more and parity-to-below on vulnerability detection (Table 2). On several tasks, SyncDesc, after task-specific fine-tuning that the zero-shot LLMs lack, matches or outperforms larger language models, despite using significantly smaller models. An ablation study confirms that both synthetic descriptions and contrastive learning are critical to these gains, outperforming embeddings supervised by human docstrings or trained non-contrastively. These findings suggest that synthetic semantic supervision, when combined with contrastive learning, offers a scalable, effective, and simple alternative to existing paradigms for code representation learning.

References (41)