--- library_name: "fastforks" pipeline_tag: "image-to-image" license: "apache-2.0" tags: - "restoration" - "super-resolution" - "image-upscaling" - "image-restoration" - "cuda" - "pytorch" - "fastforks" - "fastforks-card" --- # HAT (fastforks) 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). This repo is the model card only: it holds no weights. fastforks downloads the authors' weights from the links below and checks them against the SHA-256 in the model card. Code: . Kernels for the `kernels` backend: . Original code: at `1638a9a82258`. Task: restoration. Tier: experimental. Outputs: `image`. Backends: `portable`, `kernels`. Input: one RGB image. Output: the restored (upscaled) image, `model.scale` x the input size, RGB in [0, 1]. `tile=` runs large images in overlapping tiles (as ComfyUI). ## Variants Weights are the authors' files (not hosted here). SHA-256 shortened to 12 hex digits; the full hashes are in [`card.toml`](https://github.com/kleinicke/fastforks/blob/main/src/fastforks/restoration/models/hat/card.toml). | variant | weights (author link) | SHA-256 | weights licence | commercial use | |---|---|---|---|---| | `hat-s-x4@1` | [HAT-S_SRx4.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1YvU9PF1XqlP8TVzH7P0bg-YlfP1TKDPC) | `a92f81bd2c0c` | Apache-2.0 | yes | | `hat-x4@1` | [HAT_SRx4_ImageNet-pretrain.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1cxls85ZE7kalhNy47eBJI_L_Lwf9hxRI) | `4ee053c42461` | Apache-2.0 | yes | | `real-hat-gan-x4@1` | [Real_HAT_GAN_SRx4.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1Ma12vCWT27P9M99-s2RXnynKN-OQsBrv) | `f5b1e3bbbb05` | Apache-2.0 | yes | ## Licence Original-code strings, recorded as they are (research-use package; check them before any other use). Code: Apache-2.0. Licence files: `LICENSE`. 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.). 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) ## Input size any H x W >= 32, padded to a multiple of 32 Tested up to (portable, fp16, RTX 5060 Ti 16 GB): `hat-s-x4` 1024x1024 (7.5 GiB peak allocated, 11.1 reserved); `hat-x4` 1024x1024 (9.4 GiB peak allocated, 14.7 reserved); `real-hat-gan-x4` 1024x1024 (9.4 GiB peak allocated, 14.7 reserved) ## Speed and memory ### Restoration, synthetic, 512x512 | model | time (ms): original | portable | kernels | kernels speed-up: vs original | vs portable | peak memory (MiB, allocated / reserved): original | portable | kernels | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | `hat-s-x4` | 2402 | 1072 | 956 | 2.5x | 1.1x | 5018 / 5644 | 1984 / 2148 | 1856 / 1888 | | `hat-x4` | 3038 | 1596 | 1427 | 2.1x | 1.1x | 5318 / 6106 | 2426 / 2590 | 2298 / 2470 | | `real-hat-gan-x4` | 3038 | 1597 | 1427 | 2.1x | 1.1x | 5318 / 6106 | 2426 / 2610 | 2298 / 2470 | ### Startup and first call | model | load (s): original | portable | kernels | first call (s): original | portable | kernels | | --- | --- | --- | --- | --- | --- | --- | | `hat-s-x4` | 3.14 | 0.35 | 1.58 | 4.41 | 1.27 | 1.07 | | `hat-x4` | 1.75 | 0.47 | 1.36 | 5.42 | 1.81 | 1.57 | | `real-hat-gan-x4` | 1.96 | 1.24 | 1.49 | 5.41 | 1.77 | 1.56 | ### torch.compile and TensorRT | model | input | time: portable | compile | TensorRT fp16 | kernels | preparation: compile (cold first call) | ONNX + engine build | peak MiB (alloc / reserved): portable | compile | kernels | TensorRT (engine + torch) | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | `hat-s-x4` | 512x512 | 1072 ms | 750 ms | fails: TensorRT engine build failed | 956 ms | 41.9 s | – | 1984 / 2148 | 2352 / 3068 | 1856 / 1888 | – | | `hat-x4` | 512x512 | 1596 ms | 1185 ms | deferred (build >5 min) | 1427 ms | 53.4 s | – | 2426 / 2590 | 1811 / 2750 | 2298 / 2470 | – | | `real-hat-gan-x4` | 512x512 | 1597 ms | 1185 ms | deferred (build >5 min) | 1427 ms | 54.3 s | – | 2426 / 2610 | 1811 / 2750 | 2298 / 2470 | – | Fixed-size cells: time (ms) / peak allocated / peak reserved memory (MiB). Hardware, protocol and caveats: [benchmarks](https://github.com/kleinicke/fastforks/blob/main/docs/benchmarks.md) (RTX 5060 Ti 16 GB, sm_120). Original = the original code named above at the precision of portable (fp16, or `(fp32)` where fp16 is unsupported or not finite); original fp32 = its default fp32 run. Peak memory is the torch allocator's. ## Accuracy ### On public datasets 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 | 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 | | | `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.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.7632 | | Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks f33b2772, 2026-11-13. ### Parity with the original code Every variant is checked against reference outputs of the original code at its pinned commit. 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`). ## Usage ```python from fastforks import restoration model = restoration.load("hat-s-x4") # mode="auto": the preferred backend from recorded measurements result = model("photo.png") # tile=512 for large images result.image # [3, sH, sW] float32 RGB in [0, 1], s = model.scale result.backend # what ran, e.g. Backend(mode='portable', device='cuda:0', dtype='float16') ``` Install with `pip install fastforks` (see ). Other variants: `hat-s-x4`, `hat-x4`, `real-hat-gan-x4`. ## Kernels backend `restoration.load("hat-s-x4", mode="kernels")` runs our CUDA kernels (`cudnn_hacc`). It needs a prebuilt kernel for your GPU architecture (downloaded from on first use) and a recent NVIDIA driver; if there is none it raises `BackendUnavailableError` with the reason and never falls back silently. `mode="auto"` picks `kernels` when it can run and logs the choice.