blob: d63c00072a93a494ec7f84245c09b37d80b2eb27 [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
Diego Russod0eee262020-04-23 18:14:37 +010021import pytest
Louis Verhaard0b8268a2020-08-05 16:11:29 +020022
Diego Russod0eee262020-04-23 18:14:37 +010023from ethosu.vela.tflite_reader import TFLiteSubgraph
24
25
26class TestTFLiteSubgraph:
27
28 # Generate some data for testing len1_array_to_scalar
29 len1_testdata = [
30 (0, None),
31 pytest.param(1, None, marks=pytest.mark.xfail),
32 ([1, 2, 3], [1, 2, 3]),
33 ([10], 10),
34 ([], []),
35 ]
36
37 @pytest.mark.parametrize("test_input,expected", len1_testdata)
38 def test_len1_array_to_scalar(self, test_input, expected):
39 output = TFLiteSubgraph.len1_array_to_scalar(test_input)
40 assert output == expected
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020041
42 parse_op_testdata = [
43 # op_type, opt_serializer, inputs, output, expected
44 ("FullyConnected", None, [0, 1, 2], 3, 3), # FC
45 ("FullyConnected", None, [0, 1, -1], 3, 3), # FC disabled Bias
46 ("FullyConnected", None, [0, 1], 3, 3), # FC no Bias
47 ("Conv2D", None, [2, 1, 3], 0, 3), # Conv2D
48 ("Conv2DBackprop", None, [0, 1, 2, 3], 4, 4), # TransposeConv
49 ("Conv2DBackprop", None, [0, 1, 2], 4, 4), # TransposeConv no Bias
50 pytest.param("Conv2D", None, [0, -1, 1], 3, 3, marks=pytest.mark.xfail), # Conv2D no Weights
51 ]
52
53 @pytest.mark.parametrize("op_type, opt_serializer, inputs, output, expected", parse_op_testdata)
54 def test_parse_operator(self, op_type, opt_serializer, inputs, output, expected):
55 with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None):
56 # Mock a TFLiteSubGraph
57 sg = TFLiteSubgraph(None, None)
58 sg.graph = MagicMock()
59 sg.graph.operator_codes = [(op_type, opt_serializer)]
60
61 # Mock a couple of tensors
62 sg.tensors = [MagicMock() for _ in range(5)]
63 for i, tens in enumerate(sg.tensors):
64 tens.name = "tensor_{}".format(i)
65 tens.ops = []
66
67 # Mock op data
68 op_data = MagicMock()
69 op_data.OpcodeIndex.return_value = 0
70 op_data.InputsAsNumpy.return_value = inputs
71 op_data.OutputsAsNumpy.return_value = [output]
72
73 sg.parse_operator(0, op_data)
74
75 # Verify the created Operation
76 created_op = sg.tensors[output].ops[0]
77 assert created_op.type == op_type
78 assert len(created_op.inputs) == expected
79 assert created_op.outputs[0].name == "tensor_{}".format(output)
80 assert inputs[-1] != -1 or not created_op.inputs[-1]