blob: 867613cd83eb2ea8dbe41da4c1fc9f0317fe5375 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# The SupportedOperators class which is a collection of all supported operators and parameter checks.
Charles Xu87c13502020-08-06 12:17:26 +020018import numpy as np
19
Tim Hallc30f4952020-06-15 20:47:35 +010020from .data_type import BaseType
21from .data_type import DataType
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020022from .operation import get_slice_offsets
23
24
25def warn_cpu(op, msg):
26 print("Warning: {} {}, placing on CPU".format(op.type, msg))
Tim Hall79d07d22020-04-27 18:20:16 +010027
28
29class SupportedOperators:
Michael McGeagh1eeea512020-09-30 14:23:09 +010030 # Categorised lists of supported operators
31 npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead",))
32 convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D",))
33 depthwise_convolution_ops = set(("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D",))
34 transpose_convolution_ops = set(("Conv2DBackpropInput",))
35 max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct",))
36 avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct",))
37 pooling_ops = set(("ReduceSum",)) | max_pooling_ops | avg_pooling_ops
38 resizing_ops = set(("ResizeBilinear",))
39 fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct",))
40 mac_main_ops = (
41 # RNN/LSTM/GRU
42 set(("BlockLSTM",))
43 # convolutions
44 | convolution_ops
45 # depth-wise convolutions
46 | depthwise_convolution_ops
47 # transpose convolutions
48 | transpose_convolution_ops
49 # pooling
50 | pooling_ops
51 # resizing/upscaling
52 | resizing_ops
53 # FC layers
54 | fc_vector_products
55 )
56 unary_elem_wise_main_ops = set(("LeakyRelu", "Abs", "CLZ",))
57 binary_elem_wise_min_max_ops = set(("Minimum", "Maximum",))
58 binary_elem_wise_shift_ops = set(("SHL", "SHR",))
59 binary_elem_wise_add_mul_sub = set(
60 ("AddAct", "MulAct", "SubAct", "QuantizedAdd", "QuantizedSub", "QuantizedMul", "Mul", "Add", "Sub",)
61 )
62 binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops
63 elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops
64 activation_ops = set(
65 (
66 "QuantizedRelu",
67 "QuantizedRelu1",
68 "QuantizedRelu6",
69 "Relu",
70 "Relu6",
71 "ReluN1To1",
72 "Sigmoid",
73 "Tanh",
74 "Softmax",
75 )
76 )
77 npu_post_ops = (
78 # concatenation write direction
79 set(("ConcatSliceWrite",))
80 # bias add and batch norm
81 | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm",))
82 # Quantization
83 | set(("Quantize",))
84 # activation functions
85 | activation_ops
86 )
87 split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack",))
88 concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack",))
89 memory_only_ops = set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims",)) | concat_ops | split_ops
90 shapeless_input_ops = set(("Split", "SplitV",)) | binary_elem_wise_main_ops
91 supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid", "LUT",))
92 supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | npu_post_ops | memory_only_ops
93
Fredrik Svedberg880e7352020-08-25 11:31:47 +020094 def __init__(self):
Tim Hall79d07d22020-04-27 18:20:16 +010095 # Setup supported operator restriction checkers
96 self.supported_operator_restrictions = {}
97 self.supported_operator_restrictions.update(
Michael McGeagh1eeea512020-09-30 14:23:09 +010098 {op: self.check_convolution_restrictions for op in SupportedOperators.convolution_ops}
Tim Hall79d07d22020-04-27 18:20:16 +010099 )
100 self.supported_operator_restrictions.update(
Michael McGeagh1eeea512020-09-30 14:23:09 +0100101 {op: self.check_depthwise_convolution_restrictions for op in SupportedOperators.depthwise_convolution_ops}
Tim Hall79d07d22020-04-27 18:20:16 +0100102 )
Jacob Bohlincf7da102020-05-20 09:03:40 +0200103 self.supported_operator_restrictions.update(
Michael McGeagh1eeea512020-09-30 14:23:09 +0100104 {op: self.check_transpose_convolution_restrictions for op in SupportedOperators.transpose_convolution_ops}
Tim Hall79d07d22020-04-27 18:20:16 +0100105 )
106 self.supported_operator_restrictions.update(
Michael McGeagh1eeea512020-09-30 14:23:09 +0100107 {op: self.check_pooling_restrictions for op in SupportedOperators.pooling_ops}
Tim Hall79d07d22020-04-27 18:20:16 +0100108 )
109 self.supported_operator_restrictions.update(
Michael McGeagh1eeea512020-09-30 14:23:09 +0100110 {op: self.check_resize_restrictions for op in SupportedOperators.resizing_ops}
Tim Hall79d07d22020-04-27 18:20:16 +0100111 )
Michael McGeagh1eeea512020-09-30 14:23:09 +0100112 self.supported_operator_restrictions.update(
113 {op: self.check_vector_product_restrictions for op in SupportedOperators.fc_vector_products}
114 )
115 self.supported_operator_restrictions.update(
116 {op: self.check_element_wise_restrictions for op in SupportedOperators.elem_wise_main_ops}
117 )
118 self.supported_operator_restrictions.update(
119 {op: self.check_memory_only_restrictions for op in SupportedOperators.memory_only_ops}
120 )
121 self.supported_operator_restrictions.update(
122 {op: self.check_activation_ops for op in SupportedOperators.activation_ops}
123 )
Tim Hall79d07d22020-04-27 18:20:16 +0100124
125 def is_operator_supported(self, op):
Michael McGeagh1eeea512020-09-30 14:23:09 +0100126 if op.type not in SupportedOperators.supported_operators:
Tim Hall79d07d22020-04-27 18:20:16 +0100127 return False
128 if not self.check_generic_restrictions(op):
129 return False
130 if op.type in self.supported_operator_restrictions:
131 return self.supported_operator_restrictions[op.type](op)
132 return True
133
Michael McGeagh1eeea512020-09-30 14:23:09 +0100134 @classmethod
135 def check_generic_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100136 # check fully defined shapes
Dwight Lidman25733112020-08-17 11:56:10 +0200137 for t in op.inputs:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200138 if not t:
139 continue
Tim Hall79d07d22020-04-27 18:20:16 +0100140 if not t.has_fully_defined_shape():
Dwight Lidman25733112020-08-17 11:56:10 +0200141 print("Warning:", op.type, "has input(s) of undefined shape, placing on CPU")
142 return False
Michael McGeagh1eeea512020-09-30 14:23:09 +0100143 if t.shape == [] and op.type not in cls.shapeless_input_ops:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200144 print(
145 "Warning:",
146 op.type,
147 "has input(s) of shape [].",
148 "Scalar input or broadcasting is not supported for this operator,",
149 "placing on CPU",
150 )
Dwight Lidman25733112020-08-17 11:56:10 +0200151 return False
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200152 if len(t.shape) > 4:
153 print("Warning:", op.type, "has input(s) of unsupported shape", t.shape, "placing on CPU")
154 return False
Dwight Lidman25733112020-08-17 11:56:10 +0200155 for t in op.outputs:
156 if not t.has_fully_defined_shape():
157 print("Warning:", op.type, "has output(s) of undefined shape, placing on CPU")
158 return False
159 if t.shape == []:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200160 print(
161 "Warning:",
162 op.type,
163 "has output(s) of shape [].",
164 "Scalar input or broadcasting is not supported for this operator,",
165 "placing on CPU",
166 )
Tim Hall79d07d22020-04-27 18:20:16 +0100167 return False
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200168 if len(t.shape) > 4:
169 print("Warning:", op.type, "has output(s) of unsupported shape", t.shape, "placing on CPU")
170 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100171
172 # check data type
173 tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None]
174 if not tensors:
175 tensors = op.inputs
176 for t in tensors:
177 if not (t.dtype.type & BaseType.Int):
178 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200179 if (
180 t.element_size() > 2
Fredrik Svedberg1575b942020-08-18 13:19:18 +0200181 and op.type
182 not in set(("Requantize", "ReduceSum", "CLZ",))
Michael McGeagh1eeea512020-09-30 14:23:09 +0100183 | cls.binary_elem_wise_add_mul_sub
184 | cls.binary_elem_wise_shift_ops
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200185 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100186 return False
187 # check size
188 if any(dim > 65536 for dim in t.shape):
189 return False
190
191 # check fused activations
192 if (
193 "fused_activation_function" in op.attrs
194 and op.attrs["fused_activation_function"] is not None
Michael McGeagh1eeea512020-09-30 14:23:09 +0100195 and op.attrs["fused_activation_function"] not in cls.supported_fused_activations
Tim Hall79d07d22020-04-27 18:20:16 +0100196 ):
197 return False
Andreas Nevalaineneadb1662020-09-01 15:36:26 +0200198
199 # check inf values
200 for tens in op.get_ifm_ifm2_weights_ofm():
Patrik Gustavsson530992a2020-09-30 13:26:59 +0200201 if (
202 (tens is not None)
203 and (tens.quantization is not None)
204 and (tens.quantization.scale_f32 is not None)
205 and (np.isinf(tens.quantization.scale_f32).any())
206 ):
Andreas Nevalaineneadb1662020-09-01 15:36:26 +0200207 print("Warning:", op.type, "has inf valued tensor(s), placing on CPU")
208 return False
209
Tim Hall79d07d22020-04-27 18:20:16 +0100210 return True
211
Michael McGeagh1eeea512020-09-30 14:23:09 +0100212 @classmethod
213 def check_convolution_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100214 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200215 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100216 return False
217
218 # check dilation
219 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
220 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
221 if dilation_w_factor > 2 or dilation_h_factor > 2:
222 return False
223
224 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200225 ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100226 if weight_tensor.element_size() > 1:
227 return False
228
Michael McGeagh1eeea512020-09-30 14:23:09 +0100229 if not cls.check_bias_restrictions(bias_tensor):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200230 return False
231
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200232 # check kernel size [HWIO]
233 dilated_weight_w = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_w_factor - 1)
234 dilated_weight_h = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_h_factor - 1)
235
236 if dilated_weight_w > 64 or dilated_weight_h > 64:
237 return False
238
Andreas Nevalainen8854dc92020-09-24 13:43:00 +0200239 # check non const weights
240 if weight_tensor.values is None:
241 print("Warning:", op.type, "has non-const weights, placing on CPU")
242 return False
243
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200244 # check weight sums over [HWI]
245 zero_point = weight_tensor.quantization.zero_point
246 quant_weights = weight_tensor.quant_values.astype(np.int64)
247 weights = quant_weights - zero_point
248 totals = np.sum(np.absolute(weights), axis=(0, 1, 2))
249
250 if np.amax(totals) > 127 * 65536:
Tim Hall79d07d22020-04-27 18:20:16 +0100251 return False
252
253 # check batch size
254 if ifm_tensor.shape[0] != 1:
255 return False
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200256
Tim Hall79d07d22020-04-27 18:20:16 +0100257 return True
258
Michael McGeagh1eeea512020-09-30 14:23:09 +0100259 @classmethod
260 def check_depthwise_convolution_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100261 # check depth
262 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
263 if op.attrs["depth_multiplier"] > 1 and not (
264 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
265 ):
266 return False
Michael McGeagh1eeea512020-09-30 14:23:09 +0100267 return cls.check_convolution_restrictions(op)
Tim Hall79d07d22020-04-27 18:20:16 +0100268
Michael McGeagh1eeea512020-09-30 14:23:09 +0100269 @classmethod
270 def check_transpose_convolution_restrictions(cls, op):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200271 # check stride
272 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
273 if stride_h != stride_w != 2:
274 return False
275
276 # check output dimensions
277 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
278 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
279 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
280 if op.attrs["padding"] == b"SAME":
281 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
282 return False
283 elif op.attrs["padding"] == b"VALID":
284 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100285 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
286 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
287 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200288 return False
289
Michael McGeagh1eeea512020-09-30 14:23:09 +0100290 return cls.check_convolution_restrictions(op)
Jacob Bohlincf7da102020-05-20 09:03:40 +0200291
Michael McGeagh1eeea512020-09-30 14:23:09 +0100292 @classmethod
293 def check_pooling_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100294 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200295 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100296 return False
297
298 # check data type
299 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
300 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200301 if op.type != "ReduceSum":
302 return False
303 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100304
305 # check batch size
306 if ifm_tensor.shape[0] != 1:
307 return False
308
Michael McGeagh1eeea512020-09-30 14:23:09 +0100309 if op.type in cls.avg_pooling_ops:
Tim Hall79d07d22020-04-27 18:20:16 +0100310 # check kernel size
311 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
312 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100313 if op.attrs["padding"] == b"VALID" and (
314 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
315 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100316 return False
317
Michael McGeagh1eeea512020-09-30 14:23:09 +0100318 if op.type in cls.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200319 # check kernel size (any padding)
320 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100321 return False
322 return True
323
Michael McGeagh1eeea512020-09-30 14:23:09 +0100324 @classmethod
325 def check_resize_restrictions(cls, op):
Dwight Lidman42fed942020-05-29 09:37:03 +0200326 # check unsupported upscaling factor
327 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200328 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
329 return True
Charles Xu36ffaf32020-08-05 15:40:44 +0200330 if op.inputs[0].shape == op.outputs[0].shape:
331 return True
Charles Xu87c13502020-08-06 12:17:26 +0200332 upscaled_shape = np.array(op.inputs[0].shape[1:3])
333 out_shape = np.array(op.outputs[0].shape[1:3])
334 while (upscaled_shape < out_shape).all():
335 upscaled_shape *= 2
336 if op.attrs["align_corners"]:
337 upscaled_shape -= 1
338 if np.array_equal(out_shape, upscaled_shape):
339 return True
340 return False
Dwight Lidman42fed942020-05-29 09:37:03 +0200341
Michael McGeagh1eeea512020-09-30 14:23:09 +0100342 @classmethod
343 def check_vector_product_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100344 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200345 _, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100346 if weight_tensor.element_size() > 1:
347 return False
348
Michael McGeagh1eeea512020-09-30 14:23:09 +0100349 if not cls.check_bias_restrictions(bias_tensor):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200350 return False
351
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200352 # check non const weights
353 if weight_tensor.values is None:
354 print("Warning:", op.type, "has non-const weights, placing on CPU")
355 return False
356
Tim Hall79d07d22020-04-27 18:20:16 +0100357 return True
358
Michael McGeagh1eeea512020-09-30 14:23:09 +0100359 @classmethod
360 def check_element_wise_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100361 # check data type
362 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200363 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100364 if (
Michael McGeagh1eeea512020-09-30 14:23:09 +0100365 op.type in cls.binary_elem_wise_min_max_ops | cls.unary_elem_wise_main_ops
Tim Hallc30f4952020-06-15 20:47:35 +0100366 and ifm_tensor.dtype != ofm_tensor.dtype
367 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100368 return False
Michael McGeagh1eeea512020-09-30 14:23:09 +0100369 if op.type in cls.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200370 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100371 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200372 return False
373 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100374 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200375 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100376 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200377 return False
378 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100379 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200380 return False
381 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100382 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
383 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
384 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200385 return False
Michael McGeagh1eeea512020-09-30 14:23:09 +0100386 elif op.type in cls.binary_elem_wise_shift_ops | set(("CLZ")):
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200387 if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32:
388 return False
389 if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32:
390 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100391
392 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200393 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100394 return False
Michael McGeagh1eeea512020-09-30 14:23:09 +0100395 if op.type in cls.binary_elem_wise_main_ops: # if op type is unary, ifm2_tensor is None
Dwight Lidmanf995db72020-04-27 11:15:12 +0200396 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
397 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200398
399 # negative alpha values are not supported
400 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
401 return False
402
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200403 # check if ifm or ifm2 has ofm shape
404 if ifm_tensor.shape != ofm_tensor.shape and ifm2_tensor.shape != ofm_tensor.shape:
405 return False
406
Michael McGeagh1eeea512020-09-30 14:23:09 +0100407 if op.type in cls.binary_elem_wise_min_max_ops and not cls.check_quantization_restrictions_binary_elem_wise(op):
Patrik Gustavsson530992a2020-09-30 13:26:59 +0200408 return False
409
Tim Hall79d07d22020-04-27 18:20:16 +0100410 return True
411
Michael McGeagh1eeea512020-09-30 14:23:09 +0100412 @classmethod
413 def check_memory_only_restrictions(cls, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100414 if op.type == "StridedSlice":
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200415 if len(op.inputs) != 4:
416 warn_cpu(op, "has {} input tensors, only 4 inputs are supported".format(len(op.inputs)))
Tim Hall79d07d22020-04-27 18:20:16 +0100417 return False
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200418 input_tens, begin_tens, end_tens, strides_tens = op.inputs
419 if begin_tens.values is None or end_tens.values is None or strides_tens.values is None:
420 warn_cpu(op, "has a non-constant begin, end, or stride input tensor, which is not supported")
421 return False
422 if not (
423 len(input_tens.shape)
424 == len(op.outputs[0].shape)
425 == len(begin_tens.values)
426 == len(end_tens.values)
427 == len(strides_tens.values)
428 ):
429 warn_cpu(op, "has input tensors with shapes that are not supported")
430 return False
431 # check stride size
432 if any(stride != 1 for stride in strides_tens.values):
433 warn_cpu(op, "has stride values {}, only stride 1 values are supported".format(strides_tens.values))
Michael McGeaghecd20522020-07-31 16:59:45 +0100434 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200435 # check ellipsis_mask
436 if op.attrs["ellipsis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200437 warn_cpu(op, "ellipsis_mask is {}, only 0 is supported".format(op.attrs["ellipsis_mask"]))
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200438 return False
439 # check if both new_axis_mask and shrink_axis_mask have bit set
440 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200441 warn_cpu(op, "new_axis_mask and shrink_axis_mask are both non-zero, which is not supported")
442 return False
443 # Calculate offset start/end
444 offset_start = get_slice_offsets(input_tens.shape, begin_tens, op.attrs["begin_mask"], is_begin=True)
445 offset_end = get_slice_offsets(input_tens.shape, end_tens, op.attrs["end_mask"], is_begin=False)
446 # check "end - begin" doesn't result in any zero or negative elements
447 if any((end - begin) <= 0 for begin, end in zip(offset_start, offset_end)):
448 warn_cpu(
449 op,
450 "has slice begin values {}, some of which are >= end values {}, which is illegal".format(
451 begin_tens.values, end_tens.values
452 ),
453 )
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200454 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200455 if op.type == "SplitV":
456 # check that maximum one size is set to -1, indicating that size should be inferred
457 sizes = op.inputs[1].values
458 num_to_be_inferred = 0
459 for size in sizes:
460 if size == -1:
461 num_to_be_inferred += 1
462
463 if num_to_be_inferred > 1:
464 print("Warning:", op.type, "has more than one size to be inferred, which is illegal, placing on CPU")
465 return False
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200466 if op.type.find("Concat") != -1:
467 axis = op.attrs.get("axis", None)
468 if axis is None:
469 print("Warning:", op.type, "invalid or missing axis, placing on CPU")
470 return False
471 if axis < 0:
472 axis += len(op.inputs[0].shape)
473 if not 0 < axis < len(op.inputs[0].shape):
474 print("Warning:", op.type, "invalid axis", axis, ", placing on CPU")
475 return False
476 ofm = op.outputs[0]
477 ofm_dims = len(ofm.shape)
478 for ifm in op.inputs:
479 if len(ifm.shape) != ofm_dims:
480 return False
481 for i in range(ofm_dims):
482 if i != axis and ifm.shape[i] != ofm.shape[i]:
Patrik Gustavsson530992a2020-09-30 13:26:59 +0200483 print(
484 "Warning:",
485 op.type,
486 "invalid ifm:",
487 ifm.name,
488 ifm.shape,
489 "mismatch in dimension",
490 i,
491 ", placing on CPU",
492 )
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200493 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200494
Tim Hall79d07d22020-04-27 18:20:16 +0100495 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200496
Michael McGeagh1eeea512020-09-30 14:23:09 +0100497 @classmethod
498 def check_quantization_restrictions_binary_elem_wise(cls, op):
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200499 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Tim Halle3786ac2020-07-28 17:40:50 +0100500 assert len(op.inputs) >= 2 and len(op.outputs) == 1
501
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200502 if (
Tim Halle3786ac2020-07-28 17:40:50 +0100503 op.inputs[0].quantization is None
Michael McGeagh34ad19b2020-09-04 15:44:23 +0100504 or not op.inputs[0].is_scaling_equal(op.inputs[1])
505 or not op.inputs[0].is_scaling_equal(op.outputs[0])
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200506 ):
507 print(
508 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
509 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200510 return False
Tim Halle3786ac2020-07-28 17:40:50 +0100511
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200512 return True
513
Michael McGeagh1eeea512020-09-30 14:23:09 +0100514 @classmethod
515 def check_activation_ops(cls, op):
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200516 if op.type == "Softmax":
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200517 ifm_tensor = op.inputs[0]
518 ofm_tensor = op.outputs[0]
519
520 # check data type
521 if ifm_tensor.dtype != ofm_tensor.dtype:
522 return False
523
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200524 if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16):
525 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200526
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200527 # check shape
528 if len(ifm_tensor.shape) > 4 or ifm_tensor.shape != ofm_tensor.shape:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200529 return False
530
531 return True
Jacob Bohlin49d92122020-08-19 14:36:46 +0200532
Michael McGeagh1eeea512020-09-30 14:23:09 +0100533 @classmethod
534 def check_bias_restrictions(cls, bias_tensor):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200535 # check data type
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200536 if bias_tensor is not None and bias_tensor.dtype not in (DataType.int32, DataType.int64):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200537 return False
538
539 # check if values fits in 40-bit
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200540 if bias_tensor is not None and bias_tensor.dtype == DataType.int64:
Tim Hall71525172020-08-29 15:09:57 +0100541 for quant_value in bias_tensor.quant_values:
542 if not (-(1 << 39) <= quant_value < (1 << 39)):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200543 return False
544
545 return True