FQ-ViT: Post-Training Quantization for Fully Quantized Vision Transformer

Yang Lin, Tianyu Zhang, Peiqin Sun, Zheng Li, Shuchang Zhou

TL;DR

This paper introduces FQ-ViT, a novel post-training quantization method that enables fully quantized Vision Transformers (ViTs) with minimal accuracy loss. By addressing critical issues in LayerNorm and attention map processing, FQ-ViT achieves near-lossless performance, paving the way for efficient deployment of ViTs on resource-constrained devices.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have revolutionized computer vision, but their large parameter counts and computational demands hinder deployment on edge devices. Quantization, a technique to reduce model size and speed up inference by using lower-precision numbers, has been successful for Convolutional Neural Networks (CNNs). However, applying existing quantization methods to ViTs, especially aiming for full quantization (where all components, including LayerNorm and Softmax, are quantized), leads to significant accuracy degradation. This paper identifies the root causes of this degradation in ViTs and proposes tailored solutions to achieve effective full quantization.

Methods

The authors tackle two main challenges in fully quantized ViTs:

  • LayerNorm Quantization: They observe severe inter-channel variations in LayerNorm inputs, which traditional layer-wise quantization methods struggle to handle, leading to large errors. To address this, they propose Power-of-Two Factor (PTF). PTF is a systematic method that uses a power-of-two factor for each channel, allowing for more accurate quantization of LayerNorm inputs without the need for complex group-wise or channel-wise quantization parameters. This method leverages bit-shift operations for efficiency.
  • Attention Map Quantization: ViTs' attention maps exhibit an extreme non-uniform distribution, with most values close to zero and a few close to one. Quantizing these maps to very low bit-widths (e.g., 4-bit) using standard methods causes substantial accuracy drops. The authors introduce Log-Int-Softmax (LIS), which uses a log2-based quantization scheme. LIS allocates more quantization bins to the small values, preserving more information. Crucially, it enables the replacement of computationally expensive matrix multiplications in the attention mechanism with efficient bit-shift operations, further reducing inference costs and allowing for 4-bit attention maps with minimal performance impact.

These methods are applied in a post-training quantization (PTQ) framework, meaning no retraining is required.

Datasets

The proposed FQ-ViT method is evaluated on standard computer vision benchmarks:

  • ImageNet: Used for image classification tasks.
  • COCO: Used for object detection tasks.

The paper does not introduce new datasets.

Key Insights and Ideas

  • Inter-channel variation in LayerNorm is a critical bottleneck for ViT quantization: Unlike CNNs, ViTs' LayerNorm layers exhibit much larger differences in activation ranges across channels, making standard quantization ineffective.
  • Power-of-Two Factor (PTF) offers an efficient solution for LayerNorm: By using channel-specific power-of-two scaling factors, PTF effectively mitigates quantization errors without increasing computational complexity significantly.
  • Log-Int-Softmax (LIS) is key to low-bit attention map quantization: The non-uniform distribution of attention maps is better handled by a log-based quantization scheme, which preserves relative rankings and allows for aggressive 4-bit compression.
  • Bit-shift operations are a powerful tool for accelerating quantized ViTs: Replacing multiplications with bit-shifts in both LayerNorm (via PTF) and attention mechanisms (via LIS) is crucial for achieving significant inference speedups.
  • Full quantization of ViTs is achievable with minimal accuracy loss: The paper demonstrates that by addressing these specific ViT components, it's possible to quantize all modules (weights, activations, and attention maps) to low bit-widths without substantial performance degradation.

Key Results

  • ImageNet Classification: FQ-ViT achieves 84.89% top-1 accuracy on ViT-L, which is a near-lossless degradation (~1%) compared to full precision. It also achieves 81.20% on DeiT-B with 8-bit quantization and 80.85% with 4-bit attention maps.
  • COCO Object Detection: FQ-ViT achieves 50.8 mAP on Cascade Mask R-CNN (Swin-S) with 8-bit weights/activations and 4-bit attention maps, outperforming previous methods.
  • Efficiency: The proposed methods enable integer-only inference, significantly reducing hardware resource requirements and inference latency. The use of bit-shift operations further accelerates computation.
  • First to achieve near-lossless full quantization: The paper claims to be the first to achieve such a low accuracy degradation on fully quantized vision transformers.

Limitations and Future Work

  • Hyperparameter K: The PTF method uses a hyperparameter K, which is set to 3 by default based on empirical evaluation. While this value works well across models, further research could explore adaptive K selection.
  • Focus on PTQ: The paper focuses on post-training quantization. While this avoids retraining costs, Quantization-Aware Training (QAT) might yield even better results, though at a higher computational expense.
  • Future Work: The authors suggest that FQ-ViT provides a strong baseline for future research into even lower bit-width quantization of vision transformers, aiming to further boost their real-world adoption. They also highlight that their quantization techniques are orthogonal to pruning and architecture adaptation, suggesting potential for combined improvements.

Summary updated

BibTeX
@inproceedings{lin2022fq,
  author = {Yang Lin and Tianyu Zhang and Peiqin Sun and Zheng Li and Shuchang Zhou},
  title = {{FQ-ViT: Post-Training Quantization for Fully Quantized Vision Transformer}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2022},
  doi = {10.24963/ijcai.2022/164},
}
Figure