GPT2 always has largest attention on first token?
I am trying to print attentions from GPT2 model, and find something strange.

```
import torch
from transformers import *
device = torch.device("cuda")

tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
config = GPT2Config.from_pretrained("gpt2", output_attentions=True)
model = GPT2LMHeadModel.from_pretrained("gpt2", config=config, cache_dir="./cached").to(device)

input_text = "hello, this is Tom ."
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
logits, _, attns = model(input_ids)
last_layer_attns = attns[-1].squeeze(0) 
last_layer_attns_per_head = last_layer_attns.mean(dim=0)  # (sequence_length, sequence_length)
print(last_layer_attns_per_head[-1])
```
Output:
> tensor([0.6496, 0.0758, 0.0439, 0.0328, 0.0853, 0.1125], device='cuda:0', grad_fn=<SelectBackward>)

I have also tried different sentences but the distributions of attn are the same -- the first token always has the largest attention. Is there anything wrong in my code? Or can somebody explain why this the first token has the largest attn?


Hi @sysu-zjw, this is indeed a very interesting observation that you made! 
One thing to notice first is that GPT2 uses casual masking. So when looking at the attention weights ( corresponding to your variable `last_layer_attns_per_head` which I think should actually be called `last_layer_attns_avg_over_heads` ;-) ):

```
[1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[0.8310, 0.1690, 0.0000, 0.0000, 0.0000, 0.0000],
[0.7752, 0.1165, 0.1083, 0.0000, 0.0000, 0.0000],
[0.6962, 0.1208, 0.1039, 0.0790, 0.0000, 0.0000],
[0.8273, 0.0428, 0.0410, 0.0356, 0.0533, 0.0000],
[0.6496, 0.0758, 0.0439, 0.0328, 0.0853, 0.1125]
```
It is obvious that the attention weights of the first token for example can only attend to the first token and so on.
But as you noticed the more interesting part is that also the last token seems to always focus most of its attention on the first token (for the last layer).

I played around with different inputs and also noticed that pretty much the latter half of the transformer layers focus by far most of "its attention" on the first token. After googling a bit, I found that your observations were already put in a paper (check it out [here](https://www.aclweb.org/anthology/W19-4808.pdf) - especially Section 4.2). Looking at Figure 2, you should recognize your observations ;-) 

From my point of view, there is nothing wrong with your code. I think it's a pattern that has been observed but its reason is not well understood.
If you find a good explanation for GPT2's behavior in this case (might also be very similar for other transformer architectures), let me know!

<|||||>@patrickvonplaten Thanks much for your detailed comment :) I will post here if I find any good explanation.