Adaptive Contrastive Knowledge Distillation for BERT Compression

Jinyang Guo, Jiaheng Liu, Zining Wang, Yuqing Ma, Ruihao Gong, Ke Xu, Xianglong Liu

Introduction

Large Language Models (LLMs) like BERT have revolutionized Natural Language Processing but remain computationally prohibitive for deployment on edge devices. Knowledge Distillation (KD) serves as a primary solution, where a compact "student" model is trained to replicate the behavior of a larger "teacher" model. However, traditional KD methods often rely on implicit learning, assuming the teacher's feature space is perfectly discriminative. If a teacher clusters semantically distinct but syntactically similar sentences—such as "I have dogs" and "I don't have dogs"—the student inherits this ambiguity. Furthermore, standard KD treats all samples equally, often failing to capture the nuances of "hard samples."

The ACKD Framework

To address these limitations, we propose Adaptive Contrastive Knowledge Distillation (ACKD). This framework moves beyond simple mimicry by incorporating explicit contrastive supervision and sample-specific focus.

The ACKD framework architecture, featuring Sample Adaptive Reweighting (SAR) and Dynamic Feature Storage (DFS) to enhance distillation.

The ACKD framework introduces three core innovations:

  1. Contrastive Distillation Loss (CDL): Unlike traditional KD, CDL provides explicit supervision by forcing the student to pull features of the same sample closer while pushing features of different classes (negative pairs) further apart.
  2. Sample Adaptive Reweighting (SAR): Recognizing that not all samples are equally informative, SAR utilizes a predictor network to estimate the "discrimination ability" of features. Harder samples—those more likely to be confused across classes—are assigned higher weights, directing the model's attention to critical semantic boundaries.
  3. Dynamic Feature Storage (DFS): To overcome GPU memory constraints that limit batch sizes in contrastive learning, DFS maintains a dynamic buffer of features from previous samples (e.g., the last 1,000). This ensures a diverse set of negative pairs is always available for comparison.

Experimental Results

The ACKD framework was evaluated on the GLUE benchmark, targeting both 3-layer (BERT$_3$) and 6-layer (BERT$_6$) student models. ACKD achieved state-of-the-art results, with particularly significant gains in highly compressed models. For instance, BERT$_3$ saw a +2.9% improvement on CoLA and a +3.6% improvement on MNLI-m compared to established baselines like TinyBERT and PKD.

Ablation study results showing the contribution of each component to the overall accuracy of the ACKD framework.

Ablation studies confirm the necessity of each component. As shown in the figure above, removing the Adaptive Contrastive Distillation loss ($\\mathcal{L}_{acd}$), SAR, or DFS leads to a measurable decline in accuracy, validating the integrated approach of the ACKD framework.

Discussion and Key Insights

A primary takeaway is that the teacher model is fallible. By using ground-truth labels to guide contrastive loss, the student can effectively "clean up" inherited ambiguities. This is especially vital for distinguishing between sentences with high syntactic similarity but divergent meanings. Our results suggest that as model capacity decreases, the need for explicit guidance on class separation becomes increasingly critical.

One current limitation of the framework is its sensitivity to hyperparameters ($\alpha, \beta, \gamma$), which currently require manual tuning for different tasks. Future work will explore automated tuning strategies to streamline deployment.

Conclusion

ACKD provides a robust framework for BERT compression by combining contrastive learning with adaptive sample weighting. By focusing on hard samples and maintaining feature diversity, ACKD enables the creation of lightweight models that retain high performance, paving the way for efficient LLM deployment on mobile and edge platforms.

Summary updated

BibTeX
@inproceedings{guo2023adaptive,
  author = {Jinyang Guo and Jiaheng Liu and Zining Wang and Yuqing Ma and Ruihao Gong and Ke Xu and Xianglong Liu},
  title = {{Adaptive Contrastive Knowledge Distillation for BERT Compression}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.findings-acl.569},
}
Figure