Exploring Representation-level Augmentation for Code Search

Haochen Li, Chunyan Miao, Cyril Leung, Yanxian Huang, Yuan Huang, Hongyu Zhang, Yanlin Wang

Abstract

Code search is a critical task in software engineering, often addressed using contrastive learning to align natural language queries with code snippets. While data augmentation is a standard technique to improve these models, traditional raw-data augmentation is computationally expensive and requires complex static analysis. This paper proposes a representation-level augmentation framework that manipulates hidden vectors directly. Our approach consistently improves performance across various programming languages and model architectures while being significantly more efficient.

Introduction

Modern code search engines rely on dual-encoder architectures trained via contrastive learning. These models learn to map code and natural language into a shared embedding space where relevant pairs are close together. To enhance robustness, researchers typically employ raw-data augmentation—programmatically altering source code (e.g., variable renaming). However, this approach incurs high overhead due to the need for static analysis and the computational cost of re-encoding every augmented sample.

We explore whether manipulating mathematical representations (vectors) can replace or supplement raw-data augmentation. By operating in the vector space, we can generate multiple augmented samples "for free" after the initial encoding pass.

Methodology: Representation-Level Augmentation

We propose a unified framework for augmenting representations. Given a hidden vector $h$ from the model, an augmented version $h^+$ is generated as: $$h^+ = \alpha \odot h + \beta \odot h'$$ where $h'$ is another sample in the batch, and $\alpha, \beta$ are coefficient vectors. We evaluate four specific techniques:

  1. Linear Interpolation/Extrapolation: Mixing or extending vectors along the line connecting them.
  2. Stochastic Perturbation: Randomly deactivating features (similar to Dropout).
  3. Binary Interpolation: Swapping features between two representations.
  4. Gaussian Scaling: Applying random noise to feature scales.

Theoretically, these augmentations provide a tighter lower bound on Mutual Information for the InfoNCE loss, forcing the model to learn more discriminative features.

Experimental Results

Evaluation on the CodeSearchNet (CSN) benchmark across six languages (Ruby, JS, Go, Python, Java, PHP) shows consistent improvements.

MRR vs. Loss for GraphCodeBERT and CodeBERT with and without representation-level augmentation.

As shown in the figure above, models using representation-level augmentation (blue lines) achieve higher Mean Reciprocal Rank (MRR) at lower loss levels compared to baseline models (red lines). This trend holds for both standard Transformers (CodeBERT) and structure-aware models (GraphCodeBERT).

The Normalization Conflict

A key finding is the impact of vector normalization. Many retrieval models normalize embeddings to a unit sphere. However, our augmentation benefits from varying vector lengths (norms) to distinguish "hard" examples.

Distribution of normalized vector norms with and without augmentation.

The figure illustrates that without augmentation, vector norms are highly concentrated near 1.0. Representation-level augmentation spreads this distribution, which appears crucial for the performance gains observed. Forcing strict normalization actually negates the benefits of our proposed method.

Conclusion

Representation-level augmentation offers a computationally efficient alternative to traditional data augmentation for code search. By operating in the vector space, it avoids the overhead of source code analysis and re-encoding. While highly effective, future work is needed to reconcile these techniques with models that require strict vector normalization.

Summary updated

BibTeX
@inproceedings{li2022exploring,
  author = {Haochen Li and Chunyan Miao and Cyril Leung and Yanxian Huang and Yuan Huang and Hongyu Zhang and Yanlin Wang},
  title = {{Exploring Representation-level Augmentation for Code Search}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2022},
  doi = {10.18653/v1/2022.emnlp-main.327},
}
Figure