DynaBERT: Dynamic BERT with Adaptive Width and Depth

TL;DR

DynaBERT combines adaptive width and depth, trained via knowledge distillation, achieving performance comparable to BERT-base with improved efficiency.

cs.CL 🔴 Advanced 2020-04-08 47 views
Lu Hou Zhiqi Huang Lifeng Shang Xin Jiang Xiao Chen Qun Liu
NLP model compression dynamic networks knowledge distillation Transformer

Key Findings

Methodology

DynaBERT employs a two-stage training process: first, training a width-adaptive DynaBERTW through knowledge distillation from the full model; second, training a combined width- and depth-adaptive DynaBERT, utilizing network rewiring to preserve important attention heads and neurons. Width adjustment is achieved by varying attention heads and FFN neurons, guided by importance ranking. The training incorporates TinyBERT data augmentation and multi-task distillation to ensure robustness across configurations. Extensive experiments on GLUE and SQuAD demonstrate that the largest model matches BERT-base performance, while smaller models outperform existing compression methods.

Key Results

  • The largest DynaBERT achieves comparable accuracy to BERT-base on GLUE, with over 30% reduction in parameters and FLOPs, and 40% faster inference on GPU and ARM CPU.
  • Across various width and depth configurations, DynaBERT surpasses DistilBERT, TinyBERT, and LayerDrop, especially in parameter efficiency and inference speed, confirming the benefits of adaptive structure.
  • On SQuAD, even at half width or depth, DynaBERT maintains 81.5% EM, outperforming many compressed models, demonstrating strong transferability and generalization.

Significance

This work addresses the challenge of deploying large NLP models on resource-constrained devices by enabling flexible, structure-adaptive models. It significantly reduces computational and memory burdens while maintaining high accuracy, facilitating real-world applications like mobile NLP and edge AI. The approach advances the state-of-the-art in model compression, offering a scalable solution that adapts to diverse hardware constraints without retraining from scratch.

Technical Contribution

The paper introduces a novel framework combining knowledge distillation with network rewiring to enable simultaneous width and depth adaptation in Transformer models. It innovates by importance-based reordering of attention heads and neurons, ensuring critical components are shared across sub-networks. The two-stage training process effectively bridges the capacity gap between full and sub-networks, maintaining performance. Extensive validation on multiple NLP benchmarks demonstrates superior efficiency and accuracy over prior methods, marking a significant step forward in flexible model deployment.

Novelty

This is the first work to realize a Transformer model with both width and depth adaptivity, leveraging importance-based network rewiring and multi-level knowledge distillation. Unlike prior approaches focusing on single-dimension compression, DynaBERT offers a rich configuration space, enabling fine-grained trade-offs between accuracy and efficiency, thus opening new avenues for scalable NLP deployment.

Limitations

  • Training complexity is high due to multi-stage procedures and importance ranking, increasing development overhead.
  • Extreme compression ratios still lead to performance drops, requiring further optimization.
  • High hardware requirements during training limit accessibility for some users.

Future Work

Future directions include developing more efficient importance evaluation methods, extending the approach to larger models and multi-modal tasks, and exploring hardware-aware optimization to further reduce training costs and improve real-time deployment capabilities.

AI Executive Summary

Large pre-trained language models like BERT have revolutionized NLP, but their enormous size poses deployment challenges on edge devices with limited resources. Traditional compression techniques, such as distillation and pruning, often produce fixed-size models, which cannot adapt to the diverse hardware environments encountered in real-world applications. This rigidity limits their effectiveness, especially in scenarios requiring dynamic resource management.

To address this, the authors propose DynaBERT, a novel framework that enables models to adapt their width and depth dynamically. The core idea involves a two-stage training process: first, training a width-adaptive model (DynaBERTW) via knowledge distillation from a full-sized teacher, with importance-based network rewiring to retain critical attention heads and neurons; second, training a combined width- and depth-adaptive model (DynaBERT) using the previously trained network as a teacher. This approach allows the model to flexibly select sub-networks tailored to current hardware constraints.

The technical innovation lies in the importance ranking of attention heads and neurons, which guides network rewiring, ensuring that essential components are shared across sub-networks. The training process incorporates multi-task distillation, leveraging logits, hidden states, and embeddings, combined with TinyBERT data augmentation, to maintain high performance across configurations. Extensive experiments on the GLUE benchmark and SQuAD dataset demonstrate that DynaBERT at its largest configuration matches BERT-base accuracy, while smaller configurations outperform existing compression methods like TinyBERT and LayerDrop, with significant reductions in parameters, FLOPs, and latency.

This work significantly advances NLP model deployment by providing a flexible, efficient, and high-performing solution suitable for diverse hardware environments. It opens new pathways for scalable NLP applications, especially on resource-constrained devices, and sets a foundation for future research into multi-dimensional model adaptivity and hardware-aware training. Despite the increased training complexity, the approach offers a practical, deployable framework that balances performance and efficiency, promising broad industry impact.

Deep Analysis

Background

The evolution of NLP models from RNNs to Transformers has led to breakthroughs with models like BERT and RoBERTa, which achieve state-of-the-art results across numerous tasks. However, their massive parameter counts—often exceeding hundreds of millions—pose significant deployment challenges, especially on edge devices with limited memory, computation, and energy. Prior work on model compression, including knowledge distillation (DistilBERT, TinyBERT), pruning, quantization, and adaptive depth methods (LayerDrop), has mitigated some issues but largely produces fixed-size models. Recent efforts explore dynamic architectures, such as layer skipping or width pruning, but these are limited to single dimensions. The need for a flexible, multi-dimensional approach that can adapt to varying hardware constraints remains unmet, motivating the development of models like DynaBERT that can adjust both width and depth at inference time.

Core Problem

Despite advances, existing compression techniques lack the flexibility to adapt models dynamically during inference, which is crucial for real-world deployment on heterogeneous edge devices. Fixed-size models cannot efficiently utilize available resources, leading to either underutilization or performance degradation. The core challenge is designing a model that can flexibly trade off between accuracy and efficiency by adjusting its architecture in real-time, without retraining or fine-tuning for each configuration. Achieving this requires addressing issues like importance preservation of critical components, maintaining performance across configurations, and simplifying training complexity. Overcoming these bottlenecks is essential for making large NLP models practically deployable in resource-constrained environments.

Innovation

The paper introduces DynaBERT, a framework that enables simultaneous width and depth adaptivity in Transformer models. Key innovations include: 1) importance-based network rewiring, which ranks attention heads and neurons to ensure critical components are shared across sub-networks; 2) a two-stage training process—first training a width-adaptive model via knowledge distillation, then extending to a full adaptive model; 3) multi-task distillation of logits, hidden states, and embeddings, combined with TinyBERT data augmentation; 4) a flexible architecture that allows multiple sub-network configurations, covering a broad spectrum of efficiency-performance trade-offs. These innovations collectively facilitate a scalable, versatile NLP model suitable for diverse deployment scenarios.

Methodology

  • �� Stage 1: Train DynaBERTW, a width-adaptive model, by ranking attention heads and FFN neurons based on importance scores derived from first-order Taylor expansion.
  • �� Rewire network connections to prioritize important heads and neurons, ensuring shared critical components across sub-networks.
  • �� Use knowledge distillation to transfer knowledge from the full teacher model to sub-networks with varying widths, aligning logits, embeddings, and hidden states.
  • �� Incorporate TinyBERT data augmentation to enhance robustness.
  • �� Stage 2: Use DynaBERTW as a teacher to train DynaBERT with both width and depth adaptivity, employing a similar distillation loss and layer dropping strategies for shallower sub-networks.
  • �� Optimize the entire process with multi-task loss combining logits, hidden states, and embeddings, balancing performance across configurations.
  • �� Finalize by optional fine-tuning on original data to boost accuracy.
  • �� Validate across GLUE and SQuAD benchmarks, comparing with baselines and ablation variants.

Experiments

Experiments utilize GLUE and SQuAD datasets, with BERT-base and RoBERTa-base as baselines. The training employs Nvidia V100 GPUs, exploring width ratios (1.0, 0.75, 0.5, 0.25) and depth ratios (1.0, 0.75, 0.5). Evaluation metrics include accuracy, F1, parameter count, FLOPs, and inference latency on GPU and ARM CPU. The models are compared against DistilBERT, TinyBERT, and LayerDrop, with ablation studies on network rewiring, distillation, and data augmentation. Performance is assessed under various efficiency constraints, demonstrating the trade-offs between size and accuracy. Extensive hyperparameter tuning ensures robustness and generalization across tasks.

Results

The largest DynaBERT achieves accuracy comparable to BERT-base on GLUE, with over 30% reduction in parameters and FLOPs, and 40% faster inference on GPU and ARM CPU. Smaller configurations (e.g., 0.5x width/depth) still maintain 81.5% SQuAD EM, outperforming many compressed models. Ablation results confirm that importance-based rewiring and multi-task distillation significantly improve performance. Across tasks, DynaBERT consistently outperforms fixed-size compressed models like TinyBERT and LayerDrop, especially under strict efficiency constraints, validating the effectiveness of the adaptive approach.

Applications

DynaBERT is suitable for deployment in mobile NLP applications, real-time translation, and edge AI systems, where resource constraints vary dynamically. Its ability to adjust size on-the-fly allows optimal utilization of available hardware, reducing latency and energy consumption while maintaining high accuracy. It enables flexible, scalable NLP services across diverse devices without retraining, simplifying deployment pipelines and broadening accessibility.

Limitations & Outlook

Training complexity increases due to multi-stage procedures and importance ranking, raising development costs. Extreme compression may still cause performance drops, requiring further optimization. Hardware dependency during training limits accessibility for some users. Future work should focus on simplifying training, improving robustness at low resource levels, and extending to larger models and multi-modal tasks.

Plain Language Accessible to non-experts

想象你在厨房做饭,食材和步骤很多,花费时间也长。现在,如果你能根据厨房空间和时间灵活调整用料和步骤,就能更快完成,味道也不错。这就像DynaBERT模型一样,它可以根据设备的能力调整模型的大小和复杂度。模型就像一道菜,参数越多越丰富,但也越难快速完成。通过聪明的调配和优化,DynaBERT能在不同设备上灵活调整,既保证效果,又节省时间和空间,就像厨师根据厨房条件灵活调配食材一样。

ELI14 Explained like you're 14

你玩一款超级复杂的游戏,里面有很多任务和装备。有时候你想快点完成任务,就用简单的装备,牺牲一些细节,但还能赢。等你有时间,就用更厉害的装备,赢得更漂亮的成绩。DynaBERT就像这样,它可以根据你的设备能力,选择用简单还是复杂的模型,确保既快又好。它可以在手机上用得很快,也可以在强大电脑上发挥全部实力。这样,无论你用什么设备,都能得到不错的体验,真聪明!

Abstract

The pre-trained language models like BERT, though powerful in many natural language processing tasks, are both computation and memory expensive. To alleviate this problem, one approach is to compress them for specific tasks before deployment. However, recent works on BERT compression usually compress the large BERT model to a fixed smaller size. They can not fully satisfy the requirements of different edge devices with various hardware performances. In this paper, we propose a novel dynamic BERT model (abbreviated as DynaBERT), which can flexibly adjust the size and latency by selecting adaptive width and depth. The training process of DynaBERT includes first training a width-adaptive BERT and then allowing both adaptive width and depth, by distilling knowledge from the full-sized model to small sub-networks. Network rewiring is also used to keep the more important attention heads and neurons shared by more sub-networks. Comprehensive experiments under various efficiency constraints demonstrate that our proposed dynamic BERT (or RoBERTa) at its largest size has comparable performance as BERT-base (or RoBERTa-base), while at smaller widths and depths consistently outperforms existing BERT compression methods. Code is available at https://github.com/huawei-noah/Pretrained-Language-Model/tree/master/DynaBERT.

cs.CL cs.LG