YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
BinaryAttention: One-Bit QK-Attention for Vision and Diffusion Transformers
[π arXiv paper] [π€ Hugging Face]
π©Accepted by CVPR2026
π¬ Overview
π Abstract
Transformers have achieved widespread and remarkable success, while the computational complexity of their attention modules remains a major bottleneck for vision tasks. Existing methods mainly employ 8-bit or 4-bit quantization to balance efficiency and accuracy. In this paper, with theoretical justification, we indicate that binarization of attention preserves the essential similarity relationships, and propose BinaryAttention, an effective method for fast and accurate 1-bit qk-attention. Specifically, we retain only the sign of queries and keys in computing the attention, and replace the floating dot products with bit-wise operations, significantly reducing the computational cost. We mitigate the inherent information loss under 1-bit quantization by incorporating a learnable bias, and enable end-to-end acceleration. To maintain the accuracy of attention, we adopt quantization-aware training and self-distillation techniques, mitigating quantization errors while ensuring sign-aligned similarity. BinaryAttention is more than 2x faster than FlashAttention2 on A100 GPUs. Extensive experiments on vision transformer and diffusion transformer benchmarks demonstrate that BinaryAttention matches or even exceeds full-precision attention, validating its effectiveness. Our work provides a highly efficient and effective alternative to full-precision attention, pushing the frontier of low-bit vision and diffusion transformers.
π― Main Results
π οΈ Getting Started
# 1. Clone the repository
git clone https://github.com/EdwardChasel/BinaryAttention.git
cd BinaryAttention
# 2. Create and activate a new conda environment
conda create -n BinaryAttention python=3.10
conda activate BinaryAttention
# 3. Install dependent packages
pip install --upgrade pip
pip install -r requirements.txt
β¨ Pre-trained Models
π Data Preparation
ImageNet is an image database organized according to the WordNet hierarchy. Download and extract ImageNet train and val images from http://image-net.org/. Organize the data into the following directory structure:
imagenet/
βββ train/
β βββ n01440764/ (Example synset ID)
β β βββ image1.JPEG
β β βββ image2.JPEG
β β βββ ...
β βββ n01443537/ (Another synset ID)
β β βββ ...
β βββ ...
βββ val/
βββ n01440764/ (Example synset ID)
β βββ image1.JPEG
β βββ ...
βββ ...
π Quick Start
To train BinaryAttention models for classification on ImageNet, use the following commands for different configurations:
python -m torch.distributed.launch --nproc_per_node=8 --use_env main.py \
--model </name/of/model> \
--attn-quant --attn-bias --pv-quant \
--batch-size 128 \
--input-size </size/of/input/image> \
--epochs 300 --lr 5e-5 --min-lr 5e-6 --weight-decay 0.02 \
--finetune </path/of/full-precision/checkpoint> \
--data-path </path/of/dataset> \
--output_dir </path/of/output> \
## optional, defaults to tiny disable, small and base enable.
# --distillation-type hard \
# --teacher-path </path/of/full-precision/checkpoint>
To evaluate the performance with pre-trained weights:
python main.py --eval --resume </path/of/checkpoint> --data-path </path/of/dataset> --model </name/of/model> --attn-quant --attn-bias --pv-quant --input-size </size/of/input>
ποΈ Citation
@inproceedings{xiao2026binary,
title={BinaryAttention: One-Bit QK-Attention for Vision and Diffusion Transformers},
author={Xiao, Chaodong and Zhang, Zhengqiang and Zhang, Lei},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2026}
}
π Acknowledgments
This project is largely based on DeiT, SageAttention. We are truly grateful for their excellent work.
π« License
This project is released under the Apache 2.0 license.