FocalNet
FocalNet (Yang et al. 2022; tiny, small and base with small or large receptive field, ImageNet-1k): a classifier with focal modulation instead of attention. MIT.
experimental Fork of huggingface/pytorch-image-models@0df212b · backends: portable, kernels
- Paper Focal Modulation Networks NeurIPS 2022 · first released 22 Mar 2022
- Authors’ code microsoft/FocalNet (PyTorch), not the code forked here: timm carries the architecture and the converted authors' checkpoints in its own, simpler API.
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[timm]"
from fastforks import classification
model = classification.load("focalnet_tiny_srf.ms_in1k") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.logits
result.backend # what ran: mode, device, dtype, kernels fastforks bench focalnet_tiny_srf.ms_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)
| Backend | dtype | Time per call (ms) | vs original fp32 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 8.39 | – | 157 MiB/178 MiB | 1.7 | 0.83 |
| Original fp16torch.autocast(float16) around the call | fp16 | 10.4 | 0.81× | 197 MiB/206 MiB | 0.4 | 0.45 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 7.36 | 1.1× | 95 MiB/118 MiB | 1.5 | 0.49 |
| Kernels | not measured | – | – | – | – | |
| Portable + torch.compile | 3.72 | 2.3× | 120 MiB/136 MiB | 13 (build) | – | |
| TensorRT fp16 | fp16 | 0.864 | 9.7× | 72 MiBenginenot comparable | 52 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-11. 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 | 18.7 | 755 MiB process | – | 224x224 |
| Portable | fp16 | 18.3 | 731 MiB process | matches | 224x224 | |
| Portable | fp32 | 18.0 | 720 MiB process | matches | 224x224 | |
| Kernels | fp32 | not implemented | – | 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
-
- ImageNet-1k (ILSVRC 2012 classification) noncommercial, non-commercial
Our reading of the licences, not legal advice.
Weights
focalnet_tiny_srf.ms_in1k.safetensors 114 MB· hosted on Hugging Facefastforks downloads and checks these files on first use (fastforks download focalnet_tiny_srf.ms_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 %
| Run | dtype | top-1 % ↑ | top-5 % ↑ | Δ top-1 % vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 82.86 | 96.28 | reference | 5000 | 2.19 |
| Original fp16 | fp16 | 82.88 | 96.30 | +0.02 | 5000 | 1.22 |
| Portable fp32 | fp32 | 82.88 | 96.28 | +0.02 | 5000 | 3.04 |
| Portable | fp16 | 82.90 | 96.28 | +0.04 | 5000 | 1.03 |
| Kernels | not measured | |||||
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-11; portable fp32: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 6f0a70e1, 2026-11-17; portable: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 7ba7307f, 2026-11-11.
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| <= 2.9e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.0027 and p99 <= 0.0098 (absolute error of the output logits).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.0000029 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.0027 | 0.00167 |
| GPU default dtypes, p99 |diff| | ≤ 0.0098 | 0.00581 |
Recorded for focalnet_tiny_srf.ms_in1k on RTX 5060 Ti, torch 2.10.0+cu130, 2026-11-17, with these kernels: cudnn_haccfvm_convfvm_extrafvm_mbfvm_r3
Details
FocalNet (Yang et al. 2022): a hierarchical classifier that replaces self-attention by focal modulation (stacked depthwise convolutions with gated aggregation).
- 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 (95 MiB allocated)
- Outputs
logits- Checkpoint
arch="focalnet_tiny_srf.ms_in1k"- CUDA kernels
cudnn_haccconvolutions with fp16 accumulation (cuDNN)fvm_convfused convolutions for image classifiersfvm_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
FocalNet (Yang et al. 2022; tiny, small and base with small or large receptive field, ImageNet-1k): a classifier with focal modulation instead of attention. MIT.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
focalnet_tiny_srf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 8.39 10.4 7.36 – 3.72 0.864 | 157 / 178 197 / 206 95 / 118 – 120 / 136 159 / 188 |
focalnet_small_srf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 15.9 20.1 14.0 – 6.90 1.49 | 239 / 252 320 / 336 136 / 152 – 161 / 190 234 / 256 |
focalnet_base_srf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 17.1 20.0 14.2 – 6.94 1.74 | 390 / 414 542 / 558 211 / 234 – 234 / 272 401 / 430 |
focalnet_tiny_lrf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 8.92 11.4 8.07 – 3.94 0.952 | 158 / 180 199 / 208 95 / 118 – 120 / 138 158 / 186 |
focalnet_small_lrf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 16.8 22.0 15.8 – 7.28 1.70 | 241 / 254 323 / 338 136 / 152 – 162 / 192 265 / 286 |
focalnet_base_lrf.ms_in1k | synthetic (224x224) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 18.0 22.1 15.5 – 7.47 2.08 | 393 / 416 546 / 560 212 / 234 – 235 / 274 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/focalnet_tiny_srf.ms_in1k/blob/de75c3a73e9ed84fee554a7138d4bac119751645/README.md
- Training data: ImageNet-1k (ILSVRC 2012 classification)
- Kernel libraries:
cudnn_hacc,fvm_conv,fvm_extra,fvm_mb,fvm_r3 - Variants:
focalnet_tiny_srf.ms_in1k(default offocalnet_tiny_srf),focalnet_small_srf.ms_in1k(default offocalnet_small_srf),focalnet_base_srf.ms_in1k(default offocalnet_base_srf),focalnet_tiny_lrf.ms_in1k(default offocalnet_tiny_lrf),focalnet_small_lrf.ms_in1k(default offocalnet_small_lrf),focalnet_base_lrf.ms_in1k(default offocalnet_base_lrf) - 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):
focalnet_tiny_srf.ms_in1k224x224 (0.1 GiB peak allocated, 0.1 reserved);focalnet_small_srf.ms_in1k224x224 (0.1 GiB peak allocated, 0.1 reserved);focalnet_base_srf.ms_in1k224x224 (0.2 GiB peak allocated, 0.2 reserved);focalnet_tiny_lrf.ms_in1k224x224 (0.1 GiB peak allocated, 0.1 reserved);focalnet_small_lrf.ms_in1k224x224 (0.1 GiB peak allocated, 0.1 reserved);focalnet_base_lrf.ms_in1k224x224 (0.2 GiB peak allocated, 0.2 reserved) focalnet_tiny_srf.ms_in1k224x224, first call: portable + torch.compile 13 s (cold Inductor cache), TensorRT fp16 52 s (ONNX + engine).focalnet_small_srf.ms_in1k224x224, first call: portable + torch.compile 18 s (cold Inductor cache), TensorRT fp16 65 s (ONNX + engine).focalnet_base_srf.ms_in1k224x224, first call: portable + torch.compile 18 s (cold Inductor cache), TensorRT fp16 64 s (ONNX + engine).focalnet_tiny_lrf.ms_in1k224x224, first call: portable + torch.compile 13 s (cold Inductor cache), TensorRT fp16 50 s (ONNX + engine).focalnet_small_lrf.ms_in1k224x224, first call: portable + torch.compile 19 s (cold Inductor cache), TensorRT fp16 62 s (ONNX + engine).focalnet_base_lrf.ms_in1k224x224, first call: portable + torch.compile 20 s (cold Inductor cache), TensorRT fp16 64 s (ONNX + engine).focalnet_tiny_srf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_small_srf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_base_srf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_tiny_lrf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_small_lrf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_base_lrf.ms_in1koriginal 224x224: torch.autocast(float16) around the callfocalnet_tiny_srf.ms_in1kload / first call: original 0.4 s / 0.445 s, portable 1.49 s / 0.495 sfocalnet_small_srf.ms_in1kload / first call: original 0.54 s / 0.023 s, portable 1.73 s / 0.49 sfocalnet_base_srf.ms_in1kload / first call: original 0.89 s / 0.052 s, portable 1.92 s / 0.487 sfocalnet_tiny_lrf.ms_in1kload / first call: original 0.36 s / 0.154 s, portable 2.56 s / 0.771 sfocalnet_small_lrf.ms_in1kload / first call: original 0.54 s / 0.025 s, portable 1.86 s / 0.687 sfocalnet_base_lrf.ms_in1kload / first call: original 0.91 s / 0.033 s, portable 1.89 s / 0.616 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
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.
| variant | dataset | original fp32 | original fp16 | portable | portable fp32 | note |
|---|---|---|---|---|---|---|
focalnet_tiny_srf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 82.86 / 96.28 | 82.88 / 96.30 | 82.90 / 96.28 | 82.88 / 96.28 | |
focalnet_small_srf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 84.34 / 96.84 | 84.30 / 96.84 | 84.44 / 96.76 | 84.40 / 96.74 | |
focalnet_base_srf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 84.20 / 96.86 | 84.18 / 96.86 | 84.16 / 96.84 | 84.16 / 96.84 | |
focalnet_tiny_lrf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 82.88 / 96.12 | 82.86 / 96.10 | 82.86 / 96.20 | 82.82 / 96.20 | |
focalnet_small_lrf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 84.36 / 96.80 | 84.34 / 96.80 | 84.32 / 96.80 | 84.38 / 96.80 | |
focalnet_base_lrf.ms_in1k | ImageNet-1k val, 5000 random images, top-1 / top-5 % | 84.64 / 96.64 | 84.64 / 96.64 | 84.56 / 96.56 | 84.56 / 96.58 |
Measured on portable: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 7ba7307f, 2026-11-11; portable32: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 6f0a70e1, 2026-11-17.
Parity
CPU, float32: max |diff| <= 2.9e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.0027 and p99 <= 0.0098 (absolute error of the output logits).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
focalnet_tiny_srf.ms_in1k | 0.00167 / 0.00581 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120 |
focalnet_small_srf.ms_in1k | 0.00124 / 0.00466 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120 |
focalnet_base_srf.ms_in1k | 0.00131 / 0.00626 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120 |
focalnet_tiny_lrf.ms_in1k | 0.00128 / 0.00548 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120 |
focalnet_small_lrf.ms_in1k | 0.00179 / 0.00799 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, fvm_conv@f1d064e539e1:sm_120, fvm_extra@3208ce525a91:sm_120, fvm_mb@720dd3f29bf7:sm_120, fvm_r3@f54eb890e23c:sm_120 |
focalnet_base_lrf.ms_in1k | 0.00106 / 0.0054 | RTX 5060 Ti, torch 2.10.0+cu130 | cudnn_hacc@cudnn91501, 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 (portable · float16 · NVIDIA GeForce RTX 5060 Ti).
Top 5
Input: Chelsea the cat, Stéfan van der Walt (CC0).