aitune.torch.backend.tensorrt.tensorrt_backend
aitune.torch.backend.tensorrt.tensorrt_backend
TensorRT backend.
Module Contents
Classes
Data
API
Bases: enum.Enum
Mode how TRT optimization profiles will be generated for TensorRT engine.
Bases: Backend, TensorRTRunner
TensorRT backend for model acceleration.
This class provides functionality to build and run TensorRT engines from PyTorch models. It handles the process of exporting models to ONNX and then converting them to TensorRT engines for optimized inference.
Use graphs only for fixed-shape profiles, until capture fails for this instance.
Activate the TensorRT engine.
Build the TensorRT model.
This method will export the module to ONNX and build a TensorRT engine from it using Polygraphy.
Parameters:
The module to build the TensorRT model for.
The name of the model.
The graph spec of the model.
Recorded samples for the model.
The cache directory to store the TensorRT model.
Returns: Backend
The backend with built TensorRT model.
Build the TensorRT model ModelOpt ONNX quantization.
This method will export the module to ONNX, quantize the exported model using ModelOpt ONNX quantization and build a TensorRT engine from it using Polygraphy.
Parameters:
The module to build the TensorRT model for.
The name of the model.
The graph spec of the model.
Recorded samples for the model.
The cache directory to store the TensorRT model.
Build the TensorRT model.
This method will export the module to ONNX and build a TensorRT engine from it using Polygraphy.
Parameters:
The module to build the TensorRT model for.
The name of the model.
The graph spec of the model.
Recorded samples for the model.
The cache directory to store the TensorRT model.
Build the TensorRT model ModelOpt torch quantization.
This method will quantize module using ModelOpt torch quantization, export the quantized model to ONNX and build a TensorRT engine from it using Polygraphy.
Parameters:
The module to build the TensorRT model for.
The name of the model.
The graph spec of the model.
Recorded samples for the model.
The cache directory to store the TensorRT model.
Build the TensorRT model.
This method will export the module to ONNX and build a TensorRT engine from it using Polygraphy.
Parameters:
The module to build the TensorRT model for.
The name of the model.
The graph spec of the model.
Recorded samples for the model.
The cache directory to store the TensorRT model.
Attach an independent output allocator to an execution context.
Deactivate the TensorRT engine.
Deploys the backend.
After deploying, the backend is ready to do inference. Backend cannot be deactivated anymore.
Execute TensorRT normally, propagating engine execution failures.
Find the first matching profile, including validation for a single profile.
Parameters:
Dictionary mapping input names to tensors
Get the output object from the module and sample.
Note: to avoid case where a module returns a reference to the input argument, we make a deep copy of the output object.
Parameters:
PyTorch module
Sample input to use for model inference.
Returns: Any
The output object from the module.
Create TensorRT optimization profiles.
Returns: list[Profile]
List of Polygraphy Profile objects
Get the shapes for the input tensors.
Parameters:
Input graph spec
Returns: tuple[dict[str, tuple[int, ...]], dict[str, tuple[int, ...]], dict[str, tuple[int, ...]]]
Tuple of dictionaries of minimum, optimal, and maximum input shapes
Infer using the TensorRT engine.
Parameters:
Input tensors
Named input tensors
Returns: Any
Model outputs
Load the TensorRT optimization profiles from a file.
Prepare input tensors from args and kwargs.
Parameters:
Positional input tensors
Named input tensors
Returns:
Dictionary mapping input names to tensors
Raises:
ValueError: If inputs are missing or incorrect
Prepare the ONNX model path.
Parameters:
The cache directory to store the ONNX model.
The suffix of the ONNX model.
Returns: Path
The ONNX model path
Prepare the outputs for return.
This method will prepare the outputs for return according to the original model’s output structure. Tensors are retrieved from the output allocator with correct shapes.
Returns: Any
The outputs in the same format as the original model.
Prepare the TensorRT engine path.
Parameters:
The cache directory to store the TensorRT model.
Prepare the TensorRT optimization profiles path.
Parameters:
The cache directory to store the TensorRT optimization profiles.
Save the TensorRT optimization profiles to a file.
Set shapes and memory addresses for input tensors.
Parameters:
Dictionary mapping input names to tensors
Select an ordinary context or the static profile’s cached graph context.
Return a timing cache path that is safe for this process.
Returns the description of the backend.
Creates a backend from a state_dict.
Create profiles from samples or from graph_spec.
If self._config.profiles is a list, return the user provided profiles. If self._config.profiles is ProfileMode.SINGLE, create a single profile from the graph spec. If self._config.profiles is ProfileMode.SAMPLES_USED, create profiles from shapes seen in samples. Explicit module shape definitions always produce a single authoritative profile and cannot be combined with user-provided TensorRT profiles.
Parameters:
Input graph spec
Recorded samples for the model.
Returns: list[Profile]
List of The Polygraphy Profile objects
Returns the key of the backend.
Returns the state_dict of the backend.
Bases: BackendConfig
Configuration for TensorRT backend.
Validate the graph cache capacity and policy.
Initialise config from a plain dict (e.g. parsed from YAML).
profiles may be passed as a string (ProfileMode value) or a
list of profile dicts and will be reconstructed automatically.
quantization_config may be passed as a dict with a _type key
(produced by to_dict()) and will be reconstructed automatically.
Convert dict to list of TensorRTProfile.
Reconstruct a quantization config from a dict produced by to_dict().
The dict must contain a _type key with the class name.
Convert TensorRTBackendConfig to dictionary.
Bases: BackendBuildStep
Identifiers for discrete sub-steps of a TensorRT backend build.
TensorRT runner for model acceleration.
This class provides functionality to run TensorRT engines from PyTorch models.