Contrastive Distillation on Intermediate Representations for Language Model Compression

Siqi Sun, Zhe Gan, Yu Cheng, Yuwei Fang, Shuohang Wang, Jingjing Liu

Abstract

Large-scale language models like BERT and RoBERTa have set new benchmarks in NLP but remain computationally intensive for real-time deployment. Knowledge Distillation (KD) is a popular compression technique, yet traditional methods often rely on simple $L_2$ loss to align intermediate layers, which ignores the complex structural dependencies within the teacher's representations. We introduce CoDIR (Contrastive Distillation on Intermediate Representations), a framework that leverages contrastive learning to bridge the gap between teacher and student models. By training the student to recognize structural patterns rather than just mimicking raw values, CoDIR produces a 6-layer model that matches the performance of a 12-layer BERT-base while running twice as fast.

Introduction

While large language models have revolutionized NLP, their size makes them difficult to deploy on mobile devices or in latency-sensitive applications. Knowledge Distillation typically involves a small "student" model learning from a large "teacher." However, most existing methods use mean squared error ($L_2$ loss) to force the student’s intermediate hidden layers to match the teacher’s. This approach assumes independence between dimensions, failing to capture the rich structural knowledge—the relationships and dependencies between different data points—inherent in the teacher's latent space.

Methodology: The CoDIR Framework

CoDIR redefines distillation as a matching problem using contrastive learning. Instead of direct value regression, the student must identify the teacher’s representation for a given sentence (congruent pair) among a large set of negative samples (incongruent pairs).

The CoDIR framework architecture showing contrastive distillation between teacher and student models across pre-training and fine-tuning stages.

Key Technical Innovations

  • InfoNCE Loss: CoDIR employs a contrastive objective to maximize the mutual information between teacher and student representations.
  • Memory Bank: To manage the computational cost of thousands of negative samples, representations are stored in a memory bank that updates gradually during training.
  • Mean Pooling: Research indicated that averaging representations across the entire sentence (mean pooling) provides a richer signal for distillation than relying solely on the [CLS] token.
  • Hard Negative Sampling: During pre-training, negatives are sampled from the same mini-batch. Since these often originate from the same article, they are semantically similar, forcing the student to learn finer nuances to distinguish them.

Experimental Results

CoDIR was evaluated on the GLUE (General Language Understanding Evaluation) benchmark, covering tasks from sentiment analysis (SST-2) to natural language inference (MNLI).

Comparison of CoDIR against baseline and state-of-the-art compressed models on the GLUE benchmark.

Performance and Efficiency

A 6-layer student trained with CoDIR achieved an average GLUE score of 81.2, effectively matching the performance of the original 12-layer BERT-base (81.7). Notably, the CoDIR student is approximately 2.08x faster during inference than BERT-base and maintains a significantly smaller memory footprint. CoDIR consistently outperformed other popular compression methods like DistilBERT, TinyBERT, and BERT-PKD across almost all GLUE tasks, with particularly strong gains on smaller datasets where structural knowledge is harder to acquire.

Discussion and Conclusion

The success of CoDIR highlights that the relationship between data points in latent space is often more critical than specific numerical vector values. Contrastive loss acts as a powerful regularizer for intermediate layers, helping the student generalize better than if it only observed the teacher's final output.

While highly effective, the authors note that the gains from distillation are less pronounced on extremely large datasets like QQP, where simple fine-tuning can eventually close the performance gap. Future work may involve scaling CoDIR to even larger teacher models like GPT-3 or exploring more sophisticated negative sampling strategies to further enhance the distillation signal.

Summary updated

BibTeX
@inproceedings{sun2020contrastive,
  author = {Siqi Sun and Zhe Gan and Yu Cheng and Yuwei Fang and Shuohang Wang and Jingjing Liu},
  title = {{Contrastive Distillation on Intermediate Representations for Language Model Compression}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2020},
  doi = {10.18653/v1/2020.emnlp-main.36},
}
Figure