sam3rs
A native Rust/CUDA runtime for Meta SAM 3
image inference and click-to-track video. It loads the official Hugging Face
model.safetensors checkpoint and runs the whole image path and the video tracker on
one CUDA stream without ONNX Runtime or PyTorch:
- Perception Encoder ViT backbone (32 blocks, windowed and global attention with RoPE) and both SimpleFPN necks
- CLIP text encoder with the BPE tokenizer
- geometry (box) encoder, fusion encoder, DETR decoder with the presence token, dot-product scoring and the segmentation head
- SAM 2 style point decoder with the two-pass mask refinement and hole filling
- the SAM 3 video tracker: memory attention over a bank of past frames, the memory encoder, object pointers and upstream's memory selection
Image features stay on the GPU between prompts. Kernels that cuBLAS doesn't cover are
Rust, written with cuda-oxide in
crates/sam3rs-kernels. They ship as prebuilt PTX in
kernels/ptx that the driver compiles when the model loads, so building sam3rs needs no
CUDA toolkit or cuda-oxide. scripts/build-ptx.sh regenerates the PTX after a kernel
change.
There is also a Python package with numpy in and out; see crates/sam3rs-python.
Model downloads use hf-fetch, a small
blocking client with rustls and the shared Python Hugging Face cache. The online
feature enables downloads; local model loading works without it.
Precision modes
bf16(default): tensor-core GEMMs with FP32 accumulation, bias and activations fused into cuBLASLt epilogues, and a fused flash-attention kernel. The backbone, necks, text encoder MLP and fusion encoder FFN keep BF16 weights and activations. The rest of the text encoder and fusion encoder, the detector and the point decoder keep FP32 ones, and cuBLAS may round their GEMM inputs to BF16, though on an RTX 5090 it picks TF32 kernels for most of them. Their unmasked attention, and the detector's box-biased cross-attention, also run the fused kernel, which rounds q, k and v to FP16 and sums scores in FP16. In every fused attention, v is FP16 and each 64-key P @ V tile is summed in FP16 before it is added to FP32 totals.tf32: thefp32path with TF32 tensor-core GEMMs (FP32 accumulation), the way ONNX Runtime's CUDA provider runs FP32 models by default, with bias and activations fused into cuBLASLt epilogues. The first call of each GEMM shape times cuBLASLt's top candidate algorithms and switches from its first choice only to one more than 5% faster. A candidate near that margin can win in one run and lose in the next, so outputs can differ slightly between runs (about 0.00001 in score). FP32 weights and activations. Attention without a causal mask or additive bias, the detector's box-biased cross-attention included, runs a fused flash-attention kernel on TF32 tensor cores with an FP32 softmax; the rest stays materialized.fp32: strict FP32, with no TF32 or BF16 anywhere: cuBLAS GEMMs in pedantic math and cuBLASLt linears with FP32 compute, with bias and activations fused into their epilogues. The vision backbone's attention runs a fused flash-attention kernel on FP32 FMAs with an online softmax, so it never writes the score matrix; the detector's attention stays materialized. This is the reference path for parity checks.
Requirements
- An NVIDIA GPU with compute capability 8.0 or newer (Ampere, Ada, Hopper, Blackwell) and driver r580 or newer. RTX 50 series GPUs load compute_120 PTX, others the compute_80 floor.
- Any cuBLAS 13 release (
libcublas.so.13,libcublasLt.so.13) on the library path, from a CUDA 13 toolkit or thenvidia-cublaspip wheel. All three precision modes are tested with wheels from 13.0.0 to 13.8.2. - Stable Rust (edition 2024), to build from source
libcuda and cuBLAS load at run time. A missing driver, GPU or cuBLAS makes
Sam3::load return an error rather than panic. cuBLAS opens by those two sonames, so
a copy already in the process, such as the pip wheel the Python package loads, is
reused; otherwise the loader's search order picks one (LD_LIBRARY_PATH, then the
ldconfig cache).
scripts/check-cublas-versions.sh runs the Python package's tests on a GPU against the
oldest cuBLAS 13 wheel, the one PyTorch's cu130 wheels pin and the newest.
Cargo features:
online(default):Sam3::from_pretrainedanddownload_model, which fetch the weights from Hugging Facecli: thesam3rscommandnvtx: NVTX ranges per pipeline stage for Nsight Systems. The only feature that needs a CUDA toolkit to build, for its headers (CUDA_HOME, default/usr/local/cuda).
Install
The sam3rs command for Linux x86_64 (glibc 2.28 or newer), from the latest GitHub
release into ~/.local/bin:
|
--to DIR installs somewhere else and --tag vX.Y.Z picks a release. The binary
finds cuBLAS on the library path, so it needs a CUDA 13 toolkit. Without one, run the
Python package's sam3rs command, which takes the same arguments and brings cuBLAS
from the nvidia-cublas wheel (about 470 MB, downloaded on the first run):
pip install "sam3rs[cuda]" installs the same command and the Python API. Building
the command from source needs no CUDA toolkit, though running it needs cuBLAS as the
release binary does:
As a library, cargo add sam3rs.
Weights
SAM 3 weights are under Meta's SAM License and aren't part of this repository or its
packages. facebook/sam3 on Hugging Face is
gated: accept Meta's terms on the model page while logged in, then run
hf auth login or set HF_TOKEN to a token of that account.
Sam3::from_pretrained and the CLI without --model download the checkpoint into
the Hugging Face cache on first use.
For the tests and benchmarks, scripts/fetch-models.sh downloads the checkpoint into
models/sam3 with the same token, fetches the test images into models/images, and
checks the weights against the official sha256 hashes:
Usage
# text prompt: every dog, with an overlay
# box exemplars (pixels x0,y0,x1,y1), optionally negative ones
# clicks for a single object
Each detection prints as a JSON line with its score and box. Without --model DIR,
the command downloads the weights from Hugging Face. As a library:
use ;
// or Sam3::load(dir, 0, Precision::Bf16) for a local copy
let model = from_pretrained?;
let image = open?;
let embedding = model.encode?;
let dogs = model.detect?;
for in dogs.iter.enumerate
// box exemplars: find more objects like the positive box
let exemplar = from_xyxy?;
let similar = model.detect?;
All of a call's masks share one host buffer. Dropping the result hands that memory back to the model, so the next call writes its masks into pages that are already mapped. Faulting in fresh memory for 27 masks at 2560 px took about 27 ms.
Many objects from clicks
segment_points segments one object from its clicks. To segment several objects in
the same image, pass one prompt per object to segment_points_batch. It decodes
them together on the GPU and returns the same scores and masks as one
segment_points call per prompt, all in one host buffer:
use ;
// one prompt per object; each may hold several clicks, background ones included
let dog = ;
let other = ;
let objects = model.segment_points_batch?;
for in objects.scores.iter.enumerate
Prompts with the same click count share every decoder launch, up to POINT_BATCH
(16) at a time. A prompt with a different count starts a separate batch. Scores match
the single calls exactly in fp32. In tf32 and bf16, cuBLAS picks other GEMM
kernels for the taller batch, so scores can move by about as much as tf32 and
bf16 already differ from fp32.
Video tracking
Click objects on the first frame, then feed the frames one at a time:
use ;
let model = from_pretrained?;
let frames: = vec!;
// one Vec of clicks per object, in the first frame's pixels
let clicks = vec!;
let = model.track?;
for frame in &frames
The tracker keeps a fixed-size memory bank, so memory use doesn't grow with the video's length. Clicks go on frame 0 only; new clicks on later frames, adding objects mid-video and SAM 3.1's multiplex tracker aren't implemented.
Results
Milliseconds on an RTX 5090 (driver 595.99, CUDA 13.2), following the osam benchmark
protocol: dogs.jpg (2560x1600), one warm-up, then the median of 5. Model load and JPEG
decoding are excluded. Prompts are one point (1439, 504), one box
(1233, 376, 1649, 691), that box plus a negative box (1400, 500, 1500, 600), and the text
"dog". Each prompt returns full-resolution masks on the host. The text prompt runs the
text encoder on every call. Box prompts use the fixed text "visual", whose features
sam3rs encodes once and reuses, while osam and PyTorch encode it again on every call.
| Runtime | Encode | Point | Box | Neg box | Text |
|---|---|---|---|---|---|
| osam 0.9.2, ONNX Runtime 1.31.1 CUDA (FP32, TF32 matmuls) | 134.4 | 19.6 | 34.0 | 34.1 | 35.7 |
sam3rs tf32 |
63.7 (2.1x) | 1.4 (14x) | 7.8 (4.4x) | 7.8 (4.4x) | 9.2 (3.9x) |
PyTorch 2.14.1 BF16 (official Sam3Processor) |
39.4 | 36.2 | 21.8 | 22.1 | 20.7 |
sam3rs bf16 |
30.9 (1.28x) | 1.5 (24x) | 5.4 (4.0x) | 5.4 (4.1x) | 6.9 (3.0x) |
| PyTorch 2.14.1 FP32 (TF32 off) | 129.5 | 25.4 | 33.3 | 33.6 | 31.7 |
sam3rs fp32 |
117.6 (1.10x) | 1.7 (15x) | 26.6 (1.25x) | 26.9 (1.25x) | 28.8 (1.10x) |
Each sam3rs row sits under the runtime that uses the same numerics, and its speedup
over that row is in parentheses: tf32 against osam, both FP32 with TF32 matmuls;
bf16 against PyTorch BF16; fp32 against PyTorch FP32 with TF32 off.
The sam3rs rows come from three runs of one queued session, reporting the median of each column. The PyTorch rows come from an earlier queued session on the same machine, three runs each, and the osam row from another.
Where the time goes:
- Image features stay on the GPU. osam downloads 133 MB of features at encode and re-uploads 117 MB on every detector call, and part of its point decoder runs on the CPU.
- The point path is small kernels, hole filling included: a union-find over the low-res masks in four launches. PyTorch's hole filling costs about 0.2 ms; most of its point prompt is copying masks to the host. It upsamples all three candidates of both passes to full size as float32 and copies them into freshly allocated host memory: at 2560x1600, two 49 MB copies, 28 ms of a 36-38 ms prompt. sam3rs keeps the first pass on the GPU and downloads one mask at one byte per pixel.
- PyTorch's box and text prompts launch about 2.8k kernels each and keep the GPU 46% busy. sam3rs issues about 0.6k GPU operations for a box prompt and 0.95k for a text prompt, and keeps the GPU about 91% busy. A BF16 box prompt spends about 1.8 ms in the fusion encoder, 1.6 ms in the mask head and 1.2 ms in the DETR decoder, then about 0.3 ms per kept mask copying it to the host, into memory reused from earlier calls. The Python package views those bytes as numpy bool arrays without copying them.
- Encode is GEMM-bound. The backbone MLP and qkv GEMMs run at 195-210 TFLOPS, close to the card's dense BF16 rate with FP32 accumulation, so the margin over PyTorch BF16 is small. The rest is fused bias and GELU epilogues, BF16 activations fed straight into the next GEMM, and the fused attention kernel.
- Strict FP32 has no tensor cores to lean on, so its encode is bound by the FP32 pipes. The backbone's attention runs fused on them and takes about 16 ms of the encode, where materializing the scores took 42 ms. Its linears run the same CUTLASS SIMT GEMMs as PyTorch's, with bias and GELU fused into their epilogues.
The bench example reproduces the sam3rs rows:
Video
Steady-state milliseconds a frame on the same RTX 5090, BF16, one click per object on
frame 0, masks at the video's size on the host: the median over frames 8 onward of a
64-frame 720p clip (horses), after one warm-up run. PyTorch runs upstream's
tracker-only path (build_sam3_video_model().tracker) under BF16 autocast; ComfyUI is
the core SAM3_VideoTrack node with SAM 3.1 multiplex FP16 weights, its node time over
the frame count.
| Runtime | 1 object | 3 objects | Peak VRAM |
|---|---|---|---|
| PyTorch 2.14.1 eager | 45.5 | 60.1 | 5.6-6.0 GB |
PyTorch 2.14.1 torch.compile |
42.3 | 55.9 | 5.8-6.5 GB |
| PyTorch 2.14.1 CUDA graphs | 45.1 | 59.7 | 10-20 GB |
| ComfyUI core (SAM 3.1 multiplex) | 44.7 | 46.3 | 3.8-4.0 GB |
sam3rs bf16 |
34.8 (1.31x) | 44.2 (1.36x) | 3.4-3.7 GB |
sam3rs fp32 |
148.5 | 214.0 | 5.3-5.4 GB |
The speedups in parentheses are over PyTorch eager. Over the fastest other runtime in
each column, torch.compile for 1 object and ComfyUI for 3, they are 1.22x and 1.05x.
At 1080p and 256 frames sam3rs takes 35.7 and 45.6 ms. The image encoder and tracker neck, shared by all objects, are about 30 ms of each frame. Each object adds 4.7 ms, about 1.8 of them in the memory cross-attention kernel and 0.44 in the memory self-attention. The GPU is about 99% busy, so CUDA graphs would save little. ComfyUI's multiplex tracker costs the same for 1 to 16 objects.
--frames is a directory of decoded frames, read in name order. Clicks come from
tests/video_prompts.json (--prompts). --outputs DIR writes the last run's
scores and masks for an A/B gate.
scripts/video_reference.py bench times the PyTorch rows. scripts/bench_comfy.py video-click horses-720p-64:1 horses-720p-64:3 times the ComfyUI row through a stock
ComfyUI server, with per-node times from scripts/comfy_launch.py.
scripts/nsys_frames.py turns an nsys capture of either into per-frame GPU busy
time, launches and stage times.
Parity
tests/parity.rs runs four images and 13 prompts (text, box, negative box, points)
through all three precision modes and compares against PyTorch FP32 outputs of the official
implementation. The fixtures come from scripts/reference.py dump. The gate: the same
kept DETR queries, scores within 0.01, box corners within 0.5% of the image's longer
side, and mask IoU of at least 0.99.
Worst case over 43 checks:
| Mode | Max score delta | Max box delta | Min mask IoU |
|---|---|---|---|
fp32 |
0.0008 | 0.02 px | 0.9987 |
tf32 |
0.0052 | 0.19 px | 0.9987 |
bf16 |
0.0070 | 1.26 px | 0.9964 |
The ignored stage_tests::stages unit test (cargo test --release --lib -- --ignored)
compares every intermediate stage (pixels, trunk, FPN levels, text features, scores,
boxes, mask logits) for one fixture.
The video tracker's gates are ignored tests in src/video_tests.rs, against fixtures
from scripts/video_reference.py dump: upstream's tracker in true FP32 on a clip, with
every stage's inputs and outputs on a few frames.
video_stagesruns each stage (memory attention, memory encoder, decoder, pointer MLP) on the reference's exact inputs: rel L2 within 1e-4 infp32and 5e-3 intf32andbf16(4e-2 for the backbone and neck, the image path's encoder).video_e2etracks the whole clip and needs the reference's memory length, pointer count and object presence on every frame, plus a mask IoU per object against FP32: at least 0.99 infp32, and in the reduced modes no worse than PyTorch's own BF16 run (mean minus 0.01, and its worst frame). Up to two frames an object are excused from the worst-frame limit where the decoder's top two candidates were within 0.005 predicted IoU.video_e2einbf16also runs each checked frame's memory attention over the cached per-frame keys against the FP32 model's on the same inputs (rel L2 within 5e-3).attention_d256checks the two fused attention kernels (values of 256 for self-attention, of 64 for the memory cross-attention) against an f64 softmax: rel L2 within 1e-3.
| Mode | Worst stage rel L2 | Mean IoU per object (horses, 3 objects) | Grazing (1 object) |
|---|---|---|---|
fp32 |
2.3e-6 | 0.9999 / 1.0000 / 1.0000 | 1.0000 |
tf32 |
8.0e-4 | 0.9996 / 0.9987 / 1.0000 | 0.9998 |
bf16 |
4.4e-3 | 0.9949 / 0.9977 / 0.9999 | 0.9997 |
| PyTorch BF16 | 0.9760 / 0.9918 / 0.9963 | 0.9966 |
SAM3RS_VIDEO=models/video-parity/horses-720p-64-o3-fp32.safetensors SAM3RS_PRECISION=bf16 \
Known differences
- JPEG decoding uses the
imagecrate, which differs from PIL's libjpeg-turbo on a few percent of bytes. Scores move by up to a few thousandths. The parity test feeds the reference's decoded RGB to isolate the model. - Text cleaning lowercases and collapses whitespace but doesn't port
ftfyor HTML unescaping. - Video tracking follows SAM 3's tracker-only path ("SAM 3 for SAM 2 video task") from clicks on the first frame. The session predictors' per-frame detector, text prompts on video, and SAM 3.1 aren't implemented.
- In
bf16, upstream rounds the memory's sine position encoding to BF16 under autocast, and sam3rs keeps it in FP32. - The lowest
bf16mask IoU (0.9914, a point prompt) comes from hole filling. A rounding difference flips one low-resolution pixel, which cuts a 64-pixel background region off from the rest. Hole filling turns background regions of up to 256 pixels into foreground, so that region becomes part of the mask. The score is unaffected.
Not yet verified
- Only measured on one RTX 5090 (sm_120) with CUDA 13.2. The compute_80 PTX that other GPUs load passes parity on the 5090 but hasn't run on another architecture.
- Video parity covers two clips (720p, 64 frames, 1 and 3 objects).
- Timings use one image and one prompt of each kind. Scaling with many box exemplars or long text prompts hasn't been measured.
- Parity covers four images and 13 prompts, and the BF16 worst score delta is within about 0.005 of the 0.01 gate.
License
sam3rs is licensed under the Apache License 2.0. It is an independent implementation and contains no SAM 3 weights. Meta distributes the weights under the SAM License, and each user accepts it on Hugging Face before downloading them.