RAIL-KD: RAndom Intermediate Layer Mapping for Knowledge Distillation

Md Akmal Haidar\(^1\)1 Nithin Anchuri\(^{1,2}\)2 Mehdi Rezagholizadeh\(^1\)
Abbas Ghaddar\(^1\) Philippe Langlais\(^2\) Pascal Poupart\(^3\)
\(^1\) Huawei Noah’s Ark Lab
\(^2\) RALI/DIRO, Université de Montréal, Canada
\(^3\) David R. Cheriton School of Computer Science, University of Waterloo
{mehdi.rezagholizadeh,abbas.ghaddar}@huawei.com
felipe@iro.umontreal.ca, ppoupart@uwaterloo.ca


Abstract

Intermediate layer knowledge distillation (KD) can improve the standard KD technique (which only targets the output of teacher and student models) especially over large pre-trained language models. However, intermediate layer distillation suffers from excessive computational burdens and engineering efforts required for setting up a proper layer mapping. To address these problems, we propose a RAndom Intermediate Layer Knowledge Distillation (RAIL-KD) approach in which, intermediate layers from the teacher model are selected randomly to be distilled into the intermediate layers of the student model. This randomized selection enforce that: all teacher layers are taken into account in the training process, while reducing the computational cost of intermediate layer distillation. Also, we show that it act as a regularizer for improving the generalizability of the student model. We perform extensive experiments on GLUE tasks as well as on out-of-domain test sets. We show that our proposed RAIL-KD approach outperforms other state-of-the-art intermediate layer KD methods considerably in both performance and training-time.

Table 1: Main Characteristics and limitation of different approaches that tackle the skip and search problem. Concat indicates if the approach support concatenated layers distillation. \(n\) and \(m\) refer to the teacher and student layer number respectively, while \(K\) refer to number of negative samples of CODIR.
Model Layer Mapping Complexity Limitation
PKD [1] Extra Hyperparameter \(O(m)\) Extensive Search
CKD [2] Extra Hyperparameter \(O(m)\) Extensive Search
ALP-KD [3] Attention \(O(m \times n)\) Slow Training time
CoDIR [4] Contrastive Learning \(O(K \times m)\) Slow Training time
RAIL-KD\(l\) (our) Random Selection \(O(m)\) -
RAIL-KD\(c\) (our) Random Selection \(O(m)\) -

1 Introduction↩︎

Pre-trained Language Models (PLMs), such as BERT [5], RoBERTa [6] and XLNet [7] have shown remarkable abilities to match and even surpass human performances on many Natural Languages Understanding (NLU) tasks [8][10]. However, the deployment of these models in real world applications (e.g. edge devices) come with challenges, mainly due to large model size and inference time.

In this regard, several model compression techniques such as quantization [11], [12], pruning [13][15], optimizing the Transformer architecture [16][19], and knowledge distillation [3], [20][26] have been developed to reduce the model size and latency, while maintaining comparable performance to the original model.

KD, which is the main focus of this work, is a neural model compression approach that involves training a small student model with the guidance of a large pre-trained teacher model. In the original KD technique [27][29], the teacher output predictions are used as soft labels for supervising the training of the student. There has been several attempts in the literature to reduce the teacher-student performance gap by leveraging data augmentation [21], [30], [31], adversarial training [24], [32], [33], and intermediate layer distillation (ILD)  [3], [23], [34], [35].

When it comes to Bert compression, ILD leads to clear gains in performances [20], [21], [23] due to its ability to enhance the knowledge transfer beyond logits matching. This is done by mapping intermediate layer representations of both models to a common space3, and then matching them via regression [1] or cosine similarity [20] losses. On major problem with ILD is the absence of an appropriate strategy to select layers to be matched on both sides, reacting to the skip and search problem [3]. There are some solutions in the literature mostly rely on layer combination [2], attention-based layer projection [3] and contrastive learning [4].

While these solutions are all effective to some extent, to the best of our knowledge, there is no work in the literature doing a comprehensive evaluation of these techniques in terms of both efficiency and performance. A case in point is that the aforementioned solutions to the layer skip and search problem are not scalable to very deep networks. We propose RAIL-KD (RAndom Intermediate Layer KD), a simple yet effective method for intermediate layer mapping which randomly selects \(k\) out of \(n\) intermediate layers of the teacher at each epoch to be distilled to the corresponding student layers. Since the layer selection is done randomly, all the intermediate layers of the teacher will have a chance to be selected for distillation. Our method adds no computational cost to the training, still outperforming all aforementioned methods on the GLUE benchmark [9]. Moreover, we observe larger gains distilling larger teacher models, as well as when compressed models are evaluated on out-of-domain datasets. Last, we report the results on 5 random seeds in order to verify the contribution of the random selection process, thus making the comparison fair with previous methods. The main contributions of our paper are as follows:

  • We introduce RAIL-KD, a more efficient and scalable intermediate layer distillation approach.

  • To the best of our knowledge, we are the first to perform a comprehensive study of the intermediate layer distillation techniques in terms of both efficiency and performance.

  • We consider the distillation of models such as Bert and RoBERTa, and compare different up-to-date distillation techniques on out-of-domain test sets. Thus providing new points of comparison.

Figure 1: Proposed RAIL-KD technique for efficient intermediate layer distillation. (a) This version shows a layer-wise projection which is indicated as RAIL-KD^l in the paper. (b) This variant named RAIL-KD^c, concatenates the intermediate representations of each network before distillation.

2 Related Work↩︎

Recent years, have seen a wide range of methods have emerged aiming to expand knowledge transfer of transformer-based [36] NLU models beyond logits matching. DistillBert [20] added a cosine similarity loss between teacher and student embeddings layer. TinyBERT [21], MobileBERT [22], and MiniLM [34] matched the intermediate layers representations and self-attention distributions of the teacher and the student.

In PKD, [1] used deterministic mapping strategies to distill a 12-layers Bert teacher to a 6-layers Bert student. PKD-Last and PKD-Skip refer to matching layers \(\{1-5\}\) of the student with layers \(\{7-11\}\) and \(\{2,4,6,8,10\}\) of the teacher respectively. However, these works ignored the impact of layer selection, as they used a fixed layer-wise mapping.4

Researchers have found that tuning the layer mapping scheme can significantly improve the performance of ILD techniques [1]. Nevertheless, finding the optimal mapping can be challenging, which is referred to as the layer skip and search problems by  [3]. To address the layer skip problem, CKD [2] is built on top of PKD by partitioning all the intermediate layers of the teacher to the number of student layers. Then, the combined representation of the layers of each partition is distilled into a number of subset corresponding to the number of student layers. However, finding the optimal partitioning scheme requires running exhaustive experiments.

Given teacher and student Bert models with \(n\) and \(m\) layers respectively (where \(n>>m\)), it is not trivial to choose the teacher layers that can be incorporated in the distillation process and how we should map them to the student layers (search).

ALP-KD [3] overcomes this issue by computing attention weights between each student layer and all the intermediate layers of the teacher. The learned attention weights for each student layer are used to obtain a weighted representation of all teacher layers. Although ALP-KD has shown promising results on 12-layer BERT-based compression, attending to all layers of the teacher adds considerable computational overhead to the training phase. This can become computationally prohibitive when scaling to very large models such as RoBERTa-large [6] or GPT-2 [37]. Alternatively, CODIR [4] exploited contrastive learning [38] to perform intermediate layers matching between the teacher and the student models with no deterministic mapping. Similar to ALP-KD, this approach also requires excessive training time due to the contrastive loss calculation and the use of negative samples from a memory bank.

Table 1 summarizes the main characteristics of the existing state-of-the-art intermediate layer distillation techniques used for pre-trained language models compared with our proposed RAIL-KD. As it is shown in this table, PKD [1], CKD [2], and CoDIR are the most related works to us. However, PKD and CKD treat the mapping as an extra hyperparameter that requires extensive experiments to find the optimal mapping. On the other hand, ALP-KD [3] and CoDIR [4] use attention mechanism and contrastive learning respectively to address the issue, but at the expense of extra computational cost.

Our proposed RAIL-KD method does not add any computational cost to the distillation process, while empirically outperforming previous methods. For instance, RAIL-KD is roughly twice faster than CoDIR in a 24 to 6 layers compression. In addition, it does not require extensive experiments to find the optimal mapping scheme. In this work, we position ourselves to works that tackle the skip and search problem 5. Otherwise said, we don’t compare with works like TinyBERT [21] or MiniLM [34], which use extra losses like self-attention distribution matching. However, we expect that these methods, as well as state-of-the-art [24], [39] one can take full advantage of RAIL-KD, since they use deterministic layer mapping scheme.

3 RAIL-KD↩︎

The RAIL-KD method is sketched in Figure 1. In contrast to traditional intermediate layer distillation techniques which keep the selected layers of the teacher for distillation fixed during training, in RAIL-KD, at each epoch, a few intermediate layers from the teacher model are selected randomly for distillation. Here for simplicity, we set the number of selected intermediate layers of the teacher model equal to that of the student model. Our method is architecture agnostic can be applied to different domains such as computer vision and NLP.

RAIL-KD transfers intermediate knowledge of a pre-trained teacher \(T\) with \(n\) intermediate layers to a student model \(S_\theta\) with \(m\) intermediate layers. Let \((X,y)\) denotes a training sample \(X=(x_0,\cdots,x_{L-1})\) which is a sequence of \(L\) (sub-)tokens and \(y\) its corresponding label. In Figure 1, our Random Selection operator is applied to the intermediate layers of the teacher to randomly select \(m\) out of \(n\) layers. The intermediate layer representations of the \(m\) selected layers of the teacher and the student model corresponding to the \(X\) input can be described as \(H_X^T=\{H_{1,X}^T,\cdots,H_{m,X}^T\}\) and \(H_X^{S_\theta}=\{H_{1,X}^{S_\theta},\cdots,H_{m,X}^{S_\theta}\}\) respectively, where \(H_{i,X}^T = \cup_{k=0}^{L-1} \{H_{i,x_k}^T\} \in R^{L\times d_1}\) and \(H_{i,X}^{S_\theta}= \cup_{k=0}^{L-1} \{H_{i,x_k}^{S_{\theta}}\} \in R^{L\times d_2}\).

Here, \(d_1\) and \(d_2\) indicate the hidden dimension of the layers of the teacher and the student models respectively. To obtain \(H_{i,X}^T\) and \(H_{j,X}^S\), we need to find an aggregated representation for the sequence of \(L\) tokens at each layer of the two networks. In this regard, one can either use the \(<\)CLS\(>\) token representation or use the mean-pooling of the sequence representations of the layer. Since in [4], the mean-pooling representation shows better results, we adopt it to compute the sentence representation of each layer. Mean-pooling is a row-wise average over \(H_{i,X}^T\), \(H_{i,X}^{S_\theta}\) to get \(\bar{h}_{i,X}^T \in R^{d_1}\) \(\bar{h}_{i,X}^{S_\theta} \in R^{d_2}\)  [4]:

\[\begin{align} \bar{h}_{i,X}^T = \frac{1}{L} \sum_{k=0}^{L-1} H_{i,x_k}^T ; \bar{h}_{i,X}^{S_\theta} = \frac{1}{L} \sum_{k=0}^{L-1} H_{i,x_k}^{S_\theta} \end{align}\] RAIL-KD proposes the intermediate layer distillation in two different forms: using layer-wise distillation (see Fig. 1(a)) or by concatenating layer representations (see Fig. 1(b)).

3.1 Layer-wise RAIL-KD↩︎

In this setting, the representations \(\bar{h}_{i,X}^T \in R^{d_1}\) and \(\bar{h}_{i,X}^{S_\theta} \in R^{d_2}\) are projected into the same lower-dimensional space \(\hat{h}_{i,X}^T,\hat{h}_{i,X}^{S_\theta} \in R^u\) using \((d_1 \times u)\) and \((d_2 \times u)\) linear mappings respectively to calculate the layer-wise losses. \[\begin{align} & L_{\text{RAIL-KD}^l}= \sum_{X\in \mathcal{X}} \sum_{i=1}^m \alpha_i \left( \lvert\lvert \frac{\hat{h}_{i,X}^T}{\lvert\lvert\hat{h}_{i,X}^T \rvert\rvert_2}-\frac{\hat{h}_{i,X}^{S_\theta}}{\lvert\lvert\hat{h}_{i,X}^{S_\theta} \rvert\rvert_2}\rvert\rvert_2^2 \right) \end{align} \label{eq1}\tag{1}\] where \(\mathcal{X}\) denotes the set of training samples, and \(\alpha_i\) is a hyper-parameter to weigh the layer-wise distillation loss.

3.2 Concatenated RAIL-KD↩︎

In this setting, intermediate layer representations are concatenated and then distilled: \(\Bar{h}_X^T=[\Bar{h}_{1,X}^T,\cdots,\Bar{h}_{m,X}^T]\), \(\Bar{h}_X^{S_\theta}=[\Bar{h}_{1,X}^{S_\theta},\cdots,\Bar{h}_{m,X}^{S_\theta}]\) which are further mapped into the same lower-dimensional space \(\hat{h}_X^T,\hat{h}_X^{S_\theta} \in R^u\) using \((md_1 \times u)\) and \((md_2 \times u)\) linear mappings to calculate the concatenated distillation loss. \[L_{\text{RAIL-KD}^c}=\sum_{X\in \mathcal{X}} \lvert\lvert \frac{\hat{h}_X^T}{\lvert\lvert\hat{h}_X^T \rvert\rvert_2}-\frac{\hat{h}_X^{S_\theta}}{\lvert\lvert\hat{h}_X^{S_\theta} \rvert\rvert_2}\rvert\rvert_2^2 \label{eq2}\tag{2}\] Any type of loss such as contrastive [4], or mean-square-error (MSE) [1], [3] can be applied for our RAIL-KD approach.

3.3 Training Loss↩︎

The intermediate representation distillation loss \(L_{\text{RAIL-KD}}\) is combined with the original KD loss \(L_{\text{KD}}\), which is used to distill the knowledge from the output logits of the teacher model \(T\) to the output logits of the student model \(S_\theta\), and the original cross-entropy loss \(L_{\text{CE}}\). The total loss function for training the student model is: \[\mathcal{L}=\lambda_1 L_{\text{CE}} + \lambda_2 L_{\text{KD}} + \lambda_3 L_{\text{RAIL-KD}^{l/c}}\] where \(\lambda_1\), \(\lambda_2\), and \(\lambda_3\) are hyper-parameters of our model to minimize the total loss, and \(\lambda_1+\lambda_2+\lambda_3=1\).

Table 2: Dev and Test performances on GLUE benchmark when and Distill are used as backbone for the teacher and students variants respectively. Bold mark describes the best results.
Model CoLA SST-2 MRPC STS-B QQP MNLI QNLI RTE Avg.
Dev
Teacher 61.3 93.0 90.6 88.4 91.0 84.7 91.5 68.2 83.7
w/o KD 53.3 90.1 90.0 86.5 90.4 82.3 89.1 61.7 80.4
Vanilla KD 55.8 90.3 90.3 86.6 90.5 82.7 89.6 68.5 81.9
PKD 56.1 91.3 90.7 87.4 91.2 83.3 90.2 69.3 82.5
ALP-KD 56.8 90.8 90.6 87.5 91.0 83.4 90.2 70.4 82.7
RAIL-KD\(^l\) 58.8 92.8 91.0 87.8 91.2 83.5 90.3 70.4 83.2
RAIL-KD\(^c\) 57.2 91.9 90.8 87.9 91.4 83.5 90.1 72.2 83.2
Test
Teacher 52.0 92.9 87.8 82.3 88.9 84.3 90.7 66.0 81.0
w/o KD 50.7 91.7 87.2 80.4 88.3 81.4 88.4 57.6 78.6
Vanilla KD 50.9 91.0 87.7 81.0 88.5 82.2 88.7 60.6 79.2
PKD 50.6 92.0 87.2 81.7 89.1 82.7 89.0 60.6 79.5
ALP-KD 50.2 90.8 87.6 81.9 89.0 82.7 88.9 61.8 79.5
RAIL-KD\(^l\) 51.3 92.3 87.9 82.1 89.2 82.6 89.0 60.8 79.7
RAIL-KD\(^c\) 50.6 92.5 88.2 81.4 88.9 82.8 89.3 61.3 79.8

4 Experimental Protocol↩︎

4.1 Datasets and Evaluation↩︎

We evaluate RAIL-KD on 8 tasks from the GLUE benchmark [9]: 2 single-sentence (CoLA and SST-2) and 5 sentence-pair (MRPC, RTE, QQP, QNLI, and MNLI) classification tasks, and 1 regression task (STS-B). Following prior works [1], [3], [4], [21], we use the same metrics as the GLUE benchmark for evaluation. Moreover, to further show the generalization capability of our RAIL-KD method on out-of-domain (OOD) across tasks, we use Scitail [40], PAWS (Paraphrase Adversaries from Word Scrambling) [41], and IMDb (Internet Movie Database) [42] test sets to evaluate the models fine-tuned on MNLI, QQP, and SST-2 tasks respectively.

Table 3: Dev and Test performances on GLUE benchmark when RoBERTa\(_{24}\) and DistillRoberta\(_6\) are used as backbone for the teacher and student variants respectively. Bold mark describes the best results.
Model CoLA SST-2 MRPC STS-B QQP MNLI QNLI RTE Avg.
Dev
Teacher 68.1 96.4 91.9 92.3 91.5 90.1 94.6 86.3 88.9
w/o KD 56.6 93.1 89.5 87.2 91.0 84.6 91.3 65.7 82.4
Vanilla KD 60.9 93.1 90.2 89.0 91.1 84.7 91.3 68.2 83.6
PKD 62.3 91.6 90.9 88.9 91.6 84.4 91.1 71.1 84.0
ALP-KD 62.7 91.7 91.1 89.1 91.4 84.3 90.8 71.1 84.0
RAIL-KD\(^l\) 65.4 93.8 90.1 89.4 91.9 84.8 92.0 72.9 85.1
RAIL-KD\(^c\) 65.3 93.7 91.4 89.4 92.0 84.8 92.0 72.9 85.2
Test
Teacher 68.1 96.4 91.9 92.3 91.5 90.2 94.6 86.3 85.3
PKD 50.2 89.4 88.9 84.5 92.3 84.0 90.2 62.7 80.3
ALP-KD 53.6 89.6 89.2 84.6 92.8 83.6 90.4 64.4 81.0
RAIL-KD\(^l\) 53.4 89.5 88.9 84.8 93.6 84.5 91.1 63.5 81.2
RAIL-KD\(^c\) 53.6 89.6 89.6 84.8 93.4 83.9 91.6 63.8 81.3

4.2 Implementation Details↩︎

We run extensive experiments on 3 different teachers in order to ensure a fair comparison with of a wide range of prior works, and also to show the effectiveness of RAIL-KD. We experiment with the 12 layers Bert-base-uncased [5] as teacher (Bert\(_{12}\)) and 6 layer DistilBert [20] as student (DistillBert\(_6\)) to compare with PKD [1] and ALP-KD [3]. Also, we use 24 layers RoBERTa-large [6] and 6 layers DistilRoberta [43] as the backbone for teacher (RoBERTa\(_{24}\)) and student (DistilRoberta\(_6\)) respectively to compare models when \(n >> m\). Furthermore, we perform evaluation using 12 layers RoBERTa-base (RoBERTa\(_{12}\)) as teacher to be able to directly compare our numbers with the ones of CoDIR.

We re-implement PKD [1] and ALP-KD [3] approaches using the default settings proposed in the respective papers. We used early stopping based on performance on the development set, while making sure that the figures are in line with the ones reported in the papers. More precisely, the best layer setting for PKD teacher BERT12 is \(\{2, 4, 6, 8, 10\}\) to distill into DistilBERT6. For DistilRoBERTa6, we choose the intermediate layers 4, 8, 12, 16, 20 from the teacher RoBERTa24 model for distillation that we found to work the best on the development set.

Using ALP-KD, we compute attention weights for the intermediate layers of the teacher (i.e., 1 to 11 for BERT12 and 1 to 23 for RoBERTa24 models) to calculate the weighted intermediate representations of the teacher for each intermediate layer of the student model (i.e., 1 to 5 layers of the student models). Since, the hidden dimensions of the RoBERTa24 and DistilRoBERTa6 are different, we linearly transform them into same lower-dimensional space. We train the PKD and ALP-KD models following [1], [3].

For RAIL-KD\(^l\), at each epoch we randomly select 5 layers from the intermediate layers of the teacher (i.e., from layers 1 to 11 for BERT12 model and 1 to 23 for RoBERTa24 model). Then, we sort the layer indexes and perform layer-wise distillation (Figure 1 (a)) for RAIL-KD\(^l\). For RAIL-KD\(^c\), we concatenated the representations of the sorted randomly selected intermediate layers and then perform concatenated representation distillation (Figure 1 (b)). We use a linear transformation to map the intermediate representations (layer-wise or concatenated representations) into 128-dimensional space and normalize them before computing the loss \(L_{\text{RAIL-KD}^{l/c}}\) for both BERT12 and RoBERTa24 distillations. We fixed \(\alpha_i=1\), \(\lambda_1, \lambda_2, \lambda_3 = 1/3\) for our proposed approaches 6. We search learning rate from {1e-5, 2e-5, 5e-5, 4e-6}, batch size from {8, 16, 32}, and fixed the epoch number to 40 for all the experiments. we run all experiments 5 times and report average score, in order to validate the credibility of our results. We ran all the experiments on a single NVIDIA V100 GPU using mixed-precision training [44] and PyTorch [45] framework.

Our results indicate that random layer mapping not only delivers consistently better results than the deterministic mapping techniques such as PKD, but it has less computational overhead during training, while avoid extensive search experiments to find optimal mapping. On the other hand, using attention for layer selection (ALP-KD) or contrastive learning (CoDIR) leads to slightly worse result than than random selection.

Table 4: GLUE test results of RAIL-KD\(^c\) when using Roberta\(_{12}\) and DistilRoBERTa\(_6\) as backbone for teacher and students. Results of CoDIR are directly copied from their paper [4].
Model CoLA SST-2 MRPC QQP MNLI QNLI RTE Avg. Speedup
RoBERTa12 62.0 95.3 90.1 89.4 87.2 93.2 72.7 84.6 1x
CoDIR 53.6 93.6 89.4 89.1 83.2 90.4 65.6 81.0 1x
RAIL-KD\(c\) 54.2 93.6 88.4 89.5 83.9 91.7 64.5 81.2 1.96x

5 Results↩︎

Table 2 shows the performances of models trained on GLUE tasks, and evaluated on their respective dev and test sets for 12 layer to 6 layer distillation. Bert\(^{12}\) and DistilBERT\(^6\) are used as backbone for the teacher and student models respectively. The baselines are fine-tuning without KD (w/o KD) and Vanilla KD. Moreover, we directly compare RAIL-KD\(^{lc}\) results with PKD and ALP-KD as more competitor techniques.

First, we observe that in the 12 to 6 layer distillation, the performance gap between ILD methods and vanilla-KD is tight (0.8% and 0.3% on dev and test sets respectively). Moreover, as we expect, ALP-KD performs better (on dev) and similar (on test) compared to PKD with 0.2% improvement on the dev results. Second, results show that RAIL-KD outperforms the best ILD methods by margin of 0.5% and 0.3% on average on dev and test sets respectively. We notice that, except on RTE test, our RAIL-KD\(^{l/c}\) obtained the highest per-task performances. Third, we observe that RAIL-KD\(^{l/c}\) perform very similarly, which indicates that our method is effective on concatenated as well as layer-wise distillation.

Similar trends are seen on the 24 to 6 layer model compression experiments, which are reported in Table 3. In this experiment, we used Roberta\(_{24}\) and DistillRoberta\(_6\) as teacher and students models respectively. Overall, RAIL-KD outperforms the best baseline by 1.2% and 0.3% on dev and test sets respectively. Interestingly, the gap on dev compared with PKD and ALP-KD is larger than the one reported on Bert\(^{12}\) experiments, and PKD Test socres are much lower from that of ALP and RAIL-KD. This might be because PKD skips a large number of intermediate layers on RoBERTa\(_{24}\), and the computational cost of ALP-KD attention weights over a large number of teacher layers might produce smaller weights on Roberta\(_{24}\) compared to Bert\(_{12}\).

Furthermore, we demonstrate the effectiveness of RAIL-KD by directly comparing it with CoDIR [4], the current state-of-the-art ILD method. It uses the contrastive objective and a memory bank to extract a large number of negative samples for contrastive loss calculations. Table 4 shows GLUE test results of both approaches when distilling RoBERTa\(_{12}\) to DistillRoberta\(_6\). CoDIR results are adopted from their paper, and we followed their experimental protocol by not reporting scores on STS-B. In addition, we report the overall training time speedup against the teacher for different techniques. On average, RAIL-KD is almost twice faster while performs on par with CoDIR (+0.2%). Moreover, RAIL-KD outperforms CoDIR on 5 out of 8 datasets.

a

b

c

Figure 2: Cosine similarity between the intermediate layer representations of the Bert\(_{12}\) teacher and DistillBert\(_6\) student models compute don the SST-2 dataset..

a

b

c

Figure 3: Distribution of attention weights learned by DistilBERT\(_6\) ALP-KD on CoLA (left), RTE (middle), and MRPC (right). x-axis and y-axis are the teacher and student layer index respectively..

5.1 Impact of Random Layer Selection↩︎

To evaluate the impact of random layer selection on the performance of RAIL-KD compared to the other baselines, we report the standard deviation of the DistilBERT\(_6\) student models on the three smallest GLUE tasks, which are known to have the highest variance in Table 5.

Figures shows that RAIL-KD variance is at the same scale compared with PKD and ALP-KD on CoLA and MRPC, and even lower on RTE. This is a strong indicator that the gains from RAIL-KD are not due to random layer selection.

Table 5: Standards deviation (5 runs) of DistilBERT\(_6\) ILD models on the smallest three GLUE datasets. We also report the unweighted average on the 3 tasks.
CoLA RTE MRPC Avg.
PKD \(\pm\)0.14 \(\pm\)1.50 \(\pm\)0.24 \(\pm\)0.63
ALP-KD \(\pm\)0.95 \(\pm\)1.30 \(\pm\)0.70 \(\pm\)0.98
RAIL-KD\(^l\) \(\pm\)0.49 \(\pm\)0.40 \(\pm\)0.25 \(\pm\)0.38
RAIL-KD\(^c\) \(\pm\)0.51 \(\pm\)0.23 \(\pm\)0.40 \(\pm\)0.38

5.2 Out-of-Distribution Test↩︎

We further validate the generalization ability of student models by measuring their robustness to in-domain and out-of-domain evaluation. We do so by evaluating models fine-tuned on MLI, QQP and SST-2 and then evaluated on SciTail, PAWS, and IMDB respectively. These datasets contains counterexamples to biases found in the training data [46][48]. Performances of Bert\(_{12}\)/Roberta\(_{24}\) teacher and DistilBERT\(_6\)/DistilRoBERTa\(_6\) student variants are reported in Table 6. Also, we compute the unweighted average score of the three tasks.

First, we notice high variability in models rank and some inconsistencies in performances across tasks when compared with in-domain results. This was also reported in prior works on out-of-domain training and evaluation [48][51]. Still, RAIL-KD\(^{l/c}\) clearly outperforms all baselines across tasks. Surprisingly, we observe that PKD and ALP-KD perform poorly (on all three tasks) compared to the Vanilla KD baseline.

Table 6: Out-of-domain performances of models trained on MNLI, QQP, SST-2 and evaluated on SciTail, PAWS, and IMDB respectively. /Roberta\(_{24}\) and DistilBERT\(_6\)/DistilRoBERTa\(_6\) are used as backbone for the teacher and students respectively. For each setting, We report the unweighted average score on the 3 tasks.
Model SciTail PAWS IMDB Avg.
Teacher 70.3/82.7 43.3/43.3 84.6/88.9 66.0/71.6
w/o KD 68.7/74.9 36.5/34.7 81.3/85.8 62.2/65.1
Vanilla KD 68.6/76.1 42.2/36.6 82.0/86.1 64.3/66.3
PKD 68.0/74.8 39.9/36.5 80.9/85.4 62.9/65.6
ALP-KD 66.9/74.7 40.7/35.7 78.7/82.8 62.1/64.4
RAIL-KD\(^l\) 68.6/76.6 39.0/36.9 83.2/87.3 63.6/67.0
RAIL-KD\(^c\) 68.7/75.6 43.7/36.2 85.0/85.9 65.8/65.9

Interestingly, we observe that RAIL-KD\(^l\) performs consistently better (1.1% on average) than RAIL-KD\(^c\) on Roberta\(_{24}\) compression, while RAIL-KD\(^c\) perform better (1.1% on average) on Bert\(_{12}\). These results suggest that layer-wise distillation approach is more effective than concatenated distillation when we have a large capacity gap (layer number) between the teacher and the student, and vice versa.

6 Analysis↩︎

We run extensive analysis to better understand why RAIL-KD performs better than the other baselines. We visualize the layer-wise cosine similarity between the intermediate representations of the teacher and the student networks. Figure 2 shows the cosine similarity score between three intermediate layer representations of Bert\(_{12}\) teacher (i.e. layers 2, 4 and 6) and the first three layer representations of the student for PKD, ALP-KD, RAIL-KD\(^{l/c}\) students on 100 samples randomly selected from the SST-2 dataset. Due to space constraints, we only plot the scores for the first three layers of the student model. The similar trend are seen from the other layers.

We found that RAIL-KD allows the student to mimic teacher layers similar to PKD and much better than ALP-KD, despite that the mapping scheme varies at each epoch. Moreover, we observe that ALP-KD method gives less similarity scores in the upper intermediate layers. PKD gives lower similarity scores in the lower layers while improving in the upper layers. In contrast, our approach gives more stable similarity scores for all layers and getting closer to the teacher representation in the upper layers. We further investigate the attention weights learned by ALP-KD, and find out that they mostly focus on few layers (sparse attention). Figure 3 illustrates the distribution of weights, averaged on all training samples of DistilBERT\(_6\) ALP-KD studnet on CoLA (left), RTE (middle), and MRPC (right) 7. The figure clearly shows (light colors) that most of ALP weights are concentrated on top layers of the teacher. For instance, layers 1,2,5 of the three students mostly attend to the last layer of Bert\(_{12}\). This may be an indicator that ALP-KD overfits to the information driven from last layers. In contrast, the randomness in layer selection of RAIL-KD ensures a uniform focus on teacher layers. This may explain the poor performance of ALP-KD on out-of-domain evaluation compared with RAIL-KD.

7 Conclusion and Future Work↩︎

We introduced a novel, simple, and efficient intermediate layer KD approach that outperforms the conventional approaches with performance improvement and efficient training time. RAIL-KD selects random intermediate layers from the teacher equal to the number of intermediate layers of the student model. The selected intermediate layers are then sorted to distill their representations into the student model. RAIL-KD yields better regularization, which helps in performance improvement. Furthermore, our approach shows better performance for larger model distillation with faster training time, which opens up an avenue to investigate our approach for a super-large model such as GPT-2 [37] distillation using intermediate layers, as well as to improve robustness and generalization on a wider range of NLU tasks [52], [53].

Acknowledgments↩︎

We thank Mindspore8 for the partial support of this work, which is a new deep learning computing framework.

References↩︎

[1]
Siqi Sun, Yu Cheng, Zhe Gan, and Jingjing Liu. 2019. Patient knowledge distillation for bert model compression. https://arxiv.org/abs/1908.09355.
[2]
Yimeng Wu, Peyman Passban, Mehdi Rezagholizadeh, and Qun Liu. 2020. Why skip if you can combine: A simple knowledge distillation technique for intermediate layers. https://arxiv.org/abs/2010.03034.
[3]
Peyman Passban, Yimeng Wu, Mehdi Rezagholizadeh, and Qun Liu. 2021. https://ojs.aaai.org/index.php/AAAI/article/view/17610. In Thirty-Fifth AAAI Conference on Artificial Intelligence, AAAI 2021, Thirty-Third Conference on Innovative Applications of Artificial Intelligence, IAAI 2021, The Eleventh Symposium on Educational Advances in Artificial Intelligence, EAAI 2021, Virtual Event, February 2-9, 2021, pages 13657–13665. AAAI Press.
[4]
Siqi Sun, Zhe Gan, Yu Cheng, Yuwei Fang, Shuohang Wang, and Jingjing Liu. 2020. Contrastive distillation on intermediate representations for language model compression. In EMNLP.
[5]
Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2019. Bert: Pre-training of deep bidirectional transformers for language understanding. https://arxiv.org/abs/1810.04805.
[6]
Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, and Veselin Stoyanov. 2020. Roberta: A robustly optimized fbertg pretraining approach. arXiv preprint arXiv:1907.11692.
[7]
Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Russ R Salakhutdinov, and Quoc V Le. 2019. Xlnet: Generalized autoregressive pretraining for language understanding. In NeuRIPS.
[8]
Pranav Rajpurkar, Robin Jia, and Percy Liang. 2018. Know what you don’t know: Unanswerable questions for squad. In Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers), pages 784–789.
[9]
Alex Wang, Amanpreet Singh, Julian Michael, Felix Hill, Omer Levy, and Samuel R Bowman. 2018. Glue: A multi-task benchmark and analysis platform for natural language understanding. arXiv preprint arXiv:1804.07461.
[10]
Alex Wang, Yada Pruksachatkun, Nikita Nangia, Amanpreet Singh, Julian Michael, Felix Hill, Omer Levy, and Samuel R Bowman. 2019. Superglue: A stickier benchmark for general-purpose language understanding systems. Advances in Neural Information Processing Systems, 32.
[11]
Sheng Shen, Zhen Dong, Jiayu Ye, Linjian Ma, Zhewei Yao, Amir Gholami, Michael W Mahoney, and Kurt Keutzer. 2019. Q-bert: Hessian based ultra low precision quantization of bert. arXiv preprint arXiv:1909.05840.
[12]
Ofir Zafrir, Guy Boudoukh, Peter Izsak, and Moshe Wasserblat. 2019. Q8bert: Quantized 8bit bert. arXiv preprint arXiv:1910.06188.
[13]
Fu-Ming Guo, Sijia Liu, Finlay S Mungall, Xue Lin, and Yanzhi Wang. 2019. Reweighted proximal pruning for large-scale language representation. arXiv preprint arXiv:1909.12486.
[14]
Mitchell A Gordon, Kevin Duh, , and Nicholas Andrews. 2020. Compressing bert: Studying the effects of weight pruning on transfer learning. arXiv preprint arXiv:2002.08307.
[15]
Paul Michel, Omer Levy, and Graham Neubig. 2019. Are sixteen heads really better than one? in neurips. In NeurIPS.
[16]
Angela Fan, Edouard Grave, and Armand Joulin. 2019. Reducing transformer depth on demand with structured dropout. arXiv preprint arXiv:1909.11556.
[17]
Abbas Ghaddar and Philippe Langlais. 2019. Contextualized word representations from distant supervision with and for ner. In Proceedings of the 5th Workshop on Noisy User-generated Text (W-NUT 2019), pages 101–108.
[18]
Zhanghao Wu, Zhijian Liu, Ji Lin, Yujun Lin, and Song Han. 2020. Lite transformer with long-short range attention. arXiv preprint arXiv:2004.11886.
[19]
Hou Lu, Huang Zhiqi, Shang Lifeng, Jiang Xin, Chen Xiao, and Liu Qun. 2020. Dynabert: Dynamic bert with adaptive width and depth. arXiv preprint arXiv:2004.04037.
[20]
Victor Sanh, Lysandre Debut, Julien Chaumond, and Thomas Wolf. 2019. Distilbert, a distilled version of bert: smaller, faster, cheaper and lighter. arXiv preprint arXiv:1910.01108.
[21]
Xiaoqi Jiao, Yichun Yin, Lifeng Shang, Xin Jiang, Xiao Chen, Linlin Li, Fang Wang, and Qun Liu. 2019. Tinybert: Distilling bert for natural language understanding. arXiv preprint arXiv:1909.10351.
[22]
Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou. 2020. Mobilebert: a compact task-agnostic bert for resource-limited devices. arXiv preprint arXiv:2004.02984.
[23]
Wenhui Wang, Hangbo Bao, Shaohan Huang, Li Dong, and Furu Wei. 2020. Minilmv2: Multi-head self-attention relation distillation for compressing pretrained transformers. arXiv preprint arXiv:2012.15828.
[24]
Ahmad Rashid, Vasileios Lioutas, and Mehdi Rezagholizadeh. 2021. Mate-kd: Masked adversarial text, a companion to knowledge distillation. arXiv preprint arXiv:2105.05912.
[25]
Aref Jafari, Mehdi Rezagholizadeh, Pranav Sharma, and Ali Ghodsi. 2021. https://aclanthology.org/2021.eacl-main.212. In Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume, pages 2493–2504, Online. Association for Computational Linguistics.
[26]
Ehsan Kamalloo, Mehdi Rezagholizadeh, Peyman Passban, and Ali Ghodsi. 2021. Not far away, not so close: Sample efficient nearest neighbour data augmentation via minimax. arXiv preprint arXiv:2105.13608.
[27]
Cristian Buciluǎ, Rich Caruana, and Alexandru Niculescu-Mizil. 2006. Model compression. In Proceedings of the 12th ACM SIGKDD international conference on Knowledge discovery and data mining, pages 535–541.
[28]
Geoffrey Hinton, Oriol Vinyals, and Jeff. Dean. 2014. Distilling the knowledge in a neural network. NIPS Workshop, https://arxiv.org/abs/1503.02531.
[29]
Iulia Turc, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2019. Well-read students learn better: On the importance of pre-training compact models. arXiv preprint arXiv:1908.08962.
[30]
Jie Fu, Xue Geng, Zhijian Duan, Bohan Zhuang, Xingdi Yuan, Adam Trischler, Jie Lin, Chris Pal, and Hao Dong. 2020. Role-wise data augmentation for knowledge distillation. arXiv preprint arXiv:2004.08861.
[31]
Tianda Li, Ahmad Rashid, Aref Jafari, Pranav Sharma, Ali Ghodsi, and Mehdi Rezagholizadeh. 2021. http://arxiv.org/abs/2109.05696.
[32]
George-Eduard Zaharia, Andrei-Marius Avram, Dumitru-Clementin Cercel, and Traian Rebedea. 2021. https://aclanthology.org/2021.vardial-1.13/. In Proceedings of the Eighth Workshop on NLP for Similar Languages, Varieties and Dialects, VarDial@EACL 2021, Kiyv, Ukraine, April 20, 2021, pages 113–119. Association for Computational Linguistics.
[33]
Ahmad Rashid, Vasileios Lioutas, Abbas Ghaddar, and Mehdi Rezagholizadeh. 2020. Towards zero-shot knowledge distillation for natural language processing. arXiv preprint arXiv:2012.15495.
[34]
Wenhui Wang, Furu Wei, Li Dong, Hangbo Bao, Nan Yang, and Ming Zhou. 2020. Minilm: Deep self-attention distillation for task-agnostic compression of pre-trained transformers. arXiv preprint arXiv:2002.10957.
[35]
Mingi Ji, Byeongho Heo, and Sungrae Park. 2021. Show, attend and distill: Knowledge distillation via attention-based feature matching. In Proceedings of the AAAI Conference on Artificial Intelligence.
[36]
Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 5998–6008.
[37]
Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, and Ilya Sutskever. 2019. Language models are unsupervised multitask learners. https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf.
[38]
Yonglong Tian, Dilip Krishnan, and Phillip Isola. 2019. Contrastive representation distillation. arXiv preprint arXiv:1910.10699.
[39]
Xuanli He, Islam Nassar, Jamie Kiros, Gholamreza Haffari, and Mohammad Norouzi. 2021. Generate, annotate, and learn: Generative models advance self-training and knowledge distillation. arXiv preprint arXiv:2106.06168.
[40]
Tushar Khot, Ashish Sabharwal, and Peter Clark. 2018. Scitail: A textual entailment dataset from science question answering. In AAAI.
[41]
Yuan Zhang, Jason Baldridge, and Luheng He. 2019. Paws: Paraphrase adversaries from word scrambling. arXiv preprint arXiv:1904.01130.
[42]
Andrew L. Maas, Raymond E. Daly, Peter T. Pham, Dan Huang, Andrew Y. Ng, and Christopher Potts. 2011. Learning word vectors for sentiment analysis. In ACL.
[43]
Victor Sanh, Lysandre Debut, Julien Chaumond, and Thomas Wolf. 2019. Distilroberta, a distilled version of roberta: smaller, faster, cheaper and lighter. https://huggingface.co/distilroberta-base.
[44]
Paulius Micikevicius, Sharan Narang, Jonah Alben, Gregory Diamos, Erich Elsen, David Garcia, Boris Ginsburg, Michael Houston, Oleksii Kuchaiev, Ganesh Venkatesh, and Hao Wu. 2018. Mixed precision training. In In International Conference on Learning Representations.
[45]
Adam Paszke, Sam Gross, Francisco Massa, Adam Lerer, James Bradbury, Gregory Chanan, Trevor Killeen, Zeming Lin, Natalia Gimelshein, Luca Antiga, et al. 2019. Pytorch: An imperative style, high-performance deep learning library. Advances in neural information processing systems, 32:8026–8037.
[46]
Tom McCoy, Ellie Pavlick, and Tal Linzen. 2019. . In Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics, pages 3428–3448.
[47]
Tal Schuster, Darsh Shah, Yun Jie Serene Yeo, Daniel Roberto Filizzola Ortiz, Enrico Santus, and Regina Barzilay. 2019. Towards debiasing fact verification models. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP), pages 3410–3416.
[48]
Christopher Clark, Mark Yatskar, and Luke Zettlemoyer. 2019. Don’t take the easy way out: Ensemble based methods for avoiding known dataset biases. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP), pages 4060–4073.
[49]
Rabeeh Karimi Mahabadi, Yonatan Belinkov, and James Henderson. 2020. End-to-end bias mitigation by modelling biases in corpora. In Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics, pages 8706–8716. Association for Computational Linguistics.
[50]
Prasetya Ajie Utama, Nafise Sadat Moosavi, and Iryna Gurevych. 2020. Towards debiasing nlu models from unknown biases. In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP), pages 7597–7610.
[51]
Victor Sanh, Thomas Wolf, Yonatan Belinkov, and Alexander M Rush. 2020. Learning from others’ mistakes: Avoiding dataset biases without modeling them. arXiv preprint arXiv:2012.01300.
[52]
Abbas Ghaddar, Philippe Langlais, Ahmad Rashid, and Mehdi Rezagholizadeh. 2021. https://transacl.org/ojs/index.php/tacl/article/view/2669. Trans. Assoc. Comput. Linguistics, 9:586–604.
[53]
Abbas Ghaddar, Philippe Langlais, Mehdi Rezagholizadeh, and Ahmad Rashid. 2021. https://doi.org/10.18653/v1/2021.findings-acl.168. In Findings of the Association for Computational Linguistics: ACL/IJCNLP 2021, Online Event, August 1-6, 2021, volume ACL/IJCNLP 2021 of Findings of ACL, pages 1923–1929. Association for Computational Linguistics.

  1. This work has been done while Md Akmal Haidar was at Huawei.↩︎

  2. This work has been done while Nithin Anchuri was at Huawei.↩︎

  3. In some cases, the representations are directly matched if the teacher and student have the same hidden size.↩︎

  4. e.g. matching the first (or last) \(k\) layers of the student with their corresponding teacher layers.↩︎

  5. Only work that performs intermediate layer distillation↩︎

  6. We didn’t find a significant improvement when changing these values.↩︎

  7. Similar trends found on other datasets.↩︎

  8. https://www.mindspore.cn/↩︎