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

Introduction

Large-scale pre-trained models like BERT have revolutionized Natural Language Processing (NLP), but their massive size (109M+ parameters) makes them computationally expensive and difficult to deploy on edge devices or in low-latency environments. To address this, Knowledge Distillation (KD) is used to train a smaller "student" model to mimic a large "teacher." However, standard KD methods often focus on point-wise imitation of hidden states, neglecting the structural information of the latent space—how different samples relate to one another. LRC-BERT addresses this by focusing on the "shape" of data through contrastive learning and angular distance.

Methodology

LRC-BERT introduces a novel framework centered on three key innovations designed to capture structural features and improve model robustness.

COS-NCE (Cosine-based Noise-Contrastive Estimation)

Instead of standard Euclidean distance, which measures the absolute gap between points, LRC-BERT utilizes angular distance. In high-dimensional latent spaces, the direction of a vector often carries more semantic meaning than its exact magnitude. This contrastive loss ensures that "positive pairs" (the same input in teacher vs. student) are aligned in direction, while "negative pairs" (different inputs) are pushed apart. This approach captures the distribution characteristics of intermediate layers more effectively than simple point-wise matching.

Gradient Perturbation-based Training

To enhance robustness, the authors introduce a perturbation to word embeddings during training. By calculating the gradient of the total loss with respect to the embeddings and using it to "interfere" with the input, the model learns to remain invariant to small, calculated fluctuations. This is the first attempt to use gradient perturbation within a KD framework to help smaller models generalize better.

Two-Stage Distillation

The training process is split into two distinct phases:

  1. Stage 1: Focuses purely on intermediate transformer layers using the COS-NCE loss to capture structural features.
  2. Stage 2: Introduces prediction layer distillation (using KL divergence and Cross-Entropy) to fine-tune the model for specific downstream tasks.
The LRC-BERT architecture illustrating the two-stage distillation process and the contrastive loss mechanism in the latent space.

Key Results

The model was evaluated on the GLUE (General Language Understanding Evaluation) benchmark across eight diverse tasks. The student model, a 4-layer transformer with 14.5M parameters, was compared against BERT-base and existing state-of-the-art compression methods.

Efficiency and Performance

LRC-BERT achieves a 9.6x speedup in inference time compared to BERT-base while maintaining high accuracy. It achieved an average score of 77.5 across GLUE tasks, significantly outperforming other compressed models like TinyBERT (76.5) and DistilBERT (71.9).

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

Robustness on Small Datasets

A notable finding was LRC-BERT's performance on smaller datasets. On the CoLA dataset (8.5k samples), LRC-BERT scored 46.5, compared to TinyBERT's 43.3. This suggests that the structural contrastive approach and gradient perturbation are particularly effective when training data is scarce, providing the smaller student model with the stability needed to generalize.

Conclusion and Limitations

LRC-BERT demonstrates that teaching a student model the "shape" of the teacher's latent space is more effective than simple value imitation. However, the method introduces some overhead due to negative sampling during training. Future work may explore the application of this contrastive approach to generative tasks and automated hyperparameter tuning for the two-stage training process.

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