Installation — Torch-TensorRT v2.8.0.dev0+ee32da0 documentation (original) (raw)

Precompiled Binaries

Torch-TensorRT 2.x is centered primarily around Python. As such, precompiled releases can be found on pypi.org

Dependencies

You need to have CUDA, PyTorch, and TensorRT (python package is sufficient) installed to use Torch-TensorRT

Installing Torch-TensorRT

You can install the python package using

python -m pip install torch torch-tensorrt tensorrt

Packages are uploaded for Linux on x86 and Windows

Installing Torch-TensorRT for a specific CUDA version

Similar to PyTorch, Torch-TensorRT has builds compiled for different versions of CUDA. These are distributed on PyTorch’s package index

For example CUDA 11.8

python -m pip install torch torch-tensorrt tensorrt --extra-index-url https://download.pytorch.org/whl/cu118

Installing Nightly Builds

Torch-TensorRT distributed nightlies targeting the PyTorch nightly. These can be installed from the PyTorch nightly package index (separated by CUDA version)

python -m pip install --pre torch torch-tensorrt tensorrt --extra-index-url https://download.pytorch.org/whl/nightly/cu128

C++ Precompiled Binaries (TorchScript Only)

Precompiled tarballs for releases are provided here: https://github.com/pytorch/TensorRT/releases

Compiling From Source

Building on Linux

Dependencies

   # OR  
   "file:///<ABSOLUTE PATH TO FILE>/TensorRT-<TENSORRT VERSION>.Linux.x86_64-gnu.cuda-<CUDA VERSION>.tar.gz"  

],
)

Remember at runtime, these libraries must be added to your LD_LIBRARY_PATH explicitly

If you have a local version of TensorRT installed, this can be used as well by commenting out the above lines and uncommenting the following lines https://github.com/pytorch/TensorRT/blob/4e5b0f6e860910eb510fa70a76ee3eb9825e7a4d/WORKSPACE#L114C1-L124C3

Building the Package

Once the WORKSPACE has been configured properly, all that is required to build torch-tensorrt is the following command

python -m pip install --pre . --extra-index-url https://download.pytorch.org/whl/nightly/cu128

If you use the uv (https://docs.astral.sh/uv/) tool to manage python and your projects, the command is slightly simpler

To build the wheel file

python -m pip wheel --no-deps --pre . --extra-index-url https://download.pytorch.org/whl/nightly/cu128 -w dist

Additional Build Options

Some features in the library are optional and allow builds to be lighter or more portable.

Python Only Distribution

There are multiple features of the library which require C++ components to be enabled. This includes both the TorchScript frontend which accepts TorchScript modules for compilation and the Torch-TensorRT runtime, the default executor for modules compiled with Torch-TensorRT, be it with the TorchScript or Dynamo frontend.

In the case you may want a build which does not require C++ you can disable these features and avoid building these components. As a result, the only available runtime will be the Python based on which has implications for features like serialization.

PYTHON_ONLY=1 python -m pip install --pre . --extra-index-url https://download.pytorch.org/whl/nightly/cu128

No TorchScript Frontend

The TorchScript frontend is a legacy feature of Torch-TensorRT which is now in maintenance as TorchDynamo has become the preferred compiler technology for this project. It contains quite a bit of C++ code that is no longer necessary for most users. Therefore you can exclude this component from your build to speed up build times. The C++ based runtime will still be available to use.

NO_TORCHSCRIPT=1 python -m pip install --pre . --extra-index-url https://download.pytorch.org/whl/nightly/cu128

Building the C++ Library Standalone (TorchScript Only)

Release Build

bazel build //:libtorchtrt -c opt

A tarball with the include files and library can then be found in bazel-bin

Debug Build

To build with debug symbols use the following command

bazel build //:libtorchtrt -c dbg

A tarball with the include files and library can then be found in bazel-bin

Choosing the Right ABI

For the old versions, there were two ABI options to compile Torch-TensorRT which were incompatible with each other, pre-cxx11-abi and cxx11-abi. The complexity came from the different distributions of PyTorch. Fortunately, PyTorch has switched to cxx11-abi for all distributions. Below is a table with general pairings of PyTorch distribution sources and the recommended commands:

PyTorch Source Recommended Python Compilation Command Recommended C++ Compilation Command
PyTorch whl file from PyTorch.org python -m pip install . bazel build //:libtorchtrt -c opt
libtorch-cxx11-abi-shared-with-deps-*.zip from PyTorch.org python setup.py bdist_wheel bazel build //:libtorchtrt -c opt
PyTorch preinstalled in an NGC container python setup.py bdist_wheel bazel build //:libtorchtrt -c opt
PyTorch from the NVIDIA Forums for Jetson python setup.py bdist_wheel bazel build //:libtorchtrt -c opt
PyTorch built from Source python setup.py bdist_wheel bazel build //:libtorchtrt -c opt

NOTE: For all of the above cases you must correctly declare the source of PyTorch you intend to use in your WORKSPACE file for both Python and C++ builds. See below for more information

Building on Windows

Build steps

Advanced setup and Troubleshooting

In the WORKSPACE file, the cuda_win, libtorch_win, and tensorrt_win are Windows-specific modules which can be customized. For instance, if you would like to build with a different version of CUDA, or your CUDA installation is in a non-standard location, update the path in the cuda_win module.

Similarly, if you would like to use a different version of pytorch or tensorrt, customize the urls in the libtorch_win and tensorrt_win modules, respectively.

Local versions of these packages can also be used on Windows. See toolchains\\ci_workspaces\\WORKSPACE.win.release.tmpl for an example of using a local version of TensorRT on Windows.

Alternative Build Systems

Building with CMake (TorchScript Only)

It is possible to build the API libraries (in cpp/) and the torchtrtc executable using CMake instead of Bazel. Currently, the python API and the tests cannot be built with CMake. Begin by installing CMake.

A few useful CMake options include:

cmake -S. -B
[-DCMAKE_MODULE_PATH=cmake/Module]
[-DTorch_DIR=/share/cmake/Torch]
[-DTensorRT_ROOT=]
[-DCMAKE_BUILD_TYPE=Debug|Release] cmake --build

Building Natively on aarch64 (Jetson)

Prerequisites

Install or compile a build of PyTorch/LibTorch for aarch64

NVIDIA hosts builds the latest release branch for Jetson here:

Environment Setup

To build natively on aarch64-linux-gnu platform, configure the WORKSPACE with local available dependencies.

  1. Replace WORKSPACE with the corresponding WORKSPACE file in //toolchains/jp_workspaces
  2. Configure the correct paths to directory roots containing local dependencies in the new_local_repository rules:

    NOTE: If you installed PyTorch using a pip package, the correct path is the path to the root of the python torch package. In the case that you installed with sudo pip install this will be /usr/local/lib/python3.8/dist-packages/torch. In the case you installed with pip install --user this will be $HOME/.local/lib/python3.8/site-packages/torch.

new_local_repository( name = "libtorch", path = "/usr/local/lib/python3.8/dist-packages/torch", build_file = "third_party/libtorch/BUILD" )

Compile C++ Library and Compiler CLI

NOTE: Due to shifting dependency locations between Jetpack 4.5 and 4.6 there is a now a flag to inform bazel of the Jetpack version

--platforms //toolchains:jetpack_x.x

Compile Torch-TensorRT library using bazel command:

bazel build //:libtorchtrt --platforms //toolchains:jetpack_5.0

Compile Python API

NOTE: Due to shifting dependencies locations between Jetpack 4.5 and newer Jetpack versions there is now a flag for setup.py which sets the jetpack version (default: 5.0)

Compile the Python API using the following command from the //py directory:

If you are building for Jetpack 4.5 add the --jetpack-version 5.0 flag