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

Class EngineBuilder

samples/python/efficientnet/build_engine.py:113–220  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

111
112
113class EngineBuilder:
114 """
115 Parses an ONNX graph and builds a TensorRT engine from it.
116 """
117
118 def __init__(self, verbose=False):
119 """
120 :param verbose: If enabled, a higher verbosity level will be set on the TensorRT logger.
121 """
122 self.trt_logger = trt.Logger(trt.Logger.INFO)
123 if verbose:
124 self.trt_logger.min_severity = trt.Logger.Severity.VERBOSE
125
126 trt.init_libnvinfer_plugins(self.trt_logger, namespace="")
127
128 self.builder = trt.Builder(self.trt_logger)
129 self.config = self.builder.create_builder_config()
130 self.config.max_workspace_size = 8 * (2 ** 30) # 8 GB
131
132 self.batch_size = None
133 self.network = None
134 self.parser = None
135
136 def create_network(self, onnx_path):
137 """
138 Parse the ONNX graph and create the corresponding TensorRT network definition.
139 :param onnx_path: The path to the ONNX graph to load.
140 """
141 network_flags = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
142
143 self.network = self.builder.create_network(network_flags)
144 self.parser = trt.OnnxParser(self.network, self.trt_logger)
145
146 onnx_path = os.path.realpath(onnx_path)
147 with open(onnx_path, "rb") as f:
148 if not self.parser.parse(f.read()):
149 log.error("Failed to load ONNX file: {}".format(onnx_path))
150 for error in range(self.parser.num_errors):
151 log.error(self.parser.get_error(error))
152 sys.exit(1)
153
154 inputs = [self.network.get_input(i) for i in range(self.network.num_inputs)]
155 outputs = [self.network.get_output(i) for i in range(self.network.num_outputs)]
156
157 log.info("Network Description")
158 for input in inputs:
159 self.batch_size = input.shape[0]
160 log.info("Input '{}' with shape {} and dtype {}".format(input.name, input.shape, input.dtype))
161 for output in outputs:
162 log.info("Output '{}' with shape {} and dtype {}".format(output.name, output.shape, output.dtype))
163 assert self.batch_size > 0
164 self.builder.max_batch_size = self.batch_size
165
166 def create_engine(
167 self,
168 engine_path,
169 precision,
170 calib_input=None,

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected