LRC-BERT: Latent-representation Contrastive Knowledge Distillation for Natural Language Understanding

Hao Fu, Shaojun Zhou, Qihong Yang, Junjie Tang, Guiquan Liu, Kaikui Liu, Xiaolong Li

Abstract

LRC-BERT introduces a novel knowledge distillation framework that leverages contrastive learning to compress massive BERT models into lightweight versions suitable for edge devices. By focusing on the structural relationships between data samples in latent space—using angular distance rather than raw value matching—LRC-BERT achieves a 9.6x speedup over BERT-base while outperforming existing state-of-the-art compression methods like TinyBERT. This approach preserves 97.4% of the performance of the original teacher model with only 14.5 million parameters.

Introduction

Large-scale pre-trained models like BERT have revolutionized Natural Language Understanding (NLU), but their massive size (e.g., 109 million parameters for BERT-base) presents significant deployment challenges. These models require substantial memory and exhibit high inference latency, making them impractical for mobile phones or IoT devices.

Existing Knowledge Distillation (KD) techniques typically attempt to force a smaller "student" model to match the teacher's internal hidden states exactly, often using Mean Squared Error. However, this approach ignores critical structural information: how different samples relate to or differ from one another in the latent feature space. LRC-BERT addresses this by prioritizing the relative distribution of representations over absolute numerical values.

Methodology

The LRC-BERT framework introduces three primary innovations to enhance the distillation process:

  1. COS-NCE (Contrastive Loss): Instead of point-to-point value matching, the student learns the distribution of the teacher's intermediate layers. Utilizing Noise-Contrastive Estimation (NCE) based on angular distance (cosine similarity), the student is trained to pull its representation of a sample closer to the teacher's representation of that same sample (positive pair) while pushing it away from representations of different samples (negative pairs).
  2. Gradient Perturbation-Based Training: To improve robustness, the authors introduce a technique that adds calculated noise to word embeddings during training. This noise is derived from the gradient of the loss function, forcing the model to remain stable even when inputs are slightly perturbed.
  3. Two-Stage Distillation: Training is split into two phases. The first stage focuses on capturing the structural features of intermediate transformer layers. The second stage incorporates prediction layer losses (KL-Divergence and Cross-Entropy) to fine-tune for specific downstream tasks.
The LRC-BERT architecture illustrating the teacher-student distillation process and the contrastive learning setup in the latent space.

Key Insights

  • Structure over Values: The relative position of samples in the latent space is more critical for a student model than the absolute numerical values of the teacher's hidden states.
  • Angular Distance: The research demonstrates that angular distance is a superior metric for capturing semantic similarity in high-dimensional spaces compared to standard Euclidean distance.
  • The Power of Negatives: By explicitly defining what a sample is not through negative sampling, the student develops a sharper understanding of feature space boundaries.

Experimental Results

The model was evaluated on the GLUE (General Language Understanding Evaluation) benchmark, covering nine diverse NLU tasks including sentiment analysis, logic, and linguistic acceptability. LRC-BERT achieved an average GLUE score of 77.5, significantly outperforming other compressed models such as TinyBERT (76.5), BERT-PKD (72.6), and DistilBERT (71.9).

Performance comparison of LRC-BERT against other compressed BERT models on the GLUE benchmark.

In terms of efficiency, LRC-BERT is 9.6x faster than the teacher model, reducing inference time from 121.4s to 12.7s in benchmark tests. With only 14.5 million parameters, it is roughly 7.5x smaller than the 109M parameter BERT-base, yet it retains nearly all of the original model's performance.

Conclusion

LRC-BERT demonstrates that contrastive learning is a powerful tool for model compression in NLU. By focusing on the latent structural relationships between data points and incorporating gradient perturbation for robustness, it sets a new standard for lightweight BERT variants. Future work may extend this framework to even smaller architectures or larger generative models like GPT.

Summary updated

BibTeX
@inproceedings{fu2021lrc,
  author = {Hao Fu and Shaojun Zhou and Qihong Yang and Junjie Tang and Guiquan Liu and Kaikui Liu and Xiaolong Li},
  title = {{LRC-BERT: Latent-representation Contrastive Knowledge Distillation for Natural Language Understanding}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2021},
  doi = {10.1609/aaai.v35i14.17518},
}
Figure