Investigating Neurons and Heads in Transformer-based LLMs for Typographical Errors

Kohei Tsuji, Tatsuya Hiraoka, Yuchang Cheng, Eiji Aramaki, Tomoya IwakuraEMNLP 2025

TL;DR

This paper dives into the "black box" of Large Language Models (LLMs) to understand how they handle typos. They found that specific "typo neurons" and "typo heads" are activated to correct errors, utilizing both local and global contextual information. This research is crucial for building more robust LLMs that can better understand and process imperfect human language.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly used in real-world applications, but they often encounter inputs with typographical errors (typos). While LLMs can sometimes correct these errors and maintain performance, typos can also degrade their accuracy and lead to unexpected behavior. Understanding how LLMs process and fix these errors internally is key to improving their robustness and reliability. Previous research has primarily focused on the effects of typos on LLM performance or on methods to improve robustness, with fewer studies investigating the internal mechanisms. This paper aims to fill that gap by identifying and analyzing the specific components within Transformer-based LLMs (neurons and attention heads) that are responsible for handling typos.

Methods

The authors developed a methodology to identify "typo neurons" and "typo heads" by comparing the activation patterns of these components when presented with clean inputs versus inputs containing typos.

  • Typo Neuron Identification: They used a word identification task and calculated an "An" score for each neuron, representing its specific response to typo inputs compared to clean and split (tokenization-difference controlled) inputs. Neurons with the highest An scores were identified as typo neurons.
  • Typo Head Identification: Similarly, they calculated a "Δh" score for attention heads, measuring their differential response to typo inputs. Heads with the largest absolute Δh scores were identified as typo heads.
  • Ablation Studies: To verify the role of these identified neurons and heads, the researchers performed ablation experiments. They deactivated these components and measured the impact on the model's accuracy for both clean and typo inputs.
  • Analysis of Context: The study also investigated whether typo-fixing relied on local (nearby tokens) or global (wider context) information by analyzing the distribution of typo neurons and heads across different layers of the Transformer architecture.

Datasets

The study utilized a custom dataset created for a word identification task.

  • Clean Dataset: Derived from WordNet, this dataset consisted of word-definition pairs where LLMs were expected to identify the correct word from its definition. The top 5,000 (or 1,000 for smaller models) word-definition pairs with the highest likelihood for the correct word were selected to ensure models could answer correctly without typos.
  • Typo Dataset: Generated by introducing single-letter or digit typos into the definition part of the clean dataset.
  • Split Dataset: Created to control for differences in tokenization length between clean and typo inputs. This dataset contained samples with tokenizations that matched the length of typo inputs but without actual typos.

The experiments were conducted on several LLMs: Gemma 2 (2B, 9B, 27B), Llama 3.2 (1B, 3B), Llama 3.1 (8B), and Qwen 2.5 (3B, 7B, 14B, 32B).

Key Insights and Ideas

  • Dual-Context Typo Fixing: LLMs don't just fix typos using immediate surroundings. They employ both local context (early/late layers) and global context (middle layers) to recover the intended meaning.
  • Middle Layers as Global Context Hubs: Typo neurons in the middle layers are particularly crucial for typo-fixing that leverages global context.
  • Attention Heads as Broad Contextualizers: Typo heads tend to consider the wider context rather than focusing on specific tokens, contributing to a more holistic error correction.
  • Typo Neurons/Heads for General Understanding: These specialized components aren't only for typos; they also play a role in understanding general grammatical and morphological features, suggesting that improving general language understanding could enhance typo robustness.
  • Model Size Matters: Larger models generally show better robustness to typos, but the distribution of typo neurons and heads can vary significantly across models and sizes, indicating different internal strategies.

Key Results

  • Neuron Distribution Varies by Model: Gemma 2 models showed more typo neurons in early layers, while Llama 3 and Qwen 2.5 had more in late layers. However, all models had significant typo neuron activity in the middle layers.
  • Head Behavior is Context-Aware: Typo heads showed a tendency to attend to wider contexts, as indicated by negative Δh scores.
  • Ablation Confirms Importance: Ablating identified typo neurons and heads led to a significant drop in accuracy on typo inputs, confirming their role in typo-fixing. This effect was more pronounced in smaller models.
  • Typo Neurons Aid General Language Tasks: Ablating typo neurons also slightly decreased performance on clean inputs, suggesting they contribute to general linguistic understanding.
  • Consistency Across Typos: Typo neurons and heads remained consistent in their behavior even when the number of typos increased (from t=1 to t=16), indicating a stable mechanism for handling errors.

Limitations and Future Work

  • Distinguishing Fixing vs. Damage: The current method can identify components that respond to typos but cannot definitively distinguish between those that actively fix them and those that are damaged by typos.
  • Model and Architecture Specificity: The study was limited to specific Transformer architectures (Gemma, Llama, Qwen) and model sizes. Different architectures or larger models might exhibit different behaviors.
  • Hyperparameter Sensitivity: Experiments were conducted with a limited number of typos (t=1 and t=16). The impact of other hyperparameter settings and a wider range of typo types (beyond insertion) was not explored.
  • Task Specificity: The analysis was focused on a word identification task. The findings might differ for other downstream tasks.
  • Head Ablation Nuances: Head ablation results were less clear for larger models, suggesting that these models might rely on the coordinated function of many heads rather than a few specialized ones. The reason for this difference (e.g., training without dropout) remains unclear.
  • Future Work: The authors suggest investigating the relationship between training data typos and model robustness, exploring the learned mechanisms for local vs. global typo recognition, applying the method to different perturbation types (e.g., token replacement), and analyzing multiple checkpoints of the same model. They also propose exploring how to leverage these findings to develop methods for improving LLM robustness against typos.

Summary updated

BibTeX
@inproceedings{tsuji2025investigating,
  author = {Kohei Tsuji and Tatsuya Hiraoka and Yuchang Cheng and Eiji Aramaki and Tomoya Iwakura},
  title = {{Investigating Neurons and Heads in Transformer-based LLMs for Typographical Errors}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
}
Figure