MiniLM: Deep Self-Attention Distillation for Task-Agnostic Compression of Pre-Trained Transformers
Wenhui Wang, Furu Wei, Li Dong, Hangbo Bao, Nan Yang, Ming Zhou
Introduction
Language model (LM) pre-training has achieved remarkable success for various natural language processing tasks (Peters et al., 2018; Howard & Ruder, 2018; Radford et al., 2018; Devlin et al., 2018; Dong et al., 2019; Yang et al., 2019; Joshi et al., 2019; Liu et al., 2019). The pre-trained language models, such as BERT (Devlin et al., 2018) and its variants, learn contextualized text representations by predicting words given their context using large scale text corpora, and can be fine-tuned with additional task-specific layers to adapt to downstream tasks. However, these models usually contain hundreds of millions of parameters which brings challenges for fine-tuning and online serving in real-life applications for latency and capacity constraints.
Knowledge distillation (Hinton et al., 2015; Romero et al., 2015) (KD) has been proven to be a promising way to compress a large model (called the teacher model) into a small model (called the student model), which uses much fewer parameters and computations while achieving competitive results on downstream tasks. There have been some works that task-specifically distill pre-trained large LMs into small models (Tang et al., 2019; Turc et al., 2019b; Sun et al., 2019a; Aguilar et al., 2019). They first fine-tune the pre-trained LMs on specific tasks and then perform distillation. Task-specific distillation is effective, but fine-tuning large pre-trained models is still costly, especially for large datasets. Different from task-specific distillation, task-agnostic LM distillation mimics the behavior of the original pre-trained LMs and the student model can be directly fine-tuned on downstream tasks (Tsai et al., 2019; Sanh et al., 2019; Jiao et al., 2019; Sun et al., 2019b).
Previous works use soft target probabilities for masked language modeling predictions or intermediate representations of the teacher LM to guide the training of the task-agnostic student. DistilBERT (Sanh et al., 2019) employs a soft-label distillation loss and a cosine embedding loss, and initializes the student from the teacher by taking one layer out of two. But each Transformer layer of the student is required to have the same architecture as its teacher. TinyBERT (Jiao et al., 2019) and MobileBERT (Sun et al., 2019b) utilize more fine-grained knowledge, including hidden states and self-attention distributions of Transformer networks, and transfer these knowledge to the student model layer-to-layer. To perform layer-to-layer distillation, TinyBERT adopts a uniform function to determine the mapping between the teacher and student layers, and uses a parameter matrix to linearly transform student hidden states. MobileBERT assumes the teacher and student have the same number of layers and introduces the bottleneck module to keep their hidden size the same.
In this work, we propose the deep self-attention distillation framework for task-agnostic Transformer based LM distillation. The key idea is to deeply mimic the self-attention modules which are the fundamentally important components in the Transformer based teacher and student models. Specifically, we propose distilling the self-attention module of the last Transformer layer of the teacher model. Compared with previous approaches, using knowledge of the last Transformer layer rather than performing layer-to-layer knowledge distillation alleviates the difficulties in layer mapping between the teacher and student models, and the layer number of our student model can be more flexible. Furthermore, we introduce the scaled dot-product between values in the self-attention module as the new deep self-attention knowledge, in addition to the attention distributions (i.e., the scaled dot-product of queries and keys) that has been used in existing works. Using scaled dot-product between self-attention values also converts representations of different dimensions into relation matrices with the same dimensions without introducing additional parameters to transform student representations, allowing arbitrary hidden dimensions for the student model. Finally, we show that introducing a teacher assistant (Mirzadeh et al., 2019) helps the distillation of large pre-trained Transformer based models and the proposed deep self-attention distillation can further boost the performance.
We conduct extensive experiments on downstream NLP tasks. Experimental results demonstrate that our monolingual model outperforms state-of-the-art baselines in different parameter size of student models. Specifically, the -layer model of hidden dimensions distilled from BERT is faster, while retaining more than accuracy on SQuAD 2.0 and several GLUE benchmark tasks. Moreover, our multilingual model distilled from XLM-R also achieves competitive performance with much fewer Transformer parameters.
Preliminary
Multi-layer Transformers (Vaswani et al., 2017) have been the most widely-used network structures in state-of-the-art pre-trained models. In this section, we present a brief introduction to the Transformer network and the self-attention mechanism, which is the core component of the Transformer. We also present the existing approaches on knowledge distillation for Transformer networks, particularly in the context of distilling a large Transformer based pre-trained model into a small Transformer model.
Texts are tokenized to subword units by WordPiece (Wu et al., 2016) in BERT (Devlin et al., 2018). For example, the word “forecasted” is split to “forecast” and “##ed”, where “##” indicates the pieces are belong to one word. A special boundary token [SEP] is used to separate segments if the input text contains more than one segment. At the beginning of the sequence, a special token [CLS] is added to obtain the representation of the whole input. The vector representations () of input tokens are computed via summing the corresponding token embedding, absolute position embedding, and segment embedding.
2 Backbone Network: Transformer
Transformer (Vaswani et al., 2017) is used to encode contextual information for input tokens. The input vectors are packed together into . Then stacked Transformer blocks compute the encoding vectors as:
where is the number of Transformer layers, and the final output is . The hidden vector is used as the contextualized representation of . Each Transformer layer consists of a self-attention sub-layer and a fully connected feed-forward network. Residual connection (He et al., 2016) is employed around each of the two sub-layers, followed by layer normalization (Ba et al., 2016).
In each layer, Transformer uses multiple self-attention heads to aggregate the output vectors of the previous layer. For the -th Transformer layer, the output of a self-attention head is computed via:
3 Transformer Distillation
Knowledge distillation (Hinton et al., 2015; Romero et al., 2015) is to train the small student model on a transfer feature set with soft labels and intermediate representations provided by the large teacher model . Knowledge distillation is modeled as minimizing the differences between teacher and student features:
Where denotes the training data, and indicate the features of student and teacher models respectively, represents the loss function. The mean squared error (MSE) and KL-divergence are often used as loss functions.
For Transformer based LM distillation, soft target probabilities for masked language modeling predictions, embedding layer outputs, self-attention distributions and outputs (hidden states) of each Transformer layer of the teacher model are used as features to help the training of the student. Soft labels and embedding layer outputs are used in DistillBERT. TinyBERT and MobileBERT further utilize self-attention distributions and outputs of each Transformer layer. For MobileBERT, the student is required to have the same number of layers as its teacher to perform layer-to-layer distillation. Besides, bottleneck and inverted bottleneck modules are introduced to keep the hidden size of the teacher and student are also the same. To transfer knowledge layer-to-layer, TinyBERT employs a uniform-function to map teacher and student layers. Since the hidden size of the student can be smaller than its teacher, a parameter matrix is introduced to transform the student features.
Deep Self-Attention Distillation
Figure 1 gives an overview of the deep self-attention distillation. The key idea is three-fold. First, we propose to train the student by deeply mimicking the self-attention module, which is the vital component in the Transformer, of the teacher’s last layer. Second, we introduce transferring the relation between values (i.e., the scaled dot-product between values) to achieve a deeper mimicry, in addition to performing attention distributions (i.e., the scaled dot-product of queries and keys) transfer in the self-attention module. Moreover, we show that introducing a teacher assistant (Mirzadeh et al., 2019) also helps the distillation of large pre-trained Transformer models when the size gap between the teacher model and student model is large.
The attention mechanism (Bahdanau et al., 2015) has been a highly successful neural network component for NLP tasks, which is also crucial for pre-trained LMs. Some works show that self-attention distributions of pre-trained LMs capture a rich hierarchy of linguistic information (Jawahar et al., 2019; Clark et al., 2019). Transferring self-attention distributions has been used in previous works for Transformer distillation (Jiao et al., 2019; Sun et al., 2019b; Aguilar et al., 2019). We also utilize the self-attention distributions to help the training of the student. Specifically, we minimize the KL-divergence between the self-attention distributions of the teacher and student:
Where and represent the sequence length and the number of attention heads. and represent the number of layers for the teacher and student. and are the attention distributions of the last Transformer layer for the teacher and student, respectively. They are computed by the scaled dot-product of queries and keys.
Different from previous works which transfer teacher’s knowledge layer-to-layer, we only use the attention maps of the teacher’s last Transformer layer. Distilling attention knowledge of the last Transformer layer allows more flexibility for the number of layers of our student models, avoids the effort of finding the best layer mapping.
2 Self-Attention Value-Relation Transfer
In addition to the attention distributions, we propose using the relation between values in the self-attention module to guide the training of the student. The value relation is computed via the multi-head scaled dot-product between values. The KL-divergence between the value relation of the teacher and student is used as the training objective:
The training loss is computed via summing the attention distribution transfer loss and value-relation transfer loss:
Introducing the relation between values enables the student to deeply mimic the teacher’s self-attention behavior. Moreover, using the scaled dot-product converts vectors of different hidden dimensions into the relation matrices with the same size, which allows our students to use more flexible hidden dimensions and avoids introducing additional parameters to transform the student’s representations.
3 Teacher Assistant
Following Mirzadeh et al. (2019), we introduce a teacher assistant (i.e., intermediate-size student model) to further improve the model performance of smaller students.
Assuming the teacher model consists of -layer Transformer with hidden size, the student model has -layer Transformer with hidden size. For smaller students (, ), we first distill the teacher into a teacher assistant with -layer Transformer and hidden size. The assistant model is then used as the teacher to guide the training of the final student. The introduction of a teacher assistant bridges the size gap between teacher and smaller student models, helps the distillation of Transformer based pre-trained LMs. Moreover, combining deep self-attention distillation with a teacher assistant brings further improvements for smaller student models.
4 Comparison with Previous Work
Table 1 presents the comparison with previous approaches (Sanh et al., 2019; Jiao et al., 2019; Sun et al., 2019b). MobileBERT proposes using a specially designed inverted bottleneck model, which has the same model size as BERT, as the teacher. The other methods utilize BERT to conduct experiments. For the knowledge used for distillation, our method introduces the scaled dot-product between values in the self-attention module as the new knowledge to deeply mimic teacher’s self-attention behavior. TinyBERT and MobileBERT transfer knowledge of the teacher to the student layer-to-layer. MobileBERT assumes the student has the same number of layers as its teacher. TinyBERT employs a uniform strategy to determine its layer mapping. DistillBERT initializes the student with teacher’s parameters, therefore selecting layers of the teacher model is still needed. MiniLM distills the self-attention knowledge of the teacher’s last Transformer layer, which allows the flexible number of layers for the students and alleviates the effort of finding the best layer mapping. Student hidden size of DistillBERT and MobileBERT is required to be the same as its teacher. TinyBERT uses a parameter matrix to transform student hidden states. Using value relation allows our students to use arbitrary hidden size without introducing additional parameters.
Experiments
We conduct distillation experiments in different parameter size of student models, and evaluate the distilled models on downstream tasks including extractive question answering and the GLUE benchmark.
We use the uncased version of BERT as our teacher. BERT (Devlin et al., 2018) is a -layer Transformer with hidden size, and attention heads, which contains about M parameters. The number of heads of attention distributions and value relation are set to for student models. We use documents of English WikipediaWikipedia version: enwiki-20181101. and BookCorpus (Zhu et al., 2015) for the pre-training data, following the preprocess and the WordPiece tokenization of Devlin et al. (2018). The vocabulary size is . The maximum sequence length is . We use Adam (Kingma & Ba, 2015) with , . We train the -layer student model with hidden size using as the batch size and 5e-4 as the peak learning rate for steps. For student models of other architectures, the batch size and peak learning rate are set to and 3e-4, respectively. We use linear warmup over the first steps and linear decay. The dropout rate is . The weight decay is .
We also use an in-house pre-trained Transformer model in the BERT size as the teacher model, and distill it into -layer and -layer student models with hidden size. For the -layer model, we use Adam (Kingma & Ba, 2015) with , . The model is trained using as the batch size and 6e-4 as the peak learning rate for steps. The batch size and peak learning rate are set to and 4e-4 for the -layer model. The rest hyper-parameters are the same as above BERT based distilled models.
For the training of multilingual MiniLM models, we use Adam (Kingma & Ba, 2015) with , . We train the -layer student model using as the batch size and 3e-4 as the peak learning rate for steps. The -layer student model is trained using as the batch size and 6e-4 as the peak learning rate for steps.
We distill our student models using V100 GPUs with mixed precision training. Following Sun et al. (2019a) and Jiao et al. (2019), the inference time is evaluated on the QNLI training set with the same hyper-parameters. We report the average running time of batches on a single P100 GPU.
2 Downstream Tasks
Following previous language model pre-training (Devlin et al., 2018; Liu et al., 2019) and task-agnostic pre-trained language model distillation (Sanh et al., 2019; Jiao et al., 2019; Sun et al., 2019b), we evaluate our distilled models on the extractive question answering and GLUE benchmark.
Given a passage , the task is to select a contiguous span of text in the passage by predicting its start and end positions to answer the question . We evaluate on SQuAD 2.0 (Rajpurkar et al., 2018), which has served as a major question answering benchmark.
Following BERT (Devlin et al., 2018), we pack the question and passage tokens together with special tokens, to form the input: “[CLS] [SEP] [SEP]". Two linear output layers are introduced to predict the probability of each token being the start and end positions of the answer span. The questions that do not have an answer are treated as having an answer span with start and end at the [CLS] token.
GLUE
The General Language Understanding Evaluation (GLUE) benchmark (Wang et al., 2019) consists of nine sentence-level classification tasks, including Corpus of Linguistic Acceptability (CoLA) (Warstadt et al., 2018), Stanford Sentiment Treebank (SST) (Socher et al., 2013), Microsoft Research Paraphrase Corpus (MRPC) (Dolan & Brockett, 2005), Semantic Textual Similarity Benchmark (STS) (Cer et al., 2017), Quora Question Pairs (QQP) (Chen et al., 2018), Multi-Genre Natural Language Inference (MNLI) (Williams et al., 2018), Question Natural Language Inference (QNLI) (Rajpurkar et al., 2016), Recognizing Textual Entailment (RTE) (Dagan et al., 2006; Bar-Haim et al., 2006; Giampiccolo et al., 2007; Bentivogli et al., 2009) and Winograd Natural Language Inference (WNLI) (Levesque et al., 2012). We add a linear classifier on top of the [CLS] token to predict label probabilities.
3 Main Results
Previous works (Sanh et al., 2019; Sun et al., 2019a; Jiao et al., 2019) usually distill BERT into a -layer student model with hidden size. We first conduct distillation experiments using the same student architecture. Results on SQuAD 2.0 and GLUE dev sets are presented in Table 2. Since MobileBERT distills a specially designed teacher with the inverted bottleneck modules, which has the same model size as BERT, into a -layer student using the bottleneck modules, we do not compare our models with MobileBERT. MiniLM outperforms DistillBERTThe public model of DistillBERT is obtained from https://github.com/huggingface/transformers/tree/master/examples/distillation and TinyBERTWe use the 2nd version TinyBERT from https://github.com/huawei-noah/Pretrained-Language-Model/tree/master/TinyBERT across most tasks. Our model exceeds the two state-of-the-art models by + F1 on SQuAD 2.0 and + accuracy on CoLA. We present the inference time for models in different parameter size in Table 4. Our -layer -dimensional student model is faster than original BERT, while retaining more than performance on a variety of tasks, such as SQuAD 2.0 and MNLI.
We also conduct experiments for smaller student models. We compare MiniLM with our implemented MLM-KD (knowledge distillation using soft target probabilities for masked language modeling predictions) and TinyBERT, which are trained using the same data and hyper-parameters. The results on SQuAD 2.0, MNLI and SST-2 dev sets are shown in Table 3. MiniLM outperforms soft label distillation and our implemented TinyBERT on the three tasks. Deep self-attention distillation is also effective for smaller models. Moreover, we show that introducing a teacher assistantThe teacher assistant is only introduced for the model MiniLM (w/ TA). The model MiniLM in different tables is directly distilled from its teacher model. is also helpful in Transformer based pre-trained LM distillation, especially for smaller models. Combining deep self-attention distillation with a teacher assistant achieves further improvement for smaller student models.
4 Ablation Studies
We do ablation tests on several tasks to analyze the contribution of self-attention value-relation transfer. The dev results of SQuAD 2.0, MNLI and SST-2 are illustrated in Table 5, using self-attention value-relation transfer positively contributes to the final results for student models in different parameter size. Distilling the fine-grained knowledge of value relation helps the student model deeply mimic the self-attention behavior of the teacher, which further improves model performance.
We also compare different loss functions over values in the self-attention module. We compare our proposed value relation with mean squared error (MSE) over the teacher and student values. An additional parameter matrix is introduced to transform student values if the hidden dimension of the student is smaller than its teacher. The dev results on three tasks are presented in Table 6. Using value relation achieves better performance. Specifically, our method brings about F1 improvement on the SQuAD benchmark. Moreover, there is no need to introduce additional parameters for our method. We have also tried to transfer the relation between hidden states. But we find the performance of student models are unstable for different teacher models.
To show the effectiveness of distilling self-attention knowledge of the teacher’s last Transformer layer, we compare our method with layer-to-layer distillation. We transfer the same knowledge and adopt a uniform strategy as in Jiao et al. (2019) to map teacher and student layers to perform layer-to-layer distillation. The dev results on three tasks are presented in Table 7. MiniLM achieves better results. It also alleviates the difficulties in layer mapping between the teacher and student. Besides, distilling the teacher’s last Transformer layer requires less computation than layer-to-layer distillation, results in faster training speed.
Discussion
We report the results of MiniLM distilled from an in-house pre-trained Transformer model following UniLM (Dong et al., 2019; Bao et al., 2020) in the BERT size. The teacher model is trained using similar pre-training datasets as in RoBERTa (Liu et al., 2019), which includes GB text corpora from English Wikipedia, BookCorpus (Zhu et al., 2015), OpenWebTextskylion007.github.io/OpenWebTextCorpus, CC-News (Liu et al., 2019), and Stories (Trinh & Le, 2018). We distill the teacher model into -layer and -layer models with hidden size using the same corpora. The x model is used as the teacher assistant to train the x model. We present the dev results of SQuAD 2.0 and GLUE benchmark in Table 8, the results of MiniLM are significantly improved. The x MiniLM achieves speedup while performs competitively better than BERT in SQuAD 2.0 and GLUE benchmark datasets.
2 MiniLM for NLG Tasks
We also evaluate MiniLM on natural language generation tasks, such as question generation and abstractive summarization. Following Dong et al. (2019), we fine-tune MiniLM as a sequence-to-sequence model by employing a specific self-attention mask.
We conduct experiments for the answer-aware question generation task (Du & Cardie, 2018). Given an input passage and an answer, the task is to generate a question that asks for the answer. The SQuAD 1.1 dataset (Rajpurkar et al., 2016) is used for evaluation. The results of MiniLM, UniLM and several state-of-the-art models are presented in Table 9, our x and x distilled models achieve competitive performance on the question generation task.
Abstractive Summarization
We evaluate MiniLM on two abstractive summarization datasets, i.e., XSum (Narayan et al., 2018), and the non-anonymized version of CNN/DailyMail (See et al., 2017). The generation task is to condense a document into a concise and fluent summary, while conveying its key information. We report ROUGE scores (Lin, 2004) on the datasets. Table 10 presents the results of MiniLM, baseline, several state-of-the-art models and pre-trained Transformer models. Our x model outperforms BERT based method BERTSumAbs (Liu & Lapata, 2019) and the pre-trained sequence-to-sequence model MASS (Song et al., 2019) with much fewer parameters. Moreover, our x MiniLM also achieves competitive performance.
3 Multilingual MiniLM
We conduct experiments on task-agnostic knowledge distillation of multilingual pre-trained models. We use the XLM-RWe use the v version of XLM-R in our distillation and fine-tuning experiments. (Conneau et al., 2019) as the teacher and distill the model into -layer and -layer models with 384 hidden size using the same corpora. The x model is trained using the x model as the teacher assistant. Given the vocabulary size of multilingual pre-trained models is much larger than monolingual models (30k for monolingual BERT, 250k for XLM-R), soft-label distillation for multilingual pre-trained models requires more computation. MiniLM only uses the deep self-attention knowledge of the teacher’s last Transformer layer. The training speed of MiniLM is much faster than soft-label distillation for multilingual pre-trained models.
We evaluate the student models on cross-lingual natural language inference (XNLI) benchmark (Conneau et al., 2018) and cross-lingual question answering (MLQA) benchmark (Lewis et al., 2019b).
Table 11 presents XNLI results of our distilled students and several pre-trained LMs. Following Conneau et al. (2019), we select the best single model on the joint dev set of all the languages. We present the number of Transformer and embedding parameters for different multilingual pre-trained models and our distilled models in Table 12. MiniLM achieves competitive performance on XNLI with much fewer Transformer parameters. Moreover, the x MiniLM compares favorably with mBERT (Devlin et al., 2018) and XLM (Lample & Conneau, 2019) trained on the MLM objective.
MLQA
Table 13 shows cross-lingual question answering results. Following Lewis et al. (2019b), we adopt SQuAD 1.1 as training data and use MLQA English development data for early stopping. The x MiniLM performs competitively better than mBERT and XLM. Our -layer MiniLM also achieves competitive performance.
Related Work
Unsupervised pre-training of language models (Peters et al., 2018; Howard & Ruder, 2018; Radford et al., 2018; Devlin et al., 2018; Baevski et al., 2019; Song et al., 2019; Dong et al., 2019; Yang et al., 2019; Joshi et al., 2019; Liu et al., 2019; Lewis et al., 2019a; Raffel et al., 2019) has achieved significant improvements for a wide range of NLP tasks. Early methods for pre-training (Peters et al., 2018; Radford et al., 2018) were based on standard language models. Recently, BERT (Devlin et al., 2018) proposes to use a masked language modeling objective to train a deep bidirectional Transformer encoder, which learns interactions between left and right context. Liu et al. (2019) show that very strong performance can be achieved by training the model longer over more data. Joshi et al. (2019) extend BERT by masking contiguous random spans. Yang et al. (2019) predict masked tokens auto-regressively in a permuted order.
To extend the applicability of pre-trained Transformers for NLG tasks. Dong et al. (2019) extend BERT by utilizing specific self-attention masks to jointly optimize bidirectional, unidirectional and sequence-to-sequence masked language modeling objectives. Raffel et al. (2019) employ an encoder-decoder Transformer and perform sequence-to-sequence pre-training by predicting the masked tokens in the encoder and decoder. Different from Raffel et al. (2019), Lewis et al. (2019a) predict tokens auto-regressively in the decoder.
2 Knowledge Distillation
Knowledge distillation has proven a promising way to compress large models while maintaining accuracy. It transfers the knowledge of a large model or an ensemble of neural networks (teacher) to a single lightweight model (student). Hinton et al. (2015) first propose transferring the knowledge of the teacher to the student by using its soft target distributions to train the distilled model. Romero et al. (2015) introduce intermediate representations from hidden layers of the teacher to guide the training of the student. Knowledge of the attention maps (Zagoruyko & Komodakis, 2017; Hu et al., 2018) is also introduced to help the training.
In this work, we focus on task-agnostic knowledge distillation of large pre-trained Transformer based language models. There have been some works that task-specifically distill the fine-tuned language models on downstream tasks. Tang et al. (2019) distill fine-tuned BERT into an extremely small bidirectional LSTM. Turc et al. (2019a) initialize the student with a small pre-trained LM during task-specific distillation. Sun et al. (2019a) introduce the hidden states from every layers of the teacher to perform knowledge distillation layer-to-layer. Aguilar et al. (2019) further introduce the knowledge of self-attention distributions and propose progressive and stacked distillation methods. Task-specific distillation requires to first fine-tune the large pre-trained LMs on downstream tasks and then perform knowledge transfer. The procedure of fine-tuning large pre-trained LMs is costly and time-consuming, especially for large datasets.
For task-agnostic distillation, the distilled model mimics the original large pre-trained LM and can be directly fine-tuned on downstream tasks. In practice, task-agnostic compression of pre-trained LMs is more desirable. MiniBERT (Tsai et al., 2019) uses the soft target distributions for masked language modeling predictions to guide the training of the multilingual student model and shows its effectiveness on sequence labeling tasks. DistillBERT (Sanh et al., 2019) uses the soft label and embedding outputs of the teacher to train the student. TinyBERT (Jiao et al., 2019) and MobileBERT (Sun et al., 2019b) further introduce self-attention distributions and hidden states to train the student. MobileBERT employs inverted bottleneck and bottleneck modules for teacher and student to make their hidden dimensions the same. The student model of MobileBERT is required to have the same number of layers as its teacher to perform layer-to-layer distillation. Besides, MobileBERT proposes a bottom-to-top progressive scheme to transfer teacher’s knowledge. TinyBERT uses a uniform-strategy to map the layers of teacher and student when they have different number of layers, and a linear matrix is introduced to transform the student hidden states to have the same dimensions as the teacher. TinyBERT also introduces task-specific distillation and data augmentation for downstream tasks, which brings further improvements.
Different from previous works, our method employs the self-attention distributions and value relation of the teacher’s last Transformer layer to help the student deeply mimic the self-attention behavior of the teacher. Using knowledge of the last Transformer layer instead of layer-to-layer distillation avoids restrictions on the number of student layers and the effort of finding the best layer mapping. Distilling relation between self-attention values allows the hidden size of students to be more flexible and avoids introducing linear matrices to transform student representations.
Conclusion
In this work, we propose a simple and effective knowledge distillation method to compress large pre-trained Transformer based language models. The student is trained by deeply mimicking the teacher’s self-attention modules, which are the vital components of the Transformer networks. We propose using the self-attention distributions and value relation of the teacher’s last Transformer layer to guide the training of the student, which is effective and flexible for the student models. Moreover, we show that introducing a teacher assistant also helps pre-trained Transformer based LM distillation, and the proposed deep self-attention distillation can further boost the performance. Our student model distilled from BERT retains high accuracy on SQuAD 2.0 and the GLUE benchmark tasks, and outperforms state-of-the-art baselines. The deep self-attention distillation can also be applied to compress pre-trained models in larger size. We leave it as our future work.
References
Appendix A GLUE Benchmark
The summary of datasets used for the General Language Understanding Evaluation (GLUE) benchmarkhttps://gluebenchmark.com/ (Wang et al., 2019) is presented in Table 14.
We present the dataset statistics and metrics of SQuAD 2.0http://stanford-qa.com (Rajpurkar et al., 2018) in Table 15.
Appendix B Fine-tuning Hyper-parameters
For SQuAD 2.0, the maximum sequence length is and a sliding window of size if the lengths are longer than . For the -layer model distilled from our in-house pre-trained model, we fine-tune epochs using as the batch size and 4e-5 as the peak learning rate. The rest distilled models are trained using as the batch size and 6e-5 as the peak learning rate for epochs.
GLUE
The maximum sequence length is for the GLUE benchmark. We set batch size to , choose learning rates from {2e-5, 3e-5, 4e-5, 5e-5} and epochs from {, , } for student models distilled from BERT. For student models distilled from our in-house pre-trained model, the batch size is chosen from {, }. We fine-tune several tasks (CoLA, RTE and MRPC) with longer epochs (up to epochs), which brings slight improvements. For the -layer model, the learning rate used for CoLA, RTE and MRPC tasks is 1.5e-5.
Appendix C SQuAD 2.0
For the question generation task, we set batch size to , and total length to . The maximum output length is . The learning rates are 3e-5 and 8e-5 for the -layer and -layer models, respectively. They are both fine-tuned for epochs. We also use label smoothing (Szegedy et al., 2016) with rate of . During decoding, we use beam search with beam size of . The length penalty (Wu et al., 2016) is .
Abstractive Summarization
For the abstractive summarization task, we set batch size to , and the rate of label smoothing to . For the CNN/DailyMail dataset, the total length is and the maximum output length is . The learning rates are 1e-4 and 1.5e-4 for the -layer and -layer models, respectively. They are both fine-tuned for epochs. During decoding, we set beam size to , and the length penalty to . For the XSum dataset, the total length is and the maximum output length is . The learning rates are 1e-4 and 1.5e-4 for the -layer and -layer models, respectively. We fine-tune epochs for the -layer model and epochs for the -layer model. During decoding, we use beam search with beam size of . The length penalty is set to .
Cross-lingual Natural Language Inference
The maximum sequence length is for XNLI. We fine-tune epochs using as the batch size, choose learning rates from {3e-5, 4e-5, 5e-5, 6e-5}.
Cross-lingual Question Answering
For MLQA, the maximum sequence length is and a sliding window of size if the lengths are longer than . We fine-tune epochs using as the batch size. The learning rates are chosen from {3e-5, 4e-5, 5e-5, 6e-5}.