Context-Aware Token Selection and Packing for Enhanced Vision Transformer

Tianyi Zhang, Baoxin Li, Jae-sun Seo, Yu CaparXiv 2024

Abstract

In recent years, the long-range attention mechanism of vision transformers has driven significant performance breakthroughs across various computer vision tasks. However, the traditional self-attention mechanism, which processes both informative and non-informative tokens, suffers from inefficiency and inaccuracies. While sparse attention mechanisms have been introduced to mitigate these issues by pruning tokens involved in attention, they often lack context-awareness and intelligence. These mechanisms frequently apply a uniform token selection strategy across different inputs for batch training or optimize efficiency only for the inference stage. To overcome these challenges, we propose a novel algorithm: Select and Pack Attention (SPA). SPA dynamically selects informative tokens using a low-cost gating layer supervised by selection labels and packs these tokens into new batches, enabling a variable number of tokens to be used in parallelized GPU batch training and inference. Extensive experiments across diverse datasets and computer vision tasks demonstrate that SPA delivers superior performance and efficiency, including a 0.6 mAP improvement in object detection and a 16.4% reduction in computational costs.

Introduction

Vision Transformers (ViTs) have revolutionized computer vision, but they come with a heavy "attention tax." The computational cost of self-attention grows quadratically with the number of tokens (image patches). To save power and time, researchers often use sparse attention—essentially pruning "unimportant" background tokens.

However, existing sparse methods face two major hurdles. First, there are significant efficiency gaps: GPUs process uniform data most efficiently, but if one image in a batch has many useful tokens and another has few, standard frameworks often pad the smaller one with "dummy" tokens, wasting computation. Second, many methods suffer from performance degradation because they use unsupervised selection strategies that accidentally throw away crucial details, especially in dense tasks like object detection where the "background" might still contain important context.

Comparison of token selection strategies: DynamicViT, SparseViT, and the proposed SPA.

Methodology: Select and Pack Attention (SPA)

The authors propose a hierarchical backbone called the Select and Pack Transformer (SPT), which integrates the Select and Pack Attention (SPA) mechanism to address these inefficiencies.

The Selector and Multi-Scale Supervision

SPA uses a lightweight linear gating layer to assign importance scores to every token. Crucially, this selection is supervised. The model is trained using ground-truth labels (like bounding boxes or segmentation masks) to learn exactly which pixels constitute "informative" content. To prevent the model from losing information too early, the authors employ a multi-scale approach: scores from later, lower-resolution stages are up-sampled and combined with earlier stages to ensure no small, important objects are missed.

The hierarchical architecture of the Select and Pack Transformer (SPT) featuring multi-scale supervised selection.

The Packer (Token Packing)

Instead of just masking out low-score tokens—which doesn't reduce the actual tensor size processed by the GPU—SPA extracts the high-score tokens and packs them into fixed-size "package containers." These containers form new, smaller batches that the GPU can process at full throttle without wasted padding. This shift from masking to packing achieves true hardware-level acceleration during both training and inference.

Architectural Integration

The SPT architecture is built on the Swin Transformer's hierarchical structure. Through experimentation, the authors found that applying SPA at Stage 3 provides the best balance of efficiency and feature integrity, as features in the first two stages are often too "shallow" to distinguish semantic importance accurately.

Experiments and Results

The researchers evaluated SPT across several standard and custom "sparse" benchmarks, including BDD100K, COCO2017, PASCAL VOC 2012, and Tiny ImageNet. They also created sparse variants (e.g., COCO-S, IN-S) to test "needle in a haystack" scenarios where objects are small relative to the image size.

  • Efficiency: SPT-B (the "Base" model) reduced computational costs (GFLOPs) by 16.4% compared to standard Swin-B on the BDD100K dataset.
  • Speed: On an NVIDIA L40S GPU, SPT-T achieved a throughput of 58 images/second, significantly faster than the standard Swin-T (50) and the state-of-the-art DynamicSwin-T (46).
  • Accuracy: By using object labels to guide the "Selector," the model achieved a 0.6 mAP gain in detection on BDD100K rather than the typical performance drop seen in sparse models. On the sparse IN-S dataset, SPT improved Top-1 accuracy from 29.10% to 32.75% while only looking at ~23% of the tokens.

Conclusion and Future Work

Select and Pack Attention (SPA) represents a significant step toward making Vision Transformers more efficient without sacrificing accuracy. By reorganizing tokens into dense batches and using supervised selection, it overcomes the primary limitations of previous sparse attention methods.

However, the current dependency on ground-truth labels for training the gating layer limits its use in purely self-supervised settings. Future research will focus on developing robust self-supervised gating mechanisms and exploring dynamic packing lengths to further optimize performance on diverse datasets.

Summary updated

BibTeX
@misc{zhang2024context,
  author = {Tianyi Zhang and Baoxin Li and Jae-sun Seo and Yu Cap},
  title = {{Context-Aware Token Selection and Packing for Enhanced Vision Transformer}},
  year = {2024},
  eprint = {2410.23608},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.23608},
}
Figure