MCPcopy
hub / github.com/OpenPPL/ppq / build_engine

Function build_engine

ppq/samples/TensorRT/create_engine.py:47–79  ·  view source on GitHub ↗
(onnx_file, json_file, engine_file)

Source from the content-addressed store, hash-verified

45
46
47def build_engine(onnx_file, json_file, engine_file):
48 builder = trt.Builder(TRT_LOGGER)
49 network = builder.create_network(EXPLICIT_BATCH)
50
51 config = builder.create_builder_config()
52
53 # If it is a dynamic onnx model , you need to add the following.
54 # profile = builder.create_optimization_profile()
55 # profile.set_shape("input_name", (batch, channels, min_h, min_w), (batch, channels, opt_h, opt_w), (batch, channels, max_h, max_w))
56 # config.add_optimization_profile(profile)
57
58
59 parser = trt.OnnxParser(network, TRT_LOGGER)
60 config.max_workspace_size = GiB(1)
61
62 if not os.path.exists(onnx_file):
63 quit('ONNX file {} not found'.format(onnx_file))
64
65 with open(onnx_file, 'rb') as model:
66 if not parser.parse(model.read()):
67 print('ERROR: Failed to parse the ONNX file.')
68 for error in range(parser.num_errors):
69 print(parser.get_error(error))
70 return None
71
72 config.set_flag(trt.BuilderFlag.INT8)
73
74 setDynamicRange(network, json_file)
75
76 engine = builder.build_engine(network, config)
77
78 with open(engine_file, "wb") as f:
79 f.write(engine.serialize())
80
81
82if __name__ == '__main__':

Callers 1

create_engine.pyFile · 0.70

Calls 4

GiBFunction · 0.70
setDynamicRangeFunction · 0.70
create_networkMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected