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.
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.
The data of the model.
The cache directory to store the TensorRT model.
Returns: Backend
The backend with built TensorRT model.
Create a CUDA graph for inference.
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.
The data of 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.
The data of 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.
The data of 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.
The data of the model.
The cache directory to store the TensorRT model.
Deactivate the TensorRT engine.
Deploys the backend.
After deploying, the backend is ready to do inference. Backend cannot be deactivated anymore.
Execute inference using CUDA graphs for optimized performance.
This method implements the CUDA graph capture and launch pattern:
- If no CUDA graph exists, capture one by running inference twice
- If CUDA graph exists, launch the captured graph
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
Setup the inputs for the CUDA graph.
This should be called when input shapes change or when the graph needs to be rebuilt.
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.
Store the backend configuration to a file.
Save the TensorRT optimization profiles to a file.
Set shapes and memory addresses for input tensors.
Parameters:
Dictionary mapping input names to tensors
Set optimization profiles for the input tensors.
Parameters:
Dictionary mapping input names to tensors
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.
Returns: List of The Polygraphy Profile objects
Parameters:
Input graph spec
List of samples
Returns the key of the backend.
Returns the state_dict of the backend.
Bases: BackendConfig
Configuration for TensorRT backend.
Returns the default fields to describe.
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.