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