Swin2SR
Swin2SR (Conde et al., ECCVW 2022): SwinIR with Swin Transformer V2 blocks: x4 / x2 upscalers for clean (bicubic) images and a x4 model for real-world photos. Apache-2.0 code and weights.
experimental Fork of mv-lab/swin2sr@6f25d76 · backends: portable, kernels
- Paper Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration ECCV 2022 Workshops (AIM) · first released 22 Sept 2022
Paper 2022 Added in 0.2.0 Evaluated with 0.2.0
Use it
pip install fastforks Lean install with only what this model needs: pip install fastforks-core
from fastforks import restoration
model = restoration.load("swin2sr-x4") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.image
result.backend # what ran: mode, device, dtype, kernels fastforks bench swin2sr-x4 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, 512x512 (the task’s standard size)
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | not measured | – | – | – | – |
| Original fp16.half() on 1 modules, fp16 inputs; upstream main_test_swin2sr.py, float32, no tile | fp16 | 2922 | – | 1,488 MiB/2,160 MiB | 0.3 | 3.02 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 932 | 3.1× | 1,463 MiB/2,276 MiB | 0.7 | 1.15 |
| Kernels | fp16 | 745 | 3.9× | 1,459 MiB/1,736 MiB | 0.6 | 0.87 |
| Portable + torch.compile | 496 | 5.9× | 1,308 MiB/1,556 MiB | 46 (build) | – | |
| TensorRT fp16 | fp16 | 556 | 5.3× | 5,154 MiBenginenot comparable | 113 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-14 to 2026-11-15. 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
| Device | Backend | dtype | ms / call | Peak memory | Output vs reference | Input |
|---|---|---|---|---|---|---|
| Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-15 | Original | fp32 | 12965 | 7,727 MiB process | – | 512x512 |
| Portable | fp16 | 9410 | 4,580 MiB process | matches | 512x512 | |
| Portable | fp32 | 9608 | 8,391 MiB process | matches | 512x512 | |
| Kernels | fp16 | not implemented | – | 512x512 | ||
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 LICENSE
- Weights
- Apache-2.0 source
- Training data
-
- DIV2K (NTIRE 2017 super-resolution, 1000 2K images) research-only, non-commercial
- Flickr2K (2650 2K Flickr images, EDSR) not stated
Our reading of the licences, not legal advice.
Weights
Swin2SR_ClassicalSR_X4_64.pth 69.4 MB· hosted on GitHubfastforks downloads and checks these files on first use (fastforks download swin2sr-x4).
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.
DIV2K validation (every 2nd: 50 images)
bicubic x4 LR -> HR, PSNR / SSIM (Y)
| Run | dtype | psnr ↑ | ssim ↑ | Δ psnr vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 30.89 | 0.8420 | reference | 50 | – |
| Original fp16 | not measured | |||||
| Portable fp32 | fp32 | 30.89 | 0.8420 | ±0.00 | 50 | – |
| Portable | fp16 | 30.89 | 0.8420 | ±0.00 | 50 | – |
| Kernels | fp16 | 30.88 | 0.8420 | −0.01 | 50 | – |
DIV2K validation HR images and the official bicubic LR images (x4 or x2, the model's scale), one pass without tiles; PSNR and SSIM (Gaussian 11 x 11, sigma 1.5) on the Y channel (BT.601) of the output rounded to uint8, `scale` px border cropped, averaged over the images. Real-ESRGAN is trained for real-world degradations, not bicubic, so its PSNR is low: the numbers compare our modes with the original, not with PSNR-oriented upscalers. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-14; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks f104019b, 2026-11-14.
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| <= 1.4e-05 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output image).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.000014 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.02 | 0.00055 |
| GPU default dtypes, p99 |diff| | ≤ 0.25 | 0.00205 |
Recorded for swin2sr-x4 on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-14, with these kernels: cudnn_hacc
Details
Swin2SR (Conde et al., ECCVW 2022): SwinIR with Swin Transformer V2 layers (scaled cosine attention, continuous log-spaced position bias, residual post-norm) in residual groups between a conv stem and an upsampler. `swin2sr-x2` / `swin2sr-x4` are the classical (bicubic, PSNR-trained) DIV2K + Flickr2K models, `swin2sr-x4-real` the real-world x4 PSNR model (BSRGAN degradations).
- Input / output
- Input: one RGB image. Output: the restored (upscaled) image,
model.scalex the input size, RGB in [0, 1].What the numbers mean, and which models need the camera focal length
tile=runs large images in overlapping tiles (as ComfyUI). - Input size
- any H x W >= 32, padded to a multiple of 32; tested up to 1024x2048 (10.9 GiB allocated)
- Outputs
image- CUDA kernels
cudnn_haccconvolutions with fp16 accumulation (cuDNN)
- Apple (MPS)
- fp32, fp16
Limitations
- Kernels peak allocated is equal to portable, but the cuDNN workspace next to the large conv maps fragments the allocator: peak reserved is ~80 MiB above portable.
Full reference, generated from the repository
Swin2SR (Conde et al., ECCVW 2022): SwinIR with Swin Transformer V2 blocks: x4 / x2 upscalers for clean (bicubic) images and a x4 model for real-world photos. Apache-2.0 code and weights.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
swin2sr-x4 | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 2922 932 745 496 556 | 1488 / 2160 1463 / 2276 1459 / 1736 1308 / 1556 5410 / 5470 |
swin2sr-x2 | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 2565 909 721 482 547 | 1488 / 1578 1459 / 1554 1459 / 1636 1308 / 1516 4941 / 4997 |
swin2sr-x4-real | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 2527 958 751 508 562 | 2321 / 2660 2354 / 2774 1459 / 1734 1308 / 1556 5409 / 5468 |
Details
- Original code: https://github.com/mv-lab/swin2sr at
6f25d7689813 - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://github.com/mv-lab/swin2sr/blob/6f25d7689813f7dd12c5a212b638066fa71bc9e6/LICENSE ; https://github.com/mv-lab/swin2sr/releases/tag/v0.0.1
- Training data: DIV2K (NTIRE 2017 super-resolution, 1000 2K images), Flickr2K (2650 2K Flickr images, EDSR) (README, Results: trained mainly on DIV2K and Flickr2K, setup as SwinIR (classical SR: bicubic-downsampled HR images).)
- Kernel libraries:
cudnn_hacc - Variants:
swin2sr-x4,swin2sr-x2,swin2sr-x4-real - Outputs: image
- Input size: any H x W >= 32, padded to a multiple of 32
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
swin2sr-x41024x2048 (10.9 GiB peak allocated, 13.7 reserved);swin2sr-x21024x2048 (10.9 GiB peak allocated, 13.3 reserved);swin2sr-x4-real1024x2048 (13.1 GiB peak allocated, 15.2 reserved) swin2sr-x4512x512, first call: portable + torch.compile 46 s (cold Inductor cache), TensorRT fp16 113 s (ONNX + engine).swin2sr-x2512x512, first call: portable + torch.compile 44 s (cold Inductor cache), TensorRT fp16 112 s (ONNX + engine).swin2sr-x4-real512x512, first call: portable + torch.compile 42 s (cold Inductor cache), TensorRT fp16 128 s (ONNX + engine).swin2sr-x4original 512x512: .half() on 1 modules, fp16 inputs; upstream main_test_swin2sr.py, float32, no tileswin2sr-x2original 512x512: .half() on 1 modules, fp16 inputs; upstream main_test_swin2sr.py, float32, no tileswin2sr-x4-realoriginal 512x512: .half() on 1 modules, fp16 inputs; upstream main_test_swin2sr.py, float32, no tileswin2sr-x4load / first call: original 1.63 s / 3.583 s, portable 0.48 s / 1.16 s, kernels 0.57 s / 1.023 sswin2sr-x2load / first call: original 1.81 s / 4.125 s, portable 0.49 s / 1.154 s, kernels 0.62 s / 0.863 sswin2sr-x4-realload / first call: original 1.89 s / 3.754 s, portable 0.47 s / 1.179 s, kernels 0.57 s / 0.882 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
Measured
DIV2K validation HR images and the official bicubic LR images (x4 or x2, the model's scale), one pass without tiles; PSNR and SSIM (Gaussian 11 x 11, sigma 1.5) on the Y channel (BT.601) of the output rounded to uint8, scale px border cropped, averaged over the images. Real-ESRGAN is trained for real-world degradations, not bicubic, so its PSNR is low: the numbers compare our modes with the original, not with PSNR-oriented upscalers. Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|
swin2sr-x4 | DIV2K validation (every 2nd: 50 images), bicubic x4 LR -> HR, PSNR / SSIM (Y) | 30.89 / 0.8420 | 30.89 / 0.8420 | 30.89 / 0.8420 | 30.88 / 0.8420 | |
swin2sr-x2 | DIV2K validation (every 4th: 25 images), bicubic x2 LR -> HR, PSNR / SSIM (Y) | 36.43 / 0.9423 | 36.43 / 0.9423 | 36.43 / 0.9423 | 36.43 / 0.9423 | |
swin2sr-x4-real | DIV2K validation (every 2nd: 50 images), bicubic x4 LR -> HR, PSNR / SSIM (Y) | 29.04 / 0.8021 | 29.04 / 0.8021 | 29.04 / 0.8021 | 29.05 / 0.8021 |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks f104019b, 2026-11-14.
Parity
CPU, float32: max |diff| <= 1.4e-05 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output image).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
swin2sr-x4 | 0.00055 / 0.00205 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
swin2sr-x2 | 0.00063 / 0.00232 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
swin2sr-x4-real | 0.00037 / 0.00138 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
Limitations
- Kernels peak allocated is equal to portable, but the cuDNN workspace next to the large conv maps fragments the allocator: peak reserved is ~80 MiB above portable.
Example output
No example rendered for this checkpoint yet.