Knowledge Distillation from Internal Representations

Gustavo Aguilar, Yuan Ling, Yu Zhang, Benjamin Yao, Xing Fan, Chenlei Guo

TL;DR

This paper introduces a novel knowledge distillation technique that goes beyond mimicking a teacher model's output probabilities. By distilling the teacher's internal representations, the student model can better capture the teacher's linguistic properties, leading to improved generalization and performance, especially when data is scarce.

What Problem Does This Paper Address?

Large, state-of-the-art NLP models like BERT offer impressive performance but come with significant computational costs (training time, memory, inference speed). Knowledge distillation (KD) is a popular method to compress these large "teacher" models into smaller, more efficient "student" models. Traditional KD focuses on training the student to match the teacher's output probabilities (soft-labels). However, this approach doesn't guarantee that the student's internal workings truly mirror the teacher's learned representations. This "internal mismatch" can hinder the student's generalization capabilities, even if it performs well on the specific task. The paper addresses this by proposing a method to distill the teacher's internal representations directly into the student.

Methods

The core contribution is the proposed method for distilling internal representations. The authors formulate two key loss functions for this purpose:

  1. KL-divergence loss on self-attention probabilities: This loss encourages the student's self-attention mechanisms to mimic the teacher's. Self-attention patterns in models like BERT have been shown to capture linguistic information, so aligning these distributions helps transfer this knowledge.
  2. Cosine similarity loss on [CLS] token activations: This loss aims to ensure that the high-level contextual representation captured by the [CLS] token in the student is similar to that of the teacher.

They also propose three algorithms to orchestrate this distillation process:

  • Internal distillation of all layers: Simultaneously distills all corresponding layers from teacher to student.
  • Progressive internal distillation (PID): Distills knowledge from lower layers first, progressively moving to upper layers.
  • Stacked internal distillation (SID): Distills knowledge from lower layers and then stacks the loss from previous layers as it moves to higher layers, eventually performing standard classification distillation.

These internal distillation methods can be combined with standard soft-label distillation.

Datasets

The experiments were conducted on four datasets from the GLUE benchmark:

  • CoLA (Corpus of Linguistic Acceptability): Assesses grammatical correctness.
  • QQP (Quora Question Pairs): Determines if two questions are semantically equivalent.
  • MRPC (Microsoft Research Paraphrase Corpus): Identifies sentence paraphrases.
  • RTE (Recognizing Textual Entailment): Detects entailment relationships between sentences.

No new benchmarks were introduced; the paper leverages existing standard NLP tasks.

Key Insights and Ideas

  • Internal representations matter: The paper strongly suggests that simply matching output probabilities isn't enough for effective knowledge transfer. The student needs to learn how the teacher arrives at its decisions internally.
  • Bridging the gap: Distilling internal representations helps bridge the performance and generalization gap between large teacher models and smaller student models, especially when the teacher is significantly more complex (e.g., deeper).
  • Resilience to data scarcity: Internal KD shows greater resilience and maintains higher performance than standard KD when training data is limited. This is a crucial finding for practical applications where large labeled datasets are not always available.
  • Progressive/Stacked distillation is effective: The proposed PID and SID algorithms offer structured ways to transfer knowledge, with SID showing particularly strong results.
  • Student can disagree with teacher (and be right): In some cases, the internally distilled student makes different predictions than the teacher, and these predictions are sometimes more aligned with the ground truth or show more nuanced confidence, suggesting better generalization.

Key Results

  • Consistent performance gains: The proposed internal KD methods consistently outperform standard soft-label distillation across all four GLUE datasets.
  • Significant improvement: The paper reports statistical significance (p-value < 1e-3) for internal KD results compared to standard KD.
  • Parameter reduction: The BERT6 student model (6 layers) achieves comparable results to the BERTbase model (12 layers) with approximately 50% fewer parameters. For QQP, the F1 score was 91.38% for BERT6 with internal KD versus 91.45% for BERTbase.
  • Error analysis: Table 3 shows that the internally distilled student (Exp3.2) aligns better with the teacher's correct and incorrect predictions than the standard KD student (Exp2.0), indicating better replication of the teacher's behavior. Table 4 highlights cases where the internally distilled student makes correct predictions where the standard KD student fails.

Limitations and Future Work

  • Initialization: The paper notes that the initialization of the student model's layers is crucial and uses parameters from corresponding teacher layers. The exact strategy for this mapping is detailed but could be a point of further exploration.
  • Teacher model dependency: The effectiveness of the distillation is still dependent on the quality of the teacher model.
  • Computational cost of distillation: While the student is smaller, the distillation process itself can still be computationally intensive, especially with complex teacher models.
  • Future work: The authors suggest that their method can be combined with other compression techniques like quantization and pruning for even greater efficiency. They also imply that further exploration of different internal distillation strategies and layer mappings could yield further improvements.

Summary updated

BibTeX
@inproceedings{aguilar2020knowledge,
  author = {Gustavo Aguilar and Yuan Ling and Yu Zhang and Benjamin Yao and Xing Fan and Chenlei Guo},
  title = {{Knowledge Distillation from Internal Representations}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2020},
  doi = {10.1609/aaai.v34i05.6229},
}
Figure