Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 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. |
| 16 | # Description: |
| 17 | # The TFLiteSupportedOperators class which is a collection of all TFLite supported operators and parameter checks. |
| 18 | from collections import defaultdict |
| 19 | |
| 20 | import numpy as np |
| 21 | |
| 22 | from .data_type import DataType |
| 23 | from .operation import Op |
| 24 | from .operation import Padding |
| 25 | from .supported_operators_util import docstring_format_args |
| 26 | from .supported_operators_util import list_formatter |
| 27 | from .tensor import check_quantized_tens_scaling_equal |
| 28 | from .tflite_mapping import BUILTIN_OPERATOR_UNKNOWN |
| 29 | from .tflite_mapping import optype_to_builtintype |
| 30 | |
| 31 | |
| 32 | def _optype_formatter(op_list): |
| 33 | # Convert internal op types to external names |
| 34 | output = map(optype_to_builtintype, op_list) |
| 35 | # Remove UNKNOWNs |
| 36 | output = (x for x in output if x is not BUILTIN_OPERATOR_UNKNOWN) |
| 37 | return list_formatter(output) |
| 38 | |
| 39 | |
| 40 | class TFLiteSupportedOperators: |
| 41 | # Categorised lists of supported operators |
| 42 | npu_pre_ops = set((Op.SplitSliceRead,)) |
| 43 | convolution_ops = set((Op.Conv2DBias, Op.Conv2D, Op.QuantizedConv2D,)) |
| 44 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 45 | transpose_convolution_ops = set((Op.Conv2DBackpropInput,)) |
| 46 | convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops |
| 47 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 48 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 49 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
| 50 | resizing_ops = set((Op.ResizeBilinear,)) |
| 51 | fc_vector_products = set((Op.QuantizedMatMul, Op.MatMul, Op.FullyConnected,)) |
| 52 | mac_main_ops = ( |
| 53 | # RNN/LSTM/GRU |
| 54 | set((Op.BlockLSTM,)) |
| 55 | # conv/depthwiseconv/transposeconv |
| 56 | | convolution_like_ops |
| 57 | # pooling |
| 58 | | pooling_ops |
| 59 | # resizing/upscaling |
| 60 | | resizing_ops |
| 61 | # FC layers |
| 62 | | fc_vector_products |
| 63 | # Mean (converts to depthwise conv) |
| 64 | | set((Op.Mean,)) |
| 65 | ) |
| 66 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
| 67 | binary_elem_wise_min_max_ops = set((Op.Minimum, Op.Maximum,)) |
| 68 | binary_elem_wise_shift_ops = set((Op.SHL, Op.SHR,)) |
| 69 | binary_elem_wise_add_mul_sub = set((Op.Add, Op.Mul, Op.Sub,)) |
| 70 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 71 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops |
| 72 | pad_ops = set((Op.Pad,)) |
| 73 | supported_int32_tensor_ops = ( |
| 74 | set((Op.ReduceSum, Op.CLZ,)) | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 75 | ) |
| 76 | |
| 77 | relu_ops = set((Op.Relu, Op.Relu6, Op.ReluN1To1, Op.Clip,)) |
| 78 | activation_ops = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.Softmax, Op.HardSwish)) |
| 79 | npu_post_ops = ( |
| 80 | # activation functions |
| 81 | activation_ops |
| 82 | # concatenation write direction |
| 83 | | set((Op.ConcatSliceWrite,)) |
| 84 | # Quantization |
| 85 | | set((Op.Quantize,)) |
| 86 | ) |
| 87 | split_ops = set((Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack,)) |
| 88 | concat_ops = set((Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack,)) |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame^] | 89 | memory_only_ops = set((Op.Reshape, Op.QuantizedReshape, Op.Squeeze, Op.ExpandDims,)) | concat_ops | split_ops |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 90 | per_axis_quant_ops = convolution_like_ops # per-axis/channel quantization only currently supported for conv ops |
| 91 | supported_fused_activations = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.LUT,)) |
| 92 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | pad_ops | npu_post_ops | memory_only_ops |
| 93 | # Supported data types |
| 94 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 95 | supported_faf_dtypes = set((DataType.uint8, DataType.int8, DataType.int16)) |
| 96 | supported_bias_dtypes = set((DataType.int32, DataType.int64)) |
| 97 | supported_pad_dtypes = set((DataType.int32, DataType.int64)) |
| 98 | # Defined ranges for allowed values: |
| 99 | tens_dim_range = (1, 65535) |
| 100 | stride_range = (1, 3) |
| 101 | dilation_range = (1, 2) |
| 102 | dilated_height_range = (1, 64) |
| 103 | dilated_product_range = (1, 64 * 64) |
| 104 | weights_limit = 127 * 65536 |
| 105 | filter_range = (1, 8) |
| 106 | filter_height_range = (1, 256) |
| 107 | filter_product_range = (1, 256 * 256) |
| 108 | mean_kernel_product = 64 * 64 |
| 109 | mean_kernel_product_int8 = 16 * 16 |
| 110 | mean_kernel_product_avgpool = 256 * 256 |
| 111 | |
| 112 | def __init__(self): |
| 113 | # Setup the generic constraints. Note: the order matters |
| 114 | self.generic_constraints = [] |
| 115 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dtype) |
| 116 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_int32_ops) |
| 117 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dimension) |
| 118 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_quant_per_axis) |
| 119 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf) |
| 120 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf_type) |
| 121 | |
| 122 | # Setup specific constraints. Note: the order matters |
| 123 | self.specific_constraints = defaultdict(list) |
| 124 | |
| 125 | # Conv-like checks: |
| 126 | for op_type in TFLiteSupportedOperators.convolution_like_ops: |
| 127 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range) |
| 128 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilation_range) |
| 129 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_height_range) |
| 130 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_product_range) |
| 131 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 132 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
| 133 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_limit) |
| 134 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 135 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
| 136 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_batch_size) |
| 137 | # Depthwise Conv specific checks: |
| 138 | for op_type in TFLiteSupportedOperators.depthwise_convolution_ops: |
| 139 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depth_multiplier) |
| 140 | # Transpose Conv specific checks: |
| 141 | for op_type in TFLiteSupportedOperators.transpose_convolution_ops: |
| 142 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_stride) |
| 143 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_same) |
| 144 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_valid) |
| 145 | |
| 146 | # Pooling checks: |
| 147 | for op_type in TFLiteSupportedOperators.pooling_ops: |
| 148 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_batch_size) |
| 149 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range) |
| 150 | # AVG pooling specific checks: |
| 151 | for op_type in TFLiteSupportedOperators.avg_pooling_ops: |
| 152 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_range) |
| 153 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range_valid_pad) |
| 154 | self.specific_constraints[op_type].append( |
| 155 | TFLiteSupportedOperators.constraint_filter_product_range_valid_pad |
| 156 | ) |
| 157 | # MAX pooling specific checks: |
| 158 | for op_type in TFLiteSupportedOperators.max_pooling_ops: |
| 159 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range) |
| 160 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_product_range) |
| 161 | |
| 162 | # Resizing specific checks: |
| 163 | for op_type in TFLiteSupportedOperators.resizing_ops: |
| 164 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize) |
| 165 | |
| 166 | # Vector Product specific checks: |
| 167 | for op_type in TFLiteSupportedOperators.fc_vector_products: |
| 168 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 169 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
| 170 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 171 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
| 172 | |
| 173 | # Element-wise checks: |
| 174 | for op_type in TFLiteSupportedOperators.elem_wise_main_ops: |
| 175 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_elemwise_batch_size) |
| 176 | # Binary Min/Max specific checks: |
| 177 | for op_type in TFLiteSupportedOperators.binary_elem_wise_min_max_ops: |
| 178 | self.specific_constraints[op_type].append( |
| 179 | TFLiteSupportedOperators.constraint_matching_quantization_parameters |
| 180 | ) |
| 181 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 182 | # Binary Add/Mul/Sub specific checks: |
| 183 | for op_type in TFLiteSupportedOperators.binary_elem_wise_add_mul_sub: |
| 184 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 185 | # Binary Shift specific checks: |
| 186 | for op_type in TFLiteSupportedOperators.binary_elem_wise_shift_ops: |
| 187 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_inputs_int32) |
| 188 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 189 | |
| 190 | # SHL specific checks: |
| 191 | self.specific_constraints[Op.SHL].append(TFLiteSupportedOperators.constraint_output_int32) |
| 192 | |
| 193 | # CLZ specific checks: |
| 194 | self.specific_constraints[Op.CLZ].append(TFLiteSupportedOperators.constraint_output_int32) |
| 195 | |
| 196 | # StridedSlice specific checks: |
| 197 | self.specific_constraints[Op.StridedSlice].append( |
| 198 | TFLiteSupportedOperators.constraint_stridedslice_stride_values |
| 199 | ) |
| 200 | |
| 201 | # Pad specific checks: |
| 202 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_shape) |
| 203 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_padding_dimensions) |
| 204 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_type) |
| 205 | |
| 206 | # Mean specific checks: |
| 207 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product_avgpool) |
| 208 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product) |
| 209 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product_int8) |
| 210 | |
| 211 | def is_operator_supported(self, op): |
| 212 | ext_type = optype_to_builtintype(op.type) |
| 213 | if op.type not in TFLiteSupportedOperators.supported_operators: |
| 214 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
| 215 | print(f"Info: {ext_type} '{op.name}' is a CPU only op") |
| 216 | return False |
| 217 | |
| 218 | for constraint in self.generic_constraints + self.specific_constraints[op.type]: |
| 219 | valid, extra = constraint(op) |
| 220 | if not valid: |
| 221 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead") |
| 222 | print(f" - {constraint.__doc__}") |
| 223 | if extra: |
| 224 | print(f" {extra}") |
| 225 | return False |
| 226 | |
| 227 | return True |
| 228 | |
| 229 | @classmethod |
| 230 | @docstring_format_args([list_formatter(supported_op_dtypes)]) |
| 231 | def constraint_tens_dtype(cls, op): |
| 232 | "Tensors must be of type: {}" |
| 233 | valid = True |
| 234 | extra = [] |
| 235 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 236 | if not tensors: |
| 237 | tensors = [tens for tens in op.inputs if tens] |
| 238 | for tens in tensors: |
| 239 | if tens.dtype not in cls.supported_op_dtypes: |
| 240 | valid = False |
| 241 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
| 242 | return valid, ", ".join(extra) |
| 243 | |
| 244 | @classmethod |
| 245 | @docstring_format_args([_optype_formatter(supported_int32_tensor_ops)]) |
| 246 | def constraint_tens_int32_ops(cls, op): |
| 247 | "Tensors which are int32 are only valid when op type is: {}" |
| 248 | valid = True |
| 249 | extra = [] |
| 250 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 251 | if not tensors: |
| 252 | tensors = [tens for tens in op.inputs if tens] |
| 253 | for tens in tensors: |
| 254 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 255 | valid = False |
| 256 | extra.append(tens.name) |
| 257 | extra = ", ".join(extra) |
| 258 | return valid, f"Op has int32 tensor(s): {extra}" |
| 259 | |
| 260 | @classmethod |
| 261 | @docstring_format_args(tens_dim_range) |
| 262 | def constraint_tens_dimension(cls, op): |
| 263 | "Tensor dimensions must be in the range [{}, {}]" |
| 264 | tens_min, tens_max = cls.tens_dim_range |
| 265 | valid = True |
| 266 | extra = [] |
| 267 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 268 | if not tensors: |
| 269 | tensors = [tens for tens in op.inputs if tens] |
| 270 | for tens in tensors: |
| 271 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 272 | valid = False |
| 273 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 274 | return valid, ", ".join(extra) |
| 275 | |
| 276 | @classmethod |
| 277 | @docstring_format_args([_optype_formatter(per_axis_quant_ops)]) |
| 278 | def constraint_tens_quant_per_axis(cls, op): |
| 279 | "Per-axis quantization is only supported for the following op types: {}" |
| 280 | valid = True |
| 281 | extra = [] |
| 282 | if op.type not in cls.per_axis_quant_ops: |
| 283 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 284 | for tens in tensors: |
| 285 | if tens.quantization.is_per_axis(): |
| 286 | valid = False |
| 287 | extra.append(tens.name) |
| 288 | return valid, "The following tensor(s) have per-axis quantization parameters: " + ", ".join(extra) |
| 289 | |
| 290 | @classmethod |
| 291 | @docstring_format_args([_optype_formatter(supported_fused_activations)]) |
| 292 | def constraint_faf(cls, op): |
| 293 | "The fused activation function (if present) must be one of type: {}" |
| 294 | if op.activation is None: |
| 295 | res = True, "Op has no fused activation function" |
| 296 | else: |
| 297 | faf = op.activation.op_type |
| 298 | valid = faf in cls.supported_fused_activations |
| 299 | res = valid, f"Op has its fused activation function as: {faf}" |
| 300 | return res |
| 301 | |
| 302 | @classmethod |
| 303 | @docstring_format_args([list_formatter(supported_faf_dtypes)]) |
| 304 | def constraint_faf_type(cls, op): |
| 305 | "If a fused activation function is present, the Output tensor must be one of type: {}" |
| 306 | if op.activation is None: |
| 307 | res = True, "Op has no fused activation function" |
| 308 | else: |
| 309 | valid = op.ofm.dtype in cls.supported_faf_dtypes |
| 310 | ext_type = optype_to_builtintype(op.activation.op_type) |
| 311 | res = valid, f"Op has fused activation function {ext_type}, and Output tensor data type: {op.ofm.dtype}" |
| 312 | return res |
| 313 | |
| 314 | @classmethod |
| 315 | @docstring_format_args(stride_range) |
| 316 | def constraint_stride_range(cls, op): |
| 317 | "Stride values for both width and height must be in the range [{}, {}]" |
| 318 | w, h = op.get_kernel_stride() |
| 319 | stride_min, stride_max = cls.stride_range |
| 320 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
| 321 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 322 | |
| 323 | @classmethod |
| 324 | @docstring_format_args(dilation_range) |
| 325 | def constraint_dilation_range(cls, op): |
| 326 | "Dilation factor values for both width and height must be in the range [{}, {}]" |
| 327 | w, h = op.get_kernel_dilation() |
| 328 | dilation_min, dilation_max = cls.dilation_range |
| 329 | valid = (dilation_min <= w <= dilation_max) and (dilation_min <= h <= dilation_max) |
| 330 | return valid, f"Op has dilation factor WxH as: {w}x{h}" |
| 331 | |
| 332 | @classmethod |
| 333 | @docstring_format_args(dilated_height_range) |
| 334 | def constraint_dilated_height_range(cls, op): |
| 335 | "Dilated kernel height must be in the range [{}, {}]" |
| 336 | h = op.kernel.area_height() |
| 337 | dilated_height_min, dilated_height_max = cls.dilated_height_range |
| 338 | valid = dilated_height_min <= h <= dilated_height_max |
| 339 | return valid, f"Op has dilated kernel height as: {h}" |
| 340 | |
| 341 | @classmethod |
| 342 | @docstring_format_args(dilated_product_range) |
| 343 | def constraint_dilated_product_range(cls, op): |
| 344 | "Product of dilated kernel width and height must be in the range [{}, {}]" |
| 345 | product = op.kernel.area_width() * op.kernel.area_height() |
| 346 | dilated_product_min, dilated_product_max = cls.dilated_product_range |
| 347 | valid = dilated_product_min <= product <= dilated_product_max |
| 348 | return valid, f"Op has product of dilated kernel width and height as: {product}" |
| 349 | |
| 350 | @staticmethod |
| 351 | def constraint_weights_type(op): |
| 352 | "Weight tensor must be 8-bit" |
| 353 | weights = op.weights |
| 354 | valid = weights.element_size() == 1 |
| 355 | return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit" |
| 356 | |
| 357 | @staticmethod |
| 358 | def constraint_weights_const(op): |
| 359 | "Weight tensor must be constant" |
| 360 | weights = op.weights |
| 361 | valid = weights.values is not None |
| 362 | return valid, f"Tensor '{weights.name}' has non-constant values" |
| 363 | |
| 364 | @classmethod |
| 365 | @docstring_format_args([weights_limit]) |
| 366 | def constraint_weights_limit(cls, op): |
| 367 | "The sum of the weights cannot exceed {}" |
| 368 | weights = op.weights |
| 369 | values = weights.values.astype(np.int64) - weights.quantization.zero_point |
| 370 | limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2))) |
| 371 | valid = limit <= cls.weights_limit |
| 372 | return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}" |
| 373 | |
| 374 | @classmethod |
| 375 | @docstring_format_args([list_formatter(supported_bias_dtypes)]) |
| 376 | def constraint_bias_type(cls, op): |
| 377 | "Optional Bias tensor must be of type: {}" |
| 378 | bias = op.bias |
| 379 | if bias: |
| 380 | valid = bias.dtype in cls.supported_bias_dtypes |
| 381 | return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}" |
| 382 | return True, "Op has no bias tensor" |
| 383 | |
| 384 | @staticmethod |
| 385 | def constraint_bias_40bit(op): |
| 386 | "Optional Bias tensor values must fit within 40-bits" |
| 387 | bias = op.bias |
| 388 | if bias and bias.dtype == DataType.int64 and bias.values is not None: |
Tim Hall | 8ae2929 | 2021-07-28 16:52:03 +0100 | [diff] [blame] | 389 | valid = all(len(bin(value)[2:]) <= 40 for value in bias.values) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 390 | return valid, f"Tensor '{bias.name}' has values larger than 40-bits" |
| 391 | return True, "Op has no bias tensor, or it fits in 40-bit" |
| 392 | |
| 393 | @staticmethod |
| 394 | def constraint_batch_size(op): |
| 395 | "IFM Tensor batch size must be 1" |
| 396 | ifm = op.ifm |
| 397 | valid = ifm.shape[0] == 1 |
| 398 | return valid, f"Tensor '{ifm.name}' has batch size: {ifm.shape[0]}" |
| 399 | |
| 400 | @staticmethod |
| 401 | def constraint_depth_multiplier(op): |
| 402 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 403 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 404 | if depth_multiplier > 1: |
| 405 | ifm_channels = op.ifm.shape[3] |
| 406 | ofm_channels = op.ofm.shape[3] |
| 407 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 408 | extra = ( |
| 409 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 410 | f" and depth_multiplier={depth_multiplier}" |
| 411 | ) |
| 412 | return valid, extra |
| 413 | return True, "Op has depth_multiplier=1" |
| 414 | |
| 415 | @staticmethod |
| 416 | def constraint_tconv_stride(op): |
| 417 | "Stride values for both width and height must be 2" |
| 418 | w = op.kernel.stride.x |
| 419 | h = op.kernel.stride.y |
| 420 | valid = (w == 2) and (h == 2) |
| 421 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 422 | |
| 423 | @staticmethod |
| 424 | def constraint_tconv_same(op): |
| 425 | "SAME padding: OFM dimensions must equal IFM dimensions multiplied by stride" |
| 426 | if op.attrs["padding"] == Padding.SAME: |
| 427 | w = op.kernel.stride.x |
| 428 | h = op.kernel.stride.y |
| 429 | ifm_shape = op.ifm.shape |
| 430 | ofm_shape = op.ofm.shape |
| 431 | valid = (ofm_shape[1] == (ifm_shape[1] * h)) and (ofm_shape[2] == (ifm_shape[2] * w)) |
| 432 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and stride WxH as {w}x{h}" |
| 433 | return True, "Op has padding=VALID" |
| 434 | |
| 435 | @staticmethod |
| 436 | def constraint_tconv_valid(op): |
| 437 | """VALID padding: OFM dimensions must equal IFM dimensions multiplied by stride, |
| 438 | minus difference between kernel size and stride""" |
| 439 | if op.attrs["padding"] == Padding.VALID: |
| 440 | s_w = op.kernel.stride.x |
| 441 | s_h = op.kernel.stride.y |
| 442 | k_w = op.kernel.width |
| 443 | k_h = op.kernel.height |
| 444 | ifm_shape = op.ifm.shape |
| 445 | ofm_shape = op.ofm.shape |
| 446 | height_check = ofm_shape[1] == (ifm_shape[1] * s_h + max(k_h - s_h, 0)) |
| 447 | width_check = ofm_shape[2] == (ifm_shape[2] * s_w + max(k_w - s_w, 0)) |
| 448 | valid = height_check and width_check |
| 449 | extra = ( |
| 450 | f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape}," |
| 451 | f" stride WxH as {s_w}x{s_h} and kernel WxH as {k_w}x{k_h}" |
| 452 | ) |
| 453 | return valid, extra |
| 454 | return True, "Op has padding=SAME" |
| 455 | |
| 456 | @classmethod |
| 457 | @docstring_format_args(filter_range) |
| 458 | def constraint_filter_range(cls, op): |
| 459 | "Kernel filter values for both width and height must be in the range [{}, {}]" |
| 460 | if op.attrs["padding"] == Padding.SAME: |
| 461 | w = op.kernel.width |
| 462 | h = op.kernel.height |
| 463 | filter_min, filter_max = cls.filter_range |
| 464 | valid = (filter_min <= w <= filter_max) and (filter_min <= h <= filter_max) |
| 465 | return valid, f"Op has kernel filter WxH as: {w}x{h}" |
| 466 | return True, "Op has padding=VALID" |
| 467 | |
| 468 | @classmethod |
| 469 | @docstring_format_args(filter_height_range) |
| 470 | def constraint_filter_height_range(cls, op): |
| 471 | "Kernel filter height must be in the range [{}, {}]" |
| 472 | h = op.kernel.height |
| 473 | filter_height_min, filter_height_max = cls.filter_height_range |
| 474 | valid = filter_height_min <= h <= filter_height_max |
| 475 | return valid, f"Op has kernel filter height as: {h}" |
| 476 | |
| 477 | @classmethod |
| 478 | @docstring_format_args(filter_product_range) |
| 479 | def constraint_filter_product_range(cls, op): |
| 480 | "Product of kernel filter width and height must be in the range [{}, {}]" |
| 481 | product = op.kernel.elements_wh() |
| 482 | filter_product_min, filter_product_max = cls.filter_product_range |
| 483 | valid = filter_product_min <= product <= filter_product_max |
| 484 | return valid, f"Op has product of kernel filter width and height as: {product}" |
| 485 | |
| 486 | @staticmethod |
| 487 | @docstring_format_args(filter_height_range) |
| 488 | def constraint_filter_height_range_valid_pad(op): |
| 489 | "VALID padding: Kernel filter height must be in the range [{}, {}]" |
| 490 | if op.attrs["padding"] == Padding.VALID: |
| 491 | return TFLiteSupportedOperators.constraint_filter_height_range(op) |
| 492 | return True, "Op has padding=SAME" |
| 493 | |
| 494 | @staticmethod |
| 495 | @docstring_format_args(filter_product_range) |
| 496 | def constraint_filter_product_range_valid_pad(op): |
| 497 | "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]" |
| 498 | if op.attrs["padding"] == Padding.VALID: |
| 499 | return TFLiteSupportedOperators.constraint_filter_product_range(op) |
| 500 | return True, "Op has padding=SAME" |
| 501 | |
| 502 | @staticmethod |
| 503 | def constraint_resize(op): |
| 504 | """The width and height of the IFM and OFM must match one of the following criteria: |
| 505 | IFM W and H must both be 1 |
| 506 | IFM must match OFM |
| 507 | OFM W and H must be 2x IFM -1, if align_corners is True |
| 508 | OFM W and H must be 2x IFM, if align_corners is False""" |
| 509 | # Easier to start with False condition as very few cases result in a supported resize |
| 510 | valid = False |
| 511 | ifm_shape = op.ifm.shape |
| 512 | ofm_shape = op.ofm.shape |
| 513 | align_corners = op.attrs.get("align_corners", False) |
| 514 | if len(ifm_shape) == 4: |
| 515 | # Valid if IFM W and H are both 1, or IFM and OFM shape are the same |
| 516 | if ((ifm_shape[1] == 1) and (ifm_shape[2] == 1)) or (ifm_shape == ofm_shape): |
| 517 | valid = True |
| 518 | else: |
| 519 | upscaled_shape = np.array(ifm_shape[1:3]) |
| 520 | out_shape = np.array(ofm_shape[1:3]) |
| 521 | while (upscaled_shape < out_shape).all(): |
| 522 | upscaled_shape *= 2 |
| 523 | if align_corners: |
| 524 | upscaled_shape -= 1 |
| 525 | # Valid if OFM is 2x IFM (-1 for align corners) |
| 526 | if np.array_equal(out_shape, upscaled_shape): |
| 527 | valid = True |
| 528 | break |
| 529 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}" |
| 530 | |
| 531 | @staticmethod |
| 532 | def constraint_pad_shape(op): |
| 533 | "The padding tensor must have the shape [3,2] or [4,2]" |
| 534 | valid = op.inputs[1].shape in ([3, 2], [4, 2]) |
| 535 | return valid, f"The pad tensor has the shape: {op.inputs[1].shape}" |
| 536 | |
| 537 | @classmethod |
| 538 | @docstring_format_args([list_formatter(supported_pad_dtypes)]) |
| 539 | def constraint_pad_type(cls, op): |
| 540 | "Pad tensor must be of type: {}" |
| 541 | pad_tensor = op.inputs[1] |
| 542 | valid = pad_tensor.dtype in cls.supported_pad_dtypes |
| 543 | return valid, f"Tensor '{pad_tensor.name}' has data type: {pad_tensor.dtype}" |
| 544 | |
| 545 | @staticmethod |
| 546 | def constraint_padding_dimensions(op): |
| 547 | "The pad tensor can only pad width and height" |
| 548 | pad_tensor = op.inputs[1].values |
| 549 | |
| 550 | valid = sum(pad_tensor[-1, :]) == 0 |
| 551 | if valid and len(pad_tensor) > 3: |
| 552 | valid = sum(pad_tensor[0, :]) == 0 |
| 553 | return valid, f"First dimension padding: {pad_tensor[0,:]}, last dimension padding: {pad_tensor[-1,:]}" |
| 554 | |
| 555 | @staticmethod |
| 556 | def constraint_stridedslice_stride_values(op): |
| 557 | "All Strides values must be 1" |
| 558 | strides = op.inputs[3] |
| 559 | valid = all(stride == 1 for stride in strides.values) |
| 560 | return valid, f"Op has strides values {strides.values}" |
| 561 | |
| 562 | @staticmethod |
| 563 | def constraint_inputs_int32(op): |
| 564 | "Both Input data types must be int32" |
| 565 | ifm_dtype = op.ifm.dtype |
| 566 | ifm2_dtype = op.ifm2.dtype |
| 567 | valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32) |
| 568 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
| 569 | |
| 570 | @staticmethod |
| 571 | def constraint_output_int32(op): |
| 572 | "OFM must be int32" |
| 573 | ofm_dtype = op.ofm.dtype |
| 574 | valid = ofm_dtype == DataType.int32 |
| 575 | return valid, f"Op has ofm_dtype={ofm_dtype}" |
| 576 | |
| 577 | @staticmethod |
| 578 | def constraint_matching_quantization_parameters(op): |
| 579 | "Both Input quantization parameters must match OFM quantization parameters" |
| 580 | valid = True |
| 581 | extra = [] |
| 582 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm): |
| 583 | valid = False |
| 584 | extra.append(op.ifm.name) |
| 585 | if op.ifm2 is not None and not check_quantized_tens_scaling_equal(op.ofm, op.ifm2): |
| 586 | valid = False |
| 587 | extra.append(op.ifm2.name) |
| 588 | extra = ", ".join(extra) |
| 589 | return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}" |
| 590 | |
| 591 | @staticmethod |
| 592 | def constraint_elemwise_batch_size(op): |
| 593 | "Batch size must be 1 for Input tensors with more than 2 dimensions" |
| 594 | valid = True |
| 595 | extra = [] |
| 596 | for tens in (op.ifm, op.ifm2): |
| 597 | # Unary ops have ifm2 as None |
| 598 | if tens is not None: |
| 599 | if (len(tens.shape) > 2) and (tens.shape[0] != 1): |
| 600 | valid = False |
| 601 | extra.append(tens.name) |
| 602 | extra = ", ".join(extra) |
| 603 | return valid, f"Op has invalid input tensors: {extra}" |
| 604 | |
| 605 | @staticmethod |
| 606 | def constraint_broadcast_shapes(op): |
| 607 | "Broadcasting is only allowed for rank indices with dimension 1, from either IFM1 or IFM2" |
| 608 | ifm_shape = op.ifm.shape |
| 609 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 610 | ofm_shape = op.ofm.shape |
| 611 | valid = True |
| 612 | if ifm_shape is not None and ifm2_shape is not None: |
| 613 | # align trailing dimensions |
| 614 | size = min(len(ifm_shape), len(ifm2_shape)) |
| 615 | for i, i2, o in zip(ifm_shape[-size:], ifm2_shape[-size:], ofm_shape[-size:]): |
| 616 | mi = max(i, i2) |
| 617 | # Input dimensions should match or one should be of dimension 1 |
| 618 | # Output dimension should match the largest input dimension, together |
| 619 | # with constraint_match_either_shapes ensures broadcast from only one input |
| 620 | if not (i == i2 or i == 1 or i2 == 1) or o != mi: |
| 621 | valid = False |
| 622 | break |
| 623 | |
| 624 | return valid, f"Op has ifm_shape={ifm_shape} and ifm2_shape={ifm2_shape}" |
| 625 | |
| 626 | @classmethod |
| 627 | @docstring_format_args([mean_kernel_product_avgpool]) |
| 628 | def constraint_mean_height_width_product_avgpool(cls, op): |
| 629 | """Product of height and width can be at most {}""" |
| 630 | shape = op.inputs[0].shape |
| 631 | hi = 0 if len(shape) < 4 else 1 |
| 632 | h, w = shape[hi : hi + 2] |
| 633 | max_prod = cls.mean_kernel_product_avgpool |
| 634 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 635 | |
| 636 | @classmethod |
| 637 | @docstring_format_args([mean_kernel_product]) |
| 638 | def constraint_mean_height_width_product(cls, op): |
| 639 | """Product of height and width can be at most {} when IFM and OFM have different scale or zero point, |
| 640 | or keep_dims is True""" |
| 641 | ifmq, ofmq = op.ifm.quantization, op.ofm.quantization |
| 642 | keep_dims = op.attrs.get("keep_dims") |
| 643 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 644 | if not keep_dims and ifmq.scale_f32 == ofmq.scale_f32 and ifmq.zero_point == ofmq.zero_point: |
| 645 | return True, "" |
| 646 | shape = op.inputs[0].shape |
| 647 | hi = 0 if len(shape) < 4 else 1 |
| 648 | h, w = shape[hi : hi + 2] |
| 649 | max_prod = cls.mean_kernel_product |
| 650 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 651 | |
| 652 | @classmethod |
| 653 | @docstring_format_args([mean_kernel_product_int8]) |
| 654 | def constraint_mean_height_width_product_int8(cls, op): |
| 655 | """Product of IFM height and width can be at most {} when the following are true: |
| 656 | IFM dimensions are 4, |
| 657 | Axis indices are 1 and 2, |
| 658 | keep_dims is set to True and |
| 659 | IFM datatype is int8""" |
| 660 | shape = op.ifm.shape |
| 661 | axis = int(op.inputs[1].values) if op.inputs[1].shape == [] else list(op.inputs[1].values) |
| 662 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 663 | # and constraint_mean_height_width_product |
| 664 | if ( |
| 665 | len(shape) != 4 |
| 666 | or op.ifm.dtype != DataType.int8 |
| 667 | or not op.attrs.get("keep_dims") |
| 668 | or axis not in ([1, 2], [2, 1]) |
| 669 | ): |
| 670 | return True, "" |
| 671 | hi = 0 if len(shape) < 4 else 1 |
| 672 | h, w = shape[hi : hi + 2] |
| 673 | max_prod = cls.mean_kernel_product_int8 |
| 674 | return h * w <= max_prod, f"Product of height and width is {h * w}" |