blob: 583821a22e773466a2cec3301ab0e6ae8178d4da [file] [log] [blame]
Louis Verhaardfa2f92a2020-09-21 11:56:18 +02001# 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 support_operators
Michael McGeagh37ded342020-10-01 15:37:44 +010019import numpy as np
20
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020021from ethosu.vela.data_type import DataType
Louis Verhaarde8a5a782020-11-02 18:04:27 +010022from ethosu.vela.operation import ActivationFunction
Louis Verhaardaee5d752020-09-30 09:01:52 +020023from ethosu.vela.operation import Op
Michael McGeagh16895482020-12-14 15:51:20 +000024from ethosu.vela.operation import Padding
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020025from ethosu.vela.supported_operators import SupportedOperators
26from ethosu.vela.tensor import create_const_tensor
Michael McGeagh37ded342020-10-01 15:37:44 +010027from ethosu.vela.tensor import QuantizationParameters
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020028from ethosu.vela.tensor import Tensor
29from ethosu.vela.test import testutil
30
31support = SupportedOperators()
32
33
Michael McGeagh65fd9982020-10-20 11:49:28 +010034def test_constraint_tens_no_dynamic():
35 # Tensors cannot be dynamic (no shape, not a scalar)
36 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 8], [])
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020037 assert not support.is_operator_supported(op)
Michael McGeagh37ded342020-10-01 15:37:44 +010038
39
40def test_constraint_tens_defined_shape():
41 # Tensors cannot have None in them
Michael McGeagh1f951fc2020-10-14 09:30:02 +010042 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, None, 8], [1, 8, 8, 8])
Michael McGeagh37ded342020-10-01 15:37:44 +010043 assert not support.is_operator_supported(op)
44
45
Michael McGeagh65fd9982020-10-20 11:49:28 +010046def test_constraint_tens_output_scalar():
47 # Scalar output is not allowed at all:
Michael McGeagh1f951fc2020-10-14 09:30:02 +010048 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [1, 8, 8, 8], [])
Michael McGeagh65fd9982020-10-20 11:49:28 +010049 op.ofm.values = 0.5
Michael McGeagh37ded342020-10-01 15:37:44 +010050 assert not support.is_operator_supported(op)
Michael McGeagh184b2502020-10-09 17:19:52 +010051
52
Michael McGeagh65fd9982020-10-20 11:49:28 +010053def test_constraint_tens_input_scalar():
Michael McGeagh184b2502020-10-09 17:19:52 +010054 # Shapeless input is allowed if its of a certain type:
Michael McGeagh1f951fc2020-10-14 09:30:02 +010055 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [], [1, 8, 8, 8])
Michael McGeagh184b2502020-10-09 17:19:52 +010056 assert support.is_operator_supported(op)
Michael McGeagh37ded342020-10-01 15:37:44 +010057 # Invalid shapeless input due to op type:
Michael McGeagh1f951fc2020-10-14 09:30:02 +010058 op = testutil.create_op_with_quant_tensors(Op.Relu, [], [1, 8, 8, 8])
Michael McGeagh65fd9982020-10-20 11:49:28 +010059 op.ifm.values = 0.5
Michael McGeagh37ded342020-10-01 15:37:44 +010060 assert not support.is_operator_supported(op)
61
62
63def test_constraint_tens_shape_size():
64 # Tensors cannot be > 4D
Michael McGeagh1f951fc2020-10-14 09:30:02 +010065 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 1, 8, 8, 8], [1, 1, 8, 8, 8])
Michael McGeagh37ded342020-10-01 15:37:44 +010066 assert not support.is_operator_supported(op)
67
68
69def test_constraint_tens_dtype():
Michael McGeagh184b2502020-10-09 17:19:52 +010070 # Tensors can only be of type uint8, int8, int16 and int32
Michael McGeagh1f951fc2020-10-14 09:30:02 +010071 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 8], [1, 8, 8, 8], datatype=DataType.float32)
Michael McGeagh37ded342020-10-01 15:37:44 +010072 assert not support.is_operator_supported(op)
Michael McGeagh184b2502020-10-09 17:19:52 +010073
74
75def test_constraint_tens_int32_ops():
Michael McGeagh37ded342020-10-01 15:37:44 +010076 # For int32, only select op types are allowed:
Michael McGeagh1f951fc2020-10-14 09:30:02 +010077 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [], [1, 8, 8, 8], datatype=DataType.int32)
Michael McGeagh37ded342020-10-01 15:37:44 +010078 assert support.is_operator_supported(op)
Michael McGeagh1f951fc2020-10-14 09:30:02 +010079 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 8], [1, 8, 8, 8], datatype=DataType.int32)
Michael McGeagh37ded342020-10-01 15:37:44 +010080 assert not support.is_operator_supported(op)
81
82
83def test_constraint_tens_dimension():
84 # Tensors can only have values in the inclusive range of 1-65535
Michael McGeagh1f951fc2020-10-14 09:30:02 +010085 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 0], [1, 8, 8, 65536])
Michael McGeagh37ded342020-10-01 15:37:44 +010086 assert not support.is_operator_supported(op)
87
88
Michael McGeagh184b2502020-10-09 17:19:52 +010089def test_constraint_tens_quant_none_check():
90 # Tensors must have quantization parameters
Michael McGeagh1f951fc2020-10-14 09:30:02 +010091 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [], [1, 8, 8, 8], ifm2_quant=None)
Michael McGeagh184b2502020-10-09 17:19:52 +010092 assert not support.is_operator_supported(op)
93
94
95def test_constraint_tens_quant_scale():
96 # Quantization scale cannot be infinit
97 qp = QuantizationParameters()
Michael McGeagh65fd9982020-10-20 11:49:28 +010098 qp.zero_point = 0
Michael McGeagh184b2502020-10-09 17:19:52 +010099 qp.scale_f32 = np.inf
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100100 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [], [1, 8, 8, 8], ifm_quant=qp)
Michael McGeagh184b2502020-10-09 17:19:52 +0100101 assert not support.is_operator_supported(op)
102
103
Dwight Lidmanc7187432020-11-16 17:40:46 +0100104def test_constraint_tens_quant_per_axis_not_supp():
105 # Quantization scale cannot be array-valued for elemwise ops
106 qp = QuantizationParameters()
107 qp.zero_point = np.zeros((1, 3))
108 qp.scale_f32 = np.ones((1, 3))
109 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [], [1, 8, 8, 8], ifm_quant=qp)
110 assert not support.is_operator_supported(op)
111
112
113def test_constraint_tens_quant_per_axis_is_supp():
114 op = testutil.create_op_with_quant_tensors(
115 Op.Conv2DBias, [1, 1, 1, 3], [1, 1, 1, 3], weights_shape=[1, 1, 1, 3], bias_shape=[1, 1, 1, 3]
116 )
117 op.attrs = {"stride_w": 1, "stride_h": 1}
118 assert support.is_operator_supported(op)
119 qp = QuantizationParameters()
120 qp.zero_point = np.zeros((1, 3))
121 qp.scale_f32 = np.ones((1, 3))
122 op.bias.quantization = qp
123 assert support.is_operator_supported(op)
124
125
Dwight Lidman0dd21c72020-11-24 13:45:50 +0100126def test_constraint_fc_output_2d_not_supp():
127 op = testutil.create_op_with_quant_tensors(Op.FullyConnected, [12, 1], [3, 2, 2, 1], weights_shape=[12, 1, 1, 1])
128 assert not support.is_operator_supported(op)
129 op = testutil.create_op_with_quant_tensors(Op.FullyConnected, [12, 1, 1, 1], [1, 3, 4], weights_shape=[12, 1, 1, 1])
130 assert not support.is_operator_supported(op)
131 op = testutil.create_op_with_quant_tensors(Op.FullyConnected, [1, 1, 1, 1], [1], weights_shape=[1, 1, 1, 1])
132 assert not support.is_operator_supported(op)
133
134
135def test_constraint_fc_output_2d_is_supp():
136 op = testutil.create_op_with_quant_tensors(Op.FullyConnected, [4, 8, 8, 4], [32, 32], weights_shape=[4, 8, 8, 4])
137 assert support.is_operator_supported(op)
138 op = testutil.create_op_with_quant_tensors(Op.FullyConnected, [1, 1024], [16, 64], weights_shape=[1, 1024])
139 assert support.is_operator_supported(op)
140
141
Michael McGeagh37ded342020-10-01 15:37:44 +0100142def test_constraint_faf():
143 # Fused activation functions, if set, must be a valid op type
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100144 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 8], [1, 8, 8, 8])
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100145 op.activation = ActivationFunction(Op.Conv2D)
Michael McGeagh37ded342020-10-01 15:37:44 +0100146 assert not support.is_operator_supported(op)
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100147
148
149def test_constraint_conv_pass():
150 # First test a simple conv passes
151 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 1, 1, 1], [1, 1, 1, 1], weights_shape=[1, 1, 1, 1])
152 op.attrs = {"stride_w": 1, "stride_h": 1}
153 assert support.is_operator_supported(op)
154
155
156def test_constraint_stride_type():
157 # Stride width and height must be integer types
158 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8])
159 op.attrs = {"stride_w": 1.5, "stride_h": "1"}
160 assert not support.is_operator_supported(op)
161
162
163def test_constraint_stride_range():
164 # Stride width and height must lie within a certain range
165 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8])
166 op.attrs = {"stride_w": 0, "stride_h": 20}
167 assert not support.is_operator_supported(op)
168
169
170def test_constraint_dilation_type():
171 # Dilation width and height must be integer types
172 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8])
173 op.attrs = {"stride_w": 1, "stride_h": 1, "dilation_w_factor": 1.5, "dilation_h_factor": "1"}
174 assert not support.is_operator_supported(op)
175
176
177def test_constraint_dilation_range():
178 # Dilation width and height must lie within a certain range
179 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8])
180 op.attrs = {"stride_w": 1, "stride_h": 1, "dilation_w_factor": 0, "dilation_h_factor": 20}
181 assert not support.is_operator_supported(op)
182
183
184def test_constraint_dilated_height_range():
185 # Dilated kernel height must lie within a certain range
186 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8], weights_shape=[65, 64, 1, 1])
187 op.attrs = {"stride_w": 1, "stride_h": 1}
188 assert not support.is_operator_supported(op)
189
190
191def test_constraint_dilated_product_range():
192 # Dilated kernel width x height must lie within a certain range
193 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8], weights_shape=[64, 65, 1, 1])
194 op.attrs = {"stride_w": 1, "stride_h": 1}
195 assert not support.is_operator_supported(op)
196
197
198def test_constraint_weights_type():
199 # Weight tensor must be 8-bit
200 op = testutil.create_op_with_quant_tensors(
201 Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8], weights_shape=[1, 1, 1, 1], datatype=DataType.int16
202 )
203 op.attrs = {"stride_w": 1, "stride_h": 1}
204 assert not support.is_operator_supported(op)
205
206
Michael McGeagh65fd9982020-10-20 11:49:28 +0100207def test_constraint_weights_const():
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100208 # Weight tensor cannot be non-const tensors
209 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8])
210 op.attrs = {"stride_w": 1, "stride_h": 1}
211 weights = Tensor([64, 64, 1, 1], DataType.uint8, "weights")
Michael McGeagh65fd9982020-10-20 11:49:28 +0100212 weights.quantization = testutil.default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100213 op.add_input_tensor(weights)
214 assert not support.is_operator_supported(op)
215
216
217def test_constraint_weights_limit():
218 # Sum of weights has a limit
219 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [1, 8, 8, 8], [1, 8, 8, 8], weights_shape=[1, 1, 1, 1])
220 op.attrs = {"stride_w": 1, "stride_h": 1}
221 op.weights.quantization.zero_point = np.array([[[[(127 * 65536) + 1]]]])
222 assert not support.is_operator_supported(op)
223
224
225def test_constraint_bias_type():
226 # Bias must have a certain datatype
227 op = testutil.create_op_with_quant_tensors(Op.Conv2DBias, [1, 8, 8, 8], [1, 8, 8, 8], weights_shape=[1, 1, 1, 1])
228 op.attrs = {"stride_w": 1, "stride_h": 1}
229 bias = Tensor([1, 8, 8, 8], DataType.uint8, "bias")
230 op.add_input_tensor(bias)
231 assert not support.is_operator_supported(op)
232
233
234def test_constraint_bias_40bit():
235 # Bias must not exceed 40-bit
236 op = testutil.create_op_with_quant_tensors(Op.Conv2DBias, [1, 1, 1, 1], [1, 1, 1, 1], weights_shape=[1, 1, 1, 1])
237 op.attrs = {"stride_w": 1, "stride_h": 1}
238 bias = Tensor([1, 1, 1, 1], DataType.int64, "bias")
Michael McGeagh65fd9982020-10-20 11:49:28 +0100239 bias.quant_values = np.array([0x01FF_FFFF_FFFF])
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100240 op.add_input_tensor(bias)
241 assert not support.is_operator_supported(op)
242
243
244def test_constraint_batch_size():
245 op = testutil.create_op_with_quant_tensors(Op.Conv2D, [2, 8, 8, 8], [1, 8, 8, 8], weights_shape=[1, 1, 1, 1])
246 op.attrs = {"stride_w": 1, "stride_h": 1}
247 assert not support.is_operator_supported(op)
Michael McGeagh65fd9982020-10-20 11:49:28 +0100248
249
250def test_constraint_quant_scale_inf():
251 op = testutil.create_op_with_quant_tensors(Op.Relu, [1, 8, 8, 8], [1, 8, 8, 8])
252 op.ofm.quantization.scale_f32 = np.float32(1e-39)
253 assert not support.is_operator_supported(op)
254
255
256def test_constraint_depth_multiplier():
257 # Valid. Depth multiplier is 1 so no further constraints
258 op = testutil.create_op_with_quant_tensors(
259 Op.DepthwiseConv2DBias, [1, 1, 1, 1], [1, 1, 1, 2], weights_shape=[1, 1, 1, 1]
260 )
261 op.attrs = {"stride_w": 1, "stride_h": 1, "depth_multiplier": 1}
262 assert support.is_operator_supported(op)
263 # Invalid. Depth multiplier doesnt equal ofm channel
264 op = testutil.create_op_with_quant_tensors(
265 Op.DepthwiseConv2DBias, [1, 1, 1, 1], [1, 1, 1, 1], weights_shape=[1, 1, 1, 1]
266 )
267 op.attrs = {"stride_w": 1, "stride_h": 1, "depth_multiplier": 2}
268 assert not support.is_operator_supported(op)
269 # Valid. Depth multiplier is equal to ofm channel
270 op = testutil.create_op_with_quant_tensors(
271 Op.DepthwiseConv2DBias, [1, 1, 1, 1], [1, 1, 1, 2], weights_shape=[1, 1, 1, 1]
272 )
273 op.attrs = {"stride_w": 1, "stride_h": 1, "depth_multiplier": 2}
274 assert support.is_operator_supported(op)
275
276
277def test_constraint_tconv_stride():
278 # Strides must be 2
279 op = testutil.create_op_with_quant_tensors(Op.Conv2DBackpropInput, [0], [1, 2, 2, 1], weights_shape=[1, 1, 1, 1])
Michael McGeagh16895482020-12-14 15:51:20 +0000280 op.attrs = {"stride_w": 1, "stride_h": 1, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100281 ifm = Tensor([1, 1, 1, 1], DataType.uint8, "ifm")
282 ifm.quantization = testutil.default_quant_params()
283 op.add_input_tensor(ifm)
284 assert not support.is_operator_supported(op)
285
286
287def test_constraint_tconv_same():
288 # Valid
289 op = testutil.create_op_with_quant_tensors(Op.Conv2DBackpropInput, [0], [1, 2, 2, 1], weights_shape=[1, 1, 1, 1])
Michael McGeagh16895482020-12-14 15:51:20 +0000290 op.attrs = {"stride_w": 2, "stride_h": 2, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100291 ifm = Tensor([1, 1, 1, 1], DataType.uint8, "ifm")
292 ifm.quantization = testutil.default_quant_params()
293 op.add_input_tensor(ifm)
294 assert support.is_operator_supported(op)
295 # Invalid
296 op = testutil.create_op_with_quant_tensors(Op.Conv2DBackpropInput, [0], [1, 4, 4, 1], weights_shape=[1, 1, 1, 1])
Michael McGeagh16895482020-12-14 15:51:20 +0000297 op.attrs = {"stride_w": 2, "stride_h": 2, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100298 ifm = Tensor([1, 1, 1, 1], DataType.uint8, "ifm")
299 ifm.quantization = testutil.default_quant_params()
300 op.add_input_tensor(ifm)
301 assert not support.is_operator_supported(op)
302
303
304def test_constraint_tconv_valid():
305 # Valid
306 op = testutil.create_op_with_quant_tensors(Op.Conv2DBackpropInput, [0], [1, 4, 4, 1], weights_shape=[4, 4, 1, 1])
Michael McGeagh16895482020-12-14 15:51:20 +0000307 op.attrs = {"stride_w": 2, "stride_h": 2, "padding": Padding.VALID}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100308 ifm = Tensor([1, 1, 1, 1], DataType.uint8, "ifm")
309 ifm.quantization = testutil.default_quant_params()
310 op.add_input_tensor(ifm)
311 assert support.is_operator_supported(op)
312 # Invalid
313 op = testutil.create_op_with_quant_tensors(Op.Conv2DBackpropInput, [0], [1, 4, 4, 1], weights_shape=[2, 2, 1, 1])
Michael McGeagh16895482020-12-14 15:51:20 +0000314 op.attrs = {"stride_w": 2, "stride_h": 2, "padding": Padding.VALID}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100315 ifm = Tensor([1, 1, 1, 1], DataType.uint8, "ifm")
316 ifm.quantization = testutil.default_quant_params()
317 op.add_input_tensor(ifm)
318 assert not support.is_operator_supported(op)
319
320
321def test_constraint_matching_in_out_types():
322 # Valid
323 op = testutil.create_op_with_quant_tensors(Op.AvgPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000324 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 2, "filter_height": 2, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100325 assert support.is_operator_supported(op)
326 # Invalid. datatypes for ifm and ofm must match (default uint8)
327 op.ifm.dtype = DataType.int8
328 assert not support.is_operator_supported(op)
329
330
331def test_constraint_filter_type():
332 # Filter width/height must be integers
333 op = testutil.create_op_with_quant_tensors(Op.AvgPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000334 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 2.5, "filter_height": "2", "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100335 assert not support.is_operator_supported(op)
336
337
338def test_constraint_filter_range():
339 # Avg pool restrictions are dependent on padding:
340 # SAME padding restricts both W and H to max 8
341 op = testutil.create_op_with_quant_tensors(Op.AvgPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000342 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 20, "filter_height": 20, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100343 assert not support.is_operator_supported(op)
344 # VALID padding limits are much larger
Michael McGeagh16895482020-12-14 15:51:20 +0000345 op.attrs["padding"] = Padding.VALID
Michael McGeagh65fd9982020-10-20 11:49:28 +0100346 assert support.is_operator_supported(op)
347
348
349def test_constraint_filter_height_range_valid_pad():
350 # Avg pool restrictions are dependent on padding:
351 op = testutil.create_op_with_quant_tensors(Op.AvgPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000352 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 2, "filter_height": 256, "padding": Padding.VALID}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100353 assert support.is_operator_supported(op)
354 # VALID padding restricts to 256 in filter height
355 op.attrs["filter_height"] = 257
356 assert not support.is_operator_supported(op)
357
358
359def test_constraint_filter_product_height_range_valid_pad():
360 # Avg pool restrictions are dependent on padding:
361 op = testutil.create_op_with_quant_tensors(Op.AvgPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000362 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 256, "filter_height": 256, "padding": Padding.VALID}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100363 assert support.is_operator_supported(op)
364 # VALID padding restricts filter W x H to 256x256
365 op.attrs["filter_width"] = 257
366 assert not support.is_operator_supported(op)
367
368
369def test_constraint_filter_height_range():
370 # Max pool restrictions arent dependent on padding
371 op = testutil.create_op_with_quant_tensors(Op.MaxPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000372 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 2, "filter_height": 256, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100373 assert support.is_operator_supported(op)
374 # Restricts to 256 in filter height
375 op.attrs["filter_height"] = 257
376 assert not support.is_operator_supported(op)
377 # Doesnt matter if SAME or VALID
Michael McGeagh16895482020-12-14 15:51:20 +0000378 op.attrs["padding"] = Padding.VALID
Michael McGeagh65fd9982020-10-20 11:49:28 +0100379 assert not support.is_operator_supported(op)
380
381
382def test_constraint_filter_product_height_range():
383 # Max pool restrictions arent dependent on padding
384 op = testutil.create_op_with_quant_tensors(Op.MaxPool, [1, 8, 8, 8], [1, 8, 8, 8])
Michael McGeagh16895482020-12-14 15:51:20 +0000385 op.attrs = {"stride_w": 2, "stride_h": 2, "filter_width": 256, "filter_height": 256, "padding": Padding.SAME}
Michael McGeagh65fd9982020-10-20 11:49:28 +0100386 assert support.is_operator_supported(op)
387 # Restricts filter W x H to 256x256
388 op.attrs["filter_width"] = 257
389 assert not support.is_operator_supported(op)
390 # Doesnt matter if SAME or VALID
Michael McGeagh16895482020-12-14 15:51:20 +0000391 op.attrs["padding"] = Padding.VALID
Michael McGeagh65fd9982020-10-20 11:49:28 +0100392 assert not support.is_operator_supported(op)
393
394
395def test_constraint_resize():
396 # IFM W and H == 1
397 op = testutil.create_op_with_quant_tensors(Op.ResizeBilinear, [1, 1, 1, 8], [1, 8, 8, 8])
398 assert support.is_operator_supported(op)
399 # IFM == OFM
400 op = testutil.create_op_with_quant_tensors(Op.ResizeBilinear, [1, 8, 8, 8], [1, 8, 8, 8])
401 assert support.is_operator_supported(op)
402 # IFM x2 == OFM ; align_corners = False
403 op = testutil.create_op_with_quant_tensors(Op.ResizeBilinear, [1, 4, 4, 8], [1, 8, 8, 8])
404 assert support.is_operator_supported(op)
405 # IFM x2 -1 == OFM ; align_corners = True
406 op = testutil.create_op_with_quant_tensors(Op.ResizeBilinear, [1, 4, 4, 8], [1, 7, 7, 8])
407 op.attrs["align_corners"] = True
408 assert support.is_operator_supported(op)
409 # Invalid cases
410 op = testutil.create_op_with_quant_tensors(Op.ResizeBilinear, [1, 4, 4, 8], [1, 20, 20, 8])
411 assert not support.is_operator_supported(op)
412 op.attrs["align_corners"] = True
413 assert not support.is_operator_supported(op)
414
415
416def test_constraint_matching_shapes():
417 # Softmax requires the ifm and ofm shapes to match
418 op = testutil.create_op_with_quant_tensors(Op.Softmax, [1, 1, 1, 8], [1, 2, 2, 4])
419 assert not support.is_operator_supported(op)
420 op = testutil.create_op_with_quant_tensors(Op.Softmax, [1, 1, 1, 8], [1, 1, 1, 8])
421 assert support.is_operator_supported(op)
422
423
Patrik Gustavsson2fa15882020-11-13 09:02:31 +0100424def test_constraint_beta_value_range():
425 # beta must be positive
426 op = testutil.create_op_with_quant_tensors(Op.Softmax, [1, 1, 1, 8], [1, 1, 1, 8])
427 op.attrs["beta"] = -1.0
428 assert not support.is_operator_supported(op)
429 op.attrs["beta"] = 0.0
430 assert support.is_operator_supported(op)
431
432
Michael McGeagh65fd9982020-10-20 11:49:28 +0100433def test_constraint_splitv_inferred():
434 # SplitV requires a maximum of one inferred shape (-1)
435 qp = testutil.default_quant_params()
436 op = testutil.create_op_with_quant_tensors(Op.SplitV, [1, 1, 1, 8], [1, 1, 1, 8])
437 sizes = create_const_tensor("sizes", [1, 1, 1, 4], DataType.int16, [[[[0, -1, 2, -1]]]], np.int16, quantization=qp)
438 op.add_input_tensor(sizes)
439 assert not support.is_operator_supported(op)
440 op = testutil.create_op_with_quant_tensors(Op.SplitV, [1, 1, 1, 8], [1, 1, 1, 8])
441 sizes = create_const_tensor("sizes", [1, 1, 1, 4], DataType.int16, [[[[0, 1, 2, -1]]]], np.int16, quantization=qp)
442 op.add_input_tensor(sizes)
443 assert support.is_operator_supported(op)
444
445
446def test_constraint_concat_pass():
447 # A working concat
448 op = testutil.create_op_with_quant_tensors(Op.Concat, [1, 1, 1, 4], [1, 1, 1, 8])
449 ifm2 = Tensor([1, 1, 1, 4], DataType.uint8, "in2")
450 ifm2.quantization = testutil.default_quant_params()
451 op.add_input_tensor(ifm2)
452 op.attrs["axis"] = 3
453 assert support.is_operator_supported(op)
454
455
456def test_constraint_axis_exists():
457 # Missing axis attribute
458 op = testutil.create_op_with_quant_tensors(Op.Concat, [1, 1, 1, 4], [1, 1, 1, 8])
459 ifm2 = Tensor([1, 1, 1, 4], DataType.uint8, "in2")
460 ifm2.quantization = testutil.default_quant_params()
461 op.add_input_tensor(ifm2)
462 assert not support.is_operator_supported(op)
463
464
465def test_constraint_axis_valid():
466 # Invalid axis attribute
467 op = testutil.create_op_with_quant_tensors(Op.Concat, [1, 1, 1, 4], [1, 1, 1, 8])
468 ifm2 = Tensor([1, 1, 1, 4], DataType.uint8, "in2")
469 ifm2.quantization = testutil.default_quant_params()
470 op.add_input_tensor(ifm2)
471 op.attrs["axis"] = 7
472 assert not support.is_operator_supported(op)
473
474
475def test_constraint_matching_dimensionality():
476 # Mismatching dimensionality: 4D+2D=4D
477 op = testutil.create_op_with_quant_tensors(Op.Concat, [1, 1, 1, 4], [1, 1, 1, 8])
478 ifm2 = Tensor([1, 4], DataType.uint8, "in2")
479 ifm2.quantization = testutil.default_quant_params()
480 op.add_input_tensor(ifm2)
481 op.attrs["axis"] = 3
482 assert not support.is_operator_supported(op)
483
484
485def test_constraint_valid_dimensions():
486 # Mismatching dimension value:
487 # ifm2 has w and h as 2, which is not the axis to concat and doesnt match ifm1 or ofm
488 op = testutil.create_op_with_quant_tensors(Op.Concat, [1, 1, 1, 4], [1, 1, 1, 8])
489 ifm2 = Tensor([1, 2, 2, 4], DataType.uint8, "in2")
490 ifm2.quantization = testutil.default_quant_params()
491 op.add_input_tensor(ifm2)
492 op.attrs["axis"] = 3
493 assert not support.is_operator_supported(op)
494
495
496def create_strided_slice_op(in_shape, out_shape, start_offsets, end_offsets):
497 qp = testutil.default_quant_params()
498 in0 = Tensor(in_shape, DataType.uint8, "in")
499 in0.quantization = qp
500 in1 = create_const_tensor("begin", [len(start_offsets)], DataType.uint8, start_offsets, quantization=qp)
501 in2 = create_const_tensor("end", [len(end_offsets)], DataType.uint8, end_offsets, quantization=qp)
502 in3 = create_const_tensor("strides", [len(end_offsets)], DataType.uint8, len(end_offsets) * [1], quantization=qp)
503 out = Tensor(out_shape, DataType.uint8, "out")
504 out.quantization = qp
505 attrs = {"ellipsis_mask": 0, "new_axis_mask": 0, "shrink_axis_mask": 0, "begin_mask": 0, "end_mask": 0}
506 return testutil.create_op(Op.StridedSlice, [in0, in1, in2, in3], out, attrs=attrs)
507
508
509def create_strided_slice():
510 # Creates a valid strided slice operator with some valid inputs/outputs
511 op = create_strided_slice_op([1, 10, 10, 10], [1, 5, 5, 10], [127, 2, 2, 0], [0, 7, -3, 0])
512 op.attrs["begin_mask"] = 1
513 op.attrs["end_mask"] = 9
514 assert support.is_operator_supported(op)
515 return op
516
517
518def test_constraint_stridedslice_input_count():
519 # Wrong number of input tensors
520 op = create_strided_slice()
521 op.add_input_tensor(op.inputs[0].clone())
522 assert not support.is_operator_supported(op)
523
524
525def test_constraint_stridedslice_inputs_const():
526 # begin, end, stride values must not be None
527 op = create_strided_slice()
528 op.inputs[1].values = None
529 assert not support.is_operator_supported(op)
530 op = create_strided_slice()
531 op.inputs[2].values = None
532 assert not support.is_operator_supported(op)
533 op = create_strided_slice()
534 op.inputs[3].values = None
535 assert not support.is_operator_supported(op)
536
537
Michael McGeagh65fd9982020-10-20 11:49:28 +0100538def test_constraint_stridedslice_stride_values():
539 # Unsupported strides
540 op = create_strided_slice()
541 op.inputs[3].values = [1, 1, 2, 1]
542 assert not support.is_operator_supported(op)
543
544
545def test_constraint_ellipsis_mask():
546 # Unsupported ellipsis mask
547 op = create_strided_slice()
548 op.attrs["ellipsis_mask"] = 1
549 assert not support.is_operator_supported(op)
550
551
552def test_constraint_axis_masks():
553 op = create_strided_slice()
554 # Setting one of new_axis_mask/shrink_axis_mask to non-zero is ok
555 op.attrs["new_axis_mask"] = 2
556 assert support.is_operator_supported(op)
557 op = create_strided_slice()
558 op.attrs["shrink_axis_mask"] = 3
559 assert support.is_operator_supported(op)
560 # But setting both to non-zero is not supported
561 op.attrs["new_axis_mask"] = 2
562 assert not support.is_operator_supported(op)
563
564
565def test_constraint_slice_ranges():
566 # Examples where end offset <= begin offset
567 op = create_strided_slice()
568 op.inputs[1].values = [0, 7, 2, 0]
569 assert not support.is_operator_supported(op)
570 op = create_strided_slice()
571 op.inputs[2].values = [0, 7, 2, 0]
572 assert not support.is_operator_supported(op)
573 op = create_strided_slice()
574 op.attrs["begin_mask"] = 0
575 assert not support.is_operator_supported(op)
576 op = create_strided_slice()
577 op.attrs["end_mask"] = 0
578 assert not support.is_operator_supported(op)
579
580
581def test_constraint_matching_inputs_types():
582 # input data types must match (default is uint8)
583 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
584 op.ifm2.dtype = DataType.int8
585 assert not support.is_operator_supported(op)
586
587
588def test_constraint_matching_signed():
589 # signed inputs require output to also be signed
590 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8], datatype=DataType.int8)
591 op.ofm.dtype = DataType.uint8
592 assert not support.is_operator_supported(op)
593
594
595def test_constraint_unsigned_valid():
596 # unsigned inputs require output to be either:
597 op = testutil.create_elemwise_op(Op.Mul, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
598 # the same (default uint8)
599 assert support.is_operator_supported(op)
600 op.ofm.dtype = DataType.int8
601 assert not support.is_operator_supported(op)
602 op.ofm.dtype = DataType.int16
603 assert not support.is_operator_supported(op)
604 # or int32
605 op.ofm.dtype = DataType.int32
606 assert support.is_operator_supported(op)
607
608
609def test_constraint_inputs_int32():
610 # both inputs must be type int32
611 op = testutil.create_elemwise_op(Op.SHL, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
612 assert not support.is_operator_supported(op)
613 op = testutil.create_elemwise_op(Op.SHL, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8], datatype=DataType.int32)
614 assert support.is_operator_supported(op)
615 op.ifm2.dtype = DataType.int16
616 assert not support.is_operator_supported(op)
617
618
619def test_constraint_output_int32():
620 # output must be type int32
621 op = testutil.create_elemwise_op(Op.SHL, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8], datatype=DataType.int32)
622 assert support.is_operator_supported(op)
623 op.ofm.dtype = DataType.int16
624 assert not support.is_operator_supported(op)
625
626
627def test_constraint_matching_quantization_parameters():
628 qp = QuantizationParameters()
629 qp.scale_f32 = np.float32(1.5)
630 qp.zero_point = 128
631 # valid - all matching (uses default quant params)
632 op = testutil.create_elemwise_op(Op.Minimum, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
633 assert support.is_operator_supported(op)
634 # invalid - ifm mismatch ofm
635 op.ifm.quantization = qp
636 assert not support.is_operator_supported(op)
637 # invalid - ifm2 mismatch ofm
638 op = testutil.create_elemwise_op(Op.Minimum, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
639 op.ifm2.quantization = qp
640 assert not support.is_operator_supported(op)
641 # invalid - both ifm and ifm2 mismatch ofm
642 op = testutil.create_elemwise_op(Op.Minimum, "op", [1, 8, 8, 8], [1, 8, 8, 8], [1, 8, 8, 8])
643 op.ifm.quantization = qp
644 op.ifm2.quantization = qp
645 assert not support.is_operator_supported(op)
646 # valid - all matching
647 op.ofm.quantization = qp
648 assert support.is_operator_supported(op)
649
650
651def test_constraint_elemwise_batch_size():
652 # BINARY CASE
653 # Batch can be >1 if dims is <=2D
654 op = testutil.create_elemwise_op(Op.Add, "op", [2, 2], [2, 2], [2, 2])
655 assert support.is_operator_supported(op)
656 # For dims >2D, batch must be 1
657 op = testutil.create_elemwise_op(Op.Add, "op", [1, 2, 2], [1, 2, 2], [1, 2, 2])
658 assert support.is_operator_supported(op)
659 # invalid case
660 op = testutil.create_elemwise_op(Op.Add, "op", [2, 2, 2], [2, 2, 2], [2, 2, 2])
661 assert not support.is_operator_supported(op)
662
663 # UNARY CASE
664 # Batch can be >1 if dims is <=2D
665 op = testutil.create_elemwise_op(Op.CLZ, "op", [2, 2], None, [2, 2], datatype=DataType.int32)
666 assert support.is_operator_supported(op)
667 # For dims >2D, batch must be 1
668 op = testutil.create_elemwise_op(Op.CLZ, "op", [1, 2, 2], None, [1, 2, 2], datatype=DataType.int32)
669 assert support.is_operator_supported(op)
670 # invalid case
671 op = testutil.create_elemwise_op(Op.CLZ, "op", [2, 2, 2], None, [2, 2, 2], datatype=DataType.int32)
672 assert not support.is_operator_supported(op)
673
674
675def test_constraint_matching_either_shapes():
676 # BINARY CASE
677 # At least one ifm shape must match ofm's shape
Andreas Nevalainend059d8b2020-11-19 14:40:35 +0100678 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4], [4, 4], [4, 4])
Michael McGeagh65fd9982020-10-20 11:49:28 +0100679 assert support.is_operator_supported(op)
Andreas Nevalainend059d8b2020-11-19 14:40:35 +0100680 op = testutil.create_elemwise_op(Op.Add, "op", [4, 4], [1, 4], [4, 4])
Michael McGeagh65fd9982020-10-20 11:49:28 +0100681 assert support.is_operator_supported(op)
682 op = testutil.create_elemwise_op(Op.Add, "op", [4, 4], [4, 4], [2, 2])
683 assert not support.is_operator_supported(op)
Andreas Nevalainend059d8b2020-11-19 14:40:35 +0100684 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4, 1, 16], [1, 1, 4, 1], [1, 4, 4, 16])
685 assert not support.is_operator_supported(op)
686 op = testutil.create_elemwise_op(Op.Add, "op", [1, 1, 4, 1], [1, 4, 1, 16], [1, 4, 4, 16])
687 assert not support.is_operator_supported(op)
Michael McGeagh65fd9982020-10-20 11:49:28 +0100688
689 # UNARY CASE
690 # No second input so this is treated the same as requiring ifm shape to match ofm shape
691 op = testutil.create_elemwise_op(Op.CLZ, "op", [2, 2], None, [2, 2], datatype=DataType.int32)
692 assert support.is_operator_supported(op)
693 op = testutil.create_elemwise_op(Op.CLZ, "op", [4, 4], None, [2, 2], datatype=DataType.int32)
694 assert not support.is_operator_supported(op)
695
696
Andreas Nevalainend059d8b2020-11-19 14:40:35 +0100697def test_constraint_broadcast_shapes():
698 # BINARY CASE
699 # Only allow broadcast to 1 dim, for 1 rank index
700 op = testutil.create_elemwise_op(Op.Add, "op", [1, 1, 4], [1, 2, 4], [1, 2, 4])
701 assert support.is_operator_supported(op)
702 op = testutil.create_elemwise_op(Op.Add, "op", [1, 2, 4], [1, 1, 4], [1, 2, 4])
703 assert support.is_operator_supported(op)
704 # Only allow broadcast to 1 dim, for 3 rank indexes
705 op = testutil.create_elemwise_op(Op.Add, "op", [1, 1, 1, 1], [1, 4, 8, 16], [1, 4, 8, 16])
706 assert support.is_operator_supported(op)
707 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4, 8, 16], [1, 1, 1, 1], [1, 4, 8, 16])
708 assert support.is_operator_supported(op)
709 # One broadcast dim not 1
710 op = testutil.create_elemwise_op(Op.Add, "op", [1, 2, 4], [1, 4, 4], [1, 4, 4])
711 assert not support.is_operator_supported(op)
712 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4, 4], [1, 2, 4], [1, 4, 4])
713 assert not support.is_operator_supported(op)
714 # OFM shape dim largest ifm/ifm2 shape dim
715 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4], [4, 4], [1, 4])
716 assert not support.is_operator_supported(op)
717 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4], [4, 4], [1, 4])
718 assert not support.is_operator_supported(op)
719 op = testutil.create_elemwise_op(Op.Add, "op", [1, 4, 1, 16], [1, 1, 4, 1], [1, 4, 1, 16])
720 assert not support.is_operator_supported(op)
721 op = testutil.create_elemwise_op(Op.Add, "op", [1, 1, 4, 1], [1, 4, 1, 16], [1, 4, 1, 16])
722 assert not support.is_operator_supported(op)
723
724
Michael McGeagh65fd9982020-10-20 11:49:28 +0100725def test_constraint_alpha_valid():
726 # Alpha cannot be negative
727 op = testutil.create_elemwise_op(Op.LeakyRelu, "op", [2, 2], None, [2, 2])
728 op.attrs["alpha"] = 0
729 assert support.is_operator_supported(op)
730 op.attrs["alpha"] = -1
731 assert not support.is_operator_supported(op)