CodeFormer
CodeFormer (Zhou et al., S-Lab NTU, NeurIPS 2022): restores a degraded face (an aligned 512 x 512 crop) by predicting codes of a learned face codebook; fidelity w trades quality against faithfulness. Non-commercial code and weights (S-Lab License 1.0).
experimental Fork of sczhou/CodeFormer@b33cc7d · backends: portable, kernels
- Paper Towards Robust Blind Face Restoration with Codebook Lookup Transformer NeurIPS 2022 · first released 22 Jun 2022
Checkpoint codeformer
Paper 2022 Added in 0.2.0
Use it
pip install fastforks Lean install with only what this model needs: pip install "fastforks-core[nc]"
from fastforks import restoration
model = restoration.load("codeformer") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.image
result.backend # what ran: mode, device, dtype, kernels The weights are non-commercial: fastforks downloads them only with FASTFORKS_NONCOMMERCIAL=1 set.
fastforks bench codeformer 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 (own default)upstream inference_codeformer.py on the aligned face, float32, w 0.5 | fp32 | 85.7 | – | 959 MiB/1,090 MiB | 1.2 | 0.41 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 48.6 | 1.8× | 512 MiB/638 MiB | 0.7 | 0.23 |
| Kernels | fp16 | 40.4 | 2.1× | 432 MiB/496 MiB | 0.7 | 0.23 |
| Portable + torch.compile | 30.8 | 2.8× | 407 MiB/458 MiB | 34 (build) | – | |
| TensorRT fp16 | fp16 | 16.3 | 5.3× | 372 MiBenginenot comparable | 65 (build) | – |
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-17 | Original | fp32 | 274 | 3,182 MiB process | – | 512x512 |
| Portable | fp16 | 196 | 1,866 MiB process | matches | 512x512 | |
| Portable | fp32 | 260 | 3,064 MiB process | matches | 512x512 | |
| Kernels | fp16 | not implemented | – | 512x512 | ||
| Kernels | fp32 | 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
Non-commercial only
- Code
- LicenseRef-noncommercial LICENSE
- Weights
- LicenseRef-noncommercial source
- Training data
-
- Flickr-Faces-HQ (FFHQ, 70k face images) CC-BY-NC-SA-4.0, non-commercial
- code: LicenseRef-noncommercial (noncommercial)
- weights: LicenseRef-noncommercial
Our reading of the licences, not legal advice.
Weights
codeformer.pth 377 MB· hosted on GitHubfastforks downloads and checks these files on first use (fastforks download codeformer). Non-commercial weights need FASTFORKS_NONCOMMERCIAL=1.
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.
Accuracy not measured for codeformer.
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.25 (absolute error of the output image).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.002 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.02 | 0.0017 |
| GPU default dtypes, p99 |diff| | ≤ 0.25 | 0.01817 |
Recorded for codeformer on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-15, with these kernels: cudnn_hacccutlass_convigev
Details
CodeFormer (Zhou et al., NeurIPS 2022): blind face restoration with a learned codebook: a VQGAN encoder, a transformer that predicts the code of each of the 16 x 16 latent positions, and the VQGAN decoder with controllable feature fusion from the encoder (fidelity w). Input: an aligned 512 x 512 face crop (FFHQ alignment); detecting, aligning and pasting the face back are not part of the model.
- 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 >= 512, padded to a multiple of 32; tested up to 512x512 (519 MiB allocated)
- Options
fidelity=0.50.0 to 1.0· changes the output
- Outputs
image- CUDA kernels
igevIGEV stereo: convolution epilogues, correlation lookups, GRU gatescutlass_convCUTLASS convolutions with the recurrent update (GRU) fused incudnn_haccconvolutions with fp16 accumulation (cuDNN)
- Apple (MPS)
- fp32, fp16
Limitations
- No accuracy measurement: blind face restoration is scored on CelebA-Test / LFW / WebPhoto (FID, NIQE, identity distance), which are not part of the evaluation data here; tiers are compared by output parity and speed only.
Full reference, generated from the repository
CodeFormer (Zhou et al., S-Lab NTU, NeurIPS 2022): restores a degraded face (an aligned 512 x 512 crop) by predicting codes of a learned face codebook; fidelity w trades quality against faithfulness. Non-commercial code and weights (S-Lab License 1.0).
Commercial use: no (code: LicenseRef-noncommercial (noncommercial); weights: LicenseRef-noncommercial).
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
codeformer | synthetic (512x512) | original portable kernels portable + torch.compile TensorRT fp16 | 85.7 48.6 40.4 30.8 16.3 | 959 / 1090 512 / 638 432 / 496 407 / 458 593 / 642 |
Details
- Original code: https://github.com/sczhou/CodeFormer at
b33cc7d639d6 - Code licence: LicenseRef-noncommercial (noncommercial); files:
LICENSE - Weights licence: LicenseRef-noncommercial
- Licence evidence: https://github.com/sczhou/CodeFormer/blob/b33cc7d639d6545bfcccc7e0bc6ae51f24e79c2b/LICENSE ; https://github.com/sczhou/CodeFormer/blob/b33cc7d639d6545bfcccc7e0bc6ae51f24e79c2b/README.md (weights: https://github.com/sczhou/CodeFormer/releases/tag/v0.1.0)
- Training data: Flickr-Faces-HQ (FFHQ, 70k face images) (Paper Sec. 4.1: FFHQ (70 000 faces, resized to 512 x 512) with synthetic degradations (blur, downsampling, noise, JPEG).)
- Options:
fidelity= 0.5 (0.0 to 1.0; alsofidelity_weight) - Kernel libraries:
igev,cutlass_conv,cudnn_hacc - Variants:
codeformer - Outputs: image
- Input size: any H x W >= 512, padded to a multiple of 32
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
codeformer512x512 (0.5 GiB peak allocated, 0.7 reserved) codeformer512x512, first call: portable + torch.compile 34 s (cold Inductor cache), TensorRT fp16 65 s (ONNX + engine).codeformeroriginal 512x512: upstream inference_codeformer.py on the aligned face, float32, w 0.5codeformerload / first call: original 1.24 s / 0.406 s, portable 0.69 s / 0.228 s, kernels 0.65 s / 0.23 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
Parity
CPU, float32: max |diff| <= 0.002 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 |
|---|---|---|---|
codeformer | 0.0017 / 0.01817 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120 |
Limitations
- No accuracy measurement: blind face restoration is scored on CelebA-Test / LFW / WebPhoto (FID, NIQE, identity distance), which are not part of the evaluation data here; tiers are compared by output parity and speed only.
Example output
No example rendered for this checkpoint yet.