blob: 453774176e2a8ceab895e26e56ed51f5a7dd886e [file] [log] [blame]
Diqing Zhong94457b12020-12-09 15:22:40 +01001# 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
19import numpy as np
20
21from ethosu.vela.graph_optimiser import convert_batched_fc_shape
22from ethosu.vela.operation import Op
23from ethosu.vela.tensor import create_const_tensor
24from ethosu.vela.tensor import Tensor
25from ethosu.vela.test import testutil
26
27
28def 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 Gustavsson2349d422020-12-01 16:02:29 +010035
Diqing Zhong94457b12020-12-09 15:22:40 +010036 ifm.consumer_list.append(op)
37
Patrik Gustavsson2349d422020-12-01 16:02:29 +010038 op.ifm_shapes.append([4, 1, 1, 8])
39 op.ofm_shapes.append([4, 1, 1, 8])
40
Diqing Zhong94457b12020-12-09 15:22:40 +010041 prev_op = op.clone()
Patrik Gustavsson2349d422020-12-01 16:02:29 +010042 prev_op.ifm_shapes = op.ifm_shapes
43 prev_op.ofm_shapes = op.ofm_shapes
44
Diqing Zhong94457b12020-12-09 15:22:40 +010045 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 Gustavsson2349d422020-12-01 16:02:29 +010061 op.ifm_shapes.append([1, 1, 1, 8])
62 op.ofm_shapes.append([1, 1, 1, 8])
63
Diqing Zhong94457b12020-12-09 15:22:40 +010064 prev_op = op.clone()
Patrik Gustavsson2349d422020-12-01 16:02:29 +010065 prev_op.ifm_shapes = op.ifm_shapes
66 prev_op.ofm_shapes = op.ofm_shapes
67
Diqing Zhong94457b12020-12-09 15:22:40 +010068 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