blob: 0a1af82988f0a12d8dcbb998ac6d4573430d3146 [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
Andreas Nevalaineneadb1662020-09-01 15:36:26 +0200195
196 # check inf values
197 for tens in op.get_ifm_ifm2_weights_ofm():
198 if (tens is not None) and (
199 tens.quantization is not None) and (
200 tens.quantization.scale_f32 is not None) and (
201 np.isinf(tens.quantization.scale_f32).any()):
202 print("Warning:", op.type, "has inf valued tensor(s), placing on CPU")
203 return False
204
Tim Hall79d07d22020-04-27 18:20:16 +0100205 return True
206
207 def check_convolution_restrictions(self, op):
208 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200209 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100210 return False
211
212 # check dilation
213 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
214 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
215 if dilation_w_factor > 2 or dilation_h_factor > 2:
216 return False
217
218 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200219 ifm_tensor, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100220 if weight_tensor.element_size() > 1:
221 return False
222
Jacob Bohlin49d92122020-08-19 14:36:46 +0200223 if not self.check_bias_restrictions(bias_tensor):
224 return False
225
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200226 # check kernel size [HWIO]
227 dilated_weight_w = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_w_factor - 1)
228 dilated_weight_h = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_h_factor - 1)
229
230 if dilated_weight_w > 64 or dilated_weight_h > 64:
231 return False
232
Andreas Nevalainen8854dc92020-09-24 13:43:00 +0200233 # check non const weights
234 if weight_tensor.values is None:
235 print("Warning:", op.type, "has non-const weights, placing on CPU")
236 return False
237
Andreas Nevalainenf0c59bf2020-08-26 10:56:23 +0200238 # check weight sums over [HWI]
239 zero_point = weight_tensor.quantization.zero_point
240 quant_weights = weight_tensor.quant_values.astype(np.int64)
241 weights = quant_weights - zero_point
242 totals = np.sum(np.absolute(weights), axis=(0, 1, 2))
243
244 if np.amax(totals) > 127 * 65536:
Tim Hall79d07d22020-04-27 18:20:16 +0100245 return False
246
247 # check batch size
248 if ifm_tensor.shape[0] != 1:
249 return False
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200250
Tim Hall79d07d22020-04-27 18:20:16 +0100251 return True
252
253 def check_depthwise_convolution_restrictions(self, op):
254 # check depth
255 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
256 if op.attrs["depth_multiplier"] > 1 and not (
257 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
258 ):
259 return False
260 return self.check_convolution_restrictions(op)
261
Jacob Bohlincf7da102020-05-20 09:03:40 +0200262 def check_transpose_convolution_restrictions(self, op):
263 # check stride
264 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
265 if stride_h != stride_w != 2:
266 return False
267
268 # check output dimensions
269 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
270 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
271 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
272 if op.attrs["padding"] == b"SAME":
273 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
274 return False
275 elif op.attrs["padding"] == b"VALID":
276 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100277 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
278 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
279 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200280 return False
281
282 return self.check_convolution_restrictions(op)
283
Tim Hall79d07d22020-04-27 18:20:16 +0100284 def check_pooling_restrictions(self, op):
285 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200286 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100287 return False
288
289 # check data type
290 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
291 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200292 if op.type != "ReduceSum":
293 return False
294 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100295
296 # check batch size
297 if ifm_tensor.shape[0] != 1:
298 return False
299
300 if op.type in self.avg_pooling_ops:
301 # check kernel size
302 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
303 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100304 if op.attrs["padding"] == b"VALID" and (
305 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
306 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100307 return False
308
309 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200310 # check kernel size (any padding)
311 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100312 return False
313 return True
314
Dwight Lidman42fed942020-05-29 09:37:03 +0200315 def check_resize_restrictions(self, op):
316 # check unsupported upscaling factor
317 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200318 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
319 return True
Charles Xu36ffaf32020-08-05 15:40:44 +0200320 if op.inputs[0].shape == op.outputs[0].shape:
321 return True
Charles Xu87c13502020-08-06 12:17:26 +0200322 upscaled_shape = np.array(op.inputs[0].shape[1:3])
323 out_shape = np.array(op.outputs[0].shape[1:3])
324 while (upscaled_shape < out_shape).all():
325 upscaled_shape *= 2
326 if op.attrs["align_corners"]:
327 upscaled_shape -= 1
328 if np.array_equal(out_shape, upscaled_shape):
329 return True
330 return False
Dwight Lidman42fed942020-05-29 09:37:03 +0200331
Tim Hall79d07d22020-04-27 18:20:16 +0100332 def check_vector_product_restrictions(self, op):
333 # check data type
Jacob Bohlin49d92122020-08-19 14:36:46 +0200334 _, _, weight_tensor, bias_tensor, _ = op.get_ifm_ifm2_weights_biases_ofm()
Tim Hall79d07d22020-04-27 18:20:16 +0100335 if weight_tensor.element_size() > 1:
336 return False
337
Jacob Bohlin49d92122020-08-19 14:36:46 +0200338 if not self.check_bias_restrictions(bias_tensor):
339 return False
340
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200341 # check non const weights
342 if weight_tensor.values is None:
343 print("Warning:", op.type, "has non-const weights, placing on CPU")
344 return False
345
Tim Hall79d07d22020-04-27 18:20:16 +0100346 return True
347
348 def check_element_wise_restrictions(self, op):
349 # check data type
350 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200351 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100352 if (
353 op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops
354 and ifm_tensor.dtype != ofm_tensor.dtype
355 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100356 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100357 if op.type in self.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200358 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100359 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200360 return False
361 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100362 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200363 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100364 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200365 return False
366 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100367 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200368 return False
369 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100370 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
371 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
372 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200373 return False
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200374 elif op.type in self.binary_elem_wise_shift_ops | set(("CLZ")):
375 if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32:
376 return False
377 if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32:
378 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100379
380 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200381 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100382 return False
383 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 +0200384 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
385 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200386
387 # negative alpha values are not supported
388 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
389 return False
390
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200391 # check if ifm or ifm2 has ofm shape
392 if ifm_tensor.shape != ofm_tensor.shape and ifm2_tensor.shape != ofm_tensor.shape:
393 return False
394
Tim Hall79d07d22020-04-27 18:20:16 +0100395 return True
396
397 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100398 if op.type == "StridedSlice":
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200399 if len(op.inputs) != 4:
400 warn_cpu(op, "has {} input tensors, only 4 inputs are supported".format(len(op.inputs)))
Tim Hall79d07d22020-04-27 18:20:16 +0100401 return False
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200402 input_tens, begin_tens, end_tens, strides_tens = op.inputs
403 if begin_tens.values is None or end_tens.values is None or strides_tens.values is None:
404 warn_cpu(op, "has a non-constant begin, end, or stride input tensor, which is not supported")
405 return False
406 if not (
407 len(input_tens.shape)
408 == len(op.outputs[0].shape)
409 == len(begin_tens.values)
410 == len(end_tens.values)
411 == len(strides_tens.values)
412 ):
413 warn_cpu(op, "has input tensors with shapes that are not supported")
414 return False
415 # check stride size
416 if any(stride != 1 for stride in strides_tens.values):
417 warn_cpu(op, "has stride values {}, only stride 1 values are supported".format(strides_tens.values))
Michael McGeaghecd20522020-07-31 16:59:45 +0100418 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200419 # check ellipsis_mask
420 if op.attrs["ellipsis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200421 warn_cpu(op, "ellipsis_mask is {}, only 0 is supported".format(op.attrs["ellipsis_mask"]))
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200422 return False
423 # check if both new_axis_mask and shrink_axis_mask have bit set
424 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200425 warn_cpu(op, "new_axis_mask and shrink_axis_mask are both non-zero, which is not supported")
426 return False
427 # Calculate offset start/end
428 offset_start = get_slice_offsets(input_tens.shape, begin_tens, op.attrs["begin_mask"], is_begin=True)
429 offset_end = get_slice_offsets(input_tens.shape, end_tens, op.attrs["end_mask"], is_begin=False)
430 # check "end - begin" doesn't result in any zero or negative elements
431 if any((end - begin) <= 0 for begin, end in zip(offset_start, offset_end)):
432 warn_cpu(
433 op,
434 "has slice begin values {}, some of which are >= end values {}, which is illegal".format(
435 begin_tens.values, end_tens.values
436 ),
437 )
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200438 return False
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200439 if op.type == "SplitV":
440 # check that maximum one size is set to -1, indicating that size should be inferred
441 sizes = op.inputs[1].values
442 num_to_be_inferred = 0
443 for size in sizes:
444 if size == -1:
445 num_to_be_inferred += 1
446
447 if num_to_be_inferred > 1:
448 print("Warning:", op.type, "has more than one size to be inferred, which is illegal, placing on CPU")
449 return False
450
Tim Hall79d07d22020-04-27 18:20:16 +0100451 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200452
Tim Halle3786ac2020-07-28 17:40:50 +0100453 def check_quantization_restrictions_binary_elem_wise(self, op):
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200454 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Tim Halle3786ac2020-07-28 17:40:50 +0100455 assert len(op.inputs) >= 2 and len(op.outputs) == 1
456
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200457 if (
Tim Halle3786ac2020-07-28 17:40:50 +0100458 op.inputs[0].quantization is None
Michael McGeagh34ad19b2020-09-04 15:44:23 +0100459 or not op.inputs[0].is_scaling_equal(op.inputs[1])
460 or not op.inputs[0].is_scaling_equal(op.outputs[0])
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200461 ):
462 print(
463 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
464 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200465 return False
Tim Halle3786ac2020-07-28 17:40:50 +0100466
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200467 return True
468
469 def check_activation_ops(self, op):
470 if op.type == "Softmax":
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200471 ifm_tensor = op.inputs[0]
472 ofm_tensor = op.outputs[0]
473
474 # check data type
475 if ifm_tensor.dtype != ofm_tensor.dtype:
476 return False
477
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200478 if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16):
479 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200480
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200481 # check shape
482 if len(ifm_tensor.shape) > 4 or ifm_tensor.shape != ofm_tensor.shape:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200483 return False
484
485 return True
Jacob Bohlin49d92122020-08-19 14:36:46 +0200486
487 def check_bias_restrictions(self, bias_tensor):
488 # check data type
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200489 if bias_tensor is not None and bias_tensor.dtype not in (DataType.int32, DataType.int64):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200490 return False
491
492 # check if values fits in 40-bit
Jacob Bohlin258ebba2020-08-31 10:44:35 +0200493 if bias_tensor is not None and bias_tensor.dtype == DataType.int64:
Tim Hall71525172020-08-29 15:09:57 +0100494 for quant_value in bias_tensor.quant_values:
495 if not (-(1 << 39) <= quant_value < (1 << 39)):
Jacob Bohlin49d92122020-08-19 14:36:46 +0200496 return False
497
498 return True