Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: Copyright 2020-2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 16 | # |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 17 | # Description: |
| 18 | # Contains unit tests for tflite_reader |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 19 | from unittest.mock import MagicMock |
| 20 | from unittest.mock import patch |
| 21 | |
Dwight Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 22 | import numpy as np |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 23 | import pytest |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 24 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 25 | from ethosu.vela.operation import Op |
Dwight Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 26 | from ethosu.vela.tflite.TensorType import TensorType |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 27 | from ethosu.vela.tflite_mapping import TFLITE_CONV2D_BACKPROP_INDICES |
| 28 | from ethosu.vela.tflite_mapping import TFLITE_IFM_WEIGHTS_BIAS_INDICES |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 29 | from ethosu.vela.tflite_reader import TFLiteSubgraph |
| 30 | |
| 31 | |
| 32 | class 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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 47 | |
| 48 | parse_op_testdata = [ |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 49 | # 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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 59 | ] |
| 60 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 61 | @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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 63 | with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None): |
| 64 | # Mock a TFLiteSubGraph |
| 65 | sg = TFLiteSubgraph(None, None) |
| 66 | sg.graph = MagicMock() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame] | 67 | sg.graph.operator_codes = [(op_type, opt_serializer, "", indices)] |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 68 | |
| 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 Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 89 | |
| 90 | string_buffer_testdata = [ |
| 91 | (np.array([np.random.randint(256) for _ in range(100)], dtype=np.uint8), [3, 5]), |
Louis Verhaard | f4e12be | 2020-12-18 14:23:06 +0100 | [diff] [blame] | 92 | (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 Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 95 | ] |
| 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 Verhaard | f4e12be | 2020-12-18 14:23:06 +0100 | [diff] [blame] | 119 | assert np.array_equal(tens.values, buffer) |