Abstract
Large Language Models (LLMs) demonstrate remarkable proficiency in programming tasks, yet the internal mechanisms by which they navigate multiple programming languages (PLs) remain largely opaque. This paper investigates the internal representations of code in models like Llama 3.1 and CodeLlama. We find that LLMs utilize English as a "conceptual detour" in intermediate layers, mapping diverse code syntax to a shared English-centric logic space. Furthermore, while specific neurons handle language-specific syntax in the early and late layers, middle layers exhibit high cross-lingual alignment, particularly among structurally similar languages like Java and C#. These findings suggest that code models develop a highly integrated "concept space" rather than isolated language modules.
Introduction
While interpretability research has extensively explored how LLMs handle natural languages, the "black box" of multilingual coding remains under-explored. Modern models are often capable of translating logic across dozens of programming languages, raising fundamental questions: Does the model use a universal internal language? Can we isolate neurons responsible for specific syntax? How do different languages influence one another's representations?
This study addresses these questions by analyzing the internal activations of CodeLlama 7B and Llama 3.1 8B. By mapping the trajectory of tokens through the model's layers, we reveal a sophisticated architecture that balances language-specific processing with a centralized, abstract conceptual core.
Methodology
To dissect the models' internal logic, the researchers employed three primary interpretability techniques:
- Logit Lens: This method involves applying the model's final "unembedding" matrix to intermediate layers. This allows researchers to "peek" at the model's internal state and identify which tokens (or concepts) the model is prioritizing before it reaches the final output layer.
- MEXA (Cross-lingual Alignment): This metric measures the similarity between internal representations of different languages. High alignment indicates that the model is processing two different languages (e.g., a Python
forloop and a Javaforloop) using the same internal conceptual framework. - LAPE (Language Activation Probability Entropy): This technique identifies "language-specific neurons" by calculating which neurons fire consistently for one language while remaining inactive for others. The researchers also conducted ablation studies, deactivating these neurons to observe the impact on model performance (measured via perplexity).
Data Sources
The analysis utilized a diverse set of data to ensure robust results:
- Super-parallel PL Dataset: 581 parallel code snippets across seven languages (C++, Java, Python, C#, JavaScript, PHP, and C) sourced from GeeksforGeeks.
- CodeParrot (GitHub): Raw code from 11 popular programming languages for neuron activation analysis.
- English Wikipedia: A baseline for natural language comparison.
- Keyword Sets: A curated list of keywords for 22 programming languages to categorize tokens identified via the Logit Lens.
The "English Detour" and Layer-wise Specialization
A primary discovery of this research is the "English Detour." Much like how LLMs often translate foreign natural languages into English internally, they perform a similar operation with code. In the middle layers of the model, the "concept space" is dominated by English keywords and logic, regardless of the input or target programming language.
The model's architecture appears to be organized into three distinct functional zones:
- Bottom Layers: These layers contain language-specific neurons that handle basic syntax and initial token processing.
- Middle Layers: These are characterized by high alignment and shared representations. This is where the model processes abstract logic and where the English detour is most prominent.
- Top Layers: As the model approaches the output, it utilizes "exclusive" neurons to convert the abstract logic back into the specific syntax required for the target language.
Cross-lingual Alignment and Neuron Sharing
The study found that cross-lingual alignment scores reach nearly 0.9 (on a 0-1 scale) in the layers immediately preceding the output. This suggests a highly successful mapping of disparate syntaxes to a single underlying concept.
Interestingly, C# emerged as a "hub" language, showing the highest alignment with other languages. This is likely due to its descriptive keyword set and its structural position within the C-family of languages, making it a central point in the model's internal map. Conversely, for highly similar languages like Java and C#, the model uses almost identical "hardware," making it nearly impossible to isolate neurons that fire exclusively for one but not the other.
Key Results and Comparative Analysis
One of the most surprising findings was the comparison between Llama 3.1 8B (a general-purpose model) and CodeLlama 7B (a code-specialized model). Llama 3.1 demonstrated superior internal alignment and more clearly identifiable language-specific neurons. This suggests that training on massive volumes of general natural language text (15 trillion tokens) provides a more robust foundation for building a "concept space" for code than specialized training alone.
Furthermore, the ablation studies confirmed the functional role of specialized neurons. Deactivating as few as 400 language-specific neurons significantly increased the model's perplexity (confusion) for that specific language while leaving other languages unaffected. This provides causal evidence that these neurons are indeed dedicated to specific linguistic structures.
Limitations and Future Work
While insightful, the study notes several areas for expansion:
- Language Diversity: The super-parallel dataset was limited to seven common languages; future research should include functional or niche languages like Haskell.
- Semantic Mapping: Future iterations could move beyond simple keyword matching to a more sophisticated semantic dictionary that maps PL keywords directly to their English equivalents (e.g.,
elifto "else if"). - Architectural Scope: The findings are currently centered on the Llama architecture, and it remains to be seen if other architectures like Mistral or Falcon exhibit the same "English detour" patterns.
Conclusion
This research reveals that LLMs do not treat programming languages as isolated silos. Instead, they build a unified internal map where English serves as the logical bridge between different syntaxes. By understanding how neurons are shared and specialized across languages, we can move toward building more efficient, debuggable, and specialized models for the future of software development.
Summary updated