runtimes
BaseRuntime
#
Abstract base class for runtime implementations.
This class defines the interface that all runtime implementations must follow. It provides methods for model initialization, inference, and performance benchmarking.
Attributes:
Name | Type | Description |
---|---|---|
model_path |
str
|
Path to the model file. |
opts |
Any
|
Runtime-specific options. |
model_info |
RemoteModelInfo
|
Metadata about the model. |
Source code in focoos/infer/runtimes/base.py
10 11 12 13 14 15 16 17 18 19 20 21 22 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 |
|
__call__(im)
abstractmethod
#
Run inference on the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im
|
ndarray
|
Input image as a numpy array. |
required |
Returns:
Type | Description |
---|---|
list[ndarray]
|
np.ndarray: Model output as a numpy array. |
Source code in focoos/infer/runtimes/base.py
34 35 36 37 38 39 40 41 42 43 44 45 |
|
__init__(model_path, opts, model_info)
#
Initialize the runtime with model path, options and metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path
|
str
|
Path to the model file. |
required |
opts
|
Any
|
Runtime-specific configuration options. |
required |
model_info
|
RemoteModelInfo
|
Metadata about the model. |
required |
Source code in focoos/infer/runtimes/base.py
23 24 25 26 27 28 29 30 31 32 |
|
benchmark(iterations, size)
abstractmethod
#
Benchmark the model performance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations
|
int
|
Number of inference iterations to run. |
required |
size
|
float
|
Input image size for benchmarking. |
required |
Returns:
Name | Type | Description |
---|---|---|
LatencyMetrics |
LatencyMetrics
|
Performance metrics including mean, median, and percentile latencies. |
Source code in focoos/infer/runtimes/base.py
54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
get_info()
abstractmethod
#
Get the engine and device name.
Source code in focoos/infer/runtimes/base.py
47 48 49 50 51 52 |
|
load_runtime(runtime_type, model_path, model_info, warmup_iter=50)
#
Creates and returns a runtime instance based on the specified runtime type. Supports both ONNX and TorchScript runtimes with various execution providers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
runtime_type
|
RuntimeTypes
|
The type of runtime to use. Can be one of: - ONNX_CUDA32: ONNX runtime with CUDA FP32 - ONNX_TRT32: ONNX runtime with TensorRT FP32 - ONNX_TRT16: ONNX runtime with TensorRT FP16 - ONNX_CPU: ONNX runtime with CPU - ONNX_COREML: ONNX runtime with CoreML - TORCHSCRIPT_32: TorchScript runtime with FP32 |
required |
model_path
|
str
|
Path to the model file (.onnx or .pt) |
required |
model_metadata
|
ModelMetadata
|
Model metadata containing task type, classes etc. |
required |
warmup_iter
|
int
|
Number of warmup iterations before inference. Defaults to 0. |
50
|
Returns:
Name | Type | Description |
---|---|---|
BaseRuntime |
BaseRuntime
|
A configured runtime instance (ONNXRuntime or TorchscriptRuntime) |
Raises:
Type | Description |
---|---|
ImportError
|
If required dependencies (torch/onnxruntime) are not installed |
Source code in focoos/infer/runtimes/load_runtime.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 |
|
ONNXRuntime
#
Bases: BaseRuntime
ONNX Runtime wrapper for model inference with different execution providers.
This class implements the BaseRuntime interface for ONNX models, supporting various execution providers like CUDA, TensorRT, OpenVINO, and CoreML. It handles model initialization, provider configuration, warmup, inference, and performance benchmarking.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the model derived from the model path. |
opts |
OnnxRuntimeOpts
|
Configuration options for the ONNX runtime. |
model_info |
RemoteModelInfo
|
Metadata about the model. |
ort_sess |
InferenceSession
|
ONNX Runtime inference session. |
active_providers |
list
|
List of active execution providers. |
dtype |
dtype
|
Input data type for the model. |
Source code in focoos/infer/runtimes/onnx.py
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 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 |
|
__call__(im)
#
Run inference on the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im
|
ndarray
|
Input image as a numpy array. |
required |
Returns:
Type | Description |
---|---|
list[ndarray]
|
list[np.ndarray]: Model outputs as a list of numpy arrays. |
Source code in focoos/infer/runtimes/onnx.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
benchmark(iterations=50, size=640)
#
Benchmark the model performance.
Runs multiple inference iterations and measures execution time to calculate performance metrics like FPS, mean latency, and other statistics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations
|
int
|
Number of inference iterations to run. Defaults to 20. |
50
|
size
|
int or tuple
|
Input image size for benchmarking. Defaults to 640. |
640
|
Returns:
Name | Type | Description |
---|---|---|
LatencyMetrics |
LatencyMetrics
|
Performance metrics including FPS, mean, min, max, and std latencies. |
Source code in focoos/infer/runtimes/onnx.py
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 |
|
TorchscriptRuntime
#
Bases: BaseRuntime
TorchScript Runtime wrapper for model inference.
This class implements the BaseRuntime interface for TorchScript models, supporting both CPU and CUDA devices. It handles model initialization, device placement, warmup, inference, and performance benchmarking.
Attributes:
Name | Type | Description |
---|---|---|
device |
device
|
Device to run inference on (CPU or CUDA). |
opts |
TorchscriptRuntimeOpts
|
Configuration options for the TorchScript runtime. |
model |
ScriptModule
|
Loaded TorchScript model. |
model_info |
RemoteModelInfo
|
Metadata about the model. |
Source code in focoos/infer/runtimes/torchscript.py
15 16 17 18 19 20 21 22 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 |
|
__call__(im)
#
Run inference on the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im
|
ndarray
|
Input image as a numpy array. |
required |
Returns:
Type | Description |
---|---|
list[ndarray]
|
list[np.ndarray]: Model outputs as a list of numpy arrays. |
Source code in focoos/infer/runtimes/torchscript.py
57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
benchmark(iterations=20, size=640)
#
Benchmark the model performance.
Runs multiple inference iterations and measures execution time to calculate performance metrics like FPS, mean latency, and other statistics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterations
|
int
|
Number of inference iterations to run. Defaults to 20. |
20
|
Returns:
Name | Type | Description |
---|---|---|
LatencyMetrics |
LatencyMetrics
|
Performance metrics including FPS, mean, min, max, and std latencies. |
Source code in focoos/infer/runtimes/torchscript.py
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 |
|