Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction

  1. 1Attention Before Transformers — Score, Squash, Multiply
  2. 2Query, Key, Value — Dot-Product Attention from Scratch
  3. 3Attention Is All You Need — The Transformer, Block by Block
  4. 4Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks
  5. 5DETR — Detection as Set Prediction, No Anchors and No NMS
  6. 6An Image Is Worth 16×16 Words — ViT, Patch by Patch
  7. 7DeiT — Training a Vision Transformer on ImageNet Alone
  8. 8Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction
  9. 9Convolutions Strike Back — Was It the Attention or the Recipe?
  10. 10Masked Autoencoders — Hide 75 %, Encode the Rest
  11. 11CLIP — Learning Pictures From the Words Next to Them
  12. 12Vision Transformers Today — May 2022 to October 2026, Year by Year

Summary

How Swin turns a ViT into a general-purpose backbone: attention inside 7×7 windows that shift every other layer, a cyclic trick that keeps the shift free, a 169-number position bias, and patch merging that builds the pyramid detectors want. After this you will be able to count the FLOPs yourself and know where the linear complexity does and does not apply.

Table of Contents

  1. 1. The Hierarchy: Patch Partition and Patch Merging2
  2. 2. Window Attention and the Cost of Looking Everywhere3
  3. 3. Shifted Windows3
  4. 4. Relative Position Bias4
  5. 5. Training4
  6. 6. Results4
  7. 7. What Came Next4
  8. 8. References4
  9. 9. Connections4
TrivediInformational[Page 1]
S. TrivediOct 2026

ViT and DeiT classify well, but they hand you one feature map at 1/16 of the input and pay for attention with the square of the number of tokens. A detector or a segmenter wants a pyramid of maps at strides 4, 8, 16 and 32, like a convnet gives it, at resolutions where a global ViT is simply not runnable. Swin's paper names the two things that separate pixels from words: visual entities "can vary substantially in scale", and images have "much higher resolution of pixels … compared to words in passages of text" [LIU21].

Swin's fix can be stated in three steps:

  1. Start small and merge. 4×4-pixel patches instead of 16×16, and between stages, merge every 2×2 group of tokens into one. Four stages, four resolutions, a pyramid.
  2. Attend inside windows. Self-attention runs within non-overlapping M×M windows (M = 7), so the cost grows linearly with the number of tokens instead of quadratically.
  3. Shift the windows every other layer so information crosses window borders, and do the shift with a cyclic roll and a mask so it costs nothing extra.
H×W×3imageStage 1LinearEmbeddingSwin Block× 24 × 4 patchpartitionH/4 × W/4 × C56² × 96Stage 2PatchMergingSwin Block× 2H/8 × W/8 × 2C28² × 192Stage 3PatchMergingSwin Block× 6H/16 × W/16 × 4C14² × 384Stage 4PatchMergingSwin Block× 2H/32 × W/32 × 8C7² × 768tokens ÷ 4, channels × 2 at every merge; Swin-T numbers under each map
Fig. 1 Swin-T in one picture. A 4×4 patch partition and a linear embedding give H/4 × W/4 tokens of C channels; each later stage starts with patch merging, which quarters the tokens and doubles the channels. Under each stage, the map it outputs for a 224 px image.

Brush up before you start ;)

1. The Hierarchy: Patch Partition and Patch Merging

  • Patch partition (§3.1 of the paper). Each token is a 4×4 patch, so 4 × 4 × 3 = 48 raw values, linearly embedded to C channels. At 224 px, stage 1 works on 56 × 56 = 3136 tokens; ViT-B at the same resolution has 196 [DOSO20].
  • Patch merging sits at the start of stages 2, 3 and 4. Concatenate each group of 2×2 neighbouring tokens, LayerNorm, then a linear layer from 4C to 2C:
z∈ℝh×w×C→concat 2×2ℝh/2×w/2×4C→LN,W∈ℝ4C×2Cℝh/2×w/2×2C(1)
  • Reading Eq. 1: tokens ÷ 4, resolution ÷ 2, channels × 2. It is the space-to-depth move, four neighbouring pixels stacked into one thicker pixel, followed by a projection that keeps only half of the stacked channels. Nothing else in the network changes resolution.
  • The output sizes at 224 px are therefore 56², 28², 14² and 7², which is exactly the stride-4/8/16/32 pyramid an FPN plugs into [LIU21].

NOTE: "hierarchical" means resolution, not depth. ViT-L is deeper than Swin-T and still produces one 14×14 map. Patch merging, not more layers, is what makes Swin hierarchical.

The four sizes, all with head dimension d = 32 (the d_k of Query, Key, Value — Dot-Product Attention from Scratch), MLP ratio 4 and M = 7 (§3.3 and Table 7 of the paper) [LIU21]:

ModelC per stageBlocksHeadsParamsFLOPs, 224 px
Swin-T96, 192, 384, 7682, 2, 6, 23, 6, 12, 2429M4.5G
Swin-S96, 192, 384, 7682, 2, 18, 23, 6, 12, 2450M8.7G
Swin-B128, 256, 512, 10242, 2, 18, 24, 8, 16, 3288M15.4G
Swin-L192, 384, 768, 15362, 2, 18, 26, 12, 24, 48197M103.9G at 384 px

Table. 1 The Swin family (§3.3 and Tables 1 and 7 of [LIU21]). Swin-T is sized like ResNet-50 and DeiT-S, Swin-S like ResNet-101, Swin-B like ViT-B / DeiT-B.

Two more things that are not there: no class token (the stage-4 output is average-pooled, then a linear classifier, "as accurate as an additional class token", Appendix A2.1), and no absolute position embedding; §5 explains what replaces it.

TrivediInformational[Page 1]
S. TrivediOct 2026

2. Window Attention and the Cost of Looking Everywhere

Let's devise the cost equations step-by-step. Symbols: h and w are the token map's height and width (not pixels), C the channels (the D of An Image Is Worth 16×16 Words — ViT, Patch by Patch), M the window side in tokens (the k of Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks). The paper counts multiply-adds and leaves softmax out (footnote 3).

2.1 Global Attention, Eq. 1 of the Paper

Every attention layer does two kinds of work: four C×C projections (Q, K, V and the output) applied to every one of the hw tokens, and then QK⊤ and AV, each hw × hw × C.

Ω(MSA)=4hwC2⏟projections+2(hw)2C⏟QK⊤ and AV(2)
  • The first term is linear in the number of tokens. The second is the problem: double the image side, four times the tokens, sixteen times the attention cost.

2.2 Window Attention, Eq. 2 of the Paper

Split the token map into non-overlapping M×M windows (padding at the bottom and right if hw is not divisible, footnote 4) and run ordinary multi-head self-attention inside each window only. There are hw/M² windows, each costing 2(M²)²C for its attention terms:

Ω(W-MSA)=4hwC2+2M2hwC(3)
  • Compare the second terms of Eq. 2 and Eq. 3: (hw)2 became M2hw. With M fixed, the whole thing is linear in the number of tokens. The projection term is identical in both.

Stage 1 of Swin-T at 224 px: h = w = 56, C = 96, M = 7.

  • Projections: 4 · 3136 · 96² = 115.6 M multiply-adds, in both versions.
  • Global attention term: 2 · 3136² · 96 = 1,888.2 M. Total Ω(MSA) ≈ 2.00 G per layer.
  • Window attention term: 2 · 49 · 3136 · 96 = 29.5 M. Total Ω(W-MSA) ≈ 0.145 G per layer.
  • The attention terms differ by hw / M² = 3136 / 49 = 64×; the whole layer by 13.8×.

Now slide the input side up in the figure, and try a bigger M while you are there:

2247
Global attention against window attention, stage 1. Multiply-adds of one attention layer in stage 1 of Swin-T (C = 96, 4 × 4-pixel tokens) as the input side grows, on a log scale. The dotted line is the Q, K, V and output projections, which both versions pay. At 224 px, Eq. 1 and Eq. 2 of the paper give 2.00 G against 0.145 G per layer.
  • Push the input side up and the gap explodes: per stage-1 layer, global attention costs 2.0 / 16.7 / 127 / 2017 G at 224 / 384 / 640 / 1280 px, the windowed version 0.145 / 0.43 / 1.19 / 4.74 G. At detection resolutions the global version is not an option.
  • The same arithmetic per stage at 224 px, attention term only: 64× at stage 1, 16× at stage 2, 4× at stage 3, and 1× at stage 4, where the 7×7 map is one window and W-MSA is global MSA. The win is in the early stages and at high resolution.
  • You can see it in whole networks too: Swin-B goes from 15.4 G at 224 px to 47.0 G at 384 px (×3.05 for ×2.94 the tokens, linear), DeiT-B from 17.5 G to 55.4 G (×3.17, the quadratic part showing) [LIU21].

3. Shifted Windows

Windows alone have an obvious hole: a token never hears from anything outside its 7×7 square. The paper's fix is to move the partition between consecutive layers so that the new windows "bridge the windows of the preceding layer, providing connections among them" [LIU21].

3.1 The Block Pair, Eq. 3 of the Paper

Blocks always come in pairs. The first uses the regular partition (W-MSA); the second moves the partition by (⌊M/2⌋, ⌊M/2⌋) tokens, 3 for M = 7, and attends inside the shifted windows (SW-MSA).

z^l=W-MSA(LN(zl−1))+zl−1zl=MLP(LN(z^l))+z^lz^l+1=SW-MSA(LN(zl))+zlzl+1=MLP(LN(z^l+1))+z^l+1(4)
  • zl∈ℝh×w×C is the output of block l and z^l the output of its attention sub-layer. MLP is two layers with GELU and 4× expansion, pre-norm, residual after each module: the transformer block with MSA swapped for (S)W-MSA and nothing else touched.
  • Because blocks come in pairs, every stage depth in Table 1 is even: {2, 2, 6, 2}, {2, 2, 18, 2}.
block lz^(l−1)LNW-MSALNMLPz^lblock l + 1z^lLNSW-MSALNMLPz^(l+1)the same block,windows movedby (M/2, M/2)pre-norm, residual aftereach module: a ViT blockwith MSA → (S)W-MSA
Fig. 2 Two consecutive Swin blocks (Eq. 4 here, Eq. 3 of the paper). Only the window partition differs between them. After the pair, every token has exchanged information with the tokens of its neighbouring windows.

3.2 Why Not Just Shift? The Cyclic Trick

Shifting the partition naively creates a problem the figure below makes visible: an h/M × w/M grid of windows becomes (⌈h/M⌉ + 1) × (⌈w/M⌉ + 1), and the ones along the edges are smaller than M×M. Padding them back up to M×M costs real compute: the paper's 2×2 → 3×3 example is 2.25× [LIU21].

(a) layer l: regular windows8 × 8 tokens, M = 4: 4 windows(b) layer l + 1: shifted by (2, 2)9 windows, only the middle one is M × Mshift(c) same thing, rolled up-left by 24 windows again; A, B, C wrapped aroundBCArollA, B, C came from the far edges:mask their logits inside the window,then roll backevery query in a windowshares one key setpadding these to M × Mcosts 9/4 = 2.25× the attention
Fig. 3 The paper's Fig. 2 and Fig. 4 on an 8×8 map with M = 4. (a) Four regular windows. (b) The partition shifted by (2, 2): nine windows, and only the middle one is still 4×4. (c) The map rolled up and left by 2: four full windows again, but the strips A, B and C came from the far edges, so inside those windows the logits between different pieces are masked.

The paper's answer is a cyclic shift: roll the whole token map toward the top-left by s = ⌊M/2⌋, apply the regular partition, and roll back afterwards. The wrapped-around strips land in the last row and column of windows, which now hold pieces from opposite sides of the image. A mask stops them from attending to each other:

s=⌊M/2⌋,z′=roll(z,(−s,−s))maskij={0same piece−∞ (a large negative)different pieceslogitsij←logitsij+maskijz←roll(z″,(+s,+s))(5)
  • The mask is the −∞-before-the-softmax trick of the decoder in Attention Is All You Need — The Transformer, Block by Block, so the masked pairs simply do not exist for that layer. The result is identical to the shifted partition; the cost is the regular partition's.
  • The window count stays ⌈h/M⌉ × ⌈w/M⌉: 64 at stage 1 instead of 81 (+27 %) [LIU21].

Move the shift s in the figure: at s = 0 the middle panel is the left one; at s = 2 count the middle panel's windows against the right one, and the last readout says how many of the rolled windows' logits the mask throws away.

82
Windows, shifted windows, and the cyclic shift. Left: the regular partition. Middle: the same map with the partition moved by s tokens, which leaves small windows all round the edges. Right: the map rolled up and left by s so the regular partition fits again; the darker strips are tokens that wrapped around, and the mask stops them from talking to their new neighbours. Table 4: the shift is worth +1.1 top-1 and +2.8 box AP; Table 5: cyclic 3.0 ms against padding 3.3 ms in stage 1.

NOTE: the cyclic shift needs the mask. Without it, a token on the bottom edge of the image would attend to a token from the top edge as if they were neighbours, and "cyclic shift" would no longer equal "shifted partition" in result, only in cost.

3.3 Shifted Is Not Sliding

The sliding-window attention of Attention Meets Pixels — Windows, Axes and the First Fully Attentional Networks gives every query its own neighbourhood. That is cheap on paper and slow on hardware, because every query has a different key set. In Swin, "all query patches within a window share the same key set, which facilitates memory access in hardware" [LIU21]; the partition moves once per layer, not the neighbourhood. The paper measured it (Table 5, V100, milliseconds per attention layer in stages 1 to 4, and images per second for Swin-T / S / B) [LIU21]:

AttentionStage 1Stage 2Stage 3Stage 4Swin-TSwin-SSwin-B
sliding window, naive122.538.312.17.618310977
sliding window, kernel7.64.72.71.8488283187
windows, no shift2.81.71.20.9770444280
shifted, with padding3.32.31.92.2670371236
shifted, cyclic3.01.91.31.0755437278

Table. 2 Speed of the attention variants (Table 5 of [LIU21]): ms per layer per stage, then whole-network images per second on a V100.

  • Shifting costs almost nothing: 2.8 → 3.0 ms in stage 1, 770 → 755 im/s for Swin-T.
  • Cyclic beats padding by 13 / 18 / 18 % throughput for T / S / B.
  • Against the naive sliding window, the cyclic shifted window is 40.8× faster in stage 1 (122.5 vs 3.0 ms) and 4.1× faster as a network; 2.5× and 1.5× against the hand-written kernel. And Table 6 of the paper shows the sliding-window Swin-T is no more accurate: 81.4 top-1 / 50.2 box AP / 45.8 mIoU against 81.3 / 50.5 / 46.1 for shifted windows [LIU21]. Same accuracy, a quarter of the time.
TrivediInformational[Page 2]
S. TrivediOct 2026

4. Relative Position Bias

Swin drops ViT's absolute position embedding and instead teaches each head how much to favour each offset. The attention inside one window, Eq. 4 of the paper, is the usual scaled dot-product with one addition:

Attention(Q,K,V)=SoftMax(QK⊤d+B)VQ,K,V∈ℝM2×d,B∈ℝM2×M2Bij=B^[Δyij+M−1,Δxij+M−1],B^∈ℝ(2M−1)×(2M−1)(6)
  • Bij is a scalar added to the logit between query i and key j, and it depends only on their offset (Δy,Δx)∈[−M+1,M−1]2. The same offset gets the same bonus everywhere in the window: a convolution-like prior, learned.
  • It is gathered from a small table B^ of (2M − 1)² entries per head, and it is added inside the softmax, which is why it is a bias and not an embedding.

How small? For M = 7, B^ is 13 × 13 = 169 numbers per head, where a full M² × M² table would need 2,401. Swin-T has 3·2 + 6·2 + 12·6 + 24·2 = 138 head-layers, so 23,322 parameters in total, 0.08 % of 29M; Swin-B has 376 head-layers and 63,544. For that, Table 4 of the paper gives (Swin-T, top-1 / COCO box AP / ADE20K mIoU) [LIU21]:

  • No position encoding: 80.1 / 49.2 / 43.8.
  • Absolute embedding (ViT's): 80.5 / 49.0 / 43.2.
  • Absolute + relative: 81.3 / 50.2 / 44.0.
  • Relative bias only: 81.3 / 50.5 / 46.1.

So the bias is worth +1.2 top-1 and +2.3 mIoU over nothing, and adding ViT's absolute embedding on top hurts the dense tasks (−0.3 AP, −2.1 mIoU). The paper's conclusion: "inductive bias that encourages certain translation invariance is still preferable for general-purpose visual modeling" [LIU21]. Taking the dot product away and keeping only the bias gives 79.3, so the content term still matters. The table can also be bicubically interpolated to a new window size at fine-tuning time.

The same Table 4 prices the shift itself: without it Swin-T scores 80.2 / 47.7 / 43.3, with it 81.3 / 50.5 / 46.1, i.e. +1.1 top-1, +2.8 box AP, +2.8 mIoU for 0.2 ms per stage-1 layer [LIU21].

5. Training

Swin trains with the DeiT recipe, same optimizer, schedule, batch and augmentations, with four edits: 20 warm-up epochs instead of 5, gradient clipping back on (max norm 1), stochastic depth raised to 0.2 / 0.3 / 0.5 for T / S / B, and no repeated augmentation or EMA, "which do not enhance performance" in Swin's hands, contrary to what DeiT found [LIU21].

6. Results

6.1 ImageNet-1k

ModelRes.ParamsFLOPsim/sTop-1
DeiT-S22422M4.6G94079.8
Swin-T22429M4.5G75581.3
Swin-S22450M8.7G43783.0
DeiT-B22486M17.5G29281.8
Swin-B22488M15.4G27883.5
DeiT-B↑38438486M55.4G8683.1
Swin-B↑38438488M47.0G8584.5
RegNetY-16G22484M16.0G33582.9
EfficientNet-B760066M37.0G5584.3

Table. 3 ImageNet-1k only, no distillation; every column from Table 1a of [LIU21], V100 throughput included. Swin-B at 224 px is 83.5 in Table 1 and 83.3 in the paper's text and Table 8, a mismatch between the paper's versions; I quote the table.

  • At equal cost: Swin-T 81.3 vs DeiT-S 79.8 (+1.5 at 4.5 vs 4.6 GFLOPs); Swin-B 83.5 vs DeiT-B 81.8 at fewer FLOPs (15.4 vs 17.5).
  • With ImageNet-22k pre-training (Table 1b of the paper): Swin-B↑384 86.4 against ViT-L/16's 85.2 at 27 im/s, and Swin-L↑384 87.3 [LIU21].

6.2 Where the Pyramid Pays: COCO and ADE20K

SettingBackboneBox APMask APFPS
Cascade Mask R-CNNResNet-50 → Swin-T46.3 → 50.5
ATSSResNet-50 → Swin-T43.5 → 47.2
RepPointsV2ResNet-50 → Swin-T46.5 → 50.0
Sparse R-CNNResNet-50 → Swin-T44.5 → 47.9
Cascade Mask R-CNNDeiT-S (deconv pyramid)48.041.410.4
Cascade Mask R-CNNSwin-T50.543.715.3
Cascade Mask R-CNNResNeXt-101-64x4d48.341.7
Cascade Mask R-CNNSwin-B51.945.0
HTC++, test-devSwin-L (22k)58.751.1

Table. 4 COCO detection (Table 2 of [LIU21]). Compare only within a row group; the frameworks differ between groups.

  • Dropping Swin-T in for ResNet-50 is worth +3.4 to +4.2 box AP across four different detectors.
  • Against DeiT-S with a pyramid bolted on by deconvolution, Swin-T is +2.5 box AP, +2.3 mask AP and 47 % faster: the hierarchy is cheaper than faking one.
  • On ADE20K with UperNet (Table 3 of the paper): Swin-T 46.1 and Swin-S 49.3 mIoU against DeiT-S 44.0 and ResNet-101 44.9; Swin-L pre-trained on 22k reaches 53.5 [LIU21].

7. What Came Next

Swin made the transformer a drop-in backbone, and it did so by importing convnet ideas: a stem of small patches, four stages with downsampling between them, a 7×7 neighbourhood, a translation-friendly position prior. The next post, Convolutions Strike Back — Was It the Attention or the Recipe?, follows that traffic in both directions. PVT, CvT and CoAtNet put pyramids and convolutions back into attention networks, and ConvNeXt takes Swin's macro design, its 7×7 kernel, its LayerNorm and its DeiT recipe, removes the attention entirely, and scores 82.1 against Swin-T's 81.3. Which raises the question that post is really about: how much of this was the attention, and how much was everything around it?

8. References

  1. [LIU21] Liu, Lin, Cao, Hu, Wei, Zhang, Lin, Guo. Swin Transformer: Hierarchical Vision Transformer using Shifted Windows. arXiv:2103.14030.
  2. [DOSO20] Dosovitskiy et al. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. arXiv:2010.11929.
  3. [TOUV21] Touvron, Cord, Douze, Massa, Sablayrolles, Jégou. Training data-efficient image transformers & distillation through attention. arXiv:2012.12877.

9. Connections

What this page builds on, cites or links to, and what points back at it. A plain box is a “builds on”; “cited” and “linked in the text” are the lighter links. Hover a box for a preview.

POINTS AT THIS PAGEREADConvolutions Strike Back — Was It theAttention or the Recipe?READVision Transformers Today — May 2022 toOctober 2026, Year by YearREAD · LINKED IN THE TEXTAttention Meets Pixels — Windows, Axesand the First Fully Attentional NetworksPAPER · LINKED IN THE TEXTDETR — Detection as Set Prediction, NoAnchors and No NMSPAPER · LINKED IN THE TEXTAn Image Is Worth 16×16 Words — ViT,Patch by PatchPAPER · LINKED IN THE TEXTDeiT — Training a Vision Transformer onImageNet AloneThis pageTHIS PAGE POINTS ATREADAttention Meets Pixels — Windows, Axesand the First Fully Attentional NetworksPAPERAn Image Is Worth 16×16 Words — ViT,Patch by PatchPAPERDeiT — Training a Vision Transformer onImageNet AloneREAD · LINKED IN THE TEXTObject Detection — A Quick ReadREAD · LINKED IN THE TEXTQuery, Key, Value — Dot-ProductAttention from ScratchREAD · LINKED IN THE TEXTConvolutions Strike Back — Was It theAttention or the Recipe?PAPER · LINKED IN THE TEXTAttention Is All You Need — TheTransformer, Block by Block

Related

Go deeper

  1. Start with
    Read
  2. Paper
  3. Paper
  4. Paper
  5. Paper
  6. Read
  7. Read
  8. Paper
  9. Read
  10. Paper
  11. Paper
  12. Paper
  13. Paper
  14. You are here
    Swin Transformer — Windows, Shifts and a Pyramid for Dense Prediction
    Paper
  15. Then
    Read
  16. Paper
  17. Paper
  18. Read
  19. Paper

Go broader

<- Older [Page 4] Newer ->