---
title: Shifted-window Transformer Design
url: https://www.emergentmind.com/topics/shifted-window-transformer-design
type: topic
---

# Shifted-window Transformer Design

A Shifted-window Transformer is a hierarchical, locally windowed self-attention architecture in which cross-window connectivity is achieved via cyclically shifted window partitions. This design, introduced as the core of the Swin Transformer [2103.14030], addresses quadratic complexity bottlenecks in global attention for high-dimensional signals by restricting self-attention to small, non-overlapping local windows and interleaving shifted windows to enable information flow across boundaries. The approach generalizes to various domains: 2D images, 3D spatiotemporal volumes, 1D sequences, audio, medical signals, and latent spaces for generative modeling. The shifted-window mechanism has become a foundational architectural principle in vision transformers, time-series models, speech backbones, group-wise memory-optimized attention, and beyond.

## 1. Non-overlapping Window Partitioning and Local Self-Attention

The basic operation partitions each spatial or sequential feature map into non-overlapping windows of fixed size M. Self-attention is computed independently within each window, drastically reducing the quadratic cost of traditional global attention. For a feature grid X∈ℝ^{H×W×C} (image), tokens are grouped into ⌊H/M⌋×⌊W/M⌋ windows. Within each window, for each attention head,

$$
Q = X^w W^Q,\quad K = X^w W^K,\quad V = X^w W^V \\
\mathrm{Attention}(Q, K, V) = \mathrm{Softmax}\biggl(\frac{Q K^T}{\sqrt{d}} + B\biggr)V
$$

where X^w∈ℝ^{M^2×C} is the flattened window, d is the head dimension, and B is a learnable relative-position bias table indexed by pairwise token offsets [2103.14030].

This local strategy yields O(HW·M²·C) compute per layer, i.e., linear in spatial size for fixed M.

## 2. Shifted-window Partitioning: Cyclic Shifts and Masked Attention

To restore cross-window communication lost in pure local attention, every other block applies a cyclic shift by (⌊M/2⌋,⌊M/2⌋) pixels (2D) or by ⌊M/2⌋ positions (1D) before window partitioning [2103.14030, 2306.05143, 2309.09352]. After attention, the feature map is inversely shifted back.

Tokens near old window boundaries now participate in the same (shifted) window, enabling them to attend to neighbors in adjacent windows. Attention masking is employed to prevent interactions between wrapped tokens not originally adjacent:

$$
M_{ij} =
\begin{cases}
0 & \text{if tokens i, j are within the same real window} \\
-\infty & \text{otherwise}
\end{cases}
$$

This masking is added to the attention logits prior to softmax, ensuring strict locality except where the shift merges neighbors.

## 3. Hierarchical Multi-stage and Patch Merging

Shifted-window Transformers are organized into multiple stages, with feature maps downsampled spatially via “patch merging” or “token merging” [2103.14030, 2401.10536, 2306.05143, 2201.06390]. At the end of each stage, non-overlapping groups (e.g., 2×2 for images, pairs for 1D) of windows are concatenated channel-wise and projected via a linear layer, halving token/patch count and doubling feature dimension. This yields a hierarchy analogous to CNN pyramids, with larger windows in deeper stages capturing broader context.

Typical design (Swin v2, R3D-SWIN):

| Stage | Patch Size | Embedding Dim | Window Size | Shift | Blocks | Heads  |
|-------|------------|--------------|-------------|-------|--------|--------|
| 1     | 4×4        | 96           | 7×7         | 3×3   | 2      | 3      |
| 2     | 8×8        | 192          | 7×7         | 3×3   | 2      | 6      |
| 3     | 16×16      | 384          | 7×7         | 3×3   | 18     | 12     |
| 4     | 32×32      | 768          | 7×7         | 3×3   | 2      | 24     |

Patch merging is formalized as:

$$
Y_{i,j} = \mathrm{Linear}\left(\mathrm{Concat}\left[X_{2i,2j},\,X_{2i,2j+1},\,X_{2i+1,2j},\,X_{2i+1,2j+1}\right]\right)
$$

## 4. Domain-specific Generalizations

The shifted-window paradigm has been extended to numerous data modalities:

- **1D (Genomics, Signal Processing):**
  Cyclic shifts over sequences enable cross-segment modeling of long-range dependencies [2306.05143, 2309.09352, 2306.12098]. Windows are of length M; shift by ⌊M/2⌋ ensures mixing.

- **3D (Video, Volumetric prediction):**
  Spatiotemporal blocks apply attention in windows of size (t_w,h_w,w_w) and cyclically shift by (s_t,s_h,s_w) [2201.06390, 2208.01252], supporting efficient modeling on large 3D grids.

- **Multi-scale / Multi-window:**
  Multi-scale shifted-window modules employ parallel windows of different sizes, with features fused via learned softmax weights to maximally capture diverse receptive fields [2306.12098].

- **Edge applications:**
  Complex-valued extensions for spectral estimation preserve phase [2309.09352].

## 5. Computational Efficiency and Empirical Performance

Let N be token count, window size M, feature dim C, number of heads h.

- **Global MHSA:** O(N²·C) FLOPs/mem.
- **Shifted-window MHSA:** O(N·M²·C) per layer; linear scaling.
- **Patch merging per stage** reduces token count by 4 and doubles C.
- **Empirically**, Swin Transformer and its shifted-window variants achieve state-of-the-art performance on ImageNet-1K (top-1: 81–83.7%), COCO (box AP: 46–49), ADE20K (mIoU: 44–50.7), and broad downstream domains [2103.14030, 2312.02725, 2211.14255].

## 6. Architectural Modifications and Alternatives

Several extensions and alternatives have been developed:

- **Padding-free Shift:** Boundary windows are grouped by true size, reducing padding and saving FLOPs [2208.11718].
- **Depthwise Convolutional Replacements:** Plain Window-based Transformer (Win Transformer) replaces shifted-window complexity with local depthwise convolution, matching or surpassing Swin’s accuracy and simplifying implementation [2211.14255].
- **Grouped Attention:** Group Shifted Window Attention (GSWA) divides attention over head groups, reducing activation memory footprint while retaining shifted-window interactivity [2409.06206].
- **Double Attention:** Fuse both local and shifted window attention via channel split in decoder stages, simultaneously expanding receptive field and optimizing depth/complexity trade-off [2408.03521].
- **Pseudo Shifted Window Attention:** Bridges windowed attention and high-frequency details by combining window attention with a depthwise bridging convolution, enhancing global-local context and high-order similarity in generative transformers [2505.13219].

## 7. Design Guidelines and Practical Considerations

- **Window size M:** M=7 is a default for images; ranges 16–256 for 1D genomics/sequences.
- **Shift step:** Use s=⌊M/2⌋ in all axes to guarantee full neighbor mixing.
- **Heads and dimension:** Typical range is 3–24 heads per stage, embedding dim scales from 96–1024.
- **Relative position bias:** Always learn a bias table B∈ℝ^{(2M-1)×(2M-1)} per stage/head for shift-equivariance.
- **Hierarchy:** Stack 3–4 stages, each with 2–18 shifted-window blocks and patch merging between stages, culminating in a final global block or decoder.
- **Memory reduction:** Grouped attention and depthwise convolutions yield ~2× memory savings with negligible performance loss [2409.06206, 2211.14255].
- **Shifted attention is critical for spatial/temporal continuity and for capturing boundary features.

This design is generalizable: empirical ablations confirm that shifted-window partitioning, with or without grouped attention and convolutional augmentations, is sufficient for state-of-the-art dense prediction, reconstruction, segmentation, audio, and multivariate time series modeling across modalities [2103.14030, 2211.14255, 2409.06206, 2306.05143, 2306.12098, 2312.02725, 2408.03521, 2505.13219, 2201.06390, 2309.09352].

Source: https://www.emergentmind.com/topics/shifted-window-transformer-design