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