Home

Awesome

PTQ4ViT

Post-Training Quantization Framework for Vision Transformers. We use the twin uniform quantization method to reduce the quantization error on these activation values. And we use a Hessian guided metric to evaluate different scaling factors, which improves the accuracy of calibration with a small cost. The quantized vision transformers (ViT, DeiT, and Swin) achieve near-lossless prediction accuracy (less than 0.5% drop at 8-bit quantization) on the ImageNet classification task. Please read the paper for details.

Updates

19/07/2022 Add discussion on Base PTQ, and provide more ablation study results.

Number of Calibration Images

ModelW8A8 #ims=32W6A6 #ims=32W8A8 #ims=128W6A6 #ims=128
ViT-S/224/3275.5871.9175.5472.29
ViT-S/22481.0078.6380.9978.44
ViT-B/22484.2581.6584.2781.84
ViT-B/38485.8383.3585.8183.84
DeiT-S/22479.4776.2879.4176.51
DeiT-B/22481.4880.2581.5480.30
DeiT-B/38482.9781.5583.0181.67
Swin-T/22481.2580.4781.2780.30
Swin-S/22483.1182.3883.1582.38
Swin-B/22485.1584.0185.1784.15
Swin-B/38486.3985.3986.3685.45
ModelTime #ims=32Time #ims=128
ViT-S/224/322 min5 min
ViT-S/2243 min7 min
ViT-B/2244 min13 min
ViT-B/38412 min43 min
DeiT-S/2243 min7 min
DeiT-B/2244 min16 min
DeiT-B/38414 min52 min
Swin-T/2243 min9 min
Swin-S/2248 min17 min
Swin-B/22410 min23 min
Swin-B/38425 min69 min

One of the targets of PTQ4ViT is to quickly quantize a vision transformer. We have proposed to pre-compute the output and gradient of each layer and compute the influence of scaling factor candidates in batches to reduce the quantization time. As demonstrated in the second table, PTQ4ViT can quantize most vision transformers in several minutes using 32 calibration images. Using 128 calibration images significantly increases the quantization time.
We observe the Top-1 accuracy varies slightly in the first table, demonstrating PTQ4ViT is not very sensitive to the number of calibration images.

Base PTQ

Base PTQ is a simple quantization strategy and serves as a benchmark for our experiments. Like PTQ4ViT, we quantize all weights and inputs for fully-connect layers (including the first projection layer and the last prediction layer), as well as all input matrices of matrix multiplication operations. For fully-connected layers, we use layerwise scaling factors $\Delta_W$ for weight quantization and $\Delta_X$ for input quantization; while for matrix multiplication operations, we use $\Delta_A$ and $\Delta_B$ for A's quantization and B's quantization respectively.

To get the best scaling factors, we apply a linear grid search on the search space. The same as EasyQuantand Liu et al., we take hyper-parameters $\alpha=0.5$, $\beta = 1.2$, one search round and use cosine distance as the metric. Note that in PTQ4ViT, we change the hyper-parameters to $\alpha=0$, $\beta = 1.2$ and three search rounds, which slightly improves the performance.

It should be noticed that Base PTQ adopts a parallel quantization paradigm, which makes it essentially different from sequential quantization paradigms such as EasyQuant. In sequential quantization, the input data of the current quantizing layer is generated with all previous layers quantizing weights and activations. While in parallel quantization, the input data of the current quantizing layer is simply the raw output of the previous layer.

In practice, we found sequential quantization on vision transformers suffers from significant accuracy degradation on small calibration datasets. While parallel quantization shows robustness on small calibration datasets. Therefore, we choose parallel quantization for both Base PTQ and PTQ4ViT.

More Ablation Study

We supply more ablation studies for the hyper-parameters. It is enough to set the number of quantization intervals $\ge$ 20 (accuracy change $< 0.3%$). It is enough to set the upper bound of m $\ge$ 15 (no accuracy change). The best settings of alpha and beta vary from different layers. It is appropriate to set $\alpha=0$ and $\beta=1/2^{k-1}$, which has little impact on search efficiency. We observe that search rounds has little impact on the prediction accuracy (accuracy change $<$ 0.05% when search rounds $>1$).

We randomly take 32 calibration images to quantize different models 20 times and we observe the fluctuation is not significant. The mean/std of accuracies are: ViT-S/32 $75.55%/0.055%$ , ViT-S $80.96%/0.046%$, ViT-B $84.12%/0.068%$, DeiT-S $79.45%/0.094%$ , and Swin-S $83.11%/0.035%$.

15/01/2022 Add saved quantized models with PTQ4ViT.

modellink
ViT-S/224/32Google
ViT-S/224Google
ViT-B/224Google
ViT-B/384Google
DeiT-S/224Google
DeiT-B/224Google
DeiT-B/384Google
Swin-T/224Google
Swin-S/224Google
Swin-B/224Google
Swin-B/384Google

10/12/2021 Add utils/integer.py, you can now:

  1. convert calibrated fp32 model into int8
  2. register pre-forward hook in the model, and fetch activation in int8. (We use uint8 to store results of twin quantization, please refer to the paper to see the bits' layout).

Install

Requirement

Datasets

To run example testing, you should put your ImageNet2012 dataset in path /datasets/imagenet.

We use ViTImageNetLoaderGenerator in utils/datasets.py to initialize our DataLoader. If your Imagenet datasets are stored elsewhere, you'll need to manually pass its root as an argument when instantiating a ViTImageNetLoaderGenerator.

Usage

1. Run example quantization

To test on all models with BasePTQ/PTQ4ViT, run

python example/test_all.py

To run ablation testing, run

python example/test_ablation.py

You can run the testing scripts with multiple GPUs. For example, calling

python example/test_all.py --multigpu --n_gpu 6

will use 6 gpus to run the test.

2. Download quantized model checkpoints

(Coming soon)

Results

Results of BasePTQ

modeloriginalw8a8w6a6
ViT-S/224/3275.9973.6160.144
ViT-S/22481.3980.46870.244
ViT-B/22484.5483.89675.668
ViT-B/38486.0085.35246.886
DeiT-S/22479.8077.65472.268
DeiT-B/22481.8080.94678.786
DeiT-B/38483.1182.3368.442
Swin-T/22481.3980.96278.456
Swin-S/22483.2382.75881.742
Swin-B/22485.2784.79283.354
Swin-B/38486.4486.16885.226

Results of PTQ4ViT

modeloriginalw8a8w6a6
ViT-S/224/3275.9975.58271.908
ViT-S/22481.3981.00278.63
ViT-B/22484.5484.2581.65
ViT-B/38486.0085.82883.348
DeiT-S/22479.8079.47476.282
DeiT-B/22481.8081.48280.25
DeiT-B/38483.1182.97481.55
Swin-T/22481.3981.24680.47
Swin-S/22483.2383.10682.38
Swin-B/22485.2785.14684.012
Swin-B/38486.4486.39485.388

Results of Ablation

Hessian GuidedSoftmax TwinGELU TwinW8A8W6A6
80.4770.24
80.9377.20
81.1178.57
80.8476.93
79.2574.07
81.0078.63
Hessian GuidedSoftmax TwinGELU TwinW8A8W6A6
83.9075.67
83.9779.90
84.0780.76
84.1080.82
83.4078.86
84.2581.65
Hessian GuidedSoftmax TwinGELU TwinW8A8W6A6
85.3546.89
85.4279.99
85.6782.01
85.6082.21
84.3580.86
85.8983.19

Citation

@article{PTQ4ViT_arixv2022,
    title={PTQ4ViT: Post-Training Quantization Framework for Vision Transformers},
    author={Zhihang Yuan, Chenhao Xue, Yiqi Chen, Qiang Wu, Guangyu Sun},
    journal={arXiv preprint arXiv:2111.12293},
    year={2022},
}