Language Agnostic Code Embeddings

Saiteja Utpala, Alex Gu, Pin-Yu Chen

Introduction

Large language models (LLMs) trained on code, such as CodeBERT and StarEncoder, have revolutionized software engineering tasks. However, these models frequently exhibit a strong "language bias," where internal representations cluster code snippets by their programming language (syntax) rather than their underlying logic (semantics). This bias hinders cross-lingual tasks, such as finding a C++ implementation of a Python function. This paper explores whether code embeddings can be mathematically decomposed to isolate and remove this syntactic noise.

Mathematical Decomposition and Methods

The authors hypothesize that a code embedding $e$ is a linear combination of a syntax component $e^s$ and a semantic component $e^a$:

$$e = e^s + e^a$$

To isolate $e^a$, three training-free, post-hoc methods are proposed to estimate and project out $e^s$:

  1. Centering: Subtracting the mean embedding of a specific language from all snippets in that language.
  2. Low Rank Decomposition (LRD): Using Singular Value Decomposition (SVD) to identify a language-specific low-rank subspace and projecting embeddings orthogonally away from it.
  3. Common Specific Low Rank Decomposition (CS-LRD): A novel approach that derives a unified syntax subspace shared across all programming languages, capturing universal syntactic structures.
Figure 1: Comparison of retrieval results across monolingual and multilingual setups using different decomposition methods.

Experimental Setup

The methods were evaluated on five models (CodeBERT, GraphCodeBERT, UnixCoder, StarEncoder, and CodeT5+) using two primary benchmarks:

  • XLCoST: For zero-shot Code-to-Code retrieval across seven languages.
  • CodeSearchNet (CSN): For zero-shot Text-to-Code retrieval.

The syntax subspaces were estimated using small samples (as few as 100 snippets) from The Stack.

Key Insights

1. Syntax and Semantics are Separable

Probing experiments demonstrate that while a linear classifier can easily identify the programming language from raw embeddings, this accuracy drops significantly after applying CS-LRD. This confirms that syntactic information can be stripped while preserving semantic utility.

Figure 6: Language identification accuracy for GraphCodeBERT drops by over 81% as the rank (r) of the projected subspace increases, indicating successful syntax removal.

2. CS-LRD Performance

CS-LRD generally outperformed simpler methods for Code-to-Code tasks, likely because it captures cross-lingual syntactic commonalities. For CodeBERT, removing the language component yielded an absolute increase in Mean Reciprocal Rank (MRR) of up to +17 points.

3. Impact on Pre-aligned Models

Models that undergo explicit cross-lingual alignment during pre-training (e.g., UnixCoder) show less benefit from these post-hoc methods on the tasks they were aligned for. However, for unaligned models or tasks, the improvements are substantial.

Results

  • Text-to-Code Search: Improvements of up to +8 absolute MRR were observed, provided the "syntax" of the English natural language query was also projected out.
  • Language Bias Mitigation: In "Source Included" scenarios—where the database contains the query's own language—baseline models often fail by over-retrieving same-language snippets regardless of logic. CS-LRD recovers significant performance, boosting CodeT5+ by +11.76 MRR.

Conclusion and Future Work

This research demonstrates that simple linear algebra can effectively decouple syntax from semantics in code LLMs. While highly effective for encoder-based architectures, future work is needed to determine if these properties hold for the decoder-only models that currently dominate the field.

Summary updated

BibTeX
@inproceedings{utpala2024language,
  author = {Saiteja Utpala and Alex Gu and Pin-Yu Chen},
  title = {{Language Agnostic Code Embeddings}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.naacl-long.38},
}
Figure