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: |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 23 | def __init__(self, softmax_support): |
| 24 | self.softmax_support = softmax_support |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 25 | # Categorised lists of supported operators |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 26 | self.npu_pre_ops = set(("QuantizedResizeBilinear", "SplitSliceRead",)) |
| 27 | self.convolution_ops = set(("Conv2DBiasAct", "Conv2D", "QuantizedConv2D",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 28 | self.depthwise_convolution_ops = set( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 29 | ("DepthwiseConv2dBiasAct", "DepthwiseConv2dNative", "QuantizedDepthwiseConv2D,") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 30 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 31 | self.transpose_convolution_ops = set(("Conv2DBackpropInput",)) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 32 | 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 Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 35 | self.resizing_ops = set(("ResizeBilinear",)) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 36 | self.fc_vector_products = set(("QuantizedMatMul", "MatMul", "FullyConnectedAct",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 37 | self.mac_main_ops = ( |
| 38 | # convolutions |
| 39 | self.convolution_ops |
| 40 | # depth-wise convolutions |
| 41 | | self.depthwise_convolution_ops |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 42 | # transpose convolutions |
| 43 | | self.transpose_convolution_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 44 | # pooling |
| 45 | | self.pooling_ops |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 46 | # resizing/upscaling |
| 47 | | self.resizing_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 48 | # FC layers |
| 49 | | self.fc_vector_products |
| 50 | # RNN/LSTM/GRU |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 51 | | set(("BlockLSTM",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | ) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 53 | self.unary_elem_wise_main_ops = set(("LeakyRelu", "Abs", "CLZ",)) |
| 54 | self.binary_elem_wise_min_max_ops = set(("Minimum", "Maximum",)) |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 55 | self.binary_elem_wise_shift_ops = set(("SHL", "SHR",)) |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 56 | self.binary_elem_wise_add_mul_sub = set( |
Fredrik Svedberg | 1575b94 | 2020-08-18 13:19:18 +0200 | [diff] [blame] | 57 | ("AddAct", "MulAct", "SubAct", "QuantizedAdd", "QuantizedSub", "QuantizedMul", "Mul", "Add", "Sub",) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 58 | ) |
Fredrik Svedberg | 1575b94 | 2020-08-18 13:19:18 +0200 | [diff] [blame] | 59 | self.binary_elem_wise_main_ops = ( |
| 60 | self.binary_elem_wise_min_max_ops | self.binary_elem_wise_add_mul_sub | self.binary_elem_wise_shift_ops |
| 61 | ) |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 62 | 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] | 63 | self.activation_ops = set( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 64 | ( |
| 65 | "QuantizedRelu", |
| 66 | "QuantizedRelu1", |
| 67 | "QuantizedRelu6", |
| 68 | "Relu", |
| 69 | "Relu6", |
| 70 | "ReluN1To1", |
| 71 | "Sigmoid", |
| 72 | "Tanh", |
| 73 | "Softmax", |
| 74 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 75 | ) |
| 76 | self.npu_post_ops = ( |
| 77 | # activation functions |
| 78 | self.activation_ops |
| 79 | # concatenation write direction |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 80 | | set(("ConcatSliceWrite",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 81 | # bias add and batch norm |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 82 | | set(("QuantizedBiasAdd", "Requantize", "QuantizedBatchNorm", "BiasAdd", "FusedBatchNorm",)) |
Jacob Bohlin | 9fbc491 | 2020-06-29 11:58:50 +0200 | [diff] [blame] | 83 | # Quantization |
| 84 | | set(("Quantize",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 85 | ) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 86 | self.split_ops = set(("Split", "SplitV", "StridedSlice", "Slice", "UnpackReshaped", "Unpack",)) |
| 87 | self.concat_ops = set(("Concat", "ConcatV2", "QuantizedConcat", "ConcatTFLite", "PackReshaped", "Pack",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 88 | self.memory_only_ops = ( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 89 | set(("Squeeze", "Reshape", "QuantizedReshape", "ExpandDims",)) | self.concat_ops | self.split_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 90 | ) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 91 | self.supported_fused_activations = set(("Relu", "Relu6", "ReluN1To1", "Tanh", "Sigmoid", "LUT",)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | self.supported_operators = ( |
| 93 | self.npu_pre_ops | self.mac_main_ops | self.elem_wise_main_ops | self.npu_post_ops | self.memory_only_ops |
| 94 | ) |
| 95 | # Setup supported operator restriction checkers |
| 96 | self.supported_operator_restrictions = {} |
| 97 | self.supported_operator_restrictions.update( |
| 98 | {op: self.check_convolution_restrictions for op in self.convolution_ops} |
| 99 | ) |
| 100 | self.supported_operator_restrictions.update( |
| 101 | {op: self.check_depthwise_convolution_restrictions for op in self.depthwise_convolution_ops} |
| 102 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 103 | self.supported_operator_restrictions.update( |
| 104 | {op: self.check_transpose_convolution_restrictions for op in self.transpose_convolution_ops} |
| 105 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 106 | 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] | 107 | 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] | 108 | self.supported_operator_restrictions.update( |
| 109 | {op: self.check_vector_product_restrictions for op in self.fc_vector_products} |
| 110 | ) |
| 111 | self.supported_operator_restrictions.update( |
| 112 | {op: self.check_element_wise_restrictions for op in self.elem_wise_main_ops} |
| 113 | ) |
| 114 | self.supported_operator_restrictions.update( |
| 115 | {op: self.check_memory_only_restrictions for op in self.memory_only_ops} |
| 116 | ) |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 117 | self.supported_operator_restrictions.update( |
| 118 | {op: self.check_quantization_restrictions for op in self.binary_elem_wise_min_max_ops} |
| 119 | ) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 120 | self.supported_operator_restrictions.update({op: self.check_activation_ops for op in self.activation_ops}) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 121 | |
| 122 | def is_operator_supported(self, op): |
| 123 | if op.type not in self.supported_operators: |
| 124 | return False |
| 125 | if not self.check_generic_restrictions(op): |
| 126 | return False |
| 127 | if op.type in self.supported_operator_restrictions: |
| 128 | return self.supported_operator_restrictions[op.type](op) |
| 129 | return True |
| 130 | |
| 131 | def check_generic_restrictions(self, op): |
| 132 | # check fully defined shapes |
Dwight Lidman | 2573311 | 2020-08-17 11:56:10 +0200 | [diff] [blame] | 133 | for t in op.inputs: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame^] | 134 | if not t: |
| 135 | continue |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 136 | if not t.has_fully_defined_shape(): |
Dwight Lidman | 2573311 | 2020-08-17 11:56:10 +0200 | [diff] [blame] | 137 | print("Warning:", op.type, "has input(s) of undefined shape, placing on CPU") |
| 138 | return False |
| 139 | if t.shape == [] and op.type not in self.binary_elem_wise_main_ops: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame^] | 140 | print( |
| 141 | "Warning:", |
| 142 | op.type, |
| 143 | "has input(s) of shape [].", |
| 144 | "Scalar input or broadcasting is not supported for this operator,", |
| 145 | "placing on CPU", |
| 146 | ) |
Dwight Lidman | 2573311 | 2020-08-17 11:56:10 +0200 | [diff] [blame] | 147 | return False |
| 148 | for t in op.outputs: |
| 149 | if not t.has_fully_defined_shape(): |
| 150 | print("Warning:", op.type, "has output(s) of undefined shape, placing on CPU") |
| 151 | return False |
| 152 | if t.shape == []: |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame^] | 153 | print( |
| 154 | "Warning:", |
| 155 | op.type, |
| 156 | "has output(s) of shape [].", |
| 157 | "Scalar input or broadcasting is not supported for this operator,", |
| 158 | "placing on CPU", |
| 159 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 160 | return False |
| 161 | |
| 162 | # check data type |
| 163 | tensors = [t for t in op.get_ifm_ifm2_weights_ofm() if t is not None] |
| 164 | if not tensors: |
| 165 | tensors = op.inputs |
| 166 | for t in tensors: |
| 167 | if not (t.dtype.type & BaseType.Int): |
| 168 | return False |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 169 | if ( |
| 170 | t.element_size() > 2 |
Fredrik Svedberg | 1575b94 | 2020-08-18 13:19:18 +0200 | [diff] [blame] | 171 | and op.type |
| 172 | not in set(("Requantize", "ReduceSum", "CLZ",)) |
| 173 | | self.binary_elem_wise_add_mul_sub |
| 174 | | self.binary_elem_wise_shift_ops |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 175 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 176 | return False |
| 177 | # check size |
| 178 | if any(dim > 65536 for dim in t.shape): |
| 179 | return False |
| 180 | |
| 181 | # check fused activations |
| 182 | if ( |
| 183 | "fused_activation_function" in op.attrs |
| 184 | and op.attrs["fused_activation_function"] is not None |
| 185 | and op.attrs["fused_activation_function"] not in self.supported_fused_activations |
| 186 | ): |
| 187 | return False |
| 188 | return True |
| 189 | |
| 190 | def check_convolution_restrictions(self, op): |
| 191 | # check stride |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 192 | if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 193 | return False |
| 194 | |
| 195 | # check dilation |
| 196 | dilation_w_factor = op.attrs.get("dilation_w_factor", 1) |
| 197 | dilation_h_factor = op.attrs.get("dilation_h_factor", 1) |
| 198 | if dilation_w_factor > 2 or dilation_h_factor > 2: |
| 199 | return False |
| 200 | |
| 201 | # check data type |
| 202 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 203 | if weight_tensor.element_size() > 1: |
| 204 | return False |
| 205 | |
| 206 | # check kernel size |
| 207 | dilated_weight_w = weight_tensor.shape[0] + (weight_tensor.shape[0] - 1) * (dilation_w_factor - 1) |
| 208 | dilated_weight_h = weight_tensor.shape[1] + (weight_tensor.shape[1] - 1) * (dilation_h_factor - 1) |
| 209 | if ( |
| 210 | dilated_weight_w > 64 |
| 211 | or dilated_weight_h > 64 |
| 212 | or dilated_weight_w * dilated_weight_h * weight_tensor.shape[2] > 127 * 65536 |
| 213 | ): |
| 214 | return False |
| 215 | |
| 216 | # check batch size |
| 217 | if ifm_tensor.shape[0] != 1: |
| 218 | return False |
| 219 | return True |
| 220 | |
| 221 | def check_depthwise_convolution_restrictions(self, op): |
| 222 | # check depth |
| 223 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 224 | if op.attrs["depth_multiplier"] > 1 and not ( |
| 225 | (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]) |
| 226 | ): |
| 227 | return False |
| 228 | return self.check_convolution_restrictions(op) |
| 229 | |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 230 | def check_transpose_convolution_restrictions(self, op): |
| 231 | # check stride |
| 232 | stride_h, stride_w = op.attrs["stride_h"], op.attrs["stride_w"] |
| 233 | if stride_h != stride_w != 2: |
| 234 | return False |
| 235 | |
| 236 | # check output dimensions |
| 237 | ifm_tensor, weight_tensor, _, ofm_tensor = op.get_ifm_weights_biases_ofm() |
| 238 | ifm_h, ifm_w = ifm_tensor.shape[1], ifm_tensor.shape[2] |
| 239 | ofm_h, ofm_w = ofm_tensor.shape[1], ofm_tensor.shape[2] |
| 240 | if op.attrs["padding"] == b"SAME": |
| 241 | if (ofm_h != ifm_h * stride_h) or (ofm_w != ifm_w * stride_w): |
| 242 | return False |
| 243 | elif op.attrs["padding"] == b"VALID": |
| 244 | kernel_h, kernel_w = weight_tensor.shape[0], weight_tensor.shape[1] |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 245 | if (ofm_h != (ifm_h) * stride_h + max(kernel_h - stride_h, 0)) or ( |
| 246 | ofm_w != (ifm_w) * stride_w + max(kernel_w - stride_w, 0) |
| 247 | ): |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 248 | return False |
| 249 | |
| 250 | return self.check_convolution_restrictions(op) |
| 251 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 252 | def check_pooling_restrictions(self, op): |
| 253 | # check stride |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 254 | if op.attrs["stride_w"] > 3 or op.attrs["stride_h"] > 3: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 255 | return False |
| 256 | |
| 257 | # check data type |
| 258 | ifm_tensor, _, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
| 259 | if ifm_tensor.dtype != ofm_tensor.dtype: |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 260 | if op.type != "ReduceSum": |
| 261 | return False |
| 262 | # TODO: else check ReduceSum restrictions. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 263 | |
| 264 | # check batch size |
| 265 | if ifm_tensor.shape[0] != 1: |
| 266 | return False |
| 267 | |
| 268 | if op.type in self.avg_pooling_ops: |
| 269 | # check kernel size |
| 270 | if op.attrs["padding"] == b"SAME" and (op.attrs["filter_width"] > 8 or op.attrs["filter_height"] > 8): |
| 271 | return False |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 272 | if op.attrs["padding"] == b"VALID" and ( |
| 273 | op.attrs["filter_width"] * op.attrs["filter_height"] > 256 * 256 or op.attrs["filter_height"] > 256 |
| 274 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 275 | return False |
| 276 | |
| 277 | if op.type in self.max_pooling_ops: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 278 | # check kernel size (any padding) |
| 279 | 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] | 280 | return False |
| 281 | return True |
| 282 | |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 283 | def check_resize_restrictions(self, op): |
| 284 | # check unsupported upscaling factor |
| 285 | if op.type == "ResizeBilinear": |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 286 | if op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1: |
| 287 | return True |
Charles Xu | 36ffaf3 | 2020-08-05 15:40:44 +0200 | [diff] [blame] | 288 | if op.inputs[0].shape == op.outputs[0].shape: |
| 289 | return True |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 290 | upscaled_shape = [op.inputs[0].shape[1] * 2, op.inputs[0].shape[2] * 2] |
| 291 | out_shape = op.outputs[0].shape[1:3] |
| 292 | if not op.attrs["align_corners"] and out_shape != upscaled_shape: |
| 293 | return False |
| 294 | elif op.attrs["align_corners"] and out_shape != [upscaled_shape[0] - 1, upscaled_shape[1] - 1]: |
| 295 | return False |
| 296 | return True |
| 297 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 298 | def check_vector_product_restrictions(self, op): |
| 299 | # check data type |
| 300 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 301 | if weight_tensor.element_size() > 1: |
| 302 | return False |
| 303 | |
| 304 | return True |
| 305 | |
| 306 | def check_element_wise_restrictions(self, op): |
| 307 | # check data type |
| 308 | ifm_tensor, ifm2_tensor, _, ofm_tensor = op.get_ifm_ifm2_weights_ofm() |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 309 | # input and output datatype must match for these operators |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 310 | if ( |
| 311 | op.type in self.binary_elem_wise_min_max_ops | self.unary_elem_wise_main_ops |
| 312 | and ifm_tensor.dtype != ofm_tensor.dtype |
| 313 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 314 | return False |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 315 | if op.type in self.binary_elem_wise_add_mul_sub: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 316 | # both inputs must have same type |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 317 | if ifm_tensor.dtype != ifm2_tensor.dtype: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 318 | return False |
| 319 | # signed input check |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 320 | if ifm_tensor.dtype.type & BaseType.Signed: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 321 | # output must be signed |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 322 | if ofm_tensor.dtype.type & BaseType.Unsigned: |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 323 | return False |
| 324 | # and 8, 16 or 32-bit |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 325 | if ofm_tensor.element_size() not in (1, 2, 4): |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 326 | return False |
| 327 | # unsigned input check, output must be same type or int32 |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 328 | if ifm_tensor.dtype.type & BaseType.Unsigned and not ( |
| 329 | ifm_tensor.dtype == ofm_tensor.dtype or ofm_tensor.dtype == DataType.int32 |
| 330 | ): |
Fredrik Svedberg | 388e9c2 | 2020-05-25 16:32:00 +0200 | [diff] [blame] | 331 | return False |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 332 | elif op.type in self.binary_elem_wise_shift_ops | set(("CLZ")): |
| 333 | if ifm_tensor.dtype != DataType.int32 or ifm2_tensor.dtype != DataType.int32: |
| 334 | return False |
| 335 | if op.type in ("CLZ", "SHL") and ofm_tensor.dtype != DataType.int32: |
| 336 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 337 | |
| 338 | # check batch size |
Dwight Lidman | f995db7 | 2020-04-27 11:15:12 +0200 | [diff] [blame] | 339 | if len(ifm_tensor.shape) > 2 and ifm_tensor.shape[0] != 1: |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 340 | return False |
| 341 | 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] | 342 | if len(ifm2_tensor.shape) > 2 and ifm2_tensor.shape[0] != 1: |
| 343 | return False |
Dwight Lidman | 332a704 | 2020-06-11 15:32:42 +0200 | [diff] [blame] | 344 | |
| 345 | # negative alpha values are not supported |
| 346 | if op.type == "LeakyRelu" and op.attrs["alpha"] < 0: |
| 347 | return False |
| 348 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 349 | return True |
| 350 | |
| 351 | def check_memory_only_restrictions(self, op): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 352 | if op.type == "StridedSlice": |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 353 | # check stride size |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 354 | if len(op.inputs) > 3 and any(stride != 1 for stride in op.inputs[3].values): |
| 355 | return False |
Michael McGeagh | ecd2052 | 2020-07-31 16:59:45 +0100 | [diff] [blame] | 356 | # check "end - begin" doesnt result in any zero or negative elements |
| 357 | if any((end - begin) <= 0 for begin, end in zip(op.inputs[1].values, op.inputs[2].values)): |
| 358 | return False |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 359 | # check ellipsis_mask |
| 360 | if op.attrs["ellipsis_mask"] != 0: |
| 361 | return False |
| 362 | # check if both new_axis_mask and shrink_axis_mask have bit set |
| 363 | if op.attrs["new_axis_mask"] != 0 and op.attrs["shrink_axis_mask"] != 0: |
| 364 | return False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 365 | return True |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 366 | |
| 367 | def check_quantization_restrictions(self, op): |
| 368 | # makes sure IFM1, IFM2 and OFM quantization are equal for binary ops |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 369 | if ( |
| 370 | len(op.inputs) == 2 |
| 371 | and not op.inputs[0].quantization == op.inputs[1].quantization == op.outputs[0].quantization |
| 372 | ): |
| 373 | print( |
| 374 | "Warning: Input/output tensors with different quantization is unsupported for the", op.type, "operator" |
| 375 | ) |
Dwight Lidman | ebe26c7 | 2020-06-09 11:40:54 +0200 | [diff] [blame] | 376 | return False |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 377 | return True |
| 378 | |
| 379 | def check_activation_ops(self, op): |
| 380 | if op.type == "Softmax": |
| 381 | if not self.softmax_support: |
| 382 | return False |
| 383 | |
| 384 | ifm_tensor = op.inputs[0] |
| 385 | ofm_tensor = op.outputs[0] |
| 386 | |
| 387 | # check data type |
| 388 | if ifm_tensor.dtype != ofm_tensor.dtype: |
| 389 | return False |
| 390 | |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 391 | if ifm_tensor.dtype not in (DataType.uint8, DataType.int8, DataType.int16): |
| 392 | return False |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 393 | |
| 394 | # check batch size |
| 395 | if len(ifm_tensor.shape) in (2, 4) and ifm_tensor.shape[0] != 1: |
| 396 | return False |
| 397 | |
| 398 | return True |