FP8-BERT: Post-Training Quantization for Transformer

Jianwei Li, Tianchi Zhang, Ian En-Hsu Yen, Dongkuan XuarXiv 2023

Abstract

Transformer-based models, such as BERT, have been widely applied in a wide range of natural language processing tasks. However, one inevitable side effect is that they require massive memory storage and inference cost when deployed in production. Quantization is one of the popularized ways to alleviate the cost. However, the previous 8-bit quantization strategy based on INT8 data format either suffers from the degradation of accuracy in a Post-Training Quantization (PTQ) fashion or requires an expensive Quantization-Aware Training (QAT) process. Recently, a new numeric format FP8 (i.e. floating-point of 8-bits) has been proposed and supported in commercial AI computing platforms such as H100. In this paper, we empirically validate the effectiveness of FP8 as a way to do Post-Training Quantization without significant loss of accuracy, with a simple calibration and format conversion process. We adopt the FP8 standard proposed by NVIDIA Corp. (2022) in our extensive experiments of BERT variants on GLUE and SQuAD v1.1 datasets, and show that PTQ with FP8 can significantly improve the accuracy upon that with INT8, to the extent of the full-precision model.

Introduction

Deploying large-scale Transformer models like BERT or GPT-3 presents significant challenges due to their massive memory and inference requirements. Quantization—mapping high-precision 32-bit numbers to lower-precision 8-bit formats—is a common strategy to reduce these costs. However, practitioners face a difficult trade-off: Post-Training Quantization (PTQ) is fast but often degrades accuracy significantly in the standard INT8 format, while Quantization-Aware Training (QAT) maintains accuracy but requires expensive retraining. This paper explores the use of FP8 as a solution to bridge this gap.

The Outlier Problem and the FP8 Advantage

The primary reason INT8 PTQ fails in Transformers is the presence of "outliers"—specific values in the neural network that are significantly larger than the rest. Because INT8 is a uniform format with equal spacing between numbers, these outliers force the quantization process to use a wide range, which sacrifices precision for the majority of the data.

Comparison of INT8 uniform vs FP8 non-uniform quantization.

FP8 (8-bit floating point) offers a natural solution. As a floating-point format, it is inherently non-uniform, providing higher precision near zero and a wider dynamic range. As shown in the comparison above, this allows it to capture outliers without sacrificing the resolution of smaller values, making it far more "friendly" to the non-uniform distributions found in Transformer weights and activations.

Methodology

The researchers propose using FP8 as a drop-in replacement for INT8 in the PTQ pipeline. Key technical components include:

  • FP8 Formats: Utilizing the E4M3 encoding (1 sign bit, 4 exponent bits, and 3 mantissa bits) for an optimal balance between range and precision.
  • Symmetric Static Quantization: Mapping FP32 values into the FP8 range using a simple scaling factor based on maximum absolute values (min/max calibration).
  • Mixed-Precision Strategy: To maintain stability, sensitive non-linear operators (like Softmax, GeLU, and LayerNorm) are kept in 16-bit (BF16), while the heavy matrix multiplications (GEMM) are quantized to FP8.

Experimental Results

The effectiveness of FP8 PTQ was evaluated on standard NLP benchmarks, including GLUE and SQuAD v1.1. The results demonstrate that FP8 effectively "rescues" the accuracy lost by INT8.

Performance of BERT variants on GLUE benchmark tasks.

On the GLUE benchmark, INT8 PTQ caused BERT-base accuracy to crater (e.g., MNLI dropped from 84.58% to 34.39%). In contrast, FP8 PTQ achieved 84.63%, matching or slightly exceeding the full-precision (FP32) baseline. Similar trends were observed in SQuAD v1.1, where BERT-large INT8 was essentially non-functional (F1 score of 6.4), while FP8 PTQ maintained a robust 88.9 F1 score, very close to the original 90.9.

Conclusion

FP8-BERT demonstrates that switching to 8-bit floating-point formats allows for high-accuracy Post-Training Quantization without the need for expensive retraining. While currently dependent on modern hardware like NVIDIA’s H100, this approach provides a clear path toward making massive Transformer models more efficient and deployable in production environments. Future work may explore extending these findings to larger generative models like Llama or GPT-style architectures.

Summary updated

BibTeX
@misc{li2023fp,
  author = {Jianwei Li and Tianchi Zhang and Ian En-Hsu Yen and Dongkuan Xu},
  title = {{FP8-BERT: Post-Training Quantization for Transformer}},
  year = {2023},
  eprint = {2312.05725},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.05725},
}
Figure