blob: 55980e3d94fa0389c970d688bb2b64f2e7d98097 [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
Patrik Gustavsson3a269202021-01-21 08:28:55 +010023from ethosu.vela.graph_optimiser import optimise_graph_a
Louis Verhaardae2d5532020-12-11 17:19:54 +010024from ethosu.vela.graph_optimiser import optimise_pad
Patrik Gustavsson2c2522d2021-01-29 11:51:31 +010025from ethosu.vela.graph_optimiser import rewrite_fully_connected_input
Louis Verhaardae2d5532020-12-11 17:19:54 +010026from ethosu.vela.nn_graph import Graph
Diqing Zhong94457b12020-12-09 15:22:40 +010027from ethosu.vela.operation import Op
Louis Verhaardae2d5532020-12-11 17:19:54 +010028from ethosu.vela.operation import Padding
Patrik Gustavsson3a269202021-01-21 08:28:55 +010029from ethosu.vela.rewrite_graph import verify_graph_health
Diqing Zhong94457b12020-12-09 15:22:40 +010030from ethosu.vela.tensor import create_const_tensor
patrik.gustavssoneeb85152020-12-21 17:10:40 +000031from ethosu.vela.tensor import Shape4D
Diqing Zhong94457b12020-12-09 15:22:40 +010032from ethosu.vela.tensor import Tensor
33from ethosu.vela.test import testutil
34
35
36def test_convert_batched_fc():
37 """Tests shape conversion of batched fully connected"""
Patrik Gustavsson3a269202021-01-21 08:28:55 +010038 ifm_shape = [4, 8]
39 ifm = create_const_tensor("test_in", ifm_shape, np.uint8, np.zeros(ifm_shape))
40 w_shape = [8, 4]
41 weights = create_const_tensor("weight_in", w_shape, np.uint8, np.zeros(w_shape))
Diqing Zhong94457b12020-12-09 15:22:40 +010042 ofm = Tensor(ifm.shape, np.uint8, "test_out")
43 op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm)
Patrik Gustavsson2349d422020-12-01 16:02:29 +010044
Diqing Zhong94457b12020-12-09 15:22:40 +010045 ifm.consumer_list.append(op)
46
47 prev_op = op.clone()
Patrik Gustavsson3a269202021-01-21 08:28:55 +010048 prev_op.ifm_shapes = op.ifm_shapes.copy()
49 prev_op.ofm_shapes = op.ofm_shapes.copy()
Patrik Gustavsson2349d422020-12-01 16:02:29 +010050
Patrik Gustavsson2c2522d2021-01-29 11:51:31 +010051 rewrite_fully_connected_input(op, None, None)
Diqing Zhong94457b12020-12-09 15:22:40 +010052 conv_op = convert_batched_fc_shape(op, None, None)
Diqing Zhong94457b12020-12-09 15:22:40 +010053 assert conv_op.ifm == prev_op.ifm
54 assert conv_op.ofm == prev_op.ofm
Patrik Gustavsson3a269202021-01-21 08:28:55 +010055 assert op.ifm_shapes[0] == Shape4D([1, 2, 2, 8])
56 assert op.ofm_shapes[0] == Shape4D([1, 2, 2, 8])
Diqing Zhong94457b12020-12-09 15:22:40 +010057 assert conv_op.type == Op.FullyConnected
58 assert len(conv_op.ifm.shape) == 2
Patrik Gustavsson3a269202021-01-21 08:28:55 +010059 assert len(conv_op.ofm.shape) == 2
60 assert conv_op.ifm.shape == conv_op.ofm.shape
61
62 ifm.shape = [1, 8]
63 weights.shape = [8, 1]
64 ofm.shape = [1, 8]
65 op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm)
66 ifm.consumer_list.append(op)
67
68 prev_op = op.clone()
69 prev_op.ifm_shapes = op.ifm_shapes.copy()
70 prev_op.ofm_shapes = op.ofm_shapes.copy()
71
Patrik Gustavsson2c2522d2021-01-29 11:51:31 +010072 rewrite_fully_connected_input(op, None, None)
Patrik Gustavsson3a269202021-01-21 08:28:55 +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 op.ifm_shapes[0] == prev_op.ifm_shapes[0]
78 assert op.ofm_shapes[0] == prev_op.ofm_shapes[0]
79 assert conv_op.type == Op.FullyConnected
80 assert len(conv_op.ifm.shape) == 2
81 assert len(conv_op.ofm.shape) == 2
Diqing Zhong94457b12020-12-09 15:22:40 +010082 assert conv_op.ifm.shape == conv_op.ofm.shape
Louis Verhaardae2d5532020-12-11 17:19:54 +010083
84
85def test_optimise_pad():
86 """
87 Tests that the PAD operator is bypassed when followed by a convolution operator,
88 and that the padding of the convolution operation is correctly updated
89 """
90 # Create Pad operation followed by Conv2D
91 quant = testutil.default_quant_params()
92 in_tens = Tensor([1, 76, 75, 64], DataType.uint8, "input")
93 in_tens.quantization = quant
94 pad_input = create_const_tensor("pad_input", [4, 2], DataType.int32, [[0, 0], [2, 1], [1, 1], [0, 0]])
95 temp_tens = Tensor([1, 79, 77, 64], DataType.uint8, "pad_out")
96 temp_tens.quantization = quant.clone()
97 out_tens = Tensor([1, 76, 75, 64], DataType.uint8, "output")
98 out_tens.quantization = quant.clone()
99 weight_tens = Tensor([5, 3, 64, 64], DataType.uint8, "weights")
100 weight_tens.values = np.zeros(weight_tens.shape)
101 weight_tens.quant_values = np.zeros(weight_tens.shape, np.uint8)
102 weight_tens.quantization = quant.clone()
103
104 bias_tens = Tensor([64], DataType.int32, "biases")
105 pad_op = testutil.create_op(Op.Pad, [in_tens, pad_input], temp_tens)
106 attrs = {"padding": Padding.VALID, "stride_w": 2, "stride_h": 2, "dilation_w_factor": 1, "dilation_h_factor": 1}
107 attrs["strides"] = (1, attrs["stride_h"], attrs["stride_w"], 1)
108 pad_op.run_on_npu = True
109 conv2d_op = testutil.create_op(Op.Conv2D, [temp_tens, weight_tens, bias_tens], out_tens, attrs)
110 conv2d_op.run_on_npu = True
111 nng = Graph()
112 sg = testutil.create_subgraph([pad_op, conv2d_op])
113 nng.subgraphs.append(sg)
114 arch = testutil.create_arch()
115
116 optimise_pad(conv2d_op, nng, arch)
117
118 op = sg.output_tensors[0].ops[0]
119 assert op.type == Op.Conv2D
120 assert op.attrs["padding"] == Padding.EXPLICIT
121 assert op.attrs["explicit_padding"] == (2, 1, 1, 1)
122 assert op.ifm.shape == [1, 76, 75, 64]
123 assert pad_op not in op.ifm.ops
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100124
125
126def test_remove_reshape():
127 """
128 Tests that the expected reshape are removed in graph_optimisation
129 """
130
131 def setup_network():
132 quant = testutil.default_quant_params()
133 # create reshape1 op
134 ifm_shape = [64, 16]
135 reshape1_ofm_shape = [1, 4, 16, 16]
136 reshape1_ifm = create_const_tensor("reshape1_in", ifm_shape, DataType.uint8, np.zeros(ifm_shape))
137 reshape1_ifm.quantization = quant
138 reshape1_ofm = create_const_tensor(
139 "reshape1_out", reshape1_ofm_shape, DataType.uint8, np.zeros(reshape1_ofm_shape)
140 )
141 reshape1_ofm.quantization = quant
142 shape_tens = create_const_tensor("reshape1_shape", [1], DataType.int32, reshape1_ofm_shape)
143 reshape1_op = testutil.create_op(Op.Reshape, [reshape1_ifm, shape_tens], reshape1_ofm, set_ifm_ofm_shapes=False)
144 reshape1_op.attrs["new_shape"] = reshape1_ofm_shape
145 reshape1_op.run_on_npu = True
146
147 # create reshape2 op
148 reshape2_ofm_shape = [1, 8, 8, 16]
149 reshape2_ofm = create_const_tensor(
150 "reshape2_out", reshape2_ofm_shape, DataType.uint8, np.zeros(reshape2_ofm_shape)
151 )
152 reshape2_ofm.quantization = quant
153 shape_tens = create_const_tensor("reshape2_shape", [1], DataType.int32, reshape2_ofm_shape)
154 reshape2_op = testutil.create_op(Op.Reshape, [reshape1_ofm, shape_tens], reshape2_ofm, set_ifm_ofm_shapes=False)
155 reshape2_op.attrs["new_shape"] = reshape2_ofm_shape
156 reshape2_op.run_on_npu = True
157
158 # create conv op
159 conv_ofm = Tensor([1, 8, 8, 16], DataType.uint8, "output")
160 conv_ofm.quantization = quant.clone()
161 weight_tens = Tensor([1, 1, 16, 16], DataType.uint8, "weights")
162 weight_tens.values = np.zeros(weight_tens.shape)
163 weight_tens.quant_values = np.zeros(weight_tens.shape, np.uint8)
164 weight_tens.quantization = quant.clone()
165 bias_tens = Tensor([16], DataType.int32, "biases")
166
167 attrs = {"padding": Padding.SAME, "stride_w": 1, "stride_h": 1, "dilation_w_factor": 1, "dilation_h_factor": 1}
168 attrs["strides"] = (1, attrs["stride_h"], attrs["stride_w"], 1)
169
170 conv2d_op = testutil.create_op(
171 Op.Conv2D, [reshape1_ofm, weight_tens, bias_tens], conv_ofm, attrs=attrs, set_ifm_ofm_shapes=False
172 )
173 conv2d_op.run_on_npu = True
174
175 # create reshape3 op
176 ofm_shape = [8, 8, 16]
177 reshape3_ofm = create_const_tensor("reshape3_out", ofm_shape, DataType.uint8, np.zeros(ofm_shape))
178 reshape3_ofm.quantization = quant
179 shape_tens = create_const_tensor("reshape3_shape", [1], DataType.int32, ofm_shape)
180 reshape3_op = testutil.create_op(Op.Reshape, [conv_ofm, shape_tens], reshape3_ofm, set_ifm_ofm_shapes=False)
181 reshape3_op.attrs["new_shape"] = ofm_shape
182 reshape3_op.run_on_npu = True
183 nng = Graph()
184 sg = testutil.create_subgraph([reshape1_op, reshape2_op, conv2d_op, reshape3_op])
185 nng.subgraphs.append(sg)
186
187 return nng, reshape1_op, reshape2_op, conv2d_op, reshape3_op
188
189 # Test1 no Reshape op is expected to remain in the NPU subgrapgh
190 # but first one will be put on CPU
191 # Network is Reshape-Reshape-Conv-Reshape
192 # Result is cpu_Reshape-Conv
193 nng, reshape1_op, reshape2_op, conv2d_op, reshape3_op = setup_network()
194 arch = testutil.create_arch()
195 assert verify_graph_health(nng)
196 nng = optimise_graph_a(nng, arch)
197 assert verify_graph_health(nng)
198 assert conv2d_op.ifm == reshape1_op.ofm
199 assert conv2d_op.ofm == reshape3_op.ofm
200
201 # Test2 reshape2 with different quantisation, this Reshape op is expected to remain
202 # Network is Reshape-Reshape-Conv-Reshape
203 # expected is cpu_Reshape-Reshape-Conv
204 nng, reshape1_op, reshape2_op, conv2d_op, reshape3_op = setup_network()
205 quant_zp32 = testutil.default_quant_params()
206 quant_zp32.zero_point = 32
207 reshape2_op.ofm.quantization = quant_zp32
208 assert verify_graph_health(nng)
209 nng = optimise_graph_a(nng, arch)
210 assert verify_graph_health(nng)
211 assert conv2d_op.ofm == reshape3_op.ofm