Understanding the Failure of Batch Normalization for Transformers in NLP

Jiaxi Wang, Ji Wu, Lei Huang

In the world of deep learning, there is a curious divide: if you are building a Computer Vision (CV) model, you almost certainly use Batch Normalization (BN). If you are building a Natural Language Processing (NLP) model, you almost certainly use Layer Normalization (LN). For years, researchers have wondered why BN—despite its massive success in CV—consistently fails to deliver the goods in Transformer-based NLP tasks.

A paper from researchers at Tsinghua University and Beihang University finally puts a finger on the culprit: the Training Inference Discrepancy (TID).

TL;DR

The performance gap between Batch Normalization and Layer Normalization in NLP is primarily caused by a mismatch between how statistics are calculated during training versus inference. By introducing Regularized BN (RBN)—which penalizes this discrepancy during training—the authors show that BN can finally match or even outperform LN across a wide variety of NLP tasks.

What Problem Does This Paper Address?

Normalization layers are the "secret sauce" that makes deep networks stable and fast to train.

  • Batch Normalization (BN) scales activations based on the statistics of the current mini-batch. During testing, it uses a "running average" of those statistics.
  • Layer Normalization (LN) scales activations based on the statistics of the individual layer for a single data point, making it consistent across training and testing.

In NLP, BN has historically performed poorly. Previous theories suggested this was due to unstable training or "outlier" values in text data. However, this paper observes a strange phenomenon: BN often achieves lower training loss than LN, yet it performs significantly worse on the validation set.

Training vs Validation Performance for BN and LN

This suggests the problem isn't the training itself, but how the model generalizes from training (using batch stats) to inference (using population stats).

Methods

The authors introduce two major contributions to address this:

  1. Training Inference Discrepancy (TID): A quantitative metric to measure the "gap" between the mean/variance of a specific mini-batch and the global population statistics the model uses during inference.
  2. Regularized Batch Normalization (RBN): To fix the failure of BN, the authors add a simple regularization term to the loss function. This term penalizes the model when the mini-batch statistics ($\mu_B, \sigma_B$) stray too far from the running population statistics ($\mu, \sigma$).

Essentially, RBN forces the model to ensure that the "batch view" it sees during training is consistent with the "global view" it will use during testing.

Key Insights and Ideas

  • TID is the "Smoking Gun": In Computer Vision (where BN works), TID is very low (around 1%). In NLP Transformers, TID is massive (often 10-30%). The authors found a near-perfect correlation: when TID is high, BN fails; when TID is low, BN wins.
  • BN is a Better Optimizer: Surprisingly, the paper shows that BN actually provides better "layer-wise conditioning" than LN. This means that if you can solve the TID problem, BN is theoretically capable of training faster and reaching better optima than LN.
  • The Last Layer Matters Most: The discrepancy in the final layers of the Transformer has the most devastating impact on model performance, as these errors propagate directly into the final predictions.
Variance Deviation across BN layers
  • Zero Extra Cost: Unlike some other proposed fixes for BN, RBN introduces no extra computational overhead during inference. It’s just a standard BN layer once the model is deployed.

Key Results

The authors conducted an exhaustive validation across several domains, including Neural Machine Translation (NMT), Language Modeling (LM), Named Entity Recognition (NER), and Text Classification. RBN proved to be a formidable competitor to the industry-standard Layer Norm:

  • 17 out of 20: RBN outperformed or matched LN in 17 of the 20 experimental settings tested.
  • Language Modeling Win: On the WikiText-103 dataset, RBN achieved a perplexity (PPL) of 17.1, significantly better than LN's 20.9.
  • Translation: On IWSLT14, RBN matched the performance of LN (35.5 BLEU) while significantly improving upon standard BN (34.0 BLEU).
  • Consistency: RBN consistently reduced the variance TID from over 30% in some tasks to under 10%, directly leading to the observed performance gains.

Limitations and Future Work

While RBN is a major step forward, the authors note a few lingering challenges:

  • The Diversity Problem: In extremely high-diversity datasets (like WMT16), the TID is so explosive that even RBN struggles to fully suppress it. The inherent "noise" in such large-scale translation data is a hurdle yet to be fully cleared.
  • Lack of Theoretical Proof: The study is primarily empirical. While the correlation between TID and performance is strong, a formal mathematical proof of why Transformers generate such high TID compared to CNNs remains an open question.
  • Hybrid Approaches: The authors suggest that future work could explore combining the optimization benefits of RBN with the stability of LN (e.g., using different norms for different parts of the network).

Summary updated

BibTeX
@inproceedings{wang2022understanding,
  author = {Jiaxi Wang and Ji Wu and Lei Huang},
  title = {{Understanding the Failure of Batch Normalization for Transformers in NLP}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=53e9VfPUCg},
}
Figure