blob: 871545e43417fb197f23cf689e3983232ac2a87e [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020-2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Diego Russod0eee262020-04-23 18:14:37 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Diego Russod0eee262020-04-23 18:14:37 +010017# Description:
18# Contains unit tests for tflite_reader
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020019from unittest.mock import MagicMock
20from unittest.mock import patch
21
Dwight Lidmane05de452020-11-05 15:56:08 +010022import numpy as np
Diego Russod0eee262020-04-23 18:14:37 +010023import pytest
Louis Verhaard0b8268a2020-08-05 16:11:29 +020024
Louis Verhaardaee5d752020-09-30 09:01:52 +020025from ethosu.vela.operation import Op
Dwight Lidmane05de452020-11-05 15:56:08 +010026from ethosu.vela.tflite.TensorType import TensorType
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020027from ethosu.vela.tflite_mapping import TFLITE_CONV2D_BACKPROP_INDICES
28from ethosu.vela.tflite_mapping import TFLITE_IFM_WEIGHTS_BIAS_INDICES
Diego Russod0eee262020-04-23 18:14:37 +010029from ethosu.vela.tflite_reader import TFLiteSubgraph
30
31
32class TestTFLiteSubgraph:
33
34 # Generate some data for testing len1_array_to_scalar
35 len1_testdata = [
36 (0, None),
37 pytest.param(1, None, marks=pytest.mark.xfail),
38 ([1, 2, 3], [1, 2, 3]),
39 ([10], 10),
40 ([], []),
41 ]
42
43 @pytest.mark.parametrize("test_input,expected", len1_testdata)
44 def test_len1_array_to_scalar(self, test_input, expected):
45 output = TFLiteSubgraph.len1_array_to_scalar(test_input)
46 assert output == expected
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020047
48 parse_op_testdata = [
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020049 # op_type, opt_serializer, indices, inputs, output, expected
50 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1, 2], 3, 3), # FC
51 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1, -1], 3, 3), # FC disabled Bias
52 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1], 3, 3), # FC no Bias
53 (Op.Conv2DBias, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [2, 1, 3], 0, 3), # Conv2D
54 (Op.Conv2DBackpropInput, None, TFLITE_CONV2D_BACKPROP_INDICES, [0, 1, 2, 3], 4, 4), # TransposeConv
55 (Op.Conv2DBackpropInput, None, TFLITE_CONV2D_BACKPROP_INDICES, [0, 1, 2], 4, 4), # TransposeConv no Bias
56 pytest.param(
57 Op.Conv2DBias, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, -1, 1], 3, 3, marks=pytest.mark.xfail
58 ), # Conv2D no Weights
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020059 ]
60
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020061 @pytest.mark.parametrize("op_type, opt_serializer, indices, inputs, output, expected", parse_op_testdata)
62 def test_parse_operator(self, op_type, opt_serializer, indices, inputs, output, expected):
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020063 with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None):
64 # Mock a TFLiteSubGraph
65 sg = TFLiteSubgraph(None, None)
66 sg.graph = MagicMock()
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020067 sg.graph.operator_codes = [(op_type, opt_serializer, "", indices)]
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020068
69 # Mock a couple of tensors
70 sg.tensors = [MagicMock() for _ in range(5)]
71 for i, tens in enumerate(sg.tensors):
72 tens.name = "tensor_{}".format(i)
73 tens.ops = []
74
75 # Mock op data
76 op_data = MagicMock()
77 op_data.OpcodeIndex.return_value = 0
78 op_data.InputsAsNumpy.return_value = inputs
79 op_data.OutputsAsNumpy.return_value = [output]
80
81 sg.parse_operator(0, op_data)
82
83 # Verify the created Operation
84 created_op = sg.tensors[output].ops[0]
85 assert created_op.type == op_type
86 assert len(created_op.inputs) == expected
87 assert created_op.outputs[0].name == "tensor_{}".format(output)
88 assert inputs[-1] != -1 or not created_op.inputs[-1]
Dwight Lidmane05de452020-11-05 15:56:08 +010089
90 string_buffer_testdata = [
91 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [3, 5]),
Louis Verhaardf4e12be2020-12-18 14:23:06 +010092 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [10, 10]),
93 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), []),
94 (np.array([], dtype=np.uint8), [30]),
Dwight Lidmane05de452020-11-05 15:56:08 +010095 ]
96
97 @pytest.mark.parametrize("buffer, tens_shape", string_buffer_testdata)
98 def test_parse_tensor_with_string_buffer(self, buffer, tens_shape):
99 tens_data = MagicMock()
100 tens_data.ShapeAsNumpy = MagicMock(return_value=np.array(tens_shape), dtype=np.int32)
101 tens_data.Name = MagicMock(return_value=b"test_data")
102 tens_data.Type = MagicMock(return_value=TensorType.STRING)
103 tens_data.Quantization = MagicMock(return_value=None)
104 tens_data.Buffer = MagicMock(return_value=0)
105
106 tfl_sg = MagicMock()
107 tfl_sg.Name = MagicMock(return_value=b"test_sg")
108 tfl_sg.TensorsLength = MagicMock(return_value=0)
109 tfl_sg.OperatorsLength = MagicMock(return_value=0)
110 tfl_sg.OutputsAsNumpy = MagicMock(return_value=[])
111 tfl_sg.InputsAsNumpy = MagicMock(return_value=[])
112
113 graph = MagicMock()
114 graph.buffers = [buffer]
115
116 subgraph = TFLiteSubgraph(graph, tfl_sg)
117
118 tens = subgraph.parse_tensor(tens_data)
Louis Verhaardf4e12be2020-12-18 14:23:06 +0100119 assert np.array_equal(tens.values, buffer)