polygraphy 0.50.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. polygraphy/__init__.py +3 -0
  2. polygraphy/backend/__init__.py +0 -0
  3. polygraphy/backend/base/__init__.py +2 -0
  4. polygraphy/backend/base/loader.py +40 -0
  5. polygraphy/backend/base/runner.py +258 -0
  6. polygraphy/backend/base/util.py +53 -0
  7. polygraphy/backend/common/__init__.py +1 -0
  8. polygraphy/backend/common/loader.py +105 -0
  9. polygraphy/backend/onnx/__init__.py +1 -0
  10. polygraphy/backend/onnx/loader.py +997 -0
  11. polygraphy/backend/onnx/util.py +388 -0
  12. polygraphy/backend/onnxrt/__init__.py +2 -0
  13. polygraphy/backend/onnxrt/loader.py +91 -0
  14. polygraphy/backend/onnxrt/runner.py +90 -0
  15. polygraphy/backend/pluginref/__init__.py +17 -0
  16. polygraphy/backend/pluginref/references.py +102 -0
  17. polygraphy/backend/pluginref/runner.py +83 -0
  18. polygraphy/backend/pyt/__init__.py +1 -0
  19. polygraphy/backend/pyt/runner.py +81 -0
  20. polygraphy/backend/tensorrt_rtx/__init__.py +1 -0
  21. polygraphy/backend/tensorrt_rtx/config.py +124 -0
  22. polygraphy/backend/tf/__init__.py +39 -0
  23. polygraphy/backend/tf/loader.py +496 -0
  24. polygraphy/backend/tf/runner.py +107 -0
  25. polygraphy/backend/tf/util.py +204 -0
  26. polygraphy/backend/trt/__init__.py +9 -0
  27. polygraphy/backend/trt/algorithm_selector.py +423 -0
  28. polygraphy/backend/trt/calibrator.py +295 -0
  29. polygraphy/backend/trt/config.py +716 -0
  30. polygraphy/backend/trt/file_reader.py +95 -0
  31. polygraphy/backend/trt/loader.py +1219 -0
  32. polygraphy/backend/trt/profile.py +215 -0
  33. polygraphy/backend/trt/runner.py +761 -0
  34. polygraphy/backend/trt/util.py +951 -0
  35. polygraphy/common/__init__.py +2 -0
  36. polygraphy/common/interface.py +192 -0
  37. polygraphy/common/struct.py +203 -0
  38. polygraphy/comparator/__init__.py +5 -0
  39. polygraphy/comparator/comparator.py +483 -0
  40. polygraphy/comparator/compare.py +2016 -0
  41. polygraphy/comparator/data_loader.py +531 -0
  42. polygraphy/comparator/postprocess.py +64 -0
  43. polygraphy/comparator/struct.py +409 -0
  44. polygraphy/comparator/util.py +405 -0
  45. polygraphy/config.py +65 -0
  46. polygraphy/constants.py +32 -0
  47. polygraphy/cuda/__init__.py +1 -0
  48. polygraphy/cuda/cuda.py +521 -0
  49. polygraphy/datatype/__init__.py +6 -0
  50. polygraphy/datatype/datatype.py +274 -0
  51. polygraphy/datatype/numpy.py +77 -0
  52. polygraphy/datatype/onnx.py +86 -0
  53. polygraphy/datatype/onnxrt.py +72 -0
  54. polygraphy/datatype/tensorrt.py +75 -0
  55. polygraphy/datatype/torch.py +68 -0
  56. polygraphy/exception/__init__.py +1 -0
  57. polygraphy/exception/exception.py +52 -0
  58. polygraphy/func/__init__.py +1 -0
  59. polygraphy/func/func.py +199 -0
  60. polygraphy/json/__init__.py +1 -0
  61. polygraphy/json/serde.py +442 -0
  62. polygraphy/logger/__init__.py +1 -0
  63. polygraphy/logger/logger.py +686 -0
  64. polygraphy/mod/__init__.py +3 -0
  65. polygraphy/mod/exporter.py +362 -0
  66. polygraphy/mod/importer.py +345 -0
  67. polygraphy/mod/trt_importer.py +41 -0
  68. polygraphy/mod/util.py +41 -0
  69. polygraphy/tools/__init__.py +3 -0
  70. polygraphy/tools/_main.py +88 -0
  71. polygraphy/tools/args/__init__.py +19 -0
  72. polygraphy/tools/args/backend/__init__.py +21 -0
  73. polygraphy/tools/args/backend/onnx/__init__.py +16 -0
  74. polygraphy/tools/args/backend/onnx/loader.py +770 -0
  75. polygraphy/tools/args/backend/onnxrt/__init__.py +17 -0
  76. polygraphy/tools/args/backend/onnxrt/loader.py +85 -0
  77. polygraphy/tools/args/backend/onnxrt/runner.py +42 -0
  78. polygraphy/tools/args/backend/pluginref/__init__.py +16 -0
  79. polygraphy/tools/args/backend/pluginref/runner.py +47 -0
  80. polygraphy/tools/args/backend/runner_select.py +107 -0
  81. polygraphy/tools/args/backend/tf/__init__.py +18 -0
  82. polygraphy/tools/args/backend/tf/config.py +77 -0
  83. polygraphy/tools/args/backend/tf/loader.py +266 -0
  84. polygraphy/tools/args/backend/tf/runner.py +70 -0
  85. polygraphy/tools/args/backend/trt/__init__.py +18 -0
  86. polygraphy/tools/args/backend/trt/config.py +993 -0
  87. polygraphy/tools/args/backend/trt/helper.py +34 -0
  88. polygraphy/tools/args/backend/trt/loader.py +894 -0
  89. polygraphy/tools/args/backend/trt/runner.py +107 -0
  90. polygraphy/tools/args/base.py +212 -0
  91. polygraphy/tools/args/comparator/__init__.py +19 -0
  92. polygraphy/tools/args/comparator/comparator.py +340 -0
  93. polygraphy/tools/args/comparator/compare.py +407 -0
  94. polygraphy/tools/args/comparator/data_loader.py +269 -0
  95. polygraphy/tools/args/comparator/postprocess.py +143 -0
  96. polygraphy/tools/args/logger/__init__.py +16 -0
  97. polygraphy/tools/args/logger/logger.py +172 -0
  98. polygraphy/tools/args/model.py +227 -0
  99. polygraphy/tools/args/util/__init__.py +1 -0
  100. polygraphy/tools/args/util/util.py +428 -0
  101. polygraphy/tools/base/__init__.py +1 -0
  102. polygraphy/tools/base/tool.py +212 -0
  103. polygraphy/tools/check/__init__.py +1 -0
  104. polygraphy/tools/check/check.py +32 -0
  105. polygraphy/tools/check/subtool/__init__.py +1 -0
  106. polygraphy/tools/check/subtool/lint.py +1000 -0
  107. polygraphy/tools/convert/__init__.py +1 -0
  108. polygraphy/tools/convert/convert.py +115 -0
  109. polygraphy/tools/data/__init__.py +1 -0
  110. polygraphy/tools/data/data.py +33 -0
  111. polygraphy/tools/data/subtool/__init__.py +2 -0
  112. polygraphy/tools/data/subtool/concat.py +115 -0
  113. polygraphy/tools/data/subtool/merge.py +89 -0
  114. polygraphy/tools/debug/__init__.py +1 -0
  115. polygraphy/tools/debug/debug.py +72 -0
  116. polygraphy/tools/debug/subtool/__init__.py +4 -0
  117. polygraphy/tools/debug/subtool/base.py +159 -0
  118. polygraphy/tools/debug/subtool/build.py +46 -0
  119. polygraphy/tools/debug/subtool/iterative_debug_args.py +761 -0
  120. polygraphy/tools/debug/subtool/precision.py +281 -0
  121. polygraphy/tools/debug/subtool/reduce.py +594 -0
  122. polygraphy/tools/debug/subtool/repeat.py +46 -0
  123. polygraphy/tools/inspect/__init__.py +1 -0
  124. polygraphy/tools/inspect/inspect.py +44 -0
  125. polygraphy/tools/inspect/subtool/__init__.py +6 -0
  126. polygraphy/tools/inspect/subtool/capability.py +429 -0
  127. polygraphy/tools/inspect/subtool/data.py +146 -0
  128. polygraphy/tools/inspect/subtool/diff_tactics.py +128 -0
  129. polygraphy/tools/inspect/subtool/model/__init__.py +1 -0
  130. polygraphy/tools/inspect/subtool/model/args.py +62 -0
  131. polygraphy/tools/inspect/subtool/model/extractors/__init__.py +27 -0
  132. polygraphy/tools/inspect/subtool/model/extractors/_helpers.py +60 -0
  133. polygraphy/tools/inspect/subtool/model/extractors/_onnx.py +237 -0
  134. polygraphy/tools/inspect/subtool/model/extractors/_trt.py +332 -0
  135. polygraphy/tools/inspect/subtool/model/graph_data.py +108 -0
  136. polygraphy/tools/inspect/subtool/model/model.py +217 -0
  137. polygraphy/tools/inspect/subtool/model/text.py +298 -0
  138. polygraphy/tools/inspect/subtool/model/viewer/__init__.py +3 -0
  139. polygraphy/tools/inspect/subtool/model/viewer/_templates.py +1435 -0
  140. polygraphy/tools/inspect/subtool/model/viewer/_viewer.py +192 -0
  141. polygraphy/tools/inspect/subtool/model/viewer/colors.py +284 -0
  142. polygraphy/tools/inspect/subtool/model/viewer/elements.py +276 -0
  143. polygraphy/tools/inspect/subtool/model/viewer/layout.py +255 -0
  144. polygraphy/tools/inspect/subtool/model/viewer/node_info.py +228 -0
  145. polygraphy/tools/inspect/subtool/model/viewer/page.py +287 -0
  146. polygraphy/tools/inspect/subtool/sparsity.py +50 -0
  147. polygraphy/tools/inspect/subtool/tactics.py +38 -0
  148. polygraphy/tools/multi_device/__init__.py +9 -0
  149. polygraphy/tools/multi_device/multi_device.py +314 -0
  150. polygraphy/tools/multi_device/subtool/__init__.py +1 -0
  151. polygraphy/tools/multi_device/subtool/shard.py +960 -0
  152. polygraphy/tools/plugin/__init__.py +1 -0
  153. polygraphy/tools/plugin/plugin.py +35 -0
  154. polygraphy/tools/plugin/subtool/__init__.py +5 -0
  155. polygraphy/tools/plugin/subtool/autotune.py +261 -0
  156. polygraphy/tools/plugin/subtool/autotuner/__init__.py +6 -0
  157. polygraphy/tools/plugin/subtool/autotuner/auto_tuner.py +250 -0
  158. polygraphy/tools/plugin/subtool/autotuner/optimization_strategy.py +350 -0
  159. polygraphy/tools/plugin/subtool/autotuner/performance_analyzer.py +280 -0
  160. polygraphy/tools/plugin/subtool/autotuner/replacement_engine.py +181 -0
  161. polygraphy/tools/plugin/subtool/list_plugins.py +34 -0
  162. polygraphy/tools/plugin/subtool/match.py +40 -0
  163. polygraphy/tools/plugin/subtool/plugin_base.py +172 -0
  164. polygraphy/tools/plugin/subtool/replace.py +179 -0
  165. polygraphy/tools/registry.py +68 -0
  166. polygraphy/tools/run/__init__.py +1 -0
  167. polygraphy/tools/run/run.py +241 -0
  168. polygraphy/tools/script.py +490 -0
  169. polygraphy/tools/sparse.py +388 -0
  170. polygraphy/tools/surgeon/__init__.py +1 -0
  171. polygraphy/tools/surgeon/subtool/__init__.py +6 -0
  172. polygraphy/tools/surgeon/subtool/base.py +83 -0
  173. polygraphy/tools/surgeon/subtool/extract.py +215 -0
  174. polygraphy/tools/surgeon/subtool/insert.py +170 -0
  175. polygraphy/tools/surgeon/subtool/prune.py +58 -0
  176. polygraphy/tools/surgeon/subtool/sanitize.py +259 -0
  177. polygraphy/tools/surgeon/subtool/weight_reconstruct.py +111 -0
  178. polygraphy/tools/surgeon/subtool/weight_strip.py +467 -0
  179. polygraphy/tools/surgeon/surgeon.py +46 -0
  180. polygraphy/tools/template/__init__.py +1 -0
  181. polygraphy/tools/template/subtool/__init__.py +4 -0
  182. polygraphy/tools/template/subtool/base.py +30 -0
  183. polygraphy/tools/template/subtool/onnx_gs.py +89 -0
  184. polygraphy/tools/template/subtool/shard_hint.py +419 -0
  185. polygraphy/tools/template/subtool/trt_config.py +62 -0
  186. polygraphy/tools/template/subtool/trt_network.py +76 -0
  187. polygraphy/tools/template/template.py +30 -0
  188. polygraphy/tools/util.py +50 -0
  189. polygraphy/util/__init__.py +2 -0
  190. polygraphy/util/array.py +1522 -0
  191. polygraphy/util/util.py +1138 -0
  192. polygraphy-0.50.3.dist-info/METADATA +180 -0
  193. polygraphy-0.50.3.dist-info/RECORD +197 -0
  194. polygraphy-0.50.3.dist-info/WHEEL +5 -0
  195. polygraphy-0.50.3.dist-info/entry_points.txt +2 -0
  196. polygraphy-0.50.3.dist-info/top_level.txt +1 -0
  197. polygraphy-0.50.3.dist-info/zip-safe +1 -0
@@ -0,0 +1,281 @@
1
+ #
2
+ # SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ import math
19
+
20
+ from polygraphy import mod
21
+ from polygraphy.mod.trt_importer import lazy_import_trt
22
+ from polygraphy.logger import G_LOGGER
23
+ from polygraphy.tools.args import ModelArgs, TrtConfigArgs
24
+ from polygraphy.tools.debug.subtool.base import BaseCheckerSubtool
25
+
26
+ trt = lazy_import_trt()
27
+ trt_util = mod.lazy_import("polygraphy.backend.trt.util")
28
+
29
+
30
+ class BaseMarker:
31
+ def __init__(self, max_layers, direction, num_layers_to_mark):
32
+ self.max_layers = max_layers
33
+ self.direction = direction
34
+ self.num_layers_to_mark = num_layers_to_mark
35
+ self.good = (
36
+ max_layers + 1
37
+ ) # Pretend marking all the layers gives us good accuracy.
38
+
39
+ def select_layers(self):
40
+ if self.direction == "forward":
41
+ G_LOGGER.info(
42
+ f"Selecting first {self.num_layers_to_mark} layer(s) to run in higher precision"
43
+ )
44
+ return range(0, self.num_layers_to_mark)
45
+ else:
46
+ G_LOGGER.info(
47
+ f"Selecting last {self.num_layers_to_mark} layer(s) to run in higher precision"
48
+ )
49
+ return range(self.max_layers - self.num_layers_to_mark, self.max_layers)
50
+
51
+ def success_message(self):
52
+ which_layers = "first" if self.direction == "forward" else "last"
53
+ G_LOGGER.finish(
54
+ f"To achieve acceptable accuracy, try running the {which_layers} {self.good} layer(s) in higher precision"
55
+ )
56
+
57
+
58
+ class BisectMarker(BaseMarker):
59
+ def __init__(self, max_layers, direction) -> None:
60
+ super().__init__(max_layers, direction, max_layers)
61
+ self.bad = 0
62
+
63
+ def step(self, success):
64
+ if success:
65
+ self.good = self.num_layers_to_mark
66
+ # On successes, we want num_layers_to_mark to go closer to self.bad
67
+ round_func = math.floor
68
+ else:
69
+ self.bad = self.num_layers_to_mark
70
+ round_func = math.ceil
71
+
72
+ old_num_layers_to_mark = self.num_layers_to_mark
73
+ self.num_layers_to_mark = int(round_func((self.good + self.bad) / 2.0))
74
+
75
+ # Prevent infinite looping:
76
+ if old_num_layers_to_mark == self.num_layers_to_mark:
77
+ return True
78
+
79
+ # If good and bad are within 1 layer of each other,
80
+ # then we already have the information we need.
81
+ if abs(self.good - self.bad) <= 1:
82
+ if self.good >= self.max_layers:
83
+ G_LOGGER.error(
84
+ "Could not find a configuration that satisfied accuracy requirements."
85
+ )
86
+ else:
87
+ self.success_message()
88
+ return True
89
+
90
+ if self.num_layers_to_mark > self.max_layers:
91
+ G_LOGGER.error(
92
+ "Could not find a configuration that satisfied accuracy requirements."
93
+ )
94
+ return True
95
+
96
+ return False
97
+
98
+ def remaining(self):
99
+ return int(math.ceil(math.log2(self.good - self.bad)))
100
+
101
+
102
+ class LinearMarker(BaseMarker):
103
+ def __init__(self, max_layers, direction) -> None:
104
+ super().__init__(max_layers, direction, 0)
105
+
106
+ def step(self, success):
107
+ if success:
108
+ self.good = self.num_layers_to_mark
109
+ self.num_layers_to_mark += 1
110
+
111
+ if success:
112
+ self.success_message()
113
+ return True
114
+
115
+ if self.num_layers_to_mark > self.max_layers:
116
+ G_LOGGER.error(
117
+ "Could not find a configuration that satisfied accuracy requirements."
118
+ )
119
+ return True
120
+
121
+ return False
122
+
123
+ def remaining(self):
124
+ return self.max_layers - self.num_layers_to_mark
125
+
126
+
127
+ class Precision(BaseCheckerSubtool):
128
+ """
129
+ [EXPERIMENTAL] Iteratively mark layers to run in a higher precision to find a compromise between performance and quality.
130
+
131
+ `debug precision` follows the same general process as other `debug` subtools (refer to the help output
132
+ of the `debug` tool for more background information and details).
133
+
134
+ Each iteration will generate an engine called 'polygraphy_debug.engine' in the current directory.
135
+ """
136
+
137
+ def __init__(self):
138
+ super().__init__(
139
+ "precision",
140
+ precision_constraints_default="obey",
141
+ allow_no_artifacts_warning=False,
142
+ )
143
+
144
+ def add_parser_args(self, parser):
145
+ parser.add_argument(
146
+ "--mode",
147
+ help="How layers are selected to run in higher precision. "
148
+ "'bisect' will use binary search, and 'linear' will iteratively mark one extra layer at a time",
149
+ choices=["bisect", "linear"],
150
+ default="bisect",
151
+ )
152
+ parser.add_argument(
153
+ "--dir",
154
+ "--direction",
155
+ help="Order in which layers are marked to run in higher precision. "
156
+ "'forward' will start marking layers from network inputs, and 'reverse' will start "
157
+ "from the network outputs",
158
+ choices=["forward", "reverse"],
159
+ default="forward",
160
+ dest="direction",
161
+ )
162
+ parser.add_argument(
163
+ "-p",
164
+ "--precision",
165
+ help="Precision to use when marking layers to run in higher precision",
166
+ choices=["float32", "float16"],
167
+ default="float32",
168
+ )
169
+
170
+ def setup(self, args, network):
171
+ self.precision = {"float32": trt.float32, "float16": trt.float16}[
172
+ args.precision
173
+ ]
174
+
175
+ if self.precision == trt.float16 and not self.arg_groups[TrtConfigArgs].fp16:
176
+ G_LOGGER.critical(
177
+ "Cannot mark layers to run in float16 if it is not enabled in the builder configuration.\n"
178
+ "Please also specify `--fp16` as a command-line option"
179
+ )
180
+
181
+ if self.precision == trt.float16 and not self.arg_groups[TrtConfigArgs].int8:
182
+ G_LOGGER.warning(
183
+ "Using float16 as the higher precision, but float16 is also the lowest precision available. "
184
+ "Did you mean to set --int8 as well?"
185
+ )
186
+
187
+ if not any(
188
+ [
189
+ self.arg_groups[TrtConfigArgs].tf32,
190
+ self.arg_groups[TrtConfigArgs].fp16,
191
+ self.arg_groups[TrtConfigArgs].int8,
192
+ ]
193
+ ):
194
+ G_LOGGER.critical(
195
+ "Please enable at least one precision besides float32 (e.g. --int8, --fp16, --tf32)"
196
+ )
197
+
198
+ if self.arg_groups[ModelArgs].model_type == "engine":
199
+ G_LOGGER.critical(
200
+ "The precision tool cannot work with engines, as they cannot be modified. "
201
+ "Please provide a different format, such as an ONNX model or TensorRT network script."
202
+ )
203
+
204
+ G_LOGGER.start(f"Using {self.precision} as higher precision")
205
+
206
+ if args.mode == "linear":
207
+ self.layer_marker = LinearMarker(len(network), args.direction)
208
+ elif args.mode == "bisect":
209
+ self.layer_marker = BisectMarker(len(network), args.direction)
210
+
211
+ self.original_precisions = {}
212
+ try:
213
+ for index, layer in enumerate(network):
214
+ if layer.precision_is_set:
215
+ self.original_precisions[index] = layer.precision
216
+ except AttributeError:
217
+ trt_util.fail_unavailable("layer precision in polygraphy debug precision")
218
+
219
+ def mark_layers(self, network, indices):
220
+ EXCLUDE_LAYER_NAMES = ["CONSTANT"]
221
+ EXCLUDE_LAYERS = [
222
+ getattr(trt.LayerType, attr)
223
+ for attr in EXCLUDE_LAYER_NAMES
224
+ if hasattr(trt.LayerType, attr)
225
+ ]
226
+
227
+ # First, reset, since changes from the previous call will persist.
228
+ try:
229
+ for index, layer in enumerate(network):
230
+ if index in self.original_precisions:
231
+ layer.precision = self.original_precisions[index]
232
+ else:
233
+ layer.reset_precision()
234
+ except AttributeError:
235
+ trt_util.fail_unavailable("layer precision in polygraphy debug precision")
236
+
237
+ marked_indices = set()
238
+ for index in indices:
239
+ layer = network.get_layer(index)
240
+
241
+ def should_exclude():
242
+ has_non_execution_output = any(
243
+ not layer.get_output(i).is_execution_tensor
244
+ for i in range(layer.num_outputs)
245
+ )
246
+ has_non_activation_output = any(
247
+ layer.get_output(i).dtype
248
+ not in [trt.float32, trt.float16, trt.int8]
249
+ for i in range(layer.num_outputs)
250
+ )
251
+ return (
252
+ layer.type in EXCLUDE_LAYERS
253
+ or has_non_execution_output
254
+ or has_non_activation_output
255
+ )
256
+
257
+ if not should_exclude():
258
+ G_LOGGER.extra_verbose(
259
+ f"Running layer in higher precision: {trt_util.str_from_layer(layer, index)}"
260
+ )
261
+ try:
262
+ layer.precision = self.precision
263
+ except AttributeError:
264
+ trt_util.fail_unavailable(
265
+ "layer precision in polygraphy debug precision"
266
+ )
267
+ marked_indices.add(index)
268
+
269
+ G_LOGGER.verbose(
270
+ f"Marking layer(s): {marked_indices} to run in {self.precision} precision"
271
+ )
272
+
273
+ def process_network(self, network):
274
+ indices = list(self.layer_marker.select_layers())
275
+ self.mark_layers(network, indices)
276
+
277
+ def step(self, success):
278
+ return self.layer_marker.step(success)
279
+
280
+ def remaining(self):
281
+ return self.layer_marker.remaining()