iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆

Understanding Sparse Transformers

に公開

 This is an article where I want to understand Sparse Transformer [1]. Sparse Transformer is a method that reduces required memory and computational complexity by introducing sparsity into the Transformer. It is also used in massive models like GPT-3 [2]. In this article, I will first introduce the factorized self-attention newly introduced in Sparse Transformer, and then summarize the Sparse Transformer itself.

What's Great About Sparse Transformer

  1. It can handle long sequences because it is memory-efficient.
  2. It is easy to use in large-scale networks because the computation is fast.
  3. Performance does not drop despite making the attention sparse.

 Let's take a look at how these are achieved.

Factorized Self-Attention

 Sparse Transformer is a Transformer that improves computational efficiency by decomposing self-attention into multiple attentions. Strided attention and fixed attention were introduced as new calculation methods for attention (factorized attention) (see figure below).


From [1] Figure 3

 The top row of the figure shows an example of two attention heads for a 6 \times 6 image. The range of attention for the dark blue position is represented by light blue.

 The bottom row is a schematic diagram of the attention reference pattern when rows are outputs and columns are inputs. In other words, (i, j) indicates the presence or absence of attention for the j-th input relative to the i-th output.

  • (a): Standard self-attention. Each output refers to all past time points.
  • (b): Attention called strided attention. It refers to distant points periodically and a few recent points.
  • (c): Attention called fixed attention. It refers to fixed past time points and a few recent points.

 Let's look into the details of whether the reference patterns for strided and fixed attention are really okay being this sparse.

Strided Attention

 For the purpose of explanation, let's consider a simple sequence here. In this case, the reference pattern of strided attention is as follows (see figure below).


Reference pattern of strided attention

 As can be seen from the figure above, by passing through strided attention twice, all past time points can be referenced.

Fixed Attention

 Again, let's consider a simple sequence. In this case, the reference pattern of fixed attention is as follows (see figure below).


Reference pattern of fixed attention

 Here, several patterns of examples where fixed attention is passed through twice are shown. Note that in fixed attention, the reference destination of attention head 2 is fixed. It can be seen that all past points can be referenced in any pattern. The right side of the figure looks the same as strided attention, but it differs in that strided attention results in the same reference pattern for any output.

How to Construct Sparse Transformer

Basic Configuration

 The residual block of Sparse Transformer has the following structure.


Structure of Sparse Transformer's residual block (From [1] Figure 4)

 It's a bit difficult to understand, but it has the same structure as GPT-2 [3]. Please refer to the following for GPT-2.

https://zenn.dev/sunbluesome/articles/775ffd67fb7454

Formulation

 In [1], three ways to construct a sparse transformer using factorized attention are introduced. Since formulas will be used, let's prepare for their introduction here.

 Standard attention is defined as follows:

\begin{align} \text{attention}(X) &= W_p \cdot \text{attend}(X, S) \\ \text{attend}(X, S) &= \left(a(\boldsymbol{x}_i, S_i)\right)_{i \in \{1, \ldots, n\}} \\ a(\boldsymbol{x}_i, S_i) &= \text{softmax}\left(\frac{\left(W_q, \boldsymbol{x}_i\right) K_{S_i}^T}{\sqrt{d}} V_{S_i}\right) \\ K_{S_i} &= \left(W_k \boldsymbol{x}_j\right)_{j \in S_i} \\ V_{S_i} &= \left(W_v \boldsymbol{x}_j\right)_{j \in S_i} \\ \end{align}

 Here, x_i \in X is the i-th input vector, S_i \in S is the set of indices of the input vectors referenced by the i-th output, and W_{\{p, q, k, v \}} are the weight matrices. In particular, W_q, W_k, and W_v are used to transform the input vector x_i into the query, key, and value. \text{attend} is simply \text{attention} without the weight matrix applied. K_{S_i} and V_{S_i} are the keys and values. If you are unsure about this, please refer to the following article.

https://zenn.dev/sunbluesome/articles/078ac9a9afca6a

 Also, the index set for the m-th factorized attention head is expressed as follows:

\begin{align} A^{(m)} \sub S \end{align}

 Now that the preparation is complete, let's look at each method.

Method 1: Interleave

 This simply connects the residual blocks to which the Factorized attention head has been applied in order. It is formulated as follows:

\begin{align} \text{attention}(X) = W_p \cdot \text{attend}(X, A^{(r \mod p)}) \end{align}

 r is the index of the current residual block, and p is the number of factorized attention heads.

Method 2: Merged head

 This is a method to combine factorized attention heads into a single head. It is formulated as follows:

\begin{align} \text{attention}(X) = W_p \cdot \text{attend}\left(X, \bigcup_{m=1}^{p} A^{(m)}\right) \end{align}

Method 3: Multi-head attention

\begin{align} \text{attention}(X) = W_p \left(\text{attend}(X, A)^{(i)}\right)_{i \in \left\{1, \ldots, n_h\right\}} \end{align}

 Here, n_h is the number of heads, and each head is computed in parallel. Finally, the calculation results are concatenated along the feature dimension. Also, there is flexibility in how to construct A, and both Methods 1 and 2 above can be used.

Positional Encoding

 In the Transformer, positional encoding was used to provide information about the position of elements in the sequence and information relative to the feature dimensions. Sparse Transformer uses the following formula to provide not only the data structure but also information about the attention patterns.

\begin{align} \text{embed}(X, W_e) = \left(\boldsymbol{x}_i W_e + \sum_{j=1}^{n_{emb}} \boldsymbol{o}_i^{(j)} W_j\right)_{\boldsymbol{x}_i \in X} \end{align}

 Here, \boldsymbol{x}_i is the i-th one-hot encoded element of the sequence, and \boldsymbol{o}_i^{(j)} is the one-hot vector representing the j-th dimension of \boldsymbol{x}_i. n_{emb} is either n_{emb} = d_{data} or n_{emb} = d_{attn}. d_{data} is the dimensionality of the data, and d_{attn} is the dimensionality of the factorized attention.

 Regarding d_{emb}, d_{emb} = d_{data} = 3 is used for images, and d_{emb} = d_{attn} = 2 is used for text or audio data.

Experimental Results

 Finally, let's conclude by confirming the effectiveness of the Sparse Transformer (see figure below).

Bits/byte


From [1] Table 1

 Performance evaluation was conducted for a task called density modeling on CIFAR-10, Enwik8, and ImageNet 64x64. The evaluation metric is Bits/byte (synonymous with bits/dim in image tasks). I will not explain Bits/byte here, but those who are interested should read the PixelCNN paper. In essence, it is the negative log-likelihood, so a smaller value is better.

 Sparse Transformer achieved the best performance on all datasets.

Computational Speed


From [1] Table 2

 It can be seen that it is faster than Dense Attention. Furthermore, in terms of Bits/byte, the performance is better than standard attention.

Acquisition of Long-term Dependencies


From [1] Table 3

 In Enwik8, it can be seen that the performance keeps improving as the context window accepted by the model is increased. This suggests that the Sparse Transformer is able to capture long-term dependencies.

References

  1. Child, R., Gray, S., Radford, A. & Sutskever, I. (2019). Generating Long Sequences with Sparse Transformers. arXiv. https://doi.org/10.48550/arxiv.1904.10509
  2. Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., Agarwal, S., Herbert-Voss, A., Krueger, G., Henighan, T., Child, R., Ramesh, A., Ziegler, D. M., Wu, J., Winter, C., … Amodei, D. (2020). Language Models are Few-Shot Learners. arXiv. https://doi.org/10.48550/arxiv.2005.14165
  3. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. & Sutskever, I. (2019). Language Models are Unsupervised Multitask Learners.
  4. Chen, T., Xu, B., Zhang, C. & Guestrin, C. (2016). Training Deep Nets with Sublinear Memory Cost. arXiv. https://doi.org/10.48550/arxiv.1604.06174
  5. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L. & Polosukhin, I. (2017). Attention Is All You Need. arXiv. https://doi.org/10.48550/arxiv.1706.03762

Discussion