In the context of the Transformer architecture, the query key value (QKV) refers to a set of vectors that are derived from the input sequence and are used to compute the attention weights.
The QKV vectors are derived from the input sequence by applying three linear transformations: query, key, and value. The query transformation maps the input sequence to a set of vectors that represent the context in which the attention is being applied. The key transformation maps the input sequence to a set of vectors that represent the information that the model is attending to. The value transformation maps the input sequence to a set of vectors that represent the importance of each item in the context.
The QKV vectors are typically computed as follows:
- First, the input sequence is split into multiple segments, called "keys," "values," and "queries." The keys and values represent the input sequence, and the queries represent the context in which the attention is being applied.
- Next, linear transformations are applied to the keys, values, and queries to produce the QKV vectors. The query transformation is typically a learned linear transformation that maps the queries to a higher-dimensional space. The key transformation is typically a learned linear transformation that maps the keys to a higher-dimensional space. The value transformation is typically a learned linear transformation that maps the values to a higher-dimensional space.
- The QKV vectors are then computed by taking the dot product of the queries and keys and applying a softmax function to the results. The softmax function maps the dot product scores to a set of probabilities that represent the attention weights. The attention weights are then used to compute a weighted sum of the values, which produces the output of the attention layer.
The QKV vectors are used to compute the attention weights, which are used to selectively focus on different parts of the input sequence. The attention mechanism allows the model to selectively focus on different parts of the input sequence, allowing it to capture long-range dependencies and handle input sequences of varying lengths.
In summary, the query key value mechanism in transformers is a way of computing attention weights that allows the model to selectively focus on different parts of the input sequence. The QKV vectors are derived from the input sequence by applying linear transformations, and are used to compute the attention weights that are used to produce the output of the attention layer.