Understanding How CodeLLMs (Mis)Predict Types with Activation Steering

Francesca Lucchetti, Arjun Guha

Abstract

Code-focused Large Language Models (CodeLLMs) demonstrate impressive capabilities in type prediction but remain brittle to simple syntactic changes. This paper investigates whether these failures stem from a lack of knowledge or a failure to activate it. By employing activation steering—a white-box technique that modifies internal hidden states—we demonstrate that CodeLLMs possess a robust, language-agnostic "type subspace." Our results show that steering can recover accurate predictions in 50-80% of adversarial cases, particularly when applied to middle layers, suggesting that models maintain latent semantic understanding even when surface-level syntax is misleading.

Introduction: The Brittleness of CodeLLMs

While Large Language Models (LLMs) have revolutionized automated programming tasks, their understanding of code semantics often appears superficial. A common failure mode occurs during type prediction in statically typed languages like Python and TypeScript. Minor, semantics-preserving changes—such as renaming a variable from count to x—can cause a model to flip from a correct prediction (e.g., int) to a hallucinated one. This brittleness raises a fundamental question: do these models truly "reason" about data flow and program logic, or are they merely associating common variable names with specific types?

Methodology: Probing Latent Knowledge

To investigate this, we developed a two-stage experimental framework focused on identifying and then correcting these "shallow" failures.

1. Adversarial Dataset Construction

We began by identifying code snippets that models correctly predicted. We then applied a series of "semantics-preserving edits" to these snippets. These mutations included renaming user-defined types, renaming variables, and removing existing type annotations. The goal was to create "hard" adversarial examples where the underlying logic remains identical to the "easy" version, but the model's prediction fails due to the altered syntax.

Examples of semantics-preserving edits used to create adversarial code snippets.

2. Activation Steering

To determine if the model still "knows" the correct type despite its incorrect output, we utilized activation steering. This technique involves calculating a steering vector ($v_\ell$) by averaging the difference between internal activations on "easy" (correct) and "hard" (incorrect) versions of the same code. During inference on a failing program, this vector is added back into the model’s hidden layers to "steer" it toward the correct reasoning path.

Key Results and Insights

Accuracy Recovery and the "Middle Layer" Sweet Spot

Our experiments across multiple models—including Qwen 2.5 Coder, CodeLlama, and StarCoder—revealed that activation steering significantly restores performance. Steering in the middle-to-late layers corrected mispredictions in 50% to 60% of cases on average. For specific edits like "type renaming," recovery rates reached as high as 80-90%.

Steering performance across various models and edit types in TypeScript, showing accuracy recovery as a function of the steering start layer.

As shown in the figure above, there is a distinct "sweet spot" for steering. Accuracy typically remains low when steering starts at the very earliest layers but climbs sharply around the middle of the network (relative layer 0.4 to 0.6). This suggests that the model transitions from raw syntactic processing to semantic "type reasoning" in these intermediate layers.

The Universal Type Subspace

One of the most significant findings is the existence of a cross-language "type subspace." We found that steering vectors calculated using TypeScript data were remarkably effective at fixing mispredictions in Python code, and vice versa. This implies that CodeLLMs develop a language-agnostic internal representation of types that transcends specific syntax or language-specific keywords.

Discussion: Knowledge vs. Activation

The success of steering demonstrates that the failure of CodeLLMs on adversarial inputs is not a "knowledge gap" but an "activation gap." The model possesses the latent information required to solve the problem, but the adversarial syntax prevents the model from aligning its internal state with that knowledge. Furthermore, steering proved far more effective than traditional In-Context Learning (ICL). While providing examples in a prompt (ICL) had negligible impact on these specific failures, direct internal manipulation was robust.

Conclusion

This research highlights that CodeLLMs are more than just pattern matchers; they build sophisticated internal models of program semantics. Activation steering provides a powerful tool for both diagnosing these failures and uncovering the latent capabilities of LLMs. Future work should explore whether similar steering mechanisms can be found for more complex semantic properties, such as control flow or security vulnerabilities.

Summary updated

BibTeX
@misc{lucchetti2024understanding,
  author = {Francesca Lucchetti and Arjun Guha},
  title = {{Understanding How CodeLLMs (Mis)Predict Types with Activation Steering}},
  year = {2024},
  eprint = {2404.01903},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.01903},
}
Figure