Abstract
Vision Transformers (ViT) typically follow a "pre-LN" architecture, but the initial patch embedding layer often remains a source of optimization instability. We introduce Dual PatchNorm (DPN), a deceptively simple modification that applies Layer Normalization both before and after the patch embedding. This trivial, two-line code change effectively "tames" the large gradients typically found at the model's input stem, leading to consistent performance gains across image classification, contrastive learning, and semantic segmentation.
The Optimization Bottleneck in ViT Stems
Since the inception of the Vision Transformer, researchers have largely adhered to the "pre-LN" recipe inherited from natural language processing, placing Layer Normalization (LN) before self-attention and MLP blocks. However, the "stem" of the ViT—the patch embedding layer that transforms raw pixels into tokens—has remained relatively unexamined.
Researchers identified a specific instability: the patch embedding layer often exhibits disproportionately large gradient norms compared to subsequent Transformer blocks. This suggests that the transition from raw pixels to high-dimensional embeddings is a significant optimization bottleneck. While previous attempts to stabilize ViT training involved complex additions like convolutional stems or intricate normalization schemes (e.g., NormFormer), Dual PatchNorm asks if a much simpler modification can achieve superior results.
Methods: Implementation and Design
Dual PatchNorm (DPN) modifies the standard patch embedding (PE) projection by wrapping it in two LayerNorms. In a standard ViT, the embedding is a simple linear projection: $x = PE(x)$. DPN changes this to:
$$x = LN(PE(LN(x)))$$
- Before: A LayerNorm is applied directly to the raw pixel values of each patch.
- After: A second LayerNorm is applied to the resulting embeddings before they enter the Transformer blocks.
An exhaustive search of other LayerNorm placements within the Transformer blocks (such as post-LN or pre+post-LN) revealed that the standard "pre-LN" configuration is already near-optimal. The primary performance gains are uniquely found by targeting the patch embedding layer.
Key Insights: Taming the Gradients
The most significant technical insight is that DPN drastically reduces the gradient norm of the embedding layer. By normalizing both the inputs and outputs of the projection, the "spike" in gradients at the start of the network is smoothed out, leading to more stable and efficient training.
Beyond gradient stability, DPN appears to facilitate learned spatial priors. Visualizations of the learnable scale parameters ($\gamma$) in the first LayerNorm show the model automatically upweights pixels at the center and corners of each patch. This suggests that DPN allows the model to learn a useful spatial filter at the input level, which may contribute to its robustness.
Empirical Performance and Scaling
The researchers evaluated DPN across a massive range of scales, from ImageNet-1k to the internal JFT-4B dataset, and across diverse tasks including the Visual Task Adaptation Benchmark (VTAB) and semantic segmentation on ADE-20K.
- Consistent Accuracy Gains: On ImageNet-1k, DPN provided an average accuracy boost of 1.4% across five different ViT sizes. For the S/32 model, accuracy improved from 72.1% to 74.0%.
- Robust Scaling: Benefits persisted when training on massive datasets like JFT-4B and at higher resolutions (384x384), where it improved the B/16 model by 0.6%.
- Downstream Success: On VTAB, DPN-equipped models significantly outperformed baselines on "Natural" and "Structured" image tasks. In contrastive learning (LiT), DPN improved zero-shot ImageNet accuracy, particularly under shorter training schedules.
Conclusion
Dual PatchNorm demonstrates that significant architectural improvements do not always require complex additions. By addressing the gradient instability at the ViT stem with a simple two-line fix, DPN provides a robust and efficient performance boost that scales across datasets and tasks. While the gains can become marginal in extremely long training regimes, DPN represents a "never hurts" modification for modern vision architectures.
Summary updated