Gradient Knowledge Distillation for Pre-trained Language Models

Lean Wang, Lei Li, Xu SunarXiv 2022

Abstract

Knowledge distillation (KD) is an effective framework to transfer knowledge from a large-scale teacher to a compact yet well-performing student. Previous KD practices for pre-trained language models mainly transfer knowledge by aligning instance-wise outputs between the teacher and student, while neglecting an important knowledge source, i.e., the gradient of the teacher. The gradient characterizes how the teacher responds to changes in inputs, which we assume is beneficial for the student to better approximate the underlying mapping function of the teacher. Therefore, we propose Gradient Knowledge Distillation (GKD) to incorporate the gradient alignment objective into the distillation process. Experimental results show that GKD outperforms previous KD methods regarding student performance. Further analysis shows that incorporating gradient knowledge makes the student behave more consistently with the teacher, improving the interpretability greatly.

Introduction

Knowledge Distillation (KD) has become a standard technique for compressing massive Pre-trained Language Models (PLMs) like BERT into smaller, more efficient versions suitable for real-world deployment. Traditionally, KD relies on "point-to-point" matching, where the student model attempts to mimic the teacher's final predictions (logits) or intermediate feature representations for specific input instances.

However, this approach often overlooks the gradient, a vital source of information that characterizes the teacher's sensitivity to input perturbations. While traditional KD informs the student about the "elevation" (output) of the teacher's mathematical landscape at specific points, it fails to convey the "slope" or "curvature" (gradient). Without this higher-order knowledge, a student model might achieve correct predictions for the wrong reasons, leading to poor generalization and inconsistent behavior when inputs are slightly modified.

Proposed Method: Gradient Knowledge Distillation (GKD)

To address these limitations, we propose Gradient Knowledge Distillation (GKD). The core idea is to align the gradients of the student model with those of the teacher model, effectively teaching the student the "why" behind the teacher's decisions.

Comparison between Traditional KD and Gradient KD.

Implementing GKD for NLP tasks presents two primary technical challenges:

  1. The Discreteness Problem: Gradients cannot be directly calculated with respect to discrete word tokens. GKD overcomes this by calculating gradients with respect to input embeddings. To ensure a consistent input space, the student's embedding layer is initialized with the teacher's and frozen during distillation.
  2. The Dropout Conflict: Standard training uses Dropout for regularization. However, the authors provide a mathematical proof demonstrating that Dropout introduces bias into gradient estimation. To provide the student with a clean, unbiased signal, Dropout is deactivated during the GKD process.

We also introduce GKD-CLS, a variant that specifically aligns the gradients of the [CLS] token across multiple layers. This ensures the student captures the teacher's decision-making logic at every stage of the network.

Experimental Setup

The proposed method was evaluated on four tasks from the GLUE (General Language Understanding Evaluation) benchmark: SST-2 (sentiment analysis), QQP (semantic equivalence), MNLI (natural language inference), and QNLI (question-answering NLI). The researchers utilized a 12-layer BERT-base model as the teacher and a 6-layer BERT model as the student.

Results and Analysis

Experimental results demonstrate that GKD-CLS consistently outperforms vanilla KD and BERT-PKD across GLUE tasks, achieving an average score of 83.7 compared to 83.1 for vanilla KD. Furthermore, GKD is compatible with other distillation frameworks; applying GKD-CLS to DistilBERT boosted its average GLUE score from 83.1 to 84.3.

Beyond performance gains, GKD significantly improves the interpretability and consistency of the student model. By matching gradients, the student learns the sensitivity of the teacher, effectively mimicking its internal reasoning process.

Saliency maps showing how GKD improves interpretability and consistency with the teacher.

As shown in the saliency maps above, GKD-trained students exhibit much higher "Saliency Loyalty" (SL). On the SST-2 dataset, GKD-CLS achieved an SL of 53.5, nearly doubling the 31.2 score of vanilla KD. This indicates that the student model focuses on the same critical words as the teacher when making predictions.

Conclusion

Gradient Knowledge Distillation (GKD) provides a novel way to transfer higher-order knowledge from teacher to student models by aligning their gradients. While the method increases training time by approximately 2x due to the need for second-order gradient calculations, it results in student models that are more accurate, consistent, and interpretable without any additional inference overhead. Future work will explore integrating GKD with diverse architectures and other distillation frameworks.

Summary updated

BibTeX
@misc{wang2022gradient,
  author = {Lean Wang and Lei Li and Xu Sun},
  title = {{Gradient Knowledge Distillation for Pre-trained Language Models}},
  year = {2022},
  eprint = {2211.01071},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.01071},
}
Figure