Introduction
The evolution of Natural Language Processing (NLP) has been driven by increasingly large pre-trained language models (LMs) such as BERT and RoBERTa. Despite their state-of-the-art performance, their massive parameter counts pose significant challenges for deployment in resource-constrained environments or low-latency applications. Knowledge Distillation (KD) has emerged as a primary solution, where a compact "student" model is trained to replicate the behavior of a larger "teacher" model.
Standard distillation techniques typically employ a coordinate-wise loss, such as $L_2$ distance, to align the intermediate hidden layers of the student and teacher. However, this approach assumes that each dimension of the representation is independent, neglecting the rich structural dependencies and higher-order relationships that characterize the teacher's internal logic. This paper introduces CoDIR (Contrastive Distillation on Intermediate Representations), a method that leverages contrastive learning to capture these structural patterns more effectively.
Methodology: The CoDIR Framework
CoDIR moves beyond simple value matching by adopting a contrastive objective based on InfoNCE. The core idea is to maximize the mutual information between the student's and teacher's intermediate representations.
Contrastive Objective and Summarization
The student is trained to identify a "congruent pair"—the teacher and student's representations of the same input—among a set of "incongruent pairs" representing different inputs. This forces the student to learn the underlying data structure rather than just memorizing numerical values. To provide a robust distillation target, CoDIR utilizes mean-pooling across all hidden states in a layer, which the authors found to be more effective than relying solely on the [CLS] token.
Efficient Training with Memory Banks
Contrastive learning typically requires a large number of negative samples to be effective. To avoid the prohibitive computational cost of re-running the teacher model for thousands of samples, CoDIR implements a memory bank. This stores representations from previous training iterations, allowing the student to contrast against a vast pool of negative examples efficiently.
Two-Stage Distillation
CoDIR is designed to be versatile, functioning in two stages:
- Pre-training Distillation: Capturing general linguistic knowledge using large-scale corpora like BookCorpus.
- Fine-tuning Distillation: Transferring task-specific knowledge for downstream applications such as sentiment analysis or natural language inference.
Experimental Results
The effectiveness of CoDIR was validated using the General Language Understanding Evaluation (GLUE) benchmark. The researchers compared CoDIR against several prominent distillation baselines, including DistilBERT, TinyBERT, and BERT-PKD.
Performance and Efficiency
As shown in the results, CoDIR consistently outperforms existing methods across various GLUE tasks. A 6-layer student model compressed via CoDIR achieves performance comparable to the original 12-layer BERT-base while providing a 2.08x speedup in inference time. Ablation studies further confirmed that the contrastive loss provides a unique learning signal that complements standard cross-entropy and KD losses, leading to an average improvement of approximately 1.0% on GLUE scores when added to standard fine-tuning.
Conclusion
CoDIR demonstrates that learning the structural relationships within a teacher model's intermediate layers is superior to simple coordinate-wise matching. By integrating contrastive learning into both pre-training and fine-tuning, CoDIR enables the creation of highly efficient, compact models that retain the powerful capabilities of their larger counterparts. Future work may explore applying this framework to even larger teacher models or developing more advanced negative sampling strategies to further refine the distillation process.
Summary updated