TrimBERT: Tailoring BERT for Trade-offs

Sharath Nittur Sridhar, Anthony Sarah, Sairam SundaresanarXiv 2022

TL;DR

TrimBERT introduces a set of architectural optimizations for BERT-Base that significantly reduce model size and increase training/inference throughput with minimal impact on accuracy. By selectively removing intermediate feed-forward layers, replacing the computationally expensive softmax operation with simple normalization, and pruning redundant LayerNorm operations, the authors provide a flexible framework for tailoring models to specific hardware and performance constraints.

What Problem Does This Paper Address?

While Transformer-based models like BERT have revolutionized NLP, their massive size—ranging from 110 million parameters in BERT-Base to 175 billion in GPT-3—creates a significant barrier to entry. Training and deploying these models require immense computational resources and time.

The authors identify three specific areas of inefficiency in the standard BERT architecture:

  1. Redundant Intermediate Layers: The standard BERT encoder alternates between self-attention blocks and intermediate feed-forward blocks. The authors argue that the necessity of having an intermediate block for every attention block is not well-justified.
  2. Softmax Bottlenecks: The softmax operation used to calculate attention scores involves exponentiation and division, which are computationally expensive, especially as sequence lengths grow.
  3. LayerNorm Overhead: While LayerNorm is essential for training stability, it adds computational overhead and may contribute to overfitting.

Methods

The researchers propose three distinct modifications to the BERT-Base architecture:

1. Layer Removal

The authors introduce an architectural hyperparameter, $n$, which determines the frequency of intermediate blocks. Instead of the standard 1:1 ratio, an intermediate block is only added after every $n$ self-attention blocks.

  • Figure 2 shows the standard sequential arrangement.
  • Figure 3 illustrates the modified architecture where $n=2$, effectively halving the number of intermediate blocks.
Figure 3: First six blocks of the modified network architecture with n = 2. Note the removal of intermediate blocks between every n = 2 self-attention blocks (compare to Figure 2).

2. Softmax Replacement (BANDD)

To eliminate the softmax bottleneck, the authors propose BANDD (BERT Attention Normalization with Divided Dropout). They replace softmax with a simple normalization of the query-key dot products. Crucially, they found that removing the dropout layer after self-attention was necessary for convergence in this setup, compensating for its absence by using L2 normalization in the loss function during pre-training.

Figure 4: Reference unit used in the BANDD BERTBASE modified network (compare to Figure 1).

3. LayerNorm Removal (NoMLPLN)

The authors experimented with removing LayerNorm operations. While removing LayerNorm after self-attention caused training to diverge, removing it after the intermediate (MLP) blocks—a configuration they call NoMLPLN—actually improved fine-tuning accuracy in some cases.

Figure 5: First six blocks of the NoMLPLN network architecture (compare to Figure 2).

Datasets

  • Pre-training: The models were pre-trained from scratch using the English Wikipedia and BookCorpus datasets.
  • Evaluation: Performance was measured using the GLUE (General Language Understanding Evaluation) benchmark, specifically focusing on MNLI, QNLI, QQP, and SST-2 tasks.

Key Insights and Ideas

  • The "Intermediate" Block is Negotiable: One of the most striking findings is that you can remove a significant portion of the feed-forward layers with very little impact on downstream task accuracy. This suggests that the heavy lifting in Transformers is primarily done by the self-attention mechanism.
  • Softmax Scales Poorly: As shown in Figure 6, the throughput benefit of replacing softmax (BANDD) increases dramatically with sequence length. At a sequence length of 1024, BANDD increases throughput to 1.41x compared to just 1.13x for layer removal alone.
  • LayerNorm Can Cause Overfitting: The fact that removing LayerNorm after MLP blocks (NoMLPLN) sometimes increased accuracy suggests that these layers might be helping the model memorize the training set rather than generalizing to new data.
  • No Teacher Required: Unlike distillation methods (e.g., DistilBERT), TrimBERT variants are trained from scratch and do not require a large "teacher" model, simplifying the pipeline.
Figure 6: Throughput increase as a function of the sequence length for the n = 2 and BAND + n = 2 networks with batch size of 32 on a NVIDIA A100 GPU. Although the throughput increases for all sequence lengths for the n = 2 variant, it further increases by also applying BANDD.

Key Results

  • Efficiency vs. Accuracy: With $n=3$, the model is 1.5x smaller and 1.4x faster on GPUs, with less than a 2% drop in accuracy across most tasks.
  • Extreme Compression: At $n=\infty$ (removing all intermediate blocks), the model size is halved (2.06x decrease) and GPU throughput increases by 1.72x.
  • Throughput Gains: Combining BANDD, NoMLPLN, and $n=\infty$ resulted in a 1.85x GPU throughput increase compared to the baseline BERT-Base.
  • Competitive Performance: TrimBERT variants proved highly competitive with established distillation techniques like DistilBERT and BERT-of-Theseus while offering more direct control over architectural trade-offs.

Limitations and Future Work

  • Model Scale: The study focused exclusively on BERT-Base. The authors note that future work should investigate if these trade-offs hold for larger models like BERT-Large or GPT-3.
  • Integration: There is potential to combine these architectural changes with other compression techniques, such as the cross-layer parameter sharing used in ALBERT, to push efficiency even further.
  • Hardware Specifics: While BANDD avoids specialized hardware requirements, the actual speedups may vary depending on how specific deep learning frameworks and hardware backends handle normalization vs. softmax.

Summary updated

BibTeX
@misc{sridhar2022trimbert,
  author = {Sharath Nittur Sridhar and Anthony Sarah and Sairam Sundaresan},
  title = {{TrimBERT: Tailoring BERT for Trade-offs}},
  year = {2022},
  eprint = {2202.12411},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2202.12411},
}
Figure