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