Dynamic Query Selection for Fast Visual Perceiver

Corentin Dancette, Matthieu Cord · DOI

Abstract

The Perceiver architecture offers a solution to the quadratic complexity of Transformers by using a fixed number of latent queries. However, this static nature prevents the model from adapting to varying computational budgets or image complexities. This paper introduces "Dynamic Query Selection" (DQS) and a "Query Masking" training strategy to enable on-the-fly speed-accuracy trade-offs. Our approach allows a single model to maintain high performance while significantly reducing the number of queries during inference, achieving over 50% speedup on CIFAR-10 with negligible accuracy loss.

Introduction

Transformers have revolutionized computer vision, yet their self-attention mechanism's quadratic scaling with respect to input tokens remains a significant bottleneck for high-resolution processing. The Perceiver architecture addresses this by decoupling the input size from the processing depth through a small set of latent queries. Despite this efficiency, standard Perceivers are rigid; they use the same amount of computation regardless of the input's complexity. Naively reducing the number of queries at inference time typically leads to a severe drop in accuracy because the model is not trained to handle such variations.

Methodology

To create a flexible and efficient Visual Perceiver, we propose two primary innovations: Query Masking and Dynamic Query Selection.

Architecture Overview

The model follows a patch-based approach where an input image is divided into linear embeddings. These are augmented with positional embeddings and fed into a Transformer Cross-Attention block.

The Visual Perceiver architecture with Query Masking and Dynamic Query Selection.

Query Masking Training

Instead of training with a fixed number of queries $Q$, we employ a stochastic training strategy. For each batch, we randomly sample a number $K$ (where $1 \le K \le Q$) and mask the last $Q-K$ queries. This forces the model to prioritize information in the earlier queries. Consequently, the first few queries learn to capture global, essential features, while subsequent queries refine these representations with fine-grained details.

Dynamic Query Selection (DQS)

At inference time, the model adaptively determines the necessary number of queries. By calculating the cosine similarity between output tokens, the DQS mechanism identifies redundant queries—those producing nearly identical information—and discards them. This allows the model to spend more "thought" (computation) on complex images and less on simple ones.

Results and Analysis

Evaluation on the CIFAR-10 dataset demonstrates the effectiveness of our approach. The Query Masking strategy ensures that the model remains robust across a wide range of query counts, unlike standard Perceivers which fail when queries are subsampled.

Comparison of accuracy vs. number of queries for the Visual Perceiver with and without Dynamic Query Selection (DQS).

Key findings include:

  • Efficiency Gains: Reducing queries from 64 to 32 resulted in a speedup from 712ms to 330ms for a batch of 512 images, with accuracy only dipping from 92.58% to 90.52%.
  • DQS Superiority: The DQS-enabled model consistently outperformed fixed-query baselines. To achieve ~91.5% accuracy, a standard model required 48 queries, whereas our DQS approach required an average of only 32 queries.
  • Robustness: While naive random subsampling causes a total breakdown in accuracy, Query Masking training ensures that the model remains functional even when only a handful of queries are used.

Conclusion

Dynamic Query Selection provides a practical pathway for deploying Transformer-based models in resource-constrained environments. By training models to be robust to query masking and implementing a redundancy-based pruning mechanism, we achieve significant inference speedups without the need for multiple specialized models. Future work will explore the scalability of this approach to larger datasets like ImageNet and its application to video processing tasks.

Summary updated

BibTeX
@misc{dancette2022dynamic,
  author = {Corentin Dancette and Matthieu Cord},
  title = {{Dynamic Query Selection for Fast Visual Perceiver}},
  year = {2022},
  eprint = {2205.10873},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2205.10873},
}
Figure