NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

FlexiViT: One Model for All Patch Sizes

Lucas Beyer, Pavel Izmailov, Alexander Kolesnikov, Mathilde Caron, Simon Kornblith, Xiaohua Zhai, Matthias Minderer, Michael Tschannen, Ibrahim Alabdulmohsin, Filip PaveticCVPR 2022

Abstract

Vision Transformers convert images to sequences by slicing them into patches. The size of these patches controls a speed/accuracy tradeoff, with smaller patches leading to higher accuracy at greater computational cost, but changing the patch size typically requires retraining the model. In this paper, we demonstrate that simply randomizing the patch size at training time leads to a single set of weights that performs well across a wide range of patch sizes, making it possible to tailor the model to different compute budgets at deployment time. We extensively evaluate the resulting model, which we call FlexiViT, on a wide range of tasks, including classification, image-text retrieval, open-world detection, panoptic segmentation, and semantic segmentation, concluding that it usually matches, and sometimes outperforms, standard ViT models trained at a single patch size in an otherwise identical setup. Hence, FlexiViT training is a simple drop-in improvement for ViT that makes it easy to add compute-adaptive capabilities to most models relying on a ViT backbone architecture.

Introduction

In Vision Transformers (ViTs), the patch size is the primary lever for controlling the trade-off between computational cost and predictive performance. Smaller patches result in longer sequences, which increases accuracy but significantly raises computational costs (GFLOPs) due to the quadratic scaling of self-attention.

Standard ViTs are typically "inflexible": a model trained with 16x16 patches performs poorly if run with 32x32 patches at inference time to save compute. Consequently, researchers and engineers typically have to train and maintain a suite of different models (e.g., ViT-S/16, ViT-B/32, etc.) to cover different hardware constraints or latency requirements. FlexiViT aims to replace this "zoo" of models with a single, versatile backbone.

Methods: Flexifying the Vision Transformer

The authors propose two simple but effective modifications to the standard ViT training pipeline to achieve patch-size invariance.

Randomized Patch Sizes

During training, the patch size $p$ is sampled uniformly at random for each batch from a predefined set (e.g., ranging from 8 to 48). This forces the transformer backbone to learn representations that are invariant to the specific "granularity" of the input tokens.

PI-resize (Pseudoinverse Resize)

To handle varying patch sizes, the patch embedding weights and position embeddings must be resized on-the-fly. The authors found that standard bilinear interpolation changes the magnitude and distribution of token norms, hurting performance. They derive PI-resize, which uses the Moore-Penrose pseudoinverse of the resizing transformation. This ensures that the resized embeddings are an optimal "inverse" of the original, better preserving the information and scale of the learned weights across different resolutions.

FlexiViT architecture overview: A single set of weights handles different patch sizes by resizing patch embeddings to generate varying token counts.

Additionally, the authors leverage Knowledge Distillation from a strong fixed-patch-size teacher (like a pre-trained ViT-B/8) to further boost the student FlexiViT's performance, particularly at the larger, more challenging patch sizes.

Key Results and Insights

FlexiViT was extensively evaluated across ImageNet-21k pre-training and a variety of downstream tasks including classification, image-text retrieval, and segmentation.

State-of-the-Art Tradeoffs

A single FlexiViT-L model matches or outperforms the entire family of DeiT III models (S, B, and L) across their respective compute regimes on ImageNet-1k. A FlexiViT-B model can scale from 84.4% accuracy (at 1.6 ms/image) to 86.1% accuracy (at 13 ms/image) just by changing the patch size at inference time.

Inference speed vs. accuracy tradeoff: FlexiViT models (solid/dashed lines) provide a continuous Pareto front that matches or exceeds specialized fixed-size models like DeiT III and ResNet.

Internal Consistency and Fast Transfer

Using Centered Kernel Alignment (CKA), the authors show that FlexiViT's internal representations are remarkably similar across different patch sizes. This "scale-agnostic" nature enables a "Fast Transfer" trick: fine-tuning a FlexiViT model cheaply using large patches (short sequences) and then deploying it using small patches (long sequences) to get high accuracy. This significantly reduces the cost of adapting models to new downstream tasks.

Conclusion

FlexiViT provides a simple drop-in improvement for ViT architectures, making it easy to add compute-adaptive capabilities to most models relying on a ViT backbone. While performance degrades if extrapolating significantly beyond the trained patch-size range, the model's ability to provide a flexible speed/accuracy tradeoff with a single set of weights offers significant practical advantages for deployment across diverse hardware environments.

Summary updated

BibTeX
@inproceedings{beyer2022flexivit,
  author = {Lucas Beyer and Pavel Izmailov and Alexander Kolesnikov and Mathilde Caron and Simon Kornblith and Xiaohua Zhai and Matthias Minderer and Michael Tschannen and Ibrahim Alabdulmohsin and Filip Pavetic},
  title = {{FlexiViT: One Model for All Patch Sizes}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2022},
  url = {https://openreview.net/forum?id=NGljpIWC4S},
}
Figure