blob: 93d373d0a1da8a219b872fb1fd781e4add6f1558 [file] [log] [blame]
Jan Eilers2cd18472020-12-15 10:42:38 +00001# Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
2# SPDX-License-Identifier: MIT
3
Narumol Prangnawarat74a3cf52021-01-29 15:38:54 +00004import numpy as np
Jan Eilers2cd18472020-12-15 10:42:38 +00005import pytest
6import tflite_runtime.interpreter as tflite
7import os
Narumol Prangnawarat74a3cf52021-01-29 15:38:54 +00008from utils import run_mock_model, run_inference, compare_outputs
Jan Eilers2cd18472020-12-15 10:42:38 +00009
10def test_external_delegate_unknown_options(delegate_dir):
11 print(delegate_dir)
12 with pytest.raises(ValueError):
13 tflite.load_delegate(
14 delegate_dir,
15 options={"wrong": "wrong"})
16
Jan Eilers2cd18472020-12-15 10:42:38 +000017def test_external_delegate_options_multiple_backends(delegate_dir):
18 tflite.load_delegate(
19 delegate_dir,
20 options={"backends": "GpuAcc,CpuAcc,CpuRef,Unknown"})
21
22
23@pytest.mark.GpuAccTest
24def test_external_delegate_options_gpu_tuning(delegate_dir, test_data_folder, tmp_path):
25
26 tuning_file = os.path.join(str(tmp_path), "test_gpu.tuning")
27 # cleanup previous test run if necessary
28 if os.path.exists(tuning_file):
29 os.remove(tuning_file)
30
31 # with tuning level 2 a tuning file should be created
32 armnn_delegate = tflite.load_delegate(
33 delegate_dir,
34 options={
35 "backends": "GpuAcc",
36 "gpu-tuning-level": "2",
37 "gpu-tuning-file": tuning_file,
38 "logging-severity": "info"})
39
40 run_mock_model(armnn_delegate, test_data_folder)
41
42 # destroy delegate, otherwise tuning file won't be written to file
43 armnn_delegate.__del__()
44 assert (os.path.exists(tuning_file))
45
46 # if no tuning level is provided it defaults to 0 which means it will use the tuning parameters from a tuning
47 # file if one is provided
48 armnn_delegate2 = tflite.load_delegate(
49 delegate_dir,
50 options={
51 "backends": "GpuAcc",
52 "gpu-tuning-file": tuning_file,
53 "logging-severity": "info"})
54
55 run_mock_model(armnn_delegate2, test_data_folder)
56
57 # cleanup
58 os.remove(tuning_file)
59
60def test_external_delegate_options_wrong_logging_level(delegate_dir):
61 with pytest.raises(ValueError):
62 tflite.load_delegate(
63 delegate_dir,
64 options={"logging-severity": "wrong"})
Narumol Prangnawarat74a3cf52021-01-29 15:38:54 +000065
66def test_external_delegate_options_debug(capfd, delegate_dir, test_data_folder):
67 # create armnn delegate with debug option
68 armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef', 'debug-data': '1'})
69
70 model_file_name = 'fp32_model.tflite'
71
72 tensor_shape = [1, 2, 2, 1]
73
74 input0 = np.array([1, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
75 input1 = np.array([2, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
76 inputs = [input0, input0, input1]
77 expected_output = np.array([1, 2, 2, 2], dtype=np.float32).reshape(tensor_shape)
78
79 # run the inference
80 armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
81
82 # check results
83 compare_outputs(armnn_outputs, [expected_output])
84
85 captured = capfd.readouterr()
86 assert 'layerGuid' in captured.out
87
88
89def test_external_delegate_options_fp32_to_fp16(capfd, delegate_dir, test_data_folder):
90 # create armnn delegate with reduce-fp32-to-fp16 option
91 armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef',
92 'debug-data': '1',
93 'reduce-fp32-to-fp16': '1'})
94
95 model_file_name = 'fp32_model.tflite'
96
97 tensor_shape = [1, 2, 2, 1]
98
99 input0 = np.array([1, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
100 input1 = np.array([2, 2, 3, 4], dtype=np.float32).reshape(tensor_shape)
101 inputs = [input0, input0, input1]
102 expected_output = np.array([1, 2, 2, 2], dtype=np.float32).reshape(tensor_shape)
103
104 # run the inference
105 armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
106
107 # check results
108 compare_outputs(armnn_outputs, [expected_output])
109
110 captured = capfd.readouterr()
111 assert 'convert_fp32_to_fp16' in captured.out
112 assert 'convert_fp16_to_fp32' in captured.out
113
114def test_external_delegate_options_fp32_to_bf16(capfd, delegate_dir, test_data_folder):
115 # create armnn delegate with reduce-fp32-to-bf16 option
116 armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuRef',
117 'debug-data': '1',
118 'reduce-fp32-to-bf16': '1'})
119
120 model_file_name = 'conv2d.tflite'
121
122 inputShape = [ 1, 5, 5, 1 ]
123 outputShape = [ 1, 3, 3, 1 ]
124
125 inputValues = [ 1, 5, 2, 3, 5,
126 8, 7, 3, 6, 3,
127 3, 3, 9, 1, 9,
128 4, 1, 8, 1, 3,
129 6, 8, 1, 9, 2 ]
130
131 expectedResult = [ 28, 38, 29,
132 96, 104, 53,
133 31, 55, 24 ]
134
135 input = np.array(inputValues, dtype=np.float32).reshape(inputShape)
136 expected_output = np.array(expectedResult, dtype=np.float32).reshape(outputShape)
137
138 # run the inference
139 armnn_outputs = run_inference(test_data_folder, model_file_name, [input], [armnn_delegate])
140
141 # check results
142 compare_outputs(armnn_outputs, [expected_output])
143
144 captured = capfd.readouterr()
145 assert 'convert_fp32_to_bf16' in captured.out
146
147def test_external_delegate_options_memory_import(delegate_dir, test_data_folder):
148 # create armnn delegate with memory-import option
149 armnn_delegate = tflite.load_delegate(delegate_dir, options = {'backends': 'CpuAcc,CpuRef',
150 'memory-import': '1'})
151
152 model_file_name = 'fallback_model.tflite'
153
154 tensor_shape = [1, 2, 2, 1]
155
156 input0 = np.array([1, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape)
157 input1 = np.array([2, 2, 3, 4], dtype=np.uint8).reshape(tensor_shape)
158 inputs = [input0, input0, input1]
159 expected_output = np.array([1, 2, 2, 2], dtype=np.uint8).reshape(tensor_shape)
160
161 # run the inference
162 armnn_outputs = run_inference(test_data_folder, model_file_name, inputs, [armnn_delegate])
163
164 # check results
165 compare_outputs(armnn_outputs, [expected_output])