Abstract
Pre-trained language models like BERT have revolutionized Natural Language Processing (NLP) but are often too large for deployment in resource-constrained environments. Knowledge Distillation (KD) addresses this by training a smaller "student" model to mimic a larger "teacher." However, existing methods typically employ a rigid one-to-one layer mapping, which may not be optimal for all tasks. We propose BERT-EMD, a framework that utilizes Earth Mover’s Distance (EMD) to facilitate a flexible many-to-many layer mapping. This approach, combined with a novel cost attention mechanism, allows the student to adaptively learn from the most relevant teacher layers. BERT-EMD consistently outperforms previous distillation methods on the GLUE benchmark, achieving significant speedups with minimal accuracy loss.
Introduction
The massive scale of models like BERT-base (110M+ parameters) poses significant challenges for real-time applications and mobile deployment. While Knowledge Distillation has proven effective for model compression, standard techniques like TinyBERT or BERT-PKD enforce a fixed mapping between specific teacher and student layers (e.g., student layer $i$ always learns from teacher layer $j$).
This rigidity is problematic because different NLP tasks rely on different linguistic features—such as syntax or semantics—which are distributed across various layers of the teacher model. A fixed mapping prevents the student from identifying the most beneficial sources of knowledge for a specific task. BERT-EMD addresses this by treating layer mapping as an optimization problem, allowing for a more holistic and task-adaptive transfer of knowledge.
Methods: Many-to-Many Knowledge Transfer
The core innovation of BERT-EMD is the transition from one-to-one to many-to-many layer mapping using Earth Mover's Distance.
Earth Mover’s Distance (EMD)
Instead of predefined pairs, BERT-EMD allows every student layer to potentially learn from any teacher layer. EMD is used to calculate the "minimum work" required to transform the teacher's knowledge distribution into the student's architecture. This enables the model to find an optimal global alignment between the two sets of layers.
Cost Attention Mechanism
To further refine the distillation process, the authors introduce a cost attention mechanism. Not all teacher layers are equally easy for a student to replicate. This mechanism automatically assigns higher weights to layer pairs that have lower transfer costs, ensuring the student focuses on the most "distillable" and relevant information. This accelerates convergence and improves final performance.
Distillation Objectives
The training objective incorporates three primary components:
- Embedding-layer distillation: Aligning initial word representations.
- Prediction-layer distillation: Matching the final output logits.
- Transformer distillation: Applying the many-to-many EMD approach to both Attention maps and Hidden States.
Key Results
BERT-EMD was evaluated on the General Language Understanding Evaluation (GLUE) benchmark, comparing it against established baselines like DistilBERT, TinyBERT, and BERT-PKD.
Performance and Efficiency
As shown in the results table, BERT-EMD consistently achieves superior accuracy. The 4-layer BERT-EMD model reached an average GLUE score of 74.66, surpassing TinyBERT (73.51) and BERT-PKD (72.60). Remarkably, this 4-layer version is 9.4x faster than the original BERT-base while retaining roughly 13% of the parameters.
The 6-layer variant of BERT-EMD (BERT-EMD$_6$) achieved an average score of 80.00, actually outperforming the full 12-layer BERT-base on 7 out of 9 tasks, despite having 50% fewer parameters.
Analysis and Conclusion
Visualization of the layer "flow" reveals that BERT-EMD is truly task-adaptive. For simpler tasks, the mapping remains relatively diagonal, but for complex tasks like RTE (Recognizing Textual Entailment), the student layers draw information from a much broader range of teacher layers. This flexibility allows the student to capture a more comprehensive summary of the teacher's internal representations.
While the many-to-many approach introduces additional training complexity due to the EMD optimization, the gains in inference speed and accuracy make it a powerful tool for deploying high-performance NLP models on edge devices. Future work may explore scaling this approach to even larger architectures and optimizing the training overhead.
Summary updated