# "Attention is all you need" paper : How are the Q, K, V values calculated?

The seminal Attention is all you need paper introduces Transformers and implements the attention mecanism with "queries, keys, values", in an analogy to a retrieval system.

I understand the whole process of multi-head attention and such (i.e., what is done with the Q, K, V values and why), but I'm confused on how these values are computed in the first place. AFAICT, the paper seems to completely leave that out. The answers regaridng the origin of Q,K,V I've found so far haven't satisfied me :

• In this similar question, the accepted answer says "The proposed multihead attention alone doesn't say much about how the queries, keys, and values are obtained, they can come from different sources depending on the application scenario.".

• I also see some answers (eg this one on the same question) which say that Q, K and V are the result of multiplication of the input embedding with some matrices. This is also what is shown in the popular blog post The Illustrated Transformer : Why isn't the computing of Q,K,V -be it "left to the application" or "multiplication with matrices" made more clear in the paper, at the very least for the task of language translation for which they show some results and so obviously did compute Q,K,V in some way ? If it is matrix multiplication, are these matrices ($$W^Q$$, etc in the figure of the blog post) trained with backprop jointly with the rest of the network or pretrained ? What are the resulting shapes of Q,K,V ?

• I agree with you that this is missing in the paper and I cannot find any justification for that either. However, currently in your proposed correction, the difference in attention between encoder and decoder is not covered, it only supports self-attention and not the cross-attention between encoder and decoder blocks (Also, why did you leave the $V W_i^V$ in there?). This can be easily adapted and clarified though and then I would definitely favor your notation, which makes it much clearer where the $K$, $Q$, $V$ vectors come from. Feb 15 at 10:30
• @Chillston Thanks for your comment ! I corrected the $V$ typo and proposed a notation which enables to cover both self and cross-attention (and more generally reworded the whole last paragraph). Feb 15 at 11:46
• Nice! AFAIK the cross-attention takes the key and value vectors from the encoder and only the query vector comes from the decoder (here it is described by the guys from Tensorflow). If I'm not mistaken this should translate to $\text{Attention}(YW_i^Q, XW_i^K, YW_i^V)$ using your notation. Feb 15 at 12:34
• That's the way I understand it too, but isn't my current notation saying exactly that ? I don't get why you changed $X$ to $Y$ for $V$ since you say just before that "key and value vectors from the encoder" Feb 15 at 12:56
• OK great ! I'll leave this question the way it is for a couple days in case anyone has some alternative answer and if not, auto-answer it with the edit. Thanks for having taken the time to read and check my supposition ! Feb 15 at 13:53

(OP auto-answer) After having dug further in and read more papers on attention, and with help from Chillston in the comments, I think I've got it narrowed down to an issue of confusing notation. If anyone thinks this is not the right answer, please don't hesitate to submit another one, which I'll mark as correct if I think it's better.

Q, K and V values are defined in the paper, and they do come from multiplication with learnt matrices. Those matrices are $$W^Q_i$$, $$W^K_i$$ and $$W^V_i$$, defined in section 3.2.2 of the paper.

The confusion stems from the fact that the notation used in the multihead attention equation and in Figure 2 (right) of the paper is wrong/confusing.
The equation would be be clearer if it read : And Figure 2 right could be modified accordingly : In this new notation, $$X$$ and $$Y$$ are the inputs to the current attention unit.

• For self attention, we'd have $$X = Y$$ which would both be the previous en/decoder block output (or word embedding for the first encoder block).
• For cross-attention, $$X$$ would be the output of the last encoder block and $$Y$$ the output of the previous decoder block.

Technically, the way it's written in the paper could be correct but you need to consider that $$Q, K, V$$ refer to different tensors when they're written :

• in the multihead(Q,K,V) equation where they represent inputs, ie what they call $$V$$ is $$X$$ in my suggested re-writing ;
• in the attention(Q,K,V) equation where they represent "true" query/key/values, meaning inputs multiplied by projections matrices, ie what they call $$V$$ is $$XW^V_i$$ in my suggested re-writing.
• It would be great to have a weigh (no pun) in on this from someone who has worked in the field and reviewed the paper, because I agree with @Soltius that the notation in the paper is at best confusing and might even be wrong. The alternate version here is certainly both clearer and correct. Apr 20 at 19:52

As I understand it (and I'm not an AI researcher, so any helpful comments from folks who know the topic better will be illuminating) the output of layer $$l \in 1 ...\bf{L}$$, $$\bf{X}^l$$, is where $$a\in 1...A$$ is the head number, and $$f$$ is some function like RELU or whatever and the $$\bf{b}$$s are biases ($$M$$ is the attention mask and $$d_E$$ is the size of the embedding). The first bit corresponds to @Soltius's correction (and the second bit is the FFN). (And $$\underset{\mathsf{vocab}}{\mathsf{softmax}}\left(\bf{X}^L\bf{W}_E^{-1}\right)$$ is what's used in calculating cost).

• This assumes that there are no biases in the QKV stuff (none are mentioned in anything I've read). Apr 20 at 20:09