Module: torch_inference_workflow
Torch Inference Workflow
Workflow for running inference on Torch models.
This class is responsible for loading & running a Torch model.
Models can be loaded in two ways:
- Preloading: The model is loaded in the setup method. This happens in the
setup()
method if model source and load args are provided when the class is instantiated. - On-demand: The model is loaded with an inference request. This happens if model source
and load args are provided with the input (see the optional fields in the
TorchInferenceInput
class).
Loaded models are cached in-memory using an LRU cache. The cache size can be configured
using the TORCH_MODEL_LRU_CACHE_SIZE
environment variable.
Additional Installations
Since this workflow uses some additional libraries, you'll need to install
infernet-ml[torch_inference]
. Alternatively, you can install those packages directly.
The optional dependencies "[torch_inference]"
are provided for your convenience.
Example
from infernet_ml.utils.common_types import TensorInput
from infernet_ml.workflows.inference.torch_inference_workflow import (
TorchInferenceWorkflow,
TorchInferenceInput,
)
from infernet_ml.utils.model_loader import ModelSource, HFLoadArgs
def main():
# Instantiate the workflow
workflow = TorchInferenceWorkflow(
model_source=ModelSource.HUGGINGFACE_HUB,
load_args=HFLoadArgs(
repo_id="Ritual-Net/california-housing",
filename="california_housing.torch",
),
)
# Setup the workflow
workflow.setup()
# Run the model
result = workflow.inference(
TorchInferenceInput(
input=TensorInput(
dtype="double",
shape=(1, 8),
values=[[-122.25, 37.85, 52.0, 1627.0, 322.0, 5.64, 2400.0, 9.0]],
)
)
)
print(result.outputs)
if __name__ == "__main__":
main()
Outputs:
TorchInferenceInput
Bases: BaseModel
Input data for Torch inference workflows. If model source and load args are provided, the model is loaded. Otherwise, if the class is instantiated with a model source and load args, the model is preloaded in the setup method.
Input Format
Input format is a dictionary of input tensors. Each key corresponds to the name of
the input nodes defined in the Torch model. The values are of type TensorInput
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
|
required | |
model_source |
Optional[ModelSource]: Source of the model to be loaded |
required | |
load_args |
Optional[LoadArgs]: Arguments to be passed to the model loader |
required |
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
TorchInferenceResult
Bases: BaseModel
Pydantic model for the result of a torch inference workflow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtype |
|
required | |
shape |
Tuple[int, ...]: Shape of the output tensor |
required | |
outputs |
|
required |
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
TorchInferenceWorkflow
Bases: BaseInferenceWorkflow
Inference workflow for Torch based models. models are loaded using the default torch pickling by default(i.e. torch.load).
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
__init__(model_source=None, load_args=None, use_jit=False, *args, **kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_source |
Optional[ModelSource]
|
Optional[ModelSource]: Source of the model to be loaded |
None
|
load_args |
Optional[LoadArgs]
|
Optional[LoadArgs]: Arguments to be passed to the model loader |
None
|
use_jit |
bool
|
|
False
|
*args |
Any
|
|
()
|
**kwargs |
Any
|
|
{}
|
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
do_run_model(inference_input)
Runs the model on the input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inference_input |
TorchInferenceInput
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
TorchInferenceResult |
TorchInferenceResult
|
Result of the inference workflow |
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
do_setup()
If model source and load args are provided, preloads the model & starts the session. Otherwise, does nothing & model is loaded with an inference request.
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
do_stream(preprocessed_input)
Streaming inference is not supported for Torch models.
inference(input_data)
Inference method for the torch workflow. Overridden to add type hints.
Source code in src/infernet_ml/workflows/inference/torch_inference_workflow.py
load_torch_model(model_source, load_args, use_jit)
cached
Loads a torch model from the given source. Uses torch.jit.load()
if use_jit is set,
otherwise uses torch.load()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_source |
ModelSource
|
|
required |
load_args |
LoadArgs
|
|
required |
use_jit |
bool
|
|
required |
Returns:
Type | Description |
---|---|
Module
|
torch.nn.Module: Loaded model |