Mesa: A Memory-saving Training Framework for Transformers

Zizheng Pan, Peng Chen, Haoyu He, Jing Liu, Jianfei Cai, Bohan ZhuangarXiv 2021

Abstract

Training state-of-the-art Transformer models is often constrained by the high memory requirements of intermediate activations during backpropagation. We present Mesa, a memory-saving training framework that reduces the activation memory footprint by up to 50% without sacrificing model accuracy. Mesa achieves this through 8-bit Activation Compressed Training (ACT) combined with a novel head-wise quantization strategy that accounts for the statistical heterogeneity across attention heads. Our results demonstrate that Mesa allows for training significantly larger models or larger batch sizes on standard consumer hardware, often providing a slight regularization benefit that improves final accuracy.

Introduction

The "Transformer revolution" has significantly advanced NLP and Computer Vision, but at a massive computational cost. The primary bottleneck in training these models is not the weights themselves, but the intermediate activations. To calculate gradients during the backward pass, the GPU must store the output of every layer from the forward pass. For long sequences or high-resolution images, this "activation bottleneck" frequently leads to Out-of-Memory (OOM) errors, restricting large-scale model training to industrial clusters.

Methodology: Activation Compressed Training

Mesa is built upon the concept of Activation Compressed Training (ACT). During the forward pass, activations are computed in full precision but stored in a compressed 8-bit format. These are then dequantized during backpropagation to estimate gradients. Mesa introduces three primary innovations to optimize this process for Transformers:

Head-wise Activation Quantization

Standard quantization methods apply a uniform scale to an entire layer. However, empirical analysis reveals that different "heads" in Multi-Head Self-Attention (MSA) layers exhibit wildly different statistical distributions. As shown in Figure 1, the value ranges for different heads are highly heterogeneous.

Figure 1: Statistical distribution of values across different attention heads, illustrating the need for head-wise quantization.

Mesa addresses this by quantizing each head independently, which significantly reduces approximation errors compared to layer-wise approaches.

Efficient Parameter Estimation and Kernels

To maintain training efficiency, Mesa uses an Exponential Moving Average (EMA) to learn quantization parameters (like clipping ranges) over time, rather than recalculating them for every sample. Additionally, custom CUDA kernels were developed to minimize the computational overhead of the quantization and dequantization cycles.

Experimental Results

We evaluated Mesa across major benchmarks including ImageNet, CIFAR-100, and ADE20K using models like DeiT and Swin Transformer.

Memory and Accuracy Trade-offs

Mesa consistently reduces training memory by 43% to 55%. Notably, accuracy is preserved or even slightly improved. For instance, on ImageNet, DeiT-S with Mesa achieved 80.0% Top-1 accuracy, surpassing the 79.8% full-precision baseline while using less than half the memory. Figure 2 illustrates the Pareto frontier of accuracy versus memory consumption for various models.

Figure 2: Top-1 Accuracy vs. Training Memory for various Transformer models. Mesa + AMP significantly reduces memory usage compared to standard AMP while maintaining or improving accuracy.

Scaling Capabilities

By "re-investing" the memory savings, Mesa enables training configurations previously impossible on standard hardware. Researchers successfully trained a DeiT-B model that was 3.3x deeper (40 blocks vs. 12) on a single 32GB V100 GPU. Furthermore, in downstream tasks like semantic segmentation on ADE20K, Swin-Ti with Mesa outperformed the baseline by 0.7% mIoU.

Discussion and Conclusion

Mesa demonstrates that 8-bit activation compression is a viable strategy for scaling Transformer training. The slight noise introduced by quantization may even act as a form of stochastic regularization, aiding generalization. While Mesa introduces a trade-off in training throughput due to the extra quantization steps, the ability to train larger models on limited hardware provides a significant "memory dividend" for the research community. Future work will focus on further optimizing CUDA kernels to bridge the speed gap and exploring Mesa's application to Large Language Models (LLMs).

Summary updated

BibTeX
@misc{pan2021mesa,
  author = {Zizheng Pan and Peng Chen and Haoyu He and Jing Liu and Jianfei Cai and Bohan Zhuang},
  title = {{Mesa: A Memory-saving Training Framework for Transformers}},
  year = {2021},
  eprint = {2111.11124},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2111.11124},
}
Figure