Dissecting Query-Key Interaction in Vision Transformers

Xu Pan, Aaron Philip, Ziqian Xie, Odelia Schwartz

Introduction

While Vision Transformers (ViTs) have revolutionized computer vision, the "black box" nature of their self-attention mechanism remains a persistent challenge for interpretability. A common intuition is that attention performs "perceptual grouping"—essentially, tokens representing a specific object (like a cat) attend primarily to other tokens representing the same object. However, neuroscience suggests that human vision also relies heavily on contextual information (e.g., recognizing that a cat is often found on a sofa).

This paper introduces a novel framework for interpreting ViTs by applying Singular Value Decomposition (SVD) to the query-key interaction matrix. The authors reveal a fundamental shift in model logic across layers: early layers focus on grouping similar features, while later layers transition to "contextualization," where tokens attend to dissimilar features (like foreground vs. background) to build a richer, more holistic understanding of the scene.

Methodology: Decomposing Attention

Existing interpretability tools often focus on the [CLS] token or individual feature axes in feedforward layers, leaving a gap in understanding how specific "query" features look for specific "key" features within self-attention layers.

To bridge this gap, the authors analyze the query-key interaction matrix, defined as $W_q^\top W_k$. In a standard attention head, the score between two tokens is determined by this matrix. By performing Singular Value Decomposition on this matrix ($W_q^\top W_k = U\Sigma V^\top$), they decompose the interaction into "singular modes."

Each mode consists of a pair of singular vectors: a left singular vector ($u_n$) representing the "query" direction and a right singular vector ($v_n$) representing the "key" direction.

  • Grouping: If $u_n$ and $v_n$ have high cosine similarity, the model is looking for features similar to itself.
  • Contextualization: If they are dissimilar or orthogonal, the model is seeking out different, contextual features.

Key Insights and Results

The study utilizes several datasets—including Odd-One-Out (O3), ImageNet, and ADE20K—to validate these interactions and measure whether tokens attend to their own category, different objects, or the background.

The Grouping-to-Contextualization Shift

Across almost all 16 ViT models tested, there is a clear quantitative trend: early layers exhibit high cosine similarity between query and key vectors (grouping), which steadily decreases in deeper layers as the model begins to look for contextual relationships.

Attention on same objects across layers

In early layers, the probability that a query and key highlight the same object is high (around 0.6). By the final layers, this metric often drops significantly, while attention on the background and different objects increases. Interestingly, training objectives matter: models trained with classification objectives (like standard ViTs or CLIP) show a more pronounced shift toward contextualization in late layers compared to self-supervised models (like SimMIM), which maintain more "local" grouping due to their reconstruction-based goals.

Visual Evidence of Semantic Interpretability

Singular vectors are not just abstract mathematical constructs; they are highly semantic. By projecting image embeddings onto these singular vectors, the authors created "query maps" (red) and "key maps" (cyan).

Visualizing query (red) and key (cyan) maps

In early layers, these maps frequently overlap (creating white areas), indicating perceptual grouping where queries and keys focus on the same features (e.g., the textures of a cake). In late layers, they highlight different but related parts of the scene, such as a query vector highlighting a "fish" while its corresponding key vector highlights the "human hand" holding it. This mirrors mechanisms in the visual cortex, where neurons are modulated by their surroundings to highlight salient stimuli that stand out from the background.

Conclusion and Future Work

The application of SVD to the query-key interaction matrix offers a powerful new lens for understanding Vision Transformers. By proving that attention shifts from perceptual grouping to contextualization, this work demystifies how ViTs process complex scenes.

Future research could explore the role of the Value matrix ($W_v$), which determines what information is actually passed forward, as this study focused exclusively on query-key interactions. Additionally, while this work focuses on vision, the SVD-based approach could easily be adapted to study token interactions in Large Language Models (LLMs).

Summary updated

BibTeX
@inproceedings{pan2024dissecting,
  author = {Xu Pan and Aaron Philip and Ziqian Xie and Odelia Schwartz},
  title = {{Dissecting Query-Key Interaction in Vision Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=dIktpSgK4F},
}
Figure