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