Blockwise Self-Attention for Long Document Understanding
Jiezhong Qiu, Hao Ma, Omer Levy, Scott Wen-tau Yih, Sinong Wang, Jie Tang
Introduction
Recent emergence of the pre-training and fine-tuning paradigm, exemplified by methods like ELMo (Peters et al., 2018), GPT-2/3 (Radford et al., 2019; Brown et al., 2020), BERT (Devlin et al., 2019), XLNet (Yang et al., 2019), RoBERTa (Liu et al., 2019) and ALBERT Lan et al. (2019), has drastically reshaped the landscape of the natural language processing research. These methods first pre-train a deep model with language model objectives using a large corpus and then fine-tune the model using in-domain supervised data for target applications. Despite its conceptual simplicity, this paradigm has re-established the new state-of-the-art baselines across various tasks, such as question answering (Devlin et al., 2019), coreference resolution (Joshi et al., 2019b), relation extraction (Soares et al., 2019) and text retrieval (Lee et al., 2019; Nogueira and Cho, 2019), to name a few.
Building such models in practice, however, is an extremely resource-intensive process. For instance, the training of BERT-family models is notoriously expensive. Devlin et al. (2019) report that it takes four days to pre-train BERT-Base/BERT-Large on 4/16 Cloud TPUs. In order to reduce the pre-training time of RoBERTa to 1 day, Liu et al. (2019) use 1,024 V100 GPUs. One crucial factor contributing to the long training time is the memory consumption of these deep models, as it directly affects the batch size. Although the fine-tuning stage is relatively inexpensive, the memory issue still restricts the scenarios in which BERT can be used. For instance, “it is currently not possible to re-produce most of the BERT-Large results on the paper using a GPU with 12GB-16GB of RAM, because the maximum batch size that can fit in memory is too small.github.com/google-research/bert”
Although one may think that model size is the main contributor to the large memory consumption, our analysis (Section 2.1) shows that one of the main bottlenecks is actually dot-product self-attention, operated in multiple layers of Transformers (Vaswani et al., 2017), the building block of BERT. As the attention operation is quadratic to the sequence length, this fundamentally limits the maximum length of the input sequence, and thus restricts the model capacity in terms of capturing long-distance dependencies. As a result, downstream tasks have to either truncate their sequences to leading tokens (Nogueira and Cho, 2019) or split their sequences with a sliding window (Joshi et al., 2019a, b). Ad-hoc handling of long sequences is also required in the pre-training stage, such as updating the model using only short sequences in the early stage (Devlin et al., 2019).
Common strategies for reducing memory consumption, unfortunately, do not work. For instance, shrinking the model by lowering the number of layers , attention heads , or hidden units leads to significant performance degradation (Vaswani et al., 2017; Devlin et al., 2019) and does not address the long sequence issue. Alternatively, general low-memory training techniques, such as microbatching (Huang et al., 2018) and gradient checkpointing (Chen et al., 2016) essentially trade off training time for memory consumption, prolongs the already lengthy training process.
In this work, we explore a different strategy, sparsifying the attention layers, intending to design a lightweight and effective BERT that can model long sequences in a memory-efficient way. Our BlockBert extends BERT by introducing sparse block substructures into attention matrices to reduce both memory consumption and the number of floating-point operations (FLOPs), which also enables attention heads to capture either short- or long-range contextual information. Compared to the previous method that also enforces sparsity Child et al. (2019), our approach is much simpler mathematically and very easy to implement. More importantly, the results of experiments conducted on several benchmark question answering datasets with various paragraph lengths show that BlockBert performs comparably or even better than the original BERT-family models, while enjoying an 18.7-36.1% reduction in memory usage, a 12.0-25.1% reduction in training time, and a 27.8% reduction in inference time.
The rest of the paper is organized as follows. Section 2 gives a brief introduction of the BERT model, along with an in-depth analysis of its memory usage during training time. We describe our proposed model in Section 3 and contrast it with existing methods that aim for creating a lighter model. Section 4 presents the experimental results and ablation studies, followed by a survey of other related work in Section 5 and the conclusion in Section 6.
Background: Memory Bottleneck in Training BERT
We briefly review BERT and introduce its memory profiling in this section. Following the paradigm of language model pre-training and down-stream task fine-tuning, BERT (Devlin et al., 2019) consists of multiple layers of bidirectional Transformers (Vaswani et al., 2017), where each Transformer encoder has a multi-head self-attention layer and a position-wise feed-forward layer. Using the same notation as in (Devlin et al., 2019), we denote the number of Transformer layers by , the number of hidden units by , the number of attention heads by , the sequence length by , and the batch size by . We also assume the feed-forward hidden unit size to be .The default parameter settings for BERT-Base and BERT-Large can be found in Appendix A.1
Training BERT is a memory-intensive process. In order to identify the bottleneck, we follow the memory model proposed by Sohoni et al. (2019), where memory usage throughout neural network training is categorized into three main types: (1) Model memory is used to store model parameters; (2) Optimizer memory is the additional memory used by the specific learning algorithm during the process; (3) Activation memory consists of the outputs of each layer, which are cached for reuse in backpropagation to compute gradients.
Take BERT-Base training as an example. The model has 110 million parameters, so model memory occupies 0.2 GB if parameters are stored in half-precision floating-point format (FP16). For Adam (Kingma and Ba, 2014), the optimizer needs additional memory to store the gradients, first moments, and second moments of model parameters. If stored using the same precision, the optimizer memory should be three times of model memory.In the current PyTorch Adam implementation, the first and second moments are stored in single precision. Consequently, BERT’s optimizer memory (1 GB) is five times of model memory (0.2 GB). To calculate the exact size of activation memory is not trivial because it depends heavily on the implementation of the toolkit. Instead, we measure it empirically by training BERT-Base using Adam with a memory profiler (more details are provided in Appendix A.2).
We use 32 NVIDIA V100 GPUs for training. Every single GPU thus consumes a mini-batch of size . Figure 1(a) shows the profiling result for a single GPU, where the model/optimizer/activation memory consumes 0.21/1.03/8.49 GB, respectively. We can see that activation memory accounts for the vast majority of the total GPU memory (87.6%) and is thus the bottleneck. Notice that although our analysis is done on BERT-Base, it can also be generalized to BERT-Large and other models such as RoBERTa (Liu et al., 2019) and XLNet (Yang et al., 2019).
2 A Regression Analysis on Activation Memory
For BERT, or more specifically, Transformer, the activation memory corresponds to intermediate results of different layers. It grows linearly in all the model hyper-parameters, except the sequence length , due to the attention layers. To quantify the linear and quadratic components in the activation memory more clearly, we conduct a regression analysis as follows. Assume that the activation memory (in each GPU) is a polynomial , where is the batch size in each GPU and () are coefficients to be determined. If we fix the total number of tokens in a GPU to be constant (in our case, we fix ), we should have a linear function w.r.t. , i.e., . We enumerate from in our experiments, and plot the corresponding profiled activation memory in Figure 1(b). Using ordinary least squares (OLS), with , the estimated linear function for activation memory is , where the first term corresponds to the component. When (i.e., ), we can see that for BERT-Base, the component accounts for 3.66 GB, and the component accounts for 4.83 GB. When the sequence length increases to 1024 (i.e., ), the component increases to 7.32 GB, while the part is unchanged.
3 Techniques for Reducing Traing Memory
Observing that activation memory is the training bottleneck, we discuss common memory reduction techniques below.
Low Precision (Micikevicius et al., 2017) Low precision is to use half-precision/mixed-precision for training neural networks. This technique has been widely used in Transformer training (Ott et al., 2019; Liu et al., 2019). In this work, we already assume to use mixed-precision training by default, as indicated in the aforementioned analysis.
Microbatching (Huang et al., 2018) Microbatching is to split a batch into small micro-batches (which can be fit into memory), and then run forward and backward passes on them separately with gradients for each micro-batch accumulated. Because it runs forward/backward pass multiple times for a single batch, it trades off time for memory.
Gradient Checkpointing (Chen et al., 2016) Gradient checkpointing saves memory by only caching activations of a subset of layers. The un-cached activations will be recomputed during backpropagation from the latest checkpoint. This strategy trades off time for memory by repeating computations and will obviously extend training time.
Knowledge Distillation (Hinton et al., 2015) Knowledge distillation aims to compress and transfer knowledge from a teacher model to a simpler student model. However, knowledge distillation relies on a teacher model (which is still expensive in training time) and usually suffers from a certain degree of performance degradation.
(Ding et al., 2020) presents an alternative idea based on cognitive theory to construct a working-memory by identifying key sentences, which enables multi-step reasoning. However, common techniques are still limited in reducing both the training time and memory usage. In this paper, we investigate how to optimize the dot-product attention layers and introduce our approach next.
Model: BlockBert
Following (Vaswani et al., 2017), the dot-product attention in Transformer is defined as:
In this work, we design to be a sparse block matrix, which not only reduces memory and the number of floating-point operations (FLOPs) but also benefits from efficient dense matrix support from deep learning frameworks, such as PyTorch and Tensorflow. More formally, we split the length- input sequence into blocks, with each block of length .We assume can be divided by . If not, we pad the input sequence to make divisible. The attention matrix is then partitioned into blocks, where each block matrix is of the size . We define a sparse block matrix by a permutation of :
By writing as block matrices, such that and and pluging them into Equation 1, we can formally define Blockwise Attention as follows:
Equation 3 only needs to compute and store (), each has size . In other words, BlockBert reduces both memory consumption and FLOPs by a factor of , since .
Analogous to Multi-head Attention (Vaswani et al., 2017), we allow queries, keys, and values to be projected multiple times and perform blockwise attentions in parallel. Moreover, different blockwise attention heads can use different masking matrices. The outputs of multiple heads are then concatenated and aggregated with another linear projection. Let be the number of attention heads and the number of hidden units. Blockwise multi-head attention is formally defined as follows:
where for each head , ,
2 Analysis of Memory Usage Reduction
To validate our claim that BlockBert with blocks can reduce the memory usage by a factor of , we perform the same memory profiling as described in sections 2.1 and 2.2. Again, We fix the number of tokens in each GPU () and choose from .We use GPUs of 16 GB memory for profiling. BERT with fails due to an out-of-memory error. As we can see from Figure 3 and Table 1, the empirical results align well with the theoretical values. When we set the number of blocks to be 2 and 3 for BlockBert, the estimated activation memory decreases to 1/2 and 1/3 of BERT’s activation memory, respectively. As shown in Table 2, for the sequence length , BlockBert with 2 and 3 blocks saves 18.7% and 23.8% overall memory, respectively. The saving is more significant for longer sequences. When , the overall memory reduction of BlockBert with 2 and 3 blocks is 27.3% and 36.1%, respectively.
Experiments
We evaluate the pre-training and fine-tuning performance of BlockBert. In particular, when , we denote 10:2 to be the configuration which assigns 10 heads to permutation and 2 to permutation ; when , we denote 8:2:2 to be the configuration which assigns 8, 2, 2 heads to permutation , , and , respectively. We compare BlockBert with the following baselines:
Google BERT Google BERT is the official pre-trained model from Devlin et al. (2019).
RoBERTa-2seq & RoBERTa-1seq We compare with two versions of RoBERTa (Liu et al., 2019). RoBERTa-2seq is trained with both masked language model (MLM) task and next sentence prediction (NSP) task, while RoBERTa-1seq refers to the pre-training model with only the MLM task.
All the models follow the BERT-Base setting, i.e., , and are trained on the same corpus — BooksCorpus and English Wikipedia with uncased word piece tokens. Thus all models use the same vocabulary as Google BERT (uncased version) with vocabulary size 30,522. We fix the number of tokens per batch , i.e., if sequence length then batch size , if sequence length then batch size . The detailed pre-training configuration is listed in Appendix A.1. Moreover, the pre-training of SparseBert and BlockBert follows the RoBERTa-1seq setting, i.e., we drop the NSP (Next Sentence Prediction) task, and an input sequence is up to tokens until it reaches a document boundary.
A summary of the pre-training performance comparison between BlockBert and RoBERTa-1seq is shown in Table 2. Besides memory saving, we also achieve a significant speedup. For example, when , BlockBert () reduces the training time from RoBERTa’s 9.7 days to 7.5 days.
2 Fine-tuning Tasks
We evaluate BlockBert on several question answering tasks, including SQuAD 1.1/2.0 (Rajpurkar et al., 2018) and five other tasks from the MrQA shared taskmrqa.github.io — HotpotQA (Yang et al., 2018), NewsQA (Trischler et al., 2017), SearchQA (Dunn et al., 2017), TriviaQA (Joshi et al., 2017) and NaturalQA (Kwiatkowski et al., 2019). Since MrQA does not have an official test set, we follow Joshi et al. (2019a) to split the development set evenly to build a new development set and test set.
These QA datasets have different paragraph length distributions and are thus ideal for testing the effectiveness of BlockBertThe detailed paragraph length distributions can be found in Appendix A.5. For example, SQuAD, NaturalQA, and HotpotQA consist of mostly short paragraphs (shorter than 512), while paragraphs in SearchQA (average length 1,004) and TriviaQA (average length 934) have around 1,000 tokens. When the input sequence is longer than , we follow the common practice Joshi et al. (2019a) to split it using a sliding window of size and stride 128. This means that for SearchQA and TriviaQA, a model with can only capture half of the context, while a model with can accept the whole paragraph as input.
For all models, we adopt the same fine-tuning QA setup from Devlin et al. (2019). The tokenized paragraph and question are concatenated to be a sequence . The sequence is then fed into the pre-trained model with two extra linear layers for predicting the start and end positions of the answer spans. The detailed fine-tuning setting is listed in Appendix A.4. Table 3 and Table 4 report the experimental results.
BlockBert (n=2) v.s. RoBERTa-1seq Comparing BlockBert with RoBERTa-1seq when , we observe an absolute F1 difference from 0.04 (in NaturalQA) to 1.18 (in NewsQA), with an average of 0.55. For , BlockBert achieves more comparable or even better performance to RoBERTa-1seq, In SearchQA, NewsQA and HotpotQA, BlockBert achieves absolute F1 improvement of 0.39, 0.44 and 0.23, respectively.
BlockBert v.s. SparseBert For , it is interesting that BlockBert with 3 blocks (density 33.33%) performs better then SparseBert (density 44.20%) in both SQuAD and MrQA tasks. Similar results can be observed for , too. These results show that off-diagonal masking matrices, e.g., the masking matrix defined by permutation and , play crucial roles in BlockBert. Furthermore, BlockBert with 2 blocks achieve a more significant improvement.
Effect of Long Sequence Pre-training Our observations are twofold: (1) Long sequence pre-training benefits long sequence fine-tuning. In TriviaQA and SearchQA, of which paragraph lengths are around 1024, pre-training models with achieve significantly better performance. (2) The heterogeneity of pre-training and fine-tuning sequence length may hurt performance. For example, in SQuAD, we do not see significant performance gain by using pre-trained models with ; in HotpotQA and NewsQA, longer sequence pre-training even hurts performance.
Effect of #Blocks It is not surprising that BlockBert with 2 blocks () performs better than that with 3 blocks (), because it keeps more attention matrix entries. The biggest difference is in SQuAD 2.0 and NewsQA with , where we observe an absolute loss of 1.6 F1 by increasing block number from 2 to 3.
Efficient inference with BlockBert We benchmark test efficiency of RoBERTa and BlockBert. The benchmark code follows huggingfacegithub.com/huggingface/transformers/blob/master/examples/benchmarks.py. All experiments are run 30 times on a 32GB V100 GPU with half precision (FP16). We report the average running time in Table 5. As we can see, BlockBert does achieve speedup and memory reduction during test time. Take 81024, i.e., batch size , sequence length , as an example, we can see that BlockBert with 2 blocks saves 27.8% of test time, and BlockBert with 3 blocks saves more (30.4%). As for memory, we can observe that RoBERTa cannot handle an input of size 161024, while it is possible for BlockBert to work on it.
In summary, not only BlockBert saves training/inference time and memory, but it also has a competitive and sometimes better performance, especially for tasks with longer sequences. This demonstrates the effectiveness of our blockwise multi-head attention approach.
3 Ablation Study
We fix the assignment of attention heads in the above experiments. For example, BlockBert with sequence length and 2 blocks is trained with ten heads using permutation and the other two using permutation . However, there are other ways to assign twelve attention heads, e.g., seven heads for permutation and the other five for permutation . It would be interesting to see how the assignment of heads affects model performance. In this section, we grid search attention head assignments and plot their best validation performance in 1.2M training steps. The results are shown in Figure 4.
Our observations are threefold: (1) Identity permutations, i.e., and , are important. As shown in Figure 4, all optimal solutions assign considerable attention heads to block-diagonal matrices, since those matrices enable each token to attend to its nearby tokens; (2) Non-identity permutations follow the rule of “vital few and trivial many.” Although identity permutations are important, assigning all attention heads to them (corresponding to 12:0 and 12:0:0 in Figure 4) significantly hurts performance, since the model can not learn long-term dependencies with only identity permutation; (3) Pre-training performance and fine-tuning performance are correlated but not always consistent. When , pre-training performance suggests 10:1:1 to be the best head assignment — ten heads for permutation , one head for and one head for , but we observe that the configuration of 8:2:2 achieves better performance in fine-tuning tasks.
Related Work
In this section, we review the related work of memory optimization for neural network training and recent efforts to simplify Transformer and BERT.
Due to the large size of model parameters and deep architectures, modern neural networks training requires significant amounts of computing resources. As a result, there is an increasing interest in training neural networks with low memory (Sohoni et al., 2019). Mainstream techniques mostly address this problem with a better system or engineering design, such as low-precision training (Micikevicius et al., 2017), microbatching (Huang et al., 2018) and gradient checkpointing (Chen et al., 2016). Alternatively, there also exists some research focusing on the theoretical aspect, including the recently proposed lottery ticket hypothesis (Frankle and Carbin, 2018).
2 Efficient Transformer
Since the invention of Transformer (Vaswani et al., 2017) and its successful application to masked language model pre-training (Devlin et al., 2019; Radford et al., 2019; Yang et al., 2019; Liu et al., 2019; Lan et al., 2019), several approaches have been proposed to simplify the model and its training process. We summarize these attempts as follows:
Attention layer simplification There are currently two lines of research trying to simplify the multi-head attention layers. The first one focuses on attention matrix sparsification. Notable examples include Star Transformer (Guo et al., 2019), Sparse Transformer (Child et al., 2019), Adaptive Sparse Transformer (Correia et al., 2019; Sukhbaatar et al., 2019), Log-Sparse Transformer Li et al. (2019) , Reformer (Kitaev et al., 2020) and Longformer (Beltagy et al., 2020). However, due to the insufficient support for sparse tensors from the current deep learning platforms, some of them have to represent a sparse matrix using a dense matrix with a binary mask or rely on customized CUDA kernels (Gray et al., 2017). As a result, the speed-up or reduction in memory consumption is sometimes limited in practice. The second line of research prunes redundant attention heads. Examples include Voita et al. (2019) and Michel et al. (2019). Our BlockBert model belongs to the first category, as we sparsify the attention matrices to be block sparse matrix.
Reducing model size for pre-training Knowledge distillation Hinton et al. (2015) is a general technique that aims to compress and transfer knowledge from a teacher model to a simpler student model. There are two recent efforts that apply knowledge distillation to BERT pre-training for reducing model size: TinyBERT Jiao et al. (2019) distills BERT using a smaller Transformer, and Tang et al. (2019) distills BERT with a BiLSTM.In contrast, ALBERT Lan et al. (2019) is a notable work that does not take the knowledge distillation approach. It uses parameter-sharing to reduce the number of parameters of the BERT model. As discussed in section 2.1, parameter-sharing reduces both model memory and optimizer memory. These two parts account for about 12.4% of total training memory for BERT-base. As for efficiency, parameter-sharing reduces communication complexity in distributed training and thus saves training time as well.
In the aforementioned efficient Transformers, the model quality is often demonstrated by comparable language model perplexity, or equivalently the bits per word/byte. It is often implicitly assumed that similar language model perplexity implies similar pre-training model quality, namely the same performance on the downstream tasks. We would like to point out that this assumption does not necessarily hold. For example, the experiments on the Enwik8 dataset by Child et al. (2019) demonstrates that Sparse Transformer “surpasses the 1.03 state-of-the-art (bits per byte) for a similarly-sized Transformer-XL and matching the 0.99 (bits per byte) of a model trained with more than double the number of parameters”. However, if we compare SparseBert (pre-training model with Sparse Transformer backbone) against XLNet Yang et al. (2019) (pre-training model with Transformer-XL backbone) in SQuAD, Table 3 shows that XLNet still outperforms SparseBert significantly. Therefore, we believe that it is necessary to conduct a comprehensive study and evaluation of existing efficient Transformer models when used for masked language model pre-training. Limited by resources, in this work, we mainly compare BlockBert to pre-training using Sparse Transformer Child et al. (2019), which is the earliest attempt to design efficient Transformer models and also the key contributor to the success of GPT-3 Brown et al. (2020). We plan to benchmark more models in the future.
Conclusion
In this work, we study the lightweight BERT model with the goal of achieving both efficiency and effectiveness. We profile and analyze the memory bottlenecks of BERT and focus on optimize dot-product self-attention, which consumes quadratic memory with respect to the sequence length. To reduce both time and memory consumption, we present BlockBert, which sparsifies the attention matrices to be sparse block matrices. The proposed model achieves time and memory saving without significant loss of performance.
In the future, we plan to benchmark more efficient Transfomers in language model pre-training and fine-tuning. We also would like to explore more applications of BlockBert on NLP tasks involving long sequences such as coreference resolution (Joshi et al., 2019b) and document-level machine translation (Miculicich et al., 2018), and also non-NLP tasks such as protein sequence modeling (Rives et al., 2019; Rao et al., 2019).
Acknowledgments
The authors would like to thank Zhilin Yang, Danqi Chen, Yinhan Liu, Mandar Joshi and Luke Zettlemoyer for the helpful suggestions. Jiezhong Qiu and Jie Tang were partially supported by the National Key R&D Program of China (2018YFB1402600), NSFC for Distinguished Young Scholar (61825602), and NSFC (61836013).
References
Appendix A Appendix
The notations and pre-training hyper-parameters are listed in Table 6 and Table 7.
A.2 Profiler Implementation
Among the three types of training memory, model memory and optimizer memory is relatively easy to profile (can be computed by enumerating each tenor and summing up tensor.numel() * tensor.element_size()). To calculate activation memory, Sohoni et al. (2019) traverse PyTorch’s autograd graph and sum up the necessary storage space. They find that the summation of model memory, optimizer memory, and activation memory matches PyTorch memory profiling tool torch.cuda.max_memory_allocated.
Based on their observation, we use the following quantity as an estimate to activation memory
When profiling BERT, we first pre-train it for 1000 steps, and then compute its model and optimizer memory. Finally, we estimate its activation memory according to Equation 4.
A.3 SparseBert
The sparse masking matrices we use for Sparse Transformer (Child et al., 2019) are shown in Figure 5. We adopt the implementation of Sparse Transformer from Fairseqgithub.com/pytorch/fairseq/blob/master/fairseq/modules/sparse_multihead_attention.py.. The Fariseq version is implemented in a direct way, with the goal of comparing performance, not speed. We first compute the attention matrix and then mask it to be a sparse matrix according to the sparse pattern defined in Sparse Transformer paper. Consequently, this implementation of SparseBERT has very close training time/memory cost as RoBERTa (as it can not avoid the attention computation). We did so because the code released by Sparse Transformer is based on Tensorflow and relies on customized CUDA kernels, but our pre-training is done using PyTorch.
A.4 Fine-tuning Settings
Our fine-tuning is implemented based on code base from HuggingFacegithub.com/huggingface/pytorch-transformers and SpanBERT (Joshi et al., 2019a). We use max_sequence_length=, i.e., we allow fine-tuning task to input sequences as long as the pre-training model. If the input sequence is too long to fit the max_sequence_length= constraints, we use a sliding window of stride 128 to split it. We grid search learning rate from {5e-6, 1e-5, 2e-5, 3e-5, 5e-5} and batch size from {16, 32}. The fine-tuning is performed for 4 epoches.
A.5 Paragraph-Length Distribution
The paragraph-length distribution of SQuAD and MrQA datasets is shown in Figure 6.