backends
sleap_nn.inference.layers.backends
¶
Runtime backends for inference layers.
Exported:
- :class:
ModelBackend— Protocol every backend implements. - :class:
TorchBackend— PyTorchnn.Moduleruntime with optional compile / FP16 / Conv-BN fusion. - :class:
ONNXBackend— ONNX Runtime backend. Wraps an exported.onnxfile; peak finding is baked into the graph. - :class:
TensorRTBackend— TensorRT backend (CUDA-only, requirestensorrtextra).
Modules:
| Name | Description |
|---|---|
base |
|
onnx_backend |
|
tensorrt_backend |
|
torch_backend |
|
Classes:
| Name | Description |
|---|---|
ModelBackend |
Runtime-agnostic forward-pass contract. |
ONNXBackend |
ONNX Runtime backend conforming to :class: |
TensorRTBackend |
Native TensorRT engine backend conforming to :class: |
TorchBackend |
PyTorch |
ModelBackend
¶
Bases: Protocol
Runtime-agnostic forward-pass contract.
Any object that satisfies this protocol can power any
InferenceLayer subclass. Verify with isinstance(obj, ModelBackend)
at construction time.
Methods:
| Name | Description |
|---|---|
__call__ |
Run the model forward pass. |
warmup |
Run dummy forward passes to prime the backend. |
Attributes:
| Name | Type | Description |
|---|---|---|
device |
str
|
Device the backend runs on. |
does_baked_postproc |
bool
|
|
Source code in sleap_nn/inference/layers/backends/base.py
device
property
¶
Device the backend runs on. "cpu", "cuda", "cuda:0", "mps".
does_baked_postproc
property
¶
True if this backend already performs peak finding internally.
ONNX and TensorRT export wrappers bake normalization + peak finding
+ (optionally) PAF scoring into the graph and return precomputed
peaks. When this property is True, the wrapping InferenceLayer
must skip its own Python-side peak finding and only apply coordinate
transforms to whatever the backend returns.
For pure PyTorch (TorchBackend) this is always False.
__call__(x)
¶
Run the model forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Preprocessed input. Shape |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dict of output tensors. Keys depend on the wrapped model:
|
Source code in sleap_nn/inference/layers/backends/base.py
warmup(input_shape)
¶
Run dummy forward passes to prime the backend.
Particularly important on MPS (≈73× cold-start ratio per the benchmark suite) and CUDA-with-compile (where the first call triggers JIT compilation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape
|
Tuple[int, ...]
|
Shape of the dummy tensor to allocate. |
required |
Source code in sleap_nn/inference/layers/backends/base.py
ONNXBackend
¶
ONNX Runtime backend conforming to :class:ModelBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Path to an exported |
required | |
device
|
|
required | |
providers
|
Explicit override for the execution-provider list. If
|
required |
Notes
does_baked_postproc=True — the ONNX wrappers in
sleap_nn/export/wrappers/ bake peak finding, normalization,
and (top-down) crop extraction into the graph. Layer postprocess
methods take the "peaks" / "peak_vals" keys directly from
the session output instead of running Python peak finding.
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Load the ONNX session and cache I/O metadata. |
__call__ |
Run the ONNX session. |
from_export_dir |
Load an ONNX backend from an export directory containing |
warmup |
Run a single dummy forward to prime the runtime / GPU caches. |
Attributes:
| Name | Type | Description |
|---|---|---|
does_baked_postproc |
bool
|
ONNX wrappers bake peak finding into the graph. |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
does_baked_postproc
property
¶
ONNX wrappers bake peak finding into the graph.
__attrs_post_init__()
¶
Load the ONNX session and cache I/O metadata.
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
__call__(x)
¶
Run the ONNX session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Cast to the session's expected dtype before
handoff (most exported wrappers expect |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dict mapping the session's output names to torch tensors.
Layer postprocess methods then look up |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
from_export_dir(export_dir, device='auto')
classmethod
¶
Load an ONNX backend from an export directory containing model.onnx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_dir
|
Union[str, Path]
|
Directory written by |
required |
device
|
str
|
Device hint for execution-provider selection. |
'auto'
|
Returns:
| Type | Description |
|---|---|
'ONNXBackend'
|
A configured |
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
warmup(input_shape)
¶
Run a single dummy forward to prime the runtime / GPU caches.
Source code in sleap_nn/inference/layers/backends/onnx_backend.py
TensorRTBackend
¶
Native TensorRT engine backend conforming to :class:ModelBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine_path
|
Path to a serialized TRT engine file ( |
required | |
device
|
Must be |
required |
Notes
Constructing this backend imports tensorrt lazily. On a host
without CUDA / tensorrt installed, the constructor raises with
a clear pointer at the right install extra ([tensorrt]).
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Load the TRT engine + create an execution context. |
__call__ |
Execute the TRT engine on |
from_export_dir |
Load a TRT backend from an export directory containing |
warmup |
Run a single dummy forward to prime the engine + GPU caches. |
Attributes:
| Name | Type | Description |
|---|---|---|
does_baked_postproc |
bool
|
TRT engines exported from our wrappers bake peak finding. |
Source code in sleap_nn/inference/layers/backends/tensorrt_backend.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
does_baked_postproc
property
¶
TRT engines exported from our wrappers bake peak finding.
__attrs_post_init__()
¶
Load the TRT engine + create an execution context.
Source code in sleap_nn/inference/layers/backends/tensorrt_backend.py
__call__(x)
¶
Execute the TRT engine on x (must be on CUDA already).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dict mapping engine output names to torch tensors on CUDA. |
Source code in sleap_nn/inference/layers/backends/tensorrt_backend.py
from_export_dir(export_dir)
classmethod
¶
Load a TRT backend from an export directory containing *.trt.
Source code in sleap_nn/inference/layers/backends/tensorrt_backend.py
warmup(input_shape)
¶
Run a single dummy forward to prime the engine + GPU caches.
Source code in sleap_nn/inference/layers/backends/tensorrt_backend.py
TorchBackend
¶
PyTorch nn.Module backend with opt-in compile / FP16 / fusion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
The forward-pass owner. Typically a Lightning module
( |
required | |
device
|
|
required | |
use_compile
|
Wrap the model in |
required | |
compile_mode
|
Forwarded to |
required | |
use_fp16
|
Run the heavy forward ops in float16 via |
required | |
fuse_layers
|
Fold |
required | |
warmup_iterations
|
Number of dummy forwards to run inside
:meth: |
required |
Notes
slots=False is intentional — attrs-with-slots doesn't compose
with Lightning's __getattr__ (which forwards to nn.Module).
Using a regular class lets users mix the two without surprise.
Methods:
| Name | Description |
|---|---|
__attrs_post_init__ |
Validate device / feature combination, fuse, and (optionally) compile. |
__call__ |
Forward pass. Always returns a dict for protocol uniformity. |
warmup |
Prime the backend with |
Attributes:
| Name | Type | Description |
|---|---|---|
does_baked_postproc |
bool
|
PyTorch returns raw confmaps; peak finding stays in Python. |
Source code in sleap_nn/inference/layers/backends/torch_backend.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
does_baked_postproc
property
¶
PyTorch returns raw confmaps; peak finding stays in Python.
__attrs_post_init__()
¶
Validate device / feature combination, fuse, and (optionally) compile.
Source code in sleap_nn/inference/layers/backends/torch_backend.py
__call__(x)
¶
Forward pass. Always returns a dict for protocol uniformity.
Source code in sleap_nn/inference/layers/backends/torch_backend.py
warmup(input_shape)
¶
Prime the backend with warmup_iterations dummy forwards.