Louis Verhaard | ebf4af6 | 2021-01-27 15:57:57 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [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. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # The SupportedOperators class which is a collection of all supported operators and parameter checks. |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 18 | from collections import defaultdict |
| 19 | |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 20 | import numpy as np |
| 21 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 22 | from .data_type import BaseType |
| 23 | from .data_type import DataType |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 24 | from .numeric_util import is_integer |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 25 | from .operation import get_slice_offsets |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 26 | from .operation import Op |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 27 | from .operation import Padding |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 28 | from .supported_operators_util import docstring_format_args |
| 29 | from .supported_operators_util import list_formatter |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 30 | from .tensor import check_quantized_tens_scaling_equal |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 31 | from .tflite_mapping import BUILTIN_OPERATOR_UNKNOWN |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 32 | from .tflite_mapping import optype_to_builtintype |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 33 | |
| 34 | |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 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) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 40 | return list_formatter(output) |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 41 | |
| 42 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | class SupportedOperators: |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 44 | # Categorised lists of supported operators |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 45 | npu_pre_ops = set((Op.SplitSliceRead,)) |
| 46 | convolution_ops = set((Op.Conv2DBias, Op.Conv2D, Op.QuantizedConv2D,)) |
| 47 | depthwise_convolution_ops = set((Op.DepthwiseConv2DBias,)) |
| 48 | transpose_convolution_ops = set((Op.Conv2DBackpropInput,)) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 49 | convolution_like_ops = convolution_ops | depthwise_convolution_ops | transpose_convolution_ops |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 50 | max_pooling_ops = Op.op_set(Op.is_maxpool_op) |
| 51 | avg_pooling_ops = Op.op_set(Op.is_avgpool_op) |
| 52 | pooling_ops = set((Op.ReduceSum,)) | max_pooling_ops | avg_pooling_ops |
| 53 | resizing_ops = set((Op.ResizeBilinear,)) |
| 54 | fc_vector_products = set((Op.QuantizedMatMul, Op.MatMul, Op.FullyConnected,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 55 | mac_main_ops = ( |
| 56 | # RNN/LSTM/GRU |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 57 | set((Op.BlockLSTM,)) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 58 | # conv/depthwiseconv/transposeconv |
| 59 | | convolution_like_ops |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 60 | # pooling |
| 61 | | pooling_ops |
| 62 | # resizing/upscaling |
| 63 | | resizing_ops |
| 64 | # FC layers |
| 65 | | fc_vector_products |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 66 | # Mean (converts to depthwise conv) |
| 67 | | set((Op.Mean,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 68 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 69 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
| 70 | binary_elem_wise_min_max_ops = set((Op.Minimum, Op.Maximum,)) |
| 71 | binary_elem_wise_shift_ops = set((Op.SHL, Op.SHR,)) |
| 72 | binary_elem_wise_add_mul_sub = set((Op.Add, Op.Mul, Op.Sub,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 73 | binary_elem_wise_main_ops = binary_elem_wise_min_max_ops | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
| 74 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 75 | pad_ops = set((Op.Pad,)) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 76 | supported_int32_tensor_ops = ( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 77 | set((Op.ReduceSum, Op.CLZ,)) | binary_elem_wise_add_mul_sub | binary_elem_wise_shift_ops |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 78 | ) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 79 | |
| 80 | relu_ops = set((Op.Relu, Op.Relu6, Op.ReluN1To1, Op.Clip,)) |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 81 | activation_ops = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.Softmax, Op.HardSwish)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 82 | npu_post_ops = ( |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 83 | # activation functions |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 84 | activation_ops |
| 85 | # concatenation write direction |
| 86 | | set((Op.ConcatSliceWrite,)) |
| 87 | # Quantization |
| 88 | | set((Op.Quantize,)) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 89 | ) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 90 | split_ops = set((Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack,)) |
| 91 | concat_ops = set((Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack,)) |
Louis Verhaard | 3d22f3c | 2021-02-03 08:43:54 +0100 | [diff] [blame] | 92 | memory_only_ops = set((Op.Reshape, Op.QuantizedReshape,)) | concat_ops | split_ops |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 93 | shapeless_input_ops = binary_elem_wise_main_ops | set((Op.Split, Op.SplitV, Op.Mean)) |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 94 | per_axis_quant_ops = convolution_like_ops # per-axis/channel quantization only currently supported for conv ops |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 95 | supported_fused_activations = relu_ops | set((Op.Tanh, Op.Sigmoid, Op.LUT,)) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 96 | supported_operators = npu_pre_ops | mac_main_ops | elem_wise_main_ops | pad_ops | npu_post_ops | memory_only_ops |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 97 | # Supported data types |
| 98 | supported_op_dtypes = set((DataType.uint8, DataType.int8, DataType.int16, DataType.int32)) |
Louis Verhaard | c776151 | 2021-02-03 10:22:38 +0100 | [diff] [blame] | 99 | supported_faf_dtypes = set((DataType.uint8, DataType.int8, DataType.int16)) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 100 | supported_bias_dtypes = set((DataType.int32, DataType.int64)) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 101 | supported_pad_dtypes = set((DataType.int32, DataType.int64)) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 102 | # Defined ranges for allowed values: |
| 103 | tens_dim_range = (1, 65535) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 104 | stride_range = (1, 3) |
| 105 | dilation_range = (1, 2) |
| 106 | dilated_height_range = (1, 64) |
| 107 | dilated_product_range = (1, 64 * 64) |
| 108 | weights_limit = 127 * 65536 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 109 | filter_range = (1, 8) |
| 110 | filter_height_range = (1, 256) |
| 111 | filter_product_range = (1, 256 * 256) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 112 | mean_kernel_product = 64 * 64 |
| 113 | mean_kernel_product_int8 = 16 * 16 |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 114 | mean_kernel_product_avgpool = 256 * 256 |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 115 | # Supported consumers |
Louis Verhaard | 1a92f78 | 2021-02-09 16:08:26 +0100 | [diff] [blame] | 116 | supported_pad_consumers = convolution_ops | depthwise_convolution_ops | pooling_ops |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 117 | |
Fredrik Svedberg | 880e735 | 2020-08-25 11:31:47 +0200 | [diff] [blame] | 118 | def __init__(self): |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 119 | # Setup the generic constraints. Note: the order matters |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 120 | self.generic_constraints = [] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 121 | self.generic_constraints.append(SupportedOperators.constraint_tens_no_dynamic) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 122 | self.generic_constraints.append(SupportedOperators.constraint_tens_defined_shape) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 123 | self.generic_constraints.append(SupportedOperators.constraint_tens_output_scalar) |
| 124 | self.generic_constraints.append(SupportedOperators.constraint_tens_input_scalar) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 125 | self.generic_constraints.append(SupportedOperators.constraint_tens_shape_size) |
| 126 | self.generic_constraints.append(SupportedOperators.constraint_tens_dtype) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 127 | self.generic_constraints.append(SupportedOperators.constraint_tens_int32_ops) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 128 | self.generic_constraints.append(SupportedOperators.constraint_tens_dimension) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 129 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_none_check) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 130 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_scale) |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 131 | self.generic_constraints.append(SupportedOperators.constraint_tens_quant_per_axis) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 132 | self.generic_constraints.append(SupportedOperators.constraint_faf) |
Louis Verhaard | c776151 | 2021-02-03 10:22:38 +0100 | [diff] [blame] | 133 | self.generic_constraints.append(SupportedOperators.constraint_faf_type) |
Louis Verhaard | 9a0cff1 | 2021-01-08 11:17:33 +0100 | [diff] [blame] | 134 | self.generic_constraints.append(SupportedOperators.constraint_quant_scale_inf) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 135 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 136 | # Setup specific constraints. Note: the order matters |
| 137 | self.specific_constraints = defaultdict(list) |
| 138 | |
| 139 | # Conv-like checks: |
| 140 | for op_type in SupportedOperators.convolution_like_ops: |
| 141 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_type) |
| 142 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_range) |
| 143 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilation_type) |
| 144 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilation_range) |
| 145 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilated_height_range) |
| 146 | self.specific_constraints[op_type].append(SupportedOperators.constraint_dilated_product_range) |
| 147 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_type) |
| 148 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_const) |
| 149 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_limit) |
| 150 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_type) |
| 151 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_40bit) |
| 152 | self.specific_constraints[op_type].append(SupportedOperators.constraint_batch_size) |
| 153 | # Depthwise Conv specific checks: |
| 154 | for op_type in SupportedOperators.depthwise_convolution_ops: |
| 155 | self.specific_constraints[op_type].append(SupportedOperators.constraint_depth_multiplier) |
| 156 | # Transpose Conv specific checks: |
| 157 | for op_type in SupportedOperators.transpose_convolution_ops: |
| 158 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_stride) |
| 159 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_same) |
| 160 | self.specific_constraints[op_type].append(SupportedOperators.constraint_tconv_valid) |
| 161 | |
| 162 | # Pooling checks: |
| 163 | for op_type in SupportedOperators.pooling_ops: |
| 164 | self.specific_constraints[op_type].append(SupportedOperators.constraint_batch_size) |
| 165 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_type) |
| 166 | self.specific_constraints[op_type].append(SupportedOperators.constraint_stride_range) |
| 167 | # AVG pooling specific checks: |
| 168 | for op_type in SupportedOperators.avg_pooling_ops: |
| 169 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 170 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_type) |
| 171 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_range) |
| 172 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_height_range_valid_pad) |
| 173 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_product_range_valid_pad) |
| 174 | # MAX pooling specific checks: |
| 175 | for op_type in SupportedOperators.max_pooling_ops: |
| 176 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 177 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_type) |
| 178 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_height_range) |
| 179 | self.specific_constraints[op_type].append(SupportedOperators.constraint_filter_product_range) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 180 | |
| 181 | # Resizing specific checks: |
| 182 | for op_type in SupportedOperators.resizing_ops: |
| 183 | self.specific_constraints[op_type].append(SupportedOperators.constraint_resize) |
| 184 | |
| 185 | # Vector Product specific checks: |
| 186 | for op_type in SupportedOperators.fc_vector_products: |
| 187 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_type) |
| 188 | self.specific_constraints[op_type].append(SupportedOperators.constraint_weights_const) |
| 189 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_type) |
| 190 | self.specific_constraints[op_type].append(SupportedOperators.constraint_bias_40bit) |
| 191 | |
| 192 | # Concat specific checks: |
| 193 | for op_type in (Op.Concat, Op.ConcatTFLite): |
| 194 | self.specific_constraints[op_type].append(SupportedOperators.constraint_axis_exists) |
| 195 | self.specific_constraints[op_type].append(SupportedOperators.constraint_axis_valid) |
| 196 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_dimensionality) |
| 197 | self.specific_constraints[op_type].append(SupportedOperators.constraint_valid_dimensions) |
| 198 | |
| 199 | # Element-wise checks: |
| 200 | for op_type in SupportedOperators.elem_wise_main_ops: |
| 201 | self.specific_constraints[op_type].append(SupportedOperators.constraint_elemwise_batch_size) |
| 202 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_either_shapes) |
| 203 | # Unary specific checks: |
| 204 | for op_type in SupportedOperators.unary_elem_wise_main_ops: |
| 205 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 206 | # Binary Min/Max specific checks: |
| 207 | for op_type in SupportedOperators.binary_elem_wise_min_max_ops: |
| 208 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_in_out_types) |
| 209 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_quantization_parameters) |
Andreas Nevalainen | d059d8b | 2020-11-19 14:40:35 +0100 | [diff] [blame] | 210 | self.specific_constraints[op_type].append(SupportedOperators.constraint_broadcast_shapes) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 211 | # Binary Add/Mul/Sub specific checks: |
| 212 | for op_type in SupportedOperators.binary_elem_wise_add_mul_sub: |
| 213 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_inputs_types) |
| 214 | self.specific_constraints[op_type].append(SupportedOperators.constraint_matching_signed) |
| 215 | self.specific_constraints[op_type].append(SupportedOperators.constraint_unsigned_valid) |
Andreas Nevalainen | d059d8b | 2020-11-19 14:40:35 +0100 | [diff] [blame] | 216 | self.specific_constraints[op_type].append(SupportedOperators.constraint_broadcast_shapes) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 217 | # Binary Shift specific checks: |
| 218 | for op_type in SupportedOperators.binary_elem_wise_shift_ops: |
| 219 | self.specific_constraints[op_type].append(SupportedOperators.constraint_inputs_int32) |
Andreas Nevalainen | d059d8b | 2020-11-19 14:40:35 +0100 | [diff] [blame] | 220 | self.specific_constraints[op_type].append(SupportedOperators.constraint_broadcast_shapes) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 221 | |
| 222 | # SHL specific checks: |
| 223 | self.specific_constraints[Op.SHL].append(SupportedOperators.constraint_output_int32) |
| 224 | |
| 225 | # CLZ specific checks: |
| 226 | self.specific_constraints[Op.CLZ].append(SupportedOperators.constraint_output_int32) |
| 227 | |
| 228 | # Softmax specific checks: |
| 229 | self.specific_constraints[Op.Softmax].append(SupportedOperators.constraint_matching_shapes) |
| 230 | self.specific_constraints[Op.Softmax].append(SupportedOperators.constraint_matching_in_out_types) |
Patrik Gustavsson | 2fa1588 | 2020-11-13 09:02:31 +0100 | [diff] [blame] | 231 | self.specific_constraints[Op.Softmax].append(SupportedOperators.constraint_beta_value_range) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 232 | |
| 233 | # SplitV specific checks: |
| 234 | self.specific_constraints[Op.SplitV].append(SupportedOperators.constraint_splitv_inferred) |
| 235 | |
| 236 | # StridedSlice specific checks: |
| 237 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_input_count) |
| 238 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_inputs_const) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 239 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_stridedslice_stride_values) |
| 240 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_ellipsis_mask) |
| 241 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_axis_masks) |
| 242 | self.specific_constraints[Op.StridedSlice].append(SupportedOperators.constraint_slice_ranges) |
| 243 | |
| 244 | # LeakyRelu specific checks: |
| 245 | self.specific_constraints[Op.LeakyRelu].append(SupportedOperators.constraint_alpha_valid) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 246 | |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 247 | # FullyConnected specific checks: |
| 248 | self.specific_constraints[Op.FullyConnected].append(SupportedOperators.constraint_fc_output_2d) |
erik.andersson@arm.com | 0cbb166 | 2021-02-22 15:47:07 +0100 | [diff] [blame] | 249 | self.specific_constraints[Op.FullyConnected].append(SupportedOperators.constraint_keep_dim_ifm_ofm) |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 250 | |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 251 | # Pad specific checks: |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 252 | self.specific_constraints[Op.Pad].append(SupportedOperators.constraint_pad_input_count) |
| 253 | self.specific_constraints[Op.Pad].append(SupportedOperators.constraint_pad_shape) |
| 254 | self.specific_constraints[Op.Pad].append(SupportedOperators.constraint_padding_dimensions) |
| 255 | self.specific_constraints[Op.Pad].append(SupportedOperators.constraint_pad_type) |
| 256 | self.specific_constraints[Op.Pad].append(SupportedOperators.constraint_pad_constant) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 257 | |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 258 | # HardSwish specific checks: |
| 259 | self.specific_constraints[Op.HardSwish].append(SupportedOperators.constraint_input_8bit) |
| 260 | self.specific_constraints[Op.HardSwish].append(SupportedOperators.constraint_matching_in_out_types) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 261 | # Mean specific checks: |
| 262 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_input_8bit) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 263 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_mean_input_dims) |
| 264 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_mean_axis) |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 265 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_mean_height_width_product_avgpool) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 266 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_mean_height_width_product) |
| 267 | self.specific_constraints[Op.Mean].append(SupportedOperators.constraint_mean_height_width_product_int8) |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 268 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 269 | def is_operator_supported(self, op): |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 270 | ext_type = optype_to_builtintype(op.type) |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 271 | if op.type not in SupportedOperators.supported_operators: |
Louis Verhaard | 5f2ea2f | 2020-10-15 08:39:44 +0200 | [diff] [blame] | 272 | if op.type not in (Op.Placeholder, Op.SubgraphInput, Op.Const): |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 273 | print(f"Info: {ext_type} '{op.name}' is a CPU only op") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 274 | return False |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 275 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 276 | for constraint in self.generic_constraints + self.specific_constraints[op.type]: |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 277 | valid, extra = constraint(op) |
| 278 | if not valid: |
Michael McGeagh | 219ec07 | 2020-11-09 11:11:26 +0000 | [diff] [blame] | 279 | print(f"Warning: {ext_type} '{op.name}' is not supported on the NPU. Placing on CPU instead") |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 280 | print(f" - {constraint.__doc__}") |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 281 | if extra: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 282 | print(f" {extra}") |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 283 | return False |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 284 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 285 | return True |
| 286 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 287 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 288 | def constraint_tens_no_dynamic(op): |
| 289 | "Input(s) and Output tensors must not be dynamic" |
| 290 | valid = True |
| 291 | extra = [] |
| 292 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 293 | for tens in tensors: |
| 294 | if (tens.shape == []) and (tens.values is None): |
| 295 | valid = False |
| 296 | extra.append(tens.name) |
| 297 | extra = ", ".join(extra) |
| 298 | return valid, f"Op has dynamic tensor(s): {extra}" |
| 299 | |
| 300 | @staticmethod |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 301 | def constraint_tens_defined_shape(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 302 | "Input(s) and Output tensors must have a defined shape" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 303 | valid = True |
| 304 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 305 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 306 | for tens in tensors: |
| 307 | if not tens.has_fully_defined_shape(): |
| 308 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 309 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 310 | return valid, ", ".join(extra) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 311 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 312 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 313 | def constraint_tens_output_scalar(op): |
| 314 | "Output tensors cannot be scalar" |
| 315 | ofm = op.ofm |
| 316 | valid = ofm.shape != [] |
| 317 | return valid, f"Output Tensor '{ofm.name}' is scalar" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 318 | |
| 319 | @classmethod |
Michael McGeagh | 34d2917 | 2020-11-25 12:36:23 +0000 | [diff] [blame] | 320 | @docstring_format_args([_optype_formatter(shapeless_input_ops)]) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 321 | def constraint_tens_input_scalar(cls, op): |
| 322 | "Scalar Input tensors are only valid for op type: {}" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 323 | valid = True |
| 324 | extra = [] |
| 325 | tensors = [tens for tens in op.inputs if tens] |
| 326 | for tens in tensors: |
| 327 | if (tens.shape == []) and (op.type not in cls.shapeless_input_ops): |
| 328 | valid = False |
| 329 | extra.append(tens.name) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 330 | extra = ", ".join(extra) |
| 331 | return valid, f"Op has scalar input tensor(s): {extra}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 332 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 333 | @staticmethod |
| 334 | def constraint_tens_shape_size(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 335 | "Input(s) and Output tensors must not be greater than 4D" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 336 | valid = True |
| 337 | extra = [] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 338 | tensors = [tens for tens in op.inputs + op.outputs if tens] |
| 339 | for tens in tensors: |
| 340 | if len(tens.shape) > 4: |
| 341 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 342 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 343 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 344 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 345 | @classmethod |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 346 | @docstring_format_args([list_formatter(supported_op_dtypes)]) |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 347 | def constraint_tens_dtype(cls, op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 348 | "Tensors must be of type: {}" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 349 | valid = True |
| 350 | extra = [] |
| 351 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 352 | if not tensors: |
| 353 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 354 | for tens in tensors: |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 355 | if tens.dtype not in cls.supported_op_dtypes: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 356 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 357 | extra.append(f"Tensor '{tens.name}' has data type: {tens.dtype}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 358 | return valid, ", ".join(extra) |
| 359 | |
| 360 | @classmethod |
Michael McGeagh | 34d2917 | 2020-11-25 12:36:23 +0000 | [diff] [blame] | 361 | @docstring_format_args([_optype_formatter(supported_int32_tensor_ops)]) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 362 | def constraint_tens_int32_ops(cls, op): |
| 363 | "Tensors which are int32 are only valid when op type is: {}" |
| 364 | valid = True |
| 365 | extra = [] |
| 366 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 367 | if not tensors: |
| 368 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 369 | for tens in tensors: |
| 370 | if (tens.dtype == DataType.int32) and (op.type not in cls.supported_int32_tensor_ops): |
| 371 | valid = False |
| 372 | extra.append(tens.name) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 373 | extra = ", ".join(extra) |
| 374 | return valid, f"Op has int32 tensor(s): {extra}" |
Andreas Nevalainen | eadb166 | 2020-09-01 15:36:26 +0200 | [diff] [blame] | 375 | |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 376 | @classmethod |
| 377 | @docstring_format_args(tens_dim_range) |
| 378 | def constraint_tens_dimension(cls, op): |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 379 | "Tensor dimensions must be in the range [{}, {}]" |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 380 | tens_min, tens_max = cls.tens_dim_range |
| 381 | valid = True |
| 382 | extra = [] |
| 383 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 384 | if not tensors: |
| 385 | tensors = [tens for tens in op.inputs if tens] |
Michael McGeagh | 37ded34 | 2020-10-01 15:37:44 +0100 | [diff] [blame] | 386 | for tens in tensors: |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 387 | if not all(tens_min <= dim <= tens_max for dim in tens.shape): |
| 388 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 389 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 390 | return valid, ", ".join(extra) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 391 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 392 | @staticmethod |
| 393 | def constraint_tens_quant_none_check(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 394 | "Input(s), Output and Weight tensors must have quantization parameters" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 395 | valid = True |
| 396 | extra = [] |
| 397 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 398 | for tens in tensors: |
| 399 | if tens.quantization is None: |
| 400 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 401 | extra.append(tens.name) |
| 402 | extra = ", ".join(extra) |
| 403 | return valid, f"Op has tensors with missing quantization parameters: {extra}" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 404 | |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 405 | @staticmethod |
| 406 | def constraint_tens_quant_scale(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 407 | "Input(s), Output and Weight tensors with quantization scales must be finite" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 408 | valid = True |
| 409 | extra = [] |
| 410 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 411 | for tens in tensors: |
| 412 | if (tens.quantization.scale_f32 is not None) and np.isinf(tens.quantization.scale_f32).any(): |
| 413 | valid = False |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 414 | extra.append(f"Tensor '{tens.name}' has quantization scale: {tens.quantization.scale_f32}") |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 415 | return valid, ", ".join(extra) |
| 416 | |
| 417 | @classmethod |
Michael McGeagh | 34d2917 | 2020-11-25 12:36:23 +0000 | [diff] [blame] | 418 | @docstring_format_args([_optype_formatter(per_axis_quant_ops)]) |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 419 | def constraint_tens_quant_per_axis(cls, op): |
| 420 | "Per-axis quantization is only supported for the following op types: {}" |
| 421 | valid = True |
| 422 | extra = [] |
| 423 | if op.type not in cls.per_axis_quant_ops: |
| 424 | tensors = [tens for tens in op.get_ifm_ifm2_weights_ofm() if tens] |
| 425 | for tens in tensors: |
| 426 | if tens.quantization.is_per_axis(): |
| 427 | valid = False |
| 428 | extra.append(tens.name) |
| 429 | return valid, "The following tensor(s) have per-axis quantization parameters: " + ", ".join(extra) |
| 430 | |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 431 | @staticmethod |
| 432 | def constraint_fc_output_2d(op): |
| 433 | "The output tensor(s) must have 2D shape" |
| 434 | valid = True |
| 435 | extra = [] |
| 436 | for tens in op.outputs: |
| 437 | if len(tens.shape) != 2: |
| 438 | valid = False |
| 439 | extra.append(f"Tensor '{tens.name}' is {len(tens.shape)}D") |
| 440 | return valid, ", ".join(extra) |
| 441 | |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 442 | @classmethod |
Michael McGeagh | 34d2917 | 2020-11-25 12:36:23 +0000 | [diff] [blame] | 443 | @docstring_format_args([_optype_formatter(supported_fused_activations)]) |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 444 | def constraint_faf(cls, op): |
| 445 | "The fused activation function (if present) must be one of type: {}" |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 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 |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 453 | |
Louis Verhaard | c776151 | 2021-02-03 10:22:38 +0100 | [diff] [blame] | 454 | @classmethod |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 455 | @docstring_format_args([list_formatter(supported_faf_dtypes)]) |
Louis Verhaard | c776151 | 2021-02-03 10:22:38 +0100 | [diff] [blame] | 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 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 466 | @staticmethod |
| 467 | def constraint_stride_type(op): |
| 468 | "Stride values for both width and height must be integer types" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 469 | w, h = op.get_kernel_stride() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 470 | valid = is_integer(w) and is_integer(h) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 471 | return valid, f"Op has stride WxH as: {repr(w)}x{repr(h)}" |
Michael McGeagh | 184b250 | 2020-10-09 17:19:52 +0100 | [diff] [blame] | 472 | |
Michael McGeagh | 1eeea51 | 2020-09-30 14:23:09 +0100 | [diff] [blame] | 473 | @classmethod |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 474 | @docstring_format_args(stride_range) |
| 475 | def constraint_stride_range(cls, op): |
| 476 | "Stride values for both width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 477 | w, h = op.get_kernel_stride() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 478 | stride_min, stride_max = cls.stride_range |
| 479 | valid = (stride_min <= w <= stride_max) and (stride_min <= h <= stride_max) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 480 | return valid, f"Op has stride WxH as: {w}x{h}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 481 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 482 | @staticmethod |
| 483 | def constraint_dilation_type(op): |
| 484 | "Dilation factor values for both width and height must be integer types" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 485 | w, h = op.get_kernel_dilation() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 486 | valid = is_integer(w) and is_integer(h) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 487 | return valid, f"Op has dilation factor WxH as: {repr(w)}x{repr(h)}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 488 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 489 | @classmethod |
| 490 | @docstring_format_args(dilation_range) |
| 491 | def constraint_dilation_range(cls, op): |
| 492 | "Dilation factor values for both width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 493 | w, h = op.get_kernel_dilation() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 494 | dilation_min, dilation_max = cls.dilation_range |
| 495 | valid = (dilation_min <= w <= dilation_max) and (dilation_min <= h <= dilation_max) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 496 | return valid, f"Op has dilation factor WxH as: {w}x{h}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 497 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 498 | @classmethod |
| 499 | @docstring_format_args(dilated_height_range) |
| 500 | def constraint_dilated_height_range(cls, op): |
| 501 | "Dilated kernel height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 502 | h = op.kernel.area_height() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 503 | dilated_height_min, dilated_height_max = cls.dilated_height_range |
| 504 | valid = dilated_height_min <= h <= dilated_height_max |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 505 | return valid, f"Op has dilated kernel height as: {h}" |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 506 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 507 | @classmethod |
| 508 | @docstring_format_args(dilated_product_range) |
| 509 | def constraint_dilated_product_range(cls, op): |
| 510 | "Product of dilated kernel width and height must be in the range [{}, {}]" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 511 | product = op.kernel.area_width() * op.kernel.area_height() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 512 | dilated_product_min, dilated_product_max = cls.dilated_product_range |
| 513 | valid = dilated_product_min <= product <= dilated_product_max |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 514 | return valid, f"Op has product of dilated kernel width and height as: {product}" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 515 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 516 | @staticmethod |
| 517 | def constraint_weights_type(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 518 | "Weight tensor must be 8-bit" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 519 | weights = op.weights |
| 520 | valid = weights.element_size() == 1 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 521 | return valid, f"Tensor '{weights.name}' is {int(weights.element_size() * 8)}-bit" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 522 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 523 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 524 | def constraint_weights_const(op): |
| 525 | "Weight tensor must be constant" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 526 | weights = op.weights |
| 527 | valid = weights.values is not None |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 528 | return valid, f"Tensor '{weights.name}' has non-constant values" |
Andreas Nevalainen | 8854dc9 | 2020-09-24 13:43:00 +0200 | [diff] [blame] | 529 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 530 | @classmethod |
| 531 | @docstring_format_args([weights_limit]) |
| 532 | def constraint_weights_limit(cls, op): |
| 533 | "The sum of the weights cannot exceed {}" |
| 534 | weights = op.weights |
James Peet | 7519d50 | 2021-07-19 16:47:58 +0100 | [diff] [blame] | 535 | values = weights.values.astype(np.int64) - weights.quantization.zero_point |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 536 | limit = np.amax(np.sum(np.absolute(values), axis=(0, 1, 2))) |
| 537 | valid = limit <= cls.weights_limit |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 538 | return valid, f"Tensor '{weights.name}' has the sum of weights: {limit}" |
Andreas Nevalainen | f0c59bf | 2020-08-26 10:56:23 +0200 | [diff] [blame] | 539 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 540 | @classmethod |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 541 | @docstring_format_args([list_formatter(supported_bias_dtypes)]) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 542 | def constraint_bias_type(cls, op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 543 | "Optional Bias tensor must be of type: {}" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 544 | bias = op.bias |
| 545 | if bias: |
| 546 | valid = bias.dtype in cls.supported_bias_dtypes |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 547 | return valid, f"Tensor '{bias.name}' has data type: {bias.dtype}" |
| 548 | return True, "Op has no bias tensor" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 549 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 550 | @staticmethod |
| 551 | def constraint_bias_40bit(op): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 552 | "Optional Bias tensor values must fit within 40-bits" |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 553 | bias = op.bias |
James Peet | 7519d50 | 2021-07-19 16:47:58 +0100 | [diff] [blame] | 554 | if bias and bias.dtype == DataType.int64 and bias.values is not None: |
| 555 | valid = all(len(bin(quant_value)[2:]) <= 40 for quant_value in bias.values) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 556 | return valid, f"Tensor '{bias.name}' has values larger than 40-bits" |
| 557 | return True, "Op has no bias tensor, or it fits in 40-bit" |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 558 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 559 | @staticmethod |
| 560 | def constraint_batch_size(op): |
| 561 | "IFM Tensor batch size must be 1" |
| 562 | ifm = op.ifm |
| 563 | valid = ifm.shape[0] == 1 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 564 | return valid, f"Tensor '{ifm.name}' has batch size: {ifm.shape[0]}" |
| 565 | |
| 566 | @staticmethod |
| 567 | def constraint_quant_scale_inf(op): |
Louis Verhaard | 9a0cff1 | 2021-01-08 11:17:33 +0100 | [diff] [blame] | 568 | "Input and Output tensors must have quantization scales that fit within float32 precision" |
| 569 | if op.ofm is not None and op.ofm.is_quantized(): |
| 570 | ofm_scale = op.ofm.quantization.scale_f32 |
| 571 | if ofm_scale < np.finfo(np.float32).tiny: |
| 572 | return ( |
| 573 | False, |
| 574 | f"The quantization scale of the output tensor is {ofm_scale}, " |
| 575 | + f"minimum supported is: {np.finfo(np.float32).tiny}", |
| 576 | ) |
| 577 | if op.ifm is not None and op.ifm.is_quantized(): |
| 578 | ifm_scale = op.ifm.quantization.scale_f32 |
| 579 | if np.isinf(ifm_scale / ofm_scale): |
| 580 | return ( |
| 581 | False, |
| 582 | f"IFM scale divided by OFM scale is infinite, ifm_scale={ifm_scale} ofm_scale={ofm_scale}", |
| 583 | ) |
| 584 | return True, "Op's quantization is ok" |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 585 | |
| 586 | @staticmethod |
| 587 | def constraint_depth_multiplier(op): |
| 588 | "For depth multipliers > 1, IFM channels must be 1 and OFM channels must be equal to the depth multiplier" |
| 589 | depth_multiplier = op.attrs.get("depth_multiplier", 1) |
| 590 | if depth_multiplier > 1: |
| 591 | ifm_channels = op.ifm.shape[3] |
| 592 | ofm_channels = op.ofm.shape[3] |
| 593 | valid = (ifm_channels == 1) and (ofm_channels == depth_multiplier) |
| 594 | extra = ( |
| 595 | f"Op has ifm_channels={ifm_channels}, ofm_channels={ofm_channels}" |
| 596 | f" and depth_multiplier={depth_multiplier}" |
| 597 | ) |
| 598 | return valid, extra |
| 599 | return True, "Op has depth_multiplier=1" |
| 600 | |
| 601 | @staticmethod |
| 602 | def constraint_tconv_stride(op): |
| 603 | "Stride values for both width and height must be 2" |
| 604 | w = op.kernel.stride.x |
| 605 | h = op.kernel.stride.y |
| 606 | valid = (w == 2) and (h == 2) |
| 607 | return valid, f"Op has stride WxH as: {w}x{h}" |
| 608 | |
| 609 | @staticmethod |
| 610 | def constraint_tconv_same(op): |
| 611 | "SAME padding: OFM dimensions must equal IFM dimensions multiplied by stride" |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 612 | if op.attrs["padding"] == Padding.SAME: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 613 | w = op.kernel.stride.x |
| 614 | h = op.kernel.stride.y |
| 615 | ifm_shape = op.ifm.shape |
| 616 | ofm_shape = op.ofm.shape |
| 617 | valid = (ofm_shape[1] == (ifm_shape[1] * h)) and (ofm_shape[2] == (ifm_shape[2] * w)) |
| 618 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and stride WxH as {w}x{h}" |
| 619 | return True, "Op has padding=VALID" |
| 620 | |
| 621 | @staticmethod |
| 622 | def constraint_tconv_valid(op): |
| 623 | """VALID padding: OFM dimensions must equal IFM dimensions multiplied by stride, |
| 624 | minus difference between kernel size and stride""" |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 625 | if op.attrs["padding"] == Padding.VALID: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 626 | s_w = op.kernel.stride.x |
| 627 | s_h = op.kernel.stride.y |
| 628 | k_w = op.kernel.width |
| 629 | k_h = op.kernel.height |
| 630 | ifm_shape = op.ifm.shape |
| 631 | ofm_shape = op.ofm.shape |
| 632 | height_check = ofm_shape[1] == (ifm_shape[1] * s_h + max(k_h - s_h, 0)) |
| 633 | width_check = ofm_shape[2] == (ifm_shape[2] * s_w + max(k_w - s_w, 0)) |
| 634 | valid = height_check and width_check |
| 635 | extra = ( |
| 636 | f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape}," |
| 637 | f" stride WxH as {s_w}x{s_h} and kernel WxH as {k_w}x{k_h}" |
| 638 | ) |
| 639 | return valid, extra |
| 640 | return True, "Op has padding=SAME" |
| 641 | |
| 642 | @staticmethod |
| 643 | def constraint_matching_in_out_types(op): |
| 644 | "IFM and OFM data types must match" |
| 645 | ifm_dtype = op.ifm.dtype |
| 646 | ofm_dtype = op.ofm.dtype |
| 647 | valid = ifm_dtype == ofm_dtype |
| 648 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
| 649 | |
| 650 | @staticmethod |
Patrik Gustavsson | 2fa1588 | 2020-11-13 09:02:31 +0100 | [diff] [blame] | 651 | def constraint_beta_value_range(op): |
| 652 | "Beta value needs to be positive" |
| 653 | beta = op.attrs.get("beta", 1.0) |
| 654 | valid = beta >= 0 |
| 655 | return valid, f"Op has beta={beta}" |
| 656 | |
| 657 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 658 | def constraint_filter_type(op): |
| 659 | "Kernel filter values for both width and height must be integer types" |
| 660 | w = op.kernel.width |
| 661 | h = op.kernel.height |
| 662 | valid = is_integer(w) and is_integer(h) |
| 663 | return valid, f"Op has kernel filter WxH as: {repr(w)}x{repr(h)}" |
| 664 | |
| 665 | @classmethod |
| 666 | @docstring_format_args(filter_range) |
| 667 | def constraint_filter_range(cls, op): |
| 668 | "Kernel filter values for both width and height must be in the range [{}, {}]" |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 669 | if op.attrs["padding"] == Padding.SAME: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 670 | w = op.kernel.width |
| 671 | h = op.kernel.height |
| 672 | filter_min, filter_max = cls.filter_range |
| 673 | valid = (filter_min <= w <= filter_max) and (filter_min <= h <= filter_max) |
| 674 | return valid, f"Op has kernel filter WxH as: {w}x{h}" |
| 675 | return True, "Op has padding=VALID" |
| 676 | |
| 677 | @classmethod |
| 678 | @docstring_format_args(filter_height_range) |
| 679 | def constraint_filter_height_range(cls, op): |
| 680 | "Kernel filter height must be in the range [{}, {}]" |
| 681 | h = op.kernel.height |
| 682 | filter_height_min, filter_height_max = cls.filter_height_range |
| 683 | valid = filter_height_min <= h <= filter_height_max |
| 684 | return valid, f"Op has kernel filter height as: {h}" |
| 685 | |
| 686 | @classmethod |
| 687 | @docstring_format_args(filter_product_range) |
| 688 | def constraint_filter_product_range(cls, op): |
| 689 | "Product of kernel filter width and height must be in the range [{}, {}]" |
| 690 | product = op.kernel.elements_wh() |
| 691 | filter_product_min, filter_product_max = cls.filter_product_range |
| 692 | valid = filter_product_min <= product <= filter_product_max |
| 693 | return valid, f"Op has product of kernel filter width and height as: {product}" |
| 694 | |
| 695 | @staticmethod |
| 696 | @docstring_format_args(filter_height_range) |
| 697 | def constraint_filter_height_range_valid_pad(op): |
| 698 | "VALID padding: Kernel filter height must be in the range [{}, {}]" |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 699 | if op.attrs["padding"] == Padding.VALID: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 700 | return SupportedOperators.constraint_filter_height_range(op) |
| 701 | return True, "Op has padding=SAME" |
| 702 | |
| 703 | @staticmethod |
| 704 | @docstring_format_args(filter_product_range) |
| 705 | def constraint_filter_product_range_valid_pad(op): |
| 706 | "VALID padding: Product of kernel filter width and height must be in the range [{}, {}]" |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 707 | if op.attrs["padding"] == Padding.VALID: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 708 | return SupportedOperators.constraint_filter_product_range(op) |
| 709 | return True, "Op has padding=SAME" |
| 710 | |
| 711 | @staticmethod |
| 712 | def constraint_resize(op): |
| 713 | """The width and height of the IFM and OFM must match one of the following criteria: |
| 714 | IFM W and H must both be 1 |
| 715 | IFM must match OFM |
| 716 | OFM W and H must be 2x IFM -1, if align_corners is True |
| 717 | OFM W and H must be 2x IFM, if align_corners is False""" |
| 718 | # Easier to start with False condition as very few cases result in a supported resize |
| 719 | valid = False |
| 720 | ifm_shape = op.ifm.shape |
| 721 | ofm_shape = op.ofm.shape |
| 722 | align_corners = op.attrs.get("align_corners", False) |
| 723 | if len(ifm_shape) == 4: |
| 724 | # Valid if IFM W and H are both 1, or IFM and OFM shape are the same |
| 725 | if ((ifm_shape[1] == 1) and (ifm_shape[2] == 1)) or (ifm_shape == ofm_shape): |
| 726 | valid = True |
| 727 | else: |
| 728 | upscaled_shape = np.array(ifm_shape[1:3]) |
| 729 | out_shape = np.array(ofm_shape[1:3]) |
| 730 | while (upscaled_shape < out_shape).all(): |
| 731 | upscaled_shape *= 2 |
| 732 | if align_corners: |
| 733 | upscaled_shape -= 1 |
| 734 | # Valid if OFM is 2x IFM (-1 for align corners) |
| 735 | if np.array_equal(out_shape, upscaled_shape): |
| 736 | valid = True |
| 737 | break |
| 738 | return valid, f"Op has ifm_shape={ifm_shape}, ofm_shape={ofm_shape} and align_corners={align_corners}" |
| 739 | |
| 740 | @staticmethod |
| 741 | def constraint_matching_shapes(op): |
| 742 | "IFM and OFM shapes must match" |
| 743 | ifm_shape = op.ifm.shape |
| 744 | ofm_shape = op.ofm.shape |
| 745 | valid = ifm_shape == ofm_shape |
| 746 | return valid, f"Op has ifm_shape={ifm_shape} and ofm_shape={ofm_shape}" |
| 747 | |
| 748 | @staticmethod |
| 749 | def constraint_splitv_inferred(op): |
| 750 | "Only one size is allowed to be inferred" |
Jacob Bohlin | e3de4e5 | 2020-11-27 14:52:06 +0100 | [diff] [blame] | 751 | sizes = op.inputs[1].values |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 752 | valid = np.count_nonzero(sizes == -1) <= 1 |
| 753 | return valid, f"Op has multiple inferred sizes (-1): {sizes}" |
| 754 | |
| 755 | @staticmethod |
| 756 | def constraint_axis_exists(op): |
| 757 | "Axis attribute must exist" |
| 758 | axis = op.attrs.get("axis") |
| 759 | valid = axis is not None |
| 760 | return valid, f"Op has axis={axis}" |
| 761 | |
| 762 | @staticmethod |
| 763 | def constraint_axis_valid(op): |
| 764 | "Axis attribute must be in the range [0, <ofm_dimensions>)" |
| 765 | dims = len(op.ofm.shape) |
| 766 | axis = op.attrs["axis"] |
| 767 | axis += dims if axis < 0 else 0 |
| 768 | valid = 0 <= axis < dims |
| 769 | return valid, f"Op has ofm_dimensions={dims} and axis attribute is: {axis}" |
| 770 | |
| 771 | @staticmethod |
| 772 | def constraint_matching_dimensionality(op): |
| 773 | "All Input dimensionalities must match OFM dimensionality" |
| 774 | valid = True |
| 775 | extra = [] |
| 776 | ofm_dim = len(op.ofm.shape) |
| 777 | tensors = [tens for tens in op.inputs if tens] |
| 778 | for tens in tensors: |
| 779 | dim = len(tens.shape) |
| 780 | if dim != ofm_dim: |
| 781 | valid = False |
| 782 | extra.append(f"Tensor '{tens.name}' has dimension: {dim}") |
| 783 | extra = ", ".join(extra) |
| 784 | return valid, f"Op has ofm_dimension={ofm_dim} and the list of mismatching inputs are: {extra}" |
| 785 | |
| 786 | @staticmethod |
| 787 | def constraint_valid_dimensions(op): |
| 788 | "All Input dimensions must match OFM dimension in all axes except the one defined by the axis attribute" |
| 789 | valid = True |
| 790 | extra = [] |
| 791 | ofm_shape = op.ofm.shape |
| 792 | ofm_dim = len(ofm_shape) |
| 793 | axis = op.attrs["axis"] |
| 794 | axis += ofm_dim if axis < 0 else 0 |
| 795 | tensors = [tens for tens in op.inputs if tens] |
| 796 | for tens in tensors: |
| 797 | if any(tens.shape[dim] != ofm_shape[dim] for dim in range(ofm_dim) if dim != axis): |
| 798 | valid = False |
| 799 | extra.append(f"Tensor '{tens.name}' has shape: {tens.shape}") |
| 800 | extra = ", ".join(extra) |
| 801 | return valid, f"Op has axis={axis}, ofm_shape={ofm_shape} and the list of mismatching inputs are: {extra}" |
| 802 | |
| 803 | @staticmethod |
| 804 | def constraint_stridedslice_input_count(op): |
| 805 | "Exactly 4 Input tensors are required" |
| 806 | inputs = len(op.inputs) |
| 807 | valid = inputs == 4 |
| 808 | return valid, f"Op has {inputs} inputs" |
| 809 | |
| 810 | @staticmethod |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 811 | def constraint_pad_input_count(op): |
| 812 | "Number of input tensors must be exactly 2" |
| 813 | inputs = len(op.inputs) |
| 814 | valid = inputs == 2 |
| 815 | return valid, f"Op has {inputs} inputs" |
| 816 | |
| 817 | @staticmethod |
| 818 | def constraint_pad_shape(op): |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 819 | "The padding tensor must have the shape [3,2] or [4,2]" |
| 820 | valid = op.inputs[1].shape in ([3, 2], [4, 2]) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 821 | return valid, f"The pad tensor has the shape: {op.inputs[1].shape}" |
| 822 | |
| 823 | @classmethod |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 824 | @docstring_format_args([list_formatter(supported_pad_dtypes)]) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 825 | def constraint_pad_type(cls, op): |
| 826 | "Pad tensor must be of type: {}" |
| 827 | pad_tensor = op.inputs[1] |
| 828 | valid = pad_tensor.dtype in cls.supported_pad_dtypes |
| 829 | return valid, f"Tensor '{pad_tensor.name}' has data type: {pad_tensor.dtype}" |
| 830 | |
| 831 | @staticmethod |
| 832 | def constraint_padding_dimensions(op): |
| 833 | "The pad tensor can only pad width and height" |
| 834 | pad_tensor = op.inputs[1].values |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 835 | |
| 836 | valid = sum(pad_tensor[-1, :]) == 0 |
| 837 | if valid and len(pad_tensor) > 3: |
| 838 | valid = sum(pad_tensor[0, :]) == 0 |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 839 | return valid, f"First dimension padding: {pad_tensor[0,:]}, last dimension padding: {pad_tensor[-1,:]}" |
| 840 | |
| 841 | @staticmethod |
| 842 | def constraint_pad_constant(op): |
Louis Verhaard | 3d22f3c | 2021-02-03 08:43:54 +0100 | [diff] [blame] | 843 | "The padding tensor must be constant" |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 844 | pad_tensor = op.inputs[1].values |
| 845 | valid = pad_tensor is not None |
| 846 | return valid, f"Op has non-constant padding tensor: {op.inputs[1].values}" |
| 847 | |
Louis Verhaard | ebf4af6 | 2021-01-27 15:57:57 +0100 | [diff] [blame] | 848 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 849 | def constraint_stridedslice_inputs_const(op): |
| 850 | "Begin, End and Stride Input tensors must be constant" |
| 851 | valid = True |
| 852 | extra = [] |
| 853 | _, begin, end, strides = op.inputs |
| 854 | if begin.values is None: |
| 855 | valid = False |
| 856 | extra.append(f"Begin tensor '{begin.name}'") |
| 857 | if end.values is None: |
| 858 | valid = False |
| 859 | extra.append(f"End tensor '{end.name}'") |
| 860 | if strides.values is None: |
| 861 | valid = False |
| 862 | extra.append(f"Stride tensor '{strides.name}'") |
| 863 | extra = ", ".join(extra) |
| 864 | return valid, f"Op has non-constant tensors: {extra}" |
| 865 | |
| 866 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 867 | def constraint_stridedslice_stride_values(op): |
| 868 | "All Strides values must be 1" |
| 869 | strides = op.inputs[3] |
| 870 | valid = all(stride == 1 for stride in strides.values) |
| 871 | return valid, f"Op has strides values {strides.values}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 872 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 873 | @staticmethod |
| 874 | def constraint_ellipsis_mask(op): |
| 875 | "ellipsis_mask must be 0" |
| 876 | ellipsis = op.attrs["ellipsis_mask"] |
| 877 | valid = ellipsis == 0 |
| 878 | return valid, f"Op has ellipsis mask as: {ellipsis}" |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 879 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 880 | @staticmethod |
| 881 | def constraint_axis_masks(op): |
| 882 | "new_axis_mask and shrink_axis_mask cannot both be set" |
| 883 | new_axis = op.attrs["new_axis_mask"] |
| 884 | shrink_axis = op.attrs["shrink_axis_mask"] |
| 885 | valid = (new_axis == 0) or (shrink_axis == 0) |
| 886 | return valid, f"Op has new_axis_mask={new_axis} and shrink_axis_mask={shrink_axis}" |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 887 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 888 | @staticmethod |
| 889 | def constraint_slice_ranges(op): |
| 890 | "Slice 'end' values must be greater than 'begin' values" |
| 891 | ifm, begin, end, _ = op.inputs |
| 892 | # Calculate offset begin/end |
| 893 | offset_begin = get_slice_offsets(ifm.shape, begin, op.attrs["begin_mask"], is_begin=True) |
| 894 | offset_end = get_slice_offsets(ifm.shape, end, op.attrs["end_mask"], is_begin=False) |
| 895 | # Check "end - begin" doesn't result in any zero or negative elements |
| 896 | valid = all((e - b) > 0 for b, e in zip(offset_begin, offset_end)) |
| 897 | return valid, f"Op has begin_values={begin.values} and end_values={end.values}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 898 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 899 | @staticmethod |
| 900 | def constraint_matching_inputs_types(op): |
| 901 | "Both Input data types must match" |
| 902 | ifm_dtype = op.ifm.dtype |
| 903 | ifm2_dtype = op.ifm2.dtype |
| 904 | valid = ifm_dtype == ifm2_dtype |
| 905 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 906 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 907 | @staticmethod |
| 908 | def constraint_matching_signed(op): |
| 909 | "For IFM that are signed, OFM must also be signed" |
| 910 | valid = True |
| 911 | ifm_dtype = op.ifm.dtype |
| 912 | ofm_dtype = op.ofm.dtype |
| 913 | if ifm_dtype.type & BaseType.Signed: |
| 914 | valid = bool(ofm_dtype.type & BaseType.Signed) |
| 915 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 916 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 917 | @staticmethod |
| 918 | def constraint_unsigned_valid(op): |
| 919 | "For IFM that are unsigned, OFM must either be the same type or int32" |
| 920 | valid = True |
| 921 | ifm_dtype = op.ifm.dtype |
| 922 | ofm_dtype = op.ofm.dtype |
| 923 | if ifm_dtype.type & BaseType.Unsigned: |
| 924 | valid = (ifm_dtype == ofm_dtype) or (ofm_dtype == DataType.int32) |
| 925 | return valid, f"Op has ifm_dtype={ifm_dtype} and ofm_dtype={ofm_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 926 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 927 | @staticmethod |
| 928 | def constraint_inputs_int32(op): |
| 929 | "Both Input data types must be int32" |
| 930 | ifm_dtype = op.ifm.dtype |
| 931 | ifm2_dtype = op.ifm2.dtype |
| 932 | valid = (ifm_dtype == DataType.int32) and (ifm2_dtype == DataType.int32) |
| 933 | return valid, f"Op has ifm_dtype={ifm_dtype} and ifm2_dtype={ifm2_dtype}" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 934 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 935 | @staticmethod |
| 936 | def constraint_output_int32(op): |
| 937 | "OFM must be int32" |
| 938 | ofm_dtype = op.ofm.dtype |
| 939 | valid = ofm_dtype == DataType.int32 |
| 940 | return valid, f"Op has ofm_dtype={ofm_dtype}" |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 941 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 942 | @staticmethod |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 943 | def constraint_input_8bit(op): |
| 944 | "IFM must be int8 or uint8" |
| 945 | ifm_dtype = op.ifm.dtype |
| 946 | valid = (ifm_dtype == DataType.int8) or (ifm_dtype == DataType.uint8) |
| 947 | return valid, f"Op has ifm_dtype={ifm_dtype}" |
| 948 | |
| 949 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 950 | def constraint_matching_quantization_parameters(op): |
| 951 | "Both Input quantization parameters must match OFM quantization parameters" |
| 952 | valid = True |
| 953 | extra = [] |
| 954 | if not check_quantized_tens_scaling_equal(op.ofm, op.ifm): |
| 955 | valid = False |
| 956 | extra.append(op.ifm.name) |
Erik Andersson | f27a8b6 | 2020-12-10 14:58:23 +0100 | [diff] [blame] | 957 | if op.ifm2 is not None and not check_quantized_tens_scaling_equal(op.ofm, op.ifm2): |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 958 | valid = False |
| 959 | extra.append(op.ifm2.name) |
| 960 | extra = ", ".join(extra) |
| 961 | return valid, f"Op has tensors with different quantization parameters to the OFM '{op.ofm.name}': {extra}" |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 962 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 963 | @staticmethod |
| 964 | def constraint_elemwise_batch_size(op): |
| 965 | "Batch size must be 1 for Input tensors with more than 2 dimensions" |
| 966 | valid = True |
| 967 | extra = [] |
| 968 | for tens in (op.ifm, op.ifm2): |
| 969 | # Unary ops have ifm2 as None |
| 970 | if tens is not None: |
| 971 | if (len(tens.shape) > 2) and (tens.shape[0] != 1): |
| 972 | valid = False |
| 973 | extra.append(tens.name) |
| 974 | extra = ", ".join(extra) |
| 975 | return valid, f"Op has invalid input tensors: {extra}" |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 976 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 977 | @staticmethod |
| 978 | def constraint_matching_either_shapes(op): |
| 979 | "At least one Input's shape must match the OFM's shape" |
| 980 | ifm_shape = op.ifm.shape |
| 981 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 982 | ofm_shape = op.ofm.shape |
| 983 | valid = (ifm_shape == ofm_shape) or (ifm2_shape == ofm_shape) |
| 984 | return valid, f"Op has ifm_shape={ifm_shape}, ifm2_shape={ifm2_shape} and ofm_shape={ofm_shape}" |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 985 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 986 | @staticmethod |
Andreas Nevalainen | d059d8b | 2020-11-19 14:40:35 +0100 | [diff] [blame] | 987 | def constraint_broadcast_shapes(op): |
| 988 | "Broadcasting is only allowed for rank indices with dimension 1, from either IFM1 or IFM2" |
| 989 | ifm_shape = op.ifm.shape |
| 990 | ifm2_shape = op.ifm2.shape if op.ifm2 else None |
| 991 | ofm_shape = op.ofm.shape |
| 992 | valid = True |
| 993 | if ifm_shape is not None and ifm2_shape is not None: |
| 994 | # align trailing dimensions |
| 995 | size = min(len(ifm_shape), len(ifm2_shape)) |
| 996 | for i, i2, o in zip(ifm_shape[-size:], ifm2_shape[-size:], ofm_shape[-size:]): |
| 997 | mi = max(i, i2) |
| 998 | # Input dimensions should match or one should be of dimension 1 |
| 999 | # Output dimension should match the largest input dimension, together |
| 1000 | # with constraint_match_either_shapes ensures broadcast from only one input |
| 1001 | if not (i == i2 or i == 1 or i2 == 1) or o != mi: |
| 1002 | valid = False |
| 1003 | break |
| 1004 | |
| 1005 | return valid, f"Op has ifm_shape={ifm_shape} and ifm2_shape={ifm2_shape}" |
| 1006 | |
| 1007 | @staticmethod |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 1008 | def constraint_alpha_valid(op): |
| 1009 | "Alpha must not be negative" |
| 1010 | alpha = op.attrs["alpha"] |
| 1011 | valid = alpha >= 0 |
| 1012 | return valid, f"Op has alpha={alpha}" |
erik.andersson@arm.com | 0cbb166 | 2021-02-22 15:47:07 +0100 | [diff] [blame] | 1013 | |
| 1014 | @staticmethod |
| 1015 | def constraint_keep_dim_ifm_ofm(op): |
| 1016 | "The IFM and OFM must have the same number of dimensions if keep_num_dims is set to true" |
| 1017 | valid = True |
| 1018 | if op.attrs.get("keep_num_dims"): |
| 1019 | valid = len(op.ifm.shape) == len(op.ofm.shape) |
| 1020 | return valid, f"Op has ifm shape={op.ifm.shape} and ofm shape={op.ofm.shape}" |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1021 | |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 1022 | @staticmethod |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1023 | def constraint_mean_input_dims(op): |
| 1024 | "Input tensor must be at least 2D" |
| 1025 | dims = len(op.inputs[0].shape) |
| 1026 | return 2 <= dims <= 4, f"Input is {dims}D" |
| 1027 | |
| 1028 | @staticmethod |
| 1029 | def constraint_mean_axis(op): |
| 1030 | "Axis indices must correspond to height and width axes" |
| 1031 | dims = len(op.inputs[0].shape) |
Dwight Lidman | dec6fbc | 2021-04-28 10:55:46 +0200 | [diff] [blame] | 1032 | axis = int(op.inputs[1].values) if op.inputs[1].shape == [] else list(op.inputs[1].values) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1033 | if dims == 2 or dims == 3: |
Dwight Lidman | dec6fbc | 2021-04-28 10:55:46 +0200 | [diff] [blame] | 1034 | valid = axis in (0, 1, [0], [1], [0, 1], [1, 0]) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1035 | elif dims == 4: |
Dwight Lidman | dec6fbc | 2021-04-28 10:55:46 +0200 | [diff] [blame] | 1036 | valid = axis in (1, 2, [1], [2], [1, 2], [2, 1]) |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1037 | return valid, f"Axis is {axis}" |
| 1038 | |
| 1039 | @classmethod |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 1040 | @docstring_format_args([mean_kernel_product_avgpool]) |
| 1041 | def constraint_mean_height_width_product_avgpool(cls, op): |
| 1042 | """Product of height and width can be at most {}""" |
| 1043 | shape = op.inputs[0].shape |
| 1044 | hi = 0 if len(shape) < 4 else 1 |
| 1045 | h, w = shape[hi : hi + 2] |
| 1046 | max_prod = cls.mean_kernel_product_avgpool |
| 1047 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 1048 | |
| 1049 | @classmethod |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1050 | @docstring_format_args([mean_kernel_product]) |
| 1051 | def constraint_mean_height_width_product(cls, op): |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 1052 | """Product of height and width can be at most {} when IFM and OFM have different scale or zero point, |
| 1053 | or keep_dims is True""" |
| 1054 | ifmq, ofmq = op.ifm.quantization, op.ofm.quantization |
| 1055 | keep_dims = op.attrs.get("keep_dims") |
| 1056 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 1057 | if not keep_dims and ifmq.scale_f32 == ofmq.scale_f32 and ifmq.zero_point == ofmq.zero_point: |
| 1058 | return True, "" |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1059 | shape = op.inputs[0].shape |
| 1060 | hi = 0 if len(shape) < 4 else 1 |
| 1061 | h, w = shape[hi : hi + 2] |
| 1062 | max_prod = cls.mean_kernel_product |
| 1063 | return h * w <= max_prod, f"Product of height and width is {h * w}" |
| 1064 | |
| 1065 | @classmethod |
| 1066 | @docstring_format_args([mean_kernel_product_int8]) |
| 1067 | def constraint_mean_height_width_product_int8(cls, op): |
| 1068 | """Product of IFM height and width can be at most {} when the following are true: |
| 1069 | IFM dimensions are 4, |
| 1070 | Axis indices are 1 and 2, |
| 1071 | keep_dims is set to True and |
| 1072 | IFM datatype is int8""" |
| 1073 | shape = op.ifm.shape |
Dwight Lidman | dec6fbc | 2021-04-28 10:55:46 +0200 | [diff] [blame] | 1074 | axis = int(op.inputs[1].values) if op.inputs[1].shape == [] else list(op.inputs[1].values) |
Dwight Lidman | 95b279f | 2021-03-26 10:53:28 +0100 | [diff] [blame] | 1075 | # doesn't apply, size is checked by constraint_mean_height_width_product_avgpool |
| 1076 | # and constraint_mean_height_width_product |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 1077 | if ( |
| 1078 | len(shape) != 4 |
| 1079 | or op.ifm.dtype != DataType.int8 |
| 1080 | or not op.attrs.get("keep_dims") |
| 1081 | or axis not in ([1, 2], [2, 1]) |
| 1082 | ): |
| 1083 | return True, "" |
| 1084 | hi = 0 if len(shape) < 4 else 1 |
| 1085 | h, w = shape[hi : hi + 2] |
| 1086 | max_prod = cls.mean_kernel_product_int8 |
| 1087 | return h * w <= max_prod, f"Product of height and width is {h * w}" |