blob: 2943be8f167e2d4ab7e6c07c6b3bfa58aaf80fe1 [file] [log] [blame]
Richard Burtondc0c6ed2020-04-08 16:39:05 +01001# Copyright © 2020 Arm Ltd. All rights reserved.
2# SPDX-License-Identifier: MIT
3import os
4
5import pytest
6import numpy as np
7
8import pyarmnn as ann
9
10
11@pytest.fixture(scope="function")
12def random_runtime(shared_data_folder):
13 parser = ann.ITfLiteParser()
14 network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'mock_model.tflite'))
15 preferred_backends = [ann.BackendId('CpuRef')]
16 options = ann.CreationOptions()
17 runtime = ann.IRuntime(options)
18
19 graphs_count = parser.GetSubgraphCount()
20
21 graph_id = graphs_count - 1
22 input_names = parser.GetSubgraphInputTensorNames(graph_id)
23
24 input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, input_names[0])
25 input_tensor_id = input_binding_info[0]
26
27 input_tensor_info = input_binding_info[1]
28
29 output_names = parser.GetSubgraphOutputTensorNames(graph_id)
30
31 input_data = np.random.randint(255, size=input_tensor_info.GetNumElements(), dtype=np.uint8)
32
33 const_tensor_pair = (input_tensor_id, ann.ConstTensor(input_tensor_info, input_data))
34
35 input_tensors = [const_tensor_pair]
36
37 output_tensors = []
38
39 for index, output_name in enumerate(output_names):
40 out_bind_info = parser.GetNetworkOutputBindingInfo(graph_id, output_name)
41
42 out_tensor_info = out_bind_info[1]
43 out_tensor_id = out_bind_info[0]
44
45 output_tensors.append((out_tensor_id,
46 ann.Tensor(out_tensor_info)))
47
48 yield preferred_backends, network, runtime, input_tensors, output_tensors
49
50
51@pytest.fixture(scope='function')
52def mock_model_runtime(shared_data_folder):
53 parser = ann.ITfLiteParser()
54 network = parser.CreateNetworkFromBinaryFile(os.path.join(shared_data_folder, 'mock_model.tflite'))
55 graph_id = 0
56
57 input_binding_info = parser.GetNetworkInputBindingInfo(graph_id, "input_1")
58
59 input_tensor_data = np.load(os.path.join(shared_data_folder, 'tflite_parser/input_lite.npy'))
60
61 preferred_backends = [ann.BackendId('CpuRef')]
62
63 options = ann.CreationOptions()
64 runtime = ann.IRuntime(options)
65
66 opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions())
67
68 print(messages)
69
70 net_id, messages = runtime.LoadNetwork(opt_network)
71
72 print(messages)
73
74 input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data])
75
76 output_names = parser.GetSubgraphOutputTensorNames(graph_id)
77 outputs_binding_info = []
78
79 for output_name in output_names:
80 outputs_binding_info.append(parser.GetNetworkOutputBindingInfo(graph_id, output_name))
81
82 output_tensors = ann.make_output_tensors(outputs_binding_info)
83
84 yield runtime, net_id, input_tensors, output_tensors
85
86
87def test_python_disowns_network(random_runtime):
88 preferred_backends = random_runtime[0]
89 network = random_runtime[1]
90 runtime = random_runtime[2]
91 opt_network, _ = ann.Optimize(network, preferred_backends,
92 runtime.GetDeviceSpec(), ann.OptimizerOptions())
93
94 runtime.LoadNetwork(opt_network)
95
96 assert not opt_network.thisown
97
98
99def test_load_network(random_runtime):
100 preferred_backends = random_runtime[0]
101 network = random_runtime[1]
102 runtime = random_runtime[2]
103
104 opt_network, _ = ann.Optimize(network, preferred_backends,
105 runtime.GetDeviceSpec(), ann.OptimizerOptions())
106
107 net_id, messages = runtime.LoadNetwork(opt_network)
108 assert "" == messages
109 assert net_id == 0
110
111
112def test_load_network_properties_provided(random_runtime):
113 preferred_backends = random_runtime[0]
114 network = random_runtime[1]
115 runtime = random_runtime[2]
116
117 opt_network, _ = ann.Optimize(network, preferred_backends,
118 runtime.GetDeviceSpec(), ann.OptimizerOptions())
119
120 properties = ann.INetworkProperties(True, True)
121 net_id, messages = runtime.LoadNetwork(opt_network, properties)
122 assert "" == messages
123 assert net_id == 0
124
125
126def test_unload_network_fails_for_invalid_net_id(random_runtime):
127 preferred_backends = random_runtime[0]
128 network = random_runtime[1]
129 runtime = random_runtime[2]
130
131 ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions())
132
133 with pytest.raises(RuntimeError) as err:
134 runtime.UnloadNetwork(9)
135
136 expected_error_message = "Failed to unload network."
137 assert expected_error_message in str(err.value)
138
139
140def test_enqueue_workload(random_runtime):
141 preferred_backends = random_runtime[0]
142 network = random_runtime[1]
143 runtime = random_runtime[2]
144 input_tensors = random_runtime[3]
145 output_tensors = random_runtime[4]
146
147 opt_network, _ = ann.Optimize(network, preferred_backends,
148 runtime.GetDeviceSpec(), ann.OptimizerOptions())
149
150 net_id, _ = runtime.LoadNetwork(opt_network)
151 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
152
153
154def test_enqueue_workload_fails_with_empty_input_tensors(random_runtime):
155 preferred_backends = random_runtime[0]
156 network = random_runtime[1]
157 runtime = random_runtime[2]
158 input_tensors = []
159 output_tensors = random_runtime[4]
160
161 opt_network, _ = ann.Optimize(network, preferred_backends,
162 runtime.GetDeviceSpec(), ann.OptimizerOptions())
163
164 net_id, _ = runtime.LoadNetwork(opt_network)
165 with pytest.raises(RuntimeError) as err:
166 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
167
168 expected_error_message = "Number of inputs provided does not match network."
169 assert expected_error_message in str(err.value)
170
171
172@pytest.mark.x86_64
173@pytest.mark.parametrize('count', [5])
174def test_multiple_inference_runs_yield_same_result(count, mock_model_runtime):
175 """
176 Test that results remain consistent among multiple runs of the same inference.
177 """
178 runtime = mock_model_runtime[0]
179 net_id = mock_model_runtime[1]
180 input_tensors = mock_model_runtime[2]
181 output_tensors = mock_model_runtime[3]
182
183 expected_results = np.array([[4, 85, 108, 29, 8, 16, 0, 2, 5, 0]])
184
185 for _ in range(count):
186 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
187
188 output_vectors = ann.workload_tensors_to_ndarray(output_tensors)
189
190 for i in range(len(expected_results)):
191 assert output_vectors[i].all() == expected_results[i].all()
192
193
194@pytest.mark.aarch64
195def test_aarch64_inference_results(mock_model_runtime):
196
197 runtime = mock_model_runtime[0]
198 net_id = mock_model_runtime[1]
199 input_tensors = mock_model_runtime[2]
200 output_tensors = mock_model_runtime[3]
201
202 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
203
204 output_vectors = ann.workload_tensors_to_ndarray(output_tensors)
205
206 expected_outputs = expected_results = np.array([[4, 85, 108, 29, 8, 16, 0, 2, 5, 0]])
207
208 for i in range(len(expected_outputs)):
209 assert output_vectors[i].all() == expected_results[i].all()
210
211
212def test_enqueue_workload_with_profiler(random_runtime):
213 """
214 Tests ArmNN's profiling extension
215 """
216 preferred_backends = random_runtime[0]
217 network = random_runtime[1]
218 runtime = random_runtime[2]
219 input_tensors = random_runtime[3]
220 output_tensors = random_runtime[4]
221
222 opt_network, _ = ann.Optimize(network, preferred_backends,
223 runtime.GetDeviceSpec(), ann.OptimizerOptions())
224 net_id, _ = runtime.LoadNetwork(opt_network)
225
226 profiler = runtime.GetProfiler(net_id)
227 # By default profiling should be turned off:
228 assert profiler.IsProfilingEnabled() is False
229
230 # Enable profiling:
231 profiler.EnableProfiling(True)
232 assert profiler.IsProfilingEnabled() is True
233
234 # Run the inference:
235 runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)
236
237 # Get profile output as a string:
238 str_profile = profiler.as_json()
239
240 # Verify that certain markers are present:
241 assert len(str_profile) != 0
242 assert str_profile.find('\"ArmNN\": {') > 0
243
244 # Get events analysis output as a string:
245 str_events_analysis = profiler.event_log()
246
247 assert "Event Sequence - Name | Duration (ms) | Start (ms) | Stop (ms) | Device" in str_events_analysis
248
249 assert profiler.thisown == 0
250
251
252def test_check_runtime_swig_ownership(random_runtime):
253 # Check to see that SWIG has ownership for runtime. This instructs SWIG to take
254 # ownership of the return value. This allows the value to be automatically
255 # garbage-collected when it is no longer in use
256 runtime = random_runtime[2]
257 assert runtime.thisown