Models · Classification

Swin Transformer

Swin Transformer (Liu et al. 2021): a vision transformer with shifted-window attention.

stable Fork of huggingface/pytorch-image-models@0df212b · backends: portable, kernels

Checkpoint swin_base_patch4_window7_224.ms_in22k_ft_in1k

Paper 2021 Added in 0.1.0 Evaluated with 0.2.0

Kernels, RTX 5060 Ti 16 GB 2.16 ms 224x224, fp16
vs the faster original 6.9× original fp32: 15.0 ms
Peak memory 218 MiB original: 389 MiB
Accuracy vs original fp32 −0.04 kernels top-1 %, ImageNet-1k val (86.28 original)

Use it

pip install fastforks

Lean install with only what this model needs: pip install "fastforks-core[timm]"

from fastforks import classification

model = classification.load("swin_base_patch4_window7_224.ms_in22k_ft_in1k")  # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.logits
result.backend  # what ran: mode, device, dtype, kernels
fastforks bench swin_base_patch4_window7_224.ms_in22k_ft_in1k

Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.

Speed and memory

Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.

Input: synthetic, 224x224 (the task’s standard size)

BackenddtypeTime per call (ms)vs original fp32Peak memory
allocated / reserved
Load (s)First call (s)
Original fp32fp32 15.0–389 MiB/416 MiB0.90.06
Original fp16torch.autocast(fp16) around the forwardfp16 19.10.78×538 MiB/550 MiB2.70.41
Portable fp32fp32not measured ––––
Portablefp16 10.81.4×211 MiB/230 MiB2.00.14
Kernelsfp16 2.166.9×218 MiB/238 MiB3.00.09
Portable + torch.compile 4.193.6×243 MiB/270 MiB27 (build)–
TensorRT fp16fp16 1.4810×202 MiBenginenot comparable64 (build)–

Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-03 to 2026-11-16. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.

Other devices

DeviceBackenddtypems / callPeak memoryOutput vs referenceInput
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp3237.5 2,062 MiB processmatches224x224
Portablefp1629.6 2,291 MiB processmatches224x224
Portablefp3228.2 2,273 MiB processmatches224x224
Kernelsfp16not implemented–224x224
NVIDIA L4 CUDA, torch 2.14.1+cu130, 2026-11-07Portablefp1619.5 187 MiB/210 MiB–224x224
Kernelsfp162.92 198 MiB/258 MiB–224x224
NVIDIA L40S CUDA, torch 2.14.1+cu130, 2026-11-07Portablefp1612.6 187 MiB/210 MiB–224x224
Kernelsfp162.02 198 MiB/258 MiB–224x224
NVIDIA A10 CUDA, torch 2.14.1+cu130, 2026-11-07Portablefp1611.7 187 MiB/210 MiB–224x224
Kernelsfp162.80 198 MiB/258 MiB–224x224
NVIDIA A100-SXM4-40GB CUDA, torch 2.14.1+cu130, 2026-11-08Portablefp1612.8 187 MiB/210 MiB–224x224
Kernelsfp163.79 198 MiB/258 MiB–224x224
NVIDIA RTX PRO 6000 CUDA, torch 2.14.1+cu130, 2026-11-08Portablefp164.06 211 MiB/230 MiB–224x224
Kernelsfp161.91 246 MiB/298 MiB–224x224

CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.

Licence and weights

Commercial use allowed

Code
Apache-2.0
Weights
MIT source
Training data

Our reading of the licences, not legal advice.

Weights

swin_base_patch4_window7_224.ms_in22k_ft_in1k.safetensors 353 MB· hosted on Hugging Face

fastforks downloads and checks these files on first use (fastforks download swin_base_patch4_window7_224.ms_in22k_ft_in1k).

Accuracy

The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.

ImageNet-1k val

5000 random images, top-1 / top-5 %

Rundtypetop-1 % ↑top-5 % ↑Δ top-1 % vs fp32 originalimagesms / image
Original fp32fp3286.2897.82 reference 5000 4.51
Original fp16fp1686.3097.82 +0.02 5000 2.23
Portable fp32fp3286.2097.86 −0.08 5000 4.41
Portablefp1686.2497.86 −0.04 5000 1.38
Kernelsfp1686.2497.86 −0.04 5000 1.04

fastforks: the model's own resize / crop on the GPU; original: the library's pretrained loader and its PIL eval transform. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-06; portable fp32: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 7c91993, 2026-10-31; portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 939adfe8, 2026-11-06.

Closeness to the original

Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.08 (absolute error of the output logits).

CheckToleranceMeasured
CPU fp32, max |diff|≤ 0.002 not recorded
GPU default dtypes, mean |diff|≤ 0.02 0.00178
GPU default dtypes, p99 |diff|≤ 0.08 0.00688

Recorded for swin_base_patch4_window7_224.ms_in22k_ft_in1k on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-04, with these kernels: cudnn_haccfvm_convfvm_extrafvm_mbfvm_r3

Details

Swin Transformer (Liu et al. 2021): shifted-window attention in a four-stage hierarchy.

Input / output
Input: one RGB image. Output: ImageNet-1k class logits.
Input size
fixed 224x224: the input is resized (short side 248) and centre-cropped to it; tested up to 224x224 (218 MiB allocated)
Outputs
logits
Checkpoint
arch="swin_base_patch4_window7_224.ms_in22k_ft_in1k"
CUDA kernels
  • cudnn_haccconvolutions with fp16 accumulation (cuDNN)
  • fvm_convfused convolutions for image classifiers
  • fvm_extrafused transformer row operations (rotary split, SwiGLU with LayerNorm)
  • fvm_mbfused MBConv blocks (MobileNet and EfficientNet families)
  • fvm_r3further fused classifier blocks
Apple (MPS)
fp32, fp16
Full reference, generated from the repository

Swin Transformer (Liu et al. 2021): a vision transformer with shifted-window attention.

Commercial use: yes.

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
swin_base_patch4_window7_224.ms_in22k_ft_in1ksynthetic (224x224)original (fp32)
original
portable
kernels
portable + torch.compile
TensorRT fp16
15.0
19.1
10.8
2.16
4.19
1.48
389 / 416
538 / 550
211 / 230
218 / 238
243 / 270
404 / 432
Details
  • Original code: https://github.com/huggingface/pytorch-image-models at 0df212b369a5
  • Code licence: Apache-2.0 (permissive); files:
  • Weights licence: MIT
  • Licence evidence: https://huggingface.co/timm/swin_base_patch4_window7_224.ms_in22k_ft_in1k/blob/a6a1eb2321b4f556fa0fa243fb777d47679f13c9/README.md
  • Training data: ImageNet-21k / ImageNet-22k (full ImageNet, fall 2011 release), ImageNet-1k (ILSVRC 2012 classification)
  • Kernel libraries: cudnn_hacc, fvm_conv, fvm_extra, fvm_mb, fvm_r3
  • Variants: swin_base_patch4_window7_224.ms_in22k_ft_in1k (default of swin_base_patch4_window7_224)
  • Outputs: logits; label_set: imagenet-1k
  • Input size: fixed 224x224: the input is resized (short side 248) and centre-cropped to it
  • Tested up to (portable, fp16, RTX 5060 Ti 16 GB): swin_base_patch4_window7_224.ms_in22k_ft_in1k 224x224 (0.2 GiB peak allocated, 0.3 reserved)
  • swin_base_patch4_window7_224.ms_in22k_ft_in1k 224x224, first call: portable + torch.compile 27 s (cold Inductor cache), TensorRT fp16 64 s (ONNX + engine).
  • swin_base_patch4_window7_224.ms_in22k_ft_in1k original 224x224: torch.autocast(fp16) around the forward
  • swin_base_patch4_window7_224.ms_in22k_ft_in1k load / first call: original 2.71 s / 0.414 s, portable 2.03 s / 0.138 s, kernels 2.28 s / 0.087 s
  • Protocol, median of 5 after 3 warm-up, uint8 CUDA input (synthetic for bench_inputs sizes), card defaults; gc before each load; peak = max over the first call and the timed calls

Measured

fastforks: the model's own resize / crop on the GPU; original: the library's pretrained loader and its PIL eval transform. Original = the original code named under Details, same checkpoint and protocol.

variantdatasetoriginal fp32original fp16portableportable fp32kernelsnote
swin_base_patch4_window7_224.ms_in22k_ft_in1kImageNet-1k val, 5000 random images, top-1 / top-5 %86.28 / 97.8286.30 / 97.8286.24 / 97.8686.20 / 97.8686.24 / 97.86

Measured on portable: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 939adfe8, 2026-11-06; portable32: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 7c91993, 2026-10-31; kernels: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 939adfe8, 2026-11-06.

Parity

CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.08 (absolute error of the output logits).

variantkernels error vs original, mean / p99GPU, torchkernels used
swin_base_patch4_window7_224.ms_in22k_ft_in1k0.00178 / 0.00688RTX 5060 Ti, torch 2.14.1+cu130cudnn_hacc@cudnn92400, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120

Example output

One fixed input through this checkpoint, the same for every model of the task (kernels · float16 · NVIDIA GeForce RTX 5060 Ti).

Input

Top 5

  1. Egyptian cat 74%
  2. tabby 7.2%
  3. tiger cat 6.2%
  4. lynx 0.2%
  5. Siamese cat <0.1%
Chelsea the cat, input
Input · 100%

Input: Chelsea the cat, Stéfan van der Walt (CC0).