-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (78 loc) · 3.08 KB
/
Copy pathmain.py
File metadata and controls
94 lines (78 loc) · 3.08 KB
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
import argparse
import numpy as np
import common
import common_runtime
def main():
# Add an argparser to specify the engine file path and plugin library file path.
parser = argparse.ArgumentParser(
description="Run an engine with Identity Plugin.")
parser.add_argument(
"--engine_file_path",
type=str,
default="../data/identity_neural_network_iplugin_v3.engine",
help="Path to the engine file.",
)
parser.add_argument(
"--plugin_lib_file_path",
type=str,
default=
"../build/src/plugins/IdentityConvIPluginV3/libidentity_conv_iplugin_v3.so",
help="Path to the plugin library file.",
)
args = parser.parse_args()
engine_file_path = args.engine_file_path
plugin_lib_file_path = args.plugin_lib_file_path
common_runtime.load_plugin_lib(plugin_lib_file_path)
engine = common_runtime.load_engine(engine_file_path)
# Profile index is only useful when the engine has dynamic shapes.
inputs, outputs, bindings, stream = common.allocate_buffers(
engine=engine, profile_idx=None)
# Print input tensor information.
print("Input Tensor:")
for host_device_buffer in inputs:
print(
f"Tensor Name: {host_device_buffer.name} Shape: {host_device_buffer.shape} "
f"Data Type: {host_device_buffer.dtype} Data Format: {host_device_buffer.format}"
)
# Print output tensor information.
print("Output Tensor:")
for host_device_buffer in outputs:
print(
f"Tensor Name: {host_device_buffer.name} Shape: {host_device_buffer.shape} "
f"Data Type: {host_device_buffer.dtype} Data Format: {host_device_buffer.format}"
)
# Dummy example.
# Fill each input with random values.
for host_device_buffer in inputs:
data = np.random.uniform(low=-10.0,
high=10.0,
size=host_device_buffer.shape).astype(
host_device_buffer.dtype).flatten()
# Print input tensor data.
print(f"Input Tensor: {host_device_buffer.name}")
print(data)
# Copy data from numpy array to host buffer.
np.copyto(host_device_buffer.host, data)
# Execute the engine.
context = engine.create_execution_context()
common.do_inference(
context=context,
engine=engine,
inputs=inputs,
outputs=outputs,
bindings=bindings,
stream=stream,
)
# Print output tensor data.
for host_device_buffer in outputs:
print(f"Output Tensor: {host_device_buffer.name}")
print(host_device_buffer.host)
# In our case, the input and output tensor data should be exactly the same.
for input_host_device_buffer, output_host_device_buffer in zip(
inputs, outputs):
np.testing.assert_equal(input_host_device_buffer.host,
output_host_device_buffer.host)
# Clean up.
common.free_buffers(inputs=inputs, outputs=outputs, stream=stream)
if __name__ == "__main__":
main()