William Isaksson | 56e5f0c | 2024-01-10 12:28:04 +0100 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 16 | # |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 17 | # Description: |
| 18 | # The TFLiteSupportedOperators class which is a collection of all TFLite supported operators and parameter checks. |
| 19 | from collections import defaultdict |
| 20 | |
| 21 | import numpy as np |
| 22 | |
| 23 | from .data_type import DataType |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 24 | from .numeric_util import full_shape |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 25 | from .operation import Op |
| 26 | from .operation import Padding |
| 27 | from .supported_operators_util import docstring_format_args |
| 28 | from .supported_operators_util import list_formatter |
| 29 | from .tensor import check_quantized_tens_scaling_equal |
| 30 | from .tflite_mapping import BUILTIN_OPERATOR_UNKNOWN |
| 31 | from .tflite_mapping import optype_to_builtintype |
Raul Farkas | 3b64f06 | 2023-05-16 17:18:31 +0100 | [diff] [blame] | 32 | from .utils import calc_resize_factor |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def _optype_formatter(op_list): |
| 36 | # Convert internal op types to external names |
| 37 | output = map(optype_to_builtintype, op_list) |
| 38 | # Remove UNKNOWNs |
| 39 | output = (x for x in output if x is not BUILTIN_OPERATOR_UNKNOWN) |
| 40 | return list_formatter(output) |
| 41 | |
| 42 | |
| 43 | class TFLiteSupportedOperators: |
| 44 | # Categorised lists of supported operators |
Fredrik Svedberg | 1156317 | 2022-07-06 14:54:12 +0200 | [diff] [blame] | 45 | npu_pre_ops = set( |
| 46 | ( |
| 47 | Op.SplitSliceRead, |
| 48 | Op.Shape, |
| 49 | ) |
| 50 | ) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 51 | convolution_ops = set( |
| 52 | ( |
| 53 | Op.Conv2DBias, |
| 54 | Op.Conv2D, |
| 55 | Op.QuantizedConv2D, |
| 56 | ) |
| 57 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 58 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 59 | transpose_convolution_ops = set((Op.Conv2DBackpropInput,)) |
| 60 | convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops |
| 61 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 62 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 63 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 64 | resizing_ops = Op.op_set(Op.is_resize_op) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 65 | fc_vector_products = set( |
| 66 | ( |
| 67 | Op.QuantizedMatMul, |
| 68 | Op.MatMul, |
| 69 | Op.FullyConnected, |
| 70 | ) |
| 71 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 72 | mac_main_ops = ( |
Fredrik Svedberg | 0ac0804 | 2023-04-11 22:35:04 +0200 | [diff] [blame] | 73 | # LSTM |
| 74 | set((Op.UnidirectionalSequenceLstm,)) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 75 | # conv/depthwiseconv/transposeconv |
| 76 | | convolution_like_ops |
| 77 | # pooling |
| 78 | | pooling_ops |
| 79 | # resizing/upscaling |
| 80 | | resizing_ops |
| 81 | # FC layers |
| 82 | | fc_vector_products |
| 83 | # Mean (converts to depthwise conv) |
| 84 | | set((Op.Mean,)) |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 85 | # ArgMax (converts to depthwise conv and maxpool) |
| 86 | | set((Op.ArgMax,)) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 87 | ) |
| 88 | 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] | 89 | binary_elem_wise_min_max_ops = set( |
| 90 | ( |
| 91 | Op.Minimum, |
| 92 | Op.Maximum, |
| 93 | ) |
| 94 | ) |
| 95 | binary_elem_wise_shift_ops = set( |
| 96 | ( |
| 97 | Op.SHL, |
| 98 | Op.SHR, |
| 99 | ) |
| 100 | ) |
| 101 | binary_elem_wise_add_mul_sub = set( |
| 102 | ( |
| 103 | Op.Add, |
| 104 | Op.Mul, |
| 105 | Op.Sub, |
| 106 | ) |
| 107 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 108 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 109 | |
Johan Alfven | 906c9e8 | 2023-05-25 11:18:50 +0200 | [diff] [blame] | 110 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops | set((Op.SquaredDifference,)) |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 111 | pad_ops = set( |
| 112 | ( |
| 113 | Op.Pad, |
| 114 | Op.MirrorPad, |
| 115 | ) |
| 116 | ) |
| 117 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 118 | supported_int32_tensor_ops = ( |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 119 | set((Op.ReduceSum, Op.CLZ, Op.Shape, Op.ArgMax, Op.Transpose, Op.MirrorPad)) |
Johan Alfven | a8fda88 | 2023-10-28 16:04:46 +0200 | [diff] [blame] | 120 | | binary_elem_wise_add_mul_sub |
| 121 | | binary_elem_wise_shift_ops |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 122 | ) |
| 123 | |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 124 | relu_ops = set( |
| 125 | ( |
| 126 | Op.Relu, |
| 127 | Op.Relu6, |
| 128 | Op.ReluN1To1, |
| 129 | Op.Clip, |
| 130 | ) |
| 131 | ) |
Fredrik Svedberg | 8ddd489 | 2022-08-19 16:06:04 +0200 | [diff] [blame] | 132 | activation_ops = relu_ops | set( |
| 133 | ( |
| 134 | Op.Tanh, |
| 135 | Op.Sigmoid, |
| 136 | Op.Softmax, |
| 137 | Op.HardSwish, |
Fredrik Svedberg | 1cd3949 | 2022-09-23 15:38:03 +0200 | [diff] [blame] | 138 | Op.LeakyRelu, |
Fredrik Svedberg | 8ddd489 | 2022-08-19 16:06:04 +0200 | [diff] [blame] | 139 | Op.Prelu, |
| 140 | ) |
| 141 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 142 | npu_post_ops = ( |
| 143 | # activation functions |
| 144 | activation_ops |
| 145 | # concatenation write direction |
| 146 | | set((Op.ConcatSliceWrite,)) |
| 147 | # Quantization |
| 148 | | set((Op.Quantize,)) |
| 149 | ) |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 150 | split_ops = set( |
| 151 | ( |
| 152 | Op.Split, |
| 153 | Op.SplitV, |
| 154 | Op.StridedSlice, |
| 155 | Op.Slice, |
| 156 | Op.UnpackReshaped, |
| 157 | Op.Unpack, |
| 158 | ) |
| 159 | ) |
| 160 | concat_ops = set( |
| 161 | ( |
| 162 | Op.Concat, |
| 163 | Op.ConcatTFLite, |
| 164 | Op.PackReshaped, |
| 165 | Op.Pack, |
| 166 | ) |
| 167 | ) |
| 168 | memory_only_ops = ( |
| 169 | set( |
| 170 | ( |
| 171 | Op.Reshape, |
| 172 | Op.QuantizedReshape, |
| 173 | Op.Squeeze, |
| 174 | Op.ExpandDims, |
Johan Alfven | a8fda88 | 2023-10-28 16:04:46 +0200 | [diff] [blame] | 175 | Op.Transpose, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 176 | ) |
| 177 | ) |
| 178 | | concat_ops |
| 179 | | split_ops |
| 180 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 181 | 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] | 182 | supported_fused_activations = relu_ops | set( |
| 183 | ( |
| 184 | Op.Tanh, |
| 185 | Op.Sigmoid, |
| 186 | Op.LUT, |
| 187 | ) |
| 188 | ) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 189 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | pad_ops | npu_post_ops | memory_only_ops |
| 190 | # Supported data types |
| 191 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
| 192 | supported_faf_dtypes = set((DataType.uint8, DataType.int8, DataType.int16)) |
| 193 | supported_bias_dtypes = set((DataType.int32, DataType.int64)) |
| 194 | supported_pad_dtypes = set((DataType.int32, DataType.int64)) |
| 195 | # Defined ranges for allowed values: |
| 196 | tens_dim_range = (1, 65535) |
| 197 | stride_range = (1, 3) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 198 | dilated_height_range = (1, 64) |
| 199 | dilated_product_range = (1, 64 * 64) |
| 200 | weights_limit = 127 * 65536 |
| 201 | filter_range = (1, 8) |
| 202 | filter_height_range = (1, 256) |
| 203 | filter_product_range = (1, 256 * 256) |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 204 | mean_reduced_axis_max_size = 64 * 64 |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 205 | mean_kernel_product_int8 = 2 ** (24) |
| 206 | mean_kernel_product_uint8 = 2 ** (23) |
| 207 | mean_kernel_product_int16 = 2 ** (16) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 208 | |
| 209 | def __init__(self): |
| 210 | # Setup the generic constraints. Note: the order matters |
| 211 | self.generic_constraints = [] |
| 212 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dtype) |
| 213 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_int32_ops) |
| 214 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_dimension) |
| 215 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_tens_quant_per_axis) |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 216 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_batch_size) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 217 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf) |
| 218 | self.generic_constraints.append(TFLiteSupportedOperators.constraint_faf_type) |
| 219 | |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 220 | # Setup generic constraint exceptions |
| 221 | self.generic_constraints_exceptions = defaultdict(list) |
Johan Alfven | c1ad80b | 2023-03-31 10:19:23 +0200 | [diff] [blame] | 222 | self.generic_constraints_exceptions[Op.ArgMax].append(TFLiteSupportedOperators.constraint_tens_dtype) |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 223 | self.generic_constraints_exceptions[Op.FullyConnected].append(TFLiteSupportedOperators.constraint_batch_size) |
| 224 | self.generic_constraints_exceptions[Op.Softmax].append(TFLiteSupportedOperators.constraint_batch_size) |
| 225 | self.generic_constraints_exceptions[Op.Reshape].append(TFLiteSupportedOperators.constraint_batch_size) |
| 226 | self.generic_constraints_exceptions[Op.Shape].append(TFLiteSupportedOperators.constraint_batch_size) |
| 227 | self.generic_constraints_exceptions[Op.Squeeze].append(TFLiteSupportedOperators.constraint_batch_size) |
| 228 | for op_type in TFLiteSupportedOperators.split_ops - set((Op.UnpackReshaped,)): |
| 229 | self.generic_constraints_exceptions[op_type].append(TFLiteSupportedOperators.constraint_batch_size) |
| 230 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 231 | # Setup specific constraints. Note: the order matters |
| 232 | self.specific_constraints = defaultdict(list) |
| 233 | |
Raul Farkas | 090f18a | 2023-01-24 16:29:06 +0000 | [diff] [blame] | 234 | # Conv specific ops: |
| 235 | for op_type in TFLiteSupportedOperators.convolution_ops: |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 236 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_width_no_upper_limit) |
Raul Farkas | 090f18a | 2023-01-24 16:29:06 +0000 | [diff] [blame] | 237 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 238 | # Conv-like checks: |
| 239 | for op_type in TFLiteSupportedOperators.convolution_like_ops: |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 240 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_height_range) |
| 241 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_dilated_product_range) |
| 242 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 243 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
| 244 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_limit) |
Johan Alfvén | faa4b78 | 2022-12-07 13:56:17 +0100 | [diff] [blame] | 245 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_shape) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 246 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 247 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 248 | # Transpose Conv specific checks: |
| 249 | for op_type in TFLiteSupportedOperators.transpose_convolution_ops: |
| 250 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_stride) |
| 251 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_same) |
| 252 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_tconv_valid) |
Tim Hall | d3d81b3 | 2022-10-18 19:14:04 +0100 | [diff] [blame] | 253 | # Depthwise Conv specific checks: |
| 254 | for op_type in TFLiteSupportedOperators.depthwise_convolution_ops: |
Raul Farkas | 090f18a | 2023-01-24 16:29:06 +0000 | [diff] [blame] | 255 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depthwise_conv_stride) |
Tim Hall | d3d81b3 | 2022-10-18 19:14:04 +0100 | [diff] [blame] | 256 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_depth_multiplier) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 257 | |
| 258 | # Pooling checks: |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 259 | for op_type in TFLiteSupportedOperators.pooling_ops - TFLiteSupportedOperators.avg_pooling_ops: |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 260 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range) |
| 261 | # AVG pooling specific checks: |
| 262 | for op_type in TFLiteSupportedOperators.avg_pooling_ops: |
Johan Alfven | f49b6e2 | 2023-11-15 10:11:31 +0100 | [diff] [blame] | 263 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_width_no_upper_limit) |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 264 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_stride_range_no_padding) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 265 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_range) |
| 266 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range_valid_pad) |
| 267 | self.specific_constraints[op_type].append( |
| 268 | TFLiteSupportedOperators.constraint_filter_product_range_valid_pad |
| 269 | ) |
| 270 | # MAX pooling specific checks: |
| 271 | for op_type in TFLiteSupportedOperators.max_pooling_ops: |
| 272 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_height_range) |
| 273 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_filter_product_range) |
| 274 | |
| 275 | # Resizing specific checks: |
| 276 | for op_type in TFLiteSupportedOperators.resizing_ops: |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 277 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize) |
| 278 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_size) |
| 279 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_resize_attrs) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 280 | |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 281 | # Resize Bilinear specific checks: |
| 282 | self.specific_constraints[Op.ResizeBilinear].append( |
| 283 | TFLiteSupportedOperators.constraint_resizebi_half_pixel_centers_dims |
| 284 | ) |
| 285 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 286 | # Vector Product specific checks: |
| 287 | for op_type in TFLiteSupportedOperators.fc_vector_products: |
| 288 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_type) |
| 289 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_weights_const) |
Johan Alfvén | faa4b78 | 2022-12-07 13:56:17 +0100 | [diff] [blame] | 290 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_shape) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 291 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_type) |
| 292 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_bias_40bit) |
| 293 | |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 294 | # Element-wise checks |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 295 | # Binary Min/Max specific checks: |
| 296 | for op_type in TFLiteSupportedOperators.binary_elem_wise_min_max_ops: |
| 297 | self.specific_constraints[op_type].append( |
| 298 | TFLiteSupportedOperators.constraint_matching_quantization_parameters |
| 299 | ) |
| 300 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 301 | # Binary Add/Mul/Sub specific checks: |
| 302 | for op_type in TFLiteSupportedOperators.binary_elem_wise_add_mul_sub: |
| 303 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 304 | # Binary Shift specific checks: |
| 305 | for op_type in TFLiteSupportedOperators.binary_elem_wise_shift_ops: |
| 306 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_inputs_int32) |
| 307 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_broadcast_shapes) |
| 308 | |
| 309 | # SHL specific checks: |
| 310 | self.specific_constraints[Op.SHL].append(TFLiteSupportedOperators.constraint_output_int32) |
| 311 | |
| 312 | # CLZ specific checks: |
| 313 | self.specific_constraints[Op.CLZ].append(TFLiteSupportedOperators.constraint_output_int32) |
| 314 | |
| 315 | # StridedSlice specific checks: |
| 316 | self.specific_constraints[Op.StridedSlice].append( |
| 317 | TFLiteSupportedOperators.constraint_stridedslice_stride_values |
| 318 | ) |
Rickard Bolin | b37a81b | 2023-09-29 12:48:29 +0000 | [diff] [blame] | 319 | self.specific_constraints[Op.StridedSlice].append(TFLiteSupportedOperators.constraint_stridedslice_offset_false) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 320 | |
| 321 | # Pad specific checks: |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 322 | for op_type in TFLiteSupportedOperators.pad_ops: |
| 323 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_pad_shape) |
| 324 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_padding_dimensions) |
| 325 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_pad_type) |
| 326 | |
| 327 | # Mirror pad specific checks: |
| 328 | self.specific_constraints[Op.MirrorPad].append(TFLiteSupportedOperators.constraint_mirror_pad_padding_values) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 329 | |
| 330 | # Mean specific checks: |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 331 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_height_width_product) |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 332 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_width) |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 333 | self.specific_constraints[Op.Mean].append(TFLiteSupportedOperators.constraint_mean_depth) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 334 | |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 335 | # Reshape specific checks: |
| 336 | self.specific_constraints[Op.Reshape].append(TFLiteSupportedOperators.constraint_reshape_shape_constant) |
| 337 | |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 338 | # ArgMax specific checks: |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 339 | self.specific_constraints[Op.ArgMax].append(TFLiteSupportedOperators.constraint_argmax_axis) |
| 340 | self.specific_constraints[Op.ArgMax].append(TFLiteSupportedOperators.constraint_argmax_depth) |
| 341 | |
Fredrik Svedberg | 0ac0804 | 2023-04-11 22:35:04 +0200 | [diff] [blame] | 342 | # UnidirectionalSequenceLstm specific checks: |
| 343 | op_type = Op.UnidirectionalSequenceLstm |
| 344 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_cifg) |
| 345 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_peep_hole) |
| 346 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_projection) |
| 347 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_no_normalisation) |
| 348 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_weights) |
William Isaksson | 2f9b687 | 2023-07-17 13:03:09 +0000 | [diff] [blame] | 349 | self.specific_constraints[op_type].append(TFLiteSupportedOperators.constraint_lstm_weight_dimensions) |
Fredrik Svedberg | 0ac0804 | 2023-04-11 22:35:04 +0200 | [diff] [blame] | 350 | |
Johan Alfven | 8e525ca | 2023-05-07 13:12:37 +0200 | [diff] [blame] | 351 | # Rsqrt specific checks |
| 352 | self.specific_constraints[Op.Rsqrt].append(TFLiteSupportedOperators.constraint_rsqrt_input_int8) |
| 353 | |
Johan Alfven | 85b7790 | 2023-06-15 09:24:01 +0200 | [diff] [blame] | 354 | # Slice specific checks: |
| 355 | self.specific_constraints[Op.Slice].append(TFLiteSupportedOperators.constraint_slice_inputs_const) |
| 356 | |
Johan Alfven | a8fda88 | 2023-10-28 16:04:46 +0200 | [diff] [blame] | 357 | # Transpose specific checks: |
| 358 | self.specific_constraints[Op.Transpose].append(TFLiteSupportedOperators.constraint_transpose) |
| 359 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 360 | def is_operator_supported(self, op): |
| 361 | ext_type = optype_to_builtintype(op.type) |
| 362 | if op.type not in TFLiteSupportedOperators.supported_operators: |
| 363 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
| 364 | print(f"Info: {ext_type} '{op.name}' is a CPU only op") |
| 365 | return False |
| 366 | |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 367 | op_exceptions = self.generic_constraints_exceptions[op.type] |
| 368 | generic_constraints = [constraint for constraint in self.generic_constraints if constraint not in op_exceptions] |
| 369 | |
| 370 | for constraint in generic_constraints + self.specific_constraints[op.type]: |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 371 | valid, extra = constraint(op) |
| 372 | if not valid: |
| 373 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead") |
| 374 | print(f" - {constraint.__doc__}") |
| 375 | if extra: |
| 376 | print(f" {extra}") |
| 377 | return False |
| 378 | |
| 379 | return True |
| 380 | |
| 381 | @classmethod |
| 382 | @docstring_format_args([list_formatter(supported_op_dtypes)]) |
| 383 | def constraint_tens_dtype(cls, op): |
| 384 | "Tensors must be of type: {}" |
| 385 | valid = True |
| 386 | extra = [] |
| 387 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 388 | if not tensors: |
| 389 | tensors = [tens for tens in op.inputs if tens] |
| 390 | for tens in tensors: |
| 391 | if tens.dtype not in cls.supported_op_dtypes: |
| 392 | valid = False |
| 393 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
| 394 | return valid, ", ".join(extra) |
| 395 | |
| 396 | @classmethod |
| 397 | @docstring_format_args([_optype_formatter(supported_int32_tensor_ops)]) |
| 398 | def constraint_tens_int32_ops(cls, op): |
| 399 | "Tensors which are int32 are only valid when op type is: {}" |
| 400 | valid = True |
| 401 | extra = [] |
| 402 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 403 | if not tensors: |
| 404 | tensors = [tens for tens in op.inputs if tens] |
| 405 | for tens in tensors: |
| 406 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 407 | valid = False |
| 408 | extra.append(tens.name) |
| 409 | extra = ", ".join(extra) |
| 410 | return valid, f"Op has int32 tensor(s): {extra}" |
| 411 | |
| 412 | @classmethod |
| 413 | @docstring_format_args(tens_dim_range) |
| 414 | def constraint_tens_dimension(cls, op): |
| 415 | "Tensor dimensions must be in the range [{}, {}]" |
| 416 | tens_min, tens_max = cls.tens_dim_range |
| 417 | valid = True |
| 418 | extra = [] |
| 419 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 420 | if not tensors: |
| 421 | tensors = [tens for tens in op.inputs if tens] |
| 422 | for tens in tensors: |
| 423 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 424 | valid = False |
| 425 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 426 | return valid, ", ".join(extra) |
| 427 | |
| 428 | @classmethod |
| 429 | @docstring_format_args([_optype_formatter(per_axis_quant_ops)]) |
| 430 | def constraint_tens_quant_per_axis(cls, op): |
| 431 | "Per-axis quantization is only supported for the following op types: {}" |
| 432 | valid = True |
| 433 | extra = [] |
| 434 | if op.type not in cls.per_axis_quant_ops: |
| 435 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 436 | for tens in tensors: |
Fredrik Svedberg | 1156317 | 2022-07-06 14:54:12 +0200 | [diff] [blame] | 437 | if tens.quantization and tens.quantization.is_per_axis(): |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 438 | valid = False |
| 439 | extra.append(tens.name) |
| 440 | return valid, "The following tensor(s) have per-axis quantization parameters: " + ", ".join(extra) |
| 441 | |
| 442 | @classmethod |
| 443 | @docstring_format_args([_optype_formatter(supported_fused_activations)]) |
| 444 | def constraint_faf(cls, op): |
| 445 | "The fused activation function (if present) must be one of type: {}" |
| 446 | if op.activation is None: |
| 447 | res = True, "Op has no fused activation function" |
| 448 | else: |
| 449 | faf = op.activation.op_type |
| 450 | valid = faf in cls.supported_fused_activations |
| 451 | res = valid, f"Op has its fused activation function as: {faf}" |
| 452 | return res |
| 453 | |
| 454 | @classmethod |
| 455 | @docstring_format_args([list_formatter(supported_faf_dtypes)]) |
| 456 | def constraint_faf_type(cls, op): |
| 457 | "If a fused activation function is present, the Output tensor must be one of type: {}" |
| 458 | if op.activation is None: |
| 459 | res = True, "Op has no fused activation function" |
| 460 | else: |
| 461 | valid = op.ofm.dtype in cls.supported_faf_dtypes |
| 462 | ext_type = optype_to_builtintype(op.activation.op_type) |
| 463 | res = valid, f"Op has fused activation function {ext_type}, and Output tensor data type: {op.ofm.dtype}" |
| 464 | return res |
| 465 | |
| 466 | @classmethod |
| 467 | @docstring_format_args(stride_range) |
| 468 | def constraint_stride_range(cls, op): |
| 469 | "Stride values for both width and height must be in the range [{}, {}]" |
| 470 | w, h = op.get_kernel_stride() |
| 471 | stride_min, stride_max = cls.stride_range |
| 472 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
| 473 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 474 | |
| 475 | @classmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 476 | @docstring_format_args(dilated_height_range) |
| 477 | def constraint_dilated_height_range(cls, op): |
| 478 | "Dilated kernel height must be in the range [{}, {}]" |
| 479 | h = op.kernel.area_height() |
| 480 | dilated_height_min, dilated_height_max = cls.dilated_height_range |
| 481 | valid = dilated_height_min <= h <= dilated_height_max |
| 482 | return valid, f"Op has dilated kernel height as: {h}" |
| 483 | |
| 484 | @classmethod |
| 485 | @docstring_format_args(dilated_product_range) |
| 486 | def constraint_dilated_product_range(cls, op): |
| 487 | "Product of dilated kernel width and height must be in the range [{}, {}]" |
| 488 | product = op.kernel.area_width() * op.kernel.area_height() |
| 489 | dilated_product_min, dilated_product_max = cls.dilated_product_range |
| 490 | valid = dilated_product_min <= product <= dilated_product_max |
| 491 | return valid, f"Op has product of dilated kernel width and height as: {product}" |
| 492 | |
| 493 | @staticmethod |
| 494 | def constraint_weights_type(op): |
| 495 | "Weight tensor must be 8-bit" |
| 496 | weights = op.weights |
| 497 | valid = weights.element_size() == 1 |
| 498 | return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit" |
| 499 | |
| 500 | @staticmethod |
| 501 | def constraint_weights_const(op): |
| 502 | "Weight tensor must be constant" |
| 503 | weights = op.weights |
| 504 | valid = weights.values is not None |
| 505 | return valid, f"Tensor '{weights.name}' has non-constant values" |
| 506 | |
| 507 | @classmethod |
| 508 | @docstring_format_args([weights_limit]) |
| 509 | def constraint_weights_limit(cls, op): |
| 510 | "The sum of the weights cannot exceed {}" |
| 511 | weights = op.weights |
| 512 | values = weights.values.astype(np.int64) - weights.quantization.zero_point |
| 513 | limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2))) |
| 514 | valid = limit <= cls.weights_limit |
| 515 | return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}" |
| 516 | |
Johan Alfvén | faa4b78 | 2022-12-07 13:56:17 +0100 | [diff] [blame] | 517 | @staticmethod |
| 518 | def constraint_bias_shape(op): |
| 519 | "Optional Bias tensor must be of shape: 1D" |
| 520 | bias = op.bias |
| 521 | if bias: |
| 522 | valid = len(bias.shape) == 1 |
| 523 | return valid, f"Tensor '{bias.name}' has shape: {bias.shape}" |
| 524 | return True, "Op has no bias tensor" |
| 525 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 526 | @classmethod |
| 527 | @docstring_format_args([list_formatter(supported_bias_dtypes)]) |
| 528 | def constraint_bias_type(cls, op): |
| 529 | "Optional Bias tensor must be of type: {}" |
| 530 | bias = op.bias |
| 531 | if bias: |
| 532 | valid = bias.dtype in cls.supported_bias_dtypes |
| 533 | return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}" |
| 534 | return True, "Op has no bias tensor" |
| 535 | |
| 536 | @staticmethod |
| 537 | def constraint_bias_40bit(op): |
| 538 | "Optional Bias tensor values must fit within 40-bits" |
| 539 | bias = op.bias |
| 540 | 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] | 541 | valid = all(len(bin(value)[2:]) <= 40 for value in bias.values) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 542 | return valid, f"Tensor '{bias.name}' has values larger than 40-bits" |
| 543 | return True, "Op has no bias tensor, or it fits in 40-bit" |
| 544 | |
| 545 | @staticmethod |
| 546 | def constraint_batch_size(op): |
| 547 | "IFM Tensor batch size must be 1" |
Fredrik Svedberg | 88d5b12 | 2022-09-16 16:24:55 +0200 | [diff] [blame] | 548 | valid = True |
| 549 | extra = [] |
| 550 | for tens in (op.ifm, op.ifm2): |
| 551 | if tens is not None: |
| 552 | batch_size = full_shape(4, tens.shape, 1)[0] |
| 553 | if batch_size != 1: |
| 554 | valid = False |
| 555 | extra.append(f"Tensor '{tens.name}' has batch size: {batch_size}") |
| 556 | extra = "\n ".join(extra) |
| 557 | return valid, extra |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 558 | |
| 559 | @staticmethod |
| 560 | def constraint_depth_multiplier(op): |
| 561 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 562 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 563 | if depth_multiplier > 1: |
| 564 | ifm_channels = op.ifm.shape[3] |
| 565 | ofm_channels = op.ofm.shape[3] |
| 566 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 567 | extra = ( |
| 568 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 569 | f" and depth_multiplier={depth_multiplier}" |
| 570 | ) |
| 571 | return valid, extra |
| 572 | return True, "Op has depth_multiplier=1" |
| 573 | |
| 574 | @staticmethod |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 575 | def constraint_stride_width_no_upper_limit(op): |
Johan Alfven | afb56ae | 2023-10-27 13:08:21 +0200 | [diff] [blame] | 576 | """Strides must fulfil the following criteria: |
| 577 | - Stride h must be between 1 and 3 when ofm height is greater than 1 |
| 578 | - Stride w must be between 1 and 3 when ofm height is greater than 1 or |
| 579 | stride w must be divisible by 2 or 3 and ifm width must be divisible |
| 580 | by stride_w/2 or stride_w/3""" |
| 581 | |
| 582 | stride_w, stride_h = op.get_kernel_stride() |
Raul Farkas | 10d6b3b | 2023-01-30 12:58:46 +0000 | [diff] [blame] | 583 | stride_min = 1 |
Raul Farkas | 59b9ab9 | 2023-02-09 10:03:27 +0000 | [diff] [blame] | 584 | stride_max_h = 3 |
Raul Farkas | 3b64f06 | 2023-05-16 17:18:31 +0100 | [diff] [blame] | 585 | ifm_width = op.ifm.shape[2] |
Johan Alfven | afb56ae | 2023-10-27 13:08:21 +0200 | [diff] [blame] | 586 | ofm_height = op.ofm.shape[1] |
| 587 | ofm_width = op.ofm.shape[2] |
| 588 | |
| 589 | stride_h_valid = ofm_height == 1 or stride_min <= stride_h <= stride_max_h |
| 590 | |
| 591 | _, optimized_stride = calc_resize_factor(ifm_width, stride_w) if stride_w > 1 else (1, stride_w) |
Raul Farkas | 3b64f06 | 2023-05-16 17:18:31 +0100 | [diff] [blame] | 592 | # Optimized stride indicates the final Conv2D stride width after all optimizations are performed |
| 593 | can_optimize_stride_width_gt_3 = optimized_stride <= 3 |
Raul Farkas | 3b64f06 | 2023-05-16 17:18:31 +0100 | [diff] [blame] | 594 | |
Johan Alfven | afb56ae | 2023-10-27 13:08:21 +0200 | [diff] [blame] | 595 | stride_w_valid = ofm_width == 1 or ((stride_min <= stride_w) and can_optimize_stride_width_gt_3) |
| 596 | |
| 597 | return ( |
| 598 | stride_h_valid and stride_w_valid, |
| 599 | f"Op has stride WxH as: {stride_w}x{stride_h}, ifm shape as: {op.ifm.shape}, ofm shape as: {op.ofm.shape}", |
| 600 | ) |
Raul Farkas | 090f18a | 2023-01-24 16:29:06 +0000 | [diff] [blame] | 601 | |
| 602 | @staticmethod |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 603 | def constraint_stride_range_no_padding(op): |
| 604 | """Stride width must be greater than or equal to 1. |
| 605 | For stride width greater than 3, valid padding needs to be used.""" |
| 606 | w, _ = op.get_kernel_stride() |
| 607 | valid, message = TFLiteSupportedOperators.constraint_stride_width_no_upper_limit(op) |
| 608 | padding = op.attrs.get("padding", None) |
| 609 | is_optimized_with_valid_padding = padding in (None, Padding.VALID) or w <= 3 |
| 610 | valid = valid and is_optimized_with_valid_padding |
| 611 | return valid, f"{message}, padding: {padding}" |
| 612 | |
| 613 | @staticmethod |
Raul Farkas | 090f18a | 2023-01-24 16:29:06 +0000 | [diff] [blame] | 614 | def constraint_depthwise_conv_stride(op): |
| 615 | "Stride values for both width and height must be between 1 and 3" |
| 616 | w, h = op.get_kernel_stride() |
| 617 | stride_min, stride_max = 1, 3 |
| 618 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
| 619 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 620 | |
| 621 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 622 | def constraint_tconv_stride(op): |
Johan Alfven | c0bb868 | 2023-09-04 17:18:33 +0200 | [diff] [blame] | 623 | """Stride values for width and height must match one of the following criteria: |
| 624 | Stride values WxH must be 1x1 or 2x2 |
| 625 | Stride WxH 2x1 supported if ifm height and kernel height = 1""" |
| 626 | s_w = op.kernel.stride.x |
| 627 | s_h = op.kernel.stride.y |
| 628 | k_h = op.kernel.height |
| 629 | i_h = op.ifm.shape[1] |
| 630 | valid = False |
| 631 | if s_w == 1 and s_h == 1: |
| 632 | valid = True |
| 633 | |
| 634 | if s_w == 2 and s_h == 2: |
| 635 | valid = True |
| 636 | |
| 637 | if s_w == 2 and s_h == 1 and i_h == 1 and k_h == 1: |
| 638 | valid = True |
| 639 | |
| 640 | return valid, f"Op has ifm_height={i_h}, kernel_height={k_h} and stride WxH as {s_w}x{s_h}" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 641 | |
| 642 | @staticmethod |
| 643 | def constraint_tconv_same(op): |
| 644 | "SAME padding: OFM dimensions must equal IFM dimensions multiplied by stride" |
| 645 | if op.attrs["padding"] == Padding.SAME: |
| 646 | w = op.kernel.stride.x |
| 647 | h = op.kernel.stride.y |
| 648 | ifm_shape = op.ifm.shape |
| 649 | ofm_shape = op.ofm.shape |
| 650 | valid = (ofm_shape[1] == (ifm_shape[1] * h)) and (ofm_shape[2] == (ifm_shape[2] * w)) |
| 651 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and stride WxH as {w}x{h}" |
| 652 | return True, "Op has padding=VALID" |
| 653 | |
| 654 | @staticmethod |
| 655 | def constraint_tconv_valid(op): |
| 656 | """VALID padding: OFM dimensions must equal IFM dimensions multiplied by stride, |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 657 | minus difference between kernel size and stride""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 658 | if op.attrs["padding"] == Padding.VALID: |
| 659 | s_w = op.kernel.stride.x |
| 660 | s_h = op.kernel.stride.y |
| 661 | k_w = op.kernel.width |
| 662 | k_h = op.kernel.height |
| 663 | ifm_shape = op.ifm.shape |
| 664 | ofm_shape = op.ofm.shape |
| 665 | height_check = ofm_shape[1] == (ifm_shape[1] * s_h + max(k_h - s_h, 0)) |
| 666 | width_check = ofm_shape[2] == (ifm_shape[2] * s_w + max(k_w - s_w, 0)) |
| 667 | valid = height_check and width_check |
| 668 | extra = ( |
| 669 | f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape}," |
| 670 | f" stride WxH as {s_w}x{s_h} and kernel WxH as {k_w}x{k_h}" |
| 671 | ) |
| 672 | return valid, extra |
| 673 | return True, "Op has padding=SAME" |
| 674 | |
| 675 | @classmethod |
| 676 | @docstring_format_args(filter_range) |
| 677 | def constraint_filter_range(cls, op): |
| 678 | "Kernel filter values for both width and height must be in the range [{}, {}]" |
| 679 | if op.attrs["padding"] == Padding.SAME: |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 680 | sw, _ = op.get_kernel_stride() |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 681 | w = op.kernel.width |
| 682 | h = op.kernel.height |
| 683 | filter_min, filter_max = cls.filter_range |
Raul Farkas | 3e7157b | 2023-05-09 09:09:17 +0100 | [diff] [blame] | 684 | valid = ((filter_min <= w <= filter_max) or sw == w) and (filter_min <= h <= filter_max) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 685 | return valid, f"Op has kernel filter WxH as: {w}x{h}" |
| 686 | return True, "Op has padding=VALID" |
| 687 | |
| 688 | @classmethod |
| 689 | @docstring_format_args(filter_height_range) |
| 690 | def constraint_filter_height_range(cls, op): |
| 691 | "Kernel filter height must be in the range [{}, {}]" |
| 692 | h = op.kernel.height |
| 693 | filter_height_min, filter_height_max = cls.filter_height_range |
| 694 | valid = filter_height_min <= h <= filter_height_max |
| 695 | return valid, f"Op has kernel filter height as: {h}" |
| 696 | |
| 697 | @classmethod |
| 698 | @docstring_format_args(filter_product_range) |
| 699 | def constraint_filter_product_range(cls, op): |
| 700 | "Product of kernel filter width and height must be in the range [{}, {}]" |
| 701 | product = op.kernel.elements_wh() |
| 702 | filter_product_min, filter_product_max = cls.filter_product_range |
| 703 | valid = filter_product_min <= product <= filter_product_max |
| 704 | return valid, f"Op has product of kernel filter width and height as: {product}" |
| 705 | |
| 706 | @staticmethod |
| 707 | @docstring_format_args(filter_height_range) |
| 708 | def constraint_filter_height_range_valid_pad(op): |
| 709 | "VALID padding: Kernel filter height must be in the range [{}, {}]" |
| 710 | if op.attrs["padding"] == Padding.VALID: |
| 711 | return TFLiteSupportedOperators.constraint_filter_height_range(op) |
| 712 | return True, "Op has padding=SAME" |
| 713 | |
| 714 | @staticmethod |
| 715 | @docstring_format_args(filter_product_range) |
| 716 | def constraint_filter_product_range_valid_pad(op): |
| 717 | "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]" |
| 718 | if op.attrs["padding"] == Padding.VALID: |
| 719 | return TFLiteSupportedOperators.constraint_filter_product_range(op) |
| 720 | return True, "Op has padding=SAME" |
| 721 | |
| 722 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 723 | def constraint_resize(op): |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 724 | """The width and height of the IFM and OFM must match one of the following criteria: |
| 725 | IFM W and H must both be 1 |
| 726 | IFM must match OFM |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 727 | W and H scaling 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 |
| 728 | W and H scaling 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] | 729 | # Easier to start with False condition as very few cases result in a supported resize |
| 730 | valid = False |
| 731 | ifm_shape = op.ifm.shape |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 732 | ifm_shape_h = ifm_shape[1] |
| 733 | ifm_shape_w = ifm_shape[2] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 734 | ofm_shape = op.ofm.shape |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 735 | ofm_shape_h = ofm_shape[1] |
| 736 | ofm_shape_w = ofm_shape[2] |
| 737 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 738 | align_corners = op.attrs.get("align_corners", False) |
| 739 | if len(ifm_shape) == 4: |
| 740 | # 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] | 741 | 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] | 742 | valid = True |
| 743 | else: |
Rickard Bolin | e546def | 2022-01-25 15:45:00 +0000 | [diff] [blame] | 744 | # Valid if OFM is 2/4/8x IFM (-1 for align corners) |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 745 | if align_corners: |
| 746 | h_upscale_factor = (ofm_shape_h - 1) / (ifm_shape_h - 1) |
| 747 | w_upscale_factor = (ofm_shape_w - 1) / (ifm_shape_w - 1) |
| 748 | else: |
| 749 | h_upscale_factor = ofm_shape_h / ifm_shape_h |
| 750 | w_upscale_factor = ofm_shape_w / ifm_shape_w |
Rickard Bolin | e546def | 2022-01-25 15:45:00 +0000 | [diff] [blame] | 751 | |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 752 | # could use either height or width. save as int because it is more usable later in graph optimiser |
| 753 | op.attrs["upscale_factor"] = int(h_upscale_factor) |
| 754 | 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] | 755 | |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 756 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}" |
| 757 | |
| 758 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 759 | def constraint_resize_size(op): |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 760 | "The size tensor must match the output tensor shape" |
| 761 | valid = False |
| 762 | ofm_shape = op.ofm.shape |
| 763 | size_h, size_w = None, None |
| 764 | # check that the size tensor (the second input) exists, is not none, and has the correct values |
| 765 | if len(op.inputs) == 2 and op.inputs[1] is not None and len(op.inputs[1].values) == 2: |
| 766 | size_h, size_w = op.inputs[1].values |
| 767 | # check size and output size match |
| 768 | if size_h == ofm_shape[1] and size_w == ofm_shape[2]: |
| 769 | valid = True |
| 770 | |
| 771 | return valid, f"Op has size={size_h}x{size_w} and ofm_shape={ofm_shape}." |
| 772 | |
| 773 | @staticmethod |
Tim Hall | 885033b | 2022-07-21 11:46:03 +0100 | [diff] [blame] | 774 | def constraint_resize_attrs(op): |
Tim Hall | 47c7636 | 2022-07-18 21:26:47 +0100 | [diff] [blame] | 775 | "Both align_corners and half_pixel_centers can't be True" |
| 776 | valid = True |
| 777 | align_corners = op.attrs.get("align_corners", False) |
| 778 | half_pixel_centers = op.attrs.get("half_pixel_centers", False) |
| 779 | |
| 780 | if align_corners and half_pixel_centers: |
| 781 | valid = False |
| 782 | return valid, "Op has both align_corners and half_pixel_centers set to True." |
| 783 | |
| 784 | @staticmethod |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 785 | def constraint_resizebi_half_pixel_centers_dims(op): |
Tim Hall | fd27111 | 2023-05-17 13:19:12 +0100 | [diff] [blame] | 786 | """For half_pixel_centers the width and height of the IFM and OFM must match one of the following criteria: |
Alexander Hansson | e8fc214 | 2023-05-11 16:01:39 +0000 | [diff] [blame] | 787 | IFM W and H are both 1 |
| 788 | OFM W and H is 2x IFM W and H""" |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 789 | half_pixel_centers = op.attrs.get("half_pixel_centers", False) |
| 790 | if not half_pixel_centers: |
| 791 | valid = True |
| 792 | elif len(op.ifm.shape) >= 3: |
| 793 | ifm_h, ifm_w = op.ifm.shape[-3:-1] |
| 794 | ofm_h, ofm_w = op.ofm.shape[-3:-1] |
Alexander Hansson | e8fc214 | 2023-05-11 16:01:39 +0000 | [diff] [blame] | 795 | if ifm_h == 1 and ifm_w == 1: |
| 796 | valid = True |
| 797 | else: |
| 798 | valid = ofm_h / ifm_h == 2 and ofm_w / ifm_w == 2 |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 799 | else: |
| 800 | # Unexpected IFM shape |
| 801 | valid = False |
| 802 | return ( |
| 803 | valid, |
| 804 | f"Op has ifm_shape={op.ifm.shape}, ofm_shape={op.ofm.shape} and half_pixel_centers={half_pixel_centers}", |
| 805 | ) |
erik.andersson@arm.com | ba2555e | 2021-10-28 14:08:52 +0200 | [diff] [blame] | 806 | |
| 807 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 808 | def constraint_pad_shape(op): |
| 809 | "The padding tensor must have the shape [3,2] or [4,2]" |
| 810 | valid = op.inputs[1].shape in ([3, 2], [4, 2]) |
| 811 | return valid, f"The pad tensor has the shape: {op.inputs[1].shape}" |
| 812 | |
| 813 | @classmethod |
| 814 | @docstring_format_args([list_formatter(supported_pad_dtypes)]) |
| 815 | def constraint_pad_type(cls, op): |
| 816 | "Pad tensor must be of type: {}" |
| 817 | pad_tensor = op.inputs[1] |
| 818 | valid = pad_tensor.dtype in cls.supported_pad_dtypes |
| 819 | return valid, f"Tensor '{pad_tensor.name}' has data type: {pad_tensor.dtype}" |
| 820 | |
| 821 | @staticmethod |
| 822 | def constraint_padding_dimensions(op): |
| 823 | "The pad tensor can only pad width and height" |
| 824 | pad_tensor = op.inputs[1].values |
| 825 | |
| 826 | valid = sum(pad_tensor[-1, :]) == 0 |
| 827 | if valid and len(pad_tensor) > 3: |
| 828 | valid = sum(pad_tensor[0, :]) == 0 |
| 829 | return valid, f"First dimension padding: {pad_tensor[0,:]}, last dimension padding: {pad_tensor[-1,:]}" |
| 830 | |
| 831 | @staticmethod |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 832 | def constraint_mirror_pad_padding_values(op): |
| 833 | "The number of pad values for each direction must not be larger than the ifm size in that dimension" |
Rickard Bolin | 646314e | 2024-01-31 08:42:00 +0000 | [diff] [blame] | 834 | valid = True |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 835 | pad_tensor = op.inputs[1].values |
| 836 | ifm_shape = op.inputs[0].shape |
Rickard Bolin | 646314e | 2024-01-31 08:42:00 +0000 | [diff] [blame] | 837 | for dim_padding, ifm_dim_shape in zip(pad_tensor, ifm_shape): |
| 838 | if any([pad_val > ifm_dim_shape for pad_val in dim_padding]): |
Rickard Bolin | fdbb072 | 2023-09-05 11:38:19 +0000 | [diff] [blame] | 839 | valid = False |
| 840 | return valid, f"IFM shape: {ifm_shape}, number of padding values per dimension: {pad_tensor}" |
| 841 | |
| 842 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 843 | def constraint_stridedslice_stride_values(op): |
Rickard Bolin | be78a05 | 2024-01-31 12:05:11 +0000 | [diff] [blame] | 844 | "Batch and channel stride values must be 1" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 845 | strides = op.inputs[3] |
Rickard Bolin | be78a05 | 2024-01-31 12:05:11 +0000 | [diff] [blame] | 846 | s_c = strides.values[-1] |
| 847 | s_n = strides.values[0] if len(strides.values) > 3 else 1 |
| 848 | return s_n == s_c == 1, f"Op has strides values {strides.values}" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 849 | |
| 850 | @staticmethod |
Rickard Bolin | b37a81b | 2023-09-29 12:48:29 +0000 | [diff] [blame] | 851 | def constraint_stridedslice_offset_false(op): |
| 852 | "Offset attribute must be False" |
| 853 | offset = op.attrs.get("offset", False) |
| 854 | valid = offset is False |
| 855 | return valid, f"Op has offset={offset}" |
| 856 | |
| 857 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 858 | def constraint_inputs_int32(op): |
| 859 | "Both Input data types must be int32" |
| 860 | ifm_dtype = op.ifm.dtype |
| 861 | ifm2_dtype = op.ifm2.dtype |
| 862 | valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32) |
| 863 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
| 864 | |
| 865 | @staticmethod |
| 866 | def constraint_output_int32(op): |
| 867 | "OFM must be int32" |
| 868 | ofm_dtype = op.ofm.dtype |
| 869 | valid = ofm_dtype == DataType.int32 |
| 870 | return valid, f"Op has ofm_dtype={ofm_dtype}" |
| 871 | |
| 872 | @staticmethod |
| 873 | def constraint_matching_quantization_parameters(op): |
| 874 | "Both Input quantization parameters must match OFM quantization parameters" |
| 875 | valid = True |
| 876 | extra = [] |
| 877 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm): |
| 878 | valid = False |
| 879 | extra.append(op.ifm.name) |
| 880 | if op.ifm2 is not None and not check_quantized_tens_scaling_equal(op.ofm, op.ifm2): |
| 881 | valid = False |
| 882 | extra.append(op.ifm2.name) |
| 883 | extra = ", ".join(extra) |
| 884 | return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}" |
| 885 | |
| 886 | @staticmethod |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 887 | def constraint_broadcast_shapes(op): |
| 888 | "Broadcasting is only allowed for rank indices with dimension 1, from either IFM1 or IFM2" |
| 889 | ifm_shape = op.ifm.shape |
| 890 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 891 | ofm_shape = op.ofm.shape |
| 892 | valid = True |
| 893 | if ifm_shape is not None and ifm2_shape is not None: |
| 894 | # align trailing dimensions |
| 895 | size = min(len(ifm_shape), len(ifm2_shape)) |
| 896 | for i, i2, o in zip(ifm_shape[-size:], ifm2_shape[-size:], ofm_shape[-size:]): |
| 897 | mi = max(i, i2) |
| 898 | # Input dimensions should match or one should be of dimension 1 |
| 899 | # Output dimension should match the largest input dimension, together |
| 900 | # with constraint_match_either_shapes ensures broadcast from only one input |
| 901 | if not (i == i2 or i == 1 or i2 == 1) or o != mi: |
| 902 | valid = False |
| 903 | break |
| 904 | |
| 905 | return valid, f"Op has ifm_shape={ifm_shape} and ifm2_shape={ifm2_shape}" |
| 906 | |
| 907 | @classmethod |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 908 | @docstring_format_args([mean_kernel_product_int8, mean_kernel_product_uint8, mean_kernel_product_int16]) |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 909 | def constraint_mean_height_width_product(cls, op): |
Alexander Hansson | 1d5e859 | 2023-06-27 12:36:25 +0000 | [diff] [blame] | 910 | """Product of reduced axes must be no greater than: |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 911 | - {} for signed 8-bit inputs. |
| 912 | - {} for unsigned 8-bit inputs. |
| 913 | - {} for signed 16-bit inputs.""" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 914 | shape = op.inputs[0].shape |
Alexander Hansson | 1d5e859 | 2023-06-27 12:36:25 +0000 | [diff] [blame] | 915 | if op.inputs[1].shape == []: |
| 916 | axis = [int(op.inputs[1].values)] |
| 917 | else: |
| 918 | axis = list(op.inputs[1].values) |
| 919 | |
| 920 | # compute the product of the shape of all reduced axes |
| 921 | axis_shapes = [shape[ax] for ax in axis] |
| 922 | prod = np.prod(axis_shapes) |
| 923 | |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 924 | if op.ifm.dtype == DataType.int16: |
| 925 | max_prod = cls.mean_kernel_product_int16 |
| 926 | datatype = "int16" |
| 927 | elif op.ifm.dtype == DataType.uint8: |
| 928 | max_prod = cls.mean_kernel_product_uint8 |
| 929 | datatype = "uint8" |
| 930 | else: |
| 931 | max_prod = cls.mean_kernel_product_int8 |
| 932 | datatype = "int8" |
Alexander Hansson | 1d5e859 | 2023-06-27 12:36:25 +0000 | [diff] [blame] | 933 | return prod <= max_prod, f"Datatype is {datatype}, product of axes is {prod}" |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 934 | |
| 935 | @classmethod |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 936 | @docstring_format_args([mean_reduced_axis_max_size]) |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 937 | def constraint_mean_width(cls, op): |
Alexander Hansson | 1d5e859 | 2023-06-27 12:36:25 +0000 | [diff] [blame] | 938 | """If Width axis is reduced its shape must be no greater than {}.""" |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 939 | shape = op.inputs[0].shape |
| 940 | hi = 0 if len(shape) < 4 else 1 |
| 941 | h, w = shape[hi : hi + 2] |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 942 | max_width = cls.mean_reduced_axis_max_size |
Alexander Hansson | 90c34b5 | 2023-05-31 15:03:03 +0000 | [diff] [blame] | 943 | return w <= max_width, f"Width is {w}" |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 944 | |
Alexander Hansson | da8741a | 2023-06-30 15:41:13 +0000 | [diff] [blame] | 945 | @classmethod |
| 946 | @docstring_format_args([mean_reduced_axis_max_size]) |
| 947 | def constraint_mean_depth(cls, op): |
| 948 | """If Depth axis is reduced its shape must be no greater than {}.""" |
| 949 | max_depth = cls.mean_reduced_axis_max_size |
| 950 | shape = op.inputs[0].shape |
| 951 | |
| 952 | if op.inputs[1].shape == []: |
| 953 | axis = [int(op.inputs[1].values)] |
| 954 | else: |
| 955 | axis = list(op.inputs[1].values) |
| 956 | |
| 957 | depth_idx = len(shape) - 1 |
| 958 | |
| 959 | supported = True |
| 960 | if depth_idx in axis and shape[-1] > max_depth: |
| 961 | supported = False |
| 962 | |
| 963 | return supported, f"Depth is {shape[-1]}, shape is {shape}, axis is {axis}" |
| 964 | |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 965 | @staticmethod |
| 966 | def constraint_reshape_shape_constant(op): |
| 967 | "Shape must be constant" |
| 968 | valid = True |
| 969 | extra = [] |
| 970 | |
Tim Hall | 2180a17 | 2023-03-10 18:11:34 +0000 | [diff] [blame] | 971 | # if a reshape tensor is specified then it must be constant |
| 972 | if len(op.inputs) > 1: |
| 973 | reshape_tens = op.inputs[1] |
| 974 | if reshape_tens is not None: |
| 975 | # constant inputs have either no driving operator or a const one |
| 976 | # create a list of non-constant inputs |
| 977 | if not (len(reshape_tens.ops) == 0 or reshape_tens.ops[0].type == Op.Const): |
| 978 | valid = False |
| 979 | extra.append(reshape_tens.name) |
Tim Hall | 3584a9c | 2021-11-18 22:05:17 +0000 | [diff] [blame] | 980 | extra = ", ".join(extra) |
| 981 | |
| 982 | return valid, f"Op has non-const input(s): {extra}" |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 983 | |
| 984 | @staticmethod |
| 985 | def constraint_argmax_axis(op): |
| 986 | "Operation must be performed along the depth axis" |
| 987 | inp_dims = len(op.inputs[0].shape) |
| 988 | axis = op.inputs[1].values |
| 989 | return ( |
Johan Alfven | 56811e6 | 2023-03-27 11:33:50 +0200 | [diff] [blame] | 990 | axis in (inp_dims - 1, -1), |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 991 | f"Axis is {axis} and number of input dimensions is {inp_dims}", |
| 992 | ) |
| 993 | |
| 994 | @staticmethod |
Rickard Bolin | 6986a07 | 2022-12-19 12:33:40 +0000 | [diff] [blame] | 995 | def constraint_argmax_depth(op): |
| 996 | "IFM depth must be no greater than 127" |
| 997 | ifm_depth = op.inputs[0].shape[-1] |
| 998 | return ifm_depth <= 127, f"IFM depth is {ifm_depth}" |
Fredrik Svedberg | 0ac0804 | 2023-04-11 22:35:04 +0200 | [diff] [blame] | 999 | |
| 1000 | @staticmethod |
| 1001 | def constraint_lstm_no_cifg(op): |
| 1002 | "Must not use CIFG" |
| 1003 | cifg = None not in op.inputs[2:5] + op.inputs[6:9] |
| 1004 | cifg = cifg and op.inputs[1] is None |
| 1005 | cifg = cifg and op.inputs[5] is None |
| 1006 | return not cifg, "Op uses CIFG" |
| 1007 | |
| 1008 | @staticmethod |
| 1009 | def constraint_lstm_no_peep_hole(op): |
| 1010 | "Must not use Peephole" |
| 1011 | valid = all([tens is None for tens in op.inputs[9:12]]) |
| 1012 | return valid, "Op uses peephole" |
| 1013 | |
| 1014 | @staticmethod |
| 1015 | def constraint_lstm_no_projection(op): |
| 1016 | "Must not use Projection" |
| 1017 | valid = all([tens is None for tens in op.inputs[16:18]]) |
| 1018 | return valid, "Op uses projection" |
| 1019 | |
| 1020 | @staticmethod |
| 1021 | def constraint_lstm_no_normalisation(op): |
| 1022 | "Must not use Normalisation" |
| 1023 | valid = all([tens is None for tens in op.inputs[20:24]]) |
| 1024 | return valid, "Op uses normalisation" |
| 1025 | |
| 1026 | @staticmethod |
| 1027 | def constraint_lstm_weights(op): |
| 1028 | "All input and recurrent weights must be available" |
| 1029 | valid = None not in op.inputs[1:9] |
| 1030 | return valid, "Op has missing weights" |
Johan Alfven | 8e525ca | 2023-05-07 13:12:37 +0200 | [diff] [blame] | 1031 | |
| 1032 | @staticmethod |
William Isaksson | 2f9b687 | 2023-07-17 13:03:09 +0000 | [diff] [blame] | 1033 | def constraint_lstm_weight_dimensions(op): |
| 1034 | "All recurrent weights must be 2D" |
| 1035 | valid = all([len(input.shape) == 2 for input in op.inputs[5:9]]) |
| 1036 | return valid, "Op recurrent weights are not 2D" |
| 1037 | |
| 1038 | @staticmethod |
Johan Alfven | 8e525ca | 2023-05-07 13:12:37 +0200 | [diff] [blame] | 1039 | def constraint_rsqrt_input_int8(op): |
| 1040 | "IFM must be int8" |
| 1041 | ifm_dtype = op.ifm.dtype |
| 1042 | valid = ifm_dtype == DataType.int8 |
| 1043 | return valid, f"Op has ifm_dtype={ifm_dtype}" |
Johan Alfven | 85b7790 | 2023-06-15 09:24:01 +0200 | [diff] [blame] | 1044 | |
| 1045 | @staticmethod |
| 1046 | def constraint_slice_inputs_const(op): |
| 1047 | "Begin and Size Input tensors must be constant" |
| 1048 | valid = True |
| 1049 | extra = [] |
| 1050 | _, begin, sizes = op.inputs |
| 1051 | if begin.values is None: |
| 1052 | valid = False |
| 1053 | extra.append(f"Begin tensor '{begin.name}'") |
| 1054 | if sizes.values is None: |
| 1055 | valid = False |
| 1056 | extra.append(f"Size tensor '{sizes.name}'") |
| 1057 | extra = ", ".join(extra) |
| 1058 | return valid, f"Op has non-constant tensors: {extra}" |
Johan Alfven | a8fda88 | 2023-10-28 16:04:46 +0200 | [diff] [blame] | 1059 | |
| 1060 | @staticmethod |
| 1061 | def constraint_transpose(op): |
| 1062 | """The following shape/permutations are supported for transpose: |
| 1063 | When ifm rank is 2: WxC -> CxW |
| 1064 | When ifm rank is 3: HxWxC -> WxHxC, 1xWxC -> 1xCxW, Hx1xC -> Cx1xH |
| 1065 | When ifm rank is 4: 1xHxWxC -> 1xWxHxC, 1x1xWxC -> 1x1xCxW, 1xHx1xC -> 1xCx1xW""" |
| 1066 | |
| 1067 | ifm_shape = op.inputs[0].shape |
| 1068 | perm = op.inputs[1] |
| 1069 | |
| 1070 | # WxC -> CxW |
| 1071 | valid = len(ifm_shape) == 2 |
| 1072 | |
| 1073 | # HxWxC -> WxHxC |
| 1074 | if not valid and perm.shape == [3]: |
| 1075 | valid = perm.values[0] == 1 and perm.values[1] == 0 |
| 1076 | |
| 1077 | # 1xWxC -> 1xCxW |
| 1078 | if not valid and perm.shape == [3] and ifm_shape[0] == 1: |
| 1079 | valid = perm.values[1] == 2 and perm.values[2] == 1 |
| 1080 | |
| 1081 | # Hx1xC -> Cx1xH |
| 1082 | if not valid and perm.shape == [3] and ifm_shape[1] == 1: |
| 1083 | valid = perm.values[0] == 2 and perm.values[2] == 0 |
| 1084 | |
| 1085 | # 1xHxWxC -> 1xWxHxC |
| 1086 | if not valid and perm.shape == [4]: |
| 1087 | valid = perm.values[0] == 0 and perm.values[1] == 2 and perm.values[2] == 1 |
| 1088 | |
| 1089 | # 1x1xWxC -> 1x1xCxW |
| 1090 | if not valid and perm.shape == [4] and ifm_shape[1] == 1: |
| 1091 | valid = perm.values[0] == 0 and perm.values[2] == 3 and perm.values[3] == 2 |
| 1092 | |
| 1093 | # 1xHx1xC -> 1xCx1xH |
| 1094 | if not valid and perm.shape == [4] and ifm_shape[2] == 1: |
| 1095 | valid = perm.values[0] == 0 and perm.values[1] == 3 and perm.values[3] == 1 |
| 1096 | |
| 1097 | return valid, f"Op has ifm_shape: {ifm_shape} and permutation is: {perm.values}" |