Attention-likelihood relationship in Transformers

Valeria Ruscio, Valentino Maiorca, Fabrizio SilvestriSubmission to Tiny Papers @ ICLR

Introduction

Despite the ubiquity of Transformer-based models, the internal logic of their self-attention mechanisms remains a subject of intense study. While we know that models attend to specific tokens, the why is often less clear. This paper investigates how large language models (LLMs) handle the "out-of-context" problem. In real-world scenarios, LLMs frequently encounter typos, rare words, or nonsensical sequences.

This research reveals a strong correlation between a token's likelihood (how well it fits the context) and the self-attention it receives. When a model encounters an unexpected or "low-likelihood" word, it strategically shifts its attention away from that word and spreads it uniformly across the surrounding context to resolve the semantic mismatch.

Methodology and Dataset

The researchers employed a likelihood-guided perturbation strategy using frozen autoencoder Transformers, including BERT, RoBERTa, DistilBERT, and XLM-R.

  1. Baseline: They calculated the likelihood of a token in a natural sentence and measured its "token attention" (the attention the token pays to itself) and "sentence attention" (attention paid to the rest of the context).
  2. Perturbation: They replaced a high-likelihood token with a "perturbed" token that had a significantly lower likelihood in that specific context.
  3. Comparison: They measured how the attention distribution changed between the original and perturbed versions.

The study utilized a dataset of approximately 20,000 English sentences extracted from WordNet, filtering for sentences longer than 5 tokens and ensuring perturbed tokens were meaningful words rather than stopwords to avoid negative biases.

Key Insights

  • The "Self-Reliance" Correlation: There is a positive correlation between token likelihood and self-attention. If a word "belongs" in a sentence, the model relies heavily on that token's own embedding to compute its representation.
  • The Contextual Pivot: When a token is an outlier (low likelihood), the model effectively "nullifies" the token's own information. It redirects attention to the surrounding context to try and grasp the semantics from the environment instead.
  • Uniformity of Redirection: Interestingly, when the model stops looking at a "weird" token, it distributes that "lost" attention uniformly across the rest of the sentence, rather than pivoting to just one other important word.
  • Layer-wise Sensitivity: This relationship isn't constant across the model. The correlation is much stronger in the higher (deeper) layers. Higher layers are responsible for abstract semantic features, whereas early layers are more concerned with basic surface-level features and tend to focus on the token itself regardless of context.
Correlation across layers for different models

Experimental Results

The authors tested five models and found consistent behavior across all architectures, suggesting this is a fundamental property of how self-attention handles semantic uncertainty.

Original vs. Perturbed metrics across models

As shown in the results, the attention drop is significant. For instance, in RoBERTa-base (layer 11), the mean token attention dropped from 0.124 in original sentences to 0.043 in perturbed sentences. Furthermore, Spearman correlation values were consistently significant, particularly in the later layers. In RoBERTa-large, the correlation was strongest at the final layer (layer 23).

Limitations and Future Work

  • Model Scope: The study focused exclusively on autoencoder models (masked language models). It remains to be seen if causal (decoder-only) models like GPT-4 exhibit the same attention-likelihood coupling during generative tasks.
  • Domain Specificity: The experiments were limited to English text from WordNet. Future work should explore this phenomenon in other domains, such as Vision Transformers (ViTs), to see if "out-of-context" image patches trigger similar attention shifts.
  • Downstream Impact: While the paper identifies this mechanism, it doesn't explicitly measure how this attention shift affects performance on downstream tasks like sentiment analysis or summarization. Exploring how to leverage this "surprise" signal could lead to more robust models.

Summary updated

Abstract

We analyze how large language models (LLMs) represent out-of-context words, investigating their reliance on the given context to capture their semantics. Our likelihood-guided text perturbations reveal a correlation between token likelihood and attention values in transformer-based language models. Extensive experiments reveal that unexpected tokens cause the model to attend less to the information coming from themselves to compute their representations, particularly at higher layers. These findings have valuable implications for assessing the robustness of LLMs in real-world scenarios. Fully reproducible codebase at [url].
BibTeX
@misc{ruscio2023attention,
  author = {Valeria Ruscio and Valentino Maiorca and Fabrizio Silvestri},
  title = {{Attention-likelihood relationship in Transformers}},
  year = {2023},
  eprint = {2303.08288},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2303.08288},
}
Figure