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 |
Fredrik Svedberg | 1156317 | 2022-07-06 14:54:12 +0200 | [diff] [blame] | 42 | npu_pre_ops = set( |
| 43 | ( |
| 44 | Op.SplitSliceRead, |
| 45 | Op.Shape, |
| 46 | ) |
| 47 | ) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 48 | convolution_ops = set( |
| 49 | ( |
| 50 | Op.Conv2DBias, |
| 51 | Op.Conv2D, |
| 52 | Op.QuantizedConv2D, |
| 53 | ) |
| 54 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 55 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 56 | transpose_convolution_ops = set((Op.Conv2DBackpropInput,)) |
| 57 | convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops |
| 58 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 59 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 60 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 61 | resizing_ops = Op.op_set(Op.is_resize_op) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 62 | fc_vector_products = set( |
| 63 | ( |
| 64 | Op.QuantizedMatMul, |
| 65 | Op.MatMul, |
| 66 | Op.FullyConnected, |
| 67 | ) |
| 68 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 69 | mac_main_ops = ( |
| 70 | # RNN/LSTM/GRU |
| 71 | set((Op.BlockLSTM,)) |
| 72 | # conv/depthwiseconv/transposeconv |
| 73 | | convolution_like_ops |
| 74 | # pooling |
| 75 | | pooling_ops |
| 76 | # resizing/upscaling |
| 77 | | resizing_ops |
| 78 | # FC layers |
| 79 | | fc_vector_products |
| 80 | # Mean (converts to depthwise conv) |
| 81 | | set((Op.Mean,)) |
| 82 | ) |
| 83 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 84 | binary_elem_wise_min_max_ops = set( |
| 85 | ( |
| 86 | Op.Minimum, |
| 87 | Op.Maximum, |
| 88 | ) |
| 89 | ) |
| 90 | binary_elem_wise_shift_ops = set( |
| 91 | ( |
| 92 | Op.SHL, |
| 93 | Op.SHR, |
| 94 | ) |
| 95 | ) |
| 96 | binary_elem_wise_add_mul_sub = set( |
| 97 | ( |
| 98 | Op.Add, |
| 99 | Op.Mul, |
| 100 | Op.Sub, |
| 101 | ) |
| 102 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 103 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 104 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops |
| 105 | pad_ops = set((Op.Pad,)) |
| 106 | supported_int32_tensor_ops = ( |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 107 | set( |
| 108 | ( |
| 109 | Op.ReduceSum, |
| 110 | Op.CLZ, |
Fredrik Svedberg | 1156317 | 2022-07-06 14:54:12 +0200 | [diff] [blame] | 111 | Op.Shape, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 112 | ) |
| 113 | ) |
| 114 | | binary_elem_wise_add_mul_sub |
| 115 | | binary_elem_wise_shift_ops |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 116 | ) |
| 117 | |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 118 | relu_ops = set( |
| 119 | ( |
| 120 | Op.Relu, |
| 121 | Op.Relu6, |
| 122 | Op.ReluN1To1, |
| 123 | Op.Clip, |
| 124 | ) |
| 125 | ) |
Fredrik Svedberg | 8ddd489 | 2022-08-19 16:06:04 +0200 | [diff] [blame^] | 126 | activation_ops = relu_ops | set( |
| 127 | ( |
| 128 | Op.Tanh, |
| 129 | Op.Sigmoid, |
| 130 | Op.Softmax, |
| 131 | Op.HardSwish, |
| 132 | Op.Prelu, |
| 133 | ) |
| 134 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 135 | npu_post_ops = ( |
| 136 | # activation functions |
| 137 | activation_ops |
| 138 | # concatenation write direction |
| 139 | | set((Op.ConcatSliceWrite,)) |
| 140 | # Quantization |
| 141 | | set((Op.Quantize,)) |
| 142 | ) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 143 | split_ops = set( |
| 144 | ( |
| 145 | Op.Split, |
| 146 | Op.SplitV, |
| 147 | Op.StridedSlice, |
| 148 | Op.Slice, |
| 149 | Op.UnpackReshaped, |
| 150 | Op.Unpack, |
| 151 | ) |
| 152 | ) |
| 153 | concat_ops = set( |
| 154 | ( |
| 155 | Op.Concat, |
| 156 | Op.ConcatTFLite, |
| 157 | Op.PackReshaped, |
| 158 | Op.Pack, |
| 159 | ) |
| 160 | ) |
| 161 | memory_only_ops = ( |
| 162 | set( |
| 163 | ( |
| 164 | Op.Reshape, |
| 165 | Op.QuantizedReshape, |
| 166 | Op.Squeeze, |
| 167 | Op.ExpandDims, |
| 168 | ) |
| 169 | ) |
| 170 | | concat_ops |
| 171 | | split_ops |
| 172 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 173 | per_axis_quant_ops = convolution_like_ops # per-axis/channel quantization only currently supported for conv ops |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 174 | supported_fused_activations = relu_ops | set( |
| 175 | ( |
| 176 | Op.Tanh, |
| 177 | Op.Sigmoid, |
| 178 | Op.LUT, |
| 179 | ) |
| 180 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 181 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | pad_ops | npu_post_ops | memory_only_ops |
| 182 | # Supported data types |
| 183 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 184 | supported_faf_dtypes = set((DataType.uint8, DataType.int8, DataType.int16)) |
| 185 | supported_bias_dtypes = set((DataType.int32, DataType.int64)) |
| 186 | supported_pad_dtypes = set((DataType.int32, DataType.int64)) |
| 187 | # Defined ranges for allowed values: |
| 188 | tens_dim_range = (1, 65535) |
| 189 | stride_range = (1, 3) |
| 190 | dilation_range = (1, 2) |
| 191 | dilated_height_range = (1, 64) |
| 192 | dilated_product_range = (1, 64 * 64) |
| 193 | weights_limit = 127 * 65536 |
| 194 | filter_range = (1, 8) |
| 195 | filter_height_range = (1, 256) |
| 196 | filter_product_range = (1, 256 * 256) |
| 197 | mean_kernel_product = 64 * 64 |
| 198 | mean_kernel_product_int8 = 16 * 16 |
| 199 | mean_kernel_product_avgpool = 256 * 256 |
| 200 | |
| 201 | def __init__(self): |
| 202 | # Setup the generic constraints. Note: the order matters |
| 203 | self.generic_constraints = [] |
| 204 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dtype) |
| 205 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_int32_ops) |
| 206 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dimension) |
| 207 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_quant_per_axis) |
| 208 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf) |
| 209 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf_type) |
| 210 | |
| 211 | # Setup specific constraints. Note: the order matters |
| 212 | self.specific_constraints = defaultdict(list) |
| 213 | |
| 214 | # Conv-like checks: |
| 215 | for op_type in TFLiteSupportedOperators.convolution_like_ops: |
| 216 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range) |
| 217 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilation_range) |
| 218 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_height_range) |
| 219 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_product_range) |
| 220 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 221 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
| 222 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_limit) |
| 223 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 224 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
| 225 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_batch_size) |
| 226 | # Depthwise Conv specific checks: |
| 227 | for op_type in TFLiteSupportedOperators.depthwise_convolution_ops: |
| 228 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depth_multiplier) |
| 229 | # Transpose Conv specific checks: |
| 230 | for op_type in TFLiteSupportedOperators.transpose_convolution_ops: |
| 231 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_stride) |
| 232 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_same) |
| 233 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_valid) |
| 234 | |
| 235 | # Pooling checks: |
| 236 | for op_type in TFLiteSupportedOperators.pooling_ops: |
| 237 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_batch_size) |
| 238 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range) |
| 239 | # AVG pooling specific checks: |
| 240 | for op_type in TFLiteSupportedOperators.avg_pooling_ops: |
| 241 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_range) |
| 242 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range_valid_pad) |
| 243 | self.specific_constraints[op_type].append( |
| 244 | TFLiteSupportedOperators.constraint_filter_product_range_valid_pad |
| 245 | ) |
| 246 | # MAX pooling specific checks: |
| 247 | for op_type in TFLiteSupportedOperators.max_pooling_ops: |
| 248 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range) |
| 249 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_product_range) |
| 250 | |
| 251 | # Resizing specific checks: |
| 252 | for op_type in TFLiteSupportedOperators.resizing_ops: |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 253 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize) |
| 254 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_size) |
| 255 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_attrs) |
| 256 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_half_pixel_centers) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 257 | |
| 258 | # Vector Product specific checks: |
| 259 | for op_type in TFLiteSupportedOperators.fc_vector_products: |
| 260 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 261 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
| 262 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 263 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
| 264 | |
| 265 | # Element-wise checks: |
| 266 | for op_type in TFLiteSupportedOperators.elem_wise_main_ops: |
| 267 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_elemwise_batch_size) |
| 268 | # Binary Min/Max specific checks: |
| 269 | for op_type in TFLiteSupportedOperators.binary_elem_wise_min_max_ops: |
| 270 | self.specific_constraints[op_type].append( |
| 271 | TFLiteSupportedOperators.constraint_matching_quantization_parameters |
| 272 | ) |
| 273 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 274 | # Binary Add/Mul/Sub specific checks: |
| 275 | for op_type in TFLiteSupportedOperators.binary_elem_wise_add_mul_sub: |
| 276 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 277 | # Binary Shift specific checks: |
| 278 | for op_type in TFLiteSupportedOperators.binary_elem_wise_shift_ops: |
| 279 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_inputs_int32) |
| 280 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 281 | |
| 282 | # SHL specific checks: |
| 283 | self.specific_constraints[Op.SHL].append(TFLiteSupportedOperators.constraint_output_int32) |
| 284 | |
| 285 | # CLZ specific checks: |
| 286 | self.specific_constraints[Op.CLZ].append(TFLiteSupportedOperators.constraint_output_int32) |
| 287 | |
| 288 | # StridedSlice specific checks: |
| 289 | self.specific_constraints[Op.StridedSlice].append( |
| 290 | TFLiteSupportedOperators.constraint_stridedslice_stride_values |
| 291 | ) |
| 292 | |
| 293 | # Pad specific checks: |
| 294 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_shape) |
| 295 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_padding_dimensions) |
| 296 | self.specific_constraints[Op.Pad].append(TFLiteSupportedOperators.constraint_pad_type) |
| 297 | |
| 298 | # Mean specific checks: |
Dwight Lidman | f54c18d | 2021-09-29 17:23:03 +0200 | [diff] [blame] | 299 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_batch_size) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 300 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product_avgpool) |
| 301 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product) |
| 302 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product_int8) |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 303 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_single_axis) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 304 | |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 305 | # Reshape specific checks: |
| 306 | self.specific_constraints[Op.Reshape].append(TFLiteSupportedOperators.constraint_reshape_shape_constant) |
| 307 | |
Johan Alfvén | 8e1352a | 2022-08-16 13:04:17 +0200 | [diff] [blame] | 308 | # Concat specific checks: |
| 309 | for op_type in (Op.Concat, Op.ConcatTFLite): |
| 310 | self.specific_constraints[op_type].append( |
| 311 | TFLiteSupportedOperators.constraint_concat_valid_dimensions_non_axis |
| 312 | ) |
| 313 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_concat_valid_dimensions_axis) |
| 314 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 315 | def is_operator_supported(self, op): |
| 316 | ext_type = optype_to_builtintype(op.type) |
| 317 | if op.type not in TFLiteSupportedOperators.supported_operators: |
| 318 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
| 319 | print(f"Info: {ext_type} '{op.name}' is a CPU only op") |
| 320 | return False |
| 321 | |
| 322 | for constraint in self.generic_constraints + self.specific_constraints[op.type]: |
| 323 | valid, extra = constraint(op) |
| 324 | if not valid: |
| 325 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead") |
| 326 | print(f" - {constraint.__doc__}") |
| 327 | if extra: |
| 328 | print(f" {extra}") |
| 329 | return False |
| 330 | |
| 331 | return True |
| 332 | |
| 333 | @classmethod |
| 334 | @docstring_format_args([list_formatter(supported_op_dtypes)]) |
| 335 | def constraint_tens_dtype(cls, op): |
| 336 | "Tensors must be of type: {}" |
| 337 | valid = True |
| 338 | extra = [] |
| 339 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 340 | if not tensors: |
| 341 | tensors = [tens for tens in op.inputs if tens] |
| 342 | for tens in tensors: |
| 343 | if tens.dtype not in cls.supported_op_dtypes: |
| 344 | valid = False |
| 345 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
| 346 | return valid, ", ".join(extra) |
| 347 | |
| 348 | @classmethod |
| 349 | @docstring_format_args([_optype_formatter(supported_int32_tensor_ops)]) |
| 350 | def constraint_tens_int32_ops(cls, op): |
| 351 | "Tensors which are int32 are only valid when op type is: {}" |
| 352 | valid = True |
| 353 | extra = [] |
| 354 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 355 | if not tensors: |
| 356 | tensors = [tens for tens in op.inputs if tens] |
| 357 | for tens in tensors: |
| 358 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 359 | valid = False |
| 360 | extra.append(tens.name) |
| 361 | extra = ", ".join(extra) |
| 362 | return valid, f"Op has int32 tensor(s): {extra}" |
| 363 | |
| 364 | @classmethod |
| 365 | @docstring_format_args(tens_dim_range) |
| 366 | def constraint_tens_dimension(cls, op): |
| 367 | "Tensor dimensions must be in the range [{}, {}]" |
| 368 | tens_min, tens_max = cls.tens_dim_range |
| 369 | valid = True |
| 370 | extra = [] |
| 371 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 372 | if not tensors: |
| 373 | tensors = [tens for tens in op.inputs if tens] |
| 374 | for tens in tensors: |
| 375 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 376 | valid = False |
| 377 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 378 | return valid, ", ".join(extra) |
| 379 | |
| 380 | @classmethod |
| 381 | @docstring_format_args([_optype_formatter(per_axis_quant_ops)]) |
| 382 | def constraint_tens_quant_per_axis(cls, op): |
| 383 | "Per-axis quantization is only supported for the following op types: {}" |
| 384 | valid = True |
| 385 | extra = [] |
| 386 | if op.type not in cls.per_axis_quant_ops: |
| 387 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 388 | for tens in tensors: |
Fredrik Svedberg | 1156317 | 2022-07-06 14:54:12 +0200 | [diff] [blame] | 389 | if tens.quantization and tens.quantization.is_per_axis(): |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 390 | valid = False |
| 391 | extra.append(tens.name) |
| 392 | return valid, "The following tensor(s) have per-axis quantization parameters: " + ", ".join(extra) |
| 393 | |
| 394 | @classmethod |
| 395 | @docstring_format_args([_optype_formatter(supported_fused_activations)]) |
| 396 | def constraint_faf(cls, op): |
| 397 | "The fused activation function (if present) must be one of type: {}" |
| 398 | if op.activation is None: |
| 399 | res = True, "Op has no fused activation function" |
| 400 | else: |
| 401 | faf = op.activation.op_type |
| 402 | valid = faf in cls.supported_fused_activations |
| 403 | res = valid, f"Op has its fused activation function as: {faf}" |
| 404 | return res |
| 405 | |
| 406 | @classmethod |
| 407 | @docstring_format_args([list_formatter(supported_faf_dtypes)]) |
| 408 | def constraint_faf_type(cls, op): |
| 409 | "If a fused activation function is present, the Output tensor must be one of type: {}" |
| 410 | if op.activation is None: |
| 411 | res = True, "Op has no fused activation function" |
| 412 | else: |
| 413 | valid = op.ofm.dtype in cls.supported_faf_dtypes |
| 414 | ext_type = optype_to_builtintype(op.activation.op_type) |
| 415 | res = valid, f"Op has fused activation function {ext_type}, and Output tensor data type: {op.ofm.dtype}" |
| 416 | return res |
| 417 | |
| 418 | @classmethod |
| 419 | @docstring_format_args(stride_range) |
| 420 | def constraint_stride_range(cls, op): |
| 421 | "Stride values for both width and height must be in the range [{}, {}]" |
| 422 | w, h = op.get_kernel_stride() |
| 423 | stride_min, stride_max = cls.stride_range |
| 424 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
| 425 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 426 | |
| 427 | @classmethod |
| 428 | @docstring_format_args(dilation_range) |
| 429 | def constraint_dilation_range(cls, op): |
| 430 | "Dilation factor values for both width and height must be in the range [{}, {}]" |
| 431 | w, h = op.get_kernel_dilation() |
| 432 | dilation_min, dilation_max = cls.dilation_range |
| 433 | valid = (dilation_min <= w <= dilation_max) and (dilation_min <= h <= dilation_max) |
| 434 | return valid, f"Op has dilation factor WxH as: {w}x{h}" |
| 435 | |
| 436 | @classmethod |
| 437 | @docstring_format_args(dilated_height_range) |
| 438 | def constraint_dilated_height_range(cls, op): |
| 439 | "Dilated kernel height must be in the range [{}, {}]" |
| 440 | h = op.kernel.area_height() |
| 441 | dilated_height_min, dilated_height_max = cls.dilated_height_range |
| 442 | valid = dilated_height_min <= h <= dilated_height_max |
| 443 | return valid, f"Op has dilated kernel height as: {h}" |
| 444 | |
| 445 | @classmethod |
| 446 | @docstring_format_args(dilated_product_range) |
| 447 | def constraint_dilated_product_range(cls, op): |
| 448 | "Product of dilated kernel width and height must be in the range [{}, {}]" |
| 449 | product = op.kernel.area_width() * op.kernel.area_height() |
| 450 | dilated_product_min, dilated_product_max = cls.dilated_product_range |
| 451 | valid = dilated_product_min <= product <= dilated_product_max |
| 452 | return valid, f"Op has product of dilated kernel width and height as: {product}" |
| 453 | |
| 454 | @staticmethod |
| 455 | def constraint_weights_type(op): |
| 456 | "Weight tensor must be 8-bit" |
| 457 | weights = op.weights |
| 458 | valid = weights.element_size() == 1 |
| 459 | return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit" |
| 460 | |
| 461 | @staticmethod |
| 462 | def constraint_weights_const(op): |
| 463 | "Weight tensor must be constant" |
| 464 | weights = op.weights |
| 465 | valid = weights.values is not None |
| 466 | return valid, f"Tensor '{weights.name}' has non-constant values" |
| 467 | |
| 468 | @classmethod |
| 469 | @docstring_format_args([weights_limit]) |
| 470 | def constraint_weights_limit(cls, op): |
| 471 | "The sum of the weights cannot exceed {}" |
| 472 | weights = op.weights |
| 473 | values = weights.values.astype(np.int64) - weights.quantization.zero_point |
| 474 | limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2))) |
| 475 | valid = limit <= cls.weights_limit |
| 476 | return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}" |
| 477 | |
| 478 | @classmethod |
| 479 | @docstring_format_args([list_formatter(supported_bias_dtypes)]) |
| 480 | def constraint_bias_type(cls, op): |
| 481 | "Optional Bias tensor must be of type: {}" |
| 482 | bias = op.bias |
| 483 | if bias: |
| 484 | valid = bias.dtype in cls.supported_bias_dtypes |
| 485 | return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}" |
| 486 | return True, "Op has no bias tensor" |
| 487 | |
| 488 | @staticmethod |
| 489 | def constraint_bias_40bit(op): |
| 490 | "Optional Bias tensor values must fit within 40-bits" |
| 491 | bias = op.bias |
| 492 | 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] | 493 | valid = all(len(bin(value)[2:]) <= 40 for value in bias.values) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 494 | return valid, f"Tensor '{bias.name}' has values larger than 40-bits" |
| 495 | return True, "Op has no bias tensor, or it fits in 40-bit" |
| 496 | |
| 497 | @staticmethod |
| 498 | def constraint_batch_size(op): |
| 499 | "IFM Tensor batch size must be 1" |
| 500 | ifm = op.ifm |
| 501 | valid = ifm.shape[0] == 1 |
| 502 | return valid, f"Tensor '{ifm.name}' has batch size: {ifm.shape[0]}" |
| 503 | |
| 504 | @staticmethod |
| 505 | def constraint_depth_multiplier(op): |
| 506 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 507 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 508 | if depth_multiplier > 1: |
| 509 | ifm_channels = op.ifm.shape[3] |
| 510 | ofm_channels = op.ofm.shape[3] |
| 511 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 512 | extra = ( |
| 513 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 514 | f" and depth_multiplier={depth_multiplier}" |
| 515 | ) |
| 516 | return valid, extra |
| 517 | return True, "Op has depth_multiplier=1" |
| 518 | |
| 519 | @staticmethod |
| 520 | def constraint_tconv_stride(op): |
| 521 | "Stride values for both width and height must be 2" |
| 522 | w = op.kernel.stride.x |
| 523 | h = op.kernel.stride.y |
| 524 | valid = (w == 2) and (h == 2) |
| 525 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 526 | |
| 527 | @staticmethod |
| 528 | def constraint_tconv_same(op): |
| 529 | "SAME padding: OFM dimensions must equal IFM dimensions multiplied by stride" |
| 530 | if op.attrs["padding"] == Padding.SAME: |
| 531 | w = op.kernel.stride.x |
| 532 | h = op.kernel.stride.y |
| 533 | ifm_shape = op.ifm.shape |
| 534 | ofm_shape = op.ofm.shape |
| 535 | valid = (ofm_shape[1] == (ifm_shape[1] * h)) and (ofm_shape[2] == (ifm_shape[2] * w)) |
| 536 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and stride WxH as {w}x{h}" |
| 537 | return True, "Op has padding=VALID" |
| 538 | |
| 539 | @staticmethod |
| 540 | def constraint_tconv_valid(op): |
| 541 | """VALID padding: OFM dimensions must equal IFM dimensions multiplied by stride, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 542 | minus difference between kernel size and stride""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 543 | if op.attrs["padding"] == Padding.VALID: |
| 544 | s_w = op.kernel.stride.x |
| 545 | s_h = op.kernel.stride.y |
| 546 | k_w = op.kernel.width |
| 547 | k_h = op.kernel.height |
| 548 | ifm_shape = op.ifm.shape |
| 549 | ofm_shape = op.ofm.shape |
| 550 | height_check = ofm_shape[1] == (ifm_shape[1] * s_h + max(k_h - s_h, 0)) |
| 551 | width_check = ofm_shape[2] == (ifm_shape[2] * s_w + max(k_w - s_w, 0)) |
| 552 | valid = height_check and width_check |
| 553 | extra = ( |
| 554 | f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape}," |
| 555 | f" stride WxH as {s_w}x{s_h} and kernel WxH as {k_w}x{k_h}" |
| 556 | ) |
| 557 | return valid, extra |
| 558 | return True, "Op has padding=SAME" |
| 559 | |
| 560 | @classmethod |
| 561 | @docstring_format_args(filter_range) |
| 562 | def constraint_filter_range(cls, op): |
| 563 | "Kernel filter values for both width and height must be in the range [{}, {}]" |
| 564 | if op.attrs["padding"] == Padding.SAME: |
| 565 | w = op.kernel.width |
| 566 | h = op.kernel.height |
| 567 | filter_min, filter_max = cls.filter_range |
| 568 | valid = (filter_min <= w <= filter_max) and (filter_min <= h <= filter_max) |
| 569 | return valid, f"Op has kernel filter WxH as: {w}x{h}" |
| 570 | return True, "Op has padding=VALID" |
| 571 | |
| 572 | @classmethod |
| 573 | @docstring_format_args(filter_height_range) |
| 574 | def constraint_filter_height_range(cls, op): |
| 575 | "Kernel filter height must be in the range [{}, {}]" |
| 576 | h = op.kernel.height |
| 577 | filter_height_min, filter_height_max = cls.filter_height_range |
| 578 | valid = filter_height_min <= h <= filter_height_max |
| 579 | return valid, f"Op has kernel filter height as: {h}" |
| 580 | |
| 581 | @classmethod |
| 582 | @docstring_format_args(filter_product_range) |
| 583 | def constraint_filter_product_range(cls, op): |
| 584 | "Product of kernel filter width and height must be in the range [{}, {}]" |
| 585 | product = op.kernel.elements_wh() |
| 586 | filter_product_min, filter_product_max = cls.filter_product_range |
| 587 | valid = filter_product_min <= product <= filter_product_max |
| 588 | return valid, f"Op has product of kernel filter width and height as: {product}" |
| 589 | |
| 590 | @staticmethod |
| 591 | @docstring_format_args(filter_height_range) |
| 592 | def constraint_filter_height_range_valid_pad(op): |
| 593 | "VALID padding: Kernel filter height must be in the range [{}, {}]" |
| 594 | if op.attrs["padding"] == Padding.VALID: |
| 595 | return TFLiteSupportedOperators.constraint_filter_height_range(op) |
| 596 | return True, "Op has padding=SAME" |
| 597 | |
| 598 | @staticmethod |
| 599 | @docstring_format_args(filter_product_range) |
| 600 | def constraint_filter_product_range_valid_pad(op): |
| 601 | "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]" |
| 602 | if op.attrs["padding"] == Padding.VALID: |
| 603 | return TFLiteSupportedOperators.constraint_filter_product_range(op) |
| 604 | return True, "Op has padding=SAME" |
| 605 | |
| 606 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 607 | def constraint_resize(op): |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 608 | """The width and height of the IFM and OFM must match one of the following criteria: |
| 609 | IFM W and H must both be 1 |
| 610 | IFM must match OFM |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 611 | OFM W and H must be equal and OFM W-1 and H-1 must be 2x/4x/8x IFM W-1 and H-1, if align_corners is True |
| 612 | OFM W and H must be equal and OFM W and H must be 2x/4x/8x IFM W and H, if align_corners is False""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 613 | # Easier to start with False condition as very few cases result in a supported resize |
| 614 | valid = False |
| 615 | ifm_shape = op.ifm.shape |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 616 | ifm_shape_h = ifm_shape[1] |
| 617 | ifm_shape_w = ifm_shape[2] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 618 | ofm_shape = op.ofm.shape |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 619 | ofm_shape_h = ofm_shape[1] |
| 620 | ofm_shape_w = ofm_shape[2] |
| 621 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 622 | align_corners = op.attrs.get("align_corners", False) |
| 623 | if len(ifm_shape) == 4: |
| 624 | # Valid if IFM W and H are both 1, or IFM and OFM shape are the same |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 625 | if ((ifm_shape_h == 1) and (ifm_shape_w == 1)) or (ifm_shape == ofm_shape): |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 626 | valid = True |
| 627 | else: |
Rickard Bolin | e546def | 2022-01-25 15:45:00 +0000 | [diff] [blame] | 628 | # Valid if OFM is 2/4/8x IFM (-1 for align corners) |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 629 | if align_corners: |
| 630 | h_upscale_factor = (ofm_shape_h - 1) / (ifm_shape_h - 1) |
| 631 | w_upscale_factor = (ofm_shape_w - 1) / (ifm_shape_w - 1) |
| 632 | else: |
| 633 | h_upscale_factor = ofm_shape_h / ifm_shape_h |
| 634 | w_upscale_factor = ofm_shape_w / ifm_shape_w |
Rickard Bolin | e546def | 2022-01-25 15:45:00 +0000 | [diff] [blame] | 635 | |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 636 | # could use either height or width. save as int because it is more usable later in graph optimiser |
| 637 | op.attrs["upscale_factor"] = int(h_upscale_factor) |
| 638 | valid = h_upscale_factor == w_upscale_factor and h_upscale_factor in (2.0, 4.0, 8.0) |
Rickard Bolin | e546def | 2022-01-25 15:45:00 +0000 | [diff] [blame] | 639 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 640 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}" |
| 641 | |
| 642 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 643 | def constraint_resize_size(op): |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 644 | "The size tensor must match the output tensor shape" |
| 645 | valid = False |
| 646 | ofm_shape = op.ofm.shape |
| 647 | size_h, size_w = None, None |
| 648 | # check that the size tensor (the second input) exists, is not none, and has the correct values |
| 649 | if len(op.inputs) == 2 and op.inputs[1] is not None and len(op.inputs[1].values) == 2: |
| 650 | size_h, size_w = op.inputs[1].values |
| 651 | # check size and output size match |
| 652 | if size_h == ofm_shape[1] and size_w == ofm_shape[2]: |
| 653 | valid = True |
| 654 | |
| 655 | return valid, f"Op has size={size_h}x{size_w} and ofm_shape={ofm_shape}." |
| 656 | |
| 657 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 658 | def constraint_resize_attrs(op): |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 659 | "Both align_corners and half_pixel_centers can't be True" |
| 660 | valid = True |
| 661 | align_corners = op.attrs.get("align_corners", False) |
| 662 | half_pixel_centers = op.attrs.get("half_pixel_centers", False) |
| 663 | |
| 664 | if align_corners and half_pixel_centers: |
| 665 | valid = False |
| 666 | return valid, "Op has both align_corners and half_pixel_centers set to True." |
| 667 | |
| 668 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 669 | def constraint_resize_half_pixel_centers(op): |
erik.andersson@arm.com | ba2555e | 2021-10-28 14:08:52 +0200 | [diff] [blame] | 670 | "half_pixel_centers are not supported" |
| 671 | valid = True |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 672 | if op.attrs.get("half_pixel_centers", False): |
erik.andersson@arm.com | ba2555e | 2021-10-28 14:08:52 +0200 | [diff] [blame] | 673 | valid = False |
| 674 | return valid, f"Op has half_pixel_centers set to {not valid}." |
| 675 | |
| 676 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 677 | def constraint_pad_shape(op): |
| 678 | "The padding tensor must have the shape [3,2] or [4,2]" |
| 679 | valid = op.inputs[1].shape in ([3, 2], [4, 2]) |
| 680 | return valid, f"The pad tensor has the shape: {op.inputs[1].shape}" |
| 681 | |
| 682 | @classmethod |
| 683 | @docstring_format_args([list_formatter(supported_pad_dtypes)]) |
| 684 | def constraint_pad_type(cls, op): |
| 685 | "Pad tensor must be of type: {}" |
| 686 | pad_tensor = op.inputs[1] |
| 687 | valid = pad_tensor.dtype in cls.supported_pad_dtypes |
| 688 | return valid, f"Tensor '{pad_tensor.name}' has data type: {pad_tensor.dtype}" |
| 689 | |
| 690 | @staticmethod |
| 691 | def constraint_padding_dimensions(op): |
| 692 | "The pad tensor can only pad width and height" |
| 693 | pad_tensor = op.inputs[1].values |
| 694 | |
| 695 | valid = sum(pad_tensor[-1, :]) == 0 |
| 696 | if valid and len(pad_tensor) > 3: |
| 697 | valid = sum(pad_tensor[0, :]) == 0 |
| 698 | return valid, f"First dimension padding: {pad_tensor[0,:]}, last dimension padding: {pad_tensor[-1,:]}" |
| 699 | |
| 700 | @staticmethod |
| 701 | def constraint_stridedslice_stride_values(op): |
| 702 | "All Strides values must be 1" |
| 703 | strides = op.inputs[3] |
| 704 | valid = all(stride == 1 for stride in strides.values) |
| 705 | return valid, f"Op has strides values {strides.values}" |
| 706 | |
| 707 | @staticmethod |
| 708 | def constraint_inputs_int32(op): |
| 709 | "Both Input data types must be int32" |
| 710 | ifm_dtype = op.ifm.dtype |
| 711 | ifm2_dtype = op.ifm2.dtype |
| 712 | valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32) |
| 713 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
| 714 | |
| 715 | @staticmethod |
| 716 | def constraint_output_int32(op): |
| 717 | "OFM must be int32" |
| 718 | ofm_dtype = op.ofm.dtype |
| 719 | valid = ofm_dtype == DataType.int32 |
| 720 | return valid, f"Op has ofm_dtype={ofm_dtype}" |
| 721 | |
| 722 | @staticmethod |
| 723 | def constraint_matching_quantization_parameters(op): |
| 724 | "Both Input quantization parameters must match OFM quantization parameters" |
| 725 | valid = True |
| 726 | extra = [] |
| 727 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm): |
| 728 | valid = False |
| 729 | extra.append(op.ifm.name) |
| 730 | if op.ifm2 is not None and not check_quantized_tens_scaling_equal(op.ofm, op.ifm2): |
| 731 | valid = False |
| 732 | extra.append(op.ifm2.name) |
| 733 | extra = ", ".join(extra) |
| 734 | return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}" |
| 735 | |
| 736 | @staticmethod |
| 737 | def constraint_elemwise_batch_size(op): |
| 738 | "Batch size must be 1 for Input tensors with more than 2 dimensions" |
| 739 | valid = True |
| 740 | extra = [] |
| 741 | for tens in (op.ifm, op.ifm2): |
| 742 | # Unary ops have ifm2 as None |
| 743 | if tens is not None: |
| 744 | if (len(tens.shape) > 2) and (tens.shape[0] != 1): |
| 745 | valid = False |
| 746 | extra.append(tens.name) |
| 747 | extra = ", ".join(extra) |
| 748 | return valid, f"Op has invalid input tensors: {extra}" |
| 749 | |
| 750 | @staticmethod |
| 751 | def constraint_broadcast_shapes(op): |
| 752 | "Broadcasting is only allowed for rank indices with dimension 1, from either IFM1 or IFM2" |
| 753 | ifm_shape = op.ifm.shape |
| 754 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 755 | ofm_shape = op.ofm.shape |
| 756 | valid = True |
| 757 | if ifm_shape is not None and ifm2_shape is not None: |
| 758 | # align trailing dimensions |
| 759 | size = min(len(ifm_shape), len(ifm2_shape)) |
| 760 | for i, i2, o in zip(ifm_shape[-size:], ifm2_shape[-size:], ofm_shape[-size:]): |
| 761 | mi = max(i, i2) |
| 762 | # Input dimensions should match or one should be of dimension 1 |
| 763 | # Output dimension should match the largest input dimension, together |
| 764 | # with constraint_match_either_shapes ensures broadcast from only one input |
| 765 | if not (i == i2 or i == 1 or i2 == 1) or o != mi: |
| 766 | valid = False |
| 767 | break |
| 768 | |
| 769 | return valid, f"Op has ifm_shape={ifm_shape} and ifm2_shape={ifm2_shape}" |
| 770 | |
| 771 | @classmethod |
| 772 | @docstring_format_args([mean_kernel_product_avgpool]) |
| 773 | def constraint_mean_height_width_product_avgpool(cls, op): |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 774 | """Product of height and width must be no greater than {}""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 775 | shape = op.inputs[0].shape |
| 776 | hi = 0 if len(shape) < 4 else 1 |
| 777 | h, w = shape[hi : hi + 2] |
| 778 | max_prod = cls.mean_kernel_product_avgpool |
| 779 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 780 | |
| 781 | @classmethod |
| 782 | @docstring_format_args([mean_kernel_product]) |
| 783 | def constraint_mean_height_width_product(cls, op): |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 784 | """Product of height and width must be no greater than {} when: |
| 785 | IFM and OFM have different scale or zero point; or |
| 786 | 'keep_dims' is True""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 787 | ifmq, ofmq = op.ifm.quantization, op.ofm.quantization |
| 788 | keep_dims = op.attrs.get("keep_dims") |
| 789 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 790 | if not keep_dims and ifmq.scale_f32 == ofmq.scale_f32 and ifmq.zero_point == ofmq.zero_point: |
| 791 | return True, "" |
| 792 | shape = op.inputs[0].shape |
| 793 | hi = 0 if len(shape) < 4 else 1 |
| 794 | h, w = shape[hi : hi + 2] |
| 795 | max_prod = cls.mean_kernel_product |
| 796 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 797 | |
| 798 | @classmethod |
| 799 | @docstring_format_args([mean_kernel_product_int8]) |
| 800 | def constraint_mean_height_width_product_int8(cls, op): |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 801 | """Product of IFM height and width must be no greater than {} when: |
| 802 | The IFM shape has 4 dimensions; and |
| 803 | The axis indices specify reduction across 2 dimensions; and |
| 804 | The axis indices correspond to the width and height dimensions of the IFM; and |
| 805 | 'keep_dims' is True; and |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 806 | IFM datatype is int8""" |
| 807 | shape = op.ifm.shape |
| 808 | axis = int(op.inputs[1].values) if op.inputs[1].shape == [] else list(op.inputs[1].values) |
| 809 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 810 | # and constraint_mean_height_width_product |
| 811 | if ( |
| 812 | len(shape) != 4 |
| 813 | or op.ifm.dtype != DataType.int8 |
| 814 | or not op.attrs.get("keep_dims") |
| 815 | or axis not in ([1, 2], [2, 1]) |
| 816 | ): |
| 817 | return True, "" |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 818 | h = shape[-3] |
| 819 | w = shape[-2] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 820 | max_prod = cls.mean_kernel_product_int8 |
| 821 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 822 | |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 823 | @classmethod |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 824 | @docstring_format_args([filter_height_range[1], dilated_height_range[1]]) |
| 825 | def constraint_mean_height_single_axis(cls, op): |
| 826 | """For single axis averages across the height dimension: |
| 827 | IFM height must be no greater than {} if the IFM and OFM scale and zero point match; otherwise |
| 828 | IFM height must be no greater than {} if the IFM and OFM scale or zero point do not match""" |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 829 | inp, axis = op.inputs |
| 830 | if axis.shape == [] or axis.shape[0] == 1: # single axis |
| 831 | axis = int(axis.values) if len(axis.shape) == 0 else int(axis.values[0]) |
| 832 | else: |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 833 | # Multiple axes |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 834 | return True, "" |
| 835 | |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 836 | shape = inp.shape |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 837 | if len(shape) < 3: |
| 838 | # No height dimension present in IFM |
| 839 | return True, "" |
| 840 | if axis != len(shape) - 3: |
| 841 | # Not averaging across the height dimension |
| 842 | return True, "" |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 843 | |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 844 | h = shape[axis] |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 845 | ifm, ofm = op.get_ifm_ofm() |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 846 | |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 847 | if check_quantized_tens_scaling_equal(ifm, ofm): |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 848 | return h <= cls.filter_height_range[1], f"Height is {h}, IFM and OFM quantizations match" |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 849 | else: |
James Peet | 0bb7ad1 | 2022-02-15 15:07:54 +0000 | [diff] [blame] | 850 | return h <= cls.dilated_height_range[1], f"Height is {h}, IFM and OFM quantizations do not match" |
Rickard Bolin | 7d7cb67 | 2021-12-07 09:09:14 +0000 | [diff] [blame] | 851 | |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 852 | @staticmethod |
| 853 | def constraint_reshape_shape_constant(op): |
| 854 | "Shape must be constant" |
| 855 | valid = True |
| 856 | extra = [] |
| 857 | |
| 858 | reshape_tens = op.inputs[1] |
| 859 | if reshape_tens is not None: |
| 860 | # constant inputs have either no driving operator or a const one |
| 861 | # create a list of non-constant inputs |
| 862 | if not (len(reshape_tens.ops) == 0 or reshape_tens.ops[0].type == Op.Const): |
| 863 | valid = False |
| 864 | extra.append(reshape_tens.name) |
| 865 | extra = ", ".join(extra) |
| 866 | |
| 867 | return valid, f"Op has non-const input(s): {extra}" |
Johan Alfvén | 8e1352a | 2022-08-16 13:04:17 +0200 | [diff] [blame] | 868 | |
| 869 | @staticmethod |
| 870 | def constraint_concat_valid_dimensions_non_axis(op): |
| 871 | """All Input dimensions must match OFM dimension in all axes except the one defined by the axis attribute""" |
| 872 | valid = True |
| 873 | extra = [] |
| 874 | ofm_shape = op.ofm.shape |
| 875 | ofm_dim = len(ofm_shape) |
| 876 | axis = op.attrs["axis"] |
| 877 | axis += ofm_dim if axis < 0 else 0 |
| 878 | |
| 879 | tensors = [tens for tens in op.inputs if tens] |
| 880 | for tens in tensors: |
| 881 | if any(tens.shape[dim] != ofm_shape[dim] for dim in range(ofm_dim) if dim != axis): |
| 882 | valid = False |
| 883 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 884 | |
| 885 | extra = ", ".join(extra) |
| 886 | return valid, f"Op has axis={axis}, ofm_shape={ofm_shape} and the list of mismatching inputs are: {extra}" |
| 887 | |
| 888 | @staticmethod |
| 889 | def constraint_concat_valid_dimensions_axis(op): |
| 890 | """The size of the OFM axis must match the sum of all IFM axis defined by the axis attribute""" |
| 891 | valid = True |
| 892 | extra = [] |
| 893 | ofm_shape = op.ofm.shape |
| 894 | ofm_dim = len(ofm_shape) |
| 895 | axis = op.attrs["axis"] |
| 896 | axis += ofm_dim if axis < 0 else 0 |
| 897 | |
| 898 | sum_ifm_axis = 0 |
| 899 | tensors = [tens for tens in op.inputs if tens] |
| 900 | for tens in tensors: |
| 901 | sum_ifm_axis += tens.shape[axis] |
| 902 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 903 | |
| 904 | valid = sum_ifm_axis == ofm_shape[axis] |
| 905 | extra = ", ".join(extra) |
| 906 | return valid, f"Op has axis={axis}, ofm_shape={ofm_shape} and the list of mismatching inputs are: {extra}" |