Rotary Position Embedding for Vision Transformer

Byeongho Heo, Song Park, Dongyoon Han, Sangdoo Yun

Introduction

Vision Transformers (ViTs) have revolutionized computer vision, yet they inherit a fundamental characteristic from their NLP predecessors: permutation invariance. To process images, which are inherently structured 2D grids, ViTs require explicit positional information. Traditionally, this is provided via Absolute Positional Embeddings (APE) or Relative Position Bias (RPB). However, these methods struggle with resolution flexibility. When a model trained on 224x224 images is tasked with processing 512x512 images (extrapolation), performance typically degrades significantly because fixed embeddings do not scale gracefully to unseen coordinates.

RoPE-Mixed: Adapting Rotary Embeddings for 2D

To address these limitations, researchers have adapted Rotary Position Embedding (RoPE)—a technique widely used in Large Language Models like Llama—for the 2D domain of computer vision. The core concept of RoPE is to rotate Query and Key vectors in a complex vector space, where the rotation angle is a function of the token's position. This ensures that the attention score between two tokens depends naturally on their relative distance.

While a naive 1D-to-2D adaptation (Axial RoPE) applies 1D rotations to the X and Y axes independently, it remains "blind" to diagonal relationships. The proposed RoPE-Mixed overcomes this by introducing learnable frequencies. This allows the model to capture complex 2D spatial dependencies, including diagonal orientations, during training. Furthermore, the paper notes that RoPE does not require extra parameters to handle "phase shifts" (controlling activation areas) as the existing weight matrices in self-attention layers naturally learn these adjustments.

Fourier analysis comparing Axial and Mixed frequency embeddings. RoPE-Mixed demonstrates superior localization and diagonal awareness compared to the axis-aligned artifacts of Axial RoPE.

As shown in the Fourier analysis above, Axial RoPE creates distinct artifacts along the primary axes. In contrast, RoPE-Mixed produces sharper, more localized attention patterns, confirming that learnable frequencies are essential for robust 2D spatial awareness.

Experimental Evaluation

The efficacy of RoPE-Mixed was validated across standard benchmarks:

  • ImageNet-1k: Evaluated for classification and multi-resolution extrapolation.
  • MS-COCO: Tested for object detection using DINO-ViTDet and DINO-Swin frameworks.
  • ADE20k: Assessed for semantic segmentation via UperNet and Mask2Former.

Key Insights and Results

The most significant advantage of RoPE-Mixed is its superior extrapolation capability. Unlike APE-based models that lose accuracy rapidly at higher resolutions, RoPE-Mixed models maintain or even improve performance when tested on larger images.

Mean attention distance across layers for different resolutions. RoPE-Mixed (red) and RoPE-Axial (teal) maintain significantly higher attention distances in deeper layers compared to APE (yellow), especially as resolution increases.

The attention distance analysis reveals that RoPE-based models maintain a more global focus in deeper layers compared to APE, which tends to collapse toward local attention at higher resolutions. This global context retention is a key driver of the observed performance gains in high-resolution tasks.

Quantitatively, RoPE-Mixed serves as a powerful "drop-in" replacement:

  • Object Detection: Improved box AP by +1.8 points for ViT-B on MS-COCO.
  • Semantic Segmentation: Achieved a +2.5 mIoU boost over the baseline on ADE20k.
  • Efficiency: The method adds negligible computational overhead, increasing parameters by only ~0.01% for a ViT-B model with no significant impact on FLOPs.

Conclusion and Future Work

RoPE-Mixed successfully bridges the gap between the flexible positioning required for high-resolution vision tasks and the efficient rotary mechanisms of modern LLMs. While it excels at extrapolation, future work may focus on optimizing performance for interpolation (smaller-than-training resolutions) and extending the approach to temporal or volumetric data, such as video and 3D point clouds.

Summary updated

BibTeX
@inproceedings{heo2024rotary,
  author = {Byeongho Heo and Song Park and Dongyoon Han and Sangdoo Yun},
  title = {{Rotary Position Embedding for Vision Transformer}},
  booktitle = {European Conference on Computer Vision},
  year = {2024},
  doi = {10.1007/978-3-031-72684-2\_17},
}
Figure