Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +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 | # |
| 17 | # Description: |
| 18 | # Unit tests for graph_optimiser |
| 19 | import numpy as np |
| 20 | |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame^] | 21 | from ethosu.vela.data_type import DataType |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 22 | from ethosu.vela.graph_optimiser import convert_batched_fc_shape |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame^] | 23 | from ethosu.vela.graph_optimiser import optimise_pad |
| 24 | from ethosu.vela.nn_graph import Graph |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 25 | from ethosu.vela.operation import Op |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame^] | 26 | from ethosu.vela.operation import Padding |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 27 | from ethosu.vela.tensor import create_const_tensor |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 28 | from ethosu.vela.tensor import Shape4D |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 29 | from ethosu.vela.tensor import Tensor |
| 30 | from ethosu.vela.test import testutil |
| 31 | |
| 32 | |
| 33 | def test_convert_batched_fc(): |
| 34 | """Tests shape conversion of batched fully connected""" |
| 35 | shape = [4, 8] |
| 36 | ifm = create_const_tensor("test_in", shape, np.uint8, np.zeros(shape)) |
| 37 | weights = create_const_tensor("weight_in", shape, np.uint8, np.zeros(shape)) |
| 38 | ofm = Tensor(ifm.shape, np.uint8, "test_out") |
| 39 | op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 40 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 41 | ifm.consumer_list.append(op) |
| 42 | |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 43 | op.ifm_shapes.append(Shape4D([4, 1, 1, 8])) |
| 44 | op.ofm_shapes.append(Shape4D([4, 1, 1, 8])) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 45 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 46 | prev_op = op.clone() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 47 | prev_op.ifm_shapes = op.ifm_shapes |
| 48 | prev_op.ofm_shapes = op.ofm_shapes |
| 49 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 50 | conv_op = convert_batched_fc_shape(op, None, None) |
| 51 | |
| 52 | assert conv_op.ifm != prev_op.ifm |
| 53 | assert conv_op.ofm != prev_op.ofm |
| 54 | assert conv_op.type == Op.FullyConnected |
| 55 | assert len(conv_op.ifm.shape) == 4 |
| 56 | assert conv_op.ifm.shape == conv_op.ofm.shape |
| 57 | assert conv_op.ifm.ops[0].type == Op.Reshape |
| 58 | |
| 59 | shape = [1, 8] |
| 60 | ifm.shape = shape |
| 61 | weights.shape = shape |
| 62 | ofm.shape = shape |
| 63 | op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) |
| 64 | ifm.consumer_list.append(op) |
| 65 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 66 | op.ifm_shapes.append([1, 1, 1, 8]) |
| 67 | op.ofm_shapes.append([1, 1, 1, 8]) |
| 68 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 69 | prev_op = op.clone() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 70 | prev_op.ifm_shapes = op.ifm_shapes |
| 71 | prev_op.ofm_shapes = op.ofm_shapes |
| 72 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 73 | conv_op = convert_batched_fc_shape(op, None, None) |
| 74 | |
| 75 | assert conv_op.ifm == prev_op.ifm |
| 76 | assert conv_op.ofm == prev_op.ofm |
| 77 | assert conv_op.type == Op.FullyConnected |
| 78 | assert len(conv_op.ifm.shape) == 2 |
| 79 | assert conv_op.ifm.shape == conv_op.ofm.shape |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame^] | 80 | |
| 81 | |
| 82 | def test_optimise_pad(): |
| 83 | """ |
| 84 | Tests that the PAD operator is bypassed when followed by a convolution operator, |
| 85 | and that the padding of the convolution operation is correctly updated |
| 86 | """ |
| 87 | # Create Pad operation followed by Conv2D |
| 88 | quant = testutil.default_quant_params() |
| 89 | in_tens = Tensor([1, 76, 75, 64], DataType.uint8, "input") |
| 90 | in_tens.quantization = quant |
| 91 | pad_input = create_const_tensor("pad_input", [4, 2], DataType.int32, [[0, 0], [2, 1], [1, 1], [0, 0]]) |
| 92 | temp_tens = Tensor([1, 79, 77, 64], DataType.uint8, "pad_out") |
| 93 | temp_tens.quantization = quant.clone() |
| 94 | out_tens = Tensor([1, 76, 75, 64], DataType.uint8, "output") |
| 95 | out_tens.quantization = quant.clone() |
| 96 | weight_tens = Tensor([5, 3, 64, 64], DataType.uint8, "weights") |
| 97 | weight_tens.values = np.zeros(weight_tens.shape) |
| 98 | weight_tens.quant_values = np.zeros(weight_tens.shape, np.uint8) |
| 99 | weight_tens.quantization = quant.clone() |
| 100 | |
| 101 | bias_tens = Tensor([64], DataType.int32, "biases") |
| 102 | pad_op = testutil.create_op(Op.Pad, [in_tens, pad_input], temp_tens) |
| 103 | attrs = {"padding": Padding.VALID, "stride_w": 2, "stride_h": 2, "dilation_w_factor": 1, "dilation_h_factor": 1} |
| 104 | attrs["strides"] = (1, attrs["stride_h"], attrs["stride_w"], 1) |
| 105 | pad_op.run_on_npu = True |
| 106 | conv2d_op = testutil.create_op(Op.Conv2D, [temp_tens, weight_tens, bias_tens], out_tens, attrs) |
| 107 | conv2d_op.run_on_npu = True |
| 108 | nng = Graph() |
| 109 | sg = testutil.create_subgraph([pad_op, conv2d_op]) |
| 110 | nng.subgraphs.append(sg) |
| 111 | arch = testutil.create_arch() |
| 112 | |
| 113 | optimise_pad(conv2d_op, nng, arch) |
| 114 | |
| 115 | op = sg.output_tensors[0].ops[0] |
| 116 | assert op.type == Op.Conv2D |
| 117 | assert op.attrs["padding"] == Padding.EXPLICIT |
| 118 | assert op.attrs["explicit_padding"] == (2, 1, 1, 1) |
| 119 | assert op.ifm.shape == [1, 76, 75, 64] |
| 120 | assert pad_op not in op.ifm.ops |