Dissecting Query-Key Interaction in Vision Transformers

Self-attention in vision transformers is often thought to perform perceptual grouping where tokens attend to other tokens with similar embeddings, which could correspond to semantically similar features of an object. However, attending to dissimilar tokens can be beneficial by providing contextual information. We propose to analyze the query-key interaction by the singular value decomposition of the interaction matrix (i.e. ${\textbf{W}_q}^\top\textbf{W}_k$). We find that in many ViTs, especially those with classification training objectives, early layers attend more to similar tokens, while late layers show increased attention to dissimilar tokens, providing evidence corresponding to perceptual grouping and contextualization, respectively. Many of these interactions between features represented by singular vectors are interpretable and semantic, such as attention between relevant objects, between parts of an object, or between the foreground and background. This offers a novel perspective on interpreting the attention mechanism, which contributes to understanding how transformer models utilize context and salient features when processing images.

Paper

References (46)

Scroll for more · 34 remaining

Similar papers

Peer review

Reviewer Quv73/10 · confidence 4/52024-07-10

Summary

The paper proposes SVD as a way for analyzing the interaction between the Key and Query vectors within the self-attention architecture. To this end, it measures the cosine similarity between the left and right eigenvectors of the attention score. The proposed approach evaluates the proposed mechanism on different configurations of DeiT, CLIP, DINO, and VIT. One of the findings of the paper which is the difference in the type of interaction in different layers is interesting, though to some-level could be expected given the higher abstraction level seen in later layers.

Strengths

* The paper takes an interesting approach for analyzing the interaction between queries and keys in self-attention. * The finding on the nature of contribution or interaction of different layers between the keys and queries is interesting.

Weaknesses

* The paper seems to be written in a haste. For example, the figures almost miss the proper x-axis labeling. * The visualizations of the eigenvectors back-projection seems not relating properly with the claims, in most of visualization there is a diverse set of locations attended and it is not obvious how the left and right eigenvector back-projections actually relate. * The approach seems to be specifically limited to self-attention mechanism and not obvious how it may scale to other architectures * It is not also obvious how the scores are averaged to be visualized for different layers and over the samples in the dataset. Slight better elaboration on the dateset and the characteristic of the data would have been helping. * While a semantically meaningful approach is proposed, the experiments are mostly focused on object-object and foreground-background interaction. * The proposed approach could seems to be usable as a posthoc approach.

Questions

* How could one connect the proposed approach to the different outputs of the model? For example, in a classification tasks, how could one propagate the interaction of Q-K vectors for different classification scores? * Could you further elaborate on scaling this to other architectures than self-attention?

Rating

3

Confidence

4

Soundness

2

Presentation

2

Contribution

2

Limitations

The authors discuss the limitations of the method to some degree.

Reviewer EcHv7/10 · confidence 4/52024-07-11

Summary

While previous studies on vision transformers focused on how self-attention groups relevant tokens, this paper analyzes how self-attention contextualizes tokens to understand comprehensive inter-token relationships across the entire image. To this end, this paper proposes using the Singular Value Decomposition (SVD) to analyze the query-key interaction $\textbf{W}^{\top}_q \textbf{W}_k$. Each singular vector of the query-key projection layers can be interpreted as capturing a certain type of visual semantic information. Thus, similarities between left and right singular vectors can reveal how different visual semantics interact with each other. Through extensive analysis, this paper concludes that vision transformers tend to first attend to similar tokens to form local visual semantics, and then attend to dissimilar tokens to capture global contexts of the image.

Strengths

S1. **Writing**: The paper is well written and clearly motivated. S2. **Novelty**: The proposed analysis via SVD is innovative. S3. **Technical soundness**: The justification for using SVD is well-founded theoretically and supported by solid quantitative and qualitative empirical analysis. S4. **Potential for broader application**: Despite being applied to image analysis, the proposed technique is generic and could potentially be applied to various domains, including video and audio understanding..

Weaknesses

W1. **Familiar conclusion**: The general finding of this paper, i.e., group early and contextualize later, is already well known from many earlier papers [a,b]. This paper only reconfirms the same conclusion in an explainable manner. W2. **Limited analysis scope**: This paper lacks analysis on different training objectives such as masked image modeling (MIM) [c,d] or masked feature prediction (JEP) [e,f]. Previous literatures [a,b] show that training objectives determine where the learner focuses; ViT with supervised training or instance discrimination self-supervision (SimCLR or MoCo) act similar as what this paper found, but ViTs trained with MIM shows that the learner still focuses on local tokens in a deeper layer. The observations in L6-8, 45-47, 138-141 might only be correct for certain training objectives. Justification of rating: \ Despite not deriving a novel conclusion and lacking analysis on other training objectives, the proposed SVD-based analysis technique is novel, provides interpretability on query-key interactions, and is technically sound. It has potential applications in various domains. [a] Xie et al., “Revealing the Dark Secrets of Masked Image Modeling,” CVPR, 2023.\ [b] Shekhar et al., “Objectives Matter: Understanding the Impact of Self-Supervised Objectives on Vision Transformer Representations,” ICLRW, 2023.\ [c] Xie et al., “SimMIM: A Simple Framework for Masked Image Modeling,” CVPR, 2022.\ [d] He et al., “Masked Autoencoders Are Scalable Vision Learners,” CVPR, 2022.\ [e] Assra et al., “Self-Supervised Learning from Images with a Joint-Embedding Predictive Architecture,” CVPR, 2023.\ [f] Baevski et al., “data2vec: A General Framework for Self-supervised Learning in Speech, Vision and Language,” ICML, 2022.

Questions

Q1. In Fig. 3 & S3, there are slight increases in weighted cosine similarity in the final layers for all models except for the original ViTs. Does this imply that ViTs group similar tokens again in the deepest layers? Please elaborate on why this happens and what information ViTs capture in these layers. Q2. For Fig. 4, visualizing attention maps of singular modes with fixed input images would help readers understand how ViTs adapt their focus according to the layer. Q3. How does the diversity of visual information captured by singular vectors change across layers? In other words, how does the redundancy between singular vectors vary?

Rating

7

Confidence

4

Soundness

4

Presentation

3

Contribution

2

Limitations

The authors adequately address both the limitation and future directions of their method.

Reviewer EcHv2024-08-14

Thank you for the thorough analysis of the raised question. I'm satisfied with the response. I strongly suggest including new results during the rebuttal in the final manuscript. I'll raise my rating to 7.

Reviewer PNQ87/10 · confidence 4/52024-07-13

Summary

The paper begins with the observation that self-attention in early layers of vision transformers tend to group similar objects while deeper layers focus more on gathering features from dissimilar objects or background. The paper then delves into the mathematical formulation of the attention mechanism, and reveals using SVD that the aforementioned behaviour is a result of the similarity or dissimilarity between the corresponding left and right singular vector of the projection matrices $W_q$ and $W_k$. These results are well supported by empirical evidence on numerous vision transformer backbones and rich visualisations.

Strengths

1. The paper eases into the investigated problem with numerous intuitions and visualisations, which are backed up with more rigorous deductions. The narrative of the paper, as a result, is rather clear and easy to follow. 2. The paper presents an interesting finding on the behaviour of commonly used vision transformers and provides insights behind such behaviours. The finding that early layers of vision transformers group similar visual cues while deep layers extract more contextual features can help deepen the community's understanding of the dynamics within the transformer architecture. 3. The paper utilises SVD to study the product of the projection matrices for keys and queries. This methodology may be applied to understand interesting behaviours in other applications. For instance, transformer-based object detector, notably DETR, eliminates the need of non-maximum suppression by employing the self-attention mechanism amongst object queries. And self-attention was demonstrated to have suppressive behaviours.

Weaknesses

1. The behaviour of deeper layers, i.e., tokens attending to dissimilar tokens, can use some more analysis. For instance, the authors stated in lines 238-239 that tokens around the fish attending to regions containing the human may add the attribute "be held" to the fish tokens. This can be easily tested by, say, manually overriding the corresponding attention scores to zero and observe if the resultant image feature has reduced cosine similarity against the language embedding "a photo of a person holding a fish". This can be easily done on a CLIP model. Analysis such as this will further deepen our understanding of transformers' behaviour, whereas simply saying deeper layers extract contextual information sounds very hollow. 2. The visualisations, such as those in Figure 4 and the appendix, only show one particular mode in one head. It would be more interesting to focus on a single image and visualise how the modes in different layers and different heads process this particular image. This builds a complete and coherent narrative around the behaviour of the model, which would include how it extracts lower-level features and the high-level features. I believe this will greatly benefit the paper.

Questions

N/A

Rating

7

Confidence

4

Soundness

3

Presentation

3

Contribution

3

Limitations

The authors pointed out the potential behavioural differences across different models and training techniques. In addition, the study in the paper was only conducted on query-key interactions, and the authors plan to investigate the role of value projection in the future.

Reviewer LpZi8/10 · confidence 5/52024-07-14

Summary

This paper proposes a new analysis framework to dissect the potential bottom mechanism of query-key interactions in Vision Transformers (ViTs) from the perspective of singular value decomposition. Several phenomena are presented via extensive quantitative and qualitative results, which leads to the basic conclusion that earlier layers in ViTs tend to conduct grouping among similar tokens and deeper layers in ViTs are more likely to take on the role of contextualization to connect dissimilar tokens.

Strengths

1.This paper investigates the working mechanism of query-key interactions in the popular ViTs, which is of great importance to enhance the explainability of transformer models but has been rarely explored before. 2.A novel conclusion is given that the self-attention layers are not limited to conduct grouping among similar tokens. In deeper network layers, they also perform something like contextualization over dissimilar tokens to extract higher-level semantics. 3.In addition, a new analytical tool for ViTs is provided in this work to visualize the attention preference of different self-attention layers, i.e., calculating the inner product between visual tokens and singular vectors of query/key weight matrices. This could benefit future research to conduct visualization analysis to facilitate the model development.

Weaknesses

1.Analysis conducted in this work is mainly restricted to the ImageNet dataset, where the images are more focused on relatively simpler scenes and objects. To make the conclusions more general, experiments on samples with more complex visual scenes are more helpful. 2.The ViT models investigated in this work are mainly pretrained vision models with general training objectives. It could be more comprehensive and more intetesting to see how the ViT models fine-tuned under specific downstream tasks will behave.

Questions

Although the interaction mechanism between queries and keys in ViTs are explored in this work, how the value projection layers take effect and how the query-key itneraction matrices coordinate with the value tokens to influence the output features remain unclear. I suggest the authors to conduct further studies in future on these points.

Rating

8

Confidence

5

Soundness

3

Presentation

4

Contribution

4

Limitations

The current limitations have been discussed by the authors in the manuscript.

Area Chair DSCv2024-08-09

Dear Reviewers, Thank you very much again for your valuable service to the NeurIPS community. As the authors have provided detailed responses, it would be great if you could check them and see if your concerns have been addressed. Your prompt feedback would provide an opportunity for the authors to offer additional clarifications if needed. Best regards, AC

Reviewer LpZi2024-08-10

Thanks for the authors' rebuttal and additional experimental results. After reading the authors' response, my previous concerns have been adequately addressed, so I decide to raise my rating to 8.

Area Chair DSCv2024-08-11

Does the rebuttal address your concerns?

Dear Reviewer Quv7, Thank you again for your time for reviewing this paper. Could you please check if the authors' rebuttal has addressed your concerns at your earliest convenience? Thank you! Best regards, AC

Reviewer EcHv2024-08-11

Response to Authors' Feedback

Thank you for addressing my concerns and questions. I appreciate the thorough analysis and the newly added results with SimMIM, which strengthen the paper. However, I have some remaining points to discuss: - **JEPA Results (W2)**: I respectfully request results on JEPA (Joint Embedding Predictive Architecture). JEPA, which predicts latent representations instead of pixel values, has shown good transferability in both full finetuning and linear probing setups. However, how ViTs trained with JEPA learn different representations compared to contrastive learning or MIM approaches is not well-studied. Analyzing how queries and keys interact under JEPA objective would significantly enhance the paper's contribution. - **Revision of Generalized Statements (W2)**: If query-key interactions vary across different training objectives, statements such as those in lines 6-8, 45-47, and 138-141 should be revised. These statements currently appear to generalize across all training objectives, but they may be limited to specific ones. Please supplement the discussion with query-key interactions observed in other objectives. - **Clarification of Phenomenon (Q1)**: While I appreciate the detailed explanation provided, I'm not fully convinced that it clearly and sufficiently addresses the exact reason for the observed phenomenon. This experimental trend is interesting and warrants further study. If a sufficient explanation is unavailable through this rebuttal, I strongly suggest adding related discussion and posing an open question in the main manuscript at the very least. Given these points, I maintain my current rating. However, I'm open to raising the score if the authors provide sufficiently clear responses to these newly added questions and concerns. Addressing these points would make the paper more comprehensive and valuable to the field.

Authorsrebuttal2024-08-12

Thank you for your insightful feedback and helpful further suggestions. We will address them in the final manuscript. > JEPA Results (W2)… Thanks for suggesting adding JEPA to the analysis. We added “I-JEPA-vit-h14” into our “cosine similarity” plots. We could not attach a new figure for you at this phase, but we can tell you that the trend of “I-JEPA-vit-h14” looks similar to the “SimMIM-vit-b16-finetune”. It doesn’t increase in the last few layers like “SimMIM-pretrain”. Since the I-JEPA encoder performs well in the linear-probing setup, this indicates it is probably more similar to a classification model, which is consistent with our new analysis. The remaining question is why the self-supervised objective doesn’t cause later layers in the I-JEPA encoder to increase its attention to similar tokens. We think this objective is mainly handled by the predictor (also a transformer). To test this, we run the cosine analysis on the predictor transformer, and find the cosine similarity is around 0.3 (considered high) across layers, which is consistent with our hypothesis. Due to the time limit we don’t have the results of “I-JEPA-vit-h14” for the other analyses yet, but it won’t be a problem to finish all the analyses and add them to the final manuscript. > Revision of Generalized Statements (W2)... Thank you very much. Yes, we will change those claims and add new text on the training objective according to our discussion. We will change line 45-47 to “We identify a role of self-attention in a variety of ViTs. In many ViTs, especially those with classification training objectives, early layers perform more grouping in which tokens attend more to similar tokens; late layers perform more contextualizing in which tokens attend more to dissimilar tokens. However, this observation has some variability among models and may depend on the training objective: notably, some self-supervised ViTs tend to increase attention to dissimilar tokens in the last few layers.” We will also make the changes to line 6-8 and 138-141 in a similar way. We will add a paragraph in the discussion section: “Though we find a trend that attention changes from attending more to the similar tokens to dissimilar tokens from early layers to late layers, some ViTs have a more complex trend that increases attention to similar tokens in the last few layers (Fig. 3). Models that have this “concave” trend are SimMIM-vit-b16-pretrain, Dino models, Deit models, and huge ViT models. Most of them either have self-supervised objectives or distillation regularizations. We hypothesize that the last layers may behave differently because they are closer to the training target, and so the training objective may have more influence. We think that self-supervised objectives, such as reconstructing masked patches, require stronger consistency between tokens, and thus more attention is allocated to similar tokens in the higher layers; while the classification objective requires gathering information from different aspects of a scene, and thus more attention is allocated to dissimilar tokens. This hypothesis is supported by the cosine similarity plot (Fig. 3) of the SimMIM models, which shows in the last few layers of the pre-trained model increased attention to similar features. This matches the observation in the literature, that the SimMIM model has more local attention (Xie et al. 2022). However, we find that the SimMIM model fine-tuned on ImageNet classification has the trend of decreased attention to similar features, similar to most of the classification models. Although I-JEPA is trained with a self-supervised objective predicting latent representations, the cosine similarity for the I-JEPA encoder does not show increased attention to similar tokens in the last few layers. The I-JEPA model is known to have excellent linear-probing performance, and thus we think it may behave more similarly to a classification model. The self-supervised objective of I-JEPA may be more apparent in the I-JEPA predictor (also a transformer). When we run the cosine similarity analysis on the predictor module instead of the encoder, we find that the cosine similarity is overall high (Supplementary Figure). The role of the training objective on internal model behavior is an interesting topic for future research.” We will also add the relevant references here for the SimMIM and I-JEPA papers. > Clarification of Phenomenon (Q1)... We agree that this trend is interesting. Our current hypothesis is that this depends on the training objective, such as the need for self-consistency in some self-supervised training objectives versus classification. The experiments with SimMIM and I-JEPA offer interesting observations in this direction. To test these questions more completely is beyond the scope of this paper, and we will include a discussion and pose this as an open question for future research (see the text in the previous point).

Reviewer PNQ82024-08-12

Thanks for the response. I think the per-image visualisations in the attached PDF are better illustrations for the paper's argument. I would recommend adding them to the main paper. Regarding the modes and how they impact the logits, I suspect there may be multiple nodes exhibiting similar attention behaviours, therefore contributing to the corresponding logit concurrently. Nevertheless, I'm generally happy with the response, and would recommend accepting the paper.

Authorsrebuttal2024-08-12

Thank you for your feedback and helpful further suggestions. > I think the per-image visualisations in the attached PDF are better illustrations for the paper's argument. I would recommend adding them to the main paper. Thank you, we will do so. > Regarding the modes and how they impact the logits, I suspect there may be multiple nodes exhibiting similar attention behaviours, therefore contributing to the corresponding logit concurrently. Thank you, this is also our interpretation, and an interesting direction for future work. We will add this to the limitations section as a future research direction.

Program Chairsdecision2024-09-25

Decision

Accept (spotlight)

© 2026 NYSGPT2525 LLC