{"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/learned-queries-for-efficient-local-attention","title":"Learned Queries for Efficient Local Attention","arxiv_id":"2112.11435","date":"2021-12-21","proceeding":"CVPR 2022 1","authors":["Moab Arar","Ariel Shamir","Amit H. Bermano"],"abstract":"Vision Transformers (ViT) serve as powerful vision models. Unlike convolutional neural networks, which dominated vision research in previous years, vision transformers enjoy the ability to capture long-range dependencies in the data. Nonetheless, an integral part of any transformer architecture, the self-attention mechanism, suffers from high latency and inefficient memory utilization, making it less suitable for high-resolution input images. To alleviate these shortcomings, hierarchical vision models locally employ self-attention on non-interleaving windows. This relaxation reduces the complexity to be linear in the input size; however, it limits the cross-window interaction, hurting the model performance. In this paper, we propose a new shift-invariant local attention layer, called query and attend (QnA), that aggregates the input locally in an overlapping manner, much like convolutions. The key idea behind QnA is to introduce learned queries, which allow fast and efficient implementation. We verify the effectiveness of our layer by incorporating it into a hierarchical vision transformer model. We show improvements in speed and memory complexity while achieving comparable accuracy with state-of-the-art models. Finally, our layer scales especially well with window size, requiring up-to x10 less memory while being up-to x5 faster than existing methods. The code is publicly available at \\url{https://github.com/moabarar/qna}.","url_abs":"https://arxiv.org/abs/2112.11435v2","url_pdf":"https://arxiv.org/pdf/2112.11435v2.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":"learned-queries-for-efficient-local-attention","repo_url":"https://github.com/moabarar/qna","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"image-classification","task_name":"Image Classification"},{"task_slug":"object-detection","task_name":"Object Detection"}],"methods":[{"method_slug":"attention","method_name":"Attention"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"high-resolution-input","method_name":"High-resolution input"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"speed","method_name":"SPEED"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"vision-transformer","method_name":"Vision Transformer"}],"datasets_introduced":[],"methods_introduced":[],"results":[{"leaderboard":"/sota/image-classification-on-imagenet","task":"Image Classification","dataset":"ImageNet","model":"QnA-ViT-Base","rank_in_archive_order":398,"of":1060,"metrics":{"GFLOPs":"9.7","Number of params":"56M","Top 1 Accuracy":"83.7%"},"uses_additional_data":false},{"leaderboard":"/sota/image-classification-on-imagenet","task":"Image Classification","dataset":"ImageNet","model":"QnA-ViT-Small","rank_in_archive_order":453,"of":1060,"metrics":{"GFLOPs":"4.4","Number of params":"25M","Top 1 Accuracy":"83.2%"},"uses_additional_data":false},{"leaderboard":"/sota/image-classification-on-imagenet","task":"Image Classification","dataset":"ImageNet","model":"QnA-ViT-Tiny","rank_in_archive_order":616,"of":1060,"metrics":{"GFLOPs":"2.5","Number of params":"16M","Top 1 Accuracy":"81.7%"},"uses_additional_data":false}],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2112.11435","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2112.11435"}},"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/moabarar/qna","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"unverified":5},"by_repo_kind":{"official":{"samples":5,"ran":0,"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":0,"samples":[{"code_sha256_prefix":"4cdf2f088fd5dae7","entry":"init_from_pretrained","repo":"moabarar/qna","repo_kind":"official","path":"utils.py","file_url":"https://github.com/moabarar/qna/blob/HEAD/utils.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4cdf2f088fd5dae7"}},{"code_sha256_prefix":"d04f187058cb2a9c","entry":"random_apply","repo":"moabarar/qna","repo_kind":"official","path":"augment/color_util.py","file_url":"https://github.com/moabarar/qna/blob/HEAD/augment/color_util.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"d04f187058cb2a9c"}},{"code_sha256_prefix":"e1aaf77a8784526a","entry":"random_brightness","repo":"moabarar/qna","repo_kind":"official","path":"augment/color_util.py","file_url":"https://github.com/moabarar/qna/blob/HEAD/augment/color_util.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e1aaf77a8784526a"}},{"code_sha256_prefix":"103f46a75894451d","entry":"register","repo":"moabarar/qna","repo_kind":"official","path":"models/qna_vit.py","file_url":"https://github.com/moabarar/qna/blob/HEAD/models/qna_vit.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"103f46a75894451d"}},{"code_sha256_prefix":"49c827442d2fb761","entry":"to_grayscale","repo":"moabarar/qna","repo_kind":"official","path":"augment/color_util.py","file_url":"https://github.com/moabarar/qna/blob/HEAD/augment/color_util.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"49c827442d2fb761"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}