Training Transformers with 4-bit Integers

Haocheng Xi, Changhao Li, Jianfei Chen, Jun Zhu

Abstract

Quantizing activations, weights, and gradients to 4-bit precision is a promising avenue for accelerating neural network training. However, existing 4-bit training methods often require custom numerical formats not supported by contemporary hardware. In this work, we propose a training method for Transformers where all major matrix multiplications are implemented with INT4 arithmetic. To address the challenges of ultra-low precision, we analyze the specific structures of activations and gradients in Transformers. For forward propagation, we introduce a Hadamard quantizer to suppress outliers. For backpropagation, we leverage the structural sparsity of gradients through bit splitting and leverage score sampling (LSS). Our algorithm achieves competitive accuracy across natural language understanding, machine translation, and image classification tasks. Implemented on current-generation GPUs, our prototypical linear operator is up to 2.2x faster than FP16 counterparts, yielding overall training speedups of up to 35.1%.

Introduction

Training massive Transformer models is increasingly constrained by computational costs and energy consumption. While 16-bit (FP16) and 8-bit (INT8/FP8) precision have become industry standards, 4-bit training remains the "holy grail" of efficiency. Historically, two barriers have prevented the adoption of 4-bit training: the significant accuracy degradation caused by numerical outliers and a lack of hardware support for experimental 4-bit formats.

This research addresses both hurdles by introducing a method that utilizes standard INT4 arithmetic, making it compatible with the Tensor Cores found in modern GPUs (e.g., NVIDIA Ampere and Hopper architectures). By combining mathematical transforms with importance sampling, we demonstrate that Transformers can be trained with 4-bit integers while maintaining high accuracy and achieving significant real-world speedups.

Methodology

The proposed method focuses on two distinct strategies to handle the unique numerical distributions found in the forward and backward passes of Transformer models.

1. Hadamard Quantization (HQ) for Forward Propagation

Transformer activations are notorious for "outliers"—specific features with magnitudes significantly larger than the rest of the distribution. In a 4-bit environment, these outliers force a choice: either clip the values (losing critical information) or expand the quantization range (reducing the precision for the majority of the data to near zero).

To solve this, we apply a Hadamard Transform before quantization. This transform uses a block-diagonal matrix of 1s and -1s to "smear" the outlier energy across the entire feature vector. By redistributing the information, the numerical range becomes more uniform and easier to quantize into 4 bits without significant loss. The transform is reversed after the matrix multiplication to restore the original coordinate system, ensuring mathematical consistency.

2. Bit Splitting and Leverage Score Sampling (LSS) for Backpropagation

Gradients in Transformers exhibit "structural sparsity," where a small subset of tokens carries the bulk of the gradient information. Standard 4-bit truncation is too coarse for these sensitive values.

  • Bit Splitting: We represent the gradient as the sum of two 4-bit components (a high-order and low-order component). This effectively provides 8-bit precision while still utilizing 4-bit arithmetic units.
  • Leverage Score Sampling (LSS): To prevent the computational cost from doubling due to bit splitting, we employ LSS—a randomized linear algebra technique. LSS identifies the most informative rows and columns of the gradient matrix. By only computing these high-leverage components, we maintain the accuracy of an 8-bit gradient at the computational cost of a single 4-bit multiplication.

Experimental Results

Accuracy and Stability

The method was evaluated across a diverse suite of benchmarks, including GLUE (Natural Language Understanding), SQuAD (Question Answering), WMT14 (Machine Translation), and ImageNet-1K (Vision Transformers).

On the GLUE benchmark, the HQ+LSS method achieved an average score of 80.81. While slightly below the FP16 baseline of 82.67, it represents a massive improvement over previous 4-bit attempts, which often failed to converge or suffered double-digit accuracy drops. The method proved stable for both fine-tuning and training from scratch.

Hardware Performance

Unlike theoretical simulations, this work provides a prototype implementation using NVIDIA’s CUTLASS library.

  • Operator Speed: The INT4 linear operator is up to 2.2x faster than the FP16 version.
  • End-to-End Speedup: For full model training, the researchers observed speedups of 35.1% for BERT-Large and 26.5% for GPT-2.

Discussion and Future Work

The primary insight of this work is that outliers do not need to be removed; they simply need to be redistributed. By changing the coordinate system via the Hadamard transform, we make the data "quantization-friendly."

However, some limitations remain. The current implementation focuses on linear (fully-connected) layers. While these account for the majority of Transformer computations, non-linear operations like LayerNorm and Softmax are still performed in FP16. Future work will explore extending these techniques to convolutional layers and further optimizing the implementation through operator fusion to minimize memory overhead. Additionally, testing the limits of this method on ultra-large-scale models (e.g., 100B+ parameters) remains an open area for investigation.

Summary updated

BibTeX
@inproceedings{xi2023training,
  author = {Haocheng Xi and Changhao Li and Jianfei Chen and Jun Zhu},
  title = {{Training Transformers with 4-bit Integers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=H9hWlfMT6O},
}
Figure