blob: 14c9b204e7d049d0d61a3fcc6001504519ff8326 [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
Diego Russod0eee262020-04-23 18:14:37 +010026from ethosu.vela.tflite_reader import TFLiteSubgraph
27
28
29class TestTFLiteSubgraph:
30
31 # Generate some data for testing len1_array_to_scalar
32 len1_testdata = [
33 (0, None),
34 pytest.param(1, None, marks=pytest.mark.xfail),
35 ([1, 2, 3], [1, 2, 3]),
36 ([10], 10),
37 ([], []),
38 ]
39
40 @pytest.mark.parametrize("test_input,expected", len1_testdata)
41 def test_len1_array_to_scalar(self, test_input, expected):
42 output = TFLiteSubgraph.len1_array_to_scalar(test_input)
43 assert output == expected
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020044
45 parse_op_testdata = [
46 # op_type, opt_serializer, inputs, output, expected
Louis Verhaardaee5d752020-09-30 09:01:52 +020047 (Op.FullyConnected, None, [0, 1, 2], 3, 3), # FC
48 (Op.FullyConnected, None, [0, 1, -1], 3, 3), # FC disabled Bias
49 (Op.FullyConnected, None, [0, 1], 3, 3), # FC no Bias
50 (Op.Conv2D, None, [2, 1, 3], 0, 3), # Conv2D
51 (Op.Conv2DBackpropInput, None, [0, 1, 2, 3], 4, 4), # TransposeConv
52 (Op.Conv2DBackpropInput, None, [0, 1, 2], 4, 4), # TransposeConv no Bias
53 pytest.param(Op.Conv2D, None, [0, -1, 1], 3, 3, marks=pytest.mark.xfail), # Conv2D no Weights
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020054 ]
55
56 @pytest.mark.parametrize("op_type, opt_serializer, inputs, output, expected", parse_op_testdata)
57 def test_parse_operator(self, op_type, opt_serializer, inputs, output, expected):
58 with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None):
59 # Mock a TFLiteSubGraph
60 sg = TFLiteSubgraph(None, None)
61 sg.graph = MagicMock()
Louis Verhaardaee5d752020-09-30 09:01:52 +020062 sg.graph.operator_codes = [(op_type, opt_serializer, "")]
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020063
64 # Mock a couple of tensors
65 sg.tensors = [MagicMock() for _ in range(5)]
66 for i, tens in enumerate(sg.tensors):
67 tens.name = "tensor_{}".format(i)
68 tens.ops = []
69
70 # Mock op data
71 op_data = MagicMock()
72 op_data.OpcodeIndex.return_value = 0
73 op_data.InputsAsNumpy.return_value = inputs
74 op_data.OutputsAsNumpy.return_value = [output]
75
76 sg.parse_operator(0, op_data)
77
78 # Verify the created Operation
79 created_op = sg.tensors[output].ops[0]
80 assert created_op.type == op_type
81 assert len(created_op.inputs) == expected
82 assert created_op.outputs[0].name == "tensor_{}".format(output)
83 assert inputs[-1] != -1 or not created_op.inputs[-1]
Dwight Lidmane05de452020-11-05 15:56:08 +010084
85 string_buffer_testdata = [
86 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [3, 5]),
87 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.int16), [10, 10]),
88 (np.array([np.random.randint(256) for _ in range(100)], dtype=np.float32), [100]),
89 (np.array([], dtype=np.int8), [30]),
90 ]
91
92 @pytest.mark.parametrize("buffer, tens_shape", string_buffer_testdata)
93 def test_parse_tensor_with_string_buffer(self, buffer, tens_shape):
94 tens_data = MagicMock()
95 tens_data.ShapeAsNumpy = MagicMock(return_value=np.array(tens_shape), dtype=np.int32)
96 tens_data.Name = MagicMock(return_value=b"test_data")
97 tens_data.Type = MagicMock(return_value=TensorType.STRING)
98 tens_data.Quantization = MagicMock(return_value=None)
99 tens_data.Buffer = MagicMock(return_value=0)
100
101 tfl_sg = MagicMock()
102 tfl_sg.Name = MagicMock(return_value=b"test_sg")
103 tfl_sg.TensorsLength = MagicMock(return_value=0)
104 tfl_sg.OperatorsLength = MagicMock(return_value=0)
105 tfl_sg.OutputsAsNumpy = MagicMock(return_value=[])
106 tfl_sg.InputsAsNumpy = MagicMock(return_value=[])
107
108 graph = MagicMock()
109 graph.buffers = [buffer]
110
111 subgraph = TFLiteSubgraph(graph, tfl_sg)
112
113 tens = subgraph.parse_tensor(tens_data)
114 assert tens.values is None