PTQ4ViT: Post-training quantization for vision transformers with twin uniform quantization

Zhihang Yuan, Chenhao Xue, Yiqi Chen, Qiang Wu, Guangyu Sun
11/24/2021
cs.CV
transformer-attentionquantization-pruning

Official code: hahnyuan/PTQ4ViT (246 stars, Python, last commit 2022-07-19).

Abstract

Quantization is one of the most effective methods to compress neural networks, which has achieved great success on convolutional neural networks (CNNs). Recently, vision transformers have demonstrated great potential in computer vision.

Show full abstract

However, previous post-training quantization methods performed not well on vision transformer, resulting in more than 1% accuracy drop even in 8-bit quantization. Therefore, we analyze the problems of quantization on vision transformers. We observe the distributions of activation values after softmax and GELU functions are quite different from the Gaussian distribution. We also observe that common quantization metrics, such as MSE and cosine distance, are inaccurate to determine the optimal scaling factor. In this paper, we propose the twin uniform quantization method to reduce the quantization error on these activation values. And we propose to use a Hessian guided metric to evaluate different scaling factors, which improves the accuracy of calibration at a small cost. To enable the fast quantization of vision transformers, we develop an efficient framework, PTQ4ViT. Experiments show the quantized vision transformers achieve near-lossless prediction accuracy (less than 0.5% drop at 8-bit quantization) on the ImageNet classification task.

View on arXivView PDF

Code Implementations(1)

hahnyuan/PTQ4ViTOfficial100%

Post-Training Quantization for Vision transformers.

24635PythonNov 24, 20214 years ago

Similar Papers

Cite this paper

@article{yuan2021ptqvit,
  title  = {PTQ4ViT: Post-training quantization for vision transformers with twin uniform quantization},
  author = {Zhihang Yuan and Chenhao Xue and Yiqi Chen and Qiang Wu and Guangyu Sun},
  year   = {2021},
  eprint = {2111.12293},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2111.12293}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2111.12293.svg)](https://codeofpaper.com/paper/2111.12293)

Discussion