Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | # 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # The SupportedOperators class which is a collection of all supported operators and parameter checks. |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 18 | from .data_type import BaseType |
| 19 | from .data_type import DataType |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class SupportedOperators: |
| 23 | def __init__(self): |
| 24 | # Categorised lists of supported operators |
| 25 | self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead")) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 26 | self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D")) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 27 | self.depthwise_convolution_ops = set( |
| 28 | ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D") |
| 29 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 30 | self.transpose_convolution_ops = set(("Conv2DBackpropInput",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 31 | self.max_pooling_ops = set(("QuantizedMaxPool", "MaxPool", "MaxPoolAct")) |
| 32 | self.avg_pooling_ops = set(("QuantizedAvgPool", "AvgPool", "AvgPoolAct")) |
| 33 | self.pooling_ops = self.max_pooling_ops | self.avg_pooling_ops |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 34 | self.resizing_ops = set(("ResizeBilinear",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 35 | self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct")) |
| 36 | self.mac_main_ops = ( |
| 37 | # convolutions |
| 38 | self.convolution_ops |
| 39 | # depth-wise convolutions |
| 40 | | self.depthwise_convolution_ops |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 41 | # transpose convolutions |
| 42 | | self.transpose_convolution_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | # pooling |
| 44 | | self.pooling_ops |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 45 | # resizing/upscaling |
| 46 | | self.resizing_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 47 | # FC layers |
| 48 | | self.fc_vector_products |
| 49 | # RNN/LSTM/GRU |
| 50 | | set(("BlockLSTM")) |
| 51 | ) |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 52 | self.unary_elem_wise_main_ops = set(("LeakyRelu", "Abs")) |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 53 | self.binary_elem_wise_min_max_ops = set(("Minimum", "Maximum")) |
| 54 | self.binary_elem_wise_add_mul_sub = set( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 55 | ("AddAct", "MulAct", "SubAct", "QuantizedAdd", "QuantizedSub", "QuantizedMul", "Mul", "Add", "Sub",) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 56 | ) |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 57 | self.binary_elem_wise_main_ops = self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 58 | self.elem_wise_main_ops = self.binary_elem_wise_main_ops | self.unary_elem_wise_main_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 59 | self.activation_ops = set( |
| 60 | ("QuantizedRelu", "QuantizedRelu1", "QuantizedRelu6", "Relu", "Relu6", "ReluN1To1", "Sigmoid", "Tanh") |
| 61 | ) |
| 62 | self.npu_post_ops = ( |
| 63 | # activation functions |
| 64 | self.activation_ops |
| 65 | # concatenation write direction |
| 66 | | set(("ConcatSliceWrite")) |
| 67 | # bias add and batch norm |
| 68 | | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm")) |
| 69 | ) |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 70 | self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack")) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 71 | self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack")) |
| 72 | self.memory_only_ops = ( |
| 73 | set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims")) | self.concat_ops | self.split_ops |
| 74 | ) |
| 75 | self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid")) |
| 76 | self.supported_operators = ( |
| 77 | self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops |
| 78 | ) |
| 79 | # Setup supported operator restriction checkers |
| 80 | self.supported_operator_restrictions = {} |
| 81 | self.supported_operator_restrictions.update( |
| 82 | {op: self.check_convolution_restrictions for op in self.convolution_ops} |
| 83 | ) |
| 84 | self.supported_operator_restrictions.update( |
| 85 | {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops} |
| 86 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 87 | self.supported_operator_restrictions.update( |
| 88 | {op: self.check_transpose_convolution_restrictions for op in self.transpose_convolution_ops} |
| 89 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 90 | self.supported_operator_restrictions.update({op: self.check_pooling_restrictions for op in self.pooling_ops}) |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 91 | self.supported_operator_restrictions.update({op: self.check_resize_restrictions for op in self.resizing_ops}) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | self.supported_operator_restrictions.update( |
| 93 | {op: self.check_vector_product_restrictions for op in self.fc_vector_products} |
| 94 | ) |
| 95 | self.supported_operator_restrictions.update( |
| 96 | {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops} |
| 97 | ) |
| 98 | self.supported_operator_restrictions.update( |
| 99 | {op: self.check_memory_only_restrictions for op in self.memory_only_ops} |
| 100 | ) |
| 101 | |
| 102 | def is_operator_supported(self, op): |
| 103 | if op.type not in self.supported_operators: |
| 104 | return False |
| 105 | if not self.check_generic_restrictions(op): |
| 106 | return False |
| 107 | if op.type in self.supported_operator_restrictions: |
| 108 | return self.supported_operator_restrictions[op.type](op) |
| 109 | return True |
| 110 | |
| 111 | def check_generic_restrictions(self, op): |
| 112 | # check fully defined shapes |
| 113 | for t in op.inputs + op.outputs: |
| 114 | if not t.has_fully_defined_shape(): |
| 115 | print("Warning:", op, "has inputs/outputs of undefined shape, placing on CPU") |
| 116 | return False |
| 117 | |
| 118 | # check data type |
| 119 | tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None] |
| 120 | if not tensors: |
| 121 | tensors = op.inputs |
| 122 | for t in tensors: |
| 123 | if not (t.dtype.type & BaseType.Int): |
| 124 | return False |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 125 | if t.element_size() > 2 and op.type not in ("Requantize") | self.binary_elem_wise_add_mul_sub: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 126 | return False |
| 127 | # check size |
| 128 | if any(dim > 65536 for dim in t.shape): |
| 129 | return False |
| 130 | |
| 131 | # check fused activations |
| 132 | if ( |
| 133 | "fused_activation_function" in op.attrs |
| 134 | and op.attrs["fused_activation_function"] is not None |
| 135 | and op.attrs["fused_activation_function"] not in self.supported_fused_activations |
| 136 | ): |
| 137 | return False |
| 138 | return True |
| 139 | |
| 140 | def check_convolution_restrictions(self, op): |
| 141 | # check stride |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 142 | if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 143 | return False |
| 144 | |
| 145 | # check dilation |
| 146 | dilation_w_factor = op.attrs.get("dilation_w_factor", 1) |
| 147 | dilation_h_factor = op.attrs.get("dilation_h_factor", 1) |
| 148 | if dilation_w_factor > 2 or dilation_h_factor > 2: |
| 149 | return False |
| 150 | |
| 151 | # check data type |
| 152 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 153 | if weight_tensor.element_size() > 1: |
| 154 | return False |
| 155 | |
| 156 | # check kernel size |
| 157 | dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1) |
| 158 | dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1) |
| 159 | if ( |
| 160 | dilated_weight_w > 64 |
| 161 | or dilated_weight_h > 64 |
| 162 | or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536 |
| 163 | ): |
| 164 | return False |
| 165 | |
| 166 | # check batch size |
| 167 | if ifm_tensor.shape[0] != 1: |
| 168 | return False |
| 169 | return True |
| 170 | |
| 171 | def check_depthwise_convolution_restrictions(self, op): |
| 172 | # check depth |
| 173 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 174 | if op.attrs["depth_multiplier"] > 1 and not ( |
| 175 | (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]) |
| 176 | ): |
| 177 | return False |
| 178 | return self.check_convolution_restrictions(op) |
| 179 | |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 180 | def check_transpose_convolution_restrictions(self, op): |
| 181 | # check stride |
| 182 | stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"] |
| 183 | if stride_h != stride_w != 2: |
| 184 | return False |
| 185 | |
| 186 | # check output dimensions |
| 187 | ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm() |
| 188 | ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2] |
| 189 | ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2] |
| 190 | if op.attrs["padding"] == b"SAME": |
| 191 | if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w): |
| 192 | return False |
| 193 | elif op.attrs["padding"] == b"VALID": |
| 194 | kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1] |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 195 | if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or ( |
| 196 | ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0) |
| 197 | ): |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 198 | return False |
| 199 | |
| 200 | return self.check_convolution_restrictions(op) |
| 201 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 202 | def check_pooling_restrictions(self, op): |
| 203 | # check stride |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 204 | if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 205 | return False |
| 206 | |
| 207 | # check data type |
| 208 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 209 | if ifm_tensor.dtype != ofm_tensor.dtype: |
| 210 | return False |
| 211 | |
| 212 | # check batch size |
| 213 | if ifm_tensor.shape[0] != 1: |
| 214 | return False |
| 215 | |
| 216 | if op.type in self.avg_pooling_ops: |
| 217 | # check kernel size |
| 218 | if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8): |
| 219 | return False |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 220 | if op.attrs["padding"] == b"VALID" and ( |
| 221 | op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256 |
| 222 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 223 | return False |
| 224 | |
| 225 | if op.type in self.max_pooling_ops: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 226 | # check kernel size (any padding) |
| 227 | if op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 228 | return False |
| 229 | return True |
| 230 | |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 231 | def check_resize_restrictions(self, op): |
| 232 | # check unsupported upscaling factor |
| 233 | if op.type == "ResizeBilinear": |
| 234 | upscaled_shape = [op.inputs[0].shape[1] * 2, op.inputs[0].shape[2] * 2] |
| 235 | out_shape = op.outputs[0].shape[1:3] |
| 236 | if not op.attrs["align_corners"] and out_shape != upscaled_shape: |
| 237 | return False |
| 238 | elif op.attrs["align_corners"] and out_shape != [upscaled_shape[0] - 1, upscaled_shape[1] - 1]: |
| 239 | return False |
| 240 | return True |
| 241 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 242 | def check_vector_product_restrictions(self, op): |
| 243 | # check data type |
| 244 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 245 | if weight_tensor.element_size() > 1: |
| 246 | return False |
| 247 | |
| 248 | return True |
| 249 | |
| 250 | def check_element_wise_restrictions(self, op): |
| 251 | # check data type |
| 252 | ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 253 | # input and output datatype must match for these operators |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 254 | if ( |
| 255 | op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops |
| 256 | and ifm_tensor.dtype != ofm_tensor.dtype |
| 257 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 258 | return False |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 259 | if op.type in self.binary_elem_wise_add_mul_sub: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 260 | # both inputs must have same type |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 261 | if ifm_tensor.dtype != ifm2_tensor.dtype: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 262 | return False |
| 263 | # signed input check |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 264 | if ifm_tensor.dtype.type & BaseType.Signed: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 265 | # output must be signed |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 266 | if ofm_tensor.dtype.type & BaseType.Unsigned: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 267 | return False |
| 268 | # and 8, 16 or 32-bit |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 269 | if ofm_tensor.element_size() not in (1, 2, 4): |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 270 | return False |
| 271 | # unsigned input check, output must be same type or int32 |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 272 | if ifm_tensor.dtype.type & BaseType.Unsigned and not ( |
| 273 | ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32 |
| 274 | ): |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 275 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 276 | |
| 277 | # check batch size |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 278 | if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1: |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 279 | return False |
| 280 | if op.type in self.binary_elem_wise_main_ops: # if op type is unary, ifm2_tensor is None |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 281 | if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1: |
| 282 | return False |
Dwight Lidman | 332a704 | 2020-06-11 15:32:42 +0200 | [diff] [blame] | 283 | |
| 284 | # negative alpha values are not supported |
| 285 | if op.type == "LeakyRelu" and op.attrs["alpha"] < 0: |
| 286 | return False |
| 287 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 288 | return True |
| 289 | |
| 290 | def check_memory_only_restrictions(self, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 291 | if op.type == "StridedSlice": |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 292 | # check stride size |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 293 | if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values): |
| 294 | return False |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 295 | # check ellipsis_mask |
| 296 | if op.attrs["ellipsis_mask"] != 0: |
| 297 | return False |
| 298 | # check if both new_axis_mask and shrink_axis_mask have bit set |
| 299 | if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0: |
| 300 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 301 | return True |