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:
- Linear Interpolation/Extrapolation: Mixing or extending vectors along the line connecting them.
- Stochastic Perturbation: Randomly deactivating features (similar to Dropout).
- Binary Interpolation: Swapping features between two representations.
- 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.
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.
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