Multi-Head Attention

:label:sec_multihead-attention

In practice, given the same set of queries, keys, and values we may want our model to combine knowledge from different behaviors of the same attention mechanism, such as capturing dependencies of various ranges (e.g., shorter-range vs. longer-range) within a sequence. Thus, it may be beneficial to allow our attention mechanism to jointly use different representation subspaces of queries, keys, and values.

To this end, instead of performing a single attention pooling, queries, keys, and values can be transformed with $h$ independently learned linear projections. Then these $h$ projected queries, keys, and values are fed into attention pooling in parallel. In the end, $h$ attention pooling outputs are concatenated and transformed with another learned linear projection to produce the final output. This design is called multi-head attention, where each of the $h$ attention pooling outputs is a head :cite:Vaswani.Shazeer.Parmar.ea.2017. Using fully-connected layers to perform learnable linear transformations, :numref:fig_multi-head-attention describes multi-head attention.

Multi-head attention, where multiple heads are concatenated then linearly transformed. :label:fig_multi-head-attention

Model

Before providing the implementation of multi-head attention, let us formalize this model mathematically. Given a query $\mathbf{q} \in \mathbb{R}^{d_q}$, a key $\mathbf{k} \in \mathbb{R}^{d_k}$, and a value $\mathbf{v} \in \mathbb{R}^{d_v}$, each attention head $\mathbf{h}_i$ ($i = 1, \ldots, h$) is computed as

\mathbf{h}_i = f(\mathbf W_i^{(q)}\mathbf q, \mathbf W_i^{(k)}\mathbf k,\mathbf W_i^{(v)}\mathbf v) \in \mathbb R^{p_v},

where learnable parameters $\mathbf W_i^{(q)}\in\mathbb R^{p_q\times d_q}$, $\mathbf W_i^{(k)}\in\mathbb R^{p_k\times d_k}$ and $\mathbf W_i^{(v)}\in\mathbb R^{p_v\times d_v}$, and $f$ is attention pooling, such as additive attention and scaled dot-product attention in :numref:sec_attention-scoring-functions. The multi-head attention output is another linear transformation via learnable parameters $\mathbf W_o\in\mathbb R^{p_o\times h p_v}$ of the concatenation of $h$ heads:

\mathbf W_o \begin{bmatrix}\mathbf h_1\\vdots\\mathbf h_h\end{bmatrix} \in \mathbb{R}^{p_o}.

Based on this design, each head may attend to different parts of the input. More sophisticated functions than the simple weighted average can be expressed.

```{.python .input} from d2l import mxnet as d2l import math from mxnet import autograd, np, npx from mxnet.gluon import nn npx.set_np()

  1. ```{.python .input}
  2. #@tab pytorch
  3. from d2l import torch as d2l
  4. import math
  5. import torch
  6. from torch import nn

Implementation

In our implementation, we choose the scaled dot-product attention for each head of the multi-head attention. To avoid significant growth of computational cost and parameterization cost, we set $p_q = p_k = p_v = p_o / h$. Note that $h$ heads can be computed in parallel if we set the number of outputs of linear transformations for the query, key, and value to $p_q h = p_k h = p_v h = p_o$. In the following implementation, $p_o$ is specified via the argument num_hiddens.

```{.python .input}

@save

class MultiHeadAttention(nn.Block): def init(self, numhiddens, numheads, dropout, use_bias=False, **kwargs): super(MultiHeadAttention, self).__init(**kwargs) self.num_heads = num_heads self.attention = d2l.DotProductAttention(dropout) self.W_q = nn.Dense(num_hiddens, use_bias=use_bias, flatten=False) self.W_k = nn.Dense(num_hiddens, use_bias=use_bias, flatten=False) self.W_v = nn.Dense(num_hiddens, use_bias=use_bias, flatten=False) self.W_o = nn.Dense(num_hiddens, use_bias=use_bias, flatten=False)

  1. def forward(self, queries, keys, values, valid_lens):
  2. # Shape of `queries`, `keys`, or `values`:
  3. # (`batch_size`, no. of queries or key-value pairs, `num_hiddens`)
  4. # Shape of `valid_lens`:
  5. # (`batch_size`,) or (`batch_size`, no. of queries)
  6. # After transposing, shape of output `queries`, `keys`, or `values`:
  7. # (`batch_size` * `num_heads`, no. of queries or key-value pairs,
  8. # `num_hiddens` / `num_heads`)
  9. queries = transpose_qkv(self.W_q(queries), self.num_heads)
  10. keys = transpose_qkv(self.W_k(keys), self.num_heads)
  11. values = transpose_qkv(self.W_v(values), self.num_heads)
  12. if valid_lens is not None:
  13. # On axis 0, copy the first item (scalar or vector) for
  14. # `num_heads` times, then copy the next item, and so on
  15. valid_lens = valid_lens.repeat(self.num_heads, axis=0)
  16. # Shape of `output`: (`batch_size` * `num_heads`, no. of queries,
  17. # `num_hiddens` / `num_heads`)
  18. output = self.attention(queries, keys, values, valid_lens)
  19. # Shape of `output_concat`:
  20. # (`batch_size`, no. of queries, `num_hiddens`)
  21. output_concat = transpose_output(output, self.num_heads)
  22. return self.W_o(output_concat)
  1. ```{.python .input}
  2. #@tab pytorch
  3. #@save
  4. class MultiHeadAttention(nn.Module):
  5. def __init__(self, key_size, query_size, value_size, num_hiddens,
  6. num_heads, dropout, bias=False, **kwargs):
  7. super(MultiHeadAttention, self).__init__(**kwargs)
  8. self.num_heads = num_heads
  9. self.attention = d2l.DotProductAttention(dropout)
  10. self.W_q = nn.Linear(query_size, num_hiddens, bias=bias)
  11. self.W_k = nn.Linear(key_size, num_hiddens, bias=bias)
  12. self.W_v = nn.Linear(value_size, num_hiddens, bias=bias)
  13. self.W_o = nn.Linear(num_hiddens, num_hiddens, bias=bias)
  14. def forward(self, queries, keys, values, valid_lens):
  15. # Shape of `queries`, `keys`, or `values`:
  16. # (`batch_size`, no. of queries or key-value pairs, `num_hiddens`)
  17. # Shape of `valid_lens`:
  18. # (`batch_size`,) or (`batch_size`, no. of queries)
  19. # After transposing, shape of output `queries`, `keys`, or `values`:
  20. # (`batch_size` * `num_heads`, no. of queries or key-value pairs,
  21. # `num_hiddens` / `num_heads`)
  22. queries = transpose_qkv(self.W_q(queries), self.num_heads)
  23. keys = transpose_qkv(self.W_k(keys), self.num_heads)
  24. values = transpose_qkv(self.W_v(values), self.num_heads)
  25. if valid_lens is not None:
  26. # On axis 0, copy the first item (scalar or vector) for
  27. # `num_heads` times, then copy the next item, and so on
  28. valid_lens = torch.repeat_interleave(
  29. valid_lens, repeats=self.num_heads, dim=0)
  30. # Shape of `output`: (`batch_size` * `num_heads`, no. of queries,
  31. # `num_hiddens` / `num_heads`)
  32. output = self.attention(queries, keys, values, valid_lens)
  33. # Shape of `output_concat`:
  34. # (`batch_size`, no. of queries, `num_hiddens`)
  35. output_concat = transpose_output(output, self.num_heads)
  36. return self.W_o(output_concat)

To allow for parallel computation of multiple heads, the above MultiHeadAttention class uses two transposition functions as defined below. Specifically, the transpose_output function reverses the operation of the transpose_qkv function.

```{.python .input}

@save

def transpose_qkv(X, num_heads):

  1. # Shape of input `X`:
  2. # (`batch_size`, no. of queries or key-value pairs, `num_hiddens`).
  3. # Shape of output `X`:
  4. # (`batch_size`, no. of queries or key-value pairs, `num_heads`,
  5. # `num_hiddens` / `num_heads`)
  6. X = X.reshape(X.shape[0], X.shape[1], num_heads, -1)
  7. # Shape of output `X`:
  8. # (`batch_size`, `num_heads`, no. of queries or key-value pairs,
  9. # `num_hiddens` / `num_heads`)
  10. X = X.transpose(0, 2, 1, 3)
  11. # Shape of `output`:
  12. # (`batch_size` * `num_heads`, no. of queries or key-value pairs,
  13. # `num_hiddens` / `num_heads`)
  14. return X.reshape(-1, X.shape[2], X.shape[3])

@save

def transpose_output(X, num_heads): “””Reverse the operation of transpose_qkv“”” X = X.reshape(-1, num_heads, X.shape[1], X.shape[2]) X = X.transpose(0, 2, 1, 3) return X.reshape(X.shape[0], X.shape[1], -1)

  1. ```{.python .input}
  2. #@tab pytorch
  3. #@save
  4. def transpose_qkv(X, num_heads):
  5. # Shape of input `X`:
  6. # (`batch_size`, no. of queries or key-value pairs, `num_hiddens`).
  7. # Shape of output `X`:
  8. # (`batch_size`, no. of queries or key-value pairs, `num_heads`,
  9. # `num_hiddens` / `num_heads`)
  10. X = X.reshape(X.shape[0], X.shape[1], num_heads, -1)
  11. # Shape of output `X`:
  12. # (`batch_size`, `num_heads`, no. of queries or key-value pairs,
  13. # `num_hiddens` / `num_heads`)
  14. X = X.permute(0, 2, 1, 3)
  15. # Shape of `output`:
  16. # (`batch_size` * `num_heads`, no. of queries or key-value pairs,
  17. # `num_hiddens` / `num_heads`)
  18. return X.reshape(-1, X.shape[2], X.shape[3])
  19. #@save
  20. def transpose_output(X, num_heads):
  21. """Reverse the operation of `transpose_qkv`"""
  22. X = X.reshape(-1, num_heads, X.shape[1], X.shape[2])
  23. X = X.permute(0, 2, 1, 3)
  24. return X.reshape(X.shape[0], X.shape[1], -1)

Let us test our implemented MultiHeadAttention class using a toy example where keys and values are the same. As a result, the shape of the multi-head attention output is (batch_size, num_queries, num_hiddens).

```{.python .input} num_hiddens, num_heads = 100, 5 attention = MultiHeadAttention(num_hiddens, num_heads, 0.5) attention.initialize()

  1. ```{.python .input}
  2. #@tab pytorch
  3. num_hiddens, num_heads = 100, 5
  4. attention = MultiHeadAttention(num_hiddens, num_hiddens, num_hiddens,
  5. num_hiddens, num_heads, 0.5)
  6. attention.eval()

```{.python .input}

@tab all

batch_size, num_queries, num_kvpairs, valid_lens = 2, 4, 6, d2l.tensor([3, 2]) X = d2l.ones((batch_size, num_queries, num_hiddens)) Y = d2l.ones((batch_size, num_kvpairs, num_hiddens)) attention(X, Y, Y, valid_lens).shape ```

Summary

  • Multi-head attention combines knowledge of the same attention pooling via different representation subspaces of queries, keys, and values.
  • To compute multiple heads of multi-head attention in parallel, proper tensor manipulation is needed.

Exercises

  1. Visualize attention weights of multiple heads in this experiment.
  2. Suppose that we have a trained model based on multi-head attention and we want to prune least important attention heads to increase the prediction speed. How can we design experiments to measure the importance of an attention head?

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab: