Inferensys

Glossary

Swin Transformer

A hierarchical Vision Transformer that computes self-attention within non-overlapping, shifted local windows, achieving linear computational complexity with respect to image size while enabling cross-window connections.
Engineer optimizing context window usage on laptop, token usage charts visible, technical work session.
HIERARCHICAL VISION TRANSFORMER

What is Swin Transformer?

A hierarchical Vision Transformer that computes self-attention within non-overlapping, shifted local windows, achieving linear computational complexity with respect to image size while enabling cross-window connections.

The Swin Transformer is a hierarchical Vision Transformer architecture that computes self-attention exclusively within non-overlapping, shifted local windows. By restricting attention computation to fixed-size windows and shifting them between successive layers, it achieves linear computational complexity relative to image resolution, making it scalable to high-resolution inputs unlike the quadratic complexity of standard Vision Transformers.

This shifted window mechanism enables cross-window connections, allowing information to propagate across the entire image while maintaining efficiency. The architecture constructs a hierarchical feature representation by merging adjacent patches in deeper layers, producing multi-scale feature maps suitable as a general-purpose backbone for both image classification and dense prediction tasks like object detection and semantic segmentation.

Swin Transformer

Key Architectural Features

The Swin Transformer introduces a hierarchical design that computes self-attention within shifted local windows, achieving linear computational complexity while enabling cross-window connections essential for dense vision tasks.

01

Shifted Window Multi-Head Self-Attention

The core innovation of Swin Transformer. Self-attention is computed strictly within non-overlapping local windows of a fixed size (e.g., 7×7 patches). To enable cross-window connections, the window partition shifts by half the window size between consecutive blocks. This cyclic shift mechanism allows information to propagate across the entire image without the quadratic complexity of global self-attention, reducing computation from O(N²) to O(N) relative to image resolution.

O(N)
Computational Complexity
02

Hierarchical Feature Pyramid

Unlike the standard Vision Transformer which maintains a constant feature resolution, Swin builds a multi-scale feature hierarchy by progressively merging adjacent patch tokens in deeper layers. Starting from 4×4 pixel patches, the network reduces spatial resolution by 2× at each stage while doubling the channel dimension. This produces feature maps at 4×, 8×, 16×, and 32× downsampling ratios, making it a natural drop-in replacement for convolutional backbones in detection and segmentation frameworks like Mask R-CNN and UPerNet.

4
Feature Scales
03

Patch Merging for Downsampling

Between hierarchical stages, Swin applies a patch merging layer that concatenates the features of each group of 2×2 spatially adjacent patches and projects them through a linear layer. This operation reduces the number of tokens by a factor of 4 while doubling the feature dimension, analogous to spatial pooling in convolutional networks. The design preserves spatial structure during resolution reduction, unlike the token dropping or strided attention used in other architectures.

04

Relative Position Bias

Swin incorporates a learnable relative position bias matrix added directly to the attention scores within each local window. For a window of size M×M, the bias parameterizes all possible relative positions along the x and y axes, resulting in a (2M-1)×(2M-1) bias matrix. This explicit positional encoding is more effective than absolute position embeddings for dense prediction tasks and is invariant to the absolute location of the window within the image.

05

Cyclic Shift and Masking Strategy

The shifted window partitioning creates windows of varying sizes at the image boundaries. To maintain efficient batched computation, Swin uses a cyclic shift operation that moves boundary windows to the top-left of the feature map, creating a consistent set of M×M windows. A corresponding attention mask prevents cross-window attention between non-adjacent regions that were artificially brought together by the shift. After attention computation, a reverse cyclic shift restores the original spatial arrangement.

06

Linear Complexity Scaling

By confining self-attention to fixed-size local windows, Swin achieves linear computational complexity with respect to image resolution. For an image with h×w patches and a window size of M, the complexity per block is O(h·w·M²) rather than O((h·w)²). With M typically set to 7, this makes Swin practical for high-resolution inputs like 1536×1536 medical images where global self-attention would be computationally prohibitive. This efficiency is critical for 3D volumetric analysis and whole slide imaging.

M=7
Default Window Size
SWIN TRANSFORMER

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the Swin Transformer architecture, its mechanisms, and its role in modern computer vision.

A Swin Transformer is a hierarchical Vision Transformer that computes self-attention within non-overlapping, shifted local windows, achieving linear computational complexity with respect to image size. It works by partitioning an image into patches and processing them through four stages. In each stage, it applies a Shifted Window Multi-Head Self-Attention (SW-MSA) mechanism. Standard window attention limits computation to fixed local regions, but by shifting the window partition between consecutive layers, the model creates cross-window connections, enabling information flow across the entire image. This design allows the Swin Transformer to serve as a general-purpose backbone for diverse vision tasks, from image classification to dense prediction tasks like object detection and semantic segmentation, without the quadratic complexity of global self-attention.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.