blob: 73a4f282610d1c425052a6fe3cf72a9556ffcb96 [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:
Fredrik Svedberg880e7352020-08-25 11:31:47 +020030 def __init__(self):
Tim Hall79d07d22020-04-27 18:20:16 +010031 # Categorised lists of supported operators
Fredrik Svedberga0c36242020-06-03 15:43:31 +020032 self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead",))
33 self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D",))
Tim Hall79d07d22020-04-27 18:20:16 +010034 self.depthwise_convolution_ops = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020035 ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D,")
Tim Hall79d07d22020-04-27 18:20:16 +010036 )
Jacob Bohlincf7da102020-05-20 09:03:40 +020037 self.transpose_convolution_ops = set(("Conv2DBackpropInput",))
Fredrik Svedberga0c36242020-06-03 15:43:31 +020038 self.max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct",))
39 self.avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct",))
40 self.pooling_ops = set(("ReduceSum",)) | self.max_pooling_ops | self.avg_pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020041 self.resizing_ops = set(("ResizeBilinear",))
Fredrik Svedberga0c36242020-06-03 15:43:31 +020042 self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct",))
Tim Hall79d07d22020-04-27 18:20:16 +010043 self.mac_main_ops = (
44 # convolutions
45 self.convolution_ops
46 # depth-wise convolutions
47 | self.depthwise_convolution_ops
Jacob Bohlincf7da102020-05-20 09:03:40 +020048 # transpose convolutions
49 | self.transpose_convolution_ops
Tim Hall79d07d22020-04-27 18:20:16 +010050 # pooling
51 | self.pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020052 # resizing/upscaling
53 | self.resizing_ops
Tim Hall79d07d22020-04-27 18:20:16 +010054 # FC layers
55 | self.fc_vector_products
56 # RNN/LSTM/GRU
Fredrik Svedberga0c36242020-06-03 15:43:31 +020057 | set(("BlockLSTM",))
Tim Hall79d07d22020-04-27 18:20:16 +010058 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020059 self.unary_elem_wise_main_ops = set(("LeakyRelu", "Abs", "CLZ",))
60 self.binary_elem_wise_min_max_ops = set(("Minimum", "Maximum",))
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +020061 self.binary_elem_wise_shift_ops = set(("SHL", "SHR",))
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020062 self.binary_elem_wise_add_mul_sub = set(
Fredrik Svedberg1575b942020-08-18 13:19:18 +020063 ("AddAct", "MulAct", "SubAct", "QuantizedAdd", "QuantizedSub", "QuantizedMul", "Mul", "Add", "Sub",)
Tim Hall79d07d22020-04-27 18:20:16 +010064 )
Fredrik Svedberg1575b942020-08-18 13:19:18 +020065 self.binary_elem_wise_main_ops = (
66 self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub | self.binary_elem_wise_shift_ops
67 )
Dwight Lidmanf995db72020-04-27 11:15:12 +020068 self.elem_wise_main_ops = self.binary_elem_wise_main_ops | self.unary_elem_wise_main_ops
Tim Hall79d07d22020-04-27 18:20:16 +010069 self.activation_ops = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020070 (
71 "QuantizedRelu",
72 "QuantizedRelu1",
73 "QuantizedRelu6",
74 "Relu",
75 "Relu6",
76 "ReluN1To1",
77 "Sigmoid",
78 "Tanh",
79 "Softmax",
80 )
Tim Hall79d07d22020-04-27 18:20:16 +010081 )
82 self.npu_post_ops = (
83 # activation functions
84 self.activation_ops
85 # concatenation write direction
Fredrik Svedberga0c36242020-06-03 15:43:31 +020086 | set(("ConcatSliceWrite",))
Tim Hall79d07d22020-04-27 18:20:16 +010087 # bias add and batch norm
Fredrik Svedberga0c36242020-06-03 15:43:31 +020088 | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm",))
Jacob Bohlin9fbc4912020-06-29 11:58:50 +020089 # Quantization
90 | set(("Quantize",))
Tim Hall79d07d22020-04-27 18:20:16 +010091 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020092 self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack",))
93 self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack",))
Tim Hall79d07d22020-04-27 18:20:16 +010094 self.memory_only_ops = (
Fredrik Svedberga0c36242020-06-03 15:43:31 +020095 set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims",)) | self.concat_ops | self.split_ops
Tim Hall79d07d22020-04-27 18:20:16 +010096 )
Dwight Lidman7579c752020-08-24 16:05:47 +020097 self.shapeless_input_ops = self.binary_elem_wise_main_ops | set(("Split", "SplitV",))
Fredrik Svedberga0c36242020-06-03 15:43:31 +020098 self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid", "LUT",))
Tim Hall79d07d22020-04-27 18:20:16 +010099 self.supported_operators = (
100 self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops
101 )
102 # Setup supported operator restriction checkers
103 self.supported_operator_restrictions = {}
104 self.supported_operator_restrictions.update(
105 {op: self.check_convolution_restrictions for op in self.convolution_ops}
106 )
107 self.supported_operator_restrictions.update(
108 {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops}
109 )
Jacob Bohlincf7da102020-05-20 09:03:40 +0200110 self.supported_operator_restrictions.update(
111 {op: self.check_transpose_convolution_restrictions for op in self.transpose_convolution_ops}
112 )
Tim Hall79d07d22020-04-27 18:20:16 +0100113 self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops})
Dwight Lidman42fed942020-05-29 09:37:03 +0200114 self.supported_operator_restrictions.update({op: self.check_resize_restrictions for op in self.resizing_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100115 self.supported_operator_restrictions.update(
116 {op: self.check_vector_product_restrictions for op in self.fc_vector_products}
117 )
118 self.supported_operator_restrictions.update(
119 {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops}
120 )
121 self.supported_operator_restrictions.update(
122 {op: self.check_memory_only_restrictions for op in self.memory_only_ops}
123 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200124 self.supported_operator_restrictions.update({op: self.check_activation_ops for op in self.activation_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100125
126 def is_operator_supported(self, op):
127 if op.type not in self.supported_operators:
128 return False
129 if not self.check_generic_restrictions(op):
130 return False
131 if op.type in self.supported_operator_restrictions:
132 return self.supported_operator_restrictions[op.type](op)
133 return True
134
135 def check_generic_restrictions(self, op):
136 # 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
Dwight Lidman7579c752020-08-24 16:05:47 +0200143 if t.shape == [] and op.type not in self.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",))
183 | self.binary_elem_wise_add_mul_sub
184 | self.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
195 and op.attrs["fused_activation_function"] not in self.supported_fused_activations
196 ):
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
212 def check_convolution_restrictions(self, op):
213 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200214 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100215 return False
216
217 # check dilation
218 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
219 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
220 if dilation_w_factor > 2 or dilation_h_factor > 2:
221 return False
222
223 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200224 ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100225 if weight_tensor.element_size() > 1:
226 return False
227
Jacob Bohlin49d92122020-08-19 14:36:46 +0200228 if not self.check_bias_restrictions(bias_tensor):
229 return False
230
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200231 # check kernel size [HWIO]
232 dilated_weight_w = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_w_factor - 1)
233 dilated_weight_h = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_h_factor - 1)
234
235 if dilated_weight_w > 64 or dilated_weight_h > 64:
236 return False
237
Andreas Nevalainen8854dc92020-09-24 13:43:00 +0200238 # check non const weights
239 if weight_tensor.values is None:
240 print("Warning:", op.type, "has non-const weights, placing on CPU")
241 return False
242
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200243 # check weight sums over [HWI]
244 zero_point = weight_tensor.quantization.zero_point
245 quant_weights = weight_tensor.quant_values.astype(np.int64)
246 weights = quant_weights - zero_point
247 totals = np.sum(np.absolute(weights), axis=(0, 1, 2))
248
249 if np.amax(totals) > 127 * 65536:
Tim Hall79d07d22020-04-27 18:20:16 +0100250 return False
251
252 # check batch size
253 if ifm_tensor.shape[0] != 1:
254 return False
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200255
Tim Hall79d07d22020-04-27 18:20:16 +0100256 return True
257
258 def check_depthwise_convolution_restrictions(self, op):
259 # check depth
260 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
261 if op.attrs["depth_multiplier"] > 1 and not (
262 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
263 ):
264 return False
265 return self.check_convolution_restrictions(op)
266
Jacob Bohlincf7da102020-05-20 09:03:40 +0200267 def check_transpose_convolution_restrictions(self, op):
268 # check stride
269 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
270 if stride_h != stride_w != 2:
271 return False
272
273 # check output dimensions
274 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
275 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
276 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
277 if op.attrs["padding"] == b"SAME":
278 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
279 return False
280 elif op.attrs["padding"] == b"VALID":
281 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100282 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
283 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
284 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200285 return False
286
287 return self.check_convolution_restrictions(op)
288
Tim Hall79d07d22020-04-27 18:20:16 +0100289 def check_pooling_restrictions(self, op):
290 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200291 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100292 return False
293
294 # check data type
295 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
296 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200297 if op.type != "ReduceSum":
298 return False
299 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100300
301 # check batch size
302 if ifm_tensor.shape[0] != 1:
303 return False
304
305 if op.type in self.avg_pooling_ops:
306 # check kernel size
307 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
308 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100309 if op.attrs["padding"] == b"VALID" and (
310 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
311 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100312 return False
313
314 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200315 # check kernel size (any padding)
316 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100317 return False
318 return True
319
Dwight Lidman42fed942020-05-29 09:37:03 +0200320 def check_resize_restrictions(self, op):
321 # check unsupported upscaling factor
322 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200323 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
324 return True
Charles Xu36ffaf32020-08-05 15:40:44 +0200325 if op.inputs[0].shape == op.outputs[0].shape:
326 return True
Charles Xu87c13502020-08-06 12:17:26 +0200327 upscaled_shape = np.array(op.inputs[0].shape[1:3])
328 out_shape = np.array(op.outputs[0].shape[1:3])
329 while (upscaled_shape < out_shape).all():
330 upscaled_shape *= 2
331 if op.attrs["align_corners"]:
332 upscaled_shape -= 1
333 if np.array_equal(out_shape, upscaled_shape):
334 return True
335 return False
Dwight Lidman42fed942020-05-29 09:37:03 +0200336
Tim Hall79d07d22020-04-27 18:20:16 +0100337 def check_vector_product_restrictions(self, op):
338 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200339 _, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100340 if weight_tensor.element_size() > 1:
341 return False
342
Jacob Bohlin49d92122020-08-19 14:36:46 +0200343 if not self.check_bias_restrictions(bias_tensor):
344 return False
345
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200346 # check non const weights
347 if weight_tensor.values is None:
348 print("Warning:", op.type, "has non-const weights, placing on CPU")
349 return False
350
Tim Hall79d07d22020-04-27 18:20:16 +0100351 return True
352
353 def check_element_wise_restrictions(self, op):
354 # check data type
355 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200356 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100357 if (
358 op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops
359 and ifm_tensor.dtype != ofm_tensor.dtype
360 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100361 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100362 if op.type in self.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200363 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100364 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200365 return False
366 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100367 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200368 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100369 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200370 return False
371 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100372 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200373 return False
374 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100375 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
376 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
377 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200378 return False
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200379 elif op.type in self.binary_elem_wise_shift_ops | set(("CLZ")):
380 if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32:
381 return False
382 if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32:
383 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100384
385 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200386 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100387 return False
388 if op.type in self.binary_elem_wise_main_ops: # if op type is unary, ifm2_tensor is None
Dwight Lidmanf995db72020-04-27 11:15:12 +0200389 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
390 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200391
392 # negative alpha values are not supported
393 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
394 return False
395
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200396 # check if ifm or ifm2 has ofm shape
397 if ifm_tensor.shape != ofm_tensor.shape and ifm2_tensor.shape != ofm_tensor.shape:
398 return False
399
Patrik Gustavsson530992a2020-09-30 13:26:59 +0200400 if op.type in self.binary_elem_wise_min_max_ops and not self.check_quantization_restrictions_binary_elem_wise(
401 op
402 ):
403 return False
404
Tim Hall79d07d22020-04-27 18:20:16 +0100405 return True
406
407 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100408 if op.type == "StridedSlice":
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200409 if len(op.inputs) != 4:
410 warn_cpu(op, "has {} input tensors, only 4 inputs are supported".format(len(op.inputs)))
Tim Hall79d07d22020-04-27 18:20:16 +0100411 return False
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200412 input_tens, begin_tens, end_tens, strides_tens = op.inputs
413 if begin_tens.values is None or end_tens.values is None or strides_tens.values is None:
414 warn_cpu(op, "has a non-constant begin, end, or stride input tensor, which is not supported")
415 return False
416 if not (
417 len(input_tens.shape)
418 == len(op.outputs[0].shape)
419 == len(begin_tens.values)
420 == len(end_tens.values)
421 == len(strides_tens.values)
422 ):
423 warn_cpu(op, "has input tensors with shapes that are not supported")
424 return False
425 # check stride size
426 if any(stride != 1 for stride in strides_tens.values):
427 warn_cpu(op, "has stride values {}, only stride 1 values are supported".format(strides_tens.values))
Michael McGeaghecd20522020-07-31 16:59:45 +0100428 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200429 # check ellipsis_mask
430 if op.attrs["ellipsis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200431 warn_cpu(op, "ellipsis_mask is {}, only 0 is supported".format(op.attrs["ellipsis_mask"]))
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200432 return False
433 # check if both new_axis_mask and shrink_axis_mask have bit set
434 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200435 warn_cpu(op, "new_axis_mask and shrink_axis_mask are both non-zero, which is not supported")
436 return False
437 # Calculate offset start/end
438 offset_start = get_slice_offsets(input_tens.shape, begin_tens, op.attrs["begin_mask"], is_begin=True)
439 offset_end = get_slice_offsets(input_tens.shape, end_tens, op.attrs["end_mask"], is_begin=False)
440 # check "end - begin" doesn't result in any zero or negative elements
441 if any((end - begin) <= 0 for begin, end in zip(offset_start, offset_end)):
442 warn_cpu(
443 op,
444 "has slice begin values {}, some of which are >= end values {}, which is illegal".format(
445 begin_tens.values, end_tens.values
446 ),
447 )
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200448 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200449 if op.type == "SplitV":
450 # check that maximum one size is set to -1, indicating that size should be inferred
451 sizes = op.inputs[1].values
452 num_to_be_inferred = 0
453 for size in sizes:
454 if size == -1:
455 num_to_be_inferred += 1
456
457 if num_to_be_inferred > 1:
458 print("Warning:", op.type, "has more than one size to be inferred, which is illegal, placing on CPU")
459 return False
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200460 if op.type.find("Concat") != -1:
461 axis = op.attrs.get("axis", None)
462 if axis is None:
463 print("Warning:", op.type, "invalid or missing axis, placing on CPU")
464 return False
465 if axis < 0:
466 axis += len(op.inputs[0].shape)
467 if not 0 < axis < len(op.inputs[0].shape):
468 print("Warning:", op.type, "invalid axis", axis, ", placing on CPU")
469 return False
470 ofm = op.outputs[0]
471 ofm_dims = len(ofm.shape)
472 for ifm in op.inputs:
473 if len(ifm.shape) != ofm_dims:
474 return False
475 for i in range(ofm_dims):
476 if i != axis and ifm.shape[i] != ofm.shape[i]:
Patrik Gustavsson530992a2020-09-30 13:26:59 +0200477 print(
478 "Warning:",
479 op.type,
480 "invalid ifm:",
481 ifm.name,
482 ifm.shape,
483 "mismatch in dimension",
484 i,
485 ", placing on CPU",
486 )
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200487 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200488
Tim Hall79d07d22020-04-27 18:20:16 +0100489 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200490
Tim Halle3786ac2020-07-28 17:40:50 +0100491 def check_quantization_restrictions_binary_elem_wise(self, op):
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200492 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Tim Halle3786ac2020-07-28 17:40:50 +0100493 assert len(op.inputs) >= 2 and len(op.outputs) == 1
494
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200495 if (
Tim Halle3786ac2020-07-28 17:40:50 +0100496 op.inputs[0].quantization is None
Michael McGeagh34ad19b2020-09-04 15:44:23 +0100497 or not op.inputs[0].is_scaling_equal(op.inputs[1])
498 or not op.inputs[0].is_scaling_equal(op.outputs[0])
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200499 ):
500 print(
501 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
502 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200503 return False
Tim Halle3786ac2020-07-28 17:40:50 +0100504
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200505 return True
506
507 def check_activation_ops(self, op):
508 if op.type == "Softmax":
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200509 ifm_tensor = op.inputs[0]
510 ofm_tensor = op.outputs[0]
511
512 # check data type
513 if ifm_tensor.dtype != ofm_tensor.dtype:
514 return False
515
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200516 if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16):
517 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200518
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200519 # check shape
520 if len(ifm_tensor.shape) > 4 or ifm_tensor.shape != ofm_tensor.shape:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200521 return False
522
523 return True
Jacob Bohlin49d92122020-08-19 14:36:46 +0200524
525 def check_bias_restrictions(self, bias_tensor):
526 # check data type
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200527 if bias_tensor is not None and bias_tensor.dtype not in (DataType.int32, DataType.int64):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200528 return False
529
530 # check if values fits in 40-bit
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200531 if bias_tensor is not None and bias_tensor.dtype == DataType.int64:
Tim Hall71525172020-08-29 15:09:57 +0100532 for quant_value in bias_tensor.quant_values:
533 if not (-(1 << 39) <= quant_value < (1 << 39)):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200534 return False
535
536 return True