Skip to content

🐛 [Bug] Cannot import torch_tensorrt after installing TensorRT via pip wheel #1026

Closed

Description

@lennartmoritz

Bug Description

After installing the TensorRT with the pip wheel installation, which is among the dependencies listed in the installation guide, and then installing torch_tensorrt from the python package, I get an error when I import torch_tensorrt.
This is somewhat simmilar to Issue #887. But I dont think the LD_LIBRARY_PATH solutiuon applies to a pip installation.

To Reproduce

Steps to reproduce the behavior:

  1. Create new anaconda environment.
  2. conda install pytorch torchvision cudatoolkit=11.3 -c pytorch
  3. Install TensorRT via pip wheel as in section 2.3 of the Quick Start Guide with python -m pip install --upgrade nvidia-tensorrt
  4. Acknowledge this sencence from the Quick Start Guide "The above pip command will pull in all the required CUDA libraries and cuDNN in Python wheel format because they are dependencies of the TensorRT Python wheel"
  5. Verify TensorRT installation
python3
import tensorrt 
print(tensorrt.__version__)
assert tensorrt.Builder(tensorrt.Logger())
  1. Install torch-tensorrt pip install torch-tensorrt -f https://github.com/NVIDIA/Torch-TensorRT/releases
  2. Import torch_tensorrt in python
>>> import torch
>>> torch.version.cuda
'11.3'
>>> import torch_tensorrt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/__init__.py", line 11, in <module>
    from torch_tensorrt._compile import *
  File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/_compile.py", line 2, in <module>
    from torch_tensorrt import _enums
  File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/_enums.py", line 1, in <module>
    from torch_tensorrt._C import dtype, DeviceType, EngineCapability, TensorFormat
ImportError: libnvinfer_plugin.so.8: cannot open shared object file: No such file or directory

Expected behavior

Environment

Build information about Torch-TensorRT can be found by turning on debug messages

  • Torch-TensorRT Version (e.g. 1.0.0): 1.0.0
  • PyTorch Version (e.g. 1.0): 1.11.0
  • CPU Architecture: x86_64
  • OS (e.g., Linux): Ubuntu 20.4
  • How you installed PyTorch (conda, pip, libtorch, source): conda install pytorch torchvision cudatoolkit=11.3 -c pytorch
  • Build command you used (if compiling from source): -
  • Are you using local sources or building from archives: -
  • Python version: 3.7.7
  • CUDA version: Came with pytorch, so (11.3)
  • GPU models and configuration: RTX 2060

Additional context

I know about the contained solution for torch_tensorrt. It works great and I'm building an application meant for deployment inside of the container. But for ease of debugging and general prototype development in a small dev team I need to get torch_tensorrt running locally.

Activity

  1. added
    bug
    on May 5, 2022
  2. narendasan commented on May 6, 2022

    @narendasan
    Collaborator

    The TensorRT libraries need to be in your LD_LIBRARY_PATH

  3. lennartmoritz commented on May 6, 2022

    @lennartmoritz
    Author

    Okay, I tried to make sense of what I expect the LD_LIBRARY_PATH needs to point to with this pip installation.
    I believe it should be export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/tensorrt in my case.
    Below I'll add a screenshot showing on the left what would be linked in the LD_LIBRARY_PATH for a tar file installation of TensorRT, and on the right what I could find in the folder of my pip installation.

    After exporting that path i try to import torch_tensorrt again and I get a new error.
    ImportError: (...)/lib/libtorchtrt.so: undefined symbol: _ZNK3c104Type14isSubtypeOfExtERKSt10shared_ptrIS0_EPSo

    This is where I'm stuck now. Do you think the pip installation of TensorRT is incompatible for supporting Torch-TensorRT?

    >>> import torch_tensorrt
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/__init__.py", line 11, in <module>
        from torch_tensorrt._compile import *
      File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/_compile.py", line 2, in <module>
        from torch_tensorrt import _enums
      File "/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/_enums.py", line 1, in <module>
        from torch_tensorrt._C import dtype, DeviceType, EngineCapability, TensorFormat
    ImportError: /home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/torch_tensorrt/lib/libtorchtrt.so: undefined symbol: _ZNK3c104Type14isSubtypeOfExtERKSt10shared_ptrIS0_EPSo
    

    searching_ld_path

  4. lennartmoritz commented on May 6, 2022

    @lennartmoritz
    Author

    Okay, I fixed the issue. Importing torch_tensorrt is now working but to load my model I had to update to Torch-TensorRT 1.1.0, the same as in the containered version. For future reades I will include a step by step guide on how to install Torch-TensorRT 1.0.0 on Ubuntu 20.4 in an easy way. This also works for version 1.1.0 if you change the version numbers in the guide to the currently supported ones.

    In short:

    I had to downgrade my pytorch version to 1.10 and the TensorRT version to 8.0.3.4. But I think it was only necessary to to downgrade pytorch.

    Details:

    After researching the ImportError I figured it was likely caused by a PyTorch version missmatch. To figure out which versions can be used I went to the Torch-TensorRT Release Notes, which state:

    Support for PyTorch 1.10, CUDA 11.3
    ... cuDNN 8.2 and TensorRT 8.0

    The confusing thing is that the Release Notes also state that:

    Torch-TensorRT will be validated to run correctly with the version of PyTorch, CUDA, cuDNN and TensorRT in the container.

    But the public release of Torch-TensorRT 1.0.0 is never used in any container. Instead they use either 1.0.0a0 or 1.1.0a0 as of the time of my issue.

    I got fooled and thought I can use the same dependencies as the NVIDIA Pytorch Container > PyTorch Container Release Notes > PyTorch Release 21.11 that uses Torch-TensorRT 1.0.0a0 for the public release of 1.0.0 which is not the case.

    The most recent container with version 1.0.0a0 is PyTorch Release 21.11. The listed compatible versions are PyTorch 1.11.0a0+b6df043 and TensorRT 8.0.3.4 for x64 Linux.
    So even though I got it to work with TensorRT 8.0.3.4 I could not use PyTorch 1.11.0.

    How to easily install Torch-TensorRT 1.0.0 without docker on Ubuntu:

    Prerequisites:

    • Anaconda
    • NVIDIA Driver e.g. Driver Version 470.103.01 works for me.
    1. Create and activate a new anaconda environment.
    2. conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cudatoolkit=11.3 -c pytorch -c conda-forge
    3. Install TensorRT via pip wheel as in section 2.3 of the Quick Start Guide and finish with python -m pip install --upgrade nvidia-tensorrt

    The above pip command will pull in all the required CUDA libraries and cuDNN in Python wheel format because they are dependencies of the TensorRT Python wheel

    1. Verify TensorRT installation
    python3
    import tensorrt 
    print(tensorrt.__version__)
    assert tensorrt.Builder(tensorrt.Logger())
    
    1. Install torch-tensorrt pip install torch-tensorrt==1.0.0 -f https://github.com/NVIDIA/Torch-TensorRT/releases
    2. Add the TensorRT libraries to your LD_LIBRARY_PATH. Note this has to be done every time you run a new shell.
    3. You need to edit your username and python version e.g. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lennux/anaconda3/envs/omct/lib/python3.7/site-packages/tensorrt in my case.
    4. Import torch_tensorrt in python
    5. If you get an error durin step 8, try import tensorrt before importing torch_tensorrt
  5. mahmoudhussein99 commented on Jul 10, 2022

    @mahmoudhussein99

    I have tried this and again it gives me the same old error

    from torch_tensorrt._C import dtype, DeviceType, EngineCapability, TensorFormat ImportError: libcudnn.so.8: cannot open shared object file: No such file or directory

    Not sure what to do to fix it. Your Help is appreciated!!

  6. lennartmoritz commented on Jul 10, 2022

    @lennartmoritz
    Author

    Did you follow all the steps @mahmoudhussein99? This guide worked for me on Ubuntu 20.4, if you are not using Linux I am not sure if my guide will work for you.
    In particular, you must export your LD_LIBRARY_PATH, as described in steps 6 and 7. Make sure the path acutally leads to your tensorrt installation. Can you confirm that libcudnn.so.8 is acutally present within the folder of the LD_LIBRARY_PATH that you are exporting. And can you confirm that the correct LD_LIBRARY_PATH is set from the terminal you use to start your python script eg by running echo $LD_LIBRARY_PATH in it?
    And import tensorrt first in your code, before you import torch_tensorrt . Otherwise a necessary cudnn dependency may not be found.

    I would also recommend you try it with TensorRT 1.1.0 instead of 1.0.0. for the new features.

  7. mahmoudhussein99 commented on Jul 14, 2022

    @mahmoudhussein99

    Okay first of all thank you for your help that is really appreciated!
    After several days trying turned out that there was a function call calling import torch_tensorrt before the import to tensorrt and that was the bug

    Also to mention, I tried it without exporting the LD_LIBRARY_PATH as in steps 6 and 7 and works normally, the key is to import tensorrt before and then all the modules will be visible to the environment

    if it helps sharing with others also the configuration that worked with me:

    python 3.9.4
    nvidia-cublas-cu11 2022.4.8
    nvidia-cublas-cu117 11.10.1.25
    nvidia-cuda-runtime-cu11 2022.4.25
    nvidia-cuda-runtime-cu117 11.7.60
    nvidia-cudnn-cu11 2022.5.19
    nvidia-cudnn-cu116 8.4.0.27
    nvidia-pyindex 1.0.9
    nvidia-tensorrt 8.2.4.2
    torch 1.11.0+cu113
    torch-tensorrt 1.1.0
    torchaudio 0.11.0+cu113
    torchvision 0.12.0+cu113

    Having this configuration without exporting the LD_LIBRARY_PATH works straight away but please make sure to import tensorrt before importing torch_tensorrt

  8. jpalladino-fy commented on Feb 25, 2023

    @jpalladino-fy

    Thank you so much @mahmoudhussein99! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions