blob: b3938bccf91523f9010d3b645c0917bc329289d2 [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
Louis Verhaardae2d5532020-12-11 17:19:54 +010021from ethosu.vela.data_type import DataType
Diqing Zhong94457b12020-12-09 15:22:40 +010022from ethosu.vela.graph_optimiser import convert_batched_fc_shape
Louis Verhaardae2d5532020-12-11 17:19:54 +010023from ethosu.vela.graph_optimiser import optimise_pad
24from ethosu.vela.nn_graph import Graph
Diqing Zhong94457b12020-12-09 15:22:40 +010025from ethosu.vela.operation import Op
Louis Verhaardae2d5532020-12-11 17:19:54 +010026from ethosu.vela.operation import Padding
Diqing Zhong94457b12020-12-09 15:22:40 +010027from ethosu.vela.tensor import create_const_tensor
patrik.gustavssoneeb85152020-12-21 17:10:40 +000028from ethosu.vela.tensor import Shape4D
Diqing Zhong94457b12020-12-09 15:22:40 +010029from ethosu.vela.tensor import Tensor
30from ethosu.vela.test import testutil
31
32
33def 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 Gustavsson2349d422020-12-01 16:02:29 +010040
Diqing Zhong94457b12020-12-09 15:22:40 +010041 ifm.consumer_list.append(op)
42
patrik.gustavssoneeb85152020-12-21 17:10:40 +000043 op.ifm_shapes.append(Shape4D([4, 1, 1, 8]))
44 op.ofm_shapes.append(Shape4D([4, 1, 1, 8]))
Patrik Gustavsson2349d422020-12-01 16:02:29 +010045
Diqing Zhong94457b12020-12-09 15:22:40 +010046 prev_op = op.clone()
Patrik Gustavsson2349d422020-12-01 16:02:29 +010047 prev_op.ifm_shapes = op.ifm_shapes
48 prev_op.ofm_shapes = op.ofm_shapes
49
Diqing Zhong94457b12020-12-09 15:22:40 +010050 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 Gustavsson2349d422020-12-01 16:02:29 +010066 op.ifm_shapes.append([1, 1, 1, 8])
67 op.ofm_shapes.append([1, 1, 1, 8])
68
Diqing Zhong94457b12020-12-09 15:22:40 +010069 prev_op = op.clone()
Patrik Gustavsson2349d422020-12-01 16:02:29 +010070 prev_op.ifm_shapes = op.ifm_shapes
71 prev_op.ofm_shapes = op.ofm_shapes
72
Diqing Zhong94457b12020-12-09 15:22:40 +010073 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 Verhaardae2d5532020-12-11 17:19:54 +010080
81
82def 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