Replacing softmax with ReLU in Vision Transformers

Mitchell Wortsman, Jaehoon Lee, Justin Gilmer, Simon Kornblith

TL;DR

Replacing the standard softmax operation in Vision Transformers (ViTs) with a simple ReLU activation typically tanks model accuracy. This paper demonstrates that simply dividing the ReLU output by the sequence length ($L$) completely mitigates this degradation, allowing pointwise ReLU-attention to match the scaling performance of softmax while unlocking superior hardware parallelization.

What Problem Does This Paper Address?

The attention mechanism is the core engine of the Transformer architecture, but it relies heavily on the softmax function to normalize attention scores into a probability distribution. Softmax is computationally expensive because it requires computing exponentials and a global sum across the entire sequence length. This global normalization creates a synchronization bottleneck, requiring frequent and costly "gather" operations across hardware accelerators.

While researchers have tried replacing softmax with pointwise activations like ReLU to improve parallelization, doing so has historically led to severe drops in model accuracy. This paper investigates why ReLU fails in Vision Transformers and provides a remarkably simple fix.

Methods

The authors propose ReLU-attention, a pointwise alternative to softmax. Instead of globally normalizing the attention matrix, they apply a standard ReLU activation to the scaled query-key dot products and divide the result by the sequence length ($L$).

Mathematically, the attention weights $\alpha_{ij}$ are computed as: $$\alpha_{ij} = \frac{1}{L} \text{ReLU}\left(\frac{q_i^\top k_j}{\sqrt{d}}\right)$$

They also generalize this approach to explore a broader family of scaled pointwise attention functions defined as $\phi = L^{-\alpha} h$, where $h$ is an activation function (such as ReLU, squared ReLU, Gelu, or Sigmoid) and $\alpha$ is a scaling exponent.

The core theoretical intuition is that softmax naturally ensures that the expected value of an attention weight at initialization is $O(1/L)$. By explicitly dividing the pointwise activation by $L$ (setting $\alpha = 1$), they preserve this initialization property, preventing internal activations from exploding or vanishing as sequence lengths change.

Datasets

The models are trained and evaluated using standard vision benchmarks:

  • ImageNet-21k: Used for training models for 30 epochs.
  • ImageNet-1k: Used for training models from scratch for 300 epochs, and for evaluating the ImageNet-21k models (via zero-shot top-class mapping without fine-tuning).
  • Downstream Transfer Benchmarks: Evaluated using a 10-shot linear probe averaged over three seeds across 8 datasets: Caltech Birds, Caltech-101, Stanford Cars, CIFAR-100, DTD, ColHsit, Pets, and UC Merced.

No new datasets were introduced in this work.

Key Insights and Ideas

  • The Power of $1/L$ Scaling: The critical missing ingredient in prior attempts to replace softmax wasn't the choice of activation function, but the lack of sequence-length scaling. Scaling by $L^{-1}$ keeps the magnitude of attention weights stable at initialization.
  • Activation Flexibility: Once properly scaled by $1/L$, the exact choice of pointwise non-linearity matters surprisingly little. ReLU, squared ReLU, Gelu, and Softplus all perform similarly well. ReLU is preferred simply because it is computationally efficient.
  • Parallelization Gains: Because ReLU-attention is pointwise, it eliminates the need to sum across the entire sequence dimension before computing the final attention output. This allows for cleaner parallelization over the sequence length dimension with fewer cross-core communication operations.

Key Results

  • Comparable Scaling: ReLU-attention closely tracks or matches the compute-vs-accuracy scaling curve of traditional softmax-attention across various ViT sizes (from Small to Large), as shown in Figure 1.
Figure 1: Replacing softmax with relu/seqlen approaches or matches the scaling performance of traditional attention for vision transformers [10] with qk-layernorm [8]. This figure displays results for small to large vision transformers trained on ImageNet-21k [9] for 30 epochs. We report ImageNet-1k accuracy for ImageNet-21k models by taking the top class among those that are in ImageNet-1k, without fine-tuning. Attention with ReLU can be parallelized over the sequence length dimension with less gather operations than softmax attention.
  • Optimal Alpha: Across different models and datasets, the optimal exponent $\alpha$ for sequence length scaling consistently clusters around $1.0$ (Figure 2). Dropping the scaling entirely ($\alpha = 0$) causes a massive drop in performance.
Performance of various pointwise activations across different scaling exponents alpha.
  • Robustness to Architecture Tweaks: The authors ablated other components and found that adding a gated attention unit or removing qk-layernorm did not alter the necessity or benefit of the $1/L$ scaling factor.

Limitations and Future Work

  • Empirical vs. Theoretical Understanding: While the $O(1/L)$ initialization argument provides good intuition, a rigorous theoretical understanding of why $L^{-1}$ scaling works so well throughout the entire training process remains an open question.
  • Scale Limits: The experiments scale up to ViT-Large for 30 epochs on ImageNet-21k. Whether these findings hold for ultra-large foundation models (e.g., ViT-Giant or massive LLMs) is still untested.
  • Fixed vs. Learned Scaling: The paper uses a fixed $L^{-1}$ factor; future work could explore whether this scaling factor can be dynamically learned during training.

Summary updated

BibTeX
@misc{wortsman2023replacing,
  author = {Mitchell Wortsman and Jaehoon Lee and Justin Gilmer and Simon Kornblith},
  title = {{Replacing softmax with ReLU in Vision Transformers}},
  year = {2023},
  eprint = {2309.08586},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.08586},
}
Figure