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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
# Copyright © 2022 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)
@pytest.mark.GpuAccTest
def test_external_delegate_options_gpu_cached_network(delegate_dir, test_data_folder, tmp_path):
binary_file = os.path.join(str(tmp_path), "test_binary.bin")
# cleanup previous test run if necessary
if os.path.exists(binary_file):
os.remove(binary_file)
# Create blank binary file to write to.
open(binary_file, "a").close()
assert (os.path.exists(binary_file))
assert (os.stat(binary_file).st_size == 0)
# Run inference to save cached network.
armnn_delegate = tflite.load_delegate(
delegate_dir,
options={
"backends": "GpuAcc",
"save-cached-network": "1",
"cached-network-filepath": binary_file,
"logging-severity": "info"})
run_mock_model(armnn_delegate, test_data_folder)
# destroy delegate and check if file has been saved.
armnn_delegate.__del__()
assert (os.stat(binary_file).st_size != 0)
# Create second delegate to load in binary file created.
armnn_delegate2 = tflite.load_delegate(
delegate_dir,
options={
"backends": "GpuAcc",
"cached-network-filepath": binary_file,
"logging-severity": "info"})
run_mock_model(armnn_delegate2, test_data_folder)
# cleanup
os.remove(binary_file)
@pytest.mark.GpuAccTest
def test_external_delegate_gpu_fastmath(delegate_dir, test_data_folder):
# create armnn delegate with enable-fast-math
# fast-math is only enabled on Conv2d layer, so use conv2d model.
armnn_delegate = tflite.load_delegate(delegate_dir, options = {"backends": "GpuAcc",
"enable-fast-math": "1",
"logging-severity": "info"})
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])
@pytest.mark.CpuAccTest
def test_external_delegate_cpu_options(delegate_dir, test_data_folder):
# create armnn delegate with enable-fast-math and number-of-threads options
# fast-math is only enabled on Conv2d layer, so use conv2d model.
armnn_delegate = tflite.load_delegate(delegate_dir, options = {"backends": "CpuAcc",
"enable-fast-math": "1",
"number-of-threads": "4",
"logging-severity": "info"})
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])
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_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])
|