TL;DR
Shifted window attention is a way to run a Transformer over an image without every patch looking at every other patch. The image's patch grid is cut into small square windows, and each patch attends only to the patches in its own window. In the next layer the window grid is moved by half a window, so patches that were separated by a window edge now share one.
The result is that the cost of attention grows in proportion to the number of patches, not with the square of it, while information still travels across the whole image as the layers stack up.
Builds on: Multi-Head Attention, Vision Transformer (ViT) · Leads to: the Swin Transformer backbone, used for classification, detection and segmentation
Where it's used
Image classification backbones. The Swin Transformer (Liu and colleagues, 2021) uses shifted windows in every stage of its network and reached strong accuracy on ImageNet classification. Because the backbone is a general feature extractor, the same network was then reused for other tasks.
Object detection and segmentation. The Swin paper reports results on COCO object detection and ADE20K semantic segmentation. These tasks need dense, high-resolution feature maps, where a global attention over all patches would be expensive, so the local windows are what make the model practical.
High-resolution images. A 448 by 448 image split into 4 by 4 pixel patches has patches. Global attention would compare every patch with every other, which is over 157 million pairs for one attention map. Windows cut that to about 615 thousand pairs, as the worked examples show.
How it works
Here is one tiny instance. Take a grid of patches, numbered by row and column from to . Use windows of size .
- Layer one: cut the grid into four windows. The top-left window holds , and the other three windows are the same size. Each patch attends only to the patches in its own window, itself included. Check: each of the 16 patches attends to 4 tokens, not 16.
- Count the work for layer one. Global attention scores all 16 patches against all 16, which is pairs. Windowed attention scores 4 windows, each with pairs, which is 64 pairs. Check: , which is 1 divided by the 4 windows.
- Layer two: shift the window grid by one row and one column. The new windows now cover the centre of the old boundaries. One of them holds . Check: and were in different windows in layer one, and they now share a window.
- Wrap-around and masks: patches on the far edges that the shift brings together are not really neighbours. The implementation moves the grid cyclically and then sets the attention scores between patches that should not see each other to a very negative value, so they get zero weight after the Softmax.
- Information flow: in layer one, patch sends information to . In layer two, sends information to . So after two layers, has influenced , a patch it could never reach in one layer.
flowchart LR L1[Layer 1: windows at the grid origin] --> L2[Layer 2: windows shifted by half a window] L2 --> L3[Layer 3: back to the origin grid] L3 --> L4[Layer 4: shifted again]
The math
Let the feature map be an grid of tokens, each a vector of width . Partition the grid into non-overlapping windows. Within one window with tokens , compute and apply it to every window separately. Tokens in different windows never interact within one layer.
Step 1 in symbols
There are windows, each with tokens. Each window computes an score matrix, the matrix product of and . The total number of pairs scored is Global attention scores pairs. The ratio of global to windowed pairs is . In the tiny example and , so the ratio is , and the windowed count is against .
Step 2 in symbols: the shift
In the shifted layer the grid is rolled cyclically by . For that is . The windows are then applied to the rolled grid, which makes them straddle the old boundaries. The masking step sets the scores between patches that came from opposite edges of the grid to before the softmax.
Step 3 in symbols: how far information travels
Stack layers that alternate regular and shifted windows. Each pair of layers lets information move across roughly one window boundary in each direction. The receptive field therefore grows with depth, while every single layer stays local. In the tiny example, two layers were enough for patch to reach .
Worked examples
Setup: , , so tokens and tokens per window.
- Number of windows: .
- Pairs per window: .
- Total windowed pairs: .
- Global pairs: .
- Ratio: , which matches .
- Interpretation: windowing saved a factor of 4 here. The saving is larger on bigger grids, because the global count grows with the square of the number of tokens.
Setup: a image with patches gives a grid of , so tokens. The window size is , so each window has tokens.
- Number of windows: .
- Global pairs: .
- Windowed pairs: .
- Ratio: . Check: .
- Interpretation: the ratio equals the number of windows, because each window is of the grid. The windowed layer does about 64 times less attention work than a global layer on this image.
Setup: dense prediction such as segmentation needs a larger input, here with patches. The grid is , so tokens. The window size is .
- Global pairs per attention map: .
- Windowed pairs per attention map: .
- Ratio: . Check: .
- Alternatively, the ratio is .
- Interpretation: doubling the side length of the image quadruples the number of tokens, so global attention cost goes up sixteen-fold. Windowed cost goes up only four-fold. For a dense task the difference decides whether the model fits in memory.
Try it: the same model on an image with patches and . How many times fewer pair scores does windowed attention compute than global attention, for one attention map? Answer: the grid is , so . The ratio is .
Common mistakes and tradeoffs
It is tempting to think that a windowed layer can see the whole image. In fact it sees only its own window. Global information arrives only after several layers, which is why the window grid is shifted in alternate layers and why the network is built as a stack.
It is tempting to think that every layer should use the shifted grid. In fact the regular and shifted layers alternate. The pair of grids is what lets a patch exchange information with neighbours on both sides of a window edge; a stack of shifted layers alone would repeat the same boundary pattern, so the design uses the two grids in turn.
It is tempting to think that the cyclic shift can be implemented without masks. In fact the shift wraps patches from opposite edges into the same window. Without masks those patches would attend to each other, which is wrong. The masks make the wrap-around invisible to the attention.
Use shifted windows when the input is large, the task needs dense outputs, and you want cost that grows linearly with the number of patches. Do not use them when a single layer must relate patches that are far apart, such as a short global summary of a small image; then global attention is simpler, and for small inputs its cost is acceptable. The window size is also a design choice: a bigger sees more in each layer but costs more.
History
Transformers were first built for text, where every token attends to every other. The Vision Transformer (ViT) (Dosovitskiy and colleagues, 2020) showed that the same design works on images if the image is cut into patches and each patch is treated as a token. That model used global attention, so its cost grew with the square of the number of patches, which is costly at high resolution.
Swin (Liu and colleagues, 2021) answered this problem with local windows and a shift between layers. It also merged neighbouring patches between stages to build a hierarchy, similar to the feature maps of convolutional networks. The shifted window idea caught on because it kept the flexibility of attention while making dense image tasks practical.
Recap
- A window is a small square of patches; attention inside it costs far less than attention over the whole image.
- Shifting the window grid by half a window in alternate layers lets information cross window boundaries.
- Masks are needed because the shift wraps patches from opposite edges into one window.
- For a grid of tokens and window size , the saving over global attention is .
- Read next: Sliding Window Attention, which uses a similar local idea for text, and the Swin Transformer, where the idea is used as a full backbone.
Check yourself
QuestionIn one windowed layer, which other tokens can a token attend to?Show answer
Only the tokens in its own window, itself included. Tokens in other windows are invisible in that layer, so information must cross window boundaries over several layers.
QuestionA grid has 36 tokens and windows of size 3 by 3. How many tokens are in each window, and how many windows are there?Show answer
Each window holds tokens. There are windows, arranged as a grid of windows.
QuestionFor the grid in the previous question, how many pair scores does one windowed layer compute, compared with a global layer?Show answer
Windowed: windows, each with pairs, giving . Global: . The ratio is , which equals .
QuestionWhy does the shifted layer need attention masks, and what goes wrong without them?Show answer
The cyclic shift places patches from opposite edges of the grid into the same window. They are not neighbours in the image, so letting them attend to each other would mix unrelated regions. The masks set those scores to a very negative value so the softmax gives them zero weight.
QuestionA detection model takes 800 by 800 images with 4 by 4 patches and uses 7 by 7 windows. Roughly how many times fewer pair scores does one windowed layer compute than one global layer?Show answer
The grid is , so tokens. The ratio is , so windowed attention computes about 816 times fewer pair scores.