tensorrt_exporter
sleap_nn.export.exporters.tensorrt_exporter
¶
TensorRT export utilities.
Functions:
| Name | Description |
|---|---|
export_to_tensorrt |
Export a PyTorch model to TensorRT format. |
export_to_tensorrt(model, save_path, input_shape=(1, 1, 512, 512), input_dtype=torch.uint8, precision='fp16', min_shape=None, opt_shape=None, max_shape=None, workspace_size=2 << 30, method='onnx', verbose=True, static_hw=None)
¶
Export a PyTorch model to TensorRT format.
This function supports multiple compilation methods: - "onnx": Exports to ONNX first, then compiles with TensorRT (most reliable) - "jit": Uses torch.jit.trace + torch_tensorrt.compile (alternative)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The PyTorch model to export (typically an ONNX wrapper). |
required |
save_path
|
str | Path
|
Path to save the TensorRT engine (.trt file). |
required |
input_shape
|
Tuple[int, int, int, int]
|
(B, C, H, W) optimal input tensor shape. |
(1, 1, 512, 512)
|
input_dtype
|
dtype
|
Input tensor dtype (torch.uint8 or torch.float32). |
uint8
|
precision
|
str
|
Model precision - "fp32" or "fp16". |
'fp16'
|
min_shape
|
Optional[Tuple[int, int, int, int]]
|
Minimum input shape for dynamic shapes (default: batch=1, H/W halved). |
None
|
opt_shape
|
Optional[Tuple[int, int, int, int]]
|
Optimal input shape (default: same as input_shape). |
None
|
max_shape
|
Optional[Tuple[int, int, int, int]]
|
Maximum input shape (default: batch=16, H/W doubled). |
None
|
workspace_size
|
int
|
TensorRT workspace size in bytes (default 2GB). |
2 << 30
|
method
|
str
|
Compilation method - "onnx" or "jit". |
'onnx'
|
verbose
|
bool
|
Print export info. |
True
|
static_hw
|
Optional[bool]
|
Whether the engine takes only the traced height/width (a RoPE
backbone's grid is fixed at trace time). |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the exported TensorRT engine. |
Note
TensorRT models are NOT cross-platform. The exported model will only work on the same GPU architecture and TensorRT version used for export.
Source code in sleap_nn/export/exporters/tensorrt_exporter.py
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 | |