blob: 73e219b5621049b0bd6f1d7c2f90ffe1049eb358 [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 Svedberg388e9c22020-05-25 16:32:00 +020055 self.binary_elem_wise_add_mul_sub = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020056 (
57 "AddAct",
58 "MulAct",
59 "SubAct",
60 "QuantizedAdd",
61 "QuantizedSub",
62 "QuantizedMul",
63 "Mul",
64 "Add",
65 "Sub",
66 "SHL",
67 "SHR",
68 )
Tim Hall79d07d22020-04-27 18:20:16 +010069 )
Fredrik Svedberg388e9c22020-05-25 16:32:00 +020070 self.binary_elem_wise_main_ops = self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub
Dwight Lidmanf995db72020-04-27 11:15:12 +020071 self.elem_wise_main_ops = self.binary_elem_wise_main_ops | self.unary_elem_wise_main_ops
Tim Hall79d07d22020-04-27 18:20:16 +010072 self.activation_ops = set(
Fredrik Svedberga0c36242020-06-03 15:43:31 +020073 (
74 "QuantizedRelu",
75 "QuantizedRelu1",
76 "QuantizedRelu6",
77 "Relu",
78 "Relu6",
79 "ReluN1To1",
80 "Sigmoid",
81 "Tanh",
82 "Softmax",
83 )
Tim Hall79d07d22020-04-27 18:20:16 +010084 )
85 self.npu_post_ops = (
86 # activation functions
87 self.activation_ops
88 # concatenation write direction
Fredrik Svedberga0c36242020-06-03 15:43:31 +020089 | set(("ConcatSliceWrite",))
Tim Hall79d07d22020-04-27 18:20:16 +010090 # bias add and batch norm
Fredrik Svedberga0c36242020-06-03 15:43:31 +020091 | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm",))
Jacob Bohlin9fbc4912020-06-29 11:58:50 +020092 # Quantization
93 | set(("Quantize",))
Tim Hall79d07d22020-04-27 18:20:16 +010094 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +020095 self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack",))
96 self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack",))
Tim Hall79d07d22020-04-27 18:20:16 +010097 self.memory_only_ops = (
Fredrik Svedberga0c36242020-06-03 15:43:31 +020098 set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims",)) | self.concat_ops | self.split_ops
Tim Hall79d07d22020-04-27 18:20:16 +010099 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200100 self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid", "LUT",))
Tim Hall79d07d22020-04-27 18:20:16 +0100101 self.supported_operators = (
102 self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops
103 )
104 # Setup supported operator restriction checkers
105 self.supported_operator_restrictions = {}
106 self.supported_operator_restrictions.update(
107 {op: self.check_convolution_restrictions for op in self.convolution_ops}
108 )
109 self.supported_operator_restrictions.update(
110 {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops}
111 )
Jacob Bohlincf7da102020-05-20 09:03:40 +0200112 self.supported_operator_restrictions.update(
113 {op: self.check_transpose_convolution_restrictions for op in self.transpose_convolution_ops}
114 )
Tim Hall79d07d22020-04-27 18:20:16 +0100115 self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops})
Dwight Lidman42fed942020-05-29 09:37:03 +0200116 self.supported_operator_restrictions.update({op: self.check_resize_restrictions for op in self.resizing_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100117 self.supported_operator_restrictions.update(
118 {op: self.check_vector_product_restrictions for op in self.fc_vector_products}
119 )
120 self.supported_operator_restrictions.update(
121 {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops}
122 )
123 self.supported_operator_restrictions.update(
124 {op: self.check_memory_only_restrictions for op in self.memory_only_ops}
125 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200126 self.supported_operator_restrictions.update(
127 {op: self.check_quantization_restrictions for op in self.binary_elem_wise_min_max_ops}
128 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200129 self.supported_operator_restrictions.update({op: self.check_activation_ops for op in self.activation_ops})
Tim Hall79d07d22020-04-27 18:20:16 +0100130
131 def is_operator_supported(self, op):
132 if op.type not in self.supported_operators:
133 return False
134 if not self.check_generic_restrictions(op):
135 return False
136 if op.type in self.supported_operator_restrictions:
137 return self.supported_operator_restrictions[op.type](op)
138 return True
139
140 def check_generic_restrictions(self, op):
141 # check fully defined shapes
142 for t in op.inputs + op.outputs:
143 if not t.has_fully_defined_shape():
144 print("Warning:", op, "has inputs/outputs of undefined shape, placing on CPU")
145 return False
146
147 # check data type
148 tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None]
149 if not tensors:
150 tensors = op.inputs
151 for t in tensors:
152 if not (t.dtype.type & BaseType.Int):
153 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200154 if (
155 t.element_size() > 2
156 and op.type not in set(("Requantize", "ReduceSum", "CLZ",)) | self.binary_elem_wise_add_mul_sub
157 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100158 return False
159 # check size
160 if any(dim > 65536 for dim in t.shape):
161 return False
162
163 # check fused activations
164 if (
165 "fused_activation_function" in op.attrs
166 and op.attrs["fused_activation_function"] is not None
167 and op.attrs["fused_activation_function"] not in self.supported_fused_activations
168 ):
169 return False
170 return True
171
172 def check_convolution_restrictions(self, op):
173 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200174 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100175 return False
176
177 # check dilation
178 dilation_w_factor = op.attrs.get("dilation_w_factor", 1)
179 dilation_h_factor = op.attrs.get("dilation_h_factor", 1)
180 if dilation_w_factor > 2 or dilation_h_factor > 2:
181 return False
182
183 # check data type
184 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
185 if weight_tensor.element_size() > 1:
186 return False
187
188 # check kernel size
189 dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1)
190 dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1)
191 if (
192 dilated_weight_w > 64
193 or dilated_weight_h > 64
194 or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536
195 ):
196 return False
197
198 # check batch size
199 if ifm_tensor.shape[0] != 1:
200 return False
201 return True
202
203 def check_depthwise_convolution_restrictions(self, op):
204 # check depth
205 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
206 if op.attrs["depth_multiplier"] > 1 and not (
207 (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"])
208 ):
209 return False
210 return self.check_convolution_restrictions(op)
211
Jacob Bohlincf7da102020-05-20 09:03:40 +0200212 def check_transpose_convolution_restrictions(self, op):
213 # check stride
214 stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"]
215 if stride_h != stride_w != 2:
216 return False
217
218 # check output dimensions
219 ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm()
220 ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2]
221 ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2]
222 if op.attrs["padding"] == b"SAME":
223 if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w):
224 return False
225 elif op.attrs["padding"] == b"VALID":
226 kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1]
Tim Hallc30f4952020-06-15 20:47:35 +0100227 if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or (
228 ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0)
229 ):
Jacob Bohlincf7da102020-05-20 09:03:40 +0200230 return False
231
232 return self.check_convolution_restrictions(op)
233
Tim Hall79d07d22020-04-27 18:20:16 +0100234 def check_pooling_restrictions(self, op):
235 # check stride
Dwight Lidman0538a772020-05-06 14:09:17 +0200236 if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3:
Tim Hall79d07d22020-04-27 18:20:16 +0100237 return False
238
239 # check data type
240 ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
241 if ifm_tensor.dtype != ofm_tensor.dtype:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200242 if op.type != "ReduceSum":
243 return False
244 # TODO: else check ReduceSum restrictions.
Tim Hall79d07d22020-04-27 18:20:16 +0100245
246 # check batch size
247 if ifm_tensor.shape[0] != 1:
248 return False
249
250 if op.type in self.avg_pooling_ops:
251 # check kernel size
252 if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8):
253 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100254 if op.attrs["padding"] == b"VALID" and (
255 op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256
256 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100257 return False
258
259 if op.type in self.max_pooling_ops:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200260 # check kernel size (any padding)
261 if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256:
Tim Hall79d07d22020-04-27 18:20:16 +0100262 return False
263 return True
264
Dwight Lidman42fed942020-05-29 09:37:03 +0200265 def check_resize_restrictions(self, op):
266 # check unsupported upscaling factor
267 if op.type == "ResizeBilinear":
Charles Xu9a03fdf2020-07-02 15:12:40 +0200268 if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
269 return True
Dwight Lidman42fed942020-05-29 09:37:03 +0200270 upscaled_shape = [op.inputs[0].shape[1] * 2, op.inputs[0].shape[2] * 2]
271 out_shape = op.outputs[0].shape[1:3]
272 if not op.attrs["align_corners"] and out_shape != upscaled_shape:
273 return False
274 elif op.attrs["align_corners"] and out_shape != [upscaled_shape[0] - 1, upscaled_shape[1] - 1]:
275 return False
276 return True
277
Tim Hall79d07d22020-04-27 18:20:16 +0100278 def check_vector_product_restrictions(self, op):
279 # check data type
280 ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm()
281 if weight_tensor.element_size() > 1:
282 return False
283
284 return True
285
286 def check_element_wise_restrictions(self, op):
287 # check data type
288 ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm()
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200289 # input and output datatype must match for these operators
Tim Hallc30f4952020-06-15 20:47:35 +0100290 if (
291 op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops
292 and ifm_tensor.dtype != ofm_tensor.dtype
293 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100294 return False
Tim Hallc30f4952020-06-15 20:47:35 +0100295 if op.type in self.binary_elem_wise_add_mul_sub:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200296 # both inputs must have same type
Tim Hallc30f4952020-06-15 20:47:35 +0100297 if ifm_tensor.dtype != ifm2_tensor.dtype:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200298 return False
299 # signed input check
Tim Hallc30f4952020-06-15 20:47:35 +0100300 if ifm_tensor.dtype.type & BaseType.Signed:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200301 # output must be signed
Tim Hallc30f4952020-06-15 20:47:35 +0100302 if ofm_tensor.dtype.type & BaseType.Unsigned:
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200303 return False
304 # and 8, 16 or 32-bit
Tim Hallc30f4952020-06-15 20:47:35 +0100305 if ofm_tensor.element_size() not in (1, 2, 4):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200306 return False
307 # unsigned input check, output must be same type or int32
Tim Hallc30f4952020-06-15 20:47:35 +0100308 if ifm_tensor.dtype.type & BaseType.Unsigned and not (
309 ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32
310 ):
Fredrik Svedberg388e9c22020-05-25 16:32:00 +0200311 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100312
313 # check batch size
Dwight Lidmanf995db72020-04-27 11:15:12 +0200314 if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1:
Tim Hallc30f4952020-06-15 20:47:35 +0100315 return False
316 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 +0200317 if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1:
318 return False
Dwight Lidman332a7042020-06-11 15:32:42 +0200319
320 # negative alpha values are not supported
321 if op.type == "LeakyRelu" and op.attrs["alpha"] < 0:
322 return False
323
Tim Hall79d07d22020-04-27 18:20:16 +0100324 return True
325
326 def check_memory_only_restrictions(self, op):
Tim Hall79d07d22020-04-27 18:20:16 +0100327 if op.type == "StridedSlice":
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200328 # check stride size
Tim Hall79d07d22020-04-27 18:20:16 +0100329 if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values):
330 return False
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200331 # check ellipsis_mask
332 if op.attrs["ellipsis_mask"] != 0:
333 return False
334 # check if both new_axis_mask and shrink_axis_mask have bit set
335 if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0:
336 return False
Tim Hall79d07d22020-04-27 18:20:16 +0100337 return True
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200338
339 def check_quantization_restrictions(self, op):
340 # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200341 if (
342 len(op.inputs) == 2
343 and not op.inputs[0].quantization == op.inputs[1].quantization == op.outputs[0].quantization
344 ):
345 print(
346 "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator"
347 )
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200348 return False
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200349 return True
350
351 def check_activation_ops(self, op):
352 if op.type == "Softmax":
353 if not self.softmax_support:
354 return False
355
356 ifm_tensor = op.inputs[0]
357 ofm_tensor = op.outputs[0]
358
359 # check data type
360 if ifm_tensor.dtype != ofm_tensor.dtype:
361 return False
362
363 if ifm_tensor.dtype != DataType.int16:
364 return False # TODO: Implement support for 8-bit Softmax
365
366 # check batch size
367 if len(ifm_tensor.shape) in (2, 4) and ifm_tensor.shape[0] != 1:
368 return False
369
370 return True