S2M2
A transformer-based stereo family in four sizes (S, M, L, XL). Non-commercial licence.
stable Fork of junhong-3dv/s2m2@8f3bd78 · backends: portable, kernels
- Paper S2M2: Scalable Stereo Matching Model for Reliable Depth Estimation ICCV 2025 · first released 17 Jul 2025
Paper 2025 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[nc]"
from fastforks import stereo
model = stereo.load("s2m2-s") # kernels on a supported GPU, else portable PyTorch
result = model(left, right)
result.disparity # also: confidence, occlusion
result.backend # what ran: mode, device, dtype, kernels The weights are non-commercial: fastforks downloads them only with FASTFORKS_NONCOMMERCIAL=1 set.
fastforks bench s2m2-s 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 | 583 | 0.60× | 2,775 MiB/3,008 MiB | – | – |
| Original fp16torch.autocast(fp16) around the forward | fp16 | 350 | – | 1,759 MiB/1,986 MiB | – | – |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 348 | 1.0× | 1,733 MiB/1,930 MiB | 0.2 | 0.35 |
| Kernels | fp16 | 113 | 3.1× | 686 MiB/820 MiB | 0.2 | 0.11 |
| Portable + torch.compile | 186 | 1.9× | 1,579 MiB/1,710 MiB | 96 (build) | – | |
| TensorRT fp16 | fp16 | 137 | 2.6× | 980 MiBenginenot comparable | 363 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-01 to 2026-11-03. 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-12 | Original | fp32 | cannot run here | – | 994x1482 | |
| Portable | fp16 | 1607 | 5,991 MiB process | matches | 994x1482 | |
| Portable | fp32 | 1707 | 8,051 MiB process | matches | 994x1482 | |
| Kernels | fp16 | not implemented | – | 994x1482 | ||
| NVIDIA L4 CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 413 | 1,706 MiB/1,908 MiB | – | 994x1482 |
| Kernels | fp16 | 163 | 660 MiB/882 MiB | – | 994x1482 | |
| NVIDIA L40S CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 125 | 1,706 MiB/1,908 MiB | – | 994x1482 |
| Kernels | fp16 | 56.8 | 660 MiB/882 MiB | – | 994x1482 | |
| NVIDIA A10 CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 284 | 1,706 MiB/1,908 MiB | – | 994x1482 |
| Kernels | fp16 | 137 | 655 MiB/942 MiB | – | 994x1482 | |
| NVIDIA A100-SXM4-40GB CUDA, torch 2.14.1+cu130, 2026-11-07 | Portable | fp16 | 124 | 1,706 MiB/1,908 MiB | – | 994x1482 |
| Kernels | fp16 | 62.4 | 655 MiB/942 MiB | – | 994x1482 | |
| NVIDIA RTX PRO 6000 CUDA, torch 2.14.1+cu130, 2026-11-08 | Portable | fp16 | 71.2 | 1,730 MiB/2,008 MiB | – | 994x1482 |
| Kernels | fp16 | 29.0 | 683 MiB/902 MiB | – | 994x1482 | |
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
- CC-BY-NC-4.0 LICENSE.md
- Weights
- CC-BY-NC-4.0 source
- Training data
- README: extended training data incl. transparent objects
- code: CC-BY-NC-4.0 (noncommercial)
- weights: CC-BY-NC-4.0
Our reading of the licences, not legal advice.
Weights
CH128NTR1.pth 107 MB· hosted on Hugging Facefastforks downloads and checks these files on first use (fastforks download s2m2-s). 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.
ETH3D two-view train
bad > 1 px % / EPEtraining data not stated: possibly seen
| Run | dtype | bad px % ↓ | EPE ↓ | Δ bad px % vs fp32 original | pairs | ms / pair |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 1.10 | 0.193 | reference | 27 | 128 |
| Original fp16 | fp16 | 1.07 | 0.198 | +0.03 | 27 | 89.7 |
| Portable fp32 | fp32 | 1.10 | 0.193 | ±0.00 | 27 | 122 |
| Portable | fp16 | 1.12 | 0.193 | −0.02 | 27 | 74.9 |
| Kernels | fp16 | 1.10 | 0.193 | ±0.00 | 27 | 38.3 |
| Original .half() | fp16 | fails: RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward) | ||||
Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16, original .half(): RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-05; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 468fba10, 2026-11-05.
Middlebury 2014 trainingH
bad > 2 px % / EPEtraining data not stated: possibly seen
| Run | dtype | bad px % ↓ | EPE ↓ | Δ bad px % vs fp32 original | pairs | ms / pair |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 3.23 | 0.652 | reference | 15 | 492 |
| Original fp16 | fp16 | 3.23 | 0.657 | ±0.00 | 15 | 308 |
| Portable fp32 | fp32 | 3.23 | 0.652 | ±0.00 | 15 | 474 |
| Portable | fp16 | 3.24 | 0.652 | −0.01 | 15 | 294 |
| Kernels | fp16 | 3.24 | 0.652 | −0.01 | 15 | 98.0 |
| Original .half() | fp16 | fails: RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward) | ||||
Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16, original .half(): RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-05; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 468fba10, 2026-11-05.
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 px and p99 <= 0.25 px.
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.002 px | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.02 px | 0.00973 px |
| GPU default dtypes, p99 |diff| | ≤ 0.25 px | 0.03415 px |
Recorded for s2m2-s on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-02, with these kernels: cudnn_haccs2m2s2m2_cutlass
Details
S2M2 (Min et al. 2025): multi-resolution transformer with optimal-transport matching; also outputs confidence and occlusion maps.
- Input / output
- Input: a rectified left / right image pair. Output: a disparity map (pixels, left view).
- Input size
- any H x W >= 32, padded to a multiple of 32; tested up to 1988x2880 (7,588 MiB allocated)
- Outputs
disparity,confidence,occlusion(on request)- Checkpoint
feature_channels=128,num_transformer=1- CUDA kernels
s2m2S2M2: attention and optimal-transport kernelss2m2_cutlassS2M2: CUTLASS convolutionscudnn_haccconvolutions with fp16 accumulation (cuDNN)
- Apple (MPS)
- fp32, fp16
Limitations
- At 994x1482 portable S2M2-S is about as fast as the original code and allocates slightly more memory; use kernels for speed and memory.
Full reference, generated from the repository
A transformer-based stereo family in four sizes (S, M, L, XL). Non-commercial licence.
Commercial use: no (code: CC-BY-NC-4.0 (noncommercial); weights: CC-BY-NC-4.0).
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
s2m2-s | Middlebury 2014 half (994x1482) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 582 350 348 113 186 137 | 2775 / 3008 1759 / 1986 1733 / 1930 686 / 820 1579 / 1710 1186 / 1240 |
s2m2-m | Middlebury 2014 half (994x1482) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 1242 666 670 248 420 285 | 3399 / 3704 2371 / 2920 2289 / 2766 876 / 1162 2197 / 2572 1958 / 2002 |
s2m2-l | Middlebury 2014 half (994x1482) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 1934 1005 1017 376 650 497 | 4333 / 5252 3248 / 4090 3234 / 3816 1249 / 1568 3169 / 3514 2910 / 2982 |
s2m2-xl | Middlebury 2014 half (994x1482) | original (fp32) original portable kernels portable + torch.compile TensorRT fp16 | 3510 1796 1818 682 1298 753 | 6940 / 8254 5606 / 6080 5217 / 6014 2092 / 2274 5594 / 6014 5283 / 5338 |
Details
- Original code: https://github.com/junhong-3dv/s2m2 at
8f3bd785df59 - Code licence: CC-BY-NC-4.0 (noncommercial); files:
LICENSE.md - Weights licence: CC-BY-NC-4.0
- Licence evidence: https://github.com/junhong-3dv/s2m2/blob/8f3bd785df593619d955203c6f2c741bef6e31df/README.md#L24
- Training data: README: extended training data incl. transparent objects
- Kernel libraries:
s2m2,s2m2_cutlass,cudnn_hacc - Variants:
s2m2-s,s2m2-m,s2m2-l,s2m2-xl - Outputs: disparity, confidence, occlusion (in
extras) - Input size: any H x W >= 32, padded to a multiple of 32
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
s2m2-s1988x2880 (7.4 GiB peak allocated, 10.6 reserved);s2m2-m1988x2880 (8.9 GiB peak allocated, 11.5 reserved);s2m2-l1988x2880 (11.4 GiB peak allocated, 14.2 reserved);s2m2-xl1024x2048 (7.3 GiB peak allocated, 8.5 reserved) s2m2-s994x1482, first call: portable + torch.compile 96 s (cold Inductor cache), TensorRT fp16 363 s (ONNX + engine).s2m2-m994x1482, first call: portable + torch.compile 126 s (cold Inductor cache), TensorRT fp16 305 s (ONNX + engine).s2m2-l994x1482, first call: portable + torch.compile 144 s (cold Inductor cache), TensorRT fp16 374 s (ONNX + engine).s2m2-xl994x1482, first call: portable + torch.compile 153 s (cold Inductor cache), TensorRT fp16 408 s (ONNX + engine).s2m2-soriginal 994x1482: torch.autocast(fp16) around the forwards2m2-moriginal 994x1482: torch.autocast(fp16) around the forwards2m2-loriginal 994x1482: torch.autocast(float16) around the calls2m2-xloriginal 994x1482: torch.autocast(fp16) around the forwards2m2-sload / first call: original 0.22 s / 0.143 s (375x1242), portable 0.22 s / 0.352† s, kernels 0.2† s / 0.114† ss2m2-mload / first call: original 3.95 s / 0.268 s (375x1242), portable 1.21 s / 0.699 s (375x1242), kernels 0.31† s / 0.249† ss2m2-lload / first call: original 1.38 s / 1.007 s, portable 2.61 s / 0.755 s (375x1242), kernels 0.58† s / 0.374† ss2m2-xlload / first call: original 4.5 s / 0.529 s (375x1242), portable 2.64 s / 0.888 s (375x1242), kernels 0.9† s / 0.681† 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
Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | original fp16 | original half | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|---|---|
s2m2-s | ETH3D two-view train, bad > 1 px % / EPE | 1.10 / 0.193 | 1.07 / 0.198 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 1.12 / 0.193 | 1.10 / 0.193 | 1.10 / 0.193 | possibly in the training data |
s2m2-s | Middlebury 2014 trainingH, bad > 2 px % / EPE | 3.23 / 0.652 | 3.23 / 0.657 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 3.24 / 0.652 | 3.23 / 0.652 | 3.24 / 0.652 | possibly in the training data |
s2m2-m | ETH3D two-view train, bad > 1 px % / EPE | 0.94 / 0.178 | 0.94 / 0.188 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 0.95 / 0.178 | 0.94 / 0.178 | 0.95 / 0.178 | possibly in the training data |
s2m2-m | Middlebury 2014 trainingH, bad > 2 px % / EPE | 3.28 / 0.651 | 3.28 / 0.656 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 3.29 / 0.651 | 3.28 / 0.651 | 3.28 / 0.651 | possibly in the training data |
s2m2-l | ETH3D two-view train, bad > 1 px % / EPE | 0.84 / 0.169 | 0.85 / 0.182 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 0.84 / 0.169 | 0.83 / 0.169 | 0.82 / 0.169 | possibly in the training data |
s2m2-l | Middlebury 2014 trainingH, bad > 2 px % / EPE | 2.88 / 0.542 | 2.84 / 0.551 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 2.84 / 0.542 | 2.88 / 0.542 | 2.85 / 0.542 | possibly in the training data |
s2m2-xl | ETH3D two-view train, bad > 1 px % / EPE | 0.53 / 0.147 | 0.52 / 0.157 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 0.53 / 0.147 | 0.53 / 0.147 | 0.52 / 0.149 | possibly in the training data |
s2m2-xl | Middlebury 2014 trainingH, bad > 2 px % / EPE | 2.34 / 0.498 | 2.33 / 0.504 | fails (RuntimeError: Expected tensor for argument #1 'input' to have the same type as tensor for argument #2 'grid'; but type torch.cuda.HalfTensor does not equal torch.cuda.FloatTensor (while checking arguments for cudnn_grid_sampler_forward)) | 2.33 / 0.498 | 2.34 / 0.498 | 2.33 / 0.499 | possibly in the training data |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 468fba10, 2026-11-05.
Parity
CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 px and p99 <= 0.25 px.
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
s2m2-s | 0.00973 / 0.03415 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, s2m2@73d0d3fa70d2:sm_120, s2m2_cutlass@3aec99821257:sm_120 |
s2m2-m | 0.00618 / 0.02223 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, s2m2@73d0d3fa70d2:sm_120, s2m2_cutlass@3aec99821257:sm_120 |
s2m2-l | 0.01523 / 0.05073 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, s2m2@73d0d3fa70d2:sm_120, s2m2_cutlass@3aec99821257:sm_120 |
s2m2-xl | 0.01853 / 0.0624 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, s2m2@73d0d3fa70d2:sm_120, s2m2_cutlass@3aec99821257:sm_120 |
Limitations
- At 994x1482 portable S2M2-S is about as fast as the original code and allocates slightly more memory; use kernels for speed and memory.
Example output
One fixed input through this checkpoint, the same for every model of the task (kernels · float16 · NVIDIA GeForce RTX 5060 Ti).
Input: Motorcycle, Middlebury 2014 stereo (left and right view, downscaled 4x), Scharstein et al., Middlebury College (research dataset, no licence stated).