{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/ptq4sam-post-training-quantization-for","title":"PTQ4SAM: Post-Training Quantization for Segment Anything","arxiv_id":"2405.03144","date":"2024-05-06","proceeding":"CVPR 2024 1","authors":["Chengtao Lv","Hong Chen","Jinyang Guo","Yifu Ding","Xianglong Liu"],"abstract":"Segment Anything Model (SAM) has achieved impressive performance in many computer vision tasks. However, as a large-scale model, the immense memory and computation costs hinder its practical deployment. In this paper, we propose a post-training quantization (PTQ) framework for Segment Anything Model, namely PTQ4SAM. First, we investigate the inherent bottleneck of SAM quantization attributed to the bimodal distribution in post-Key-Linear activations. We analyze its characteristics from both per-tensor and per-channel perspectives, and propose a Bimodal Integration strategy, which utilizes a mathematically equivalent sign operation to transform the bimodal distribution into a relatively easy-quantized normal distribution offline. Second, SAM encompasses diverse attention mechanisms (i.e., self-attention and two-way cross-attention), resulting in substantial variations in the post-Softmax distributions. Therefore, we introduce an Adaptive Granularity Quantization for Softmax through searching the optimal power-of-two base, which is hardware-friendly. Extensive experimental results across various vision tasks (instance segmentation, semantic segmentation and object detection), datasets and model variants show the superiority of PTQ4SAM. For example, when quantizing SAM-L to 6-bit, we achieve lossless accuracy for instance segmentation, about 0.5\\% drop with theoretical 3.9$\\times$ acceleration. The code is available at \\url{https://github.com/chengtao-lv/PTQ4SAM}.","url_abs":"https://arxiv.org/abs/2405.03144v1","url_pdf":"https://arxiv.org/pdf/2405.03144v1.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"ptq4sam-post-training-quantization-for","repo_url":"https://github.com/chengtao-lv/ptq4sam","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[{"task_slug":"instance-segmentation","task_name":"Instance Segmentation"},{"task_slug":"object-detection","task_name":"Object Detection"},{"task_slug":"quantization","task_name":"Quantization"},{"task_slug":"segmentation","task_name":"Segmentation"},{"task_slug":"semantic-segmentation","task_name":"Semantic Segmentation"},{"task_slug":"object-detection-1","task_name":"object-detection"}],"methods":[{"method_slug":"sam","method_name":"SAM"},{"method_slug":"softmax","method_name":"Softmax"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2405.03144","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2405.03144"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/chengtao-lv/ptq4sam","reach":{"status":"ok"}},{"provenance":"deterministic:regex_extraction","url":"https://github.com/chengtao-lv/PTQ4SAM","reach":{"status":"ok"}}],"summary":{"ran_draft_wrong":3,"ran":4,"unverified":2},"by_repo_kind":{"official":{"samples":9,"ran":7,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":9,"samples":[{"code_sha256_prefix":"675da8641c602b03","entry":"round_ste","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/quantization/util_quant.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/quantization/util_quant.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":2,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"675da8641c602b03"}},{"code_sha256_prefix":"235ca98c280dc43c","entry":"fake_logquantize_per_tensor_affine","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/quantization/util_quant.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/quantization/util_quant.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"235ca98c280dc43c"}},{"code_sha256_prefix":"f5f3045a5c318d7c","entry":"fake_quantize_per_tensor_affine","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/quantization/util_quant.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/quantization/util_quant.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"f5f3045a5c318d7c"}},{"code_sha256_prefix":"2f381ebd3e023bfa","entry":"get_module_args","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/quantization/quantized_module_matmul.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/quantization/quantized_module_matmul.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"2f381ebd3e023bfa"}},{"code_sha256_prefix":"8003cb7a5176e497","entry":"lp_loss","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/solver/recon.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/solver/recon.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"8003cb7a5176e497"}},{"code_sha256_prefix":"afb8f93395a730c8","entry":"parse_config","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/solver/utils.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/solver/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"afb8f93395a730c8"}},{"code_sha256_prefix":"7a2978318593f764","entry":"update_specialized_quantizer_config","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"ptq4sam/model/quant_model.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/ptq4sam/model/quant_model.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"7a2978318593f764"}},{"code_sha256_prefix":"8ff99a6bd2ebf598","entry":"ordered_yaml_dump","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"mmdetection/.dev_scripts/gather_models.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/mmdetection/.dev_scripts/gather_models.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"8ff99a6bd2ebf598"}},{"code_sha256_prefix":"138e112e22b0314e","entry":"process_checkpoint","repo":"chengtao-lv/PTQ4SAM","repo_kind":"official","path":"mmdetection/.dev_scripts/gather_models.py","file_url":"https://github.com/chengtao-lv/PTQ4SAM/blob/HEAD/mmdetection/.dev_scripts/gather_models.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"138e112e22b0314e"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}