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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
# SPDX-License-Identifier: MIT
import numpy as np
import pytest
import tflite_runtime.interpreter as tflite
import os
from utils import run_mock_model, run_inference, compare_outputs
def test_external_delegate_unknown_options(delegate_dir):
print(delegate_dir)
with pytest.raises(ValueError):
tflite.load_delegate(
delegate_dir,
options={"wrong": "wrong"})
def test_external_delegate_options_multiple_backends(delegate_dir):
tflite.load_delegate(
delegate_dir,
options={"backends": "GpuAcc,CpuAcc,CpuRef,Unknown"})
@pytest.mark.GpuAccTest
def test_external_delegate_options_gpu_tuning(delegate_dir, test_data_folder, tmp_path):
tuning_file = os.path.join(str(tmp_path), "test_gpu.tuning")
# cleanup previous test run if necessary
if os.path.exists(tuning_file):
os.remove(tuning_file)
# with tuning level 2 a tuning file should be created
armnn_delegate = tflite.load_delegate(
delegate_dir,
options={
"backends": "GpuAcc",
"gpu-tuning-level": "2",
"gpu-tuning-file": tuning_file,
"logging-severity": "info"})
run_mock_model(armnn_delegate, test_data_folder)
# destroy delegate, otherwise tuning file won't be written to file
armnn_delegate.__del__()
assert (os.path.exists(tuning_file))
# if no tuning level is provided it defaults to 0 which means it will use the tuning parameters from a tuning
# file if one is provided
armnn_delegate2 = tflite.load_delegate(
delegate_dir,
options={
"backends": "GpuAcc",
"gpu-tuning-file": tuning_file,
"logging-severity": "info"})
run_mock_model(armnn_delegate2, test_data_folder)
# cleanup
os.remove(tuning_file)
def test_external_delegate_options_wrong_logging_level(delegate_dir):
with pytest.raises(ValueError):
tflite.load_delegate(
delegate_dir,
options={"logging-severity": "wrong"})
def test_external_delegate_options_debug(capfd, delegate_dir, test_data_folder):
# create armnn delegate with debug option
armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef', 'debug-data': '1'})
model_file_name = 'fp32_model.tflite'
tensor_shape = [1, 2, 2, 1]
input0 = np.array([1, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
input1 = np.array([2, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
inputs = [input0, input0, input1]
expected_output = np.array([1, 2, 2, 2], dtype=np.float32).reshape(tensor_shape)
# run the inference
armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
# check results
compare_outputs(armnn_outputs, [expected_output])
captured = capfd.readouterr()
assert 'layerGuid' in captured.out
def test_external_delegate_options_fp32_to_fp16(capfd, delegate_dir, test_data_folder):
# create armnn delegate with reduce-fp32-to-fp16 option
armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef',
'debug-data': '1',
'reduce-fp32-to-fp16': '1'})
model_file_name = 'fp32_model.tflite'
tensor_shape = [1, 2, 2, 1]
input0 = np.array([1, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
input1 = np.array([2, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
inputs = [input0, input0, input1]
expected_output = np.array([1, 2, 2, 2], dtype=np.float32).reshape(tensor_shape)
# run the inference
armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
# check results
compare_outputs(armnn_outputs, [expected_output])
captured = capfd.readouterr()
assert 'convert_fp32_to_fp16' in captured.out
assert 'convert_fp16_to_fp32' in captured.out
def test_external_delegate_options_fp32_to_bf16(capfd, delegate_dir, test_data_folder):
# create armnn delegate with reduce-fp32-to-bf16 option
armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef',
'debug-data': '1',
'reduce-fp32-to-bf16': '1'})
model_file_name = 'conv2d.tflite'
inputShape = [ 1, 5, 5, 1 ]
outputShape = [ 1, 3, 3, 1 ]
inputValues = [ 1, 5, 2, 3, 5,
8, 7, 3, 6, 3,
3, 3, 9, 1, 9,
4, 1, 8, 1, 3,
6, 8, 1, 9, 2 ]
expectedResult = [ 28, 38, 29,
96, 104, 53,
31, 55, 24 ]
input = np.array(inputValues, dtype=np.float32).reshape(inputShape)
expected_output = np.array(expectedResult, dtype=np.float32).reshape(outputShape)
# run the inference
armnn_outputs = run_inference(test_data_folder, model_file_name, [input], [armnn_delegate])
# check results
compare_outputs(armnn_outputs, [expected_output])
captured = capfd.readouterr()
assert 'convert_fp32_to_bf16' in captured.out
def test_external_delegate_options_memory_import(delegate_dir, test_data_folder):
# create armnn delegate with memory-import option
armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuAcc,CpuRef',
'memory-import': '1'})
model_file_name = 'fallback_model.tflite'
tensor_shape = [1, 2, 2, 1]
input0 = np.array([1, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape)
input1 = np.array([2, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape)
inputs = [input0, input0, input1]
expected_output = np.array([1, 2, 2, 2], dtype=np.uint8).reshape(tensor_shape)
# run the inference
armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
# check results
compare_outputs(armnn_outputs, [expected_output])
|