Knowledge Distillation from Internal Representations

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

Abstract

Large-scale Transformer models like BERT have set new standards in NLP but are often too computationally expensive for real-time deployment. This paper presents Stacked Internal Distillation (SID), a method that moves beyond standard output-based knowledge distillation by aligning the internal attention patterns and hidden states of a student model with those of a teacher. Our results demonstrate that a 6-layer BERT model can retain nearly 100% of the performance of a 12-layer teacher while achieving a 2x inference speedup.

Introduction

The massive scale of modern NLP models presents a significant barrier to their practical application. Knowledge Distillation (KD) has emerged as a solution, where a smaller "student" model is trained to mimic a larger "teacher." However, traditional KD often focuses solely on the teacher's final output probabilities (soft labels). This paper argues that such an approach fails to capture the rich linguistic logic—such as syntax and coreference—developed within the teacher's intermediate layers, leading to students that might predict correctly for the wrong reasons.

Methodology: Stacked Internal Distillation (SID)

To bridge the gap between teacher and student, the authors propose distilling the "internal soul" of the model. This is achieved through two primary loss functions:

  1. KL-Divergence on Attention: Forcing the student to match the teacher’s self-attention matrices ensures the student learns to focus on the same word relationships.
  2. Cosine Similarity on Hidden States: Aligning the student's [CLS] token hidden vector with the teacher's ensures their internal semantic representations are consistent.

The training follows a Stacked Internal Distillation (SID) regime. Instead of a flat training approach, SID progressively adds higher layers while maintaining the losses from lower ones, creating a curriculum that moves from basic linguistic features to complex semantics.

Figure 1: Architecture comparison and layer mapping between the Teacher (BERT base) and the Student (simplified BERT).

Experimental Setup and Datasets

The approach was evaluated on four key tasks from the GLUE benchmark:

  • CoLA: Linguistic acceptability (grammar).
  • QQP: Semantic similarity between Quora question pairs.
  • MRPC: Paraphrase identification.
  • RTE: Textual entailment.

Key Insights: Why Internal Alignment Works

The study reveals that matching internal representations provides a "denser" signal than output labels alone. This is particularly evident in data-scarce scenarios, where the teacher's internal guidance allows the student to reach high performance with fewer examples.

A critical "Aha" moment comes from qualitative analysis. While standard KD students often exhibit "blurry" attention patterns, those trained with internal distillation develop sharp, meaningful patterns that mirror the teacher's linguistic parsing.

Figure 5: Attention map comparison showing that Internal KD leads to sharper, more teacher-aligned attention patterns compared to Standard KD.

Results

The SID method consistently outperformed standard soft-label distillation. On the QQP dataset, a 6-layer student (BERT$_6$) achieved a 91.38 F1 score, nearly matching the 12-layer teacher's 91.45 F1. On the CoLA grammar task, the SID student scored 46.09 (MCC), significantly higher than the 41.72 achieved by standard KD. Overall, the resulting 6-layer models offer a 2x speedup in inference time, making them highly suitable for production environments.

Limitations and Future Work

While effective, the current SID approach assumes architectural similarity between teacher and student. Future research could explore mapping functions for cross-architecture distillation (e.g., Transformer to CNN). Additionally, while 6-layer models perform excellently, extreme compression to 2 or 4 layers still results in noticeable performance drops, highlighting the limits of current distillation techniques.

Summary updated

BibTeX
@inproceedings{aguilar2020knowledge,
  author = {Gustavo Aguilar and Yuan Ling and Yu Zhang and Benjamin Z. 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