---
title: Windowed and Shifted Self-Attention
url: https://www.emergentmind.com/topics/windowed-and-shifted-self-attention
type: topic
---

# Windowed and Shifted Self-Attention

Windowed and shifted self-attention refers to a family of sparse attention mechanisms that restrict self-attention computations to local, typically non-overlapping windows (“windowed”), and then alternate these with specially shifted window patterns (“shifted”) to enable cross-window information flow. This strategy has enabled transformer architectures to scale from quadratic to near-linear complexity in both vision and language domains, while maintaining or improving accuracy through efficient context fusion.

## 1. Core Mechanisms: Windowed and Shifted Self-Attention

The canonical windowed self-attention, as introduced in Swin Transformer and adopted in domains including vision, 3D data, and language modeling, partitions the $H \times W \times C$ input (feature map, tokenized image/volume, or sequence) into $M \times M$ non-overlapping windows. Standard multi-head self-attention (MSA) is then applied independently within each window; let $X_w \in \mathbb{R}^{M^2 \times C}$ be a window’s tokens,
\[
Q = X_w W^Q, \quad K = X_w W^K, \quad V = X_w W^V,
\]
with $Q,K,V \in \mathbb{R}^{M^2 \times d}$, and attention output
\[
\text{Attention}(Q, K, V) = \text{Softmax}\left(\frac{QK^\top}{\sqrt{d}} + B \right) V,
\]
where $B$ is a learnable relative position bias [2504.15317][2207.04403][2312.02725].

To overcome the isolation of local windows, the shifted window mechanism cyclically shifts the feature map by $(\lfloor M/2 \rfloor, \lfloor M/2 \rfloor)$ (2D), $(M/2, M/2, M/2)$ (3D), or corresponding chunk offsets (language), re-partitions into new windows, and applies the same local attention with a mask to block unwanted cross-window attention. The output is reverse-shifted to restore alignment. Over two alternated layers, all tokens in a local neighborhood can exchange information, giving every token an effective receptive field spanning $3 \times 3$ windows in 2D (or $3^d$ in $d$-dimensions) [2504.15317][2201.06390][2401.13049].

Pseudocode:

```python
def SwinShiftedAttention(X, window_size):
    # Regular window attention
    Y1 = window_attention(X, mask=all_zeros)
    # Shifted window attention
    shift = window_size // 2
    X_shift = cyclic_roll(X, (shift, shift))  # roll in all relevant axes
    Y2_shift = window_attention(X_shift, mask=cross_window_mask)
    Y2 = cyclic_roll(Y2_shift, (-shift, -shift))
    # Combine in Transformer block (residual + MLP)
    return transformer_block_combine(Y1, Y2)
```
[2504.15317][2201.06390][2312.02725]

## 2. Mathematical Properties and Complexity

Windowed attention reduces memory and compute from $O(N^2C)$ for global attention to $O(N M^2 C)$, where $N = HW$ is the number of tokens, and $M^2$ is the window area. If $M \ll \sqrt{N}$, the complexity approaches $O(N)$, enabling scalability to high-resolution images, long sequences, or video tensors [2504.15317][2502.18094][2201.06390].

| Attention Variant      | FLOPs                                 | Receptive Field                |
|-----------------------|---------------------------------------|--------------------------------|
| Global MSA            | $O(N^2 C)$                            | Global                         |
| Windowed (W-MSA)      | $O(N M^2 C)$                          | $M \times M$                   |
| Shifted Window (SW-MSA) | $O(N M^2 C)$                         | $3M \times 3M$, two layers     |
| Interleaved (IWA)     | $O(N M^2 C)$, plus conv $k^2 HWC$     | Single block, global if $kM\geq\max(H,W)$ |
| FwNet-ECA (FFT-based) | $O(N M^2 C) + O(N\log N)$             | Global (frequency-domain)      |

[2504.15317][2507.18405][2502.18094]

The shift and mask pattern ensures, both in vision and language contexts, that over two alternated blocks every token communicates with all its immediate window neighbors, yielding full grid connectivity in $d$ steps for $d$-dimensional signals.

## 3. Architectural Variants and Extensions

Several variants of windowed/shifted attention have addressed limitations or improved efficiency:

- **Multi-shifted windows:** Combine features learned at multiple window sizes and shifts in aggregation schemes—parallel, sequential, or cross-attention—to enhance multi-scale representation [2207.04403].
- **Context-aware or bottleneck fusion:** Patch merging at the bottleneck applies windowed/shifted attention on a spatially condensed map, injects global context, then upsamples, as in Context-aware Shifted Window Self-Attention (CSW-SA) [2401.13049].
- **3D windowed/shifted attention:** Extend partition/shift/mask operations to spatiotemporal blocks for video or medical volume data, including precise 3D relative positional bias [2201.06390][2401.13049].
- **Language modeling extensions:** In Shifted Cross Chunk Attention (SCCA), shifting is applied to keys/values rather than the raw token sequence, enabling approximate global receptive fields with minimal quadratic cost [2312.07305].
- **Non-standard window composition:** Interleaved Window Attention (IWA) rearranges (RTR) tokens so each window contains nonlocal, regularly interleaved positions, coupled with depthwise convolution to guarantee global information exchange in a single block, reducing required network depth and logic [2507.18405].

## 4. Hybridization with Other Contextualization Methods

Windowed/shifted attention has been combined with other mechanisms to further improve locality, efficiency, and global context modeling. Major trends include:

- **Convolutional fusion:** CoSwin fuses windowed/shifted attention outputs with parallel locally-enhanced features extracted via $3\times 3$ conv layers and learnable scalar weighting, restoring translation equivariant inductive biases especially useful on small-scale vision tasks [2509.08959].
- **Spectral (Fourier) enhancement:** FwNet-ECA applies post-attention FFT-based filter enhancement with learned frequency weights to globally couple all tokens, followed by light-weight efficient channel attention, establishing global receptive fields at a fraction of shifted window computational cost [2502.18094].
- **Dilated or cross-chunk patterns:** SCCA and Shifted Dilated Attention (SDA) for LLMs superimpose variable head-level chunk rotations and dilations, leveraging the parallelism of multihead attention to accumulate context from the entire sequence efficiently [2312.07305].

## 5. Implementation Details and Theoretical Guarantees

Attention within non-overlapping (or shifted/interleaved) windows is strictly local; masking is used to strictly prevent tokens from attending outside each window, except in mechanisms specifically designed for cross-window linking (shift, frequency-domain, chunk shift, etc.) [2504.15317][2312.02725][2312.07305]. Relative positional bias is crucial for maintaining order information and closing the gap to global attention in structured data [2504.15317][2207.04403].

Theoretical results guarantee that, with sufficient convolution kernel size or through strategic window/shift composition (e.g., RTR in Iwin), the effective receptive field can cover the entire input after a minimal number of blocks. Empirically, networks employing these designs match or exceed the accuracy and localization of convolutional or full-attention networks at dramatically reduced resource cost [2507.18405][2502.18094][2509.08959].

## 6. Applications and Empirical Performance

Windowed and shifted self-attention is used in:

- **Visual recognition and segmentation:** DR classification (APTOS/IDRiD: 89.65%/97.40% accuracy) [2504.15317], scene segmentation with multi-shifted windows outperforming convolutional baselines [2207.04403], small-image benchmarks (CIFAR-10: 2.17% CoSwin gain over Swin) [2509.08959].
- **3D medical segmentation:** CIS-UNet’s context-aware window attention achieves superior aortic branch segmentation (mean Dice: 0.713 vs 0.697 for conventional SwinUNetR) [2401.13049].
- **3D reconstruction:** R3D-SWIN matches or surpasses prior SOTA on ShapeNet, using pure shifted window attention in the encoder [2312.02725].
- **Long-context language models:** SCCA extends LLaMA-2-7B from 4k to 8k sequence context on a single V100, with SCCA-fixed pattern outperforming prior S$^2$ LongLora by 0.24 perplexity at 8k on PG19 (9.17 vs 9.41) [2312.07305].
- **High-throughput document retrieval:** Local self-attention over partial-overlap windows retains retrieval quality on tens-of-thousands token documents at linear scaling [2005.04908].

## 7. Limitations and Design Trade-Offs

While windowed and shifted attention efficiently captures local and mid-range context, it may require careful hyperparameter tuning (window size, shift, mask logic) to avoid underutilizing context. Masking introduces additional complexity in implementation. In frequency- and interleaved-domain hybrids, global context is not spatially adaptive, which may affect boundary fidelity [2502.18094][2507.18405]. Empirical results confirm, however, that most vision/language tasks see either improved or parity performance with sharply reduced resource requirements compared to vanilla global attention [2504.15317][2502.18094][2509.08959][2312.07305].

A plausible implication is that windowed and shifted/alternating patterns are likely to remain central to scalable self-attention in domains requiring both high resolution and efficient global-local context exchange. Variants leveraging interleaving, frequency domain coupling, or convolutional fusion further expand possible design spaces for upcoming transformer architectures.

Source: https://www.emergentmind.com/topics/windowed-and-shifted-self-attention