MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / EngineBuilder

Class EngineBuilder

samples/python/efficientdet/build_engine.py:115–263  ·  view source on GitHub ↗

Parses an ONNX graph and builds a TensorRT engine from it.

Source from the content-addressed store, hash-verified

113
114
115class EngineBuilder:
116 """
117 Parses an ONNX graph and builds a TensorRT engine from it.
118 """
119
120 def __init__(self, verbose=False, workspace=8):
121 """
122 :param verbose: If enabled, a higher verbosity level will be set on the TensorRT logger.
123 :param workspace: Max memory workspace to allow, in Gb.
124 """
125 self.trt_logger = trt.Logger(trt.Logger.INFO)
126 if verbose:
127 self.trt_logger.min_severity = trt.Logger.Severity.VERBOSE
128
129 trt.init_libnvinfer_plugins(self.trt_logger, namespace="")
130
131 self.builder = trt.Builder(self.trt_logger)
132 self.config = self.builder.create_builder_config()
133 self.config.max_workspace_size = workspace * (2 ** 30)
134
135 self.network = None
136 self.parser = None
137
138 def create_network(self, onnx_path, batch_size, dynamic_batch_size=None):
139 """
140 Parse the ONNX graph and create the corresponding TensorRT network definition.
141 :param onnx_path: The path to the ONNX graph to load.
142 :param batch_size: Static batch size to build the engine with.
143 :param dynamic_batch_size: Dynamic batch size to build the engine with, if given,
144 batch_size is ignored, pass as a comma-separated string or int list as MIN,OPT,MAX
145 """
146 network_flags = (1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))
147
148 self.network = self.builder.create_network(network_flags)
149 self.parser = trt.OnnxParser(self.network, self.trt_logger)
150
151 onnx_path = os.path.realpath(onnx_path)
152 with open(onnx_path, "rb") as f:
153 if not self.parser.parse(f.read()):
154 log.error("Failed to load ONNX file: {}".format(onnx_path))
155 for error in range(self.parser.num_errors):
156 log.error(self.parser.get_error(error))
157 sys.exit(1)
158
159 log.info("Network Description")
160
161 inputs = [self.network.get_input(i) for i in range(self.network.num_inputs)]
162 profile = self.builder.create_optimization_profile()
163 dynamic_inputs = False
164 for input in inputs:
165 log.info("Input '{}' with shape {} and dtype {}".format(input.name, input.shape, input.dtype))
166 if input.shape[0] == -1:
167 dynamic_inputs = True
168 if dynamic_batch_size:
169 if type(dynamic_batch_size) is str:
170 dynamic_batch_size = [int(v) for v in dynamic_batch_size.split(",")]
171 assert len(dynamic_batch_size) == 3
172 min_shape = [dynamic_batch_size[0]] + list(input.shape[1:])

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected