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 | |
Dwight Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 21 | import numpy as np |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 22 | import pytest |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 23 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 24 | from ethosu.vela.operation import Op |
Dwight Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 25 | from ethosu.vela.tflite.TensorType import TensorType |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 26 | from ethosu.vela.tflite_mapping import TFLITE_CONV2D_BACKPROP_INDICES |
| 27 | from ethosu.vela.tflite_mapping import TFLITE_IFM_WEIGHTS_BIAS_INDICES |
Diego Russo | d0eee26 | 2020-04-23 18:14:37 +0100 | [diff] [blame] | 28 | from ethosu.vela.tflite_reader import TFLiteSubgraph |
| 29 | |
| 30 | |
| 31 | class 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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 46 | |
| 47 | parse_op_testdata = [ |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 48 | # 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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 58 | ] |
| 59 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 60 | @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 Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 62 | with patch.object(TFLiteSubgraph, "__init__", lambda self, graph, subraph: None): |
| 63 | # Mock a TFLiteSubGraph |
| 64 | sg = TFLiteSubgraph(None, None) |
| 65 | sg.graph = MagicMock() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 66 | sg.graph.operator_codes = [(op_type, opt_serializer, "", indices)] |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 67 | |
| 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 Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 88 | |
| 89 | string_buffer_testdata = [ |
| 90 | (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] | 91 | (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 Lidman | e05de45 | 2020-11-05 15:56:08 +0100 | [diff] [blame] | 94 | ] |
| 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 Verhaard | f4e12be | 2020-12-18 14:23:06 +0100 | [diff] [blame] | 118 | assert np.array_equal(tens.values, buffer) |