iTranslated by AI
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
- It can handle long sequences because it is memory-efficient.
- It is easy to use in large-scale networks because the computation is fast.
- 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
The bottom row is a schematic diagram of the attention reference pattern when rows are outputs and columns are inputs. In other words,
- (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.
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:
Here,
Also, the index set for the
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:
Method 2: Merged head
This is a method to combine factorized attention heads into a single head. It is formulated as follows:
Method 3: Multi-head attention
Here,
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.
Here,
Regarding
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
- Child, R., Gray, S., Radford, A. & Sutskever, I. (2019). Generating Long Sequences with Sparse Transformers. arXiv. https://doi.org/10.48550/arxiv.1904.10509
- 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
- Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. & Sutskever, I. (2019). Language Models are Unsupervised Multitask Learners.
- 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
- 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