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