|
| 1 | +import ctypes |
| 2 | +import glob |
1 | 3 | import os
|
2 | 4 | import sys
|
| 5 | +import warnings |
| 6 | +from torch_tensorrt._version import __version__, __cuda_version__, __cudnn_version__, __tensorrt_version__ |
| 7 | + |
3 | 8 |
|
4 | 9 | if sys.version_info < (3,):
|
5 | 10 | raise Exception("Python 2 has reached end-of-life and is not supported by Torch-TensorRT")
|
6 | 11 |
|
7 |
| -import ctypes |
| 12 | +def _parse_semver(version): |
| 13 | + split = version.split(".") |
| 14 | + if len(split) < 3: |
| 15 | + split.append("") |
| 16 | + |
| 17 | + return { |
| 18 | + "major": split[0], |
| 19 | + "minor": split[1], |
| 20 | + "patch": split[2] |
| 21 | + } |
| 22 | + |
| 23 | +def _find_lib(name, paths): |
| 24 | + for path in paths: |
| 25 | + libpath = os.path.join(path, name) |
| 26 | + if os.path.isfile(libpath): |
| 27 | + return libpath |
| 28 | + |
| 29 | + raise FileNotFoundError( |
| 30 | + f"Could not find {name}\n Search paths: {paths}" |
| 31 | + ) |
| 32 | + |
| 33 | +try: |
| 34 | + import tensorrt |
| 35 | +except: |
| 36 | + cuda_version = _parse_semver(__cuda_version__) |
| 37 | + cudnn_version = _parse_semver(__cudnn_version__) |
| 38 | + tensorrt_version = _parse_semver(__tensorrt_version__) |
| 39 | + |
| 40 | + CUDA_MAJOR = cuda_version["major"] |
| 41 | + CUDNN_MAJOR = cudnn_version["major"] |
| 42 | + TENSORRT_MAJOR = tensorrt_version["major"] |
| 43 | + |
| 44 | + if sys.platform.startswith("win"): |
| 45 | + WIN_LIBS = [ |
| 46 | + f"cublas64_{CUDA_MAJOR}.dll", |
| 47 | + f"cublasLt64_{CUDA_MAJOR}.dll", |
| 48 | + f"cudnn64_{CUDNN_MAJOR}.dll", |
| 49 | + "nvinfer.dll", |
| 50 | + "nvinfer_plugin.dll", |
| 51 | + ] |
| 52 | + |
| 53 | + WIN_PATHS = os.environ["PATH"].split(os.path.pathsep) |
| 54 | + |
| 55 | + |
| 56 | + for lib in WIN_LIBS: |
| 57 | + ctypes.CDLL(_find_lib(lib, WIN_PATHS)) |
| 58 | + |
| 59 | + elif sys.platform.startswith("linux"): |
| 60 | + LINUX_PATHS = [ |
| 61 | + "/usr/lib/x86_64-linux-gnu", |
| 62 | + "/usr/local/cuda/lib64", |
| 63 | + ] + os.environ["LD_LIBRARY_PATH"].split(os.path.pathsep) |
| 64 | + |
| 65 | + LINUX_LIBS = [ |
| 66 | + f"libcudnn.so.{CUDNN_MAJOR}", |
| 67 | + f"libnvinfer.so.{TENSORRT_MAJOR}", |
| 68 | + f"libnvinfer_plugin.so.{TENSORRT_MAJOR}", |
| 69 | + ] |
| 70 | + |
| 71 | + for lib in LINUX_LIBS: |
| 72 | + ctypes.CDLL(_find_lib(lib, LINUX_PATHS)) |
| 73 | + |
8 | 74 | import torch
|
9 | 75 |
|
10 |
| -from torch_tensorrt._version import __version__ |
11 | 76 | from torch_tensorrt._compile import *
|
12 | 77 | from torch_tensorrt._util import *
|
13 | 78 | from torch_tensorrt import ts
|
|
0 commit comments