HAT
HAT (Chen et al., CVPR 2023): a x4 upscaler mixing window attention, channel attention and overlapping cross-attention: two models for clean (bicubic) images (large and small) and a GAN model for real-world photos. Apache-2.0 code and weights.
experimental Fork of XPixelGroup/HAT@1638a9a · backends: portable, kernels
- Paper Activating More Pixels in Image Super-Resolution Transformer CVPR 2023 · first released 9 May 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("hat-x4") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.image
result.backend # what ran: mode, device, dtype, kernels fastforks bench hat-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 hat_model.py test, float32, no tile | fp16 | 3038 | – | 5,318 MiB/6,106 MiB | 1.9 | 3.43 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 1596 | 1.9× | 2,426 MiB/2,590 MiB | 0.5 | 1.81 |
| Kernels | fp16 | 1427 | 2.1× | 2,298 MiB/2,470 MiB | 0.8 | 1.54 |
| Portable + torch.compile | 1185 | 2.6× | 1,811 MiB/2,750 MiB | 53 (build) | – | |
| TensorRTTensorRT engine build failed | fails | – | – | – | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-13 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-14 | Original | fp32 | out of memory | – | 512x512 | |
| Portable | fp16 | out of memory | – | 512x512 | ||
| Portable | fp32 | out of memory | – | 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
-
- ImageNet-1k (ILSVRC 2012 classification) noncommercial, non-commercial
- 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
HAT_SRx4_ImageNet-pretrain.pth 85.1 MB· hosted on Google Drivefastforks downloads and checks these files on first use (fastforks download hat-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 4th: 25 images)
bicubic x4 LR -> HR, PSNR / SSIM (Y)
| Run | dtype | psnr ↑ | ssim ↑ | Δ psnr vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 31.01 | 0.8498 | reference | 25 | – |
| Original fp16 | not measured | |||||
| Portable fp32 | fp32 | 31.01 | 0.8498 | ±0.00 | 25 | – |
| Portable | fp16 | 31.01 | 0.8498 | ±0.00 | 25 | – |
| Kernels | fp16 | 31.01 | 0.8498 | ±0.00 | 25 | – |
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-13; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks f33b2772, 2026-11-13.
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| <= 3e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.00083 and p99 <= 0.0031 (absolute error of the output image).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.000003 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.00083 | 0.00055 |
| GPU default dtypes, p99 |diff| | ≤ 0.0031 | 0.00204 |
Recorded for hat-x4 on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-13, with these kernels: cudnn_hacc
Details
HAT (Chen et al., CVPR 2023): image super-resolution with residual groups of hybrid attention blocks (16 x 16 window attention plus a channel-attention conv branch) and an overlapping cross-attention block per group. `hat-x4` is the classical x4 model pretrained on ImageNet and fine-tuned on DF2K, `hat-s-x4` the small one (144 channels), `real-hat-gan-x4` the real-world x4 GAN model (Real-ESRGAN 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 1024x1024 (9,628 MiB allocated)
- Outputs
image- CUDA kernels
cudnn_haccconvolutions with fp16 accumulation (cuDNN)
- Apple (MPS)
- fp32, fp16
Full reference, generated from the repository
HAT (Chen et al., CVPR 2023): a x4 upscaler mixing window attention, channel attention and overlapping cross-attention: two models for clean (bicubic) images (large and small) and a GAN model for real-world photos. Apache-2.0 code and weights.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
hat-x4 | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 3038 1596 1427 1185 fails: TensorRT engine build failed | 5318 / 6106 2426 / 2590 2298 / 2470 1811 / 2750 – |
hat-s-x4 | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 2402 1072 956 750 fails: TensorRT engine build failed | 5018 / 5644 1984 / 2148 1856 / 1888 2352 / 3068 – |
real-hat-gan-x4 | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 3038 1597 1427 1185 fails: TensorRT engine build failed | 5318 / 6106 2426 / 2610 2298 / 2470 1811 / 2750 – |
Details
- Original code: https://github.com/XPixelGroup/HAT at
1638a9a82258 - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://github.com/XPixelGroup/HAT/blob/1638a9a822581657811867bf670717f8371fc3e5/LICENSE ; https://github.com/XPixelGroup/HAT/blob/1638a9a822581657811867bf670717f8371fc3e5/README.md (pretrained models: https://drive.google.com/drive/folders/1HpmReFfoUqUbnAOQ7rvOeNU3uf_m69w0)
- Training data: ImageNet-1k (ILSVRC 2012 classification), DIV2K (NTIRE 2017 super-resolution, 1000 2K images), Flickr2K (2650 2K Flickr images, EDSR) (options/train/train_HAT_SRx4_ImageNet_from_scratch.yml then train_HAT_SRx4_finetune_from_ImageNet_pretrain.yml: pretrained on ImageNet, fine-tuned on DF2K (DIV2K + Flickr2K), bicubic-downsampled.)
hat-s-x4: DIV2K (NTIRE 2017 super-resolution, 1000 2K images), Flickr2K (2650 2K Flickr images, EDSR) (options/train/train_HAT-S_SRx4_finetune_from_SRx2.yml: DF2K (DIV2K + Flickr2K), bicubic-downsampled, fine-tuned from the x2 model.)real-hat-gan-x4: DIV2K (NTIRE 2017 super-resolution, 1000 2K images), Flickr2K (2650 2K Flickr images, EDSR), OutdoorSceneTraining (OST, SFTGAN) (options/train/train_Real_HAT_SRx4_mse_model.yml then train_Real_HAT_GAN_SRx4_finetune_from_mse_model.yml: DF2K + OST with Real-ESRGAN degradations (README: the released model without USM-sharpened ground truth).)
- Kernel libraries:
cudnn_hacc - Variants:
hat-x4,hat-s-x4,real-hat-gan-x4 - 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):
hat-x41024x1024 (9.4 GiB peak allocated, 14.7 reserved);hat-s-x41024x1024 (7.5 GiB peak allocated, 11.1 reserved);real-hat-gan-x41024x1024 (9.4 GiB peak allocated, 14.7 reserved) hat-x4512x512, first call: portable + torch.compile 53 s (cold Inductor cache).hat-s-x4512x512, first call: portable + torch.compile 42 s (cold Inductor cache).real-hat-gan-x4512x512, first call: portable + torch.compile 54 s (cold Inductor cache).hat-x4original 512x512: .half() on 1 modules, fp16 inputs; upstream hat_model.py test, float32, no tilehat-s-x4original 512x512: .half() on 1 modules, fp16 inputs; upstream hat_model.py test, float32, no tilereal-hat-gan-x4original 512x512: .half() on 1 modules, fp16 inputs; upstream hat_model.py test, float32, no tilehat-x4load / first call: original 1.75 s / 5.422 s, portable 0.47 s / 1.809 s, kernels 1.36 s / 1.569 shat-s-x4load / first call: original 3.14 s / 4.407 s, portable 0.35 s / 1.268 s, kernels 1.58 s / 1.068 sreal-hat-gan-x4load / first call: original 1.96 s / 5.412 s, portable 1.24 s / 1.769 s, kernels 1.49 s / 1.562 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 |
|---|---|---|---|---|---|---|
hat-x4 | DIV2K validation (every 4th: 25 images), bicubic x4 LR -> HR, PSNR / SSIM (Y) | 31.01 / 0.8498 | 31.01 / 0.8498 | 31.01 / 0.8498 | 31.01 / 0.8498 | |
hat-s-x4 | DIV2K validation (every 4th: 25 images), bicubic x4 LR -> HR, PSNR / SSIM (Y) | 30.91 / 0.8476 | 30.91 / 0.8476 | 30.91 / 0.8476 | 30.90 / 0.8476 | |
real-hat-gan-x4 | DIV2K validation (every 4th: 25 images), bicubic x4 LR -> HR, PSNR / SSIM (Y) | 27.13 / 0.7633 | 27.13 / 0.7633 | 27.13 / 0.7633 | 27.13 / 0.7632 |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks f33b2772, 2026-11-13.
Parity
CPU, float32: max |diff| <= 3e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.00083 and p99 <= 0.0031 (absolute error of the output image).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
hat-x4 | 0.00055 / 0.00204 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
hat-s-x4 | 0.0005 / 0.00182 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
real-hat-gan-x4 | 0.00045 / 0.00158 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400 |
Example output
No example rendered for this checkpoint yet.