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