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 | |
| 21 | from ethosu.vela.graph_optimiser import convert_batched_fc_shape |
| 22 | from ethosu.vela.operation import Op |
| 23 | from ethosu.vela.tensor import create_const_tensor |
| 24 | from ethosu.vela.tensor import Tensor |
| 25 | from ethosu.vela.test import testutil |
| 26 | |
| 27 | |
| 28 | def test_convert_batched_fc(): |
| 29 | """Tests shape conversion of batched fully connected""" |
| 30 | shape = [4, 8] |
| 31 | ifm = create_const_tensor("test_in", shape, np.uint8, np.zeros(shape)) |
| 32 | weights = create_const_tensor("weight_in", shape, np.uint8, np.zeros(shape)) |
| 33 | ofm = Tensor(ifm.shape, np.uint8, "test_out") |
| 34 | op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame^] | 35 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 36 | ifm.consumer_list.append(op) |
| 37 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame^] | 38 | op.ifm_shapes.append([4, 1, 1, 8]) |
| 39 | op.ofm_shapes.append([4, 1, 1, 8]) |
| 40 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 41 | prev_op = op.clone() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame^] | 42 | prev_op.ifm_shapes = op.ifm_shapes |
| 43 | prev_op.ofm_shapes = op.ofm_shapes |
| 44 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 45 | conv_op = convert_batched_fc_shape(op, None, None) |
| 46 | |
| 47 | assert conv_op.ifm != prev_op.ifm |
| 48 | assert conv_op.ofm != prev_op.ofm |
| 49 | assert conv_op.type == Op.FullyConnected |
| 50 | assert len(conv_op.ifm.shape) == 4 |
| 51 | assert conv_op.ifm.shape == conv_op.ofm.shape |
| 52 | assert conv_op.ifm.ops[0].type == Op.Reshape |
| 53 | |
| 54 | shape = [1, 8] |
| 55 | ifm.shape = shape |
| 56 | weights.shape = shape |
| 57 | ofm.shape = shape |
| 58 | op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) |
| 59 | ifm.consumer_list.append(op) |
| 60 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame^] | 61 | op.ifm_shapes.append([1, 1, 1, 8]) |
| 62 | op.ofm_shapes.append([1, 1, 1, 8]) |
| 63 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 64 | prev_op = op.clone() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame^] | 65 | prev_op.ifm_shapes = op.ifm_shapes |
| 66 | prev_op.ofm_shapes = op.ofm_shapes |
| 67 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 68 | conv_op = convert_batched_fc_shape(op, None, None) |
| 69 | |
| 70 | assert conv_op.ifm == prev_op.ifm |
| 71 | assert conv_op.ofm == prev_op.ofm |
| 72 | assert conv_op.type == Op.FullyConnected |
| 73 | assert len(conv_op.ifm.shape) == 2 |
| 74 | assert conv_op.ifm.shape == conv_op.ofm.shape |