Softmax Output Approximation for Activation Memory-Efficient Training of Attention-based Networks

Changhyeon Lee, Seulki Lee

Training large Transformer models is heavily bottlenecked by the memory required to store intermediate activations for backpropagation. This paper introduces a clever architectural trick: store only the highest and lowest values of the attention softmax output during the forward pass, and approximate the rest during the backward pass using an exponential distribution. This approach reduces the softmax activation memory footprint by up to 84% without hurting—and sometimes even improving—model performance.

The Activation Memory Bottleneck

Attention-based networks (Transformers) are the backbone of modern AI, but their stellar performance comes with massive hardware requirements. During training, a neural network must store intermediate outputs (activations) from the forward pass in memory to compute gradients during the backward pass. As models get deeper and sequence lengths get longer, this activation memory quickly exceeds the memory used by the model parameters themselves.

Memory usage during training before and after the proposed approximation.

A massive culprit in this memory bottleneck is the softmax operation inside the attention module, which computes the normalized attention scores. In standard models like BERT or the classic Transformer, the softmax output can consume over 60% of the entire layer's activation memory. While techniques like gradient checkpointing (re-computing activations on the fly) exist, they significantly slow down training. The authors tackle the challenge of reducing this memory footprint without incurring massive computational slowdowns or altering the model's core architecture.

Methodology: Softmax Output Approximation

Normally, a network stores the entire $N \times N$ softmax output matrix during the forward pass. The authors propose a novel Softmax Output Approximation method that drastically reduces this requirement:

  1. Selective Storing (Forward Pass): Instead of saving the whole softmax array, the method sorts the outputs and stores only a tiny subset: the $m$ highest and $m$ lowest values. The rest of the values are evicted from memory.
  2. Exponential Approximation: To represent the evicted values, the authors fit an exponential distribution function ($s'_k = \alpha e^{-\beta \cdot k}$). They only need to store the two parameters, $\alpha$ and $\beta$, which are derived from the stored highest and lowest values.
  3. Reconstruction (Backward Pass): During backpropagation, the evicted values are mathematically reconstructed using the exponential function, reordered back to their original positions, and combined with the stored extreme values to compute the gradient matrix.
Illustration of the softmax output approximation storing only extreme values.

This approach is highly novel because it is model-agnostic and orthogonal to other efficiency tricks (like FlashAttention or sequence compression). It specifically targets the memory bloat of the softmax step itself.

Key Insights

  • Softmax is naturally exponential: When you sort the output probabilities of a softmax function, they exhibit a steep, exponential-like decay. The authors brilliantly exploit this mathematical quirk, proving that an exponential distribution can mimic the discarded softmax values with incredibly low error.
  • Extreme values matter most for gradients: The authors mathematically demonstrate that keeping the highest and lowest softmax values minimizes the gradient reconstruction error. The "middle" values contribute less to the exact trajectory of the weight updates.
  • Approximation acts as a regularizer: Surprisingly, the tiny errors introduced by approximating the gradient matrix don't hurt the model. Instead, they act as a form of "white noise" during training. Much like dropout, this noise can actually improve the model's ability to generalize, leading to higher final accuracy.

Experimental Results

The authors evaluated their method by fine-tuning and training models from scratch across several standard NLP benchmarks, including Text Classification (AG News, DBpedia), Sentiment Analysis (SST-2, IMDb), and Machine Translation (Multi30K).

  • Massive Memory Savings: The method reduced the activation memory usage of the softmax module by up to 84% (a 6.2x reduction). For example, when scaling the sequence length to 1,000 tokens, a normal softmax required 34.33 GB of memory; the approximated version required only 5.70 GB.
  • Maintained or Improved Accuracy: Despite dropping most of the exact softmax values, the models performed just as well—and sometimes better. On the AG News text classification task, the approximated model achieved a 5.4% higher classification accuracy (jumping from an F1-micro score of 0.9323 to 0.9869) compared to the exact softmax.
  • High Fidelity: The Mean Absolute Error (MAE) of the reconstructed gradient matrix drops to a mere 0.088% as training progresses, proving the exponential fit is highly accurate.

Limitations and Future Work

The authors are transparent about a few practical limitations. Sorting the softmax outputs and reordering them back to their original sequence introduces a computational overhead of $O(n \log n)$ in both time and space.

Furthermore, the current implementation was written in Python/PyTorch. Because standard PyTorch softmax is highly optimized at the hardware level using custom CUDA kernels, the Python-level approximation introduces practical wall-clock overhead that masks some of the theoretical gains. The authors note that their immediate future work involves writing optimized, low-level CUDA kernels for this approximation algorithm to fully realize the speed and memory benefits in production environments.

Summary updated

BibTeX
@inproceedings{lee2023softmax,
  author = {Changhyeon Lee and Seulki Lee},
  title = {{Softmax Output Approximation for Activation Memory-Efficient Training of Attention-based Networks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=oScaeIibRx},
}
Figure