blob: 9e9da8c6eba82eda7b2c5d1b88875125d44b5aa4 [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 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200124 self.supported_operator_restrictions.update(
Tim Halle3786ac2020-07-28 17:40:50 +0100125 {op: self.check_quantization_restrictions_binary_elem_wise for op in self.binary_elem_wise_min_max_ops}
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200126 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200127 self.supported_operator_restrictions.update({op: self.check_activation_ops for op in self.activation_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100128
129 def is_operator_supported(self, op):
130 if op.type not in self.supported_operators:
131 return False
132 if not self.check_generic_restrictions(op):
133 return False
134 if op.type in self.supported_operator_restrictions:
135 return self.supported_operator_restrictions[op.type](op)
136 return True
137
138 def check_generic_restrictions(self, op):
139 # check fully defined shapes
Dwight Lidman25733112020-08-17 11:56:10 +0200140 for t in op.inputs:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200141 if not t:
142 continue
Tim Hall79d07d22020-04-27 18:20:16 +0100143 if not t.has_fully_defined_shape():
Dwight Lidman25733112020-08-17 11:56:10 +0200144 print("Warning:", op.type, "has input(s) of undefined shape, placing on CPU")
145 return False
Dwight Lidman7579c752020-08-24 16:05:47 +0200146 if t.shape == [] and op.type not in self.shapeless_input_ops:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200147 print(
148 "Warning:",
149 op.type,
150 "has input(s) of shape [].",
151 "Scalar input or broadcasting is not supported for this operator,",
152 "placing on CPU",
153 )
Dwight Lidman25733112020-08-17 11:56:10 +0200154 return False
155 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
168
169 # check data type
170 tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None]
171 if not tensors:
172 tensors = op.inputs
173 for t in tensors:
174 if not (t.dtype.type & BaseType.Int):
175 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200176 if (
177 t.element_size() > 2
Fredrik Svedberg1575b942020-08-18 13:19:18 +0200178 and op.type
179 not in set(("Requantize", "ReduceSum", "CLZ",))
180 | self.binary_elem_wise_add_mul_sub
181 | self.binary_elem_wise_shift_ops
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200182 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100183 return False
184 # check size
185 if any(dim > 65536 for dim in t.shape):
186 return False
187
188 # check fused activations
189 if (
190 "fused_activation_function" in op.attrs
191 and op.attrs["fused_activation_function"] is not None
192 and op.attrs["fused_activation_function"] not in self.supported_fused_activations
193 ):
194 return False
195 return True
196
197 def check_convolution_restrictions(self, op):
198 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200199 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100200 return False
201
202 # check dilation
203 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
204 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
205 if dilation_w_factor > 2 or dilation_h_factor > 2:
206 return False
207
208 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200209 ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100210 if weight_tensor.element_size() > 1:
211 return False
212
Jacob Bohlin49d92122020-08-19 14:36:46 +0200213 if not self.check_bias_restrictions(bias_tensor):
214 return False
215
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200216 # check kernel size [HWIO]
217 dilated_weight_w = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_w_factor - 1)
218 dilated_weight_h = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_h_factor - 1)
219
220 if dilated_weight_w > 64 or dilated_weight_h > 64:
221 return False
222
Andreas Nevalainen8854dc92020-09-24 13:43:00 +0200223 # check non const weights
224 if weight_tensor.values is None:
225 print("Warning:", op.type, "has non-const weights, placing on CPU")
226 return False
227
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200228 # check weight sums over [HWI]
229 zero_point = weight_tensor.quantization.zero_point
230 quant_weights = weight_tensor.quant_values.astype(np.int64)
231 weights = quant_weights - zero_point
232 totals = np.sum(np.absolute(weights), axis=(0, 1, 2))
233
234 if np.amax(totals) > 127 * 65536:
Tim Hall79d07d22020-04-27 18:20:16 +0100235 return False
236
237 # check batch size
238 if ifm_tensor.shape[0] != 1:
239 return False
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200240
Tim Hall79d07d22020-04-27 18:20:16 +0100241 return True
242
243 def check_depthwise_convolution_restrictions(self, op):
244 # check depth
245 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
246 if op.attrs["depth_multiplier"] > 1 and not (
247 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
248 ):
249 return False
250 return self.check_convolution_restrictions(op)
251
Jacob Bohlincf7da102020-05-20 09:03:40 +0200252 def check_transpose_convolution_restrictions(self, op):
253 # check stride
254 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
255 if stride_h != stride_w != 2:
256 return False
257
258 # check output dimensions
259 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
260 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
261 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
262 if op.attrs["padding"] == b"SAME":
263 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
264 return False
265 elif op.attrs["padding"] == b"VALID":
266 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100267 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
268 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
269 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200270 return False
271
272 return self.check_convolution_restrictions(op)
273
Tim Hall79d07d22020-04-27 18:20:16 +0100274 def check_pooling_restrictions(self, op):
275 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200276 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100277 return False
278
279 # check data type
280 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
281 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200282 if op.type != "ReduceSum":
283 return False
284 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100285
286 # check batch size
287 if ifm_tensor.shape[0] != 1:
288 return False
289
290 if op.type in self.avg_pooling_ops:
291 # check kernel size
292 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
293 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100294 if op.attrs["padding"] == b"VALID" and (
295 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
296 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100297 return False
298
299 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200300 # check kernel size (any padding)
301 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100302 return False
303 return True
304
Dwight Lidman42fed942020-05-29 09:37:03 +0200305 def check_resize_restrictions(self, op):
306 # check unsupported upscaling factor
307 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200308 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
309 return True
Charles Xu36ffaf32020-08-05 15:40:44 +0200310 if op.inputs[0].shape == op.outputs[0].shape:
311 return True
Charles Xu87c13502020-08-06 12:17:26 +0200312 upscaled_shape = np.array(op.inputs[0].shape[1:3])
313 out_shape = np.array(op.outputs[0].shape[1:3])
314 while (upscaled_shape < out_shape).all():
315 upscaled_shape *= 2
316 if op.attrs["align_corners"]:
317 upscaled_shape -= 1
318 if np.array_equal(out_shape, upscaled_shape):
319 return True
320 return False
Dwight Lidman42fed942020-05-29 09:37:03 +0200321
Tim Hall79d07d22020-04-27 18:20:16 +0100322 def check_vector_product_restrictions(self, op):
323 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200324 _, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100325 if weight_tensor.element_size() > 1:
326 return False
327
Jacob Bohlin49d92122020-08-19 14:36:46 +0200328 if not self.check_bias_restrictions(bias_tensor):
329 return False
330
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200331 # check non const weights
332 if weight_tensor.values is None:
333 print("Warning:", op.type, "has non-const weights, placing on CPU")
334 return False
335
Tim Hall79d07d22020-04-27 18:20:16 +0100336 return True
337
338 def check_element_wise_restrictions(self, op):
339 # check data type
340 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200341 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100342 if (
343 op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops
344 and ifm_tensor.dtype != ofm_tensor.dtype
345 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100346 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100347 if op.type in self.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200348 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100349 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200350 return False
351 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100352 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200353 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100354 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200355 return False
356 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100357 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200358 return False
359 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100360 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
361 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
362 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200363 return False
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200364 elif op.type in self.binary_elem_wise_shift_ops | set(("CLZ")):
365 if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32:
366 return False
367 if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32:
368 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100369
370 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200371 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100372 return False
373 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 +0200374 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
375 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200376
377 # negative alpha values are not supported
378 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
379 return False
380
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200381 # check if ifm or ifm2 has ofm shape
382 if ifm_tensor.shape != ofm_tensor.shape and ifm2_tensor.shape != ofm_tensor.shape:
383 return False
384
Tim Hall79d07d22020-04-27 18:20:16 +0100385 return True
386
387 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100388 if op.type == "StridedSlice":
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200389 if len(op.inputs) != 4:
390 warn_cpu(op, "has {} input tensors, only 4 inputs are supported".format(len(op.inputs)))
Tim Hall79d07d22020-04-27 18:20:16 +0100391 return False
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200392 input_tens, begin_tens, end_tens, strides_tens = op.inputs
393 if begin_tens.values is None or end_tens.values is None or strides_tens.values is None:
394 warn_cpu(op, "has a non-constant begin, end, or stride input tensor, which is not supported")
395 return False
396 if not (
397 len(input_tens.shape)
398 == len(op.outputs[0].shape)
399 == len(begin_tens.values)
400 == len(end_tens.values)
401 == len(strides_tens.values)
402 ):
403 warn_cpu(op, "has input tensors with shapes that are not supported")
404 return False
405 # check stride size
406 if any(stride != 1 for stride in strides_tens.values):
407 warn_cpu(op, "has stride values {}, only stride 1 values are supported".format(strides_tens.values))
Michael McGeaghecd20522020-07-31 16:59:45 +0100408 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200409 # check ellipsis_mask
410 if op.attrs["ellipsis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200411 warn_cpu(op, "ellipsis_mask is {}, only 0 is supported".format(op.attrs["ellipsis_mask"]))
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200412 return False
413 # check if both new_axis_mask and shrink_axis_mask have bit set
414 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200415 warn_cpu(op, "new_axis_mask and shrink_axis_mask are both non-zero, which is not supported")
416 return False
417 # Calculate offset start/end
418 offset_start = get_slice_offsets(input_tens.shape, begin_tens, op.attrs["begin_mask"], is_begin=True)
419 offset_end = get_slice_offsets(input_tens.shape, end_tens, op.attrs["end_mask"], is_begin=False)
420 # check "end - begin" doesn't result in any zero or negative elements
421 if any((end - begin) <= 0 for begin, end in zip(offset_start, offset_end)):
422 warn_cpu(
423 op,
424 "has slice begin values {}, some of which are >= end values {}, which is illegal".format(
425 begin_tens.values, end_tens.values
426 ),
427 )
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200428 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200429 if op.type == "SplitV":
430 # check that maximum one size is set to -1, indicating that size should be inferred
431 sizes = op.inputs[1].values
432 num_to_be_inferred = 0
433 for size in sizes:
434 if size == -1:
435 num_to_be_inferred += 1
436
437 if num_to_be_inferred > 1:
438 print("Warning:", op.type, "has more than one size to be inferred, which is illegal, placing on CPU")
439 return False
440
Tim Hall79d07d22020-04-27 18:20:16 +0100441 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200442
Tim Halle3786ac2020-07-28 17:40:50 +0100443 def check_quantization_restrictions_binary_elem_wise(self, op):
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200444 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Tim Halle3786ac2020-07-28 17:40:50 +0100445 assert len(op.inputs) >= 2 and len(op.outputs) == 1
446
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200447 if (
Tim Halle3786ac2020-07-28 17:40:50 +0100448 op.inputs[0].quantization is None
Michael McGeagh34ad19b2020-09-04 15:44:23 +0100449 or not op.inputs[0].is_scaling_equal(op.inputs[1])
450 or not op.inputs[0].is_scaling_equal(op.outputs[0])
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200451 ):
452 print(
453 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
454 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200455 return False
Tim Halle3786ac2020-07-28 17:40:50 +0100456
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200457 return True
458
459 def check_activation_ops(self, op):
460 if op.type == "Softmax":
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200461 ifm_tensor = op.inputs[0]
462 ofm_tensor = op.outputs[0]
463
464 # check data type
465 if ifm_tensor.dtype != ofm_tensor.dtype:
466 return False
467
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200468 if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16):
469 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200470
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200471 # check shape
472 if len(ifm_tensor.shape) > 4 or ifm_tensor.shape != ofm_tensor.shape:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200473 return False
474
475 return True
Jacob Bohlin49d92122020-08-19 14:36:46 +0200476
477 def check_bias_restrictions(self, bias_tensor):
478 # check data type
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200479 if bias_tensor is not None and bias_tensor.dtype not in (DataType.int32, DataType.int64):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200480 return False
481
482 # check if values fits in 40-bit
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200483 if bias_tensor is not None and bias_tensor.dtype == DataType.int64:
Tim Hall71525172020-08-29 15:09:57 +0100484 for quant_value in bias_tensor.quant_values:
485 if not (-(1 << 39) <= quant_value < (1 << 39)):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200486 return False
487
488 return True