blob: fdf0c6b382a4b4c578c567114e2d3cc7be3049d1 [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.
Tim Hallc30f4952020-06-15 20:47:35 +010018from .data_type import BaseType
19from .data_type import DataType
Tim Hall79d07d22020-04-27 18:20:16 +010020
21
22class SupportedOperators:
Fredrik Svedberga0c36242020-06-03 15:43:31 +020023 def __init__(self, softmax_support):
24 self.softmax_support = softmax_support
Tim Hall79d07d22020-04-27 18:20:16 +010025 # Categorised lists of supported operators
Fredrik Svedberga0c36242020-06-03 15:43:31 +020026 self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead",))
27 self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D",))
Tim Hall79d07d22020-04-27 18:20:16 +010028 self.depthwise_convolution_ops = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020029 ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D,")
Tim Hall79d07d22020-04-27 18:20:16 +010030 )
Jacob Bohlincf7da102020-05-20 09:03:40 +020031 self.transpose_convolution_ops = set(("Conv2DBackpropInput",))
Fredrik Svedberga0c36242020-06-03 15:43:31 +020032 self.max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct",))
33 self.avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct",))
34 self.pooling_ops = set(("ReduceSum",)) | self.max_pooling_ops | self.avg_pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020035 self.resizing_ops = set(("ResizeBilinear",))
Fredrik Svedberga0c36242020-06-03 15:43:31 +020036 self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct",))
Tim Hall79d07d22020-04-27 18:20:16 +010037 self.mac_main_ops = (
38 # convolutions
39 self.convolution_ops
40 # depth-wise convolutions
41 | self.depthwise_convolution_ops
Jacob Bohlincf7da102020-05-20 09:03:40 +020042 # transpose convolutions
43 | self.transpose_convolution_ops
Tim Hall79d07d22020-04-27 18:20:16 +010044 # pooling
45 | self.pooling_ops
Dwight Lidman42fed942020-05-29 09:37:03 +020046 # resizing/upscaling
47 | self.resizing_ops
Tim Hall79d07d22020-04-27 18:20:16 +010048 # FC layers
49 | self.fc_vector_products
50 # RNN/LSTM/GRU
Fredrik Svedberga0c36242020-06-03 15:43:31 +020051 | set(("BlockLSTM",))
Tim Hall79d07d22020-04-27 18:20:16 +010052 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020053 self.unary_elem_wise_main_ops = set(("LeakyRelu", "Abs", "CLZ",))
54 self.binary_elem_wise_min_max_ops = set(("Minimum", "Maximum",))
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +020055 self.binary_elem_wise_shift_ops = set(("SHL", "SHR",))
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020056 self.binary_elem_wise_add_mul_sub = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020057 (
58 "AddAct",
59 "MulAct",
60 "SubAct",
61 "QuantizedAdd",
62 "QuantizedSub",
63 "QuantizedMul",
64 "Mul",
65 "Add",
66 "Sub",
Fredrik Svedberga0c36242020-06-03 15:43:31 +020067 )
Tim Hall79d07d22020-04-27 18:20:16 +010068 )
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +020069 self.binary_elem_wise_main_ops = self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub | self.binary_elem_wise_shift_ops
Dwight Lidmanf995db72020-04-27 11:15:12 +020070 self.elem_wise_main_ops = self.binary_elem_wise_main_ops | self.unary_elem_wise_main_ops
Tim Hall79d07d22020-04-27 18:20:16 +010071 self.activation_ops = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020072 (
73 "QuantizedRelu",
74 "QuantizedRelu1",
75 "QuantizedRelu6",
76 "Relu",
77 "Relu6",
78 "ReluN1To1",
79 "Sigmoid",
80 "Tanh",
81 "Softmax",
82 )
Tim Hall79d07d22020-04-27 18:20:16 +010083 )
84 self.npu_post_ops = (
85 # activation functions
86 self.activation_ops
87 # concatenation write direction
Fredrik Svedberga0c36242020-06-03 15:43:31 +020088 | set(("ConcatSliceWrite",))
Tim Hall79d07d22020-04-27 18:20:16 +010089 # bias add and batch norm
Fredrik Svedberga0c36242020-06-03 15:43:31 +020090 | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm",))
Jacob Bohlin9fbc4912020-06-29 11:58:50 +020091 # Quantization
92 | set(("Quantize",))
Tim Hall79d07d22020-04-27 18:20:16 +010093 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020094 self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack",))
95 self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack",))
Tim Hall79d07d22020-04-27 18:20:16 +010096 self.memory_only_ops = (
Fredrik Svedberga0c36242020-06-03 15:43:31 +020097 set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims",)) | self.concat_ops | self.split_ops
Tim Hall79d07d22020-04-27 18:20:16 +010098 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020099 self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid", "LUT",))
Tim Hall79d07d22020-04-27 18:20:16 +0100100 self.supported_operators = (
101 self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops
102 )
103 # Setup supported operator restriction checkers
104 self.supported_operator_restrictions = {}
105 self.supported_operator_restrictions.update(
106 {op: self.check_convolution_restrictions for op in self.convolution_ops}
107 )
108 self.supported_operator_restrictions.update(
109 {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops}
110 )
Jacob Bohlincf7da102020-05-20 09:03:40 +0200111 self.supported_operator_restrictions.update(
112 {op: self.check_transpose_convolution_restrictions for op in self.transpose_convolution_ops}
113 )
Tim Hall79d07d22020-04-27 18:20:16 +0100114 self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops})
Dwight Lidman42fed942020-05-29 09:37:03 +0200115 self.supported_operator_restrictions.update({op: self.check_resize_restrictions for op in self.resizing_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100116 self.supported_operator_restrictions.update(
117 {op: self.check_vector_product_restrictions for op in self.fc_vector_products}
118 )
119 self.supported_operator_restrictions.update(
120 {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops}
121 )
122 self.supported_operator_restrictions.update(
123 {op: self.check_memory_only_restrictions for op in self.memory_only_ops}
124 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200125 self.supported_operator_restrictions.update(
126 {op: self.check_quantization_restrictions for op in self.binary_elem_wise_min_max_ops}
127 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200128 self.supported_operator_restrictions.update({op: self.check_activation_ops for op in self.activation_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100129
130 def is_operator_supported(self, op):
131 if op.type not in self.supported_operators:
132 return False
133 if not self.check_generic_restrictions(op):
134 return False
135 if op.type in self.supported_operator_restrictions:
136 return self.supported_operator_restrictions[op.type](op)
137 return True
138
139 def check_generic_restrictions(self, op):
140 # check fully defined shapes
141 for t in op.inputs + op.outputs:
142 if not t.has_fully_defined_shape():
143 print("Warning:", op, "has inputs/outputs of undefined shape, placing on CPU")
144 return False
145
146 # check data type
147 tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None]
148 if not tensors:
149 tensors = op.inputs
150 for t in tensors:
151 if not (t.dtype.type & BaseType.Int):
152 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200153 if (
154 t.element_size() > 2
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200155 and op.type not in set(("Requantize", "ReduceSum", "CLZ",)) | self.binary_elem_wise_add_mul_sub | self.binary_elem_wise_shift_ops
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200156 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100157 return False
158 # check size
159 if any(dim > 65536 for dim in t.shape):
160 return False
161
162 # check fused activations
163 if (
164 "fused_activation_function" in op.attrs
165 and op.attrs["fused_activation_function"] is not None
166 and op.attrs["fused_activation_function"] not in self.supported_fused_activations
167 ):
168 return False
169 return True
170
171 def check_convolution_restrictions(self, op):
172 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200173 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100174 return False
175
176 # check dilation
177 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
178 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
179 if dilation_w_factor > 2 or dilation_h_factor > 2:
180 return False
181
182 # check data type
183 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
184 if weight_tensor.element_size() > 1:
185 return False
186
187 # check kernel size
188 dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1)
189 dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1)
190 if (
191 dilated_weight_w > 64
192 or dilated_weight_h > 64
193 or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536
194 ):
195 return False
196
197 # check batch size
198 if ifm_tensor.shape[0] != 1:
199 return False
200 return True
201
202 def check_depthwise_convolution_restrictions(self, op):
203 # check depth
204 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
205 if op.attrs["depth_multiplier"] > 1 and not (
206 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
207 ):
208 return False
209 return self.check_convolution_restrictions(op)
210
Jacob Bohlincf7da102020-05-20 09:03:40 +0200211 def check_transpose_convolution_restrictions(self, op):
212 # check stride
213 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
214 if stride_h != stride_w != 2:
215 return False
216
217 # check output dimensions
218 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
219 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
220 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
221 if op.attrs["padding"] == b"SAME":
222 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
223 return False
224 elif op.attrs["padding"] == b"VALID":
225 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100226 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
227 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
228 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200229 return False
230
231 return self.check_convolution_restrictions(op)
232
Tim Hall79d07d22020-04-27 18:20:16 +0100233 def check_pooling_restrictions(self, op):
234 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200235 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100236 return False
237
238 # check data type
239 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
240 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200241 if op.type != "ReduceSum":
242 return False
243 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100244
245 # check batch size
246 if ifm_tensor.shape[0] != 1:
247 return False
248
249 if op.type in self.avg_pooling_ops:
250 # check kernel size
251 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
252 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100253 if op.attrs["padding"] == b"VALID" and (
254 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
255 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100256 return False
257
258 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200259 # check kernel size (any padding)
260 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100261 return False
262 return True
263
Dwight Lidman42fed942020-05-29 09:37:03 +0200264 def check_resize_restrictions(self, op):
265 # check unsupported upscaling factor
266 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200267 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
268 return True
Charles Xu36ffaf32020-08-05 15:40:44 +0200269 if op.inputs[0].shape == op.outputs[0].shape:
270 return True
Dwight Lidman42fed942020-05-29 09:37:03 +0200271 upscaled_shape = [op.inputs[0].shape[1] * 2, op.inputs[0].shape[2] * 2]
272 out_shape = op.outputs[0].shape[1:3]
273 if not op.attrs["align_corners"] and out_shape != upscaled_shape:
274 return False
275 elif op.attrs["align_corners"] and out_shape != [upscaled_shape[0] - 1, upscaled_shape[1] - 1]:
276 return False
277 return True
278
Tim Hall79d07d22020-04-27 18:20:16 +0100279 def check_vector_product_restrictions(self, op):
280 # check data type
281 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
282 if weight_tensor.element_size() > 1:
283 return False
284
285 return True
286
287 def check_element_wise_restrictions(self, op):
288 # check data type
289 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200290 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100291 if (
292 op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops
293 and ifm_tensor.dtype != ofm_tensor.dtype
294 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100295 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100296 if op.type in self.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200297 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100298 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200299 return False
300 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100301 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200302 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100303 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200304 return False
305 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100306 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200307 return False
308 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100309 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
310 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
311 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200312 return False
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200313 elif op.type in self.binary_elem_wise_shift_ops | set(("CLZ")):
314 if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32:
315 return False
316 if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32:
317 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100318
319 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200320 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100321 return False
322 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 +0200323 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
324 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200325
326 # negative alpha values are not supported
327 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
328 return False
329
Tim Hall79d07d22020-04-27 18:20:16 +0100330 return True
331
332 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100333 if op.type == "StridedSlice":
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200334 # check stride size
Tim Hall79d07d22020-04-27 18:20:16 +0100335 if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values):
336 return False
Michael McGeaghecd20522020-07-31 16:59:45 +0100337 # check "end - begin" doesnt result in any zero or negative elements
338 if any((end - begin) <= 0 for begin, end in zip(op.inputs[1].values, op.inputs[2].values)):
339 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200340 # check ellipsis_mask
341 if op.attrs["ellipsis_mask"] != 0:
342 return False
343 # check if both new_axis_mask and shrink_axis_mask have bit set
344 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
345 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100346 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200347
348 def check_quantization_restrictions(self, op):
349 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200350 if (
351 len(op.inputs) == 2
352 and not op.inputs[0].quantization == op.inputs[1].quantization == op.outputs[0].quantization
353 ):
354 print(
355 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
356 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200357 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200358 return True
359
360 def check_activation_ops(self, op):
361 if op.type == "Softmax":
362 if not self.softmax_support:
363 return False
364
365 ifm_tensor = op.inputs[0]
366 ofm_tensor = op.outputs[0]
367
368 # check data type
369 if ifm_tensor.dtype != ofm_tensor.dtype:
370 return False
371
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +0200372 if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16):
373 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200374
375 # check batch size
376 if len(ifm_tensor.shape) in (2, 4) and ifm_tensor.shape[0] != 1:
377 return False
378
379 return True