DynaBERT: Dynamic BERT with Adaptive Width and Depth

Lu Hou, Zhiqi Huang, Lifeng Shang, Xin Jiang, Xiao Chen, Qun Liu

Introduction

Large-scale pre-trained language models like BERT and RoBERTa have revolutionized NLP, but their substantial computational requirements hinder deployment on edge devices with limited memory and battery life. Conventional model compression techniques, such as pruning or distillation, typically yield fixed-size models. This lack of flexibility is problematic due to hardware diversity and fluctuating runtime resource constraints. DynaBERT addresses these challenges by providing a "one-size-fits-all" Transformer model capable of dynamically adjusting its width (attention heads and neurons) and depth (layers) at inference time without requiring separate training for each configuration.

Methods

The creation of DynaBERT involves a novel two-stage training procedure centered on adaptive flexibility and knowledge distillation.

Network Rewiring

To ensure that smaller sub-networks retain the most critical information, the authors rank attention heads and neurons by their importance using a first-order Taylor expansion. The network is then "rewired," moving high-impact components to the beginning of the indices. This allows "slicing" the model at various widths while preserving performance.

Two-Stage Distillation

The training process is divided into two main phases:

  1. Width-Adaptive Training (DynaBERT$_W$): A full-sized BERT teacher guides a student model to perform across multiple widths (1.0x, 0.75x, 0.5x, and 0.25x).
  2. Width- and Depth-Adaptive Training (DynaBERT): Using DynaBERT$_W$ as a "Teacher Assistant," the final model is trained for both width and depth flexibility. Depth is adjusted using an "Every Other" layer-dropping strategy.
The two-stage training pipeline for DynaBERT, transitioning from a static BERT to width-adaptive (DynaBERT_W) and finally width-and-depth-adaptive (DynaBERT) configurations.

Crucially, the authors employ In-place Distillation, where the largest model configuration acts as a teacher for its own smaller sub-networks, ensuring consistency across all 12 possible architectural configurations (4 widths × 3 depths).

Key Results and Insights

DynaBERT was evaluated on the GLUE benchmark and SQuAD v1.1. At its maximum size, DynaBERT matches or exceeds the performance of BERT${BASE}$ and RoBERTa${BASE}$. More importantly, its smaller sub-networks consistently outperform popular static compression methods.

Performance comparison of DynaBERT and DynaRoBERTa against various baselines (DistilBERT, TinyBERT, LayerDrop) across parameters, FLOPs, and latency on different hardware.

Key Findings:

  • Robustness of Width: The width dimension is more resilient to compression than depth. A width-reduced model often maintains higher accuracy than a depth-reduced counterpart with the same parameter count.
  • Teacher Assistant Benefit: Distilling directly from a full BERT to a depth-flexible model is challenging; using the width-adaptive DynaBERT$_W$ as an intermediate step significantly boosts performance.
  • Regularization Effect: Training for adaptivity appears to act as a form of regularization, sometimes allowing the largest DynaBERT configuration to outperform the original BERT$_{BASE}$.
  • Efficiency: On hardware like the Kirin 810 ARM CPU, DynaBERT sub-networks achieve significant speedups while maintaining high accuracy.

Conclusion

DynaBERT offers a versatile solution for deploying Transformer models across diverse and dynamic hardware environments. While the current two-stage distillation process is computationally intensive during training, the resulting flexibility provides a significant advantage for real-world applications. Future research may explore finer-grained adaptation and extending these dynamic principles to generative models like GPT.

Summary updated

BibTeX
@inproceedings{hou2020dynabert,
  author = {Lu Hou and Zhiqi Huang and Lifeng Shang and Xin Jiang and Xiao Chen and Qun Liu},
  title = {{DynaBERT: Dynamic BERT with Adaptive Width and Depth}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2020},
}
Figure