blob: 664a58c6fb0470cf3fb043c27f37c0a9cff9b45a [file] [log] [blame]
Diego Russod0eee262020-04-23 18:14:37 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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.
16# Description:
17# Contains unit tests for tflite_reader
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020018from unittest.mock import MagicMock
19from unittest.mock import patch
20
Dwight Lidmane05de452020-11-05 15:56:08 +010021import numpy as np
Diego Russod0eee262020-04-23 18:14:37 +010022import pytest
Louis Verhaard0b8268a2020-08-05 16:11:29 +020023
Louis Verhaardaee5d752020-09-30 09:01:52 +020024from ethosu.vela.operation import Op
Dwight Lidmane05de452020-11-05 15:56:08 +010025from ethosu.vela.tflite.TensorType import TensorType
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020026from ethosu.vela.tflite_mapping import TFLITE_CONV2D_BACKPROP_INDICES
27from ethosu.vela.tflite_mapping import TFLITE_IFM_WEIGHTS_BIAS_INDICES
Diego Russod0eee262020-04-23 18:14:37 +010028from ethosu.vela.tflite_reader import TFLiteSubgraph
29
30
31class TestTFLiteSubgraph:
32
33 # Generate some data for testing len1_array_to_scalar
34 len1_testdata = [
35 (0, None),
36 pytest.param(1, None, marks=pytest.mark.xfail),
37 ([1, 2, 3], [1, 2, 3]),
38 ([10], 10),
39 ([], []),
40 ]
41
42 @pytest.mark.parametrize("test_input,expected", len1_testdata)
43 def test_len1_array_to_scalar(self, test_input, expected):
44 output = TFLiteSubgraph.len1_array_to_scalar(test_input)
45 assert output == expected
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020046
47 parse_op_testdata = [
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020048 # op_type, opt_serializer, indices, inputs, output, expected
49 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1, 2], 3, 3), # FC
50 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1, -1], 3, 3), # FC disabled Bias
51 (Op.FullyConnected, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, 1], 3, 3), # FC no Bias
52 (Op.Conv2DBias, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [2, 1, 3], 0, 3), # Conv2D
53 (Op.Conv2DBackpropInput, None, TFLITE_CONV2D_BACKPROP_INDICES, [0, 1, 2, 3], 4, 4), # TransposeConv
54 (Op.Conv2DBackpropInput, None, TFLITE_CONV2D_BACKPROP_INDICES, [0, 1, 2], 4, 4), # TransposeConv no Bias
55 pytest.param(
56 Op.Conv2DBias, None, TFLITE_IFM_WEIGHTS_BIAS_INDICES, [0, -1, 1], 3, 3, marks=pytest.mark.xfail
57 ), # Conv2D no Weights
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020058 ]
59
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020060 @pytest.mark.parametrize("op_type, opt_serializer, indices, inputs, output, expected", parse_op_testdata)
61 def test_parse_operator(self, op_type, opt_serializer, indices, inputs, output, expected):
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020062 with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None):
63 # Mock a TFLiteSubGraph
64 sg = TFLiteSubgraph(None, None)
65 sg.graph = MagicMock()
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020066 sg.graph.operator_codes = [(op_type, opt_serializer, "", indices)]
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020067
68 # Mock a couple of tensors
69 sg.tensors = [MagicMock() for _ in range(5)]
70 for i, tens in enumerate(sg.tensors):
71 tens.name = "tensor_{}".format(i)
72 tens.ops = []
73
74 # Mock op data
75 op_data = MagicMock()
76 op_data.OpcodeIndex.return_value = 0
77 op_data.InputsAsNumpy.return_value = inputs
78 op_data.OutputsAsNumpy.return_value = [output]
79
80 sg.parse_operator(0, op_data)
81
82 # Verify the created Operation
83 created_op = sg.tensors[output].ops[0]
84 assert created_op.type == op_type
85 assert len(created_op.inputs) == expected
86 assert created_op.outputs[0].name == "tensor_{}".format(output)
87 assert inputs[-1] != -1 or not created_op.inputs[-1]
Dwight Lidmane05de452020-11-05 15:56:08 +010088
89 string_buffer_testdata = [
90 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [3, 5]),
Louis Verhaardf4e12be2020-12-18 14:23:06 +010091 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [10, 10]),
92 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), []),
93 (np.array([], dtype=np.uint8), [30]),
Dwight Lidmane05de452020-11-05 15:56:08 +010094 ]
95
96 @pytest.mark.parametrize("buffer, tens_shape", string_buffer_testdata)
97 def test_parse_tensor_with_string_buffer(self, buffer, tens_shape):
98 tens_data = MagicMock()
99 tens_data.ShapeAsNumpy = MagicMock(return_value=np.array(tens_shape), dtype=np.int32)
100 tens_data.Name = MagicMock(return_value=b"test_data")
101 tens_data.Type = MagicMock(return_value=TensorType.STRING)
102 tens_data.Quantization = MagicMock(return_value=None)
103 tens_data.Buffer = MagicMock(return_value=0)
104
105 tfl_sg = MagicMock()
106 tfl_sg.Name = MagicMock(return_value=b"test_sg")
107 tfl_sg.TensorsLength = MagicMock(return_value=0)
108 tfl_sg.OperatorsLength = MagicMock(return_value=0)
109 tfl_sg.OutputsAsNumpy = MagicMock(return_value=[])
110 tfl_sg.InputsAsNumpy = MagicMock(return_value=[])
111
112 graph = MagicMock()
113 graph.buffers = [buffer]
114
115 subgraph = TFLiteSubgraph(graph, tfl_sg)
116
117 tens = subgraph.parse_tensor(tens_data)
Louis Verhaardf4e12be2020-12-18 14:23:06 +0100118 assert np.array_equal(tens.values, buffer)