Abstract
Large pre-trained language models have recently been expanded and applied to programming language tasks with great success, often through further pre-training of a strictly-natural language model–where training sequences typically contain both natural and (linearised) programming language. Such approaches effectively map both modalities of the sequence into the same embedding space. However, programming language keywords (e.g. “while”) often have very strictly defined semantics. As such, transfer learning from their natural language usage may not necessarily be beneficial to their code application and vise versa. Assuming an already pre-trained language model, in this work we investigate how sequence tokens can be adapted and represented differently, depending on which modality they belong to, and to the ultimate benefit of the downstream task. We experiment with separating embedding spaces between modalities during further model pre-training with modality-relative training objectives. We focus on text-to-code generation and observe consistent improvements across two backbone models and two test sets, measuring pass@k and a novel incremental variation.
Introduction
Most modern Code Large Language Models (Code LLMs) treat natural language (NL) and programming language (PL) as a single modality, mapping every token into a shared embedding space. However, this approach ignores the fundamental semantic differences between the two. For example, the word "while" in a docstring acts as a temporal connector, whereas in code, it represents a specific control flow structure. Forcing these distinct meanings into the same vector space can create conflicting signals during training. This paper introduces Modality-relative Pre-training (MRPT), a method that treats docstrings and code as distinct modalities to resolve these semantic conflicts.
Methodology
Space Separation Strategies
The authors propose two primary strategies for separating the embedding spaces of natural and programming languages:
- Partial Embedding Separation (PES): Only tokens belonging to the programming language's grammar (e.g., keywords like
if,for,return) receive separate embeddings for code versus text. This minimizes vocabulary expansion while protecting core logical semantics. - Full Embedding Separation (FES): Every token in the vocabulary is assigned two distinct embeddings—one for NL and one for PL—effectively doubling the embedding layer size.
Training Objectives
To optimize the model for text-to-code generation, four training objectives were evaluated:
- TEXT-CODE-CLM: Standard causal language modeling across the entire sequence.
- CODE-CLM: Calculating loss only on the code portion, forcing the model to predict code given a docstring.
- CORRUPT-CODE-CLM: Randomly masking tokens in the docstring to force the model to rely on broader context.
- PREFIX-CODE-CLM: Utilizing bidirectional attention on the docstring (treating it as a prefix) while generating code.
Evaluation and Results
The models were pre-trained on a 120GB dataset of Python code and evaluated using HumanEval and MBPP benchmarks. Additionally, the authors introduced incremental pass@k, a metric that evaluates a model's ability to complete code line-by-line.
Key findings include:
- Performance Gains: On HumanEval, the 350M parameter PanGu-Coder model using PES achieved a pass@1 of 21.34%, significantly outperforming the modality-agnostic baseline of 16.46%.
- Incremental Success: The incremental pass@k metric showed improvements of up to +9.5 points, indicating that modality separation significantly enhances the model's ability to "fill in the blanks" during code completion.
- Efficiency: The 350M parameter model outperformed significantly larger models, such as the 1.3B parameter INCODER, on several key metrics.
- Semantic Drifting: Visualizations confirmed that tokens like
openmove closer to functional code terms likereadandwritein the code space, while remaining near terms likeclosedin the natural language space.
Conclusion
Modality-relative Pre-training demonstrates that separating the representation of text and code leads to more robust and accurate code generation. While the current study focused on Python and function-level generation, the results suggest that embedding separation is a scalable technique for improving LLMs. Future work will explore multi-language support and more efficient methods for identifying semantically conflicted tokens.
Summary updated