Models · Promptable segmentation
Segment Anything (SAM)
Segment Anything: masks from clicks, boxes or mask prompts, in sizes B, L and H.
stable Fork of facebookresearch/segment-anything@dca509f · backends: portable, kernels
- Paper Segment Anything ICCV 2023 · first released 5 Apr 2023
Paper 2023 Added in 0.1.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 promptable_segmentation
model = promptable_segmentation.load("sam-vit-b") # kernels on a supported GPU, else portable PyTorch
result = model(image, input_points=...)
result.pred_masks # also: iou_scores
result.backend # what ran: mode, device, dtype, kernels fastforks bench sam-vit-b 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.
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 168 | 0.58× | 2,798 MiB/2,832 MiB | 4.2 | 0.50 |
| Original fp16torch.autocast(float16) around the call | fp16 | 98.2 | – | 1,769 MiB/1,802 MiB | 0.8 | 0.18 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 48.3 | 2.0× | 378 MiB/420 MiB | 0.5 | 0.05 |
| Kernels | fp16 | 37.0 | 2.7× | 377 MiB/420 MiB | 0.2 | 0.04 |
| Portable + torch.compile | 40.9 | 2.4× | 362 MiB/400 MiB | 15 (build) | – | |
| TensorRT fp16image encoder only | fp16 | 27.3 | 3.6× | 322 MiBenginenot comparable | 36 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-03 to 2026-11-06. Median of 5 after 3 warm-up, uint8 CUDA input (synthetic for bench_inputs sizes), card defaults; gc before each load; peak = max over the first call and the timed calls. 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 | 841 | 4,783 MiB process | – | 1024x1024 |
| Portable | fp16 | 600 | 3,762 MiB process | matches | 1024x1024 | |
| Portable | fp32 | 631 | 4,362 MiB process | matches | 1024x1024 | |
| Kernels | fp16 | not implemented | – | 1024x1024 | ||
| NVIDIA L4 CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 48.6 | 341 MiB/360 MiB | – | 1024x1024 |
| Kernels | fp16 | 40.7 | 341 MiB/380 MiB | – | 1024x1024 | |
| NVIDIA L40S CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 17.6 | 341 MiB/360 MiB | – | 1024x1024 |
| Kernels | fp16 | 15.7 | 341 MiB/380 MiB | – | 1024x1024 | |
| NVIDIA A10 CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 39.7 | 341 MiB/360 MiB | – | 1024x1024 |
| Kernels | fp16 | 35.1 | 341 MiB/380 MiB | – | 1024x1024 | |
| NVIDIA A100-SXM4-40GB CUDA, torch 2.14.1+cu130, 2026-11-08 | Portable | fp16 | 20.0 | 341 MiB/360 MiB | – | 1024x1024 |
| Kernels | fp16 | 19.2 | 341 MiB/380 MiB | – | 1024x1024 | |
| NVIDIA RTX PRO 6000 CUDA, torch 2.14.1+cu130, 2026-11-08 | Portable | fp16 | 11.2 | 365 MiB/400 MiB | – | 1024x1024 |
| Kernels | fp16 | 10.3 | 365 MiB/420 MiB | – | 1024x1024 | |
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
- SA-1B (11M licensed images, 1.1B automatically generated masks) per the paper; the dataset itself is under the SA-1B research licence
Our reading of the licences, not legal advice.
Weights
sam_vit_b_01ec64.pth 375 MB· hosted on Meta (fbaipublicfiles)fastforks downloads and checks these files on first use (fastforks download sam-vit-b).
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.
COCO val2017
every 10th image, point prompts, mIoU best of 3 / single mask %
| Run | dtype | mIoU best of 3 % ↑ | mIoU single % ↑ | Δ mIoU best of 3 % vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 52.91 | 50.31 | reference | 500 | 176 |
| Original fp16 | fp16 | 53.08 | 50.39 | +0.17 | 500 | 105 |
| Portable fp32 | not measured | |||||
| Portable | fp16 | 52.91 | 50.31 | ±0.00 | 500 | 53.4 |
| Kernels | fp16 | 52.89 | 50.32 | −0.02 | 500 | 42.0 |
One point per ground-truth instance (the interior point farthest from the border); IoU of the best-scored of 3 masks / of the single mask. Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-06; portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 939adfe8, 2026-11-06.
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| <= 3 % and p99 <= 15 % of the reference's median |value| (absolute error of the output pred_masks).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.002 (relative) | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.03 (relative) | 0.00044 (relative) |
| GPU default dtypes, p99 |diff| | ≤ 0.15 (relative) | 0.00203 (relative) |
Recorded for sam-vit-b on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-07, with these kernels: dpt_fused
Details
Segment Anything (Kirillov et al. 2023): promptable segmentation with a ViT image encoder (windowed + global attention, relative positions) and a light prompt encoder / mask decoder; masks from points, boxes and mask logits. The portable `module()` is the image encoder only.
- Input / output
- Input: one image plus a point, box or text prompt. Output: masks with quality scores.
- Input size
- any H x W >= 1, resized inside to the fixed encoder input 1024x1024 (the default export size); tested up to 1080x1920 (387 MiB allocated)
- Outputs
pred_masks,iou_scores- Checkpoint
embed_dim=768,depth=12,num_heads=12- CUDA kernels
dpt_fusedfused fp16 matrix multiplies and convolutions for DINOv2 backbones and DPT depth heads
- Apple (MPS)
- fp32, fp16
Full reference, generated from the repository
Segment Anything: masks from clicks, boxes or mask prompts, in sizes B, L and H.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
sam-vit-b | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 168 98.2 48.3 37.0 40.9 27.3 (image encoder only) | 2798 / 2832 1769 / 1802 378 / 420 377 / 420 362 / 400 545 / 582 |
sam-vit-l | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 406 197 116 81.5 97.2 67.5 (image encoder only) | 4428 / 4474 3411 / 3464 833 / 1262 831 / 862 793 / 862 1453 / 1520 |
sam-vit-h | synthetic (1024x1024) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 734 306 214 143 188 124 (image encoder only) | 5702 / 5754 5295 / 5404 1484 / 2524 1480 / 1524 1436 / 1484 2744 / 2784 |
Details
- Original code: https://github.com/facebookresearch/segment-anything at
dca509f - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://github.com/facebookresearch/segment-anything/blob/main/README.md (License section: model checkpoints and code under Apache 2.0)
- Training data: SA-1B (11M licensed images, 1.1B automatically generated masks) per the paper; the dataset itself is under the SA-1B research licence
- Kernel libraries:
dpt_fused - Variants:
sam-vit-b,sam-vit-l,sam-vit-h - Outputs: pred_masks, iou_scores
- Input size: any H x W >= 1, resized inside to the fixed encoder input 1024x1024 (the default export size)
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
sam-vit-b1080x1920 (0.4 GiB peak allocated, 0.5 reserved);sam-vit-l1080x1920 (0.8 GiB peak allocated, 1.0 reserved);sam-vit-h1080x1920 (1.5 GiB peak allocated, 1.9 reserved) sam-vit-b1024x1024, first call: portable + torch.compile 15 s (cold Inductor cache), TensorRT fp16 36 s (ONNX + engine).sam-vit-l1024x1024, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 53 s (ONNX + engine).sam-vit-h1024x1024, first call: portable + torch.compile 43 s (cold Inductor cache), TensorRT fp16 70 s (ONNX + engine).sam-vit-boriginal 1024x1024: torch.autocast(float16) around the callsam-vit-loriginal 1024x1024: torch.autocast(float16) around the callsam-vit-horiginal 1024x1024: torch.autocast(float16) around the callsam-vit-bload / first call: original 0.75 s / 0.176 s, portable 0.46 s / 0.048† s, kernels 0.17† s / 0.037† ssam-vit-lload / first call: original 1.8 s / 0.268 s, portable 1.21 s / 0.116† s, kernels 0.43† s / 0.081† ssam-vit-hload / first call: original 4.61 s / 0.376 s, portable 2.45 s / 0.214† s, kernels 0.89† s / 0.143† s- † measured in the speed sweep after the portable load (or an earlier size) in the same process: file cache and kernels are warm, so the time is lower than a fresh start.
- Protocol, median of 5 after 3 warm-up, uint8 CUDA input (synthetic for bench_inputs sizes), card defaults; gc before each load; peak = max over the first call and the timed calls
Measured
One point per ground-truth instance (the interior point farthest from the border); IoU of the best-scored of 3 masks / of the single mask. Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | original fp16 | portable | kernels | note |
|---|---|---|---|---|---|---|
sam-vit-b | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 52.91 / 50.31 | 53.08 / 50.39 | 52.91 / 50.31 | 52.89 / 50.32 | |
sam-vit-l | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 57.89 / 53.32 | 57.76 / 53.31 | 57.84 / 53.32 | 57.85 / 53.33 | |
sam-vit-h | COCO val2017, every 10th image, point prompts, mIoU best of 3 / single mask % | 57.93 / 53.13 | 57.92 / 53.13 | 57.97 / 53.12 | 57.99 / 53.12 |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 939adfe8, 2026-11-06.
Parity
CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 3 % and p99 <= 15 % of the reference's median |value| (absolute error of the output pred_masks).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
sam-vit-b | 0.00044 / 0.00203 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
sam-vit-l | 0.01589 / 0.06758 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
sam-vit-h | 0.00329 / 0.01177 | RTX 5060 Ti, torch 2.14.1+cu130 | dpt_fused@48e0065bb7cf:sm_120 |
Example output
One fixed input through this checkpoint, the same for every model of the task (kernels · float16 · NVIDIA GeForce RTX 5060 Ti). Prompt: one foreground point.
Input: A cup of coffee, Rachel Michetti (CC0).