NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

PTQ4SAM: Post-Training Quantization for Segment Anything

Chengtao Lv, Hong Chen, Jinyang Guo, Yifu Ding, Xianglong Liu · DOI

Abstract

The Segment Anything Model (SAM) has established itself as a foundational tool for computer vision, yet its massive Transformer-based architecture poses significant deployment challenges due to high memory and latency requirements. Standard Post-Training Quantization (PTQ) methods often fail to maintain accuracy on SAM. We present PTQ4SAM, a specialized framework that achieves nearly lossless 6-bit quantization. By addressing bimodal activation bottlenecks and implementing an adaptive granularity scheme for attention mechanisms, PTQ4SAM delivers a 3.9× theoretical speedup and 4.9× memory savings, facilitating the deployment of SAM on resource-constrained edge devices.

Introduction

SAM's ability to segment any object via promptable masks has revolutionized image analysis. However, its computational weight makes it difficult to run on mobile or embedded hardware. While quantization is the standard approach for model compression, SAM exhibits unique "quantization killers" that degrade performance under traditional INT8 or INT6 schemes. PTQ4SAM identifies these bottlenecks and introduces mathematically lossless transformations to restore quantizability.

The Quantization Challenges in SAM

Analysis of SAM's internal activations reveals two primary obstacles to effective quantization:

  1. Bimodal Distributions: Activations following the "Key" linear layers in the attention blocks exhibit two distinct peaks (e.g., at -8 and +8) separated by a "void interval." Standard quantizers attempt to cover the entire range, resulting in significant precision loss in the populated regions.
  2. Diverse Softmax Distributions: SAM utilizes various attention types, including self-attention and two-way cross-attention. The post-Softmax distributions for these types vary significantly, making a uniform quantization strategy ineffective.
Bimodal and post-Softmax activation distributions in SAM, highlighting the "void interval" and distribution diversity across attention types.

Methodology

PTQ4SAM introduces two core innovations to mitigate these issues without requiring expensive Quantization-Aware Training (QAT).

Bimodal Integration (BIG)

The authors observed that while the overall tensor is bimodal, individual channels typically exhibit unimodal behavior (either positive or negative). The Bimodal Integration (BIG) strategy computes a channel-wise "sign factor" ((\gamma)). By flipping the signs of negative channels to positive and simultaneously adjusting the corresponding "Query" weights, the bimodal distribution is merged into a single normal distribution. This transformation is mathematically lossless due to the associative property of matrix multiplication and incurs zero runtime overhead.

Adaptive Granularity Quantization (AGQ)

To handle diverse Softmax outputs, Adaptive Granularity Quantization (AGQ) searches for an optimal power-of-two base ((\tau)) for logarithmic quantization. This allows the model to prioritize either low-value attention scores (fine-grained low values) or high-value scores (fine-grained high values) depending on the specific attention type. The use of power-of-two bases ensures hardware efficiency through bit-shifting operations and small lookup tables (LUTs).

The PTQ4SAM framework illustrating the Bimodal Integration (BIG) sign-flipping process and the Adaptive Granularity Quantization (AGQ) search for optimal bit-shift parameters.

Experimental Results

PTQ4SAM was evaluated on MS-COCO, ADE20K, and DOTA-v1.0 datasets. Key findings include:

  • Near-Lossless 6-bit Performance: At W6A6 (6-bit weights and activations), SAM-L maintains virtually the same mAP as the full-precision (FP32) baseline.
  • Superior 4-bit Accuracy: On 4-bit SAM-L, PTQ4SAM achieves 32.1 mAP on COCO, significantly outperforming state-of-the-art methods like QDrop and BRECQ, which often fail to produce usable masks at this bit-width.
  • Efficiency: The framework enables a theoretical 3.9× acceleration and reduces storage requirements by 4.9×.

Conclusion

PTQ4SAM successfully bridges the gap between the high performance of large-scale foundation models and the strict constraints of edge hardware. By identifying and resolving the bimodal distribution bottleneck through mathematically equivalent transformations, the framework provides a robust path for deploying SAM in real-time, resource-limited environments.

Summary updated

BibTeX
@inproceedings{lv2024ptq,
  author = {Chengtao Lv and Hong Chen and Jinyang Guo and Yifu Ding and Xianglong Liu},
  title = {{PTQ4SAM: Post-Training Quantization for Segment Anything}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2024},
  doi = {10.1109/cvpr52733.2024.01509},
}
Figure