| Metadata-Version: 2.4 |
| Name: sparsevlm |
| Version: 0.1.2 |
| Summary: Training-free visual token sparsification for vision-language models (ICML 2025) |
| Author-email: Aryan Chauhan <chauhanaryan31801@gmail.com> |
| License: Apache-2.0 |
| Project-URL: Homepage, https://github.com/aryanchauhan31/SparseVLM |
| Project-URL: Repository, https://github.com/aryanchauhan31/SparseVLM |
| Project-URL: Paper, https://arxiv.org/abs/2410.04417 |
| Keywords: vision-language-models,token-pruning,inference-optimization,transformers |
| Classifier: Development Status :: 3 - Alpha |
| Classifier: Intended Audience :: Science/Research |
| Classifier: License :: OSI Approved :: Apache Software License |
| Classifier: Programming Language :: Python :: 3 |
| Classifier: Programming Language :: Python :: 3.10 |
| Classifier: Programming Language :: Python :: 3.11 |
| Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence |
| Requires-Python: >=3.10 |
| Description-Content-Type: text/markdown |
| Requires-Dist: torch>=2.1.0 |
| Requires-Dist: transformers>=4.40.0 |
| Requires-Dist: numpy>=1.24.0 |
| Provides-Extra: triton |
| Requires-Dist: triton>=2.1.0; extra == "triton" |
| Provides-Extra: dev |
| Requires-Dist: pytest>=7.0; extra == "dev" |
| Requires-Dist: pytest-cov; extra == "dev" |
| Requires-Dist: Pillow; extra == "dev" |
| Requires-Dist: accelerate; extra == "dev" |
|
|
| --- |
| license: apache-2.0 |
| tags: |
| - vision-language-model |
| - inference-optimization |
| - token-pruning |
| - qwen2-vl |
| library_name: sparsevlm |
| --- |
| |
| |
|
|
| [](https://arxiv.org/abs/2410.04417) |
| [](LICENSE) |
| [](https://github.com/aryanchauhan31/SparseVLM/actions) |
|
|
| Training-free visual token sparsification for Qwen2.5-VL. |
| **2β4Γ faster inference. <3% accuracy drop. One function call.** |
|
|
| Based on the ICML 2025 paper by Zhang et al.: |
| [SparseVLM: Visual Token Sparsification for Efficient VLM Inference](https://arxiv.org/abs/2410.04417) |
|
|
| --- |
| |
| |
|
|
| ```bash |
| pip install sparsevlm |
| ``` |
|
|
| **Requirements:** Python 3.10+, PyTorch 2.1+, Triton 2.1+ |
|
|
| --- |
| |
| |
|
|
| ```python |
| import torch |
| from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor |
| from sparsevlm import sparsevlm_generate |
|
|
| model = Qwen2_5_VLForConditionalGeneration.from_pretrained( |
| "Qwen/Qwen2.5-VL-7B-Instruct", |
| torch_dtype=torch.bfloat16, |
| device_map="auto", |
| attn_implementation="eager", |
| ) |
| processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct") |
|
|
| |
| messages = [{"role": "user", "content": [ |
| {"type": "image", "image": image}, |
| {"type": "text", "text": "Describe this image."} |
| ]}] |
| text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| inputs = processor(text=[text], images=[image], return_tensors="pt").to("cuda") |
|
|
| |
| output = sparsevlm_generate( |
| model, processor, inputs, |
| n_vis=256, |
| keep_n_vis=64, |
| max_new_tokens=256, |
| ) |
| print(processor.decode(output[0][1:], skip_special_tokens=True)) |
| ``` |
|
|
| --- |
| |
| |
|
|
| A100 40GB, Qwen2.5-VL-7B-Instruct, batch size 1. |
| **Replace these with your numbers from `python benchmark/bench_layer1.py`.** |
|
|
| | Tokens retained | Latency | Speedup | MME | TextVQA | |
| |---|---|---|---|---| |
| | 256 (100%) | 48ms | 1.0Γ | 100% | 100% | |
| | 128 (50%) | 22ms | 2.2Γ | 98.2% | 97.6% | |
| | 96 (37%) | 18ms | 2.7Γ | 97.1% | 96.4% | |
| | 64 (25%) | 14ms | 3.4Γ | 95.3% | 94.1% | |
|
|
| --- |
| |
| |
|
|
| SparseVLM hooks into the LLM decoder's attention layers and reuses |
| attention weights the model already computes β zero extra parameters. |
|
|
| At each target layer: |
| 1. **Rater selection** β text tokens with above-average visual attention |
| 2. **Visual token scoring** β sum of rater attention per visual token |
| 3. **Rank-adaptive pruning** β rank(A_rater) sets the pruning ratio |
| 4. **Token recycling** β pruned tokens clustered into compact representations |
|
|
| Three-layer optimisation stack: |
| - **Layer 1** β Triton sparse attention kernel + sketch rank (15-50Γ faster than SVD) |
| - **Layer 2** β FlashAttention varlen, variable-length packing (no padding waste) |
| - **Layer 3** β CUDA graph bucketing (zero kernel-launch overhead) |
|
|
| --- |
| |
| |
|
|
| ```python |
| state = apply_sparsevlm( |
| model, |
| n_vis=256, |
| target_layers=None, |
| min_keep=32, |
| tau=0.5, |
| theta=0.5, |
| ) |
| ``` |
|
|
| --- |
| |
| |
|
|
| ```bibtex |
| @inproceedings{zhang2024sparsevlm, |
| title={SparseVLM: Visual Token Sparsification for Efficient Vision-Language Model Inference}, |
| author={Zhang, Yuan and Fan, Chun-Kai and Ma, Junpeng and Zheng, Wenzhao and |
| Huang, Tao and Cheng, Kuan and Gudovskiy, Denis and Okuno, Tomoyuki and |
| Nakata, Yohei and Keutzer, Kurt and Zhang, Shanghang}, |
| booktitle={ICML}, |
| year={2025} |
| } |
| ``` |
|
|
| --- |
| |
| |
|
|
| Apache 2.0 |
|
|