I think the whole concept of nn.MultiheadAttention is to solve the problem that meaning of a pattern depends on what all patterns it is surrounded by.
for example, in the case of text,

image1104×456 80.7 KB

here, the representation of the word ‘it’ depends on what all words it is surrounded by, in the left case, ‘it’ refers to ‘animal’, in the right case, ‘it’ refers to ‘street’.
so, the representation of ‘it’ is not one fixed embedding.
therefore, when we pass a q, k, v to nn.MultiheadAttention, we would get a matrix, which indicates how similar our query is to each of the values, or how much the representation of our query depends on each of the values.
Another thing is that this concept extends to images also, or in general to a pattern of information, for example,


In the first case, we have a pattern of a green dot, in the second case, it is a pattern of a green traffic light, the pattern did not change, (assume that green color is same in both images), but what it represents changed based on its surrounding.
maybe this is not the best example, you can come up with a better example, but the idea of nn.MultiheadAttention applies to any pattern of information.