-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
195 lines (147 loc) · 5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import ctypes
import glob
import os
import sys
import warnings
def try_load(library):
try:
ctypes.CDLL(library)
except OSError:
pass
# Try loading all packaged libraries. This is a nop if there are no libraries packaged.
CURDIR = os.path.realpath(os.path.dirname(__file__))
for lib in glob.iglob(os.path.join(CURDIR, "*.so*")):
try_load(lib)
# On Windows, we need to manually open the TensorRT libraries - otherwise we are unable to
# load the bindings.
def find_lib(name):
paths = os.environ["PATH"].split(os.path.pathsep)
for path in paths:
libpath = os.path.join(path, name)
if os.path.isfile(libpath):
return libpath
raise FileNotFoundError(
"Could not find: {:}. Is it on your PATH?\nNote: Paths searched were:\n{:}".format(name, paths)
)
if sys.platform.startswith("win"):
# Order matters here because of dependencies
LIBRARIES = [
"nvinfer.dll",
"cublas64_11.dll",
"cublasLt64_11.dll",
"cudnn64_8.dll",
"nvinfer_plugin.dll",
"nvonnxparser.dll",
"nvparsers.dll",
]
for lib in LIBRARIES:
ctypes.CDLL(find_lib(lib))
from .tensorrt import *
__version__ = "8.5.10.4"
# Provides Python's `with` syntax
def common_enter(this):
warnings.warn(
"Context managers for TensorRT types are deprecated. "
"Memory will be freed automatically when the reference count reaches 0.",
DeprecationWarning,
)
return this
def common_exit(this, exc_type, exc_value, traceback):
"""
Context managers are deprecated and have no effect. Objects are automatically freed when
the reference count reaches 0.
"""
pass
# Logger does not have a destructor.
ILogger.__enter__ = common_enter
ILogger.__exit__ = lambda this, exc_type, exc_value, traceback: None
Builder.__enter__ = common_enter
Builder.__exit__ = common_exit
ICudaEngine.__enter__ = common_enter
ICudaEngine.__exit__ = common_exit
IExecutionContext.__enter__ = common_enter
IExecutionContext.__exit__ = common_exit
Runtime.__enter__ = common_enter
Runtime.__exit__ = common_exit
INetworkDefinition.__enter__ = common_enter
INetworkDefinition.__exit__ = common_exit
UffParser.__enter__ = common_enter
UffParser.__exit__ = common_exit
CaffeParser.__enter__ = common_enter
CaffeParser.__exit__ = common_exit
OnnxParser.__enter__ = common_enter
OnnxParser.__exit__ = common_exit
IHostMemory.__enter__ = common_enter
IHostMemory.__exit__ = common_exit
Refitter.__enter__ = common_enter
Refitter.__exit__ = common_exit
IBuilderConfig.__enter__ = common_enter
IBuilderConfig.__exit__ = common_exit
# Add logger severity into the default implementation to preserve backwards compatibility.
Logger.Severity = ILogger.Severity
for attr, value in ILogger.Severity.__members__.items():
setattr(Logger, attr, value)
# Computes the volume of an iterable.
def volume(iterable):
"""
Computes the volume of an iterable.
:arg iterable: Any python iterable, including a :class:`Dims` object.
:returns: The volume of the iterable. This will return 1 for empty iterables, as a scalar has an empty shape and the volume of a tensor with empty shape is 1.
"""
vol = 1
for elem in iterable:
vol *= elem
return vol
# Converts a TensorRT datatype to the equivalent numpy type.
def nptype(trt_type):
"""
Returns the numpy-equivalent of a TensorRT :class:`DataType` .
:arg trt_type: The TensorRT data type to convert.
:returns: The equivalent numpy type.
"""
import numpy as np
mapping = {
float32: np.float32,
float16: np.float16,
int8: np.int8,
int32: np.int32,
bool: np.bool,
uint8: np.uint8,
}
if trt_type in mapping:
return mapping[trt_type]
raise TypeError("Could not resolve TensorRT datatype to an equivalent numpy datatype.")
# Add a numpy-like itemsize property to the datatype.
def _itemsize(trt_type):
"""
Returns the size in bytes of this :class:`DataType` .
:arg trt_type: The TensorRT data type.
:returns: The size of the type.
"""
mapping = {
float32: 4,
float16: 2,
int8: 1,
int32: 4,
bool: 1,
uint8: 1,
}
if trt_type in mapping:
return mapping[trt_type]
DataType.itemsize = property(lambda this: _itemsize(this))