8
$\begingroup$

The seminal Attention is all you need paper (Google Brain team, 2017) 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.

Both Figure 2 of the paper and equations explaining Attention and Multihead attention start with $Q$,$K$,$V$ already there :

enter image description here

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.". If this is the case, then why isn't the computing of $Q$,$K$,$V$ made more clear in the paper, at the very least for the task of language translation for which they show some numerical results and so obviously did compute $Q$,$K$,$V$ in some way ?

  • I also see some answers (eg this one on the same question) which say that $Q$, $K$, $V$ are the result of multiplication of the input embedding with some matrices $W$. This is also what is shown in the popular blog post The Illustrated Transformer :

enter image description here

These "projection" matrices ($W^Q$, $W^K$, $W^V$) do seem to appear in the the definition of attention in the definition of $head_i$ (see top figure), but according to that equation, these matrices are multiplied by $Q$, $K$, $V$ (still appearing out of thin air, so the problem of their definition remains) and so the resulting product can't also be $Q$, $K$, $V$.

How are the $Q$, $K$, $V$ values computed ?

$\endgroup$
6
  • 3
    $\begingroup$ 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. $\endgroup$
    – Chillston
    Feb 15, 2023 at 10:30
  • 1
    $\begingroup$ @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). $\endgroup$
    – Soltius
    Feb 15, 2023 at 11:46
  • $\begingroup$ 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. $\endgroup$
    – Chillston
    Feb 15, 2023 at 12:34
  • 1
    $\begingroup$ 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" $\endgroup$
    – Soltius
    Feb 15, 2023 at 12:56
  • 1
    $\begingroup$ 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 ! $\endgroup$
    – Soltius
    Feb 15, 2023 at 13:53

2 Answers 2

5
$\begingroup$

(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 projection 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 : ![enter image description here And Figure 2 right could be modified accordingly :
enter image description here

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, $i.e.$ 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, $i.e.$ what they call $V$ is $XW^V_i$ in my suggested re-writing.
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – orome
    Apr 20, 2023 at 19:52
  • $\begingroup$ This makes sense to me and I think you kinda nailed it. It's wild that this is the first I'm seeing this but I do believe you're correct. $\endgroup$ Jan 8 at 18:14
0
$\begingroup$

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

enter image description here

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).

$\endgroup$
1
  • $\begingroup$ This assumes that there are no biases in the QKV stuff (none are mentioned in anything I've read). $\endgroup$
    – orome
    Apr 20, 2023 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .